From 72dbf8e8a4fafb329c285548861c0c4e8b159cb4 Mon Sep 17 00:00:00 2001 From: zhangsheng Date: Wed, 11 Mar 2026 10:39:34 +0800 Subject: [PATCH] optimize code and add control alarm --- common/parameter_defination.hpp | 6 +++++ jsonparse/communication_cmd.hpp | 3 +++ jsonparse/web_cmd_parse4.cpp | 45 +++++++++++++++++++++++++++++++++ localserver/local_server.hpp | 5 +++- localserver/web_cmd.cpp | 8 ++++++ main.cpp | 1 - uart/uart.cpp | 21 +++++++-------- uart/uart.hpp | 1 + uart/uart_parameter_config.cpp | 44 ++++++++++++++++++++++---------- 9 files changed, 108 insertions(+), 26 deletions(-) diff --git a/common/parameter_defination.hpp b/common/parameter_defination.hpp index 91de697..d1aa55f 100644 --- a/common/parameter_defination.hpp +++ b/common/parameter_defination.hpp @@ -389,4 +389,10 @@ struct Param_103{ std::string operate; Param_103() : mac(""),name(""),operate(""){}; }; + +struct Param_110{ + int alarmMode; // 0:关灯,1:绿⾊,2:红⾊,3:蓝⾊,4:⻘⾊,5:⻩⾊,6:紫⾊,7:⽩⾊ + int flashTime; // 单位是ms,0表⽰不闪烁常亮,其他值为闪烁的间隔 + Param_110() : alarmMode(0),flashTime(0){}; +}; #endif // PARAMETER_DEFINATION_HPP_ diff --git a/jsonparse/communication_cmd.hpp b/jsonparse/communication_cmd.hpp index 2877d1c..09cf684 100644 --- a/jsonparse/communication_cmd.hpp +++ b/jsonparse/communication_cmd.hpp @@ -82,6 +82,9 @@ public: std::string JsonCmd_Cgi_103(Param_103 ¶m); std::string JsonCmd_Cgi_default(); + //报警灯 + std::string JsonCmd_Cgi_110(Param_110 ¶m); + //CMT tcp void CmtCmd_80(char* send_data,int& send_length); void CmtCmd_81(char* recv_body,int& count,char* send_data,int& send_length); diff --git a/jsonparse/web_cmd_parse4.cpp b/jsonparse/web_cmd_parse4.cpp index 3d3c808..3c352f8 100644 --- a/jsonparse/web_cmd_parse4.cpp +++ b/jsonparse/web_cmd_parse4.cpp @@ -6,6 +6,7 @@ #include "dbaccess/sql_db.hpp" #include "platform/platform_init.hpp" #include "wifi_5g/scan_blueteeth.h" +#include "uart/uart.hpp" extern zlog_category_t *zct; extern const char *JSON_FIELD_CMD; @@ -195,5 +196,49 @@ std::string JsonData::JsonCmd_Cgi_103(Param_103 ¶m){ sqlite_db_ctrl::instance().DeleteTableData(T_SENSOR_BT_INFO(TNAME), whereCon); } + return show_value_.write(jsonVal); +} + +//报警灯 +std::string JsonData::JsonCmd_Cgi_110(Param_110 ¶m){ + Json::Value jsonVal; + jsonVal.clear(); + jsonVal[JSON_FIELD_CMD] = "110"; + jsonVal["success"] = true; + jsonVal["message"] = "设置报警灯成功"; + uart_inst::instance().alarmFlag = true; + if(uart_inst::instance().bUpdate){ + jsonVal["success"] = false; + jsonVal["message"] = "设置报警灯失败"; + return show_value_.write(jsonVal); + } + unsigned char command[11] = {0x00}; + int data = (param.flashTime << 16) | (param.alarmMode & 0xFFFF); + command[0] = 0xA5; + command[1] = 0x5A; + command[2] = 0xA5; + command[3] = 0x02; + command[4] = 0x02; + command[5] = 0x03; + command[6] = data & 0xFF; + command[7] = (data >> 8) & 0xFF; + command[8] = (data >> 16) & 0xFF; + command[9] = (data >> 24) & 0xFF; + unsigned char tmp = 0x00; + for (int i = 0; i < 10; i++) { + tmp += command[i]; + } + command[10] = tmp; + uart_inst::instance().WriteToUart((const char*)command, 11); + mssleep(15000); + char recvData[12] = {0x00}; + int ret = uart_inst::instance().ReadFromAlarm(); + if (ret == 2 ) { + jsonVal["message"] = "设置报警灯成功"; + } else { + jsonVal["success"] = false; + jsonVal["message"] = "设置报警灯失败"; + } + uart_inst::instance().alarmFlag = false; return show_value_.write(jsonVal); } \ No newline at end of file diff --git a/localserver/local_server.hpp b/localserver/local_server.hpp index d4f91cf..c7c60ec 100644 --- a/localserver/local_server.hpp +++ b/localserver/local_server.hpp @@ -71,7 +71,10 @@ enum WebCommand { kGetSensorInfo = 100, kGetFeatureSensorInfo = 101, kScanSenorInfo = 102, - kOperateSensor = 103 + kOperateSensor = 103, + + //报警灯 + kAlarmLight = 110 }; enum GatewayType{ kGWTDW2700 = 1, diff --git a/localserver/web_cmd.cpp b/localserver/web_cmd.cpp index db9fd2a..b75d494 100644 --- a/localserver/web_cmd.cpp +++ b/localserver/web_cmd.cpp @@ -553,6 +553,14 @@ std::string LocalServer::HandleCgi_cmd(std::string &pData) { std::string data = jd.JsonCmd_Cgi_103(param); return data; }break; + case kAlarmLight:{ + JsonData jd; + Param_110 param; + param.alarmMode = recvBody["alarmMode"].asInt(); + param.flashTime = recvBody["flashTime"].asInt(); + std::string data = jd.JsonCmd_Cgi_110(param); + return data; + }break; default: JsonData jd; std::string data = jd.JsonCmd_Cgi_default(); diff --git a/main.cpp b/main.cpp index e8130dc..bbb9156 100644 --- a/main.cpp +++ b/main.cpp @@ -54,7 +54,6 @@ int main(int argc, char *argv[]) { sqlite_db_ctrl::instance().InintGateway(); - uart_inst::instance().InitZigbeeHW(); // UDP,接收客户端发来的组播消息,用于外接 QT 专家系统,屏蔽之 boost::thread searchT(SearchThread); searchT.detach(); diff --git a/uart/uart.cpp b/uart/uart.cpp index aa70654..cdb39b9 100644 --- a/uart/uart.cpp +++ b/uart/uart.cpp @@ -148,6 +148,7 @@ Uart::Uart() : mUart(mIoSev), mStrand(mIoSev) { memset(send_data, 0, sizeof(send_data)); last_short_addr = 0; last_time = 0; + alarmFlag = false; } Uart::~Uart() { @@ -256,7 +257,7 @@ int Uart::ZigbeeTest() { } void Uart::WriteToUart(const char *strSend, int pLen) { - //if (!bUpdate) + if (!bUpdate) { printf( "\nWrite To Uart Start: %s\n",GetCurrentTime().c_str()); for (int i = 0; i < pLen; i++) { @@ -266,7 +267,6 @@ void Uart::WriteToUart(const char *strSend, int pLen) { } int iRet = write_data(fd, (char *)strSend, pLen); - printf( "WriteToUart iRet = %d, pLen = %d\n", iRet, pLen); } int Uart::ReadFromUart() { @@ -283,12 +283,8 @@ int Uart::ReadFromUart() { } int Uart::ReadFromAlarm(){ - char buffer[100] = {0x00}; - int len = read_data(fd, (char *)buffer, 100, 10); - printf( "ReadFromAlarm len = %d\n", len); - for (int i = 0; i < len; i++) printf( "%02X ", buffer[i] & 0xFF); - printf( "print end\n"); - zlog_info(zct, "==========ReadFromAlarm========len = %d", len); + char buffer[1024] = {0x00}; + int len = read_data(fd, (char *)buffer, 1024, 20); char head[] = {0xA5, 0x5A, 0xA5}; if (len > 0) { @@ -301,14 +297,19 @@ int Uart::ReadFromAlarm(){ if(buffer[i + 5] == 0x02 && buffer[i + 6] == 0x01){ return 0 ; - }else{ + }else if(buffer[i + 5] == 0x03 && buffer[i + 6] == 0xFF){ + return 2 ; + }else if(buffer[i + 5] == 0x02 && buffer[i + 6] == 0x00){ + return 1 ; + } + else{ continue; } } } } } - return 1 ; + return 1; } void Uart::Run() { mIoSev.run(); } diff --git a/uart/uart.hpp b/uart/uart.hpp index 4089cda..f873995 100644 --- a/uart/uart.hpp +++ b/uart/uart.hpp @@ -236,6 +236,7 @@ public: bool bZigbeeSinal; bool bModifyAddr; bool bSendTimeStamp; + bool alarmFlag; std::string DataNodeUpdateFile; private: diff --git a/uart/uart_parameter_config.cpp b/uart/uart_parameter_config.cpp index d84592e..c1733ea 100644 --- a/uart/uart_parameter_config.cpp +++ b/uart/uart_parameter_config.cpp @@ -184,8 +184,10 @@ void Uart::UpdateWirelessNode(uint16_t shortAdd) { //帧头[3byte] 节点地址[2byte] 数据类型[1byte] 序号[1byte] 数据包[92byte] CRC校验[1byte] zlog_warn(zbt, "Start Upgrade!!! file Size = %d,fileName = %s,Count = %d,lastSize = %d,shortAddr = %02x%02x", (int)thisSize,DataNodeUpdateFile.c_str(),Count,lastSize,UINT16_HIGH(shortAdd), UINT16_LOW(shortAdd)); tmp = 0x00; + int time = 0 ,value = -1; for(int j = 0; j < Count;j++){ - int time ,value; + time = 0 ; + value = -1; UpdateData[0]=0xAA; UpdateData[1]=0x55; UpdateData[2]=0xAA; @@ -200,13 +202,19 @@ void Uart::UpdateWirelessNode(uint16_t shortAdd) { } UpdateData[99] = tmp; WriteToUart((const char*)UpdateData,100); - mssleep(100000); + mssleep(10000); WriteToUart((const char*)command,11); - mssleep(100000); - if (ReadFromAlarm() == 0) { - zlog_info(zct, "Packge %d ACK send success,shortAddr = %02x%02x", j, UINT16_HIGH(shortAdd), UINT16_LOW(shortAdd)); - } else { - zlog_warn(zct, "Packge %d ACK send failed,shortAddr = %02x%02x", j, UINT16_HIGH(shortAdd), UINT16_LOW(shortAdd)); + mssleep(10000); + do{ + value = ReadFromAlarm(); + if(value == 0) + break; + WriteToUart((const char*)command,11); + mssleep(10000); + time += 1; + }while(time < 150);//1500000 + if(time >= 150){ + zlog_warn(zct, "Packge %d ACK send failed,shortAddr = %02x%02x,time = %d", j, UINT16_HIGH(shortAdd), UINT16_LOW(shortAdd),time); zlog_warn(zct, "gpio_read failed shortAdd %02x %02x,index = %d", UINT16_HIGH(shortAdd), UINT16_LOW(shortAdd),j); zlog_warn(zct, "gpio_read failed \n"); bUpdate = false; @@ -216,7 +224,8 @@ void Uart::UpdateWirelessNode(uint16_t shortAdd) { memset(UpdateData,0x00,sizeof(UpdateData)); mssleep(5000); } - + time = 0 ; + value = -1; if (lastSize > 0) { UpdateData[0] = 0xAA; UpdateData[1] = 0x55; @@ -232,13 +241,20 @@ void Uart::UpdateWirelessNode(uint16_t shortAdd) { } UpdateData[99] = tmp; WriteToUart((const char*)UpdateData, 100); - mssleep(100000); + mssleep(10000); getGPIOStatus(); - mssleep(100000); - if (ReadFromAlarm() == 0) { - zlog_info(zct, "ACK send success,shortAddr = %02x%02x", UINT16_HIGH(shortAdd), UINT16_LOW(shortAdd)); - } else { - zlog_warn(zct, "ACK send failed,shortAddr = %02x%02x", UINT16_HIGH(shortAdd), UINT16_LOW(shortAdd)); + mssleep(10000); + do{ + value = ReadFromAlarm(); + if(value == 0) + break; + WriteToUart((const char*)command,11); + mssleep(10000); + time += 1; + }while(time < 150);//1500000 + if(time >= 150){ + zlog_warn(zct, "Packge %d ACK send failed,shortAddr = %02x%02x,time = %d", lastSize, UINT16_HIGH(shortAdd), UINT16_LOW(shortAdd),time); + zlog_warn(zct, "gpio_read failed shortAdd %02x %02x,index = %d", UINT16_HIGH(shortAdd), UINT16_LOW(shortAdd),lastSize); zlog_warn(zct, "gpio_read failed \n"); bUpdate = false; upgrade_status = 3;