From 6eb5df592ea21e5b3683458fc6e29173340fce7d Mon Sep 17 00:00:00 2001 From: zzh <838331105@qq.com> Date: Fri, 5 Dec 2025 18:35:10 +0800 Subject: [PATCH] =?UTF-8?q?=20=E4=BF=AE=E5=A4=8D=E5=B8=B8=E4=BA=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.c | 55 +++++++++++++++++++++++++++++++++++++++-- mqtt_utils/mqtt_utils.c | 3 ++- 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/main.c b/main.c index 78b7428..193f1d5 100644 --- a/main.c +++ b/main.c @@ -37,6 +37,7 @@ pthread_t pt_removeduplicatetag; pthread_t pt_keycheck; pthread_t pt_mqtt_status; pthread_t pt_station_heartbeat; +pthread_t pt_simulate_light; uart_utils_t uartSend = {0}; uart_utils_t uartRecvData = {0}; uart_utils_t uartRecvBack = {0}; @@ -72,6 +73,7 @@ int lightbars_size=0; int lightbars_count=0; uint8_t changecolor=0; uint8_t changesound=0; +uint8_t changeflash=3; // 1=常亮, 3=闪烁 uint8_t groupno=0; int fd; int UPCASE=0; @@ -132,6 +134,7 @@ void hmacsha1_hex(char *key, char* data, char *signhex, int signhex_len); void *thread_mqtt_status_check(void *arg); void *thread_station_heartbeat(void *arg); void *thread_heartbeat_check(void *arg); +void *thread_simulate_light(void *arg); void update_lightbar_heartbeat(uint32_t tagCode); void report_lightbar_login(uint32_t tagCode); void report_lightbar_logout(uint32_t tagCode); @@ -286,6 +289,42 @@ void *thread_heartbeat_check(void *arg) { return NULL; } +/*================================================================================*/ +// 模拟亮灯线程函数 - 每30秒发送一次亮灯任务 +void *thread_simulate_light(void *arg){ + LOG_I("thread_simulate_light started\n"); + + char sim_payload[1024] = {0}; + + while(1){ + sleep(30); // 每30秒执行一次 + + // 从/root/payload文件读取内容 + FILE *fp = fopen("/root/payload", "r"); + if(fp == NULL){ + LOG_I("simulate_light: cannot open /root/payload\n"); + continue; + } + + memset(sim_payload, 0, sizeof(sim_payload)); + if(fgets(sim_payload, sizeof(sim_payload), fp) != NULL){ + // 去掉换行符 + int len = strlen(sim_payload); + if(len > 0 && sim_payload[len-1] == '\n'){ + sim_payload[len-1] = '\0'; + } + + LOG_I("simulate_light: putting payload into queue\n"); + LOG_I("payload: %s\n", sim_payload); + + // 直接把payload放入消息队列,由thread_mqtt_recv处理 + PutDataIntoMQueue(sim_payload); + } + fclose(fp); + } + pthread_exit(NULL); +} + /*================================================================================*/ void doCommand_help(int argc, char *argv[]) { @@ -1357,7 +1396,7 @@ void *thread_uart_recv_ack(void *arg){ .s.color=changecolor, .s.sound=changesound, .s.single=0, - .s.flash=3, + .s.flash=changeflash, };//0xC1 11000001 flash高位 jt_led_or_group_package_t group={ .group=groupno, @@ -1910,7 +1949,8 @@ void *thread_mqtt_recv(void *arg){ // 解析Flashing字段(闪烁) int flashing_enable = 0; get_int_from_json_string_by_key(msg_items,"Flashing",&flashing_enable); - LOG_I("Flashing = %d\n", flashing_enable); + changeflash = flashing_enable ? 3 : 1; // 3=闪烁, 1=常亮 + LOG_I("Flashing = %d, changeflash = %d\n", flashing_enable, changeflash); // 解析TagID字段(灯条ID) LOG_I("Before parsing TagID, msg_items = %s\n", msg_items); @@ -2026,6 +2066,7 @@ void *thread_mqtt_recv(void *arg){ isBindTag = false; // P点灯 B绑定 &群控(不支持) *根据id点亮 + // timeout单位是5秒,所以 Time(秒) / 5 = timeout uart_data_send_head(&uartSend, 'P', 5, mqtt_parm.msg_duration/5, lightbars_size); } else { LOG_I("Processing OTA task (task_id: ota)\n"); @@ -2343,6 +2384,16 @@ int main(int argc, char *argv[]) LOG_I("pthread_create heartbeat_check success\n"); pthread_detach(pt_heartbeat_check); } + +#if 1 + ret = pthread_create(&pt_simulate_light,NULL,thread_simulate_light,NULL); + if(ret!=0){ + LOG_I("pthread_create simulate_light fail\n"); + }else{ + LOG_I("pthread_create simulate_light success\n"); + pthread_detach(pt_simulate_light); + } +#endif #if 0 readresult=file_to_buffer("mqttRawPassword",&len); if(readresult!=NULL){ diff --git a/mqtt_utils/mqtt_utils.c b/mqtt_utils/mqtt_utils.c index d89c53f..359c1a1 100644 --- a/mqtt_utils/mqtt_utils.c +++ b/mqtt_utils/mqtt_utils.c @@ -43,7 +43,7 @@ char nativeInvokTopicName[1024] = ""; char nativeUpgradeTopicName[1024] = ""; // 多个订阅topic数组 -char subscribeTopics[5][1024] = {""}; +char subscribeTopics[10][1024] = {""}; int subscribeTopicCount = 0; extern char softwareVersion[16]; typedef struct{ @@ -357,6 +357,7 @@ int mqtt_utils_init(mqtt_utils_t *mqtt_config) sprintf(subscribeTopics[subscribeTopicCount++], "/iot/estation%s/bind", mqtt_conf->username); sprintf(subscribeTopics[subscribeTopicCount++], "/iot/estation%s/group", mqtt_conf->username); sprintf(subscribeTopics[subscribeTopicCount++], "/iot%s/thing/ota/upgrade", mqtt_conf->username); + sprintf(subscribeTopics[subscribeTopicCount++], "/sys/WcSubLightStrip/AD1000014C11/thing/service/lightOperate/invoke"); LOG_I("设置了%d个订阅topic:\n", subscribeTopicCount); for(int i = 0; i < subscribeTopicCount; i++) {