From 888bea8fa3440d3b940d30fd7dcf609595ede675 Mon Sep 17 00:00:00 2001 From: zhangsheng Date: Thu, 28 Nov 2024 09:16:56 +0800 Subject: [PATCH] modify mqtt --- dbaccess/sql_db.cpp | 2 ++ jsonparse/mqtt_cmd_parse.cpp | 15 +++++++------ main.cpp | 2 +- threadfunc/check_thread.cpp | 12 +++++++--- threadfunc/thread_func.cpp | 21 ++++++++++-------- uart/uart.hpp | 2 +- uart/uart_feature_parse.cpp | 43 +++++++++++++++++++----------------- 7 files changed, 56 insertions(+), 41 deletions(-) diff --git a/dbaccess/sql_db.cpp b/dbaccess/sql_db.cpp index 5503a56..a0bdf5c 100644 --- a/dbaccess/sql_db.cpp +++ b/dbaccess/sql_db.cpp @@ -257,6 +257,8 @@ void SqliteDB::SqliteInit(const char *pDbName) { execute_sql_file("/opt/configenv/firmware_upgrade.sql"); execute_sql_file("/opt/configenv/receive_wave_status.sql"); + + execute_sql_file("/opt/configenv/reboot_record.sql"); } void SqliteDB::Createtable(const char *ptableName) { diff --git a/jsonparse/mqtt_cmd_parse.cpp b/jsonparse/mqtt_cmd_parse.cpp index a5d2ce6..5a1ffe4 100644 --- a/jsonparse/mqtt_cmd_parse.cpp +++ b/jsonparse/mqtt_cmd_parse.cpp @@ -227,6 +227,9 @@ std::string JsonData::JsonCmd_26(Param_26 ¶m) { for (int j = 0; j < iResult; j++) { Json::Value jsSensorData; jsSensorData["dataNodeNo"] = arrRes[j][44]; + char NodeName[100]={0}; + stringToHex(arrRes[j][1].c_str(),NodeName); + jsSensorData["dataNodeNameHex"] = NodeName; jsSensorData["dataNodeName"] = arrRes[j][1]; jsSensorData["initFlag"] = atoi(arrRes[j][2].c_str()); jsSensorData["accFlag"] = atoi(arrRes[j][3].c_str()); @@ -523,7 +526,9 @@ int JsonData::JsonCmd_30(){ for (int j = 0; j < iResult; j++) { Json::Value jsSensorData; jsSensorData["dataNodeNo"] = arrRes[j][44]; - jsSensorData["dataNodeName"] = arrRes[j][1]; + char NodeName[100]={0}; + stringToHex(arrRes[j][1].c_str(),NodeName); + jsSensorData["dataNodeName"] = NodeName; jsSensorData["acc"] = atoi(arrRes[j][3].c_str()); jsSensorData["zigbee"] = atoi(arrRes[j][4].c_str()); @@ -581,7 +586,7 @@ int JsonData::JsonCmd_30(){ 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()); + jsSensorData["battery"] = float(atof(vParambattery[1].c_str())/atof(vParambattery[0].c_str())); } else { jsSensorData["battery"] = 0.99; } @@ -700,16 +705,12 @@ void JsonData::DataNodeStatusCheck() { char whereCon[32] = {0}; sprintf(whereCon, "dataNodeNo='%s'", strDataNodeNo.c_str()); sqlite_db_ctrl::instance().UpdateTableData(T_SENSOR_INFO(TNAME), "status='0'", whereCon); + count++; } else { char whereCon[32] = {0}; sprintf(whereCon, "dataNodeNo='%s'", strDataNodeNo.c_str()); sqlite_db_ctrl::instance().UpdateTableData(T_SENSOR_INFO(TNAME), "status='1'", whereCon); } - if (lTimeTemp > atoi(vetRes[i][21].c_str()) * 5 * 60) //判定传感器是否超过五次未传特征值 - { - zlog_info(zct, "lTimeTemp = %d,featureInterVal = %d", lTimeTemp, atoi(vetRes[i][21].c_str())); - count++; - } } } if (count == nSize && nodeOnline && count != 0) { diff --git a/main.cpp b/main.cpp index 0b4d825..36f16cc 100644 --- a/main.cpp +++ b/main.cpp @@ -142,7 +142,7 @@ int main(int argc, char *argv[]) { // gpio_set(GlobalConfig::GPIO_G.hardWatchDog, 1); // usleep(20000); // gpio_set(GlobalConfig::GPIO_G.hardWatchDog, 0); - //WriteWatchDog(fd); + // WriteWatchDog(fd); sleep(20); if (GlobalConfig::threadStatus == 0) { count++; diff --git a/threadfunc/check_thread.cpp b/threadfunc/check_thread.cpp index 44688dc..bf3e5e3 100644 --- a/threadfunc/check_thread.cpp +++ b/threadfunc/check_thread.cpp @@ -98,9 +98,10 @@ void CheckThread() { #endif } } - if (HardStatus == 3600) { // one hour 3600 + if (HardStatus == 60) { // one hour 3600 JsonData jd; jd.JsonCmd_07(); + jd.JsonCmd_30(); HardStatus = 0; } if (mqttresend == 7200) { @@ -137,12 +138,17 @@ void CheckThread() { } if (7200 == Battery) { Battery = 0; - zlog_info(zct, "Battery"); + zlog_warn(zct, "Battery"); sqlite_db_ctrl::instance().CalculateBattery(); + JsonData jd; + jd.JsonCmd_30(); + jd.JsonCmd_07(); + jd.JsonCmd_29(); + jd.JsonCmd_31(); } if (3500 == loose_check) { - zlog_info(zct, "loosecheck\n"); + zlog_warn(zct, "loosecheck\n"); loose_check = 0; sqlite_db_ctrl::instance().CalculateDip(); } diff --git a/threadfunc/thread_func.cpp b/threadfunc/thread_func.cpp index 477b3ed..8854ff9 100644 --- a/threadfunc/thread_func.cpp +++ b/threadfunc/thread_func.cpp @@ -209,15 +209,18 @@ void InitModule() { #ifdef Q4G_MODULE - InitGpio(GlobalConfig::GPIO_G.commPower, 1); // 4G,5G模组供电, - gpio_set(GlobalConfig::GPIO_G.commPower, 1); - InitGpio(GlobalConfig::GPIO_G.commRest, 1); - gpio_set(GlobalConfig::GPIO_G.commRest, 0); //高电平复位 - sleep(10); - char szquectel[100] = {0x00}; - std::string strAPN = ReadStrByOpt(SERVERCONFIG, "Server", "APN"); - sprintf(szquectel, "/opt/quectel-CM/quectel-CM -s %s &", strAPN.c_str()); - system(szquectel); + system("killall daemon_CM"); + InitGpio(GlobalConfig::GPIO_G.commPower,1);//4G,5G模组供电, + gpio_set(GlobalConfig::GPIO_G.commPower,1); + InitGpio(GlobalConfig::GPIO_G.commRest,1); + gpio_set(GlobalConfig::GPIO_G.commRest,0);//高电平复位 + sleep(10); + char szquectel[100]={0x00}; + std::string strAPN = ReadStrByOpt(SERVERCONFIG, "Server", "APN"); + sprintf(szquectel,"/opt/quectel-CM/quectel-CM -s %s &",strAPN.c_str()); + system(szquectel); + sleep(2); + system("/opt/Cidn/daemon_CM &"); #endif // Q4G_MODULE #ifdef WIFI_MODULE zlog_info(zbt, "Init WiFi!"); diff --git a/uart/uart.hpp b/uart/uart.hpp index 05a974a..25ca150 100644 --- a/uart/uart.hpp +++ b/uart/uart.hpp @@ -183,7 +183,7 @@ public: void DealWave(); std::vector DealData(int ichannel, float coe, unsigned int sampleRate, int ACCSampleTime, std::string strProduct); float Calcoe(int ran, int iChannel, std::string& product, int range); - void WriteDatFile(int sampleRate, std::string& strMeasurementID, int iChannel, std::vector& vecData,std::string &product); + void WriteDatFile(int sampleRate, std::string& strMeasurementID, int iChannel, std::vector& vecData,std::string &product,int ACCSampleTime); float ScaleConvert(int highbit); void DataExtract(RecvData *p, int id, unsigned int &lowbit, float &n); diff --git a/uart/uart_feature_parse.cpp b/uart/uart_feature_parse.cpp index 9df42a9..e78febe 100644 --- a/uart/uart_feature_parse.cpp +++ b/uart/uart_feature_parse.cpp @@ -25,7 +25,7 @@ std::map g_mapWaveChannel; unsigned char data[96000] = {0x00}; unsigned char outdata[96000] = {0x00}; unsigned char dealdata[96000] = {0x00}; -char mqttData[512000] = {0}; +char mqttData[1024000] = {0}; void Uart::RecordBattery(std::string &strLongAddr, DataRecvStatic &dataStatic, std::string &nowTimetamp) { char insertSql[1024] = {0}; @@ -802,7 +802,7 @@ void Uart::DealWave() { zlog_info(zct, "m_waveCountX = %d,VecWaveData = %d", m_waveCountX, VecWaveDataX.size()); coe = Calcoe(n, WAVE_X, strProduct, range); vecData = DealData(WAVE_X, coe, sampleRate, ACCSampleTime, strProduct); - WriteDatFile(sampleRate, strMeasurementID, WAVE_X, vecData,strProduct); + WriteDatFile(sampleRate, strMeasurementID, WAVE_X, vecData,strProduct,ACCSampleTime); m_waveCountX = 0; g_VecWaveDataX.clear(); VecWaveDataX.clear(); @@ -811,7 +811,7 @@ void Uart::DealWave() { zlog_info(zct, "m_waveCountY = %d,VecWaveData = %d", m_waveCountY, VecWaveDataY.size()); coe = Calcoe(n, WAVE_Y, strProduct, range); vecData = DealData(WAVE_Y, coe, sampleRate, ACCSampleTime, strProduct); - WriteDatFile(sampleRate, strMeasurementID, WAVE_Y, vecData,strProduct); + WriteDatFile(sampleRate, strMeasurementID, WAVE_Y, vecData,strProduct,ACCSampleTime); m_waveCountY = 0; g_VecWaveDataY.clear(); VecWaveDataY.clear(); @@ -820,7 +820,7 @@ void Uart::DealWave() { zlog_info(zct, "m_waveCountZ = %d,VecWaveDataZ = %d", m_waveCountZ, VecWaveDataZ.size()); coe = Calcoe(n, WAVE_Z, strProduct, range); vecData = DealData(WAVE_Z, coe, sampleRate, ACCSampleTime, strProduct); - WriteDatFile(sampleRate, strMeasurementID, WAVE_Z, vecData,strProduct); + WriteDatFile(sampleRate, strMeasurementID, WAVE_Z, vecData,strProduct,ACCSampleTime); m_waveCountZ = 0; g_VecWaveDataZ.clear(); VecWaveDataZ.clear(); @@ -845,7 +845,7 @@ float Uart::Calcoe(int ran, int iChannel, std::string &product, int range) { return coe; } -void Uart::WriteDatFile(int sampleRate, std::string &strMeasurementID, int iChannel, std::vector &vecData,std::string &product) { +void Uart::WriteDatFile(int sampleRate, std::string &strMeasurementID, int iChannel, std::vector &vecData,std::string &product,int ACCSampleTime) { if (vecData.size() <= 0) return; std::string strFileName = ""; char localtimestamp[32] = {0}; @@ -903,27 +903,31 @@ void Uart::WriteDatFile(int sampleRate, std::string &strMeasurementID, int iChan } } fclose(fp); - if (product == "02" && sampleRate == 24000 && strMeasurementID == "8eff96feff141a88"){ + if (product == "02" && sampleRate == 24000 && iChannel == WAVE_Z){ + sampleRate = 25600; + zlog_info(zct, " sampleRate = %d,product = %s ", sampleRate,product.c_str()); size_t outSize = 25600; - std::vector outputData = Calculation::fftInterpolate(vecData, outSize); + std::vector outputData,outputData2; + if (ACCSampleTime == 1){ + outputData = Calculation::fftInterpolate(vecData, outSize); + }else if(ACCSampleTime == 2){ + std::vector first_wave = std::vector(vecData.begin(), vecData.begin() + vecData.size()/2); + std::vector second_wave = std::vector(vecData.begin() + vecData.size()/2, vecData.end()); + outputData = Calculation::fftInterpolate(first_wave, outSize); + outputData2 = Calculation::fftInterpolate(second_wave, outSize); + for (size_t i = 0; i < outputData2.size(); i++) + { + outputData.push_back(outputData2[i]); + } + } + zlog_info(zct, " outputData_size %d ", outputData.size()); float mean = Calculation::mean(outputData); memset(mqttData,0,sizeof(mqttData)); - id = 0; - strFileName = strFileName + std::string(localtimestamp); - FILE *fp = fopen(strFileName.c_str(), "w"); - fwrite(localtimestamp,sizeof(localtimestamp),1,fp); + id = 0; for (size_t i = 0; i < outputData.size(); i++) { frTemp = outputData[i] - mean; memset(buf, 0x00, sizeof(buf)); sprintf(buf, "%.2f", frTemp); - fwrite(&frTemp,sizeof(float),1,fp); - if (iChannel == WAVE_X){ - wave_channel.WaveChannelX[i] = frTemp; - }else if (iChannel == WAVE_Y){ - wave_channel.WaveChannelY[i] = frTemp; - }else if (iChannel == WAVE_Z){ - wave_channel.WaveChannelZ[i] = frTemp; - } if(i != outputData.size() -1){ strncpy(mqttData + id ,buf,strlen(buf)); id = id + strlen(buf); @@ -933,7 +937,6 @@ void Uart::WriteDatFile(int sampleRate, std::string &strMeasurementID, int iChan strncpy(mqttData + id ,buf,strlen(buf)); } } - fclose(fp); } zlog_info(zct, "fopen FIle vecData.size : %d end ", vecData.size());