From 634b54f435160fee854ec0147c75bf50b745dbf7 Mon Sep 17 00:00:00 2001 From: zhangsheng Date: Fri, 8 Nov 2024 16:55:14 +0800 Subject: [PATCH] fix wave save bugs --- common/common_func.cpp | 14 ++--- common/common_func.hpp | 12 +++- common/parameter_defination.hpp | 4 +- jsonparse/web_cmd_parse2.cpp | 10 ++++ jsonparse/web_cmd_parse3.cpp | 4 +- platform/platform_init.cpp | 11 ++-- threadfunc/thread_func.cpp | 2 +- uart/uart.cpp | 25 ++++++--- uart/uart.hpp | 11 ++++ uart/uart_feature_parse.cpp | 98 ++++++++++++++++++--------------- uart/uart_parameter_config.cpp | 4 ++ 11 files changed, 128 insertions(+), 67 deletions(-) diff --git a/common/common_func.cpp b/common/common_func.cpp index c159354..310e187 100644 --- a/common/common_func.cpp +++ b/common/common_func.cpp @@ -917,37 +917,37 @@ std::string IpAddrInit() { std::string strip = ""; strip = GetGwIp_(WLAN0); if (strip.compare("0.0.0.0") != 0) { - zlog_info(zct, "[IpAddrInit] wlan0: %s", strip.c_str()); + //zlog_info(zct, "[IpAddrInit] wlan0: %s", strip.c_str()); return strip; } strip = GetGwIp_(WLAN2); if (strip.compare("0.0.0.0") != 0) { - zlog_info(zct, "[IpAddrInit] wlan2: %s", strip.c_str()); + //zlog_info(zct, "[IpAddrInit] wlan2: %s", strip.c_str()); return strip; } strip = GetGwIp_(ETH1); if (strip.compare("0.0.0.0") != 0 && strip.compare("192.168.188.188") != 0) { - zlog_info(zct, "[IpAddrInit] eth1: %s", strip.c_str()); + //zlog_info(zct, "[IpAddrInit] eth1: %s", strip.c_str()); return strip; } strip = GetGwIp_(ETH2); if (strip.compare("0.0.0.0") != 0 && strip.compare("192.168.188.188") != 0) { - zlog_info(zct, "[IpAddrInit] eth2: %s", strip.c_str()); + //zlog_info(zct, "[IpAddrInit] eth2: %s", strip.c_str()); return strip; } strip = GetGwIp_(ETH0); if (strip.compare("0.0.0.0") != 0) { - zlog_info(zct, "[IpAddrInit] eth0: %s", strip.c_str()); + //zlog_info(zct, "[IpAddrInit] eth0: %s", strip.c_str()); return strip; } strip = GetGwIp_(USB0); if (strip.compare("0.0.0.0") != 0) { - zlog_info(zct, "[IpAddrInit] usb0: %s", strip.c_str()); + //zlog_info(zct, "[IpAddrInit] usb0: %s", strip.c_str()); return strip; } strip = GetGwIp_(WWAN0); if (strip.compare("0.0.0.0") != 0) { - zlog_info(zct, "[IpAddrInit] wwan0: %s", strip.c_str()); + //zlog_info(zct, "[IpAddrInit] wwan0: %s", strip.c_str()); return strip; } zlog_warn(zct, "[IpAddrInit] ip: %s", strip.c_str()); diff --git a/common/common_func.hpp b/common/common_func.hpp index 66fc6bb..0cd0b70 100644 --- a/common/common_func.hpp +++ b/common/common_func.hpp @@ -72,7 +72,17 @@ struct compressWaveChannel { CountZ = 0; } }; - +struct WaveChannel { + std::vector WaveChannelX; + std::vector WaveChannelY; + std::vector WaveChannelZ; + std::string wave_timestamp; + WaveChannel() { + WaveChannelX.reserve(25600); + WaveChannelY.reserve(25600); + WaveChannelZ.reserve(48000); + } +}; struct DevDataOfGwid { std::string mDevdata; std::string mDevid; diff --git a/common/parameter_defination.hpp b/common/parameter_defination.hpp index 05e6b71..5615042 100644 --- a/common/parameter_defination.hpp +++ b/common/parameter_defination.hpp @@ -101,8 +101,10 @@ struct Param_27 { int mMode; std::string mCmdSerial; std::string mDataNodeNo; + std::string mShortAddr; + std::string mMeasurementID; std::string mType; - Param_27() : mMode(0), mCmdSerial(""), mDataNodeNo(""), mType(""){}; + Param_27() : mMode(0), mCmdSerial(""), mDataNodeNo(""),mShortAddr(""),mMeasurementID(""), mType(""){}; }; struct Param_28 { diff --git a/jsonparse/web_cmd_parse2.cpp b/jsonparse/web_cmd_parse2.cpp index 068b529..3e27b1d 100644 --- a/jsonparse/web_cmd_parse2.cpp +++ b/jsonparse/web_cmd_parse2.cpp @@ -7,6 +7,7 @@ #include "platform/platform_init.hpp" #include "utility/md5.h" #include "utility/calculation.hpp" +#include "scheduler/schedule.hpp" extern zlog_category_t *zct; @@ -271,16 +272,25 @@ std::string JsonData::JsonCmd_Cgi_27(Param_27 ¶m) { if (param.mDataNodeNo.length() > 0 && param.mType == "DELETE") { sprintf(whereCon, "%s= '%s'", T_SENSOR_INFO(DATANODENO), param.mDataNodeNo.c_str()); sqlite_db_ctrl::instance().DeleteTableData(T_SENSOR_INFO(TNAME), whereCon); + memset(whereCon,0,sizeof(whereCon)); + sprintf(whereCon, "%s= '%s'", T_SENSOR_INFO(DATANODENO), param.mMeasurementID.c_str()); sqlite_db_ctrl::instance().DeleteTableData(T_DATA_INFO(TNAME), whereCon); sqlite_db_ctrl::instance().DeleteTableData(T_DATASTATIC_INFO(TNAME), whereCon); sqlite_db_ctrl::instance().DeleteTableData(T_DATANODE_TIME(TNAME), whereCon); sqlite_db_ctrl::instance().DeleteTableData(T_BATTERY_INFO(TNAME), whereCon); + sqlite_db_ctrl::instance().DeleteTableData(" t_battery_history ", whereCon); + memset(whereCon,0,sizeof(whereCon)); + sprintf(whereCon, "channelID like'%s'", param.mDataNodeNo.c_str()); + sqlite_db_ctrl::instance().DeleteTableData(" t_data_waveSend ", whereCon); char szTableName[50] = {0x00}; sprintf(szTableName, "DROP TABLE t_data_%s", param.mDataNodeNo.c_str()); sqlite_db_ctrl::instance().CreateTable(szTableName); memset(szTableName, 0x00, sizeof(szTableName)); sprintf(szTableName, "DROP TABLE t_dataStatic_%s", param.mDataNodeNo.c_str()); sqlite_db_ctrl::instance().CreateTable(szTableName); + + scheduler::instance().ClearScheduleCfg(atoi(param.mShortAddr.c_str())); + } else if (param.mDataNodeNo.length() > 0 && param.mType == "CORRECT") { char updateSql[1024] = {0}; sprintf(whereCon, "%s= '%s'", T_SENSOR_INFO(DATANODENO), param.mDataNodeNo.c_str()); diff --git a/jsonparse/web_cmd_parse3.cpp b/jsonparse/web_cmd_parse3.cpp index 199c093..328c5b9 100644 --- a/jsonparse/web_cmd_parse3.cpp +++ b/jsonparse/web_cmd_parse3.cpp @@ -618,9 +618,9 @@ std::string JsonData::JsonCmd_Cgi_60(Param_60 ¶m){ jsonVal["message"] = ""; char file_path[64]={0}; char cmd[64]={0}; - sprintf(cmd,"mv /opt/%s /opt/DataNode/",param.fileName.c_str()); + sprintf(cmd, "mv /opt/%s /opt/DataNode/",param.fileName.c_str()); system(cmd); - sprintf(file_path,"/opt/DataNodeNo/%s",file_path); + sprintf(file_path, "/opt/DataNodeNo/%s",param.fileName.c_str()); FILE * pFile=NULL; size_t thisSize = 0; char *buffer=NULL; diff --git a/platform/platform_init.cpp b/platform/platform_init.cpp index 0f43262..a5a9b46 100644 --- a/platform/platform_init.cpp +++ b/platform/platform_init.cpp @@ -14,7 +14,7 @@ int GlobalConfig::LinkStatus_G = 0; int GlobalConfig::LinkCount = 0; int GlobalConfig::net0Status = 1; -std::string GlobalConfig::Version = "3.2.5"; +std::string GlobalConfig::Version = "5.0"; std::string GlobalConfig::MacAddr_G = ""; std::string GlobalConfig::MacAddr_G2 = ""; std::string GlobalConfig::IpAddr_G = ""; @@ -29,6 +29,7 @@ int GlobalConfig::threadStatus = 1; int GlobalConfig::day = 0; extern std::map g_mapCompress; +extern std::map g_mapWaveChannel; TopicList GlobalConfig::Topic_G; ZigbeeInfo GlobalConfig::ZigbeeInfo_G; @@ -98,10 +99,12 @@ void PlatformInit::Init() { zlog_error(zbt, "PlatFormInit exception happend."); std::string errorinfo = "系统初始化异常"; } - vec_t vecResult = sqlite_db_ctrl::instance().GetDataMultiLineOfOneColumn(T_SENSOR_INFO(TNAME), " zigbeeShortAddr ", NULL); - for (size_t i = 0; i < vecResult.size(); i++) { + array_t arrResult = sqlite_db_ctrl::instance().GetDataMultiLineTransaction(T_SENSOR_INFO(TNAME), " MeasurementID,zigbeeShortAddr ", NULL); + for (size_t i = 0; i < arrResult.size(); i++) { compressWaveChannel tempchannel; - g_mapCompress.insert(std::make_pair(vecResult[0], tempchannel)); + WaveChannel tempwavechannel; + g_mapCompress.insert(std::make_pair(arrResult[i][1], tempchannel)); + g_mapWaveChannel.insert(std::make_pair(arrResult[i][0], tempwavechannel)); } } diff --git a/threadfunc/thread_func.cpp b/threadfunc/thread_func.cpp index 9c7004a..5d21211 100644 --- a/threadfunc/thread_func.cpp +++ b/threadfunc/thread_func.cpp @@ -75,7 +75,7 @@ void HeartRep() { GlobalConfig::serverStatus = 0; gpio_set(GlobalConfig::GPIO_G.errorLed, 0); } - sleep(10); + sleep(30); } } diff --git a/uart/uart.cpp b/uart/uart.cpp index bf0e2fd..4d80d02 100644 --- a/uart/uart.cpp +++ b/uart/uart.cpp @@ -540,18 +540,27 @@ void Uart::DealDataNodeName(const char *pData) { if (vecRes.size() > 1) { for (size_t i = 0; i < vecRes.size(); i++) { if (vecRes[i][1] != "") { - char whereCon1[64] = {0}; - sprintf(whereCon1, " dataNodeNo='%s' ", vecRes[i][0].c_str()); - sqlite_db_ctrl::instance().DeleteTableData(T_SENSOR_INFO(TNAME), whereCon1); - sqlite_db_ctrl::instance().DeleteTableData(T_DATA_INFO(TNAME), whereCon1); - sqlite_db_ctrl::instance().DeleteTableData(T_DATASTATIC_INFO(TNAME), whereCon1); - sqlite_db_ctrl::instance().DeleteTableData(T_DATANODE_TIME(TNAME), whereCon1); + memset(whereCon,0,sizeof(whereCon)); + sprintf(whereCon, "%s= '%s'", T_SENSOR_INFO(DATANODENO), vecRes[i][0].c_str()); + sqlite_db_ctrl::instance().DeleteTableData(T_SENSOR_INFO(TNAME), whereCon); + memset(whereCon,0,sizeof(whereCon)); + sprintf(whereCon, "%s= '%s'", T_SENSOR_INFO(DATANODENO), vecRes[i][1].c_str()); + sqlite_db_ctrl::instance().DeleteTableData(T_DATA_INFO(TNAME), whereCon); + sqlite_db_ctrl::instance().DeleteTableData(T_DATASTATIC_INFO(TNAME), whereCon); + sqlite_db_ctrl::instance().DeleteTableData(T_DATANODE_TIME(TNAME), whereCon); + sqlite_db_ctrl::instance().DeleteTableData(T_BATTERY_INFO(TNAME), whereCon); + sqlite_db_ctrl::instance().DeleteTableData(" t_battery_history ", whereCon); + memset(whereCon,0,sizeof(whereCon)); + sprintf(whereCon, "channelID like'%s'", vecRes[i][1].c_str()); + sqlite_db_ctrl::instance().DeleteTableData(" t_data_waveSend ", whereCon); char szTableName[50] = {0x00}; - sprintf(szTableName, "DROP TABLE t_data_%s", vecRes[i][0].c_str()); + sprintf(szTableName, "DROP TABLE t_data_%s", vecRes[i][1].c_str()); sqlite_db_ctrl::instance().CreateTable(szTableName); memset(szTableName, 0x00, sizeof(szTableName)); - sprintf(szTableName, "DROP TABLE t_dataStatic_%s", vecRes[i][0].c_str()); + sprintf(szTableName, "DROP TABLE t_dataStatic_%s", vecRes[i][1].c_str()); sqlite_db_ctrl::instance().CreateTable(szTableName); + scheduler::instance().ClearScheduleCfg(atoi(szShortAdd)); + } } } diff --git a/uart/uart.hpp b/uart/uart.hpp index a58eff9..439115c 100644 --- a/uart/uart.hpp +++ b/uart/uart.hpp @@ -31,6 +31,16 @@ enum InteractiveCommand { WAVE_COMPRESS = 15, // 波形数据压缩 UPGRADE_FIRMWARE = 16 //固件升级内容 }; + +typedef enum { + kUpgradeSuccess = 0, // 成功 + kUpgradeIndexError = 1, //包接收序号错误,要重试 + kProductTypeMismatch = 2, // 包有问题,不再重试 + kZigbeeHWMismatch = 3, // 包有问题,不再重试 + kTransmitFileCrcError = 4, // 此返回值时,要重试 + kRecvDataLenError = 5, // 此返回值时,要重试 + kUpgradeDoneBefore = 6 // 当前就是这个版本,不需要升级了 +} FirmFileCheckResult; // 无线传感器请求任务 typedef struct { uint8_t cmd; // kAskTask @@ -138,6 +148,7 @@ typedef struct { uint8_t cmd; uint8_t success; // 0:成功, 1:其它失败,需要重发 } NormalResp; + class Uart { public: Uart(); diff --git a/uart/uart_feature_parse.cpp b/uart/uart_feature_parse.cpp index a645109..ea2f418 100644 --- a/uart/uart_feature_parse.cpp +++ b/uart/uart_feature_parse.cpp @@ -18,6 +18,12 @@ std::vector g_VecWaveDataX; std::vector g_VecWaveDataY; std::vector g_VecWaveDataZ; std::map g_mapCompress; +std::map g_mapWaveChannel; + +unsigned char data[1024 * 30] = {0x00}; +unsigned char outdata[1024 * 50] = {0x00}; +unsigned char dealdata[1024 * 30] = {0x00}; +char mqttData[512000] = {0}; void Uart::RecordBattery(std::string &strLongAddr, DataRecvStatic &dataStatic, std::string &nowTimetamp) { char insertSql[1024] = {0}; @@ -60,7 +66,6 @@ void Uart::DealDataNodeFeature(const char *pData, int flag) { if (!bSendTimeStamp) { bSendTimeStamp = true; - //modify_distaddr_info(0x9999, (char*)"", pRecvData->ShortAddr); //临时参数配置 mssleep(10000); zlog_info(zct, "Zigbee Signal !\n"); @@ -76,10 +81,6 @@ void Uart::DealDataNodeFeature(const char *pData, int flag) { return; } - // GlobalConfig::ZigbeeInfo_G.MyAddr = "9999"; - // GlobalConfig::Zigbee_G.MyAddr = 0x9999; - // std::string strTime = GetLocalTimeWithMs(); - m_strDestShortAddr = std::string(buf); @@ -805,10 +806,11 @@ void Uart::DealWaveThread() { } } std::vector Uart::DealData(int iChannel, float coe, unsigned int sampleRate, int ACCSampleTime, std::string strProduct) { + + memset(data,0,sizeof(data)); + memset(dealdata,0,sizeof(dealdata)); + memset(outdata,0,sizeof(outdata)); size_t waveCount = 0; - unsigned char data[1024 * 100] = {0x00}; - unsigned char outdata[1024 * 100] = {0x00}; - unsigned char dealdata[1024 * 100] = {0x00}; long unsigned int new_len = 0, deallen = 0; int compress = 0; size_t count = 0; @@ -1033,51 +1035,61 @@ void Uart::WriteDatFile(int sampleRate, std::string &strMeasurementID, int iChan GetTimeNet(localtimestamp, 1); std::string nowTimetamp = std::string(localtimestamp); std::string strChannelID = ""; - switch (iChannel) { - case WAVE_X: { - strFileName = "/opt/data/" + strMeasurementID + "-X.dat"; - strChannelID = strMeasurementID + "-X"; - } break; - case WAVE_Y: { - strFileName = "/opt/data/" + strMeasurementID + "-Y.dat"; - strChannelID = strMeasurementID + "-Y"; - } break; - case WAVE_Z: { - strFileName = "/opt/data/" + strMeasurementID + "-Z.dat"; - strChannelID = strMeasurementID + "-Z"; - } break; - default: break; - } - if (access(strFileName.c_str(), 0) > 0) { //如果存在原始数据删除原来的,只保留一份 - std::string strCmd = "rm " + strFileName; - system(strCmd.c_str()); - } - - FILE *fp = fopen(strFileName.c_str(), "w"); - fwrite(localtimestamp, sizeof(localtimestamp), 1, fp); - zlog_info(zct, "fopen FIle vecData.size : %d", vecData.size()); float mean = Calculation::mean(vecData); float frTemp; char buf[33] = {0x00}; std::string strWaveData = ""; + WaveChannel wave_channel; + + memset(mqttData,0,sizeof(mqttData)); + switch (iChannel) { + case WAVE_X: { + strFileName = "/opt/data/" + strMeasurementID + "-X.dat"; + strChannelID = strMeasurementID + "-X"; + + } break; + case WAVE_Y: { + strFileName = "/opt/data/" + strMeasurementID + "-Y.dat"; + strChannelID = strMeasurementID + "-Y"; + + } break; + case WAVE_Z: { + strFileName = "/opt/data/" + strMeasurementID + "-Z.dat"; + strChannelID = strMeasurementID + "-Z"; + + } break; + default: break; + } + FILE *fp = fopen(strFileName.c_str(), "w"); + fwrite(localtimestamp,sizeof(localtimestamp),1,fp); + zlog_info(zct, " vecData.size : %d,start ", vecData.size()); + int id = 0; for (size_t i = 0; i < vecData.size(); i++) { frTemp = vecData[i] - mean; - fwrite(&frTemp, sizeof(float), 1, fp); memset(buf, 0x00, sizeof(buf)); sprintf(buf, "%.2f", frTemp); - std::string waveTemp(buf); - if (i == 0) - strWaveData = waveTemp; - else - strWaveData = strWaveData + "," + waveTemp; - - if (i % 100 == 0) { - mssleep(5000); + 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 != vecData.size() -1){ + strncpy(mqttData + id ,buf,strlen(buf)); + id = id + strlen(buf); + strncpy(mqttData + id,",",1); + id = id + 1; + } + else{ + strncpy(mqttData + id ,buf,strlen(buf)); } } - fclose(fp); - + zlog_info(zct, "fopen FIle vecData.size : %d end ", vecData.size()); + wave_channel.wave_timestamp = nowTimetamp; + g_mapWaveChannel[strMeasurementID] = wave_channel; Json::Value valWaveData; valWaveData["number"] = sampleRate; valWaveData["channelId"] = strChannelID; @@ -1085,7 +1097,7 @@ void Uart::WriteDatFile(int sampleRate, std::string &strMeasurementID, int iChan valWaveData["dataNodeGatewayNo"] = GlobalConfig::MacAddr_G; valWaveData["SensorEngineeringUnit"] = ""; valWaveData["timeStamp"] = nowTimetamp; - valWaveData["waveData"] = strWaveData; + valWaveData["waveData"] = mqttData; valWaveData["mean"] = mean; Json::FastWriter WaveValue; std::string WaveData = WaveValue.write(valWaveData); diff --git a/uart/uart_parameter_config.cpp b/uart/uart_parameter_config.cpp index 16f68fc..1ffb29a 100644 --- a/uart/uart_parameter_config.cpp +++ b/uart/uart_parameter_config.cpp @@ -67,6 +67,7 @@ bool Uart::ReadUpdatePackge(unsigned short shortAdd) { zlog_info(zct, "thisindex = %d", thisindex); FILE* pFile = NULL; + char* buffer = NULL; int thisSize = 0; DataNodeUpdateFile = "/opt/DataNode/" + strFileName; zlog_info(zct, "strFileName = %s", DataNodeUpdateFile.c_str()); @@ -77,6 +78,9 @@ bool Uart::ReadUpdatePackge(unsigned short shortAdd) { while (fgetc(pFile) != EOF) { ++thisSize; } + rewind(pFile); + buffer = (char*)malloc(thisSize); + fread(buffer, sizeof(char), thisSize, pFile); fclose(pFile); }