From 255ebf0855d0c47b2f929287544586e102f5f431 Mon Sep 17 00:00:00 2001 From: zzh Date: Wed, 11 Jun 2025 15:45:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=82=B9=E4=BA=AE=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=EF=BC=8C=E5=88=86=E6=89=B9=E7=82=B9=E4=BA=AE=E6=94=B9?= =?UTF-8?q?=E5=B9=BF=E6=92=AD=E7=82=B9=E4=BA=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.c | 87 ++++++++++++++++++++++++++-------------------------------- 1 file changed, 39 insertions(+), 48 deletions(-) diff --git a/main.c b/main.c index 5cc891b..e13dee5 100644 --- a/main.c +++ b/main.c @@ -1768,63 +1768,47 @@ void *thread_mqtt_recv(void *arg){ mqtt_service_reply(stationsn,mqtt_parm.msg_messageId,"labelconfig is empty",0,productid); } }else if(strcmp(mqtt_parm.msg_type,"3027")==0){ - LOG_I("3027 All light on/off\n"); - get_string_from_json_string_by_key_unescape(msg_data, "scene", mqtt_parm.msg_scene, sizeof(mqtt_parm.msg_scene)); - LOG_I("scene:%s\n",mqtt_parm.msg_scene); - - get_string_from_json_string_by_key_unescape(msg_data, "lights", mqtt_parm.msg_lights, sizeof(mqtt_parm.msg_lights)); - LOG_I("lights:%s\n",mqtt_parm.msg_lights); - - get_size_from_json_string_arry_by_key(msg_data, "lights", &mqtt_parm.msg_opNumber); - LOG_I("lights size = %d\n", mqtt_parm.msg_opNumber); - lightsnNum=mqtt_parm.msg_opNumber; - for(int i = 0; i < mqtt_parm.msg_opNumber; i++){ - get_string_from_json_string_arry_by_key(msg_data,"lights",msg_lightsn_value,128,i); - snprintf(msg_lightsn, sizeof(msg_lightsn), "{%s}", msg_lightsn_value); - get_string_from_json_string_by_key_unescape(msg_lightsn, "sn", mqtt_parm.msg_sn,sizeof(mqtt_parm.msg_sn)); - LOG_I("sn:%s\n",mqtt_parm.msg_sn); - if(i==0){ - memset(lightsn1,0,9); - memcpy(lightsn1,mqtt_parm.msg_sn,strlen(mqtt_parm.msg_sn)); - }else if(i==1){ - memset(lightsn2,0,9); - memcpy(lightsn2,mqtt_parm.msg_sn,strlen(mqtt_parm.msg_sn)); - }else if(i==2){ - memset(lightsn3,0,9); - memcpy(lightsn3,mqtt_parm.msg_sn,strlen(mqtt_parm.msg_sn)); - }else if(i==3){ - memset(lightsn4,0,9); - memcpy(lightsn4,mqtt_parm.msg_sn,strlen(mqtt_parm.msg_sn)); - }else if(i==4){ - memset(lightsn5,0,9); - memcpy(lightsn5,mqtt_parm.msg_sn,strlen(mqtt_parm.msg_sn)); - } - } - + LOG_I("3027 All light on/off 广播点亮\n"); + get_string_from_json_string_by_key_unescape(msg_data, "color", mqtt_parm.msg_color, sizeof(mqtt_parm.msg_color)); LOG_I("color:%s\n",mqtt_parm.msg_color); - + get_string_from_json_string_by_key_unescape(msg_data, "sound", mqtt_parm.msg_sound, sizeof(mqtt_parm.msg_sound)); + strcpy(mqtt_parm.msg_sound, "0"); // 默认关闭声音 LOG_I("sound:%s\n",mqtt_parm.msg_sound); - //strcpy(mqtt_parm.msg_sound, "0"); - + get_string_from_json_string_by_key_unescape(msg_data, "flash", mqtt_parm.msg_flash, sizeof(mqtt_parm.msg_flash)); - //strcpy(mqtt_parm.msg_flash, "0"); + strcpy(mqtt_parm.msg_flash, "0"); // 默认关闭闪烁 LOG_I("flash:%s\n",mqtt_parm.msg_flash); - - get_string_from_json_string_by_key_unescape(msg_data, "flashInterval", mqtt_parm.msg_flashInterval, - sizeof(mqtt_parm.msg_flashInterval)); + + get_string_from_json_string_by_key_unescape(msg_data, "flashInterval", mqtt_parm.msg_flashInterval, sizeof(mqtt_parm.msg_flashInterval)); LOG_I("flashInterval:%s\n",mqtt_parm.msg_flashInterval); - - get_string_from_json_string_by_key_unescape(msg_data, "lightDuration", mqtt_parm.msg_lightDuration, - sizeof(mqtt_parm.msg_lightDuration)); - strcpy(mqtt_parm.msg_lightDuration, "60"); + + get_string_from_json_string_by_key_unescape(msg_data, "lightDuration", mqtt_parm.msg_lightDuration, sizeof(mqtt_parm.msg_lightDuration)); LOG_I("lightDuration:%s\n",mqtt_parm.msg_lightDuration); - if(isLightEnable && (mqtt_parm.msg_opNumber>0)){ + if(isLightEnable){ mqtt_service_reply(stationsn,mqtt_parm.msg_messageId,"ok",1,productid); - isSendComEnd=false; + + // 先搜索灯带 + LOG_I("Searching for lights...\n"); isLightOn=false; + isLightOnByRule=false; + isLabelUp=false; + searchTimeOut=120; // 设置搜索超时时间为120秒 + LOG_I("searchTimeOut:%d\n",searchTimeOut); + searchTimeOld=getCurrentTime(); + LOG_I("searchTimeOld:%d\n",searchTimeOld); + isSearchLabelOn=true; + + // 等待搜索完成 + while(isSearchLabelOn && (getCurrentTime() - searchTimeOld) < searchTimeOut) { + usleep(100000); // 休眠100ms + } + + // 设置规则并广播点亮 + isSendComEnd=false; + isLightOn=true; isLightOnByRule=true; // 使用规则控制 isLabelUp=false; @@ -1835,9 +1819,16 @@ void *thread_mqtt_recv(void *arg){ strcpy(mqtt_parm.msg_label3Rule, "03"); // ≠ 不等于 strcpy(mqtt_parm.msg_label3, "386cd300"); - uart_data_send_head_lightonrule(&uartSend,5); + // 广播点亮命令 + LOG_I("Broadcasting light on command\n"); + uart_data_send_head_lightonrule(&uartSend, 5); + + // 再次广播,提升可靠性 + usleep(500000); // 等待500ms + LOG_I("Sending second broadcast\n"); + uart_data_send_head_lightonrule(&uartSend, 5); }else{ - mqtt_service_reply(stationsn,mqtt_parm.msg_messageId,"light disabled or sn is null",0,productid); + mqtt_service_reply(stationsn,mqtt_parm.msg_messageId,"light disabled",0,productid); } } }