fix: MQTT协议 V1.02 同步 — 字段名缩短 + 时间单位修正
- docs/DLD960_IoT_MQTT协议.md: 字段缩写 + 时间单位 5ms→50ms - vd960DBN/iot_mqtt_srv.c: 6字段缩短对齐协议 (level/iscar/freq/diff/sens/cndtn) - vd960DBN/tcp_json_srv.c: gap_ms5/passtime_ms → gap/passtime - vd960DBN/loop_uart_proto.h: 注释 5ms→50ms - DBNMQTTool/main.py: 模拟样本数据字段同步
This commit is contained in:
@@ -386,72 +386,59 @@ void iot_mqtt_publish_sensor(void) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* Batch-publish: 2 channels per message to keep each under 512 bytes.
|
||||
Full-field JSON (freq_current, freq_diff, condition, misc) per the protocol doc. */
|
||||
static char data_json[1024]; // single buffer for 2-channel JSON
|
||||
static char payload[1024];
|
||||
char *pub_topic = (char *)g_iot_topic.topic_pub;
|
||||
uint8_t batch_start = 0;
|
||||
/* 全通道一次性上报,字段按 DLD960_TCP_JSON协议.md §5.1:
|
||||
ch, freq_level, has_car, loop_ok, freq_current, freq_diff,
|
||||
sensitivity, condition, misc.type, misc.value
|
||||
注意: mqttBuf 需 ≥ 1024 才能容纳 4 通道全字段封包 (~846 字节) */
|
||||
static char data_json[1024];
|
||||
static char payload[1400];
|
||||
char *p = data_json;
|
||||
int remaining = sizeof(data_json);
|
||||
int written;
|
||||
const char *freq_level_names[] = {"high", "mid_high", "mid_low", "low"};
|
||||
uint8_t i;
|
||||
|
||||
while (batch_start < sr.coil_count) {
|
||||
char *p = data_json;
|
||||
int remaining = sizeof(data_json);
|
||||
int written;
|
||||
uint8_t batch_end = batch_start + 2;
|
||||
if (batch_end > sr.coil_count) batch_end = sr.coil_count;
|
||||
written = snprintf(p, remaining, "{\"channels\":[");
|
||||
if (written < 0 || written >= remaining) goto done;
|
||||
p += written; remaining -= written;
|
||||
|
||||
for (i = 0; i < sr.coil_count; i++) {
|
||||
const LUP_CoilSensor *cs = &sr.coils[i];
|
||||
const char *misc_type_str = "time";
|
||||
uint32_t misc_val = 0;
|
||||
|
||||
if (cs->misc_type == 0) { misc_type_str = "time"; misc_val = cs->misc.passtime_ms; }
|
||||
else if (cs->misc_type == 1) { misc_type_str = "cut_count"; misc_val = cs->misc.cut_amount; }
|
||||
else if (cs->misc_type == 2) { misc_type_str = "flow_count"; misc_val = cs->misc.flow_amount; }
|
||||
else if (cs->misc_type == 3) { misc_type_str = "relay_count"; misc_val = cs->misc.relay_count; }
|
||||
|
||||
written = snprintf(p, remaining,
|
||||
"{\"channels\":[");
|
||||
"%s{\"ch\":%d,\"level\":\"%s\",\"iscar\":%s,"
|
||||
"\"loop_ok\":%s,\"freq\":%lu,\"diff\":%d,"
|
||||
"\"sens\":%d,\"cndtn\":%d,"
|
||||
"\"misc\":{\"type\":\"%s\",\"value\":%lu}}",
|
||||
(i > 0) ? "," : "",
|
||||
i + 1,
|
||||
freq_level_names[cs->freq_level],
|
||||
cs->car_state ? "true" : "false",
|
||||
cs->loop_state ? "false" : "true",
|
||||
cs->freq, cs->variation,
|
||||
cs->sensitivity, cs->condition,
|
||||
misc_type_str, misc_val);
|
||||
if (written < 0 || written >= remaining) goto done;
|
||||
p += written; remaining -= written;
|
||||
|
||||
for (i = batch_start; i < batch_end; i++) {
|
||||
const LUP_CoilSensor *cs = &sr.coils[i];
|
||||
const char *misc_type_str = "time";
|
||||
uint32_t misc_val = 0;
|
||||
|
||||
if (cs->misc_type == 0) { misc_type_str = "time"; misc_val = cs->misc.passtime_ms; }
|
||||
else if (cs->misc_type == 1) { misc_type_str = "cut_count"; misc_val = cs->misc.cut_amount; }
|
||||
else if (cs->misc_type == 2) { misc_type_str = "flow_count"; misc_val = cs->misc.flow_amount; }
|
||||
else if (cs->misc_type == 3) { misc_type_str = "relay_count"; misc_val = cs->misc.relay_count; }
|
||||
|
||||
const char *freq_level_names[] = {"high", "mid_high", "mid_low", "low"};
|
||||
|
||||
written = snprintf(p, remaining,
|
||||
"%s{\"ch\":%d,\"freq_level\":\"%s\",\"has_car\":%s,"
|
||||
"\"loop_ok\":%s,\"freq_current\":%lu,\"freq_diff\":%d,"
|
||||
"\"sensitivity\":%d,\"condition\":%d,"
|
||||
"\"misc\":{\"type\":\"%s\",\"value\":%lu}}",
|
||||
(i > batch_start) ? "," : "",
|
||||
i + 1,
|
||||
freq_level_names[cs->freq_level],
|
||||
cs->car_state ? "true" : "false",
|
||||
cs->loop_state ? "false" : "true",
|
||||
cs->freq, cs->variation,
|
||||
cs->sensitivity, cs->condition,
|
||||
misc_type_str, misc_val);
|
||||
if (written < 0 || written >= remaining) goto done;
|
||||
p += written; remaining -= written;
|
||||
}
|
||||
|
||||
snprintf(p, remaining, "]}");
|
||||
|
||||
/* Wrap in outer message and publish */
|
||||
snprintf(payload, sizeof(payload),
|
||||
"{\"msg_id\":%d,\"cmd\":\"loop_data\",\"ts\":%lu,\"data\":%s}",
|
||||
++g_iot_msg_id, mstick() / 1000, data_json);
|
||||
|
||||
uint16_t plen = (uint16_t)strlen(payload);
|
||||
mqtt_publish(pub_topic, payload, 0);
|
||||
|
||||
batch_start = batch_end;
|
||||
}
|
||||
|
||||
done:
|
||||
InitPkgUart(&g_pkg_uart_2);
|
||||
}
|
||||
snprintf(p, remaining, "]}");
|
||||
|
||||
/* Wrap and publish */
|
||||
snprintf(payload, sizeof(payload),
|
||||
"{\"msg_id\":%d,\"cmd\":\"loop_data\",\"ts\":%lu,\"data\":%s}",
|
||||
++g_iot_msg_id, mstick() / 1000, data_json);
|
||||
|
||||
mqtt_publish((char *)g_iot_topic.topic_pub, payload, 0);
|
||||
|
||||
done:
|
||||
InitPkgUart(&g_pkg_uart_2);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user