feat(vd960DBN): MQTT/TCP report_config 支持完整7参数配置

- 新增 ReportConfig 结构体 (sensor_type/enable/once/env_eval/interval/ack_required/timeout)
- net_srv.c: MQTT report_config handler — 查询/设置均返回完整配置JSON
- tcp_json_srv.c: TCP JSON handle_report_config 同步升级,兼容旧 active_report
- iot_mqtt_srv.c: g_report_active → g_report_cfg.enable
- g_report_active 全局变量替换为 g_report_cfg 结构体
This commit is contained in:
wangfq
2026-07-07 18:39:45 +08:00
parent cd92f3e0c3
commit a7de4dfcde
4 changed files with 162 additions and 26 deletions
@@ -31,7 +31,7 @@ uint32_t g_json_auth_timer = 0;
uint8_t g_json_pwd_retry = 0;
uint32_t g_json_lockout_timer = 0;
TcpJsonPending g_json_pending = {0, 0, 0, "", 0};
uint8_t g_report_active = 0; // 0=不主动上报, 1=主动上报传感器数据
ReportConfig g_report_cfg = {0}; // 上电默认关闭上报
uint32_t g_json_last_comm_ts = 0; // 最后通信时间戳
uint32_t g_json_last_connect_ts = 0; // 最后连接时间戳
uint8_t g_json_restart_count = 0; // 自动重启计数
@@ -555,29 +555,85 @@ static void handle_loop_param_query(uint8_t socket, uint32_t msg_id, const char
PRINT("JSON: loop_param_query sent\n");
}
/* 4.15 report_config — 配置主动上报开关 */
/* 4.15 report_config — 配置主动上报参数 */
static void handle_report_config(uint8_t socket, uint32_t msg_id, const char *json) {
char *data = json_get_data_str(json);
if (!data) {
json_send_error(socket, msg_id, "report_config", JSON_CODE_PARAM_ERR, "missing data");
return;
}
if (data) {
reset_tmp();
// Parse active_report flag
reset_tmp();
simple_parse_json(data, "\"active_report\"", g_tmp_value);
if (strlen(g_tmp_value) > 0) {
if (strstr(g_tmp_value, "true")) {
g_report_active = 1;
} else {
g_report_active = 0;
// sensor_type (int)
simple_parse_json(data, "\"sensor_type\"", g_tmp_value);
if (strlen(g_tmp_value) > 0) {
g_report_cfg.sensor_type = (uint8_t)strtoul(g_tmp_value, NULL, 10);
}
}
free(data);
json_send_ok(socket, msg_id, "report_config",
g_report_active ? "{\"active_report\":true}" : "{\"active_report\":false}");
PRINT("JSON: report_config active_report=%d\n", g_report_active);
// enable (bool)
memset(g_tmp_value, 0, sizeof(g_tmp_value));
simple_parse_json(data, "\"enable\"", g_tmp_value);
if (strlen(g_tmp_value) > 0) {
g_report_cfg.enable = (uint8_t)(strstr(g_tmp_value, "true") ? 1 : 0);
}
// once (bool)
memset(g_tmp_value, 0, sizeof(g_tmp_value));
simple_parse_json(data, "\"once\"", g_tmp_value);
if (strlen(g_tmp_value) > 0) {
g_report_cfg.once = (uint8_t)(strstr(g_tmp_value, "true") ? 1 : 0);
}
// env_eval (bool)
memset(g_tmp_value, 0, sizeof(g_tmp_value));
simple_parse_json(data, "\"env_eval\"", g_tmp_value);
if (strlen(g_tmp_value) > 0) {
g_report_cfg.env_eval = (uint8_t)(strstr(g_tmp_value, "true") ? 1 : 0);
}
// interval (int)
memset(g_tmp_value, 0, sizeof(g_tmp_value));
simple_parse_json(data, "\"interval\"", g_tmp_value);
if (strlen(g_tmp_value) > 0) {
g_report_cfg.interval = (uint16_t)strtoul(g_tmp_value, NULL, 10);
}
// ack_required (bool)
memset(g_tmp_value, 0, sizeof(g_tmp_value));
simple_parse_json(data, "\"ack_required\"", g_tmp_value);
if (strlen(g_tmp_value) > 0) {
g_report_cfg.ack_required = (uint8_t)(strstr(g_tmp_value, "true") ? 1 : 0);
}
// timeout (int)
memset(g_tmp_value, 0, sizeof(g_tmp_value));
simple_parse_json(data, "\"timeout\"", g_tmp_value);
if (strlen(g_tmp_value) > 0) {
g_report_cfg.timeout = (uint16_t)strtoul(g_tmp_value, NULL, 10);
}
// 兼容旧协议 active_report
memset(g_tmp_value, 0, sizeof(g_tmp_value));
simple_parse_json(data, "\"active_report\"", g_tmp_value);
if (strlen(g_tmp_value) > 0) {
g_report_cfg.enable = (uint8_t)(strstr(g_tmp_value, "true") ? 1 : 0);
}
free(data);
}
char data_json[256];
snprintf(data_json, sizeof(data_json),
"{\"sensor_type\":%d,\"enable\":%s,\"once\":%s,\"env_eval\":%s,"
"\"interval\":%u,\"ack_required\":%s,\"timeout\":%u}",
g_report_cfg.sensor_type,
g_report_cfg.enable ? "true" : "false",
g_report_cfg.once ? "true" : "false",
g_report_cfg.env_eval ? "true" : "false",
g_report_cfg.interval,
g_report_cfg.ack_required ? "true" : "false",
g_report_cfg.timeout);
json_send_ok(socket, msg_id, "report_config", data_json);
PRINT("JSON: report_config enable=%d interval=%u sensor=%d\\n",
g_report_cfg.enable, g_report_cfg.interval, g_report_cfg.sensor_type);
}
/* 4.16 loop_version_query — 获取地感MCU版本号 (CMD 0x4A) */
@@ -758,12 +814,12 @@ static int format_loop_param_json(char *buf, uint16_t buf_size, const LUP_ParamG
static void json_sensor_callback(const uint8_t *pkg, uint16_t len)
{
PRINT("JSON: sensor_cb enter socket=%d auth=%d report=%d\n",
g_json_socket_listen, g_json_auth_state, g_report_active);
g_json_socket_listen, g_json_auth_state, g_report_cfg.enable);
// Check: socket active, authed, report enabled
if (g_json_socket_listen == 0xFF) { PRINT("JSON: sensor_cb skip: no socket\n"); return; }
if (g_json_auth_state != JSON_STATE_AUTHED) { PRINT("JSON: sensor_cb skip: not authed\n"); return; }
if (!g_report_active) { PRINT("JSON: sensor_cb skip: report disabled\n"); return; }
if (!g_report_cfg.enable) { PRINT("JSON: sensor_cb skip: report disabled\\n"); return; }
LUP_SensorReport sr;
memset(&sr, 0, sizeof(sr));
@@ -1247,7 +1303,7 @@ void tcp_json_push_sensor(void) {
// Check: socket active, authed, report enabled
if (g_json_socket_listen == 0xFF) return;
if (g_json_auth_state != JSON_STATE_AUTHED) return;
if (!g_report_active) return; // 主动上报未开启
if (!g_report_cfg.enable) return; // 主动上报未开启
// Only proceed if g_pkg_uart_2 has data and it's a 0xC0 sensor report
if (g_pkg_uart_2.flag == 0) return;