mirror of
http://180.163.74.83:13000/zhangzhenghao/AP05.git
synced 2025-12-12 15:04:29 +00:00
完成亮灯合包的功能,增加mqtt消息返回接口,解决一个主线程cpu会占用100%的问题
This commit is contained in:
parent
d36a67e979
commit
876f563492
165
main.c
165
main.c
@ -59,7 +59,7 @@ int fd;
|
||||
int UPCASE=0;
|
||||
int count_value=0;
|
||||
int getPayloadTime=120*1000;//usecond
|
||||
char softwareVersion[16]="1.1.18";
|
||||
char softwareVersion[16]="1.1.19";
|
||||
char stationsn[16]="TJ251372224247";//TJ250995217957
|
||||
char productid[8]="10045";
|
||||
char appKey[32]="fdhQmhqhvbL1cf1K9mUqt";
|
||||
@ -111,7 +111,7 @@ static time_t last_2323_time = 0; // 记录上次收到0x2323的时间
|
||||
static bool waiting_for_4646 = false; // 是否在等待0x4646
|
||||
|
||||
// ====== 3015合并点亮相关全局变量 ======
|
||||
#define MAX_LIGHTSN_BUFFER 30
|
||||
#define MAX_LIGHTSN_BUFFER 4096
|
||||
static char lightsn_buffer[MAX_LIGHTSN_BUFFER][32] = {0};
|
||||
static char color_buffer[MAX_LIGHTSN_BUFFER][8] = {0}; // 颜色缓冲区,与sn一一对应
|
||||
static char sound_buffer[MAX_LIGHTSN_BUFFER][8] = {0}; // 声音缓冲区
|
||||
@ -1390,6 +1390,7 @@ void *thread_uart_recv_ack(void *arg){
|
||||
// 记录收到0x2323的时间,并设置等待0x4646标志
|
||||
time(&last_2323_time);
|
||||
waiting_for_4646 = true;
|
||||
#if 0
|
||||
if(isLightOn){
|
||||
uint32_t tags[30] = {0};
|
||||
uint8_t tag_leds[30] = {0};
|
||||
@ -1468,6 +1469,7 @@ void *thread_uart_recv_ack(void *arg){
|
||||
memset(flash_buffer, 0, sizeof(flash_buffer));
|
||||
pthread_mutex_unlock(&lightsn_buffer_mutex);
|
||||
}
|
||||
#endif
|
||||
#if 0
|
||||
if(isSearchLabel){
|
||||
uart_data_send_search(&uartSend,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF);
|
||||
@ -1658,9 +1660,16 @@ void light_off_tag_report(uint32_t name,uint16_t batteryv){
|
||||
json_object_object_add(light, "sn", json_object_new_string(tagName));
|
||||
json_object_object_add(light, "battery", json_object_new_string(battery));
|
||||
json_object_array_add(lights,light);
|
||||
|
||||
json_object *lights_2 = json_object_new_array();
|
||||
json_object *light_2 = json_object_new_object();
|
||||
json_object_object_add(light_2, "sn", json_object_new_string(tagName));
|
||||
json_object_object_add(light_2, "battery", json_object_new_string(battery));
|
||||
json_object_array_add(lights_2, light_2);
|
||||
char myid[32]={0};
|
||||
myrand(myid,19);
|
||||
mqtt_server_light_status_report(stationsn,myid,"2",lights);
|
||||
//mqtt_server_light_status_report_2(stationsn,myid,"2",lights_2);
|
||||
}
|
||||
|
||||
void search_tag_report(){
|
||||
@ -1964,7 +1973,7 @@ void *thread_mqtt_recv(void *arg){
|
||||
get_string_from_json_string_by_key_unescape(msg_lightsn, "color", color_buffer[lightsn_buffer_count], 8);
|
||||
get_string_from_json_string_by_key_unescape(msg_lightsn, "sound", sound_buffer[lightsn_buffer_count], 8);
|
||||
get_string_from_json_string_by_key_unescape(msg_lightsn, "flash", flash_buffer[lightsn_buffer_count], 8);
|
||||
// 如果没有单独字段,使用全局默认
|
||||
|
||||
if (strlen(color_buffer[lightsn_buffer_count]) == 0) strcpy(color_buffer[lightsn_buffer_count], mqtt_parm.msg_color);
|
||||
if (strlen(sound_buffer[lightsn_buffer_count]) == 0) strcpy(sound_buffer[lightsn_buffer_count], mqtt_parm.msg_sound);
|
||||
if (strlen(flash_buffer[lightsn_buffer_count]) == 0) strcpy(flash_buffer[lightsn_buffer_count], mqtt_parm.msg_flash);
|
||||
@ -2244,6 +2253,7 @@ void updateStationInfo(char *msg_id){
|
||||
calculateStartUpTime(startTime);
|
||||
LOG_I("Time:%s\n",startTime);
|
||||
mqtt_server_station_status_report(msg_id,productid,stationsn,local_ip,softwareVersion,"1",startTime);
|
||||
mqtt_server_station_status_report_test(msg_id,productid,stationsn,local_ip,softwareVersion,"1",startTime);
|
||||
}
|
||||
|
||||
bool timeNew(){
|
||||
@ -2536,7 +2546,25 @@ int main(int argc, char *argv[])
|
||||
#if 1
|
||||
while(1)
|
||||
{
|
||||
fgets(command_buffer, sizeof(command_buffer), stdin);
|
||||
// 先检查标准输入是否可用
|
||||
if (feof(stdin) || ferror(stdin)) {
|
||||
LOG_I("stdin error or EOF, sleeping for 1 second\n");
|
||||
sleep(1);
|
||||
continue;
|
||||
}
|
||||
|
||||
memset(command_buffer, 0, sizeof(command_buffer));
|
||||
if (fgets(command_buffer, sizeof(command_buffer), stdin) == NULL) {
|
||||
usleep(100000);
|
||||
continue;
|
||||
}
|
||||
|
||||
command_buffer[strcspn(command_buffer, "\n")] = 0;
|
||||
|
||||
if (strlen(command_buffer) == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if(execute_command(command_buffer, (command_t *)&__start_command, (&__stop_command - &__start_command)/2) < 0)
|
||||
{
|
||||
LOG_I("unsupport command!\r\n");
|
||||
@ -2577,58 +2605,89 @@ void *thread_3015_lighton_merge(void *arg) {
|
||||
pthread_mutex_lock(&lightsn_buffer_mutex);
|
||||
if (is_3015_collecting) {
|
||||
time_t now = time(NULL);
|
||||
if ((now - first_3015_time >= 5) || (lightsn_buffer_count >= MAX_LIGHTSN_BUFFER)) {
|
||||
int i;
|
||||
lightsnNum = lightsn_buffer_count;
|
||||
for (i = 0; i < lightsn_buffer_count; i++) {
|
||||
switch(i) {
|
||||
case 0: memcpy(lightsn1, lightsn_buffer[i], 32); break;
|
||||
case 1: memcpy(lightsn2, lightsn_buffer[i], 32); break;
|
||||
case 2: memcpy(lightsn3, lightsn_buffer[i], 32); break;
|
||||
case 3: memcpy(lightsn4, lightsn_buffer[i], 32); break;
|
||||
case 4: memcpy(lightsn5, lightsn_buffer[i], 32); break;
|
||||
case 5: memcpy(lightsn6, lightsn_buffer[i], 32); break;
|
||||
case 6: memcpy(lightsn7, lightsn_buffer[i], 32); break;
|
||||
case 7: memcpy(lightsn8, lightsn_buffer[i], 32); break;
|
||||
case 8: memcpy(lightsn9, lightsn_buffer[i], 32); break;
|
||||
case 9: memcpy(lightsn10, lightsn_buffer[i], 32); break;
|
||||
case 10: memcpy(lightsn11, lightsn_buffer[i], 32); break;
|
||||
case 11: memcpy(lightsn12, lightsn_buffer[i], 32); break;
|
||||
case 12: memcpy(lightsn13, lightsn_buffer[i], 32); break;
|
||||
case 13: memcpy(lightsn14, lightsn_buffer[i], 32); break;
|
||||
case 14: memcpy(lightsn15, lightsn_buffer[i], 32); break;
|
||||
case 15: memcpy(lightsn16, lightsn_buffer[i], 32); break;
|
||||
case 16: memcpy(lightsn17, lightsn_buffer[i], 32); break;
|
||||
case 17: memcpy(lightsn18, lightsn_buffer[i], 32); break;
|
||||
case 18: memcpy(lightsn19, lightsn_buffer[i], 32); break;
|
||||
case 19: memcpy(lightsn20, lightsn_buffer[i], 32); break;
|
||||
case 20: memcpy(lightsn21, lightsn_buffer[i], 32); break;
|
||||
case 21: memcpy(lightsn22, lightsn_buffer[i], 32); break;
|
||||
case 22: memcpy(lightsn23, lightsn_buffer[i], 32); break;
|
||||
case 23: memcpy(lightsn24, lightsn_buffer[i], 32); break;
|
||||
case 24: memcpy(lightsn25, lightsn_buffer[i], 32); break;
|
||||
case 25: memcpy(lightsn26, lightsn_buffer[i], 32); break;
|
||||
case 26: memcpy(lightsn27, lightsn_buffer[i], 32); break;
|
||||
case 27: memcpy(lightsn28, lightsn_buffer[i], 32); break;
|
||||
case 28: memcpy(lightsn29, lightsn_buffer[i], 32); break;
|
||||
case 29: memcpy(lightsn30, lightsn_buffer[i], 32); break;
|
||||
if ((now - first_3015_time >= 3) || (lightsn_buffer_count >= MAX_LIGHTSN_BUFFER)) {
|
||||
int total = lightsn_buffer_count;
|
||||
int batch_size = 10;
|
||||
int sent = 0;
|
||||
while (sent < total) {
|
||||
int this_batch = (total - sent > batch_size) ? batch_size : (total - sent);
|
||||
if (isLightEnable && this_batch > 0) {
|
||||
isLightOn = true;
|
||||
isLightOnByRule = false;
|
||||
isLabelUp = false;
|
||||
uint32_t tags[10] = {0};
|
||||
uint8_t tag_leds[10] = {0};
|
||||
for (int i = 0; i < this_batch; i++) {
|
||||
LOG_I("sn[%d]=%s\n", sent + i + 1, lightsn_buffer[sent + i]);
|
||||
// 1. SN转MAC(HEX字符串转uint32_t)
|
||||
if (strlen(lightsn_buffer[sent + i]) == 8) {
|
||||
tags[i] = (uint32_t)strtoul(lightsn_buffer[sent + i], NULL, 16);
|
||||
} else {
|
||||
LOG_I("[WARN] tag[%d] sn格式非法: %s, 跳过", sent + i, lightsn_buffer[sent + i]);
|
||||
continue;
|
||||
}
|
||||
// 2. 颜色+闪光+声音合成LED控制字节(协议格式)
|
||||
int flash = (strlen(flash_buffer[sent + i]) > 0) ? atoi(flash_buffer[sent + i]) : atoi(mqtt_parm.msg_flash); // "1"=闪光, "0"=常亮
|
||||
int sound = (strlen(sound_buffer[sent + i]) > 0) ? atoi(sound_buffer[sent + i]) : atoi(mqtt_parm.msg_sound); // "1"=有声, "0"=无声
|
||||
int changecolor = 0;
|
||||
if(strcmp(color_buffer[sent + i],"1")==0){
|
||||
changecolor=4;
|
||||
}else if(strcmp(color_buffer[sent + i],"2")==0){
|
||||
changecolor=6;
|
||||
}else if(strcmp(color_buffer[sent + i],"3")==0){
|
||||
changecolor=1;
|
||||
}else if(strcmp(color_buffer[sent + i],"4")==0){
|
||||
changecolor=2;
|
||||
}else if(strcmp(color_buffer[sent + i],"5")==0){
|
||||
changecolor=3;
|
||||
}else if(strcmp(color_buffer[sent + i],"6")==0){
|
||||
changecolor=7;
|
||||
}else if(strcmp(color_buffer[sent + i],"7")==0){
|
||||
changecolor=5;
|
||||
}else if(strcmp(color_buffer[sent + i],"8")==0){
|
||||
changecolor=0;
|
||||
}
|
||||
uint8_t led_val = 0;
|
||||
if (flash) led_val |= 0x40; // Bit6
|
||||
if (sound) led_val |= 0x10; // Bit4
|
||||
led_val |= (changecolor & 0x0F); // 低4位为颜色
|
||||
tag_leds[i] = led_val;
|
||||
}
|
||||
uint8_t changecolor=0;
|
||||
if(strcmp(mqtt_parm.msg_color,"1")==0){
|
||||
changecolor=4;
|
||||
}else if(strcmp(mqtt_parm.msg_color,"2")==0){
|
||||
changecolor=6;
|
||||
}else if(strcmp(mqtt_parm.msg_color,"3")==0){
|
||||
changecolor=1;
|
||||
}else if(strcmp(mqtt_parm.msg_color,"4")==0){
|
||||
changecolor=2;
|
||||
}else if(strcmp(mqtt_parm.msg_color,"5")==0){
|
||||
changecolor=3;
|
||||
}else if(strcmp(mqtt_parm.msg_color,"6")==0){
|
||||
changecolor=7;
|
||||
}else if(strcmp(mqtt_parm.msg_color,"7")==0){
|
||||
changecolor=5;
|
||||
}else if(strcmp(mqtt_parm.msg_color,"8")==0){
|
||||
changecolor=0;
|
||||
}
|
||||
jt_ledctrl_package_t led_ctrl={
|
||||
.s.color=changecolor,
|
||||
.s.sound=atoi(mqtt_parm.msg_sound),
|
||||
.s.flash=atoi(mqtt_parm.msg_flash),
|
||||
};
|
||||
uart_data_send_head_lightband(&uartSend, 2, this_batch);
|
||||
usleep(50000);
|
||||
uart_data_send_lightband(&uartSend, led_ctrl.ch, atoi(mqtt_parm.msg_flash), atoi(mqtt_parm.msg_lightDuration), tags, tag_leds, this_batch);
|
||||
}
|
||||
sent += this_batch;
|
||||
if (sent < total) {
|
||||
pthread_mutex_unlock(&lightsn_buffer_mutex); // 释放锁等待
|
||||
sleep(3);
|
||||
pthread_mutex_lock(&lightsn_buffer_mutex); // 重新加锁
|
||||
}
|
||||
}
|
||||
|
||||
if (isLightEnable && (lightsnNum > 0)) {
|
||||
//isSendComEnd = false;
|
||||
isLightOn = true;
|
||||
isLightOnByRule = false;
|
||||
isLabelUp = false;
|
||||
for (int i = 0; i < lightsnNum; i++) {
|
||||
LOG_I("sn[%d]=%s\n", i+1, lightsn_buffer[i]);
|
||||
}
|
||||
uart_data_send_head_lightband(&uartSend, 5, lightsnNum);
|
||||
}
|
||||
|
||||
// 不要立即清空buffer,等待isLightOn处理完成
|
||||
// lightsn_buffer_count = 0;
|
||||
// memset(lightsn_buffer, 0, sizeof(lightsn_buffer));
|
||||
is_3015_collecting = false;
|
||||
first_3015_time = 0;
|
||||
}
|
||||
|
||||
@ -109,6 +109,56 @@ json_error:
|
||||
json_object_put(root);
|
||||
}
|
||||
|
||||
void mqtt_server_light_status_report_2(char *sn,char *msg_id,char *scene,json_object *lights){
|
||||
char topic[128] = "";
|
||||
const char *payload = NULL;
|
||||
char time_buffer[128] = "";
|
||||
json_object *root = NULL;
|
||||
json_object *data = NULL;
|
||||
json_object *msg = NULL;
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
snprintf(time_buffer,sizeof(time_buffer),"%ld",tv.tv_sec*1000+tv.tv_usec);
|
||||
LOG_I("%s\n",__func__);
|
||||
|
||||
root = json_object_new_object();
|
||||
if(root == NULL){
|
||||
LOG_I("json_object_new_object error\n");
|
||||
goto json_error;
|
||||
}
|
||||
msg = json_object_new_object();
|
||||
if(msg == NULL){
|
||||
LOG_I("msg: NULL\n");
|
||||
goto json_error;
|
||||
}
|
||||
|
||||
data = json_object_new_object();
|
||||
if(msg == NULL){
|
||||
LOG_I("msg: NULL\n");
|
||||
goto json_error;
|
||||
}
|
||||
|
||||
json_object_object_add(root, "deviceId", json_object_new_string(sn));
|
||||
json_object_object_add(root, "messageId", json_object_new_string(msg_id));
|
||||
json_object_object_add(root, "productId", json_object_new_string(mqtt_conf->productcode));
|
||||
json_object_object_add(root, "timestamp", json_object_new_string(time_buffer));
|
||||
|
||||
json_object_object_add(data, "scene", json_object_new_string(scene));
|
||||
json_object_object_add(data, "lights", lights);
|
||||
|
||||
json_object_object_add(msg, "data", data);
|
||||
json_object_object_add(msg, "msgType", json_object_new_string("9007"));
|
||||
json_object_object_add(root, "msg", msg);
|
||||
|
||||
snprintf(topic, sizeof(topic), "iot/%s/%s/message/report",mqtt_conf->productcode,mqtt_conf->username);
|
||||
//LOG_I("publish topic:[%s]\n", topic);
|
||||
payload = json_object_to_json_string(root);
|
||||
//LOG_I("payload[%d][%s]\n", strlen(payload), payload);
|
||||
mqtt_utils_publish(mqtt_conf, topic, 2, payload, strlen(payload));
|
||||
json_error:
|
||||
json_object_put(root);
|
||||
}
|
||||
|
||||
void mqtt_server_station_status_report(char *msg_id,char *product_id,char *sn,char *local_ip, char *base_version,
|
||||
char *status, char *duration){
|
||||
char topic[128] = "";
|
||||
@ -162,6 +212,59 @@ json_error:
|
||||
json_object_put(root);
|
||||
}
|
||||
|
||||
void mqtt_server_station_status_report_test(char *msg_id,char *product_id,char *sn,char *local_ip, char *base_version,
|
||||
char *status, char *duration){
|
||||
char topic[128] = "";
|
||||
const char *payload = NULL;
|
||||
char time_buffer[128] = "";
|
||||
json_object *root = NULL;
|
||||
json_object *msg = NULL;
|
||||
json_object *data = NULL;
|
||||
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
snprintf(time_buffer,sizeof(time_buffer),"%ld",tv.tv_sec*1000+tv.tv_usec);
|
||||
LOG_I("%s\n",__func__);
|
||||
|
||||
root = json_object_new_object();
|
||||
if(root == NULL){
|
||||
LOG_I("json_object_new_object error\n");
|
||||
goto json_error;
|
||||
}
|
||||
|
||||
msg = json_object_new_object();
|
||||
if(msg == NULL){
|
||||
LOG_I("msg: NULL\n");
|
||||
goto json_error;
|
||||
}
|
||||
|
||||
data = json_object_new_object();
|
||||
if(msg == NULL){
|
||||
LOG_I("msg: NULL\n");
|
||||
goto json_error;
|
||||
}
|
||||
json_object_object_add(root, "deviceId", json_object_new_string(sn));
|
||||
json_object_object_add(root, "messageId", json_object_new_string(msg_id));
|
||||
json_object_object_add(root, "productId", json_object_new_string(mqtt_conf->productcode));
|
||||
json_object_object_add(root, "timestamp", json_object_new_string(time_buffer));
|
||||
|
||||
json_object_object_add(data, "ip", json_object_new_string(local_ip));
|
||||
json_object_object_add(data, "baseVersion", json_object_new_string(base_version));
|
||||
json_object_object_add(data, "status", json_object_new_string(status));
|
||||
json_object_object_add(data, "duration", json_object_new_string(duration));
|
||||
|
||||
json_object_object_add(msg, "data", data);
|
||||
json_object_object_add(msg, "msgType", json_object_new_string("9008"));
|
||||
json_object_object_add(root, "msg", msg);
|
||||
snprintf(topic, sizeof(topic), "iot/%s/%s/station/report", mqtt_conf->productcode, mqtt_conf->username);
|
||||
//LOG_I("publish topic:[%s]\n", topic);
|
||||
payload = json_object_to_json_string(root);
|
||||
//LOG_I("payload[%d][%s]\n", strlen(payload), payload);
|
||||
mqtt_utils_publish(mqtt_conf, topic, 2, payload, strlen(payload));
|
||||
json_error:
|
||||
json_object_put(root);
|
||||
}
|
||||
|
||||
void mqtt_service_reply(char *sn,char *msg_id,char *msg,int success,char *product_id)
|
||||
{
|
||||
char topic[128] = "";
|
||||
|
||||
@ -156,6 +156,9 @@ void mqtt_server_station_status_report(char *msg_id,char *product_id,char *sn,ch
|
||||
char *status, char *duration);
|
||||
void mqtt_service_reply(char *sn,char *msg_id,char *msg,int success,char *product_id);
|
||||
void mqtt_server_light_status_report(char *sn,char *msg_id,char *scene,json_object *lights);
|
||||
void mqtt_server_light_status_report_2(char *sn,char *msg_id,char *scene,json_object *lights);
|
||||
void mqtt_server_station_status_report_test(char *msg_id,char *product_id,char *sn,char *local_ip, char *base_version,
|
||||
char *status, char *duration);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
Loading…
Reference in New Issue
Block a user