From c598ad5996a14ade76331f4d1664ae568988a146 Mon Sep 17 00:00:00 2001 From: zhangsheng Date: Wed, 20 Nov 2024 16:45:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0mqtt=E5=8D=8F=E8=AE=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jsonparse/communication_cmd.hpp | 5 +- jsonparse/mqtt_cmd_parse.cpp | 193 ++++++++++++++++++++++++++++++-- threadfunc/check_thread.cpp | 3 - uart/uart.cpp | 40 +++++-- 4 files changed, 217 insertions(+), 24 deletions(-) diff --git a/jsonparse/communication_cmd.hpp b/jsonparse/communication_cmd.hpp index 5ce1019..8e1c5e7 100644 --- a/jsonparse/communication_cmd.hpp +++ b/jsonparse/communication_cmd.hpp @@ -18,7 +18,10 @@ public: std::string JsonCmd_25(Param_25 ¶m); std::string JsonCmd_26(Param_26 ¶m); std::string JsonCmd_27(Json::Value &recvBody); - std::string JsonCmd_29(Param_29 ¶m); //系统配置信息 + int JsonCmd_29(); //网关版本信息 + int JsonCmd_30(); //传感器信息 + int JsonCmd_31(); + int JsonCmd_32(std::string MeasurementID,int status,int type,std::string Data,std::string Msg); std::string JsonCmd_50(Json::Value &recvBody); // download update file std::string JsonCmd_51(Json::Value &recvBody); std::string JsonCmd_52(); // upload static data diff --git a/jsonparse/mqtt_cmd_parse.cpp b/jsonparse/mqtt_cmd_parse.cpp index e603e03..3b377dc 100644 --- a/jsonparse/mqtt_cmd_parse.cpp +++ b/jsonparse/mqtt_cmd_parse.cpp @@ -1,11 +1,13 @@ #include "communication_cmd.hpp" #include +#include #include "common/global.hpp" #include "dbaccess/sql_db.hpp" #include "mqttclient/mqtt_client.h" #include "datatransfer/data_trans.hpp" #include "platform/platform_init.hpp" #include "utility/md5.h" +#include "scheduler/schedule.hpp" extern zlog_category_t *zct; @@ -475,30 +477,194 @@ std::string JsonData::JsonCmd_53(Json::Value &recvBody) { return show_value_.write(jsonVal); } -std::string JsonData::JsonCmd_29(Param_29 ¶m) { +int JsonData::JsonCmd_29() { Json::Value jsonVal; Json::Value jsBody; - Json::Value SystemSetting; + Json::Value jsSystemSetting; jsonVal.clear(); jsonVal["cmd"] = "29"; jsonVal["dataNodeGatewayNo"] = GlobalConfig::MacAddr_G; + jsSystemSetting["WebVersion"] = ReadStrByOpt(SYSTEMINFOFILE, "Version", "WebVersion"); + jsSystemSetting["SystemVersion"] = ReadStrByOpt(SYSTEMINFOFILE, "Version", "SystemVersion"); + jsSystemSetting["GateWayVersion"] = ReadStrByOpt(SYSTEMINFOFILE, "Version", "GateWayVersion"); + jsBody["localIP"] = GlobalConfig::IpAddr_G; + jsBody["dataNodeGatewayType"] = ReadStrByOpt(SYSTEMINFOFILE, "Version", "GateWayProduct"); + jsBody["CommMode"] = "有线"; +#ifdef NR5G_MODULE + jsBody["CommMode"] = "5G"; +#endif +#ifdef Q4G_MODULE + jsBody["CommMode"] = "4G"; +#endif +#ifdef WIFI_MODULE + jsBody["CommMode"] = "WiFi"; +#endif + jsBody["GateWayHwVesion"] = ReadStrByOpt(SYSTEMINFOFILE, "Version", "GateWayHwVesion"); + std::string dataBody = show_value_.write(jsBody); + jsonVal["cmdBody"] = dataBody; + char localtimestamp[32] = {0}; + GetTimeNet(localtimestamp, 1); + jsonVal["timestamp"] = localtimestamp; + int res = data_publish(show_value_.write(jsonVal).c_str(), GlobalConfig::Topic_G.mPubStatus.c_str()); + if(res !=0 ){ + zlog_warn(zct, "data_publish res = %d", res); + } + return 0; +} +int JsonData::JsonCmd_30(){ + Json::Value jsonVal; + Json::Value jsBody; + Json::Value jsSystemSetting; + Json::Value jsArray; + jsonVal.clear(); + jsonVal["cmd"] = "30"; array_t arrRes; - arrRes = sqlite_db_ctrl::instance().GetDataMultiLine(T_SENSOR_INFO(TNAME), "*", NULL); + arrRes = sqlite_db_ctrl::instance().GetDataMultiLineTransaction(T_SENSOR_INFO(TNAME), "*", NULL); int iResult = arrRes.size(); if (iResult > 0) { - for (int i = 0; i < iResult; i++) { - Json::Value jsDataNode; - jsDataNode["dataNodeNo"] = arrRes[i][0]; - jsDataNode["status"] = atoi(arrRes[i][37].c_str()); - jsBody["dataNodeArray"].append(jsDataNode); + for (int j = 0; j < iResult; j++) { + Json::Value jsSensorData; + jsSensorData["dataNodeNo"] = arrRes[j][44]; + jsSensorData["dataNodeName"] = arrRes[j][1]; + jsSensorData["acc"] = atoi(arrRes[j][3].c_str()); + jsSensorData["zigbee"] = atoi(arrRes[j][4].c_str()); + jsSensorData["temTop"] = atoi(arrRes[j][5].c_str()); + jsSensorData["temBot"] = atoi(arrRes[j][6].c_str()); + jsSensorData["hwVersion"] = arrRes[j][8]; + jsSensorData["softVersion"] = arrRes[j][9]; + jsSensorData["bpNo"] = arrRes[j][10]; + jsSensorData["serialNo"] = arrRes[j][11]; + jsSensorData["firstPowerTime"] = arrRes[j][12]; + jsSensorData["WakeupTime"] = atoi(arrRes[j][13].c_str()); + jsSensorData["StaticTime"] = atoi(arrRes[j][14].c_str()); + jsSensorData["WaveTime"] = atoi(arrRes[j][15].c_str()); + jsSensorData["BateryV"] = arrRes[j][16]; + jsSensorData["ProductNo"] = arrRes[j][17]; + jsSensorData["configFlag"] = atoi(arrRes[j][18].c_str()); + jsSensorData["startBrands"] = arrRes[j][19]; + jsSensorData["stopBrands"] = arrRes[j][20]; + jsSensorData["featureInterVal"] = atoi(arrRes[j][21].c_str()); + jsSensorData["waveInterVal"] = atoi(arrRes[j][22].c_str()); + jsSensorData["samplingRate"] = atoi(arrRes[j][23].c_str()); + jsSensorData["range"] = atoi(arrRes[j][25].c_str()); + jsSensorData["envelopeBandPass"] = arrRes[j][26]; + jsSensorData["faultFrequency"] = arrRes[j][27]; + jsSensorData["zigbeePanId"] = arrRes[j][28]; + jsSensorData["zigbeeChannel"] = (arrRes[j][29]); + jsSensorData["zigbeeAddr"] = arrRes[j][30]; + jsSensorData["zigbeeLongAddr"] = arrRes[j][31]; + jsSensorData["zigbeeDesAddr"] = arrRes[j][32]; + jsSensorData["ZigbeePower"] = atoi(arrRes[j][33].c_str()); + jsSensorData["ZigbeeRetry"] = atoi(arrRes[j][34].c_str()); + jsSensorData["ZigbeeRetryGap"] = atoi(arrRes[j][35].c_str()); + jsSensorData["ACCSampleTime"] = atoi(arrRes[j][36].c_str()); + jsSensorData["status"] = atoi(arrRes[j][37].c_str()); + jsSensorData["timeStamp"] = arrRes[j][38]; + jsSensorData["viff"] = atoi(arrRes[j][39].c_str()); + std::vector vParam; + boost::split(vParam, arrRes[j][42], boost::is_any_of(","), boost::token_compress_on); + if (vParam.size() > 1){ + if (vParam[1] != "0"){ + jsSensorData["looseStatus"] = 1; + }else{ + jsSensorData["looseStatus"] = 0; + } + } + std::vector vParamRSSI; + boost::split(vParamRSSI, arrRes[j][40], boost::is_any_of(","), boost::token_compress_on); + if (vParamRSSI.size() > 1) { + jsSensorData["gatewayRSSI"] = atoi(vParamRSSI[0].c_str()); + jsSensorData["sensorRSSI"] = atoi(vParamRSSI[1].c_str()); + } else { + jsSensorData["gatewayRSSI"] = 0; + jsSensorData["sensorRSSI"] = atoi(arrRes[j][40].c_str()); + } + jsSensorData["update"] = atoi(arrRes[j][41].c_str()); + jsSensorData["MeasurementID"] = arrRes[j][44]; + std::vector vParambattery; + boost::split(vParambattery, arrRes[j][43], boost::is_any_of(","), boost::token_compress_on); + if (vParambattery.size() > 1) { + jsSensorData["battery"] = atoi(vParambattery[1].c_str())/atoi(vParambattery[0].c_str()); + } else { + jsSensorData["battery"] = 0.99; + } + jsArray.append(jsSensorData); } + } else { + jsArray.resize(0); + jsonVal["success"] = true; } - - std::string strBody = show_value_.write(jsBody); - jsonVal["cmdBody"] = strBody; - return show_value_.write(jsonVal); + jsBody["dataNodeGatewayNo"] = GlobalConfig::MacAddr_G; + jsBody["dataNodeArray"] = jsArray; + std::string dataBody = show_value_.write(jsBody); + jsonVal["cmdBody"] = dataBody; + char localtimestamp[32] = {0}; + GetTimeNet(localtimestamp, 1); + jsonVal["timestamp"] = localtimestamp; + int res = data_publish(show_value_.write(jsonVal).c_str(), GlobalConfig::Topic_G.mPubStatus.c_str()); + if(res !=0 ){ + zlog_warn(zct, "data_publish res = %d", res); + } + return 0; } +int JsonData::JsonCmd_31(){ + Json::Value jsonVal; + jsonVal.clear(); + Json::Value jsBody; + jsonVal[JSON_FIELD_CMD] = "31"; + jsonVal["success"] = true; + jsonVal["message"] = ""; + int sensorCount = 0; + int featureInterVal = 0; + int featureInterTime = 0; + int waveInterVal = 0; + int waveInterTime = 0; + int maxSensorNum = 0; + + sensorCount = sqlite_db_ctrl::instance().GetTableRows(T_SENSOR_INFO(TNAME), NULL); + scheduler::instance().GetScheduleConfig(featureInterVal,waveInterVal,featureInterTime,waveInterTime,maxSensorNum); + jsBody["featureInterVal"] = featureInterVal; + jsBody["featureInterTime"] = featureInterTime; + jsBody["waveInterVal"] = waveInterVal; + jsBody["waveInterTime"] = waveInterTime; + jsBody["maxSensorNum"] = maxSensorNum; + jsBody["sensorCount"] = sensorCount; + std::string dataBody = show_value_.write(jsBody); + jsonVal["cmdBody"] = dataBody; + char localtimestamp[32] = {0}; + GetTimeNet(localtimestamp, 1); + jsonVal["timestamp"] = localtimestamp; + int res = data_publish(show_value_.write(jsonVal).c_str(), GlobalConfig::Topic_G.mPubStatus.c_str()); + if(res !=0 ){ + zlog_warn(zct, "data_publish res = %d", res); + } + return 0; +} +int JsonData::JsonCmd_32(std::string MeasurementID,int status,int type,std::string Data,std::string Msg){ + Json::Value jsonVal; + jsonVal.clear(); + Json::Value jsBody; + Json::FastWriter show_value_; + jsonVal["CMD"] = "32"; + jsonVal["success"] = true; + jsonVal["message"] = ""; + jsBody["dataNodeNo"] = MeasurementID; + jsBody["errorStatus"] = status; + jsBody["errorType"] = type; + jsBody["errorData"] = Data; + jsBody["errorMsg"] = Msg; + std::string dataBody = show_value_.write(jsBody); + jsonVal["cmdBody"] = dataBody; + char timestamp[32] = {0}; + GetTimeNet(timestamp, 1); + jsonVal["timestamp"] = timestamp; + int res = data_publish(show_value_.write(jsonVal).c_str(), GlobalConfig::Topic_G.mPubStatus.c_str()); + if(res !=0 ){ + zlog_warn(zct, "data_publish res = %d", res); + } + +} void JsonData::DataNodeStatusCheck() { array_t vetRes = sqlite_db_ctrl::instance().GetDataMultiLine(T_SENSOR_INFO(TNAME), "*", NULL); int nSize = vetRes.size(); @@ -559,6 +725,9 @@ std::string JsonData::JsonCmd_07() { jsonVal[JSON_FIELD_CMD] = "07"; jsonVal["success"] = true; jsonVal["message"] = " "; + char localtimestamp[32] = {0}; + GetTimeNet(localtimestamp, 1); + jsonVal["timestamp"] = localtimestamp; if (recvReader.parse(sysStatus, recvSys)) { jsonVal["cmdBody"] = sysStatus; } else { diff --git a/threadfunc/check_thread.cpp b/threadfunc/check_thread.cpp index 4b5bab0..c158ea0 100644 --- a/threadfunc/check_thread.cpp +++ b/threadfunc/check_thread.cpp @@ -125,9 +125,6 @@ void CheckThread() { if (Count > 0) { JsonData jd; jd.DataNodeStatusCheck(); - Param_29 param; - std::string cmd29 = jd.JsonCmd_29(param); - data_publish(cmd29.c_str(), GlobalConfig::Topic_G.mPubCmd.c_str()); char localtimestamp[32] = {0}; GetTimeNet(localtimestamp, 1); std::string nowTimetamp = std::string(localtimestamp); diff --git a/uart/uart.cpp b/uart/uart.cpp index b42ac09..8985783 100644 --- a/uart/uart.cpp +++ b/uart/uart.cpp @@ -17,6 +17,7 @@ #include "mqttclient/mqtt_client.h" #include "utility/serial.h" #include "scheduler/schedule.hpp" +#include "jsonparse/communication_cmd.hpp" extern zlog_category_t *zct; extern zlog_category_t *zbt; @@ -1044,13 +1045,27 @@ int Uart::FindRecvPackage(int bytesRead, char *mUartRecvBuf, char *head) { char insertSql[100] = {0x00}; char whereCon[50] = {0x00}; - char error_msg[50] = {0x00}; + std::string error_msg = ""; + std::string strChannelID = ""; sprintf(whereCon, "zigbeeShortAddr='%s'",strShortAddr.c_str()); - std::string strDataNodeNo = sqlite_db_ctrl::instance().GetData(T_SENSOR_INFO(TNAME), " dataNodeNo ", whereCon); + std::string strMeasurementID = sqlite_db_ctrl::instance().GetData(T_SENSOR_INFO(TNAME), " MeasurementID ", whereCon); memset(whereCon, 0x00, sizeof(whereCon)); - sprintf(error_msg,"Index error,command = %d",command); - sprintf(insertSql, "'%s','%s','%s',%d,'%d','%s' ", strDataNodeNo.c_str(),strShortAddr.c_str(),localtimestamp,bytesRead,0,error_msg); + if (command == WAVE_X){ + error_msg = "Index error,wave X"; + strChannelID = strMeasurementID + "-X"; + }else if (command == WAVE_Y){ + error_msg = "Index error,wave Y"; + strChannelID = strMeasurementID + "-Y"; + }else if (command == WAVE_Z){ + error_msg = "Index error,wave Z"; + strChannelID = strMeasurementID + "-Z"; + } + + sprintf(insertSql, "'%s','%s','%s',%d,'%d','%s' ", strMeasurementID.c_str(),strShortAddr.c_str(),localtimestamp,bytesRead,0,error_msg.c_str()); sqlite_db_ctrl::instance().InsertData(" receive_wave_status ", insertSql); + JsonData jd; + jd.JsonCmd_32(strMeasurementID,1,1,strChannelID,error_msg); + break; } @@ -1075,13 +1090,22 @@ int Uart::FindRecvPackage(int bytesRead, char *mUartRecvBuf, char *head) { g_VecWaveDataZ.clear(); char insertSql[100] = {0x00}; char whereCon[50] = {0x00}; - char error_msg[50] = {0x00}; + std::string error_msg = ""; + std::string strChannelID = ""; sprintf(whereCon, "zigbeeShortAddr='%s'",strShortAddr.c_str()); - std::string strDataNodeNo = sqlite_db_ctrl::instance().GetData(T_SENSOR_INFO(TNAME), " dataNodeNo ", whereCon); + std::string strMeasurementID = sqlite_db_ctrl::instance().GetData(T_SENSOR_INFO(TNAME), " MeasurementID ", whereCon); memset(whereCon, 0x00, sizeof(whereCon)); - sprintf(error_msg,"Crc error,command = %d",command); - sprintf(insertSql, "'%s','%s','%s',%d,'%d','%s' ", strDataNodeNo.c_str(),strShortAddr.c_str(),localtimestamp,bytesRead,0,error_msg); + if (command == WAVE_X){ + error_msg = "Crc error,wave X"; + }else if (command == WAVE_Y){ + error_msg = "Crc error,wave Y"; + }else if (command == WAVE_Z){ + error_msg = "Crc error,wave Z"; + } + sprintf(insertSql, "'%s','%s','%s',%d,'%d','%s' ", strMeasurementID.c_str(),strShortAddr.c_str(),localtimestamp,bytesRead,0,error_msg.c_str()); sqlite_db_ctrl::instance().InsertData(" receive_wave_status ", insertSql); + JsonData jd; + jd.JsonCmd_32(strMeasurementID,1,1,strChannelID,error_msg); break; }