From 7fc05f48187e58e02c20f1d3a525168b883f6505 Mon Sep 17 00:00:00 2001 From: zhangsheng Date: Tue, 27 Jan 2026 19:35:24 +0800 Subject: [PATCH] add parse eigen,wave and new scheduler --- common/common_func.hpp | 37 + common/parameter_defination.hpp | 6 +- dbaccess/sql_db.cpp | 12 +- jsonparse/cmt_parse.cpp | 17 +- jsonparse/mqtt_cmd_parse.cpp | 17 +- jsonparse/web_cmd_parse3.cpp | 60 +- localserver/web_cmd.cpp | 4 +- main.cpp | 7 +- platform/platform_init.cpp | 2 +- scheduler/schedule.cpp | 1450 +++++++++++++++++++++---------- scheduler/schedule.hpp | 135 ++- scheduler/status_mgr.cpp | 124 +++ scheduler/status_mgr.hpp | 23 + uart/uart.cpp | 65 +- uart/uart.hpp | 11 +- uart/uart_feature_parse.cpp | 134 ++- uart/uart_parameter_config.cpp | 2 +- 17 files changed, 1486 insertions(+), 620 deletions(-) create mode 100644 scheduler/status_mgr.cpp create mode 100644 scheduler/status_mgr.hpp diff --git a/common/common_func.hpp b/common/common_func.hpp index 8293914..dcccf77 100644 --- a/common/common_func.hpp +++ b/common/common_func.hpp @@ -61,16 +61,40 @@ struct compressWaveChannel { int compressChannelX; int compressChannelY; int compressChannelZ; + int compressChannelVolX; + int compressChannelVolY; + int compressChannelVolZ; int CountX; int CountY; int CountZ; + int CountVolX; + int CountVolY; + int CountVolZ; + int samplerateX; + int samplerateY; + int samplerateZ; + int samplerateVolX; + int samplerateVolY; + int samplerateVolZ; compressWaveChannel() { compressChannelX = 0; compressChannelY = 0; compressChannelZ = 0; + compressChannelVolX = 0; + compressChannelVolY = 0; + compressChannelVolZ = 0; CountX = 0; CountY = 0; CountZ = 0; + CountVolX = 0; + CountVolY = 0; + CountVolZ = 0; + samplerateX = 0; + samplerateY = 0; + samplerateZ = 0; + samplerateVolX = 0; + samplerateVolY = 0; + samplerateVolZ = 0; } }; struct WaveChannel { @@ -84,6 +108,17 @@ struct WaveChannel { WaveChannelZ.reserve(48000); } }; +struct WaveVolChannel { + std::vector WaveChannelVolX; + std::vector WaveChannelVolY; + std::vector WaveChannelVolZ; + std::string wave_timestamp; + WaveVolChannel() { + WaveChannelVolX.reserve(25600); + WaveChannelVolY.reserve(25600); + WaveChannelVolZ.reserve(48000); + } +}; struct DevDataOfGwid { std::string mDevdata; std::string mDevid; @@ -213,6 +248,8 @@ typedef struct { float Phase2; float Phase3; float Phase4; + float kurtosis; + float IntegratRMSMENS; }DataRecvDym; struct TopicList { diff --git a/common/parameter_defination.hpp b/common/parameter_defination.hpp index 7f59a1b..2efdd2a 100644 --- a/common/parameter_defination.hpp +++ b/common/parameter_defination.hpp @@ -307,12 +307,12 @@ struct Param_57 { struct Param_58 { int mMode; int featureInterVal; - int featureInterTime; int waveInterVal; - int waveInterTime; int maxSensorNum; int sensorCount; - Param_58() : mMode(0),featureInterVal(0),featureInterTime(0),waveInterVal(0),waveInterTime(0),maxSensorNum(0),sensorCount(0){}; + int waveResendNum; + int resend; + Param_58() : mMode(0),featureInterVal(0),waveInterVal(0),maxSensorNum(0),sensorCount(0),waveResendNum(0),resend(0){}; }; struct Param_59 { diff --git a/dbaccess/sql_db.cpp b/dbaccess/sql_db.cpp index 2fe4e86..876295f 100644 --- a/dbaccess/sql_db.cpp +++ b/dbaccess/sql_db.cpp @@ -171,6 +171,14 @@ void SqliteDB::SqliteInit(const char *pDbName) { if(iRet == 0){ CreateTable("ALTER TABLE t_data_info ADD COLUMN 'nodeTimeStamp'"); } + iRet = GetTableRows(" sqlite_master "," name = 't_data_info' and sql LIKE '%kurtosis%' "); + if(iRet == 0){ + CreateTable("ALTER TABLE t_data_info ADD COLUMN 'kurtosis'"); + } + iRet = GetTableRows(" sqlite_master "," name = 't_data_info' and sql LIKE '%IntegratRMSMENS%' "); + if(iRet == 0){ + CreateTable("ALTER TABLE t_data_info ADD COLUMN 'IntegratRMSMENS'"); + } // 创建传感器静态数据存储表 memset(sql_exec, 0, 2048); @@ -328,9 +336,9 @@ void SqliteDB::Createtable(const char *ptableName) { char sql_exec[2048]; // 创建传感器数据存储表 memset(sql_exec, 0, 2048); - sprintf(sql_exec, "create table if not exists %s(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s integer,%s,%s,%s,%s);", ptableName, T_DATA_INFO(DATANODENO), T_DATA_INFO(CHANNELID), T_DATA_INFO(DIAGNOSISEAK), T_DATA_INFO(INTEGRATPK), T_DATA_INFO(INTEGRATRMS), + sprintf(sql_exec, "create table if not exists %s(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s integer,%s,%s,%s,%s,%s,%s);", ptableName, T_DATA_INFO(DATANODENO), T_DATA_INFO(CHANNELID), T_DATA_INFO(DIAGNOSISEAK), T_DATA_INFO(INTEGRATPK), T_DATA_INFO(INTEGRATRMS), T_DATA_INFO(RMSVALUES), T_DATA_INFO(ENVELOPNERGY), T_DATA_INFO(AMP1), T_DATA_INFO(AMP2), T_DATA_INFO(AMP3), T_DATA_INFO(AMP4), T_DATA_INFO(AMP5), T_DATA_INFO(PHASE1), T_DATA_INFO(PHASE2), T_DATA_INFO(PHASE3), T_DATA_INFO(PHASE4), "StaticIndex", T_DATA_INFO(TIMESTAMP), - "sendMsg", "nodeResend", "nodeTimestamp"); + "sendMsg", "nodeResend", "nodeTimestamp","kurtosis","IntegratRMSMENS"); CreateTable(sql_exec); memset(sql_exec, 0, 2048); sprintf(sql_exec, "CREATE INDEX %s_1 ON %s (%s)", ptableName, ptableName, T_DATA_INFO(DATANODENO)); diff --git a/jsonparse/cmt_parse.cpp b/jsonparse/cmt_parse.cpp index eb48e68..d8c749f 100644 --- a/jsonparse/cmt_parse.cpp +++ b/jsonparse/cmt_parse.cpp @@ -48,9 +48,8 @@ void JsonData::CmtCmd_80(char* send_data,int& send_length) void JsonData::CmtCmd_81(char* recv_body,int& count,char* send_data,int& send_length) { int featureInterVal; - int featureInterTime; int waveInterVal; - int waveInterTime; + int waveResendNum; int maxSensorNum; int sensorCount; array_t arrRes; @@ -159,7 +158,7 @@ void JsonData::CmtCmd_81(char* recv_body,int& count,char* send_data,int& send_le strsql = std::string(sql) + std::string(whereCon); int static_Count = sqlite_db_ctrl::instance().GetTableRows(szTableName,strsql.c_str()); - scheduler::instance().GetScheduleConfig(featureInterVal,waveInterVal,featureInterTime,waveInterTime,maxSensorNum); + scheduler::instance().GetScheduleConfig(featureInterVal,waveInterVal,waveResendNum,maxSensorNum); zlog_info(zct,"wavex = %d,featureInterVal = %d,waveInterVal = %d",waveX_Count,featureInterVal,waveInterVal); int day_count = 86400 / waveInterVal; @@ -505,8 +504,10 @@ void JsonData::CmtCmd_86(char* recv_body,int& count,char* filename,char* file_m zlog_info(zct,"sum = %x\n",sum % 256); char localtimestamp[32] = {0}; GetTimeNet(localtimestamp, 1); + std::vector param_list; for (size_t i = 0; i < count; i++) { + UpgradeParameter upgrade_parameter; char wherecon[100] = {0}; char insertSql[200] = {0}; char updateSql[100] = {0}; @@ -532,8 +533,14 @@ void JsonData::CmtCmd_86(char* recv_body,int& count,char* filename,char* file_m uint16_t short_addr; char *end_ptr = NULL; short_addr = strtol(vecResult[3].c_str(), &end_ptr, 16); - int res = scheduler::instance().UpgradeSensor(short_addr,std::string(sensor_type),atoi(vecResult[0].c_str()),vecResult[1],std::string(sf_version)); - } + upgrade_parameter.short_addr = short_addr; + upgrade_parameter.sensor_type = std::string(sensor_type); + upgrade_parameter.hw_version = atoi(vecResult[0].c_str()); + upgrade_parameter.current_sw_version = vecResult[1]; + upgrade_parameter.upgrade_sw_version = std::string(sf_version); + param_list.push_back(upgrade_parameter); + } + int res = scheduler::instance().UpgradeSensor(param_list); free(buffer); } void JsonData::CmtCmd_87(char* MeasurementID,char* send_data,int& send_length) diff --git a/jsonparse/mqtt_cmd_parse.cpp b/jsonparse/mqtt_cmd_parse.cpp index 914b951..c4d83b5 100644 --- a/jsonparse/mqtt_cmd_parse.cpp +++ b/jsonparse/mqtt_cmd_parse.cpp @@ -628,19 +628,18 @@ int JsonData::JsonCmd_31(){ jsonVal["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; + int featureInterVal; + int waveInterVal; + int waveResendNum; + int maxSensorNum; + int sensorCount; sensorCount = sqlite_db_ctrl::instance().GetTableRows(T_SENSOR_INFO(TNAME), NULL); - scheduler::instance().GetScheduleConfig(featureInterVal,waveInterVal,featureInterTime,waveInterTime,maxSensorNum); + scheduler::instance().GetScheduleConfig(featureInterVal,waveInterVal,waveResendNum,maxSensorNum); jsBody["featureInterVal"] = featureInterVal; - jsBody["featureInterTime"] = featureInterTime; + jsBody["featureInterTime"] = 0; jsBody["waveInterVal"] = waveInterVal; - jsBody["waveInterTime"] = waveInterTime; + jsBody["waveInterTime"] = 0; jsBody["maxSensorNum"] = maxSensorNum; jsBody["sensorCount"] = sensorCount; jsonVal["cmdBody"] = jsBody; diff --git a/jsonparse/web_cmd_parse3.cpp b/jsonparse/web_cmd_parse3.cpp index d48e0e9..1c70c18 100644 --- a/jsonparse/web_cmd_parse3.cpp +++ b/jsonparse/web_cmd_parse3.cpp @@ -531,19 +531,18 @@ std::string JsonData::JsonCmd_Cgi_58(Param_58 ¶m) { std::string error_msg = ""; if (param.mMode == 1) { - ret = scheduler::instance().Config(param.featureInterVal,param.waveInterVal,param.featureInterTime,param.waveInterTime,param.maxSensorNum,error_msg); + ret = scheduler::instance().Config(param.featureInterVal,param.waveInterVal,param.maxSensorNum,param.waveResendNum,error_msg); }else if (param.mMode == 0) { sensorCount = sqlite_db_ctrl::instance().GetTableRows(T_SENSOR_INFO(TNAME), NULL); - ret = scheduler::instance().GetScheduleConfig(param.featureInterVal,param.waveInterVal,param.featureInterTime,param.waveInterTime,param.maxSensorNum); + ret = scheduler::instance().GetScheduleConfig(param.featureInterVal,param.waveInterVal,param.waveResendNum,param.maxSensorNum); } if (ret == 0) { jsBody["featureInterVal"] = param.featureInterVal; - jsBody["featureInterTime"] = param.featureInterTime; jsBody["waveInterVal"] = param.waveInterVal; - jsBody["waveInterTime"] = param.waveInterTime; jsBody["maxSensorNum"] = param.maxSensorNum; + jsBody["resend"] = param.resend; jsBody["sensorCount"] = sensorCount; }else{ jsonVal["success"] = false; @@ -562,11 +561,11 @@ std::string JsonData::JsonCmd_Cgi_59(Param_59 ¶m) { char table_name[50] ={0}; char sql[1024]={0}; int featureInterVal = 0; - int featureInterTime = 0; int waveInterVal = 0; - int waveInterTime = 0; + int waveResendNum = 0; int maxSensorNum = 0; - scheduler::instance().GetScheduleConfig(featureInterVal,waveInterVal,featureInterTime,waveInterTime,maxSensorNum); + int sensorCount = 0; + scheduler::instance().GetScheduleConfig(featureInterVal,waveInterVal,waveResendNum,maxSensorNum); if (param.mMode == 1) { @@ -726,8 +725,10 @@ std::string JsonData::JsonCmd_Cgi_60(Param_60 ¶m){ zlog_info(zct,"sum = %x\n",sum % 256); char localtimestamp[32] = {0}; GetTimeNet(localtimestamp, 1); + std::vector param_list; for (size_t i = 0; i < param.dataNodeNo.size(); i++) { + UpgradeParameter upgrade_parameter; char wherecon[100] = {0}; char insertSql[200] = {0}; char updateSql[100] = {0}; @@ -751,7 +752,14 @@ std::string JsonData::JsonCmd_Cgi_60(Param_60 ¶m){ uint16_t short_addr; char *end_ptr = NULL; short_addr = strtol(vecResult[3].c_str(), &end_ptr, 16); - int res = scheduler::instance().UpgradeSensor(short_addr,std::string(sensor_type),atoi(vecResult[0].c_str()),vecResult[1],std::string(sf_version)); + upgrade_parameter.short_addr = short_addr; + upgrade_parameter.sensor_type = std::string(sensor_type); + upgrade_parameter.hw_version = atoi(vecResult[0].c_str()); + upgrade_parameter.current_sw_version = vecResult[1]; + upgrade_parameter.upgrade_sw_version = std::string(sf_version); + param_list.push_back(upgrade_parameter); + } + int res = scheduler::instance().UpgradeSensor(param_list); if (res != 0) { jsonVal["success"] = false; @@ -759,8 +767,6 @@ std::string JsonData::JsonCmd_Cgi_60(Param_60 ¶m){ free(buffer); return show_value_.write(jsonVal); } - - } free(buffer); return show_value_.write(jsonVal); @@ -1096,8 +1102,8 @@ std::string JsonData::JsonCmd_Cgi_65(Param_65 ¶m){ if (nSize > 0) { int interval = atol(param.timeEnd.c_str()) - atol(param.timeStart.c_str()); - int featureInterVal = 0,waveInterVal = 0,featureInterTime = 0,waveInterTime = 0,maxSensorNum = 0; - scheduler::instance().GetScheduleConfig(featureInterVal,waveInterVal,featureInterTime,waveInterTime,maxSensorNum); + int featureInterVal = 0,waveInterVal = 0,waveRsendNum = 0,maxSensorNum = 0; + scheduler::instance().GetScheduleConfig(featureInterVal,waveInterVal,waveRsendNum,maxSensorNum); Json::Value jsBody; char count_sql[1024] = {0}; int recive_feature = 0, send_feature = 0,resend_feature = 0; @@ -1277,7 +1283,7 @@ std::string JsonData::JsonCmd_Cgi_67(Param_67 ¶m){ char column[128] = {0}; char tablename[256] = {0}; sprintf(selectCon, " t_sensor_info.MeasurementID='%s' ", strMeasurementID.c_str()); - sprintf(column, " _debug_info.*,t_sensor_info.status,t_sensor_info.dataNodeName "); + sprintf(column, " t_debug_info.*,t_sensor_info.status,t_sensor_info.dataNodeName "); sprintf(tablename, " t_debug_info LEFT JOIN t_sensor_info "); array_t arrRes; arrRes = sqlite_db_ctrl::instance().GetDataMultiLineTransaction(tablename, "*", selectCon); @@ -1285,7 +1291,7 @@ std::string JsonData::JsonCmd_Cgi_67(Param_67 ¶m){ if (iResult > 0) { for (int j = 0; j < iResult; j++) { Json::Value jsChannelData; - jsChannelData["dataNodeName"] = arrRes[j][9]; + jsChannelData["dataNodeName"] = arrRes[j][10]; jsChannelData["zigbeeAddr"] = arrRes[j][1]; jsChannelData["MeasurementID"] = arrRes[j][0]; jsChannelData["status"] = atoi(arrRes[j][0].c_str()); @@ -1304,7 +1310,7 @@ std::string JsonData::JsonCmd_Cgi_67(Param_67 ¶m){ jsonVal["success"] = false; jsonVal["content"].resize(0); } else { - jsonVal["content"] = (jsSensor); + jsonVal["content"]["dataNodeArray"] = (jsSensor); } }else { jsonVal["success"] = false; @@ -1318,7 +1324,7 @@ std::string JsonData::JsonCmd_Cgi_67(Param_67 ¶m){ char updateSql[256] = {0}; sprintf(whereCon, " MeasurementID = '%s' ", param.measurementID[i].c_str()); sprintf(updateSql, " status = '%d' ", 1);// 0 未调试,1 调试中,2 调试完成 - sqlite_db_ctrl::instance().UpdateTableData(T_SENSOR_INFO(TNAME), updateSql, whereCon); + sqlite_db_ctrl::instance().UpdateTableData("t_debug_info", updateSql, whereCon); //uint16_t short_addr; //char *end_ptr = NULL; //vec_t vecResult = sqlite_db_ctrl::instance().GetDataSingleLine(T_SENSOR_INFO(TNAME), " zigbeeShortAddr ", whereCon); @@ -1339,6 +1345,8 @@ std::string JsonData::JsonCmd_Cgi_68(Param_68 ¶m){ jsonVal["success"] = true; jsonVal["message"] = " "; int lowSignal = -1, signalThreshold = -1, lowBatteryLevel = -1, batteryLevelThreshold = -1; + char whereCon[512] = {0}; + char updateSql[256] = {0}; if (param.mMode == 1) { lowSignal = writeIntValue("config", "lowSignal", param.lowSignal, (char *)GlobalConfig::Config_G.c_str()); signalThreshold = writeIntValue("config", "signalThreshold", param.signalThreshold, (char *)GlobalConfig::Config_G.c_str()); @@ -1348,8 +1356,8 @@ std::string JsonData::JsonCmd_Cgi_68(Param_68 ¶m){ if(param.vecParam68.size() > 0){ for (size_t i = 0; i < param.vecParam68.size(); i++) { - char whereCon[512] = {0}; - char updateSql[256] = {0}; + memset(whereCon,0,sizeof(whereCon)); + memset(updateSql,0,sizeof(updateSql)); sprintf(whereCon, " MeasurementID = '%s' ", param.vecParam68[i].measurementID.c_str()); sprintf(updateSql, " status = '%d',statisticType = '%d', threshold = '%f'",param.vecParam68[i].status,param.vecParam68[i].statisticType,param.vecParam68[i].threshold); sqlite_db_ctrl::instance().UpdateTableData(" t_waveUploadRule_info ", updateSql, whereCon); @@ -1360,7 +1368,7 @@ std::string JsonData::JsonCmd_Cgi_68(Param_68 ¶m){ signalThreshold = readIntValue("config", "signalThreshold", (char *)GlobalConfig::Config_G.c_str()); lowBatteryLevel = readIntValue("config", "lowBatteryLevel", (char *)GlobalConfig::Config_G.c_str()); batteryLevelThreshold = readIntValue("config", "batteryLevelThreshold", (char *)GlobalConfig::Config_G.c_str()); - array_t arrRes = sqlite_db_ctrl::instance().GetDataMultiLineTransaction(" t_waveUploadRule_info ", " * ", NULL); + array_t arrRes = sqlite_db_ctrl::instance().GetDataMultiLineTransaction(" t_waveUploadRule_info LEFT JOIN t_sensor_info ", " t_waveUploadRule_info.*,t_sensor_info.dataNodeName ", "t_waveUploadRule_info.MeasurementID = t_sensor_info.MeasurementID"); if (arrRes.size() > 0) { for (size_t i = 0; i < arrRes.size(); i++) { @@ -1369,6 +1377,7 @@ std::string JsonData::JsonCmd_Cgi_68(Param_68 ¶m){ iTem.append(atoi(arrRes[i][2].c_str())); iTem.append(atoi(arrRes[i][3].c_str())); iTem.append(atof(arrRes[i][4].c_str())); + iTem.append(atof(arrRes[i][5].c_str())); dataNodeArray.append(iTem); } } @@ -1395,13 +1404,15 @@ std::string JsonData::JsonCmd_Cgi_69(Param_69 ¶m){ jsonVal["success"] = true; jsonVal["message"] = " "; int trigerThresholdEnable = -1; + char whereCon[512] = {0}; + char updateSql[256] = {0}; if (param.mMode == 1) { trigerThresholdEnable = writeIntValue("config", "trigerThresholdEnable", param.trigerThresholdEnable, (char *)GlobalConfig::Config_G.c_str()); if(param.vecParam69.size() > 0){ for (size_t i = 0; i < param.vecParam69.size(); i++) { - char whereCon[512] = {0}; - char updateSql[256] = {0}; + memset(whereCon,0,sizeof(whereCon)); + memset(updateSql,0,sizeof(updateSql)); sprintf(whereCon, " MeasurementID = '%s' ", param.vecParam69[i].measurementID.c_str()); sprintf(updateSql, " status = '%d',trigerType = '%d',statisticType = '%d', threshold = '%f'",param.vecParam69[i].status,param.vecParam69[i].trigerType,param.vecParam69[i].statisticType,param.vecParam69[i].threshold); sqlite_db_ctrl::instance().UpdateTableData(" t_waveTriger_info ", updateSql, whereCon); @@ -1409,7 +1420,7 @@ std::string JsonData::JsonCmd_Cgi_69(Param_69 ¶m){ } } else if (param.mMode == 0) { trigerThresholdEnable = readIntValue("config", "trigerThresholdEnable", (char *)GlobalConfig::Config_G.c_str()); - array_t arrRes = sqlite_db_ctrl::instance().GetDataMultiLineTransaction(" t_wave_triger_info ", " * ", NULL); + array_t arrRes = sqlite_db_ctrl::instance().GetDataMultiLine(" t_wave_triger_info LEFT JOIN t_sensor_info ", " t_wave_triger_info.*,t_sensor_info.dataNodeName ", "t_wave_triger_info.MeasurementID = t_sensor_info.MeasurementID"); if (arrRes.size() > 0) { for (size_t i = 0; i < arrRes.size(); i++) { @@ -1419,7 +1430,9 @@ std::string JsonData::JsonCmd_Cgi_69(Param_69 ¶m){ iTem.append(atoi(arrRes[i][3].c_str())); iTem.append(atoi(arrRes[i][4].c_str())); iTem.append(atof(arrRes[i][5].c_str())); - jsonVal["content"]["dataNodeArray"].append(iTem); + iTem.append(arrRes[i][6]); + dataNodeArray.append(iTem); + } } if (dataNodeArray.size() == 0) { @@ -1428,6 +1441,7 @@ std::string JsonData::JsonCmd_Cgi_69(Param_69 ¶m){ } } jsonVal["content"]["trigerThresholdEnable"] = trigerThresholdEnable; + jsonVal["content"]["dataNodeArray"] = dataNodeArray; return show_value_.write(jsonVal); } std::string JsonData::JsonCmd_Cgi_70(Param_70 ¶m){ diff --git a/localserver/web_cmd.cpp b/localserver/web_cmd.cpp index 1b78676..45cc1d9 100644 --- a/localserver/web_cmd.cpp +++ b/localserver/web_cmd.cpp @@ -389,10 +389,10 @@ std::string LocalServer::HandleCgi_cmd(std::string &pData) { param.mMode = 0; } param.featureInterVal = recvBody["featureInterVal"].asInt(); - param.featureInterTime = recvBody["featureInterTime"].asInt(); param.waveInterVal = recvBody["waveInterVal"].asInt(); - param.waveInterTime = recvBody["waveInterTime"].asInt(); param.maxSensorNum = recvBody["maxSensorNum"].asInt(); + param.waveResendNum = recvBody["waveResendNum"].asInt(); + param.resend = recvBody["resend"].asInt(); std::string data = jd.JsonCmd_Cgi_58(param); return data; }break; diff --git a/main.cpp b/main.cpp index 488e646..a3eda81 100644 --- a/main.cpp +++ b/main.cpp @@ -22,7 +22,9 @@ extern std::vector g_VecWaveDataX; extern std::vector g_VecWaveDataY; extern std::vector g_VecWaveDataZ; - +extern std::vector g_VecWaveDataVolX; +extern std::vector g_VecWaveDataVolY; +extern std::vector g_VecWaveDataVolZ; zlog_category_t *zct = NULL; zlog_category_t *zbt = NULL; @@ -47,6 +49,9 @@ int main(int argc, char *argv[]) { g_VecWaveDataX.reserve(1000); g_VecWaveDataY.reserve(1000); g_VecWaveDataZ.reserve(1500); + g_VecWaveDataVolX.reserve(1000); + g_VecWaveDataVolY.reserve(1000); + g_VecWaveDataVolZ.reserve(1500); boost::thread::attributes attrs; attrs.set_stack_size(1024 * 1024); diff --git a/platform/platform_init.cpp b/platform/platform_init.cpp index 0728894..109756e 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 = "5.7.1"; +std::string GlobalConfig::Version = "5.8"; std::string GlobalConfig::MacAddr_G = ""; std::string GlobalConfig::MacAddr_G2 = ""; std::string GlobalConfig::IpAddr_G = ""; diff --git a/scheduler/schedule.cpp b/scheduler/schedule.cpp index 18ca09c..e8572a9 100644 --- a/scheduler/schedule.cpp +++ b/scheduler/schedule.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -17,7 +18,7 @@ extern zlog_category_t *zbt; uint8_t g_x, g_y, g_z; -int SensorScheduler::StartSchedule(int short_addr, int &next_duration, int &next_task_id) { +int SensorScheduler::StartSchedule(uint16_t short_addr, int &next_duration, bool &z) { int id = 0; auto iter = short_addr_map_.find(short_addr); if (iter == short_addr_map_.end()) { @@ -36,7 +37,7 @@ int SensorScheduler::StartSchedule(int short_addr, int &next_duration, int &next seconds_in_current_eigen_slice_ = seconds_in_current_wave_slice_ % eigen_value_send_interval_; ts_in_eigen_slice_ = false; - if (seconds_in_current_eigen_slice_ < eigen_value_slice_total_seconds_ - 3) { + if (seconds_in_current_eigen_slice_ < 60 - 3) { ts_in_eigen_slice_ = true; } @@ -46,309 +47,394 @@ int SensorScheduler::StartSchedule(int short_addr, int &next_duration, int &next ClearFailureSuccessMap(); } } else { - nth_wave_slice_ = (seconds_in_current_eigen_slice_ - eigen_value_slice_total_seconds_ + 3) / seconds_per_wave_slice_; + nth_wave_slice_ = (seconds_in_current_eigen_slice_ - 60 + 3) / seconds_per_wave_slice_; } - zlog_warn(zct, "[%d:%x] ts:%ld, current utc:%s, nth eigen_value slice:%d, seconds in eigen slice:%d, eigen slice:%d", + zlog_debug(zbt, "[%d:%x] ts:%ld, current utc:%s, nth eigen_value slice:%d, seconds in eigen slice:%d, eigen slice:%d", id, short_addr, current_ts_, GetUTCTime(current_ts_).c_str(), nth_eigen_value_slice_+1, seconds_in_current_eigen_slice_, ts_in_eigen_slice_); - if (ts_in_eigen_slice_) { - // if (id == nth_eigen_slice_ + 1) { - // 传感器需要执行上送特征值任务, 如果有配置需要下发的话,下发配置 - if (update_.count(id)) { - // execute config - zlog_warn(zct, "[%d:%x] update config in eigen slice", id, short_addr); - current_request_ = kScheduleConfigSensor; - return kScheduleConfigSensor; - } else { - // wave_feature_set_inst::instance().GetFeatureCfg(short_addr, g_x, g_y, g_z); - // if (g_x || g_y || g_z) { - // // 执行上送特征值任务 - // zlog_warn(zct, "[%d:%x] send eigen value in eigen slice", id, short_addr); - // current_request_ = kScheduleEigenValue; - // return kScheduleEigenValue; - // } else { - next_duration = GetNextDuration(short_addr, next_task_id); - zlog_warn(zct, "[%d:%x] no need for eigen", id, short_addr); - return kScheduleResultNone; - // } - } - // } else { - // zlog_warn(zct, "[%d:%x] Invalid request, revive in %d eigen slice", id, short_addr, nth_eigen_slice_ + 1); - // if (id < nth_eigen_slice_ + 1) { - // // 不正确的请求 - // long available_ts = current_wave_start_ts_ + (nth_eigen_value_slice_ + 1) * eigen_value_send_interval_ + (id - 1) * eigen_value_send_duration_; - // next_duration = available_ts - current_ts_; - // zlog_warn(zct, "[%d:%x] wrong time in eigen slice, next feature in next interval send utc time:[%s], duration:%d", id, short_addr, GetUTCTime(available_ts).c_str(), next_duration); - // } else { - // long available_ts = current_wave_start_ts_ + nth_eigen_value_slice_ * eigen_value_send_interval_ + (id - 1) * eigen_value_send_duration_; - // next_duration = available_ts - current_ts_; - // zlog_warn(zct, "[%d:%x] wrong time in eigen slice, next feature in current interval send utc time:[%s], duration:%d", id, short_addr, GetUTCTime(available_ts).c_str(), next_duration); - // } - // return kScheduleWrongTime; - // } + if (ts_in_eigen_slice_) { + // 传感器需要执行上送特征值任务, 如果有配置需要下发的话,下发配置 + if (update_.count(id)) { + // execute config + zlog_debug(zbt, "[%d:%x] update config in eigen slice", id, short_addr); + current_request_ = kScheduleConfigSensor; + return kScheduleConfigSensor; + } else { + wave_feature_set_inst::instance().GetFeatureCfg(short_addr, g_x, g_y, g_z); + if (g_x || g_y || g_z) { + // 执行上送特征值任务 + zlog_debug(zbt, "[%d:%x] send eigen value in eigen slice", id, short_addr); + current_request_ = kScheduleEigenValue; + return kScheduleEigenValue; + } else { + next_duration = GetNextDuration(short_addr); + zlog_warn(zbt, "[%d:%x] no need for eigen", id, short_addr); + return kScheduleWrongTime; + } + } } else { - int nth_wave_slice = nth_eigen_value_slice_ * wave_slice_num_per_eigen_interval_ + nth_wave_slice_ + 1; + if (current_schedule_status_ == kScheduleStatusDebug) { + if (debug_list_.count(short_addr) == 0) { + next_duration = GetDebugUpgradeNextDuration(short_addr); + zlog_debug(zbt, "[%d:%x] not in debug list", id, short_addr); + return kScheduleWrongTime; + } else { + // z wave + int nth_wave_slice = nth_eigen_value_slice_ * wave_slice_num_per_eigen_interval_ + nth_wave_slice_ + 1; + if (debug_slice_sensor_id_[nth_wave_slice] == short_addr) { + current_request_ = kScheduleWaveForm; + z = true; + return kScheduleWaveForm; + } else { + // 当前特征值间隔内是否存在此传感器的波形区间 + for (int i = nth_wave_slice + 1; i <= (nth_eigen_value_slice_+1) * wave_slice_num_per_eigen_interval_; ++i) { + if (debug_slice_sensor_id_[i] == short_addr) { + long nxt_ts = current_wave_start_ts_ + nth_eigen_value_slice_ * eigen_value_send_interval_ + 60 + (i-nth_wave_slice)*60; + next_duration = nxt_ts - current_ts_; + if (next_duration < 10) { + zlog_debug(zbt, "[%d:%x] [Nxt] debug exception duration:%d, adjust to 25", id, short_addr); + next_duration = 25; + } else if (next_duration > eigen_value_send_interval_) { + zlog_debug(zbt, "[%d:%x] [Nxt] debug exception duration:%d, adjust to 120", id, short_addr); + next_duration = 120; + } + return kScheduleWrongTime; + } + } + + next_duration = GetDebugUpgradeNextDuration(short_addr); + zlog_debug(zbt, "[%d:%x] debug wrong time", id, short_addr); + return kScheduleWrongTime; + } + } + return 0; + } else if (current_schedule_status_ == kScheduleStatusUpgrade) { + if (upgrade_list_.count(short_addr) == 0) { + next_duration = GetDebugUpgradeNextDuration(short_addr); + zlog_debug(zbt, "[%d:%x] not in upgrade list", id, short_addr); + return kScheduleWrongTime; + } else { + int nth_wave_slice = nth_eigen_value_slice_ * wave_slice_num_per_eigen_interval_ + nth_wave_slice_ + 1; + if (upgrade_slice_sensor_id_[nth_wave_slice] == short_addr) { + current_request_ = kScheduleUpgrade; + upgrade_list_.erase(short_addr); + return kScheduleUpgrade; + } else { + next_duration = GetDebugUpgradeNextDuration(short_addr); + zlog_debug(zbt, "[%d:%x] in wrong time", id, short_addr); + return kScheduleWrongTime; + } + } + } + int nth_wave_slice = nth_eigen_value_slice_ * wave_slice_num_per_eigen_interval_ + nth_wave_slice_ + 1; // 从1开始编号 auto wave_slice_iter = sensor_id_nth_slice_.find(id); if (wave_slice_iter == sensor_id_nth_slice_.end()) { - zlog_error(zct, "[%d:%x]invaild id, not find wave slice id, need to check further", id, short_addr); + zlog_error(zbt, "[%d:%x] invaild id, not find wave slice id, need to check further", id, short_addr); return kScheduleUnknownSensor; } - if (nth_wave_slice == wave_slice_iter->second) { - // 需要发送波形, 需要判断是否有配置,升级需求 - auto upgrade_iter = upgrade_.find(id); - if (upgrade_iter != upgrade_.end()) { - if (upgrade_iter->second.try_times < 10) { - current_request_ = kScheduleUpgrade; - UpdateUpgradeInfo(id); - zlog_warn(zct, "[%d:%x] in wave slice to upgrade now from version:%s to %s, try time:%d", - id, short_addr, upgrade_iter->second.current_sw_version.c_str(), - upgrade_iter->second.upgrade_sw_version.c_str(), upgrade_iter->second.try_times); - return kScheduleUpgrade; - } + wave_feature_set_inst::instance().GetWaveCfg(short_addr, g_x, g_y, g_z); + if (nth_wave_slice == wave_slice_iter->second.first) { // Z轴 + if (g_z) { + zlog_debug(zbt, "[%d:%x] it is wave z time", id, short_addr); + current_request_ = kScheduleWaveForm; + z = true; + return kScheduleWaveForm; + } else { + next_duration = GetNextDuration(short_addr); + zlog_debug(zbt, "[%d:%x] no need for wave", id, short_addr); + return kScheduleWrongTime; } - - // wave_feature_set_inst::instance().GetWaveCfg(short_addr, g_x, g_y, g_z); - // if (g_x || g_y || g_z) { - // zlog_warn(zct, "[%d:%x] it is wave time", id, short_addr); - // current_request_ = kScheduleWaveForm; - // return kScheduleWaveForm; - // } else { - next_duration = GetNextDuration(short_addr, next_task_id); - zlog_warn(zct, "[%d:%x] no need for wave", id, short_addr); - return kScheduleResultNone; - // } - } else { - if (slice_sensor_id_[nth_wave_slice-1] == 0) { - zlog_warn(zct, "[%d:%x] in idle time", id, short_addr); - // idle time - auto upgrade_iter = upgrade_.find(id); - if (upgrade_iter != upgrade_.end()) { - if (upgrade_iter->second.try_times < 10) { - current_request_ = kScheduleUpgrade; - UpdateUpgradeInfo(id); - zlog_warn(zct, "[%d:%x] in idle to upgrade now from version:%s to %s, try time:%d", - id, short_addr, upgrade_iter->second.current_sw_version.c_str(), - upgrade_iter->second.upgrade_sw_version.c_str(), upgrade_iter->second.try_times); - return kScheduleUpgrade; - } - } - - // if (RetransferWave(short_addr)) { - // zlog_warn(zct, "[%d:%x] it is retransfer wave time", id, short_addr); - // current_request_ = kScheduleWaveForm; - // return kScheduleWaveForm; - // } else if (MissedWave(short_addr)) { - // zlog_warn(zct, "[%d:%x] it is patch wave time", id, short_addr); - // current_request_ = kScheduleWaveForm; - // patch_set_.erase(short_addr); - // return kScheduleWaveForm; - // } + } else if (nth_wave_slice == wave_slice_iter->second.second) { // XY轴 + if (g_x || g_y) { + zlog_debug(zbt, "[%d:%x] it is wave xy time", id, short_addr); + current_request_ = kScheduleWaveForm; + z = false; + return kScheduleWaveForm; + } else { + next_duration = GetNextDuration(short_addr); + zlog_debug(zbt, "[%d:%x] no need for wave", id, short_addr); + return kScheduleWrongTime; + } + } + else { + if (slice_sensor_id_[nth_wave_slice] == 0) { // idle time + zlog_debug(zbt, "[%d:%x] in idle time", id, short_addr); + if (ZRetransferWave(short_addr)) { + zlog_debug(zbt, "[%d:%x] z retransfer wave time", id, short_addr); + current_request_ = kScheduleWaveForm; + z = true; + return kScheduleWaveForm; + } else if (XYRetransferWave(short_addr)) { + zlog_debug(zbt, "[%d:%x] xy retransfer wave time", id, short_addr); + current_request_ = kScheduleWaveForm; + z = false; + return kScheduleWaveForm; + } else if (ZMissedWave(short_addr)) { + zlog_debug(zbt, "[%d:%x] z patch wave time", id, short_addr); + current_request_ = kScheduleWaveForm; + z = true; + z_patch_set_.erase(short_addr); + return kScheduleWaveForm; + } else if (XYMissedWave(short_addr)) { + zlog_debug(zbt, "[%d:%x] xy patch wave time", id, short_addr); + current_request_ = kScheduleWaveForm; + xy_patch_set_.erase(short_addr); + z = false; + return kScheduleWaveForm; + } } // wrong time to come - long available_ts = current_wave_start_ts_ + (nth_eigen_value_slice_ + 1) * eigen_value_send_interval_ + (id - 1) * eigen_value_send_duration_; - next_duration = available_ts - current_ts_; - if (next_duration < 0 || next_duration > eigen_value_send_interval_) { - zlog_warn(zct, "[Nxt] exception duration: %d", next_duration); - next_duration = eigen_value_send_interval_; - }else if(next_duration < 15){ - zlog_warn(zct, "[Nxt] exception duration: %d", next_duration); - next_duration = 15; + int eigen_send_ts = (id - 1) * 2; + if (eigen_send_ts > 57) { + eigen_send_ts = eigen_send_ts % 57; } - next_task_id = kScheduleEigenValue; - zlog_warn(zct, "[%d:%x] wrong time in wave slice, next feature send utc time:[%s], duration:%d", id, short_addr, GetUTCTime(available_ts).c_str(), next_duration); - return kScheduleResultNone; + long available_ts = current_wave_start_ts_ + (nth_eigen_value_slice_ + 1) * eigen_value_send_interval_ + eigen_send_ts; + next_duration = available_ts - current_ts_; + if (next_duration < 10 || next_duration > eigen_value_send_interval_) { + zlog_debug(zbt, "[%d:%x] invalid next duration:%d, adjust to 120", id, short_addr, next_duration); + next_duration = 120; + } + zlog_debug(zbt, "[%d:%x] wrong time in wave slice, next feature send utc time:[%s], duration:%d", id, short_addr, GetUTCTime(available_ts).c_str(), next_duration); + return kScheduleWrongTime; } } } -long SensorScheduler::CalcNextTimestamp(int id, uint16_t short_addr, int& next_task_id) { - // current_ts_ = GetLocalTs(); - // nth_wave_start_slice_ = (current_ts_ - start_timestamp_) / wave_form_send_interval_; - // current_wave_start_ts_ = nth_wave_start_slice_ * wave_form_send_interval_ + start_timestamp_; - // seconds_in_current_wave_slice_ = current_ts_ - current_wave_start_ts_; - // nth_eigen_value_slice_ = seconds_in_current_wave_slice_ / eigen_value_send_interval_; - // seconds_in_current_eigen_slice_ = seconds_in_current_wave_slice_ % eigen_value_send_interval_; - // ts_in_eigen_slice_ = false; - - // if (seconds_in_current_eigen_slice_ < eigen_value_slice_total_seconds_ - 3) { - // ts_in_eigen_slice_ = true; - // } - nth_wave_start_slice_ = (current_ts_ - start_timestamp_) / wave_form_send_interval_; - current_wave_start_ts_ = nth_wave_start_slice_ * wave_form_send_interval_ + start_timestamp_; - seconds_in_current_wave_slice_ = current_ts_ - current_wave_start_ts_; - nth_eigen_value_slice_ = seconds_in_current_wave_slice_ / eigen_value_send_interval_; - seconds_in_current_eigen_slice_ = seconds_in_current_wave_slice_ % eigen_value_send_interval_; - ts_in_eigen_slice_ = false; - - if (seconds_in_current_eigen_slice_ < eigen_value_slice_total_seconds_ - 3) { - ts_in_eigen_slice_ = true; - } - +long SensorScheduler::CalcNextTimestamp(int id, uint16_t short_addr) { if (ts_in_eigen_slice_) { + if (current_schedule_status_ == kScheduleStatusDebug) { + if (debug_list_.count(short_addr) == 0) { + return GetDebugUpgradeNextTS(short_addr); + } else { + // 计算发送波形是否在后面的波形时间窗口中 + int nth_wave_slice = nth_eigen_value_slice_ * wave_slice_num_per_eigen_interval_ + 1; + for (int i = nth_wave_slice; i <= nth_wave_slice + wave_slice_num_per_eigen_interval_; ++i) { + if (debug_slice_sensor_id_[i] == short_addr) { + long nxt_ts = current_wave_start_ts_ + nth_eigen_value_slice_ * eigen_value_send_interval_ + 60 + (i-nth_wave_slice)*60; + return nxt_ts; + } + } + + return GetDebugUpgradeNextTS(short_addr); + } + } else if (current_schedule_status_ == kScheduleStatusUpgrade) { + if (upgrade_list_.count(short_addr) == 0) { + return GetDebugUpgradeNextTS(short_addr); + } else { + // 计算升级是否在后面的波形时间窗口中 + int nth_wave_slice = nth_eigen_value_slice_ * wave_slice_num_per_eigen_interval_ + 1; + for (int i = nth_wave_slice; i <= nth_wave_slice + wave_slice_num_per_eigen_interval_; ++i) { + if (upgrade_slice_sensor_id_[i] == short_addr) { + long nxt_ts = current_wave_start_ts_ + nth_eigen_value_slice_ * eigen_value_send_interval_ + 60 + (i-nth_wave_slice)*60; + return nxt_ts; + } + } + return GetDebugUpgradeNextTS(short_addr); + } + } int forward_wave_slice_num = nth_eigen_value_slice_ * wave_slice_num_per_eigen_interval_; auto wave_slice_iter = sensor_id_nth_slice_.find(id); if (wave_slice_iter == sensor_id_nth_slice_.end()) { - zlog_error(zct, "[Nxt] invaild id:%d, not find wave slice id", id); + zlog_warn(zbt, "[Nxt] invaild id:%d, not find wave slice id", id); long available_ts = current_wave_start_ts_ + eigen_value_send_interval_ + nth_eigen_slice_ * eigen_value_send_duration_; - zlog_error(zct, "[Nxt] [%d] next feature send utc time:[%s]", id, GetUTCTime(available_ts).c_str()); - next_task_id = kScheduleEigenValue; + zlog_warn(zbt, "[Nxt] [%d] next feature send utc time:[%s]", id, GetUTCTime(available_ts).c_str()); return available_ts; } - int wave_slice = wave_slice_iter->second; // 从1开始 + int first_wave_slice = wave_slice_iter->second.first; + int second_wave_slice = wave_slice_iter->second.second; long send_wave_ts = 0; + long available_ts = 0; wave_feature_set_inst::instance().GetWaveCfg(short_addr, g_x, g_y, g_z); if (g_x || g_y || g_z) { - if (wave_slice > forward_wave_slice_num && - wave_slice <= forward_wave_slice_num + wave_slice_num_per_eigen_interval_) { + if (g_z && first_wave_slice > forward_wave_slice_num && + first_wave_slice <= forward_wave_slice_num + wave_slice_num_per_eigen_interval_) { // 发送波的时间窗也在本次特征值发送间隔中 - for (int i = forward_wave_slice_num; i <= forward_wave_slice_num + wave_slice_num_per_eigen_interval_; ++i) { - if (wave_slice - 1 == i) { - send_wave_ts = current_wave_start_ts_ + nth_eigen_value_slice_ * eigen_value_send_interval_ + eigen_value_slice_total_seconds_ + (i - forward_wave_slice_num) * seconds_per_wave_slice_; - zlog_warn(zct, "[Nxt] [%d:%x] send wave time:[%s]", id, short_addr, GetUTCTime(send_wave_ts).c_str()); + for (int i = forward_wave_slice_num+1; i <= forward_wave_slice_num + wave_slice_num_per_eigen_interval_; ++i) { + if (first_wave_slice == i) { + send_wave_ts = current_wave_start_ts_ + nth_eigen_value_slice_ * eigen_value_send_interval_ + 60 + (i - forward_wave_slice_num - 1) * 60; + zlog_debug(zbt, "[Nxt] [%d:%x] send wave time:[%s]", id, short_addr, GetUTCTime(send_wave_ts).c_str()); break; } } - } - - if (!MissedWave(short_addr) && send_wave_ts == 0 && success_set_.count(short_addr) == 0) { + } else if ((g_x || g_y) && second_wave_slice > forward_wave_slice_num && + second_wave_slice <= forward_wave_slice_num + wave_slice_num_per_eigen_interval_) { + // 发送波的时间窗也在本次特征值发送间隔中 + for (int i = forward_wave_slice_num+1; i <= forward_wave_slice_num + wave_slice_num_per_eigen_interval_; ++i) { + if (second_wave_slice == i) { + send_wave_ts = current_wave_start_ts_ + nth_eigen_value_slice_ * eigen_value_send_interval_ + 60 + (i - forward_wave_slice_num - 1) * 60; + zlog_debug(zbt, "[Nxt] [%d:%x] send wave time:[%s]", id, short_addr, GetUTCTime(send_wave_ts).c_str()); + break; + } + } + } + + if (g_z && !ZMissedWave(short_addr) && send_wave_ts == 0 && z_success_set_.count(short_addr) == 0) { // add for patch wave int nth_wave_slice = nth_eigen_value_slice_ * wave_slice_num_per_eigen_interval_ + nth_wave_slice_ + 1; auto wave_slice_iter = sensor_id_nth_slice_.find(id); if (wave_slice_iter != sensor_id_nth_slice_.end()) { - if (nth_wave_slice > wave_slice_iter->second) { - if (success_set_.count(short_addr) == 0 && !RetransferWave(short_addr)) { - zlog_warn(zct, "[Nxt] [%d:%x] add it to patch set", id, short_addr); - patch_set_.insert(short_addr); + if (nth_wave_slice > wave_slice_iter->second.first) { + if (z_success_set_.count(short_addr) == 0 && !ZRetransferWave(short_addr)) { + zlog_debug(zbt, "[Nxt] [%d:%x] add z to patch set", id, short_addr); + z_patch_set_.insert(short_addr); + } + } + } + } else if ((g_x || g_y) && !XYMissedWave(short_addr) && send_wave_ts == 0 && xy_success_set_.count(short_addr) == 0) { + // add for patch wave + int nth_wave_slice = nth_eigen_value_slice_ * wave_slice_num_per_eigen_interval_ + nth_wave_slice_ + 1; + auto wave_slice_iter = sensor_id_nth_slice_.find(id); + if (wave_slice_iter != sensor_id_nth_slice_.end()) { + if (nth_wave_slice > wave_slice_iter->second.second) { + if (xy_success_set_.count(short_addr) == 0 && !XYRetransferWave(short_addr)) { + zlog_debug(zbt, "[Nxt] [%d:%x] add xy to patch set", id, short_addr); + xy_patch_set_.insert(short_addr); } } } } - } - - long available_ts = 0; - auto upgrade_iter = upgrade_.find(id); - if (upgrade_iter != upgrade_.end()) { - if (upgrade_iter->second.try_times < 10) { - for (int i = 0; i < wave_slice_num_per_eigen_interval_; ++i) { + + if (ZRetransferWave(short_addr) || XYRetransferWave(short_addr) || ZMissedWave(short_addr) || XYMissedWave(short_addr)) { + for (int i = 1; i <= wave_slice_num_per_eigen_interval_; ++i) { if (slice_sensor_id_[i+forward_wave_slice_num] == 0) { // 判断此空闲位置是否被占用 - long current_wave_slice_ts = current_wave_start_ts_ + nth_eigen_value_slice_ * eigen_value_send_interval_ + eigen_value_slice_total_seconds_ + i * seconds_per_wave_slice_; + long current_wave_slice_ts = current_wave_start_ts_ + nth_eigen_value_slice_ * eigen_value_send_interval_ + 60 + (i-1) * seconds_per_wave_slice_; if (free_slice_ocuppied_.count(current_wave_slice_ts) == 0) { available_ts = current_wave_slice_ts; free_slice_ocuppied_.insert(available_ts); - zlog_warn(zct, "[Nxt][%d:%x] %d nth free wave slice will be used to upgrade, utc time:[%s]", id, short_addr, i+forward_wave_slice_num, GetUTCTime(available_ts).c_str()); - // break; - next_task_id = kScheduleUpgrade; - return available_ts; + zlog_debug(zbt, "[Nxt][%d:%x] %d nth free wave slice will be used to retransfer or patch wave, utc time:[%s]", id, short_addr, i+forward_wave_slice_num, GetUTCTime(available_ts).c_str()); + break; } } } - } - } else { - if (g_x || g_y || g_z) { - if (RetransferWave(short_addr)) { - for (int i = 0; i < wave_slice_num_per_eigen_interval_; ++i) { - if (slice_sensor_id_[i+forward_wave_slice_num] == 0) { - // 判断此空闲位置是否被占用 - long current_wave_slice_ts = current_wave_start_ts_ + nth_eigen_value_slice_ * eigen_value_send_interval_ + eigen_value_slice_total_seconds_ + i * seconds_per_wave_slice_; - if (free_slice_ocuppied_.count(current_wave_slice_ts) == 0) { - available_ts = current_wave_slice_ts; - free_slice_ocuppied_.insert(available_ts); - zlog_warn(zct, "[Nxt][%d:%x] %d nth free wave slice will be used to retransfer wave, utc time:[%s]", id, short_addr, i+forward_wave_slice_num, GetUTCTime(available_ts).c_str()); - // break; - next_task_id = kScheduleWaveForm; - return available_ts; - } - } - } - } else if (MissedWave(short_addr)) { - for (int i = 0; i < wave_slice_num_per_eigen_interval_; ++i) { - if (slice_sensor_id_[i+forward_wave_slice_num] == 0) { - // 判断此空闲位置是否被占用 - long current_wave_slice_ts = current_wave_start_ts_ + nth_eigen_value_slice_ * eigen_value_send_interval_ + eigen_value_slice_total_seconds_ + i * seconds_per_wave_slice_; - if (free_slice_ocuppied_.count(current_wave_slice_ts) == 0) { - available_ts = current_wave_slice_ts; - free_slice_ocuppied_.insert(available_ts); - zlog_warn(zct, "[Nxt][%d:%x] %d nth free wave slice will be used to patch wave, utc time:[%s]", id, short_addr, i+forward_wave_slice_num, GetUTCTime(available_ts).c_str()); - // break; - next_task_id = kScheduleWaveForm; - return available_ts; - } - } - } - } - } - } - if (send_wave_ts > 0) { - // long min_ts = std::min(send_wave_ts, available_ts); - zlog_warn(zct, "[Nxt] [%d:%x] next wave send utc time1:%s", id, short_addr, GetUTCTime(send_wave_ts).c_str()); - next_task_id = kScheduleWaveForm; - return send_wave_ts; - // return min_ts; + } + } + + if (send_wave_ts > 0 && available_ts > 0) { + long min_ts = std::min(send_wave_ts, available_ts); + zlog_debug(zbt, "[Nxt] [%d:%x] next feature send utc time1:%s", id, short_addr, GetUTCTime(min_ts).c_str()); + return min_ts; } - // if (send_wave_ts + available_ts > 0) { - // long max_ts = std::max(send_wave_ts, available_ts); - // zlog_warn(zct, "[Nxt] [%d:%x] next feature send utc time2:%s", id, short_addr, GetUTCTime(max_ts).c_str()); - // return max_ts; - // } + if (send_wave_ts + available_ts > 0) { + long max_ts = std::max(send_wave_ts, available_ts); + zlog_debug(zbt, "[Nxt] [%d:%x] next feature send utc time2:%s", id, short_addr, GetUTCTime(max_ts).c_str()); + return max_ts; + } + } else { + if (current_schedule_status_ == kScheduleStatusDebug) { + if (debug_list_.count(short_addr) == 0) { + return GetDebugUpgradeNextTS(short_addr); + } else { + // 计算发送波形是否在后面的波形时间窗口中 + int nth_wave_slice = nth_eigen_value_slice_ * wave_slice_num_per_eigen_interval_ + nth_wave_slice_ + 2; + for (int i = nth_wave_slice; i <= nth_wave_slice + wave_slice_num_per_eigen_interval_; ++i) { + if (debug_slice_sensor_id_[i] == short_addr) { + long nxt_ts = current_wave_start_ts_ + nth_eigen_value_slice_ * eigen_value_send_interval_ + 60 + (i-nth_eigen_value_slice_ * wave_slice_num_per_eigen_interval_-1)*60; + return nxt_ts; + } + } + + return GetDebugUpgradeNextTS(short_addr); + } + } else if (current_schedule_status_ == kScheduleStatusUpgrade) { + if (upgrade_list_.count(short_addr) == 0) { + return GetDebugUpgradeNextDuration(short_addr); + } else { + // 计算升级是否在后面的波形时间窗口中 + } + } } // 如果是在当前波形时间窗中,不管是空闲时间窗,还是发送波形的时间窗,下一个时间窗是特征值 - long available_ts = current_wave_start_ts_ + (nth_eigen_value_slice_ + 1)* eigen_value_send_interval_ + (id - 1) * eigen_value_send_duration_; - zlog_warn(zct, "[Nxt] [%d:%x] next feature send utc time3:[%s]", id, short_addr, GetUTCTime(available_ts).c_str()); - next_task_id = kScheduleEigenValue; + int eigen_send_ts = (id - 1) * 2; + if (eigen_send_ts > 57) { + eigen_send_ts = eigen_send_ts % 57; + } + long available_ts = current_wave_start_ts_ + (nth_eigen_value_slice_ + 1)* eigen_value_send_interval_ + eigen_send_ts; + zlog_debug(zbt, "[Nxt] [%d:%x] next feature send utc time3:[%s]", id, short_addr, GetUTCTime(available_ts).c_str()); return available_ts; } -int SensorScheduler::GetNextDuration(int short_addr, int &next_task_id) { +int SensorScheduler::GetNextDuration(uint16_t short_addr) { int id = 0; auto iter = short_addr_map_.find(short_addr); if (iter == short_addr_map_.end()) { - zlog_error(zct, "cannot find id for short_addr %x", short_addr); + zlog_error(zbt, "cannot find id for short_addr %x", short_addr); return 0; } else { id = iter->second; } - current_ts_ = GetLocalTs(); - // nth_wave_start_slice_ = (current_ts_ - start_timestamp_) / wave_form_send_interval_; - // current_wave_start_ts_ = nth_wave_start_slice_ * wave_form_send_interval_ + start_timestamp_; - // seconds_in_current_wave_slice_ = current_ts_ - current_wave_start_ts_; - // nth_eigen_value_slice_ = seconds_in_current_wave_slice_ / eigen_value_send_interval_; - long next_ts = CalcNextTimestamp(id, short_addr, next_task_id); - int duration = next_ts - current_ts_; - if (duration < 0 || duration > eigen_value_send_interval_) { - zlog_warn(zct, "[Nxt] exception duration: %d", duration); - duration = eigen_value_send_interval_; - }else if(duration < 15){ - zlog_warn(zct, "[Nxt] exception duration: %d", duration); - duration = 15; + long current_ts = GetLocalTs(); + long next_ts = CalcNextTimestamp(id, short_addr); + int duration = next_ts - current_ts; + if (duration < 10) { + zlog_debug(zbt, "[%d:%x] [Nxt] exception duration:%d, adjust to 25", id, short_addr); + duration = 25; + return duration; + } else if (duration > eigen_value_send_interval_) { + zlog_debug(zbt, "[%d:%x] [Nxt] exception duration:%d, adjust to 120", id, short_addr); + duration = 120; + return duration; } - zlog_warn(zct, "[Nxt] [%d:%x] next duration: %d, taskid: %d", id, short_addr, duration, next_task_id); + zlog_debug(zbt, "[Nxt] [%d:%x] next duration is %d", id, short_addr, duration); return duration; } -// int SensorScheduler::GetNextDuration(int short_addr) { -// int id = 0; -// auto iter = short_addr_map_.find(short_addr); -// if (iter == short_addr_map_.end()) { -// zlog_error(zct, "cannot find id for short_addr %x", short_addr); -// return 0; -// } else { -// id = iter->second; -// } -// long current_ts = GetLocalTs(); -// long next_ts = CalcNextTimestamp(id, short_addr); -// int duration = next_ts - current_ts; -// if (duration < 0 || duration > eigen_value_send_interval_) { -// zlog_warn(zct, "[Nxt] exception duration: %d", duration); -// duration = eigen_value_send_interval_; -// } -// zlog_warn(zct, "[Nxt] [%d:%x] next duration is %d", id, short_addr, duration); -// return duration; -// } +// 仅返回下一次特征值的时间,用于Debug/Upgrade模式 +int SensorScheduler::GetDebugUpgradeNextDuration(uint16_t short_addr) { + int id = 0; + auto iter = short_addr_map_.find(short_addr); + if (iter == short_addr_map_.end()) { + zlog_error(zbt, "cannot find id for short_addr %x", short_addr); + return 0; + } else { + id = iter->second; + } + long current_ts = GetLocalTs(); + int eigen_send_ts = (id - 1) * 2; + if (eigen_send_ts > 57) { + eigen_send_ts = eigen_send_ts % 57; + } + long available_ts = current_wave_start_ts_ + (nth_eigen_value_slice_ + 1)* eigen_value_send_interval_ + eigen_send_ts; + zlog_debug(zbt, "[Nxt] [%d:%x] next feature send utc time4:[%s]", id, short_addr, GetUTCTime(available_ts).c_str()); + + int duration = available_ts - current_ts; + if (duration < 10) { + zlog_debug(zbt, "[%d:%x] [Nxt] exception duration:%d, adjust to 25", id, short_addr); + duration = 25; + return duration; + } else if (duration > eigen_value_send_interval_) { + zlog_debug(zbt, "[%d:%x] [Nxt] exception duration:%d, adjust to 120", id, short_addr); + duration = 120; + return duration; + } + zlog_debug(zbt, "[Nxt] [%d:%x] next duration is %d", id, short_addr, duration); + return duration; +} + +long SensorScheduler::GetDebugUpgradeNextTS(uint16_t short_addr) { + int id = 0; + auto iter = short_addr_map_.find(short_addr); + if (iter == short_addr_map_.end()) { + zlog_warn(zbt, "cannot find id for short_addr %x", short_addr); + return 0; + } else { + id = iter->second; + } + // long current_ts = GetLocalTs(); + int eigen_send_ts = (id - 1) * 2; + if (eigen_send_ts > 57) { + eigen_send_ts = eigen_send_ts % 57; + } + long available_ts = current_wave_start_ts_ + (nth_eigen_value_slice_ + 1)* eigen_value_send_interval_ + eigen_send_ts; + zlog_debug(zbt, "[Nxt] [%d:%x] next feature send utc time4:[%s]", id, short_addr, GetUTCTime(available_ts).c_str()); + return available_ts; +} SensorScheduler::SensorScheduler() { - support_modification_ = true; + current_schedule_status_ = get_schedule_status(); + slice_sensor_id_ = NULL; + zlog_debug(zbt, "current schedule status:%s", get_status_desc(current_schedule_status_).c_str()); std::ifstream schedule_file(SCHEDULE_CONFIG); bool configed = false; if (schedule_file.good()) { @@ -383,27 +469,33 @@ SensorScheduler::SensorScheduler() { } eigen_value_send_interval_ = root["eigen_value_send_interval"].asInt(); - eigen_value_send_duration_ = root["eigen_value_send_duration"].asInt(); + // eigen_value_send_duration_ = root["eigen_value_send_duration"].asInt(); wave_form_send_interval_ = root["wave_form_send_interval"].asInt(); - wave_form_send_duration_ = root["wave_form_send_duration"].asInt(); + wave_form_send_duration_ = 60; //root["wave_form_send_duration"].asInt(); max_sensor_num_ = root["max_sensor_num"].asInt(); available_slice_ = root["available_slice"].asInt(); free_slice_ = root["free_slice"].asInt(); - support_modification_ = root["support_modification"].asBool(); - - const Json::Value ids = root["id"]; - int sensor_id = 0; - int i = 0; - for (const auto& id : ids) { - sensor_id = id.asInt(); - slice_sensor_id_.push_back(sensor_id); - if (sensor_id != 0) { - sensor_id_nth_slice_[sensor_id] = i+1; - } - ++i; + slice_sensor_id_ = new int[available_slice_+1]; + slice_is_z_wave_ = new uint8_t[available_slice_+1]; + for (int i = 0; i <= available_slice_; ++i) { + slice_sensor_id_[i] = 0; } - eigen_value_slice_total_seconds_ = eigen_value_send_duration_ * max_sensor_num_; + int xy_wave_start_id = 0; + if (max_sensor_num_ <= 4) { + xy_wave_start_id = 5; // 防止z,xy在时间上排得太近 + } else { + xy_wave_start_id = max_sensor_num_ + 1; + } + for (int i = 1; i <= max_sensor_num_; ++i) { + sensor_id_nth_slice_[i] = { i, i + xy_wave_start_id }; + slice_sensor_id_[i] = i; + slice_sensor_id_[i+xy_wave_start_id] = i; + slice_is_z_wave_[i] = true; + slice_is_z_wave_[i + xy_wave_start_id] = false; + } + + eigen_value_slice_total_seconds_ = 60; // eigen_value_send_duration_ * max_sensor_num_; int rest_duration = eigen_value_send_interval_ - eigen_value_slice_total_seconds_; wave_slice_num_per_eigen_interval_ = rest_duration / wave_form_send_duration_; seconds_per_wave_slice_ = rest_duration / wave_slice_num_per_eigen_interval_; @@ -420,87 +512,217 @@ init_config: UpgradeCfg::ReadCfg(upgrade_); // read config update file: CONFIG_UPDATE - UpdateCfg::ReadCfg(update_); -} + UpdateCfg::ReadCfg(update_); -int SensorScheduler::WaveError(uint16_t short_addr) { - auto iter = failure_map_.find(short_addr); - if (iter == failure_map_.end()) { - failure_map_[short_addr] = 3; // 重试次数 - zlog_warn(zct, "[WaveError][%x] will try 3 times", short_addr); - return 1; - } - if (iter->second == 0) { - zlog_warn(zct, "[WaveError][%x] no try times", short_addr); - failure_map_.erase(short_addr); - return 3; + if (current_schedule_status_ == kScheduleStatusDebug) { + std::ifstream debug_schedule_file(DEBUG_SCHEDULE_CONFIG); + if (debug_schedule_file.good()) { + Json::Reader reader; + Json::Value root; + if (!reader.parse(debug_schedule_file, root, false)) { + zlog_error(zbt, "invalid format, fail to parse %s", DEBUG_SCHEDULE_CONFIG); + zlog_warn(zct, "due to invalid debug file, status change to normal"); + SetScheduleStatus(kScheduleStatusNormal); + return; + } + debug_schedule_file.close(); + if (!root.isArray()) { + zlog_error(zbt, "invalid format, not an object: %s", DEBUG_SCHEDULE_CONFIG); + zlog_warn(zct, "due to invalid debug file, status change to normal"); + SetScheduleStatus(kScheduleStatusNormal); + return; + } else { + debug_slice_sensor_id_ = new uint16_t[available_slice_+1]; + for (int i = 0; i <= available_slice_; ++i) { + debug_slice_sensor_id_[i] = 0; + } + int i = 1; + for (const auto& value : root) { + std::cout << value.asInt() << std::endl; // 转换并输出每个整数 + zlog_debug(zbt, "[%d] debug sensor:%x", value.asInt()); + debug_slice_sensor_id_[i] = value.asInt(); + ++i; + } + if (i == 1) { + zlog_warn(zbt, "due to empty debug file, status change to normal"); + if (debug_slice_sensor_id_ != NULL) { + delete []debug_slice_sensor_id_; + debug_slice_sensor_id_ = NULL; + } + SetScheduleStatus(kScheduleStatusNormal); + return; + } + } + + } else { + zlog_warn(zbt, "due to no debug file, status change to normal"); + SetScheduleStatus(kScheduleStatusNormal); + return; + } + } else if (current_schedule_status_ == kScheduleStatusUpgrade) { + std::ifstream upgrade_schedule_file(UPGRADE_SCHEDULE_CONFIG); + if (upgrade_schedule_file.good()) { + Json::Reader reader; + Json::Value root; + if (!reader.parse(upgrade_schedule_file, root, false)) { + zlog_error(zbt, "invalid format, fail to parse %s", UPGRADE_SCHEDULE_CONFIG); + zlog_warn(zbt, "due to invalid upgrade file, status change to normal"); + SetScheduleStatus(kScheduleStatusNormal); + return; + } + upgrade_schedule_file.close(); + if (!root.isArray()) { + zlog_error(zbt, "invalid format, not an object: %s", UPGRADE_SCHEDULE_CONFIG); + zlog_warn(zbt, "due to invalid upgrade file, status change to normal"); + SetScheduleStatus(kScheduleStatusNormal); + return; + } else { + upgrade_slice_sensor_id_ = new uint16_t[available_slice_+1]; + for (int i = 0; i <= available_slice_; ++i) { + upgrade_slice_sensor_id_[i] = 0; + } + int i = 1; + for (const auto& value : root) { + std::cout << value.asInt() << std::endl; // 转换并输出每个整数 + zlog_debug(zbt, "[%d] upgrade sensor:%x", value.asInt()); + upgrade_slice_sensor_id_[i] = value.asInt(); + ++i; + } + if (i == 1) { + zlog_warn(zbt, "due to empty upgrade file, status change to normal"); + if (upgrade_slice_sensor_id_ != NULL) { + delete []upgrade_slice_sensor_id_; + upgrade_slice_sensor_id_ = NULL; + } + SetScheduleStatus(kScheduleStatusNormal); + return; + } + } + + } else { + zlog_warn(zbt, "due to no upgrade file, status change to normal"); + SetScheduleStatus(kScheduleStatusNormal); + return; + } } - iter->second = iter->second - 1; - zlog_warn(zct, "[WaveError][%x] remain try %d times", short_addr, iter->second); - return 3 - iter->second; } -bool SensorScheduler::RetransferWave(uint16_t short_addr) { - auto iter = failure_map_.find(short_addr); - if (iter != failure_map_.end()) { +int SensorScheduler::WaveError(uint16_t short_addr, bool z) { + if (wave_resend_num_ <= 0) { + zlog_debug(zbt, "[WaveError][%x] no config to resend wave", short_addr); + return -1; + } + + if (z) { + auto iter = z_failure_map_.find(short_addr); + if (iter == z_failure_map_.end()) { + z_failure_map_[short_addr] = wave_resend_num_; // 重试次数 + zlog_debug(zbt, "[WaveError][%x] z will try %d times", short_addr, wave_resend_num_); + return 0; + } + if (iter->second == 0) { + zlog_warn(zbt, "[WaveError][%x] z no try times", short_addr); + z_failure_map_.erase(short_addr); + return -1; + } + iter->second = iter->second - 1; + zlog_debug(zbt, "[WaveError][%x] z remain try %d times", short_addr, iter->second); + } else { + auto iter = xy_failure_map_.find(short_addr); + if (iter == xy_failure_map_.end()) { + xy_failure_map_[short_addr] = wave_resend_num_; // 重试次数 + zlog_debug(zbt, "[WaveError][%x] xy will try %d times", short_addr, wave_resend_num_); + return 0; + } + if (iter->second == 0) { + zlog_debug(zct, "[WaveError][%x] xy no try times", short_addr); + xy_failure_map_.erase(short_addr); + return -1; + } + iter->second = iter->second - 1; + zlog_debug(zbt, "[WaveError][%x] xy remain try %d times", short_addr, iter->second); + } + return 0; +} + +bool SensorScheduler::ZRetransferWave(uint16_t short_addr) { + auto iter = z_failure_map_.find(short_addr); + if (iter != z_failure_map_.end()) { return true; - } + } + return false; } -bool SensorScheduler::MissedWave(uint16_t short_addr) { - if (patch_set_.count(short_addr) > 0) { +bool SensorScheduler::XYRetransferWave(uint16_t short_addr) { + auto iter = xy_failure_map_.find(short_addr); + if (iter != xy_failure_map_.end()) { + return true; + } + + return false; +} + +bool SensorScheduler::ZMissedWave(uint16_t short_addr) { + if (z_patch_set_.count(short_addr) > 0) { + return true; + } + + return false; +} + +bool SensorScheduler::XYMissedWave(uint16_t short_addr) { + if (xy_patch_set_.count(short_addr) > 0) { return true; } return false; } -int SensorScheduler::WaveSuccess(uint16_t short_addr) { - zlog_warn(zct, "[WaveSuccess][%x]", short_addr); - success_set_.insert(short_addr); - patch_set_.erase(short_addr); - auto iter = failure_map_.find(short_addr); - if (iter != failure_map_.end()) { - zlog_warn(zct, "[WaveSuccess][%x] try %d times success", short_addr, 4 - iter->second); - failure_map_.erase(short_addr); - return 4 - iter->second; - } - return 0; +void SensorScheduler::WaveSuccess(uint16_t short_addr, bool z) { + if (z) { + z_success_set_.insert(short_addr); + auto iter = z_failure_map_.find(short_addr); + if (iter != z_failure_map_.end()) { + zlog_debug(zbt, "[WaveSuccess][%x] try %d times success", short_addr, 4 - iter->second); + z_failure_map_.erase(short_addr); + return; + } + } else { + xy_success_set_.insert(short_addr); + auto iter = xy_failure_map_.find(short_addr); + if (iter != xy_failure_map_.end()) { + zlog_debug(zbt, "[WaveSuccess][%x] try %d times success", short_addr, 4 - iter->second); + xy_failure_map_.erase(short_addr); + return; + } + } + + return; } void SensorScheduler::ClearFailureSuccessMap() { - zlog_warn(zct, "[ClearFailureSuccessMap]"); - failure_map_.clear(); - success_set_.clear(); - patch_set_.clear(); + z_failure_map_.clear(); + z_success_set_.clear(); + z_patch_set_.clear(); + xy_failure_map_.clear(); + xy_success_set_.clear(); + xy_patch_set_.clear(); } -long SensorScheduler::GetBaseTimestamp(int short_addr) { - int id = 0; - auto iter = short_addr_map_.find(short_addr); - if (iter == short_addr_map_.end()) { - zlog_error(zct, "cannot find id for short_addr %x", short_addr); - return 0; - } else { - id = iter->second; - } - return start_timestamp_ + (id - 1) * eigen_value_send_duration_; -} - -int SensorScheduler::GetAvailableId(int short_addr) { - int max_support_sensor[128] = {0}; +int SensorScheduler::GetAvailableId(uint16_t short_addr) { + int max_support_sensor[96] = {0}; // 5分钟里面有4分钟用于传输波形,2小时有96个 for (auto it = short_addr_map_.begin(); it != short_addr_map_.end(); ++it) { max_support_sensor[it->second] = 1; } int available_id = 0; - for (int i = 1; i < 128; ++i) { + for (int i = 1; i < 96; ++i) { if (max_support_sensor[i] == 0) { available_id = i; break; } } - zlog_warn(zct, "[GetAvailableId][%d] short addr : %x", available_id, short_addr); + zlog_warn(zbt, "[GetAvailableId][%d] short addr : %x", available_id, short_addr); short_addr_map_[short_addr] = available_id; ShortAddrCfg::WriteCfg(short_addr_map_); return available_id; @@ -512,40 +734,42 @@ int SensorScheduler::WriteScheduleCfg(long &ts, std::string &world_time) { root["schedule_start_timestamp"] = std::to_string(ts); root["schedule_start_time"] = world_time; root["eigen_value_send_interval"] = eigen_value_send_interval_; - root["eigen_value_send_duration"] = eigen_value_send_duration_; root["wave_form_send_interval"] = wave_form_send_interval_; - root["wave_form_send_duration"] = wave_form_send_duration_; root["max_sensor_num"] = max_sensor_num_; root["available_slice"] = available_slice_; root["free_slice"] = free_slice_; - root["support_modification"] = true; - - int *slice_allocation = new int[available_slice_]; - int slice_index = 0; // 有时间片的索引 - int no_slice_index = 0; // 没有时间片的索引 - int k = 1; - slice_sensor_id_.clear(); - for (int i = 0; i < available_slice_; ++i) { - if (slice_index < max_sensor_num_ && (i % 2 == 0 || no_slice_index >= free_slice_)) { - slice_allocation[i] = k; - sensor_id_nth_slice_[k] = i + 1; - slice_sensor_id_.push_back(k); - k = k + 1; - slice_index++; - } else if (no_slice_index < free_slice_) { - slice_sensor_id_.push_back(0); - slice_allocation[i] = 0; - no_slice_index++; - } - } - - Json::Value ids; - for (int i = 0; i < available_slice_; ++i) { - ids.append(slice_allocation[i]); + if (slice_sensor_id_ != NULL) { + delete [] slice_sensor_id_; + slice_sensor_id_ = NULL; } - delete []slice_allocation; - root["id"] = ids; + + if (slice_is_z_wave_ != NULL) { + delete [] slice_is_z_wave_; + slice_is_z_wave_ = NULL; + } + + slice_sensor_id_ = new int[available_slice_+1]; + slice_is_z_wave_ = new uint8_t[available_slice_+1]; + for (int i = 0; i <= available_slice_; ++i) { + slice_sensor_id_[i] = 0; + } + + int xy_wave_start_id = 0; + if (max_sensor_num_ <= 4) { + xy_wave_start_id = 5; // 防止z,xy在时间上排得太近 + } else { + xy_wave_start_id = max_sensor_num_ + 1; + } + + for (int i = 1; i <= max_sensor_num_; ++i) { + sensor_id_nth_slice_[i] = { i, i + xy_wave_start_id }; + slice_sensor_id_[i] = i; + slice_sensor_id_[i + xy_wave_start_id] = i; + slice_is_z_wave_[i] = true; + slice_is_z_wave_[i + xy_wave_start_id] = false; + } + Json::StyledStreamWriter streamWriter; std::ofstream out_file(SCHEDULE_CONFIG); streamWriter.write(out_file, root); @@ -553,19 +777,12 @@ int SensorScheduler::WriteScheduleCfg(long &ts, std::string &world_time) { return 0; } -int SensorScheduler::Config(int eigen_value_send_interval, int wave_form_send_interval, int eigen_value_send_duration, - int wave_form_send_duration, int max_sensor_num, std::string &error_msg) { - if (!support_modification_) { - zlog_warn(zct, "not support modification"); - return 1; - } - +int SensorScheduler::Config(int eigen_value_send_interval, int wave_form_send_interval, int max_sensor_num, + int wave_resend_num, std::string &error_msg) { int available_slice = 0; int free_slice = 0; int ret = CalcAvailableSlice(eigen_value_send_interval, - wave_form_send_interval, - eigen_value_send_duration, - wave_form_send_duration, + wave_form_send_interval, max_sensor_num, available_slice, free_slice, @@ -575,15 +792,15 @@ int SensorScheduler::Config(int eigen_value_send_interval, int wave_form_send_in } eigen_value_send_interval_ = eigen_value_send_interval; - eigen_value_send_duration_ = eigen_value_send_duration; + eigen_value_send_duration_ = 2; wave_form_send_interval_ = wave_form_send_interval; - wave_form_send_duration_ = wave_form_send_duration; + wave_form_send_duration_ = 60; max_sensor_num_ = max_sensor_num; + wave_resend_num_ = wave_resend_num; available_slice_ = available_slice; free_slice_ = free_slice; - support_modification_ = true; - eigen_value_slice_total_seconds_ = eigen_value_send_duration_ * max_sensor_num_; + eigen_value_slice_total_seconds_ = 60; int rest_duration = eigen_value_send_interval_ - eigen_value_slice_total_seconds_; wave_slice_num_per_eigen_interval_ = rest_duration / wave_form_send_duration_; seconds_per_wave_slice_ = rest_duration / wave_slice_num_per_eigen_interval_; @@ -601,9 +818,8 @@ int SensorScheduler::Config(int eigen_value_send_interval, int wave_form_send_in } // 特征值发送间隔300秒,波形发送间隔为7200秒 -// 一次特征值发送时长为6秒,波形发送时长为60秒 -int SensorScheduler::CalcAvailableSlice(int eigen_value_send_interval, int wave_form_send_interval, - int eigen_value_send_duration, int wave_form_send_duration, +// 一次特征值发送时长为2秒,波形发送时长为60秒,所有特征值在特征值发送间隔的第1分钟中的第3秒至第57秒全部完成 +int SensorScheduler::CalcAvailableSlice(int eigen_value_send_interval, int wave_form_send_interval, int max_sensor_num, int &available_slice, int &free_slice, std::string &error_msg) { if (max_sensor_num <= 0) { @@ -612,16 +828,16 @@ int SensorScheduler::CalcAvailableSlice(int eigen_value_send_interval, int wave_ return 1; } - if (max_sensor_num * eigen_value_send_duration > eigen_value_send_interval) { + if (2 > eigen_value_send_interval) { error_msg = "invalid max_sensor_num:" + std::to_string(max_sensor_num) + - " * eigen_value_send_duration:" + std::to_string(eigen_value_send_duration) + " > eigen_value_send_interval" + std::to_string(eigen_value_send_interval); + " * eigen_value_send_duration:" + std::to_string(2) + " > eigen_value_send_interval" + std::to_string(eigen_value_send_interval); zlog_error(zbt, "%s", error_msg.c_str()); return 2; } - if (max_sensor_num * wave_form_send_duration > wave_form_send_interval) { - error_msg = "invalid wave_form_send_duration:" + std::to_string(wave_form_send_duration) + - " * max_sensor_num:" + std::to_string(max_sensor_num) + " > wave_form_send_interval:" + std::to_string(wave_form_send_interval); + if (max_sensor_num * 60 * 2 > wave_form_send_interval) { // xy, z分开发送 + error_msg = "invalid wave_form_send_duration:" + std::to_string(60) + + "* 2 * max_sensor_num:" + std::to_string(max_sensor_num) + " > wave_form_send_interval:" + std::to_string(wave_form_send_interval); zlog_error(zbt, "%s", error_msg.c_str()); return 3; } @@ -633,11 +849,11 @@ int SensorScheduler::CalcAvailableSlice(int eigen_value_send_interval, int wave_ return 4; } - int total_eigen_value_send_duration = eigen_value_send_duration * max_sensor_num; + int total_eigen_value_send_duration = 60; int rest_duration = eigen_value_send_interval - total_eigen_value_send_duration; - int slice_per_eigen_value_interval = rest_duration / wave_form_send_duration; + int slice_per_eigen_value_interval = rest_duration / 60; available_slice = wave_form_send_interval / eigen_value_send_interval * slice_per_eigen_value_interval; - free_slice = available_slice - max_sensor_num; + free_slice = available_slice - max_sensor_num * 2; if (free_slice < 0) { error_msg = "invalid config, available slice:" + std::to_string(available_slice) + ", required slice:" + std::to_string(max_sensor_num); zlog_error(zbt, "%s", error_msg.c_str()); @@ -646,22 +862,20 @@ int SensorScheduler::CalcAvailableSlice(int eigen_value_send_interval, int wave_ return 0; } -int SensorScheduler::GetScheduleConfig(int &eigen_value_send_interval, int &wave_form_send_interval, - int &eigen_value_send_duration, int &wave_form_send_duration, +int SensorScheduler::GetScheduleConfig(int &eigen_value_send_interval, int &wave_form_send_interval, int &wave_resend_num, int &max_sensor_num) { eigen_value_send_interval = eigen_value_send_interval_; - wave_form_send_interval = wave_form_send_interval_; - eigen_value_send_duration = eigen_value_send_duration_; - wave_form_send_duration = wave_form_send_duration_; + wave_form_send_interval = wave_form_send_interval_; + wave_resend_num = wave_resend_num_; max_sensor_num = max_sensor_num_; return 0; } -int SensorScheduler::UpdateSensorConfig(int short_addr) { +int SensorScheduler::UpdateSensorConfig(uint16_t short_addr) { int id = 0; auto iter = short_addr_map_.find(short_addr); if (iter == short_addr_map_.end()) { - zlog_info(zct, "cannot find id for short_addr %d", short_addr); + zlog_warn(zbt, "cannot find id for short_addr %d", short_addr); return 1; } else { id = iter->second; @@ -674,11 +888,11 @@ int SensorScheduler::UpdateSensorConfig(int short_addr) { return 0; } -int SensorScheduler::UpdateConfigResult(int short_addr, int result) { +int SensorScheduler::UpdateConfigResult(uint16_t short_addr, int result) { int id = 0; auto iter = short_addr_map_.find(short_addr); if (iter == short_addr_map_.end()) { - zlog_info(zct, "cannot find id for short_addr %x", short_addr); + zlog_warn(zbt, "cannot find id for short_addr %x", short_addr); return 1; } else { id = iter->second; @@ -692,56 +906,415 @@ int SensorScheduler::UpdateConfigResult(int short_addr, int result) { return 0; } -int SensorScheduler::UpgradeSensor(int short_addr, std::string sensor_type, int hw_version, - std::string current_sw_version, std::string upgrade_sw_version) { - int id = 0; - auto iter = short_addr_map_.find(short_addr); - if (iter == short_addr_map_.end()) { - zlog_error(zct, "cannot find id for short_addr %x", short_addr); - return 1; +int SensorScheduler::UpgradeSensor(std::vector ¶m_list) { + if (param_list.size() == 0) { + zlog_warn(zbt, "upgrade list is empty, do nothing"); + return 1; + } + zlog_debug(zbt, "current status:%d", current_schedule_status_); + if (current_schedule_status_ == kScheduleStatusUpgrade) { + std::unordered_set tmp_set; + for (auto item : param_list) { + tmp_set.insert(item.short_addr); + } + + if (tmp_set == upgrade_list_) { + zlog_warn(zct, "upgrade list and mode are same, do nothing"); + return 0; + } } else { - id = iter->second; - } - UpgradeInfo info; - info.try_times = 0; - info.sensor_type = sensor_type; - info.hw_version = hw_version; - info.current_sw_version = current_sw_version; - info.upgrade_sw_version = upgrade_sw_version; + upgrade_list_.clear(); + } + + for (auto item : param_list) { + upgrade_list_.insert(item.short_addr); + } + + int upgrade_num = 0; + int id = 0; long ts = GetLocalTs(); - info.submit_time = GetUTCTime(ts); - upgrade_[id] = info; - zlog_info(zbt, "[%d] short addr:%x add upgrade info", id, short_addr); - UpgradeCfg::WriteCfg(upgrade_); + for (auto item : param_list) { + auto iter = short_addr_map_.find(item.short_addr); + if (iter == short_addr_map_.end()) { + zlog_error(zbt, "cannot find id for short_addr %x", item.short_addr); + continue; + } else { + id = iter->second; + } + UpgradeInfo info; + info.try_times = 0; + info.sensor_type = item.sensor_type; + info.hw_version = item.hw_version; + info.current_sw_version = item.current_sw_version; + info.upgrade_sw_version = item.upgrade_sw_version; + + info.submit_time = GetUTCTime(ts); + upgrade_[id] = info; + zlog_info(zbt, "[%d] short addr:%x add upgrade info", id, item.short_addr); + ++upgrade_num; + } + + if (upgrade_num > 0) { + UpgradeCfg::WriteCfg(upgrade_); + GenerateUpgradeSchedule(); + current_schedule_status_ = kScheduleStatusUpgrade; + set_schedule_status(current_schedule_status_); + } else { + return 1; + } + return 0; } -int SensorScheduler::UpgradeResult(int short_addr, int result) { +int SensorScheduler::CancelUpgradeSensor(std::vector short_addr_list) { + if (short_addr_list.size() == 0) { + return 0; + } + int cancel_num = 0; + + for (auto short_addr : short_addr_list) { + upgrade_list_.erase(short_addr); + auto iter = short_addr_map_.find(short_addr); + if (iter == short_addr_map_.end()) { + zlog_info(zbt, "cannot find id for short_addr %x", short_addr); + continue; + } else { + int id = iter->second; + upgrade_.erase(id); + ++cancel_num; + } + } + + if (cancel_num > 0) { + if (upgrade_list_.size() == 0) { + current_schedule_status_ = kScheduleStatusNormal; + set_schedule_status(current_schedule_status_); + UpgradeCfg::ClearCfg(); + } else { + GenerateUpgradeSchedule(); + UpgradeCfg::WriteCfg(upgrade_); + } + } + return 0; +} + +int SensorScheduler::UpgradeResult(uint16_t short_addr, int result) { int id = 0; auto iter = short_addr_map_.find(short_addr); if (iter == short_addr_map_.end()) { - zlog_info(zct, "cannot find id for short_addr %x", short_addr); + zlog_info(zbt, "cannot find id for short_addr %x", short_addr); return 1; } else { id = iter->second; } - + if (result == kUpgradeSuccess || result == kProductTypeMismatch || result == kZigbeeHWMismatch || result == kUpgradeDoneBefore) { - upgrade_.erase(id); + upgrade_list_.erase(short_addr); + upgrade_.erase(id); zlog_info(zbt, "[%d] short addr:%x upgrade successfully", id, short_addr); UpgradeCfg::WriteCfg(upgrade_); + GenerateUpgradeSchedule(); } else { auto upgrade_iter = upgrade_.find(id); - if (upgrade_iter->second.try_times >= 10) { - zlog_error(zct, "[%d] short addr:%x upgrade 10 time failure", id, short_addr); + if (upgrade_iter->second.try_times >= wave_resend_num_) { + zlog_warn(zbt, "[%d] short addr:%x upgrade %d time failure", id, short_addr, wave_resend_num_); + upgrade_list_.erase(short_addr); upgrade_.erase(id); UpgradeCfg::WriteCfg(upgrade_); - // TODO: call interface to write into database + GenerateUpgradeSchedule(); + } else { + UpdateUpgradeInfo(id); } } + return 0; +} + +void SensorScheduler::UpdateUpgradeInfo(int id) { + auto upgrade_iter = upgrade_.find(id); + upgrade_iter->second.try_times++; + long ts = GetLocalTs(); + upgrade_iter->second.try_world_time1.push_back(GetUTCTime(ts)); + UpgradeCfg::WriteCfg(upgrade_); +} + +void SensorScheduler::ModifyScheduleTs(int diff_ts) { + zlog_warn(zbt, "[ModifyScheduleTs] adjust ts:%d, from:%ld to:%ld", diff_ts, start_timestamp_, start_timestamp_ + diff_ts); + start_timestamp_ += diff_ts; + start_ts_str_ = GetUTCTime(start_timestamp_); + std::ifstream schedule_file(SCHEDULE_CONFIG); + Json::Reader reader; + Json::Value root; + if (!reader.parse(schedule_file, root, false)) { + zlog_error(zbt, "[ModifyScheduleTs] invalid format, fail to parse %s", SCHEDULE_CONFIG); + schedule_file.close(); + return; + } + schedule_file.close(); + root["schedule_start_timestamp"] = std::to_string(start_timestamp_); + root["schedule_start_time"] = start_ts_str_; + + Json::StyledStreamWriter streamWriter; + std::ofstream out_file(SCHEDULE_CONFIG); + streamWriter.write(out_file, root); + out_file.close(); +} + +void SensorScheduler::ClearScheduleCfg(uint16_t short_addr) { + zlog_warn(zbt, "[ClearScheduleCfg] clear all schedule config, short_addr:%x", short_addr); + if (short_addr == 0) { + zlog_warn(zbt, "[ClearScheduleCfg] clear all"); + update_.clear(); + upgrade_.clear(); + short_addr_map_.clear(); + ShortAddrCfg::ClearCfg(); + UpdateCfg::ClearCfg(); + UpgradeCfg::ClearCfg(); + wave_feature_set_inst::instance().RemoveAllFeatureCfg(); + wave_feature_set_inst::instance().RemoveAllWaveCfg(); + } else { + UpdateConfigResult(short_addr, 0); + UpgradeResult(short_addr, kUpgradeSuccess); + short_addr_map_.erase(short_addr); + ShortAddrCfg::WriteCfg(short_addr_map_); + wave_feature_set_inst::instance().RemoveFeatureCfg(short_addr); + wave_feature_set_inst::instance().RemoveWaveCfg(short_addr); + } +} + +void SensorScheduler::CleanIdleOccupiedSet(long ts) { + if (free_slice_ocuppied_.size() > 5) { + for (auto it = free_slice_ocuppied_.begin(); it != free_slice_ocuppied_.end();) { + if ((*it) < ts) { + it = free_slice_ocuppied_.erase(it); + } else ++it; + } + } +} + +void SensorScheduler::UseDefaultConfig() { + zlog_info(zbt, "use default configuration"); + int eigen_value_send_interval = 300; + int wave_form_send_interval = 7200; + int eigen_value_send_duration = 6; + int wave_form_send_duration = 50; + int max_sensor_num = 32; + int wave_resend_num = 3; + + // int eigen_value_send_interval = 120; + // int wave_form_send_interval = 240; + // int eigen_value_send_duration = 2; // 固定的 + // int wave_form_send_duration = 60; // 固定的 + // int max_sensor_num = 4; + std::string error_msg; + Config(eigen_value_send_interval, + wave_form_send_interval, + max_sensor_num, wave_resend_num, + error_msg); +} + +void SensorScheduler::SetScheduleStatus(ScheduleStatus status) { + if (status != current_schedule_status_) { + zlog_warn(zbt, "schedule status from:%d to %d", current_schedule_status_, status); + current_schedule_status_ = status; + set_schedule_status(current_schedule_status_); + } else { + zlog_warn(zbt, "schedule status not change:%d", status); + } +} + +void SensorScheduler::GenerateDebugSchedule(std::vector short_addr_list) { + int debug_size = short_addr_list.size(); + + debug_list_.clear(); + for (auto item : short_addr_list) { + debug_list_.insert(item); + } + + // 时间还是用正常模式的,因为特征值还是正常的,所以只把原来波形调度的地方换成调试的传感器 + // + // 1 根据传感器列表,找到传感器编号,例如:1,2,3 + // 2 这是第几个5分钟,从这个5分钟开始,放波形,如果这是2小时中的最后一个5分钟,要考虑越界的问题 + // 3 如果是1到2个传感器,隔2分钟(两个波形间隔)测试一次; 如果是大于2个传感器,一直连排就行了 + // 进入调试模式 + + long current_ts = GetLocalTs(); + long nth_wave_start_slice = (current_ts - start_timestamp_) / wave_form_send_interval_; + long current_wave_start_ts = nth_wave_start_slice * wave_form_send_interval_ + start_timestamp_; + long seconds_in_current_wave_slice = current_ts_ - current_wave_start_ts; + long nth_eigen_value_slice = seconds_in_current_wave_slice / eigen_value_send_interval_; + long seconds_in_current_eigen_slice = seconds_in_current_wave_slice % eigen_value_send_interval_; + int previous_wave_slice = wave_slice_num_per_eigen_interval_ * (nth_eigen_value_slice + 1); + if (previous_wave_slice == available_slice_) { + previous_wave_slice = 0; + } + + debug_slice_sensor_id_ = new uint16_t[available_slice_+1]; + for (int i = 0; i <= available_slice_; ++i) { + debug_slice_sensor_id_[i] = 0; + } + + if (debug_size == 1) { // 只有一个传感器的话,两分钟一次波形 + int j = 0; + for (int i = previous_wave_slice+1; i < previous_wave_slice+1+available_slice_; i = i + 3) { + j = i % available_slice_; + if (j == 0) { + j = i; + } + debug_slice_sensor_id_[j] = short_addr_list[0]; + } + } else { + int j = 0; + int k = 0; + for (int i = previous_wave_slice+1; i < previous_wave_slice+1+available_slice_; ++i) { + j = i % available_slice_; + if (j == 0) { + j = i; + } + debug_slice_sensor_id_[j] = short_addr_list[k%debug_size]; + ++k; + } + } + + Json::Value root; + for (int i = 1; i <= available_slice_; ++i) { + root.append(debug_slice_sensor_id_[i]); + } + Json::StyledStreamWriter streamWriter; + std::ofstream out_file(DEBUG_SCHEDULE_CONFIG); + streamWriter.write(out_file, root); + out_file.close(); + + // 从下一个5分钟开始排,因为只要错过了这个5分钟的第1分钟时间,传感器都将无法调度 + // 下一个5分钟是第几个5分钟间隔,波形窗口是第多少个 + // 对于特征值后的波形,如果特征值时间离波形时间间隔小于10秒,那就再加20秒上去,这样保证传感器至少休息20秒 + current_schedule_status_ = kScheduleStatusDebug; + set_schedule_status(current_schedule_status_); +} + +int SensorScheduler::OpenDebugMode(std::vector short_addr_list) { + if (short_addr_list.size() == 0) { + zlog_warn(zbt, "debug list is empty, do nothing"); + return 1; + } + // 给进入调试模式的传感器分配波形时间片 + if (current_schedule_status_ == kScheduleStatusDebug) { + std::unordered_set tmp_set; + for (auto item : short_addr_list) { + tmp_set.insert(item); + } + if (tmp_set == debug_list_) { + zlog_warn(zbt, "debug list and mode are same, do nothing"); + return 0; + } + // 重新分配z轴波形时间片 + GenerateDebugSchedule(short_addr_list); + return 0; + } + GenerateDebugSchedule(short_addr_list); + return 0; +} + +int SensorScheduler::CloseDebugMode() { + if (current_schedule_status_ != kScheduleStatusDebug) { + return 0; + } + + if (debug_slice_sensor_id_ != NULL) { + delete []debug_slice_sensor_id_; + debug_slice_sensor_id_ = NULL; + } + current_schedule_status_ = kScheduleStatusNormal; + set_schedule_status(current_schedule_status_); + return 0; +} + +void SensorScheduler::GenerateUpgradeSchedule() { + if (upgrade_list_.size() == 0) { + return; + } + std::vector short_addr_list; + for (auto item : upgrade_list_) { + short_addr_list.push_back(item); + } + int upgrade_size = short_addr_list.size(); + + // 时间还是用正常模式的,因为特征值还是正常的,所以只把原来波形调度的地方换成升级的传感器 + // + // 1 根据传感器列表,找到传感器编号,例如:1,2,3 + // 2 这是第几个5分钟,从这个5分钟开始,放波形,如果这是2小时中的最后一个5分钟,要考虑越界的问题 + // 3 如果是1到2个传感器,隔2分钟(两个波形间隔)测试一次; 如果是大于2个传感器,一直连排就行了 + + long current_ts = GetLocalTs(); + long nth_wave_start_slice = (current_ts - start_timestamp_) / wave_form_send_interval_; + long current_wave_start_ts = nth_wave_start_slice * wave_form_send_interval_ + start_timestamp_; + long seconds_in_current_wave_slice = current_ts_ - current_wave_start_ts; + long nth_eigen_value_slice = seconds_in_current_wave_slice / eigen_value_send_interval_; + long seconds_in_current_eigen_slice = seconds_in_current_wave_slice % eigen_value_send_interval_; + int previous_wave_slice = wave_slice_num_per_eigen_interval_ * (nth_eigen_value_slice + 1); + if (previous_wave_slice == available_slice_) { + previous_wave_slice = 0; + } + if (upgrade_slice_sensor_id_ != NULL) { + delete [] upgrade_slice_sensor_id_; + upgrade_slice_sensor_id_ = NULL; + } + + upgrade_slice_sensor_id_ = new uint16_t[available_slice_+1]; + for (int i = 0; i <= available_slice_; ++i) { + upgrade_slice_sensor_id_[i] = 0; + } + + if (upgrade_size == 1) { // 只有一个传感器的话,两分钟一次波形 + int j = 0; + int k = 0; + for (int i = previous_wave_slice+1; i < previous_wave_slice+1+available_slice_; i=i+2) { + j = i % available_slice_; + if (j == 0) { + j = i; + } + k = k % upgrade_size; + upgrade_slice_sensor_id_[j] = short_addr_list[0]; + } + } else { + int j = 0; + int k = 0; + for (int i = previous_wave_slice+1; i < previous_wave_slice+1+available_slice_; ++i) { + j = i % available_slice_; + if (j == 0) { + j = i; + } + k = k % upgrade_size; + upgrade_slice_sensor_id_[j] = short_addr_list[k]; + ++k; + } + } + + Json::Value root; + for (int i = 1; i <= available_slice_; ++i) { + root.append(upgrade_slice_sensor_id_[i]); + } + Json::StyledStreamWriter streamWriter; + std::ofstream out_file(UPGRADE_SCHEDULE_CONFIG); + streamWriter.write(out_file, root); + out_file.close(); +} + +int SensorScheduler::CloseUpgradeMode() { + if (current_schedule_status_ != kScheduleStatusUpgrade) { + return 0; + } + if (upgrade_slice_sensor_id_ != NULL) { + delete []upgrade_slice_sensor_id_; + upgrade_slice_sensor_id_ = NULL; + } + upgrade_list_.clear(); + std::string clear_cfg_file_cmd = "rm -f " + std::string(UPGRADE_SCHEDULE_CONFIG); + system(clear_cfg_file_cmd.c_str()); + current_schedule_status_ = kScheduleStatusNormal; + set_schedule_status(current_schedule_status_); return 0; } @@ -779,89 +1352,4 @@ std::string SensorScheduler::GetUTCTime(long ts) { local_time->tm_year + 1900, local_time->tm_mon+1, local_time->tm_mday, local_time->tm_hour, local_time->tm_min, local_time->tm_sec); std::string world_time = str; return world_time; -} - -void SensorScheduler::UpdateUpgradeInfo(int id) { - auto upgrade_iter = upgrade_.find(id); - upgrade_iter->second.try_times++; - long ts = GetLocalTs(); - upgrade_iter->second.try_world_time1.push_back(GetUTCTime(ts)); - UpgradeCfg::WriteCfg(upgrade_); -} - -void SensorScheduler::ModifyScheduleTs(int diff_ts) { - zlog_warn(zbt, "[ModifyScheduleTs] adjust ts:%d, from:%ld to:%ld", diff_ts, start_timestamp_, start_timestamp_ + diff_ts); - start_timestamp_ += diff_ts; - start_ts_str_ = GetUTCTime(start_timestamp_); - std::ifstream schedule_file(SCHEDULE_CONFIG); - Json::Reader reader; - Json::Value root; - if (!reader.parse(schedule_file, root, false)) { - zlog_error(zbt, "[ModifyScheduleTs] invalid format, fail to parse %s", SCHEDULE_CONFIG); - schedule_file.close(); - return; - } - schedule_file.close(); - root["schedule_start_timestamp"] = std::to_string(start_timestamp_); - root["schedule_start_time"] = start_ts_str_; - - Json::StyledStreamWriter streamWriter; - std::ofstream out_file(SCHEDULE_CONFIG); - streamWriter.write(out_file, root); - out_file.close(); -} - -void SensorScheduler::ClearScheduleCfg(int short_addr) { - zlog_warn(zbt, "[ClearScheduleCfg] clear all schedule config, short_addr:%x", short_addr); - if (short_addr == 0) { - zlog_warn(zbt, "[ClearScheduleCfg] clear all"); - update_.clear(); - upgrade_.clear(); - short_addr_map_.clear(); - ShortAddrCfg::ClearCfg(); - UpdateCfg::ClearCfg(); - UpgradeCfg::ClearCfg(); - wave_feature_set_inst::instance().RemoveAllFeatureCfg(); - wave_feature_set_inst::instance().RemoveAllWaveCfg(); - } else { - UpdateConfigResult(short_addr, 0); - UpgradeResult(short_addr, kUpgradeSuccess); - short_addr_map_.erase(short_addr); - ShortAddrCfg::WriteCfg(short_addr_map_); - wave_feature_set_inst::instance().RemoveFeatureCfg(short_addr); - wave_feature_set_inst::instance().RemoveWaveCfg(short_addr); - } - -} - -void SensorScheduler::CleanIdleOccupiedSet(long ts) { - if (free_slice_ocuppied_.size() > 5) { - for (auto it = free_slice_ocuppied_.begin(); it != free_slice_ocuppied_.end();) { - if ((*it) < ts) { - it = free_slice_ocuppied_.erase(it); - } else ++it; - } - } -} - -void SensorScheduler::UseDefaultConfig() { - zlog_info(zbt, "use default configuration"); - int eigen_value_send_interval = 300; - int wave_form_send_interval = 7200; - int eigen_value_send_duration = 6; - int wave_form_send_duration = 50; - int max_sensor_num = 32; - - // int eigen_value_send_interval = 120; - // int wave_form_send_interval = 240; - // int eigen_value_send_duration = 6; - // int wave_form_send_duration = 40; - // int max_sensor_num = 4; - std::string error_msg; - Config(eigen_value_send_interval, - wave_form_send_interval, - eigen_value_send_duration, - wave_form_send_duration, - max_sensor_num, - error_msg); -} +} \ No newline at end of file diff --git a/scheduler/schedule.hpp b/scheduler/schedule.hpp index 4d88843..d973639 100644 --- a/scheduler/schedule.hpp +++ b/scheduler/schedule.hpp @@ -4,12 +4,18 @@ #include #include #include +#include #include #include #include #include "upgrade_cfg.hpp" +#include "status_mgr.hpp" #define SCHEDULE_CONFIG "/opt/configenv/schedule.json" +#define DEBUG_SCHEDULE_CONFIG "/opt/configenv/debug_schedule.json" +#define UPGRADE_SCHEDULE_CONFIG "/opt/configenv/upgrade_schedule.json" + + typedef enum { kScheduleResultNone = 0, @@ -30,57 +36,45 @@ typedef enum { kUpgradeDoneBefore = 5 // 当前就是这个版本,不需要升级了 } FirmFileCheckResult; +typedef struct { + uint16_t short_addr; + std::string sensor_type; + int hw_version; + std::string current_sw_version; + std::string upgrade_sw_version; +} UpgradeParameter; + class SensorScheduler { public: SensorScheduler(); + void SetScheduleStatus(ScheduleStatus status); // kScheduleConfigSensor kScheduleUpgrade 等有结果,调我接口通知结果 // kScheduleEigenValue kScheduleWaveForm // 上面4个结束,调GetNextDuration()获取休眠时间 // 如果是kScheduleWrongTime, 此函数next_duration表明休眠时间 - int StartSchedule(int short_addr, int &next_duration, int &next_task_id); - int GetNextDuration(int short_addr,int &next_task_id); - // int GetNextDuration(int short_addr); + int StartSchedule(uint16_t short_addr, int &next_duration, bool &z); + int GetNextDuration(uint16_t short_addr); - int WaveError(uint16_t short_addr); - int WaveSuccess(uint16_t short_addr); - long GetBaseTimestamp(int id); - long CalcNextTimestamp(int id, uint16_t short_addr, int& next_task_id); + // z用于说明是z轴波形,还是xy轴波形 + int WaveError(uint16_t short_addr, bool z); + void WaveSuccess(uint16_t short_addr, bool z); + // long GetBaseTimestamp(int id); + long CalcNextTimestamp(int id, uint16_t short_addr); // 当有传感器需要更新配置时调用 - int UpdateSensorConfig(int short_addr); + int UpdateSensorConfig(uint16_t short_addr); // 当更新结束后,调用此接口,result为0是成功,其它值为失败 - int UpdateConfigResult(int short_addr, int result); - - /** - * @brief 当有传感器需要升级时调用 - * sensor_type: DN101, DN102 - * hw_version: 3, 4 - * current_sw_version: 1.1 - * upgrade_sw_version: 1.2 - */ - int UpgradeSensor(int short_addr, std::string sensor_type, int hw_version, - std::string current_sw_version, std::string upgrade_sw_version); - /** - * @brief 升级后,无线传感器发回来的返回值 - * - * @param short_addr - * @param result 参考:FirmFileCheckResult - * 返回值表明操作是否成功,0成功,其它失败 - */ - int UpgradeResult(int short_addr, int result); + int UpdateConfigResult(uint16_t short_addr, int result); - int Config(int eigen_value_send_interval, int wave_form_send_interval, - int eigen_value_send_duration, int wave_form_send_duration, - int max_sensor_num, std::string &error_msg); + int Config(int eigen_value_send_interval, int wave_form_send_interval, + int max_sensor_num, int wave_resend_num, std::string &error_msg); - int CalcAvailableSlice(int eigen_value_send_interval, int wave_form_send_interval, - int eigen_value_send_duration, int wave_form_send_duration, + int CalcAvailableSlice(int eigen_value_send_interval, int wave_form_send_interval, int max_sensor_num, int &available_slice, int &free_slice, std::string &error_msg); - int GetScheduleConfig(int &eigen_value_send_interval, int &wave_form_send_interval, - int &eigen_value_send_duration, int &wave_form_send_duration, + int GetScheduleConfig(int &eigen_value_send_interval, int &wave_form_send_interval, int &wave_resend_num, int &max_sensor_num); // ======schedule.json操作开始====== // 无线网关程序重启时 @@ -92,20 +86,47 @@ public: void ModifyScheduleTs(int diff_ts); // 当接入传感器时,设置为不可修改; 当停用所有传感器后,修改为可修改 - void AdjustSupportModification(bool support_modification); + // void AdjustSupportModification(bool support_modification); // ======schedule.json操作结束====== - void ClearScheduleCfg(int short_addr = 0); + void ClearScheduleCfg(uint16_t short_addr = 0); long GetLocalTs(); long GetLocalWorldTime(std::string &world_time); std::string GetUTCTime(long ts); - int GetAvailableId(int short_addr); + int GetAvailableId(uint16_t short_addr); + + // 调试模式相关接口 + // 此接口可多次调用,但是总是以最后一次调用为准 + int OpenDebugMode(std::vector short_addr_list); + int CloseDebugMode(); + + // 升级模式相关接口 + /** + * @brief 当有传感器需要升级时调用, 此接口也会打开升级模式,可重复调用 + * sensor_type: DN101, DN102 + * hw_version: 3, 4 + * current_sw_version: 1.1 + * upgrade_sw_version: 1.2 + */ + int UpgradeSensor(std::vector ¶m_list); + int CancelUpgradeSensor(std::vector short_addr_list); + int CloseUpgradeMode(); + /** + * @brief 升级后,无线传感器发回来的返回值 + * + * @param short_addr + * @param result 参考:FirmFileCheckResult + * 返回值表明操作是否成功,0成功,其它失败 + */ + int UpgradeResult(uint16_t short_addr, int result); private: void UpdateUpgradeInfo(int id); void CleanIdleOccupiedSet(long ts); void UseDefaultConfig(); + int GetDebugUpgradeNextDuration(uint16_t short_addr); + long GetDebugUpgradeNextTS(uint16_t short_addr); // user config int eigen_value_send_interval_; @@ -113,6 +134,7 @@ private: int wave_form_send_interval_; int wave_form_send_duration_; int max_sensor_num_; + int wave_resend_num_; // calc result long start_timestamp_; @@ -122,19 +144,23 @@ private: int wave_slice_num_per_eigen_interval_; // 每个特征值窗口中有几个波形发送窗口 int seconds_per_wave_slice_; // 波形窗口时长 int eigen_value_slice_total_seconds_; // 特征值窗口总时长 - std::vector slice_sensor_id_; // 每个时间窗是哪个传感器使用的 - - bool support_modification_; - std::map sensor_id_nth_slice_; // 传感器编号与第几个波形发送窗口对应关系 + int *slice_sensor_id_; // 每个时间窗是哪个传感器使用的 + uint8_t *slice_is_z_wave_; // 表明此窗口是z轴波形还是xy的 + std::unordered_map> sensor_id_nth_slice_; // 传感器编号与z,xy波形发送窗口对应关系 std::map short_addr_map_; // base_relation.json // 存储当前2小时内失败与成功的传感器 - std::map failure_map_; - std::unordered_set success_set_; - std::unordered_set patch_set_; // 漏传的补传 + std::map z_failure_map_; + std::map xy_failure_map_; + std::unordered_set z_success_set_; + std::unordered_set xy_success_set_; + std::unordered_set z_patch_set_; // 漏传的补传 + std::unordered_set xy_patch_set_; // 漏传的补传 void ClearFailureSuccessMap(); - bool RetransferWave(uint16_t short_addr); - bool MissedWave(uint16_t short_addr); + bool ZRetransferWave(uint16_t short_addr); + bool ZMissedWave(uint16_t short_addr); + bool XYRetransferWave(uint16_t short_addr); + bool XYMissedWave(uint16_t short_addr); // 空闲时间戳被占用 std::unordered_set free_slice_ocuppied_; @@ -157,8 +183,23 @@ private: int nth_wave_slice_; // 如果ts_in_eigen_slice_是假的话,此值表明是第几个波形窗口 int current_request_; + ScheduleStatus current_schedule_status_; + + + // debug mode + std::map debug_sensor_id_nth_slice_; // 调度模式下传感器编号与z波形发送窗口对应关系 + uint16_t *debug_slice_sensor_id_; // 每个时间窗是哪个传感器使用的 + // std::vector debug_short_addr_list_; + std::unordered_set debug_list_; + void GenerateDebugSchedule(std::vector short_addr_list); + + // upgrade mode + std::map upgrade_sensor_id_nth_slice_; // 升级模式下传感器编号与升级发送窗口对应关系 + std::unordered_set upgrade_list_; + uint16_t *upgrade_slice_sensor_id_; // 每个时间窗是哪个传感器使用的 + void GenerateUpgradeSchedule(); }; typedef boost::container::dtl::singleton_default scheduler; -#endif // SCHEDULE_HPP_ +#endif // SCHEDULE_HPP_ \ No newline at end of file diff --git a/scheduler/status_mgr.cpp b/scheduler/status_mgr.cpp new file mode 100644 index 0000000..944c72d --- /dev/null +++ b/scheduler/status_mgr.cpp @@ -0,0 +1,124 @@ +#include "status_mgr.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +extern zlog_category_t *zct; + +ScheduleStatus get_schedule_status() { + // std::ifstream status_file("/opt/configenv/status.json"); + // Json::Value json_data; + // Json::CharReaderBuilder reader; + // std::string errs; + + // if (status_file.is_open()) { + // if (Json::parseFromStream(reader, status_file, &json_data, &errs)) { + // std::string status = json_data["status"].asString(); + + // if (status == "debug") { + // status_file.close(); + // return kScheduleStatusDebug; + // } else if (status == "normal") { + // status_file.close(); + // return kScheduleStatusNormal; + // } else if (status == "upgrade") { + // status_file.close(); + // return kScheduleStatusUpgrade; + // } + // } else { + // std::cerr << "Failed to parse JSON: " << errs << std::endl; + // } + // status_file.close(); + // } else { + // std::cerr << "Unable to open status.json" << std::endl; + // } + + return kScheduleStatusNormal; +} + +void set_schedule_status(ScheduleStatus status) { + std::string status_str; + switch (status) { + case kScheduleStatusNormal: + status_str = "normal"; + break; + case kScheduleStatusDebug: + status_str = "debug"; + break; + case kScheduleStatusUpgrade: + status_str = "upgrade"; + break; + } + + // Write to status.json + Json::Value json_data; + json_data["status"] = status_str; + + std::ofstream status_file("/opt/configenv/status.json"); + if (status_file.is_open()) { + status_file << json_data; + status_file.close(); + } else { + std::cerr << "Unable to open status.json for writing" << std::endl; + } + + // Append to status_history.json + Json::Value history_data; + // std::ifstream history_file("/opt/configenv/status_history.json"); + // if (history_file.is_open()) { + // if (Json::parseFromStream(Json::CharReaderBuilder(), history_file, &history_data, nullptr)) { + // // successfully read existing history + // } + // history_file.close(); + // } + + // Get the current time + auto now = std::chrono::system_clock::now(); + auto now_c = std::chrono::system_clock::to_time_t(now); + std::tm* now_tm = std::localtime(&now_c); + + // Format time as "YYYY-MM-DD HH:MM:SS" + char time_buffer[20]; + std::strftime(time_buffer, sizeof(time_buffer), "%Y-%m-%d %H:%M:%S", now_tm); + + // Add the new status and time to history + Json::Value new_entry; + new_entry["status"] = status_str; + new_entry["time"] = time_buffer; + history_data.append(new_entry); + + // Write back to status_history.json + std::ofstream history_file_out("/opt/configenv/status_history.json"); + if (history_file_out.is_open()) { + history_file_out << history_data; + history_file_out.close(); + } else { + std::cerr << "Unable to open status_history.json for writing" << std::endl; + } +} + +std::string get_status_desc(ScheduleStatus status) { + std::string status_str; + switch (status) { + case kScheduleStatusNormal: + status_str = "normal"; + break; + case kScheduleStatusDebug: + status_str = "debug"; + break; + case kScheduleStatusUpgrade: + status_str = "upgrade"; + break; + default: + status_str = "normal"; + zlog_error(zct, "fail to get status desc:%d", status); + break; + } + return status_str; +} diff --git a/scheduler/status_mgr.hpp b/scheduler/status_mgr.hpp new file mode 100644 index 0000000..0fe99c3 --- /dev/null +++ b/scheduler/status_mgr.hpp @@ -0,0 +1,23 @@ +#ifndef STATUS_MGR_HPP_ +#define STATUS_MGR_HPP_ +#include +/** + * @brief 对调度状态切换进行管理,并对调度切换时历史进行记录 + * + * @version 0.1 + * @author pandx (dxpan2002@163.com) + * @date 2026-01-21 + * @copyright Copyright (c) 2026 + */ + +typedef enum { + kScheduleStatusNormal = 1, + kScheduleStatusDebug = 2, + kScheduleStatusUpgrade = 3 +} ScheduleStatus; + +ScheduleStatus get_schedule_status(); +void set_schedule_status(ScheduleStatus status); +std::string get_status_desc(ScheduleStatus status); + +#endif \ No newline at end of file diff --git a/uart/uart.cpp b/uart/uart.cpp index 0ac2175..358cd18 100644 --- a/uart/uart.cpp +++ b/uart/uart.cpp @@ -29,6 +29,9 @@ pTestRecvCallBack pTestRecv; extern std::vector g_VecWaveDataX; extern std::vector g_VecWaveDataY; extern std::vector g_VecWaveDataZ; +extern std::vector g_VecWaveDataVolX; +extern std::vector g_VecWaveDataVolY; +extern std::vector g_VecWaveDataVolZ; int Uart::UartRecv(int fd, char srcshow, char *buffer) { char buff[BUF_LENGTH]; @@ -145,6 +148,12 @@ Uart::Uart() : mUart(mIoSev), mStrand(mIoSev) { VecWaveDataX.reserve(1000); VecWaveDataY.reserve(1000); VecWaveDataZ.reserve(1500); + m_waveCountVolX = 0; + m_waveCountVolY = 0; + m_waveCountVolZ = 0; + VecWaveDataVolX.reserve(1000); + VecWaveDataVolY.reserve(1000); + VecWaveDataVolZ.reserve(1500); memset(send_data, 0, sizeof(send_data)); last_short_addr = 0; last_time = 0; @@ -368,7 +377,8 @@ int Uart::DealAskTask(uint16_t ushortAdd){ int next_duration = 0,next_task_id = 0; int taskID; ScheduleTask scheduleTask; - taskID = scheduler::instance().StartSchedule(ushortAdd,next_duration, next_task_id); + bool z = false; + taskID = scheduler::instance().StartSchedule(ushortAdd,next_duration, z); zlog_info(zct, "taskID = %d next_duration = %d next_task_id = %d", taskID, next_duration, next_task_id); if (taskID == kScheduleConfigSensor) { @@ -439,7 +449,7 @@ int Uart::DealReviveDuration(uint16_t ushortAdd){ ScheduleTask scheduleTask; int next_taskID = 0; - uint16_t next_duration = scheduler::instance().GetNextDuration(ushortAdd,next_taskID); + uint16_t next_duration = scheduler::instance().GetNextDuration(ushortAdd); zlog_info(zct, "next_duration = %d next_taskID = %d", next_duration,next_taskID); scheduleTask.cmd = REVIVE_DURATION; scheduleTask.shortAddr = ushortAdd; @@ -468,18 +478,31 @@ int Uart::DealWaveCompress(const char *pData,uint16_t ushortAdd){ sprintf(buf, "%02x%02x", (ushortAdd >> 8) & 0xFF, ushortAdd & 0xFF); std::string strShortAddr = std::string(buf); compressWaveChannel tempchannel; - tempchannel.compressChannelX = pData[7]; - tempchannel.compressChannelY = pData[8]; - tempchannel.compressChannelZ = pData[9]; + tempchannel.compressChannelX = pData[8]; + tempchannel.compressChannelY = pData[9]; + tempchannel.compressChannelZ = pData[10]; + tempchannel.compressChannelVolX = pData[23]; + tempchannel.compressChannelVolY = pData[24]; + tempchannel.compressChannelVolZ = pData[25]; - tempchannel.CountX = BUILD_UINT32(pData[13], pData[12],pData[11],pData[10]); - tempchannel.CountY = BUILD_UINT32(pData[17], pData[16],pData[15],pData[14]); - tempchannel.CountZ = BUILD_UINT32(pData[21], pData[20],pData[19],pData[18]); + tempchannel.CountX = BUILD_UINT32(pData[14], pData[13],pData[12],pData[11]); + tempchannel.CountY = BUILD_UINT32(pData[18], pData[17],pData[16],pData[15]); + tempchannel.CountZ = BUILD_UINT32(pData[22], pData[21],pData[20],pData[19]); + tempchannel.CountVolX = BUILD_UINT32(pData[29], pData[28],pData[27],pData[26]); + tempchannel.CountVolY = BUILD_UINT32(pData[33], pData[32],pData[31],pData[30]); + tempchannel.CountVolZ = BUILD_UINT32(pData[37], pData[36],pData[35],pData[34]); + + tempchannel.samplerateX = BUILD_UINT32(pData[41], pData[40],pData[39],pData[38]); + tempchannel.samplerateY = BUILD_UINT32(pData[45], pData[44],pData[43],pData[42]); + tempchannel.samplerateZ = BUILD_UINT32(pData[49], pData[48],pData[47],pData[46]); + tempchannel.samplerateVolX = BUILD_UINT32(pData[53], pData[52],pData[51],pData[50]); + tempchannel.samplerateVolY = BUILD_UINT32(pData[57], pData[56],pData[55],pData[54]); + tempchannel.samplerateVolZ = BUILD_UINT32(pData[61], pData[60],pData[59],pData[58]); g_mapCompress[strShortAddr] = tempchannel; wave_shortAddr = ushortAdd; char sensor_rssi[10] = {0x00}; - sprintf(sensor_rssi, "%02d", pData[22] & 0xFF); + sprintf(sensor_rssi, "%02d", pData[7] & 0xFF); if (!strcmp(sensor_rssi, "00") || !strcmp(sensor_rssi, "0")) { char errorInfo[100] = {0x00}; sprintf(errorInfo, "No RSSI %s", sensor_rssi); @@ -617,7 +640,8 @@ int Uart::DealAskTaskOld(uint16_t ushortAdd){ int next_duration = 0,next_task_id = 0; int taskID; ScheduleTask scheduleTask; - taskID = scheduler::instance().StartSchedule(ushortAdd,next_duration, next_task_id); + bool z = false; + taskID = scheduler::instance().StartSchedule(ushortAdd,next_duration, z); zlog_info(zct, "DealAskTaskOld taskID = %d next_duration = %d next_task_id = %d", taskID, next_duration, next_task_id); if(next_task_id == kScheduleUpgrade){ scheduleTask.cmd = REVIVE_DURATION; @@ -639,6 +663,7 @@ void Uart::DealRecvData(const char *pData) { int next_duration = 0,next_task_id = 0; int taskID = 0; ScheduleTask scheduleTask; + bool z = false; zlog_info(zct, "shortAdd = %02x%02x,command = %d ",UINT16_HIGH(ushortAdd),UINT16_LOW(ushortAdd),command); if (command == WAVE_COMPRESS || command == UPGRADE_ASK) { @@ -708,7 +733,7 @@ void Uart::DealRecvData(const char *pData) { break; case UPGRADE_ASK: mssleep(50000); - taskID = scheduler::instance().StartSchedule(ushortAdd,next_duration, next_task_id); + taskID = scheduler::instance().StartSchedule(ushortAdd,next_duration, z); taskID = kScheduleUpgrade; if (taskID == kScheduleUpgrade){ UpdateWirelessNode(ushortAdd); @@ -835,15 +860,15 @@ void Uart::DealDataNodeName(const char *pData) { char insertSql[1024] = {0}; sprintf(insertSql, " '%s','%s','','','','','','0',''", - MeasurementID, shortAdd); + MeasurementID, szShortAdd); sqlite_db_ctrl::instance().InsertData("t_debug_info", insertSql); memset(insertSql,0,sizeof(insertSql)); sprintf(insertSql, " '%s','%s','0','0','0'", - MeasurementID, shortAdd); + MeasurementID, szShortAdd); sqlite_db_ctrl::instance().InsertData("t_wave_upload_rule_info", insertSql); memset(insertSql,0,sizeof(insertSql)); sprintf(insertSql, " '%s','%s','0','0','0','0'", - MeasurementID, shortAdd); + MeasurementID, szShortAdd); sqlite_db_ctrl::instance().InsertData("t_wave_triger_info", insertSql); } @@ -1258,13 +1283,17 @@ int Uart::FindRecvPackage(int bytesRead, char *mUartRecvBuf, char *head) { g_VecWaveDataZ.clear(); break; } - if ((mPackgeIndex == -1 || (unsigned int)UartRecvBuf[i + 6] == 0) && (command == WAVE_X || command == WAVE_Y || command == WAVE_Z) && now_task == WAVE_CMD) { + if ((mPackgeIndex == -1 || (unsigned int)UartRecvBuf[i + 6] == 0) && \ + (command == WAVE_X || command == WAVE_Y || command == WAVE_Z || command == WAVE_VOL_X || command == WAVE_VOL_Y || command == WAVE_VOL_Z) && \ + now_task == WAVE_CMD) { mPackgeIndex = UartRecvBuf[i + 6] & 0xFF; - } else if ((unsigned int)mPackgeIndex == (unsigned int)UartRecvBuf[i + 6] && mPackgeIndex != -1 && command != 2 && (command == WAVE_X || command == WAVE_Y || command == WAVE_Z) && now_task == WAVE_CMD) { + } else if ((unsigned int)mPackgeIndex == (unsigned int)UartRecvBuf[i + 6] && mPackgeIndex != -1 && command != 2 && \ + (command == WAVE_X || command == WAVE_Y || command == WAVE_Z || command == WAVE_VOL_X || command == WAVE_VOL_Y || command == WAVE_VOL_Z) && \ + now_task == WAVE_CMD) { zlog_warn(zct, "mPackgeIndex same index1:%d,index2:%02d ShortAddr :%s ", mPackgeIndex, UartRecvBuf[i + 6] & 0xff, strShortAddr.c_str()); continue; - } else if ((unsigned int)mPackgeIndex + 1 != (unsigned int)UartRecvBuf[i + 6] && mPackgeIndex != -1 && command != 2 && (command == WAVE_X || command == WAVE_Y || command == WAVE_Z) && now_task == WAVE_CMD) { + } else if ((unsigned int)mPackgeIndex + 1 != (unsigned int)UartRecvBuf[i + 6] && mPackgeIndex != -1 && command != 2 && (command == WAVE_X || command == WAVE_Y || command == WAVE_Z || command == WAVE_VOL_X || command == WAVE_VOL_Y || command == WAVE_VOL_Z) && now_task == WAVE_CMD) { zlog_warn(zct, "mPackgeIndex error index1:%d,index2:%02d ShortAddr :%s ,now_task = %d", mPackgeIndex, UartRecvBuf[i + 6] & 0xff, strShortAddr.c_str(),now_task); zlog_warn(zct, "mPackgeIndex error ShortAddr :%s", strShortAddr.c_str()); @@ -1307,7 +1336,7 @@ int Uart::FindRecvPackage(int bytesRead, char *mUartRecvBuf, char *head) { continue; } - if (now_task == WAVE_CMD && (command == WAVE_X || command == WAVE_Y || command == WAVE_Z)) { + if (now_task == WAVE_CMD && (command == WAVE_X || command == WAVE_Y || command == WAVE_Z || command == WAVE_VOL_X || command == WAVE_VOL_Y || command == WAVE_VOL_Z)) { if (!CheckCrc(&UartRecvBuf[i], 99)) { zlog_warn(zct, "CheckCrc error ShortAddr :%s command = %d", strShortAddr.c_str(), command); char tmp[10] = {0x00}; diff --git a/uart/uart.hpp b/uart/uart.hpp index f966cf4..198b702 100644 --- a/uart/uart.hpp +++ b/uart/uart.hpp @@ -31,7 +31,10 @@ enum InteractiveCommand { WAVE_COMPRESS = 15, // 波形数据压缩 UPGRADE_FIRMWARE = 16, //固件升级内容 UPGRADE_ASK = 17, //固件升级请求 - WAVE_RESP = 18 // 波形数据回复 + WAVE_RESP = 18, // 波形数据回复 + WAVE_VOL_X = 19, + WAVE_VOL_Y = 20, + WAVE_VOL_Z = 21 }; // 无线传感器请求任务 @@ -263,9 +266,15 @@ private: int m_waveCountX; int m_waveCountY; int m_waveCountZ; + int m_waveCountVolX; + int m_waveCountVolY; + int m_waveCountVolZ; std::vector VecWaveDataX; std::vector VecWaveDataY; std::vector VecWaveDataZ; + std::vector VecWaveDataVolX; + std::vector VecWaveDataVolY; + std::vector VecWaveDataVolZ; uint8_t send_data[100]; uint16_t last_short_addr; diff --git a/uart/uart_feature_parse.cpp b/uart/uart_feature_parse.cpp index e54ae56..2c0611e 100644 --- a/uart/uart_feature_parse.cpp +++ b/uart/uart_feature_parse.cpp @@ -20,6 +20,9 @@ extern zlog_category_t *zbt; std::vector g_VecWaveDataX; std::vector g_VecWaveDataY; std::vector g_VecWaveDataZ; +std::vector g_VecWaveDataVolX; +std::vector g_VecWaveDataVolY; +std::vector g_VecWaveDataVolZ; std::map g_mapCompress; std::map g_mapWaveChannel; @@ -366,6 +369,9 @@ int Uart::DealDataNodeFeature(const char *pData, int flag) { DataExtract(pRecvData, 24, lowbit, n); dataDymX.Amp5 = lowbit * n; + DataExtract(pRecvData, 84, lowbit, n); + dataDymX.kurtosis = lowbit * n; + memset(buf, 0, sizeof(buf)); dataDymX.EnvelopEnergy = 0; memset(buf, 0, sizeof(buf)); @@ -384,14 +390,14 @@ int Uart::DealDataNodeFeature(const char *pData, int flag) { sprintf(whereCon, "channelID='%s' ", (strMeasurementID + "-X").c_str()); memset(updateSql, 0, 1024); sprintf(updateSql, "diagnosisPk='%f',integratPk='%f',integratRMS='%f',rmsValues='%f',envelopEnergy='%f',\ - Amp1='%f',Amp2='%f',Amp3='%f',Amp4='%f',Amp5='%f',Phase1='%f',Phase2='%f',Phase3='%f',Phase4='%f',timeStamp='%s',StaticIndex = %ld,nodeResend = %d, nodeTimestamp = '%s' ", + Amp1='%f',Amp2='%f',Amp3='%f',Amp4='%f',Amp5='%f',Phase1='%f',Phase2='%f',Phase3='%f',Phase4='%f',timeStamp='%s',StaticIndex = %ld,nodeResend = %d, nodeTimestamp = '%s',kurtosis = '%f'", dataDymX.DiagnosisPk, dataDymX.IntegratPk, dataDymX.IntegratRMS, dataDymX.RmsValues, dataDymX.EnvelopEnergy, dataDymX.Amp1, dataDymX.Amp2, dataDymX.Amp3, dataDymX.Amp4, dataDymX.Amp5, dataDymX.Phase1, dataDymX.Phase2, dataDymX.Phase3, dataDymX.Phase4, - nowTimetamp.c_str(), staticIndex, nodeResend,nodetimestamp); + nowTimetamp.c_str(), staticIndex, nodeResend,nodetimestamp,dataDymX.kurtosis); if ((Count * 3 < SAVE_COUNT && lTime < OneWeek) || strTime.size() == 0 || nodeResend != 0) { // 1 week char insertSql[1024] = {0}; memset(insertSql, 0x00, sizeof(insertSql)); - sprintf(insertSql, "'%s','%s','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f',%ld,'%s','1',%d,'%s'", strMeasurementID.c_str(), (strMeasurementID + "-X").c_str(), dataDymX.DiagnosisPk, dataDymX.IntegratPk, dataDymX.IntegratRMS, dataDymX.RmsValues, - dataDymX.EnvelopEnergy, dataDymX.Amp1, dataDymX.Amp2, dataDymX.Amp3, dataDymX.Amp4, dataDymX.Amp5, dataDymX.Phase1, dataDymX.Phase2, dataDymX.Phase3, dataDymX.Phase4, staticIndex, nowTimetamp.c_str(), nodeResend,nodetimestamp); + sprintf(insertSql, "'%s','%s','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f',%ld,'%s','1',%d,'%s','%f',0", strMeasurementID.c_str(), (strMeasurementID + "-X").c_str(), dataDymX.DiagnosisPk, dataDymX.IntegratPk, dataDymX.IntegratRMS, dataDymX.RmsValues, + dataDymX.EnvelopEnergy, dataDymX.Amp1, dataDymX.Amp2, dataDymX.Amp3, dataDymX.Amp4, dataDymX.Amp5, dataDymX.Phase1, dataDymX.Phase2, dataDymX.Phase3, dataDymX.Phase4, staticIndex, nowTimetamp.c_str(), nodeResend,nodetimestamp,dataDymX.kurtosis); sqlite_db_ctrl::instance().InsertData(szTableName, insertSql); if (0 == sqlite_db_ctrl::instance().GetTableRows(T_DATA_INFO(TNAME), whereCon)) @@ -428,6 +434,7 @@ int Uart::DealDataNodeFeature(const char *pData, int flag) { valNodeFeature["Phase2"] = dataDymX.Phase2; valNodeFeature["Phase3"] = dataDymX.Phase3; valNodeFeature["Phase4"] = dataDymX.Phase4; + valNodeFeature["kurtosis"] = dataDymX.kurtosis; valNodeFeature["timeStamp"] = localtimestamp; if (Feature_x) { @@ -462,6 +469,9 @@ int Uart::DealDataNodeFeature(const char *pData, int flag) { DataExtract(pRecvData, 50, lowbit, n); dataDymY.Amp5 = lowbit * n; + DataExtract(pRecvData, 86, lowbit, n); + dataDymY.kurtosis = lowbit * n; + memset(buf, 0, sizeof(buf)); dataDymY.EnvelopEnergy = 0; memset(buf, 0, sizeof(buf)); @@ -477,14 +487,14 @@ int Uart::DealDataNodeFeature(const char *pData, int flag) { sprintf(whereCon, "channelID='%s' ", (strMeasurementID + "-Y").c_str()); memset(updateSql, 0, 1024); sprintf(updateSql, "diagnosisPk='%f',integratPk='%f',integratRMS='%f',rmsValues='%f',envelopEnergy='%f',\ - Amp1='%f',Amp2='%f',Amp3='%f',Amp4='%f',Amp5='%f',Phase1='%f',Phase2='%f',Phase3='%f',Phase4='%f',timeStamp='%s',StaticIndex = %ld,nodeResend = %d, nodeTimestamp = '%s' ", + Amp1='%f',Amp2='%f',Amp3='%f',Amp4='%f',Amp5='%f',Phase1='%f',Phase2='%f',Phase3='%f',Phase4='%f',timeStamp='%s',StaticIndex = %ld,nodeResend = %d, nodeTimestamp = '%s',kurtosis = '%f' ", dataDymY.DiagnosisPk, dataDymY.IntegratPk, dataDymY.IntegratRMS, dataDymY.RmsValues, dataDymY.EnvelopEnergy, dataDymY.Amp1, dataDymY.Amp2, dataDymY.Amp3, dataDymY.Amp4, dataDymY.Amp5, dataDymY.Phase1, dataDymY.Phase2, dataDymY.Phase3, dataDymY.Phase4, - nowTimetamp.c_str(), staticIndex, nodeResend,nodetimestamp); + nowTimetamp.c_str(), staticIndex, nodeResend,nodetimestamp,dataDymY.kurtosis); if (/*0 == sqlite_db_ctrl::instance().GetTableRows(T_DATA_INFO(TNAME), whereCon)*/ (Count * 3 < SAVE_COUNT && lTime < OneWeek) || strTime.size() == 0 ) { char insertSql[1024] = {0}; memset(insertSql, 0x00, sizeof(insertSql)); - sprintf(insertSql, "'%s','%s','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f',%ld,'%s','1',%d,%s", strMeasurementID.c_str(), (strMeasurementID + "-Y").c_str(), dataDymY.DiagnosisPk, dataDymY.IntegratPk, dataDymY.IntegratRMS, dataDymY.RmsValues, - dataDymY.EnvelopEnergy, dataDymY.Amp1, dataDymY.Amp2, dataDymY.Amp3, dataDymY.Amp4, dataDymY.Amp5, dataDymY.Phase1, dataDymY.Phase2, dataDymY.Phase3, dataDymY.Phase4, staticIndex, nowTimetamp.c_str(), nodeResend,nodetimestamp); + sprintf(insertSql, "'%s','%s','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f',%ld,'%s','1',%d,%s,%f,0", strMeasurementID.c_str(), (strMeasurementID + "-Y").c_str(), dataDymY.DiagnosisPk, dataDymY.IntegratPk, dataDymY.IntegratRMS, dataDymY.RmsValues, + dataDymY.EnvelopEnergy, dataDymY.Amp1, dataDymY.Amp2, dataDymY.Amp3, dataDymY.Amp4, dataDymY.Amp5, dataDymY.Phase1, dataDymY.Phase2, dataDymY.Phase3, dataDymY.Phase4, staticIndex, nowTimetamp.c_str(), nodeResend,nodetimestamp,dataDymY.kurtosis); sqlite_db_ctrl::instance().InsertData(szTableName, insertSql); if (0 == sqlite_db_ctrl::instance().GetTableRows(T_DATA_INFO(TNAME), whereCon)) @@ -499,8 +509,8 @@ int Uart::DealDataNodeFeature(const char *pData, int flag) { sprintf(whereCon, "channelID='%s' ", (strMeasurementID + "-Y").c_str()); sqlite_db_ctrl::instance().UpdateTableData(T_DATA_INFO(TNAME), updateSql, whereCon); } - zlog_info(zct, "y: %s,%s,diagnosisPk=%f,integratPk=%f,integratRMS=%f,rmsValues=%f,envelopEnergy=%f,Amp1=%f,Amp2=%f,Amp3=%f,Amp4=%f,Amp5=%f,Phase1=%f,Phase2=%f,Phase3=%f,Phase4=%f,timeStamp=%s", strMeasurementID.c_str(), (strMeasurementID + "-Y").c_str(), dataDymY.DiagnosisPk, - dataDymY.IntegratPk, dataDymY.IntegratRMS, dataDymY.RmsValues, dataDymY.EnvelopEnergy, dataDymY.Amp1, dataDymY.Amp2, dataDymY.Amp3, dataDymY.Amp4, dataDymY.Amp5, dataDymY.Phase1, dataDymY.Phase2, dataDymY.Phase3, dataDymY.Phase4, nowTimetamp.c_str()); + zlog_info(zct, "y: %s,%s,diagnosisPk=%f,integratPk=%f,integratRMS=%f,rmsValues=%f,envelopEnergy=%f,Amp1=%f,Amp2=%f,Amp3=%f,Amp4=%f,Amp5=%f,Phase1=%f,Phase2=%f,Phase3=%f,Phase4=%f,timeStamp=%s,,kurtosis = %f", strMeasurementID.c_str(), (strMeasurementID + "-Y").c_str(), dataDymY.DiagnosisPk, + dataDymY.IntegratPk, dataDymY.IntegratRMS, dataDymY.RmsValues, dataDymY.EnvelopEnergy, dataDymY.Amp1, dataDymY.Amp2, dataDymY.Amp3, dataDymY.Amp4, dataDymY.Amp5, dataDymY.Phase1, dataDymY.Phase2, dataDymY.Phase3, dataDymY.Phase4, nowTimetamp.c_str(),dataDymY.kurtosis); valNodeFeature["dataNodeNo"] = strMeasurementID; valNodeFeature["ChannelId"] = strMeasurementID + "-Y"; @@ -518,6 +528,7 @@ int Uart::DealDataNodeFeature(const char *pData, int flag) { valNodeFeature["Phase2"] = dataDymY.Phase2; valNodeFeature["Phase3"] = dataDymY.Phase3; valNodeFeature["Phase4"] = dataDymY.Phase4; + valNodeFeature["kurtosis"] = dataDymY.kurtosis; valNodeFeature["timeStamp"] = localtimestamp; if (Feature_y) { @@ -567,19 +578,26 @@ int Uart::DealDataNodeFeature(const char *pData, int flag) { DataExtract(pRecvData, 78, lowbit, n); dataDymZ.Phase4 = lowbit * n; + DataExtract(pRecvData, 88, lowbit, n); + dataDymZ.kurtosis = lowbit * n; + + DataExtract(pRecvData, 90, lowbit, n); + dataDymZ.IntegratRMSMENS = lowbit * n; + + memset(whereCon, 0, 1024); sprintf(whereCon, "channelID='%s' ", (strMeasurementID + "-Z").c_str()); memset(updateSql, 0, 1024); sprintf(updateSql, "diagnosisPk='%f',integratPk='%f',integratRMS='%f',rmsValues='%f',envelopEnergy='%f',\ - Amp1='%f',Amp2='%f',Amp3='%f',Amp4='%f',Amp5='%f',Phase1='%f',Phase2='%f',Phase3='%f',Phase4='%f',timeStamp='%s',StaticIndex = %ld,nodeResend = %d, nodeTimestamp = '%s' ", + Amp1='%f',Amp2='%f',Amp3='%f',Amp4='%f',Amp5='%f',Phase1='%f',Phase2='%f',Phase3='%f',Phase4='%f',timeStamp='%s',StaticIndex = %ld,nodeResend = %d, nodeTimestamp = '%s',kurtosis = '%f',integratRMSMENS = '%f' ", dataDymZ.DiagnosisPk, dataDymZ.IntegratPk, dataDymZ.IntegratRMS, dataDymZ.RmsValues, dataDymZ.EnvelopEnergy, dataDymZ.Amp1, dataDymZ.Amp2, dataDymZ.Amp3, dataDymZ.Amp4, dataDymZ.Amp5, dataDymZ.Phase1, dataDymZ.Phase2, dataDymZ.Phase3, dataDymZ.Phase4, - nowTimetamp.c_str(), staticIndex, nodeResend,nodetimestamp); + nowTimetamp.c_str(), staticIndex, nodeResend,nodetimestamp,dataDymZ.kurtosis,dataDymZ.IntegratRMSMENS); if (/*0 == sqlite_db_ctrl::instance().GetTableRows(T_DATA_INFO(TNAME), whereCon)*/ (Count * 3 < SAVE_COUNT && lTime < OneWeek) || strTime.size() == 0 ) { char insertSql[1024] = {0}; memset(insertSql, 0x00, sizeof(insertSql)); - sprintf(insertSql, "'%s','%s','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f',%ld,'%s','1',%d,'%s'", strMeasurementID.c_str(), (strMeasurementID + "-Z").c_str(), dataDymZ.DiagnosisPk, dataDymZ.IntegratPk, dataDymZ.IntegratRMS, dataDymZ.RmsValues, - dataDymZ.EnvelopEnergy, dataDymZ.Amp1, dataDymZ.Amp2, dataDymZ.Amp3, dataDymZ.Amp4, dataDymZ.Amp5, dataDymZ.Phase1, dataDymZ.Phase2, dataDymZ.Phase3, dataDymZ.Phase4, staticIndex, nowTimetamp.c_str(), nodeResend,nodetimestamp); + sprintf(insertSql, "'%s','%s','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f',%ld,'%s','1',%d,'%s','%f','%f'", strMeasurementID.c_str(), (strMeasurementID + "-Z").c_str(), dataDymZ.DiagnosisPk, dataDymZ.IntegratPk, dataDymZ.IntegratRMS, dataDymZ.RmsValues, + dataDymZ.EnvelopEnergy, dataDymZ.Amp1, dataDymZ.Amp2, dataDymZ.Amp3, dataDymZ.Amp4, dataDymZ.Amp5, dataDymZ.Phase1, dataDymZ.Phase2, dataDymZ.Phase3, dataDymZ.Phase4, staticIndex, nowTimetamp.c_str(), nodeResend,nodetimestamp,dataDymZ.kurtosis,dataDymZ.IntegratRMSMENS); sqlite_db_ctrl::instance().InsertData(szTableName, insertSql); if (0 == sqlite_db_ctrl::instance().GetTableRows(T_DATA_INFO(TNAME), whereCon)) @@ -594,8 +612,8 @@ int Uart::DealDataNodeFeature(const char *pData, int flag) { sprintf(whereCon, "channelID='%s' ", (strMeasurementID + "-Z").c_str()); sqlite_db_ctrl::instance().UpdateTableData(T_DATA_INFO(TNAME), updateSql, whereCon); } - zlog_info(zct, "Z: %s,%s,diagnosisPk=%f,integratPk=%f,integratRMS=%f,rmsValues=%f,envelopEnergy=%f,Amp1=%f,Amp2=%f,Amp3=%f,Amp4=%f,Amp5=%f,Phase1=%f,Phase2=%f,Phase3=%f,Phase4=%f,timeStamp=%s", strMeasurementID.c_str(), (strMeasurementID + "-Z").c_str(), dataDymZ.DiagnosisPk, - dataDymZ.IntegratPk, dataDymZ.IntegratRMS, dataDymZ.RmsValues, dataDymZ.EnvelopEnergy, dataDymZ.Amp1, dataDymZ.Amp2, dataDymZ.Amp3, dataDymZ.Amp4, dataDymZ.Amp5, dataDymZ.Phase1, dataDymZ.Phase2, dataDymZ.Phase3, dataDymZ.Phase4, nowTimetamp.c_str()); + zlog_info(zct, "Z: %s,%s,diagnosisPk=%f,integratPk=%f,integratRMS=%f,rmsValues=%f,envelopEnergy=%f,Amp1=%f,Amp2=%f,Amp3=%f,Amp4=%f,Amp5=%f,Phase1=%f,Phase2=%f,Phase3=%f,Phase4=%f,timeStamp=%s,kurtosis = %f,integratRMSMENS = %f", strMeasurementID.c_str(), (strMeasurementID + "-Z").c_str(), dataDymZ.DiagnosisPk, + dataDymZ.IntegratPk, dataDymZ.IntegratRMS, dataDymZ.RmsValues, dataDymZ.EnvelopEnergy, dataDymZ.Amp1, dataDymZ.Amp2, dataDymZ.Amp3, dataDymZ.Amp4, dataDymZ.Amp5, dataDymZ.Phase1, dataDymZ.Phase2, dataDymZ.Phase3, dataDymZ.Phase4, nowTimetamp.c_str(),dataDymZ.kurtosis,dataDymZ.IntegratRMSMENS); memset(whereCon, 0x00, sizeof(whereCon)); sprintf(whereCon, "MeasurementID='%s'", strMeasurementID.c_str()); @@ -618,6 +636,7 @@ int Uart::DealDataNodeFeature(const char *pData, int flag) { valNodeFeature["Phase2"] = dataDymZ.Phase2; valNodeFeature["Phase3"] = dataDymZ.Phase3; valNodeFeature["Phase4"] = dataDymZ.Phase4; + valNodeFeature["kurtosis"] = dataDymZ.kurtosis; valNodeFeature["timeStamp"] = localtimestamp; if (Feature_z) { @@ -696,6 +715,12 @@ void Uart::DealDataNodeWave(const char *pData, int comand) { VecWaveDataY.push_back(*pRecvData); } else if (comand == WAVE_Z) { VecWaveDataZ.push_back(*pRecvData); + }else if (comand == WAVE_VOL_X) { + VecWaveDataVolX.push_back(*pRecvData); + }else if (comand == WAVE_VOL_Y) { + VecWaveDataVolY.push_back(*pRecvData); + }else if (comand == WAVE_VOL_Z) { + VecWaveDataVolZ.push_back(*pRecvData); } } else { if (comand == WAVE_X) { @@ -707,6 +732,15 @@ void Uart::DealDataNodeWave(const char *pData, int comand) { } else if (comand == WAVE_Z) { g_VecWaveDataZ[m_waveCountZ] = *pRecvData; m_waveCountZ++; + }else if (comand == WAVE_VOL_X) { + g_VecWaveDataVolX[m_waveCountVolX] = *pRecvData; + m_waveCountVolX++; + }else if (comand == WAVE_VOL_Y) { + g_VecWaveDataVolY[m_waveCountVolY] = *pRecvData; + m_waveCountVolY++; + }else if (comand == WAVE_VOL_Z) { + g_VecWaveDataVolZ[m_waveCountVolZ] = *pRecvData; + m_waveCountVolZ++; } } @@ -784,6 +818,54 @@ std::vector Uart::DealData(int iChannel, float coe, unsigned int sampleRa compress = g_mapCompress[strShortAddr].compressChannelZ; count = g_mapCompress[strShortAddr].CountZ; } + if (iChannel == WAVE_VOL_X) { + if (VecWaveDataVolX.size() > 0) { + g_VecWaveDataVolX.assign(VecWaveDataVolX.begin(), VecWaveDataVolX.end()); + waveCount = VecWaveDataVolX.size(); + } + waveCount = m_waveCountVolX; + for (; j < waveCount; j++) { + RecvData recvData = g_VecWaveDataVolX[j]; + memcpy(data + j * 92, recvData.Data, 92); + } + memset(buf, 0x00, sizeof(buf)); + sprintf(buf, "%02x%02x", g_VecWaveDataVolX[0].ShortAddr[0], g_VecWaveDataVolX[0].ShortAddr[1]); + strShortAddr = std::string(buf); + compress = g_mapCompress[strShortAddr].compressChannelVolX; + count = g_mapCompress[strShortAddr].CountVolX; + } + if (iChannel == WAVE_VOL_Y) { + if (VecWaveDataVolY.size() > 0) { + g_VecWaveDataVolY.assign(VecWaveDataVolY.begin(), VecWaveDataVolY.end()); + waveCount = VecWaveDataVolY.size(); + } + waveCount = m_waveCountVolY; + for (; j < waveCount; j++) { + RecvData recvData = g_VecWaveDataVolY[j]; + memcpy(data + j * 92, recvData.Data, 92); + } + memset(buf, 0x00, sizeof(buf)); + sprintf(buf, "%02x%02x", g_VecWaveDataVolY[0].ShortAddr[0], g_VecWaveDataVolY[0].ShortAddr[1]); + strShortAddr = std::string(buf); + compress = g_mapCompress[strShortAddr].compressChannelVolY; + count = g_mapCompress[strShortAddr].CountVolY; + } + if (iChannel == WAVE_VOL_Z) { + if (VecWaveDataVolZ.size() > 0) { + g_VecWaveDataVolZ.assign(VecWaveDataVolZ.begin(), VecWaveDataVolZ.end()); + waveCount = VecWaveDataVolZ.size(); + } + waveCount = m_waveCountVolZ; + for (; j < waveCount; j++) { + RecvData recvData = g_VecWaveDataVolZ[j]; + memcpy(data + j * 92, recvData.Data, 92); + } + memset(buf, 0x00, sizeof(buf)); + sprintf(buf, "%02x%02x", g_VecWaveDataVolZ[0].ShortAddr[0], g_VecWaveDataVolZ[0].ShortAddr[1]); + strShortAddr = std::string(buf); + compress = g_mapCompress[strShortAddr].compressChannelVolZ; + count = g_mapCompress[strShortAddr].CountVolZ; + } zlog_info(zct, "len = %d,data = %02x,iChannel = %d,compress = %d,count = %d", j, data[0], iChannel, compress, count); if (j * 92 < count) return vecData; @@ -852,6 +934,7 @@ void Uart::DealWave() { std::string strFileName = ""; std::string strProduct = ""; std::vector vecDataX, vecDataY, vecDataZ; + bool z = false; if (wave_trans_) { //对每个传感器的每个通道进行遍历然后处理数据,例如:传感器1x轴的数据处理完后,再去处理y轴的。传感器1的所有数据处理完后,再处理传感器2的 char getzigbeeShortAddr[32] = {0}, tableName[64] = {0}, whereCon[128] = {0}; @@ -942,7 +1025,7 @@ void Uart::DealWave() { (vecDataY.size() <= 0 && g_mapCompress[strShortAddr].CountY > 0) || (vecDataZ.size() <= 0 && g_mapCompress[strShortAddr].CountZ > 0)) { - int iRet = scheduler::instance().WaveError(wave_shortAddr); + int iRet = scheduler::instance().WaveError(wave_shortAddr,z); if (iRet != 0) { memset(whereCon, 0x00, sizeof(whereCon)); char updateSql[1024] = { 0 }; @@ -954,18 +1037,17 @@ void Uart::DealWave() { (vecDataY.size() > 0 && g_mapCompress[strShortAddr].CountY > 0) || (vecDataZ.size() > 0 && g_mapCompress[strShortAddr].CountZ > 0)) { - int iRet = scheduler::instance().WaveSuccess(wave_shortAddr); - if (iRet != 0) { - memset(whereCon, 0x00, sizeof(whereCon)); - char updateSql[1024] = { 0 }; - sprintf(updateSql, "resend = '%d'", iRet); - sprintf(whereCon, "short_addr='%02x%02x' and timestamp = '%s'", (wave_shortAddr >> 8) & 0xFF,wave_shortAddr & 0xFF,(char*)timestamp_last.c_str()); - sqlite_db_ctrl::instance().UpdateTableData(" receive_wave_status ", updateSql, whereCon); - } + scheduler::instance().WaveSuccess(wave_shortAddr,z); + memset(whereCon, 0x00, sizeof(whereCon)); + char updateSql[1024] = { 0 }; + sprintf(updateSql, "resend = '%d'", 1); + sprintf(whereCon, "short_addr='%02x%02x' and timestamp = '%s'", (wave_shortAddr >> 8) & 0xFF,wave_shortAddr & 0xFF,(char*)timestamp_last.c_str()); + sqlite_db_ctrl::instance().UpdateTableData(" receive_wave_status ", updateSql, whereCon); + } if (g_mapCompress[strShortAddr].CountX <= 0 && g_mapCompress[strShortAddr].CountY <= 0 && g_mapCompress[strShortAddr].CountZ <= 0) { - int iRet = scheduler::instance().WaveError(wave_shortAddr); + int iRet = scheduler::instance().WaveError(wave_shortAddr,z); if (iRet != 0) { memset(whereCon, 0x00, sizeof(whereCon)); char updateSql[1024] = { 0 }; diff --git a/uart/uart_parameter_config.cpp b/uart/uart_parameter_config.cpp index 2212f52..b236d8c 100644 --- a/uart/uart_parameter_config.cpp +++ b/uart/uart_parameter_config.cpp @@ -50,7 +50,7 @@ void Uart::UpdateWirelessNode(uint16_t shortAdd) { char localtimestamp[32] = {0}; GetTimeNet(localtimestamp, 1); char insertSql[100] = {0}; - char wherecon[100] = {0}; + char wherecon[512] = {0}; sprintf(wherecon," short_Addr = '%02x%02x' and status = 3 and start_timestamp > ( SELECT MAX(submit_timestamp) FROM firmware_upgrade ) order by start_timestamp DESC",UINT16_HIGH(shortAdd), UINT16_LOW(shortAdd)); std::string spend_count = sqlite_db_ctrl::instance().GetData(" firmware_upgrade ","spend_count",wherecon); if (atoi(spend_count.c_str()) >= 10){