update HTML and http_test_request

This commit is contained in:
CSSC-WORK\murmur 2024-12-30 16:11:43 +08:00
parent b7cded5e58
commit 128b050fa8
4 changed files with 203 additions and 36 deletions

View File

@ -198,11 +198,6 @@ R"(<!DOCTYPE html>
gap: 20px;
}
.device-group {
flex: 1;
min-width: 300px;
}
.device-row {
display: flex;
gap: 20px;
@ -224,6 +219,20 @@ R"(<!DOCTYPE html>
width: 100%;
}
/* 添加急停状态的特殊样式 */
.status-item.emergency-stop.success {
background-color: #28a745;
}
.status-item.emergency-stop.warning {
background-color: yellow;
}
.status-item.emergency-stop.error {
background-color: #dc3545;
}
</style>
<script>
// 参数配置模板
@ -234,8 +243,8 @@ const motorParamFields = [
{ name: 'accel', label: '加速度', type: 'number' },
{ name: 'decel', label: '减速度', type: 'number' },
{ name: 'fullCount', label: '总步数', type: 'number' },
{ name: 'offsetPos', label: '偏移位置', type: 'number' },
{ name: 'speedPercent', label: '速度百分比(%)', type: 'number', min: 0, max: 100 },
{ name: 'offsetPos', label: '偏移补偿', type: 'number' },
{ name: 'speedPercent', label: '速度(%)', type: 'number', min: 0, max: 100 },
{ name: 'torque', label: '堵转力矩(%)', type: 'number', min: 0, max: 100 },
{ name: 'timeout', label: '超时时间(秒)', type: 'number' }
];
@ -324,7 +333,99 @@ document.addEventListener('DOMContentLoaded', function() {
})
.catch(error => {
console.error('加载设备参数失败:', error);
// 获取所有泵和阀的输入框
const allInputs = document.querySelectorAll('input[name^="pump"], input[name^="valve"]');
// 将所有输入框的值设置为 # 并添加错误样式
allInputs.forEach(input => {
input.value = '-1';
input.classList.add('error');
});
});
function updateDeviceStatus() {
fetch('/status')
.then(response => {
return response.json();
})
.then(data => {
const deviceElement = document.getElementById('device-status');
deviceElement.textContent = data.deviceStatus ? '正常' : '异常';
deviceElement.className = data.deviceStatus ? 'success' : 'error';
const initStatus = ['等待', '成功', '失败'];
const initElement = document.getElementById('init-status');
initElement.textContent = initStatus[data.initStatus];
initElement.className = data.initStatus === 0 ? 'warning' : (data.initStatus === 1 ? 'success' : 'error');
const errorElement = document.getElementById('error-status');
errorElement.textContent = data.errorCode ? `Error: ${data.errorCode}` : '正常';
errorElement.className = data.errorCode ? 'error' : 'success';
const stopElement = document.getElementById('stop-status');
stopElement.textContent = data.stopStatus ? '停止' : '正常';
stopElement.className = data.stopStatus ? 'error' : 'success';
document.getElementById('valve1-angle').textContent = `${data.valves.angle1}°`;
document.getElementById('valve2-angle').textContent = `${data.valves.angle2}°`;
document.getElementById('valve1-angle').className = 'success';
document.getElementById('valve2-angle').className = 'success';
const pumpStatus = ['停止', '顺时针', '逆时针'];
document.getElementById('pump1-status').textContent =
`${pumpStatus[data.pumps.status1] || '#'} (${data.pumps.speed1}%)`;
document.getElementById('pump2-status').textContent =
`${pumpStatus[data.pumps.status2] || '#'} (${data.pumps.speed2}%)`;
document.getElementById('pump1-status').className = 'success';
document.getElementById('pump2-status').className = 'success';
const bubbleElement = document.getElementById('bubble-status');
bubbleElement.textContent = data.bubbleStatus ? '有' : '无';
bubbleElement.className = data.bubbleStatus ? 'error' : 'success';
})
.catch(error => {
console.error('Status update failed:', error);
// 修改状态值更新逻辑
const statusElements = {
'device-status': '#',
'init-status': '#',
'error-status': '#',
'stop-status': '#',
'valve1-angle': '#',
'valve2-angle': '#',
'pump1-status': '#',
'pump2-status': '#',
'bubble-status': '#'
};
// 更新所有状态值和样式
Object.entries(statusElements).forEach(([id, value]) => {
const element = document.getElementById(id);
if (element) {
element.textContent = value;
// 找到父级的 status-value 元素并应用样式
const statusValue = element.closest('.status-value');
if (statusValue) {
statusValue.className = 'status-value error';
} else {
element.className = 'error';
}
}
});
const emergencyStopElement = document.querySelector('.status-item.emergency-stop');
if (emergencyStopElement) {
emergencyStopElement.classList.remove('success', 'warning', 'error');
emergencyStopElement.classList.add('warning');
}
});
}
setInterval(updateDeviceStatus, 1000);
});
// 如果需要动态添加新的泵或阀,可以调用:
@ -384,12 +485,8 @@ document.addEventListener('DOMContentLoaded', function() {
</div>
</div>
<div class='status-item'>
<div class='status-item emergency-stop'>
<h3>急停</h3>
<div class='status-value'>
<span class='status-label'>状态</span>
<span id='stop-status'>#</span>
</div>
</div>
</div>
@ -411,12 +508,20 @@ document.addEventListener('DOMContentLoaded', function() {
<h2>设备参数设置</h2>
<form method='post' action='/device-params'>
<div class='device-params'>
<div class='device-group' id='pump-group'>
<h3>泵参数设置</h3>
<div class='device-row' id='pump-container'>
<!-- 泵参数将由JS动态生成 -->
<h3>传感器参数设置</h3>
<div class='form-group'>
<label>IP地址:</label>
<input type='text' name='ip' value='#.#.#.#'>
</div>
</div>
<div class='form-group'>
<label>路径:</label>
<input type='text' name='addr' value='#.#.#.#'>
</div>
</div>
<div class='device-group' id='valve-group'>
<h3>阀参数设置</h3>
@ -424,6 +529,13 @@ document.addEventListener('DOMContentLoaded', function() {
<!-- 阀参数将由JS动态生成 -->
</div>
</div>
<div class='device-group' id='pump-group'>
<h3>泵参数设置</h3>
<div class='device-row' id='pump-container'>
<!-- 泵参数将由JS动态生成 -->
</div>
</div>
</div>
<input type='submit' value='保存设备参数'>
</form>
@ -432,22 +544,10 @@ document.addEventListener('DOMContentLoaded', function() {
<div class='network-config'>
<h2>参数设置</h2>
<form method='post' action='/config'>
<div class='form-group'>
<label>MAC地址:</label>
<input type='text' name='mac' value='#:#:#:#:#:#'>
</div>
<div class='form-group'>
<label>IP地址:</label>
<input type='text' name='ip' value='#.#.#.#'>
</div>
<div class='form-group'>
<label>子网地址:</label>
<input type='text' name='subnet' value='#.#.#.#'>
</div>
<div class='form-group'>
<label>网关地址:</label>
<input type='text' name='gateway' value='#.#.#.#'>
</div>
<input type='submit' value='保存配置'>
</form>
</div>

View File

@ -6,7 +6,9 @@ extern "C" {
#endif
#include "http_client.h"
#include "protocol.h"
#include <stdio.h>
#include <string.h>
// HTTP GET测试函数
void http_test_request(void);

View File

@ -1,21 +1,86 @@
#include "http_test.h"
#ifdef CJSON
void http_test_request(void)
{
http_response response;
// 目标IP地址169.254.1.1
uint8_t server_ip[4] = {169, 254, 1, 1};
const char* path = "/status.xml";
// 发送GET请求
if(http_get_ip(server_ip, 80, path, &response) == HTTP_OK)
if(http_get_ip(dp.sensor.IP, 80, dp.sensor.path, &response) == HTTP_OK)
{
// 处理响应数据
response.buffer[response.length] = '\0'; // 确保字符串结束
printf("Response: %s\n", response.buffer);
// 解析JSON响应
cJSON *json = cJSON_Parse(response.buffer);
if (json != NULL) {
// 获取'a'字段
cJSON *activity = cJSON_GetObjectItem(json, "a");
if (activity != NULL && activity->valuestring != NULL) {
float value;
char unit[10];
if (sscanf(activity->valuestring, "%f %s", &value, unit) == 2) {
// 转换单位如果是uCi转换为mCi
if (strcmp(unit, "uCi") == 0) {
value /= 1000.0f;
printf("Activity: %.3f mCi\n", value);
}
}
}
cJSON_Delete(json);
}
else
{
printf("JSON parsing failed\n");
}
}
else
{
printf("HTTP request failed\n");
}
}
}
#else
void http_test_request(void)
{
http_response response;
if(http_get_ip(dp.sensor.IP, 80, dp.sensor.path, &response) == HTTP_OK)
{
response.buffer[response.length] = '\0'; // 确保字符串结束
// 查找"a":"字段
char *activity_start = strstr(response.buffer, "\"a\":\"");
if (activity_start != NULL) {
activity_start += 5; // 跳过"a":"
// 查找值的结束位置
char *activity_end = strchr(activity_start, '\"');
if (activity_end != NULL) {
// 临时存储活度值字符串
char activity_str[20] = {0};
int len = activity_end - activity_start;
if (len < sizeof(activity_str)) {
strncpy(activity_str, activity_start, len);
float value;
char unit[10];
if (sscanf(activity_str, "%f %s", &value, unit) == 2) {
// 如果单位是uCi转换为mCi
if (strcmp(unit, "uCi") == 0) {
value *= 1000.0f;
printf("Activity: %.3f mCi\n", value);
}
}
}
}
}
}
else
{
printf("HTTP request failed\n");
}
}
#endif

@ -1 +1 @@
Subproject commit 13781723e047b8df30eb4f459fd304ef21f042b2
Subproject commit 6dcc87aa594ab9151e33bf1a6c20ba27e5fdf909