diff --git a/jsonparse/cmt_parse.cpp b/jsonparse/cmt_parse.cpp index afac081..8111c94 100644 --- a/jsonparse/cmt_parse.cpp +++ b/jsonparse/cmt_parse.cpp @@ -229,7 +229,7 @@ void JsonData::CmtCmd_82(char* MeasurementID,char* send_data,int& channel,int& s void JsonData::CmtCmd_83(char* recv_body,int& count,char* send_data,int& send_length) { - vec_t vecRes; + array_t arrRes; std::string filename = ""; char whereCon[128]={0}; if (recv_body){ @@ -247,84 +247,95 @@ void JsonData::CmtCmd_83(char* recv_body,int& count,char* send_data,int& send_le } } sprintf(whereCon,"MeasurementID IN (%s)",MeasurementID_); - vecRes = sqlite_db_ctrl::instance().GetDataSingleLine(T_SENSOR_INFO(TNAME), "*", whereCon); + arrRes = sqlite_db_ctrl::instance().GetDataMultiLineTransaction(T_SENSOR_INFO(TNAME), "*", whereCon); }else{ - vecRes = sqlite_db_ctrl::instance().GetDataSingleLine(T_SENSOR_INFO(TNAME), "*", NULL); + arrRes = sqlite_db_ctrl::instance().GetDataMultiLineTransaction(T_SENSOR_INFO(TNAME), "*", NULL); } - if (vecRes.size() > 0){ - DownloadConfig download_config; - memcpy(download_config.gw_mac,GlobalConfig::MacAddr_G.c_str(),sizeof(download_config.gw_mac)); - if (vecRes[17] == "01"){ - memcpy(download_config.product,"DN101",sizeof(download_config.product)); - }else if (vecRes[17] == "02"){ - memcpy(download_config.product,"DN102",sizeof(download_config.product)); - } - memcpy(download_config.ip,GlobalConfig::IpAddr_G.c_str(),sizeof(download_config.ip)); - download_config.panid = atoi(vecRes[28].c_str()); - download_config.signal_channle = atoi(vecRes[29].c_str()); - memcpy(download_config.terminal_name,"",sizeof(download_config.terminal_name)); - memcpy(download_config.sn,vecRes[11].c_str(),sizeof(download_config.sn)); - memcpy(download_config.sensor_mac,vecRes[0].c_str(),sizeof(download_config.sensor_mac)); - memcpy(download_config.measurement_id,vecRes[44].c_str(),sizeof(download_config.measurement_id)); - download_config.short_addr = atoi(vecRes[30].c_str()); - memcpy(download_config.sensor_name,vecRes[1].c_str(),sizeof(download_config.sensor_name)); - memcpy(download_config.update_date,vecRes[38].c_str(),sizeof(download_config.update_date)); - std::vector vParamRSSI; - boost::split(vParamRSSI, vecRes[40], boost::is_any_of(","), boost::token_compress_on); - if (vParamRSSI.size() > 1) { - download_config.gateway_rssi = (atof(vParamRSSI[0].c_str())/255) * 100; - download_config.sensor_rssi = (atof(vParamRSSI[1].c_str())/255) * 100; - } else { - download_config.gateway_rssi = (atof(vParamRSSI[0].c_str())/255) * 100; - download_config.sensor_rssi = 99; - } - memcpy(download_config.hw_ver,vecRes[8].c_str(),sizeof(download_config.hw_ver)); - memcpy(download_config.soft_ver,vecRes[9].c_str(),sizeof(download_config.soft_ver)); - download_config.sampling_rate = atoi(vecRes[23].c_str()); - download_config.range = atoi(vecRes[25].c_str()); - download_config.sampling_time = atoi(vecRes[36].c_str()); - download_config.viff = atoi(vecRes[39].c_str()); - download_config.power = atoi(vecRes[33].c_str()); - download_config.retry_time = atoi(vecRes[34].c_str()); - + if (arrRes.size() > 0){ char localtimestamp[32] = {0}; - GetTimeNet(localtimestamp, 1); - filename = "/opt/DataNode/config_" + std::string(localtimestamp)+ ".csv"; + std::string current_time = GetLocalTimeWithMs(); + filename = "/opt/DataNode/config_" + GlobalConfig::MacAddr_G + "_" + std::string(current_time)+ ".csv"; std::ofstream csvFile(filename); if (!csvFile.is_open()) { std::cerr << "Error: Could not open file " << filename << std::endl; return; } - - // 写入 CSV 标题行 + // 写入 CSV 标题行 csvFile << "GW MAC,Product,IP,PanID,Signal Channel,Terminal Name,SN,Sensor MAC,Measurement ID," "Short Addr,Sensor Name,Update Date,Gateway RSSI,Sensor RSSI,HW Ver,Soft Ver,Sampling Rate," "Range,Sampling Time,VIFF,Power,Retry Time\n"; - csvFile << download_config.gw_mac << ',' << download_config.product << ','<< download_config.ip << ',' << download_config.panid << ',' - << download_config.signal_channle << ','<< download_config.terminal_name << ','<< download_config.sn << ','<< download_config.sensor_mac << ',' - << download_config.measurement_id << ','<< download_config.short_addr << ','<< download_config.sensor_name << ','<< download_config.update_date << ',' - << download_config.gateway_rssi << ','<< download_config.sensor_rssi << ','<< download_config.hw_ver << ','<< download_config.soft_ver << ',' - << download_config.sampling_rate << ','<< download_config.range << ','<< download_config.sampling_time << ','<< download_config.viff << ',' - << download_config.power << ','<< download_config.retry_time ; + for (size_t i = 0; i < count; i++) + { + DownloadConfig download_config; + memcpy(download_config.gw_mac,GlobalConfig::MacAddr_G.c_str(),sizeof(download_config.gw_mac)); + if (arrRes[i][17] == "01"){ + memcpy(download_config.product,"DN101",sizeof(download_config.product)); + }else if (arrRes[i][17] == "02"){ + memcpy(download_config.product,"DN102",sizeof(download_config.product)); + } + memcpy(download_config.ip,GlobalConfig::IpAddr_G.c_str(),sizeof(download_config.ip)); + download_config.panid = atoi(arrRes[i][28].c_str()); + download_config.signal_channle = atoi(arrRes[i][29].c_str()); + memcpy(download_config.terminal_name,"",sizeof(download_config.terminal_name)); + memcpy(download_config.sn,arrRes[i][11].c_str(),sizeof(download_config.sn)); + memcpy(download_config.sensor_mac,arrRes[i][0].c_str(),sizeof(download_config.sensor_mac)); + memcpy(download_config.measurement_id,arrRes[i][44].c_str(),sizeof(download_config.measurement_id)); + download_config.short_addr = atoi(arrRes[i][30].c_str()); + memcpy(download_config.sensor_name,arrRes[i][1].c_str(),sizeof(download_config.sensor_name)); + memcpy(download_config.update_date,arrRes[i][38].c_str(),sizeof(download_config.update_date)); + std::vector vParamRSSI; + boost::split(vParamRSSI, arrRes[i][40], boost::is_any_of(","), boost::token_compress_on); + if (vParamRSSI.size() > 1) { + download_config.gateway_rssi = (atof(vParamRSSI[0].c_str())/255) * 100; + download_config.sensor_rssi = (atof(vParamRSSI[1].c_str())/255) * 100; + } else { + download_config.gateway_rssi = (atof(vParamRSSI[0].c_str())/255) * 100; + download_config.sensor_rssi = 99; + } + memcpy(download_config.hw_ver,arrRes[i][8].c_str(),sizeof(download_config.hw_ver)); + memcpy(download_config.soft_ver,arrRes[i][9].c_str(),sizeof(download_config.soft_ver)); + download_config.sampling_rate = atoi(arrRes[i][23].c_str()); + download_config.range = atoi(arrRes[i][25].c_str()); + download_config.sampling_time = atoi(arrRes[i][36].c_str()); + download_config.viff = atoi(arrRes[i][39].c_str()); + download_config.power = atoi(arrRes[i][33].c_str()); + download_config.retry_time = atoi(arrRes[i][34].c_str()); + + csvFile << download_config.gw_mac << ',' << download_config.product << ','<< download_config.ip << ',' << download_config.panid << ',' + << download_config.signal_channle << ','<< download_config.terminal_name << ','<< download_config.sn << ','<< download_config.sensor_mac << ',' + << download_config.measurement_id << ','<< download_config.short_addr << ','<< download_config.sensor_name << ','<< download_config.update_date << ',' + << download_config.gateway_rssi << ','<< download_config.sensor_rssi << ','<< download_config.hw_ver << ','<< download_config.soft_ver << ',' + << download_config.sampling_rate << ','<< download_config.range << ','<< download_config.sampling_time << ','<< download_config.viff << ',' + << download_config.power << ','<< download_config.retry_time << '\n' ; + } csvFile.close(); std::cout << "CSV file written to " << filename << std::endl; } send_length = sizeof(DownloadConfigRes); DownloadConfigRes download_condfig_res; download_condfig_res.version = 1; - memcpy(download_condfig_res.filename,filename.c_str(),sizeof(download_condfig_res)); + memcpy(download_condfig_res.filename,filename.c_str(),sizeof(download_condfig_res.filename)); memcpy(send_data,(char*)&download_condfig_res,sizeof(DownloadConfigRes)); zlog_info(zct, "cmd 83 send_length = %d",send_length); } -void JsonData::CmtCmd_84(char* filename) +void JsonData::CmtCmd_84(char* filename,char* file_md5,char* send_data,int& send_length) { - std::vector vecDataNode; - std::ifstream csv_data(filename, std::ios::in); + std::vector vecDataNode; + UploadConfigRes upload_condfig_res; + upload_condfig_res.code = 0; + sprintf(upload_condfig_res.message ,"%s",""); + send_length = sizeof(UploadConfigRes); + char file_path[64]={0}; + char cmd[128]={0}; + sprintf(cmd, "mv /opt/%s /opt/DataNode/",filename); + system(cmd); + sprintf(file_path, "/opt/DataNode/%s",filename); + std::ifstream csv_data(file_path, std::ios::in); int iRet = 0; if (!csv_data.is_open()) { - zlog_error(zct, "UpdataDataNodeConfig fail to open:%s", filename); + zlog_error(zct, "UpdataDataNodeConfig fail to open:%s", file_path); } std::string line; std::vector words; @@ -341,25 +352,28 @@ void JsonData::CmtCmd_84(char* filename) while (getline(sin, word, ',')) { words.push_back(word); } - std::string mac = words[1]; + std::string mac = words[0]; if (mac != GlobalConfig::MacAddr_G) { iRet = -2; break; } dataNode.ZigbeeLongAddr = words[7]; - dataNode.FeatureInterVal = atoi(words[16].c_str()); - dataNode.WaveInterVal = atoi(words[17].c_str()); - dataNode.SamplingRate = atoi(words[18].c_str()); - dataNode.Range = atoi(words[19].c_str()); - dataNode.ACCSampleTime = atoi(words[20].c_str()); - dataNode.VIntegralFilterFrequency = atoi(words[21].c_str()); - dataNode.ZigbeePower = atoi(words[22].c_str()); - dataNode.ZigbeeRetry = atoi(words[23].c_str()); + dataNode.ZigbeeShortAddr = words[9]; + dataNode.SamplingRate = atoi(words[16].c_str()); + dataNode.Range = atoi(words[17].c_str()); + dataNode.ACCSampleTime = atoi(words[18].c_str()); + dataNode.VIntegralFilterFrequency = atoi(words[19].c_str()); + dataNode.ZigbeePower = atoi(words[20].c_str()); + dataNode.ZigbeeRetry = atoi(words[21].c_str()); vecDataNode.push_back(dataNode); } csv_data.close(); if (vecDataNode.size() == 0) { zlog_error(zct, "UpdataDataNodeConfig vecDataNode is 0"); + upload_condfig_res.code = 0; + sprintf(upload_condfig_res.message ,"%s",""); + memcpy(send_data,(char*)&upload_condfig_res,sizeof(UploadConfigRes)); + return ; } char whereCon[1024] = {0}; @@ -373,11 +387,17 @@ void JsonData::CmtCmd_84(char* filename) if (iRet != 0) { zlog_error(zct, "UpdataDataNodeConfig UpdateTableData fail"); } - + uint16_t short_addr; + char *end_ptr = NULL; + short_addr = strtol(vecDataNode[i].ZigbeeShortAddr.c_str(), &end_ptr, 16); + int res = scheduler::instance().UpdateSensorConfig(short_addr); + memset(whereCon, 0x00, sizeof(whereCon)); memset(updateSql, 0x00, sizeof(updateSql)); } - + + memcpy(send_data,(char*)&upload_condfig_res,sizeof(UploadConfigRes)); + zlog_info(zct, "cmd 84 send_length = %d",send_length); } void JsonData::CmtCmd_85(char* filename,char* file_md5,char* send_data,int& send_length) @@ -397,6 +417,7 @@ void JsonData::CmtCmd_86(char* recv_body,int& count,char* filename,char* file_m UpgradeSensorRes upgrade_sensor_res; upgrade_sensor_res.code = 0; sprintf(upgrade_sensor_res.message ,"%s",""); + send_length = sizeof(UpgradeSensorRes); char cmd[128]={0}; char file_path[64]={0}; sprintf(cmd, "mv /opt/%s /opt/DataNode/",filename); diff --git a/jsonparse/communication_cmd.hpp b/jsonparse/communication_cmd.hpp index 6cb0092..581f1bd 100644 --- a/jsonparse/communication_cmd.hpp +++ b/jsonparse/communication_cmd.hpp @@ -80,7 +80,7 @@ public: void CmtCmd_81(char* recv_body,int& count,char* send_data,int& return_length); void CmtCmd_82(char* MeasurementID,char* send_data,int& channel,int& return_length); void CmtCmd_83(char* recv_body,int& count,char* send_data,int& return_length); - void CmtCmd_84(char* filename); + void CmtCmd_84(char* filename,char* file_md5,char* send_data,int& send_length); void CmtCmd_85(char* filename,char* file_md5,char* send_data,int& send_length); void CmtCmd_86(char* recv_body,int& count,char* filename,char* file_md5,char* send_data,int& send_length); void CmtCmd_88(char* recv_body,int& count); diff --git a/localserver/cmt_cmd.cpp b/localserver/cmt_cmd.cpp index fd6b28e..e8b0929 100644 --- a/localserver/cmt_cmd.cpp +++ b/localserver/cmt_cmd.cpp @@ -24,7 +24,6 @@ void LocalServer::HandleTcp_cmd(const char* recv_data,char* send_data,uint8_t& r memcpy(search.mac,GlobalConfig::MacAddr_G.c_str(),sizeof(search.mac)); search.gw_type = kGWTDG102; send_length = sizeof(Search); - printf("send length = %d\n",send_length); memcpy(send_data,&search,sizeof(Search)); }break; case kGateWayVersion: { @@ -58,7 +57,6 @@ void LocalServer::HandleTcp_cmd(const char* recv_data,char* send_data,uint8_t& r memcpy(recv_body,recv_data + sizeof(PackageHead) + 4,recvbody_length - 4); } jd.CmtCmd_83(recv_body,count,send_data,send_length); - zlog_info(zct, "CmtCmd_83 = %d",send_length); if (recv_body){ free(recv_body); } @@ -66,7 +64,8 @@ void LocalServer::HandleTcp_cmd(const char* recv_data,char* send_data,uint8_t& r case KUploadConfig:{ UploadConfigReq upload_config; memcpy(&upload_config,recv_data + sizeof(PackageHead),sizeof(UploadConfigReq)); - jd.CmtCmd_84(upload_config.filename); + printf("filename = %s\n",upload_config.filename); + jd.CmtCmd_84(upload_config.filename,upload_config.md5,send_data,send_length); }break; case KUpgadeGateway:{ diff --git a/localserver/cmt_server.hpp b/localserver/cmt_server.hpp index 11bcfc7..0157076 100644 --- a/localserver/cmt_server.hpp +++ b/localserver/cmt_server.hpp @@ -114,7 +114,7 @@ enum Range{ }; struct DownloadConfigRes{ int version; - char filename[64]; + char filename[128]; }; struct DownloadConfig{ char gw_mac[20]; @@ -146,6 +146,10 @@ struct UploadConfigReq{ char filename[64]; char md5[33]; }; +struct UploadConfigRes{ + int code; + char message[64]; +}; //网关更新 cmd 85 struct UpgradeGwReq{ char filename[64];