WLG/jsonparse/cmt_parse.cpp

519 lines
24 KiB
C++
Raw Normal View History

2025-01-22 19:06:01 +08:00
#include <fstream>
#include "zlog.h"
2025-01-09 20:00:34 +08:00
#include "communication_cmd.hpp"
#include "localserver/local_server.hpp"
#include "common/common_func.hpp"
#include "common/global.hpp"
#include <boost/algorithm/string.hpp>
#include "dbaccess/sql_db.hpp"
#include "scheduler/schedule.hpp"
extern zlog_category_t *zct;
2025-01-13 20:11:40 +08:00
void JsonData::CmtCmd_80(char* send_data,int& send_length)
2025-01-09 20:00:34 +08:00
{
zlog_info(zct,"CmtCmd_80");
GatewayVersion gateway_ver;
2025-01-13 20:11:40 +08:00
gateway_ver.version = 1;
2025-01-09 20:00:34 +08:00
memcpy(gateway_ver.mac,GlobalConfig::MacAddr_G.c_str(),sizeof(gateway_ver.mac));
memcpy(gateway_ver.web_ver,ReadStrByOpt(SYSTEMINFOFILE, "Version", "WebVersion").c_str(),sizeof(gateway_ver.web_ver));
memcpy(gateway_ver.system_ver,ReadStrByOpt(SYSTEMINFOFILE, "Version", "SystemVersion").c_str(),sizeof(gateway_ver.system_ver));
memcpy(gateway_ver.gateway_ver,ReadStrByOpt(SYSTEMINFOFILE, "Version", "GateWayVersion").c_str(),sizeof(gateway_ver.gateway_ver));
2025-01-13 20:11:40 +08:00
memcpy(gateway_ver.ip,GlobalConfig::IpAddr_G.c_str(),sizeof(gateway_ver.ip));
2025-01-09 20:00:34 +08:00
memcpy(gateway_ver.gateway_type,ReadStrByOpt(SYSTEMINFOFILE, "Version", "GateWayProduct").c_str(),sizeof(gateway_ver.gateway_type));
2025-01-13 20:11:40 +08:00
memcpy(gateway_ver.gateway_hw_ver,ReadStrByOpt(SYSTEMINFOFILE, "Version", "GateWayHwVesion").c_str(),sizeof(gateway_ver.gateway_hw_ver));
2025-01-09 20:00:34 +08:00
#ifdef NR5G_MODULE
memcpy(gateway_ver.comm_mode,"5G",sizeof(gateway_ver.comm_mode));
#endif
#ifdef Q4G_MODULE
memcpy(gateway_ver.comm_mode,"4G",sizeof(gateway_ver.comm_mode));
#endif
#ifdef WIFI_MODULE
memcpy(gateway_ver.comm_mode,"WiFi",sizeof(gateway_ver.comm_mode));
#endif
memcpy(gateway_ver.comm_mode,"有线",sizeof(gateway_ver.comm_mode));
2025-01-22 17:13:45 +08:00
memcpy(gateway_ver.terminal_name,ReadStrByOpt(NETWORKCONFIG, "eth0", "hostName").c_str(),sizeof(gateway_ver.terminal_name));
2025-01-13 20:11:40 +08:00
GetSysStatusCMT(gateway_ver.cpu_use,gateway_ver.memory_use,gateway_ver.disk_remain,gateway_ver.temperature);
memcpy(send_data,&gateway_ver,sizeof(GatewayVersion));
send_length = sizeof(GatewayVersion);
2025-01-09 20:00:34 +08:00
}
2025-01-22 17:13:45 +08:00
void JsonData::CmtCmd_81(char* recv_body,int& count,char* send_data,int& send_length)
2025-01-09 20:00:34 +08:00
{
int featureInterVal;
int featureInterTime;
int waveInterVal;
int waveInterTime;
int maxSensorNum;
int sensorCount;
array_t arrRes;
2025-01-22 17:13:45 +08:00
char whereCon[512]={0};
if (recv_body != NULL)
{
printf("count = %d\n",count);
char short_addr_[256]={0};
for (size_t i = 0; i < count; i++){
char temp[5]={0};
memcpy(temp,recv_body + i * 4,4);
printf("short_addr = %s\n",temp);
strcat(short_addr_,"'");
strcat(short_addr_,temp);
strcat(short_addr_,"'");
if (i + 1 != count){
strcat(short_addr_ ,",");
}
}
sprintf(whereCon,"zigbeeShortAddr IN (%s)",short_addr_);
arrRes = sqlite_db_ctrl::instance().GetDataMultiLineTransaction(T_SENSOR_INFO(TNAME), "*", whereCon);
}else{
arrRes = sqlite_db_ctrl::instance().GetDataMultiLineTransaction(T_SENSOR_INFO(TNAME), "*", NULL);
}
2025-01-09 20:00:34 +08:00
int iResult = arrRes.size();
2025-01-22 17:13:45 +08:00
printf("result = %d\n",iResult);
2025-01-09 20:00:34 +08:00
size_t j = 0;
if (iResult > 0) {
SensorInfo sensor_info[iResult];
for (; j < iResult; j++)
{
2025-01-22 17:13:45 +08:00
memcpy(sensor_info[j].sensor_name,arrRes[j][1].c_str(),sizeof(sensor_info[j].sensor_name));
2025-01-13 20:11:40 +08:00
memcpy(sensor_info[j].measurement_id,arrRes[j][44].c_str(),sizeof(sensor_info[j].measurement_id));
2025-01-22 17:13:45 +08:00
memcpy(sensor_info[j].short_addr , arrRes[j][30].c_str(),sizeof(sensor_info[j].short_addr));
2025-01-09 20:00:34 +08:00
memcpy(sensor_info[j].hw_ver,arrRes[j][8].c_str(),sizeof(sensor_info[j].hw_ver));
memcpy(sensor_info[j].soft_ver,arrRes[j][9].c_str(),sizeof(sensor_info[j].soft_ver));
std::vector<std::string> vParamRSSI;
boost::split(vParamRSSI, arrRes[j][40], boost::is_any_of(","), boost::token_compress_on);
if (vParamRSSI.size() > 1) {
2025-01-13 20:11:40 +08:00
sensor_info[j].gateway_rssi = (atof(vParamRSSI[0].c_str())/255) * 100;
sensor_info[j].sensor_rssi = (atof(vParamRSSI[1].c_str())/255) * 100;
2025-01-09 20:00:34 +08:00
} else {
2025-01-13 20:11:40 +08:00
sensor_info[j].gateway_rssi = (atof(vParamRSSI[0].c_str())/255) * 100;
sensor_info[j].sensor_rssi = 99;
2025-01-09 20:00:34 +08:00
}
std::vector<std::string> vParambattery;
boost::split(vParambattery, arrRes[j][43], boost::is_any_of(","), boost::token_compress_on);
if (vParambattery.size() > 1) {
2025-01-13 20:11:40 +08:00
sensor_info[j].battry = (atof(vParambattery[1].c_str())/atof(vParambattery[0].c_str())) * 100;
2025-01-09 20:00:34 +08:00
} else {
2025-01-13 20:11:40 +08:00
sensor_info[j].battry = 99;
2025-01-09 20:00:34 +08:00
}
std::vector<std::string> vParam;
boost::split(vParam, arrRes[j][42], boost::is_any_of(","), boost::token_compress_on);
if (vParam.size() > 1){
if (vParam[1] != "0"){
sensor_info[j].loose_status = 1;
}else{
sensor_info[j].loose_status = 0;
}
}
char whereCon[256]={0x00};
sprintf(whereCon,"dataNodeNo = '%s'",arrRes[j][44].c_str());
vec_t vecRes = sqlite_db_ctrl::instance().GetDataSingleLine(T_DATASTATIC_INFO(TNAME), "temTop,temBot",whereCon);
if (vecRes.size() > 0)
{
sensor_info[j].temperature_top = atoi(vecRes[0].c_str());
sensor_info[j].temperature_bot = atoi(vecRes[1].c_str());
}else{
sensor_info[j].temperature_top = 200;
sensor_info[j].temperature_bot = 200;
}
2025-01-13 20:11:40 +08:00
memcpy(sensor_info[j].product,arrRes[j][17].c_str(),sizeof(sensor_info[j].product));
if (arrRes[j][17] == "01"){
memcpy(sensor_info[j].product,"DN101",sizeof(sensor_info[j].product));
}else if (arrRes[j][17] == "02"){
memcpy(sensor_info[j].product,"DN102",sizeof(sensor_info[j].product));
}
2025-01-09 20:00:34 +08:00
sensor_info[j].status = atoi(arrRes[j][37].c_str());
char szTableName[100] = {0x00};
memset(whereCon,0,sizeof(whereCon));
sprintf(szTableName, " t_data_waveSend ");
const char *sql =
" timestamp >= strftime('%s', 'now', '-1 day', 'start of day','utc') "
"AND timestamp < strftime('%s', 'now', '-1 day','start of day','utc','+24 hours') ";
sprintf(whereCon," and channelID = '%s-X'",arrRes[j][44].c_str());
std::string strsql = std::string(sql) + std::string(whereCon);
int waveX_Count = sqlite_db_ctrl::instance().GetTableRows(szTableName,strsql.c_str());
memset(whereCon,0,sizeof(whereCon));
sprintf(whereCon," and channelID = '%s-Y'",arrRes[j][44].c_str());
strsql = std::string(sql) + std::string(whereCon);
int waveY_Count = sqlite_db_ctrl::instance().GetTableRows(szTableName,strsql.c_str());
memset(whereCon,0,sizeof(whereCon));
sprintf(whereCon," and channelID = '%s-Z'",arrRes[j][44].c_str());
strsql = std::string(sql) + std::string(whereCon);
int waveZ_Count = sqlite_db_ctrl::instance().GetTableRows(szTableName,strsql.c_str());
memset(whereCon,0,sizeof(whereCon));
memset(szTableName,0,sizeof(szTableName));
sprintf(szTableName, " t_dataStatic_%s ",arrRes[j][44].c_str());
sprintf(whereCon," and channelID = '%s-S'",arrRes[j][44].c_str());
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);
2025-01-22 17:13:45 +08:00
zlog_info(zct,"wavex = %d,featureInterVal = %d,waveInterVal = %d",waveX_Count,featureInterVal,waveInterVal);
int day_count = 86400 / waveInterVal;
sensor_info[j].wave_x_reporting_rate = float( waveX_Count/ day_count) * 100;
printf("day_count = %d,wave_x_reporting_rate = %d\n",day_count,sensor_info[j].wave_x_reporting_rate );
sensor_info[j].wave_y_reporting_rate = (float(waveY_Count)/(86400/waveInterVal)) * 100;
sensor_info[j].wave_z_reporting_rate = (float(waveZ_Count)/(86400/waveInterVal)) * 100;
sensor_info[j].eigen_value_reporting_rate = (float(static_Count)/(86400/featureInterVal)) * 100;
day_count = 86400 / featureInterVal;
printf("static_Count = %d,day_count = %d,wave_x_reporting_rate = %d\n",static_Count,day_count,sensor_info[j].eigen_value_reporting_rate );
2025-01-09 20:00:34 +08:00
memset(whereCon,0,sizeof(whereCon));
sprintf(whereCon,"channelID = '%s-Z'",arrRes[j][44].c_str());
std::string integratRMS = sqlite_db_ctrl::instance().GetData(T_DATA_INFO(TNAME), "integratRMS",whereCon);
2025-01-22 17:13:45 +08:00
sensor_info[j].velocity_rms = atof(integratRMS.c_str());
sensor_info[j].upgrade_status = atoi(arrRes[j][46].c_str());
memset(whereCon,0,sizeof(whereCon));
sprintf(whereCon,"short_addr = '%s' ORDER BY start_timestamp DESC limit 0,1",arrRes[j][30].c_str());
std::string upgrade_time = sqlite_db_ctrl::instance().GetData("firmware_upgrade", " start_timestamp ", whereCon);
memcpy(sensor_info[j].upgrade_time,upgrade_time.c_str(),sizeof(sensor_info[j].upgrade_time));
2025-01-13 20:11:40 +08:00
sensor_info[j].version = 1;
2025-01-09 20:00:34 +08:00
}
2025-01-13 20:11:40 +08:00
memcpy(send_data,&sensor_info,sizeof(SensorInfo) * j);
2025-01-22 17:13:45 +08:00
send_length = sizeof(SensorInfo) * j;
2025-01-13 20:11:40 +08:00
zlog_info(zct,"sizeof(SensorInfo) = %d",sizeof(SensorInfo)*j);
zlog_info(zct,"send_length = %d",send_length);
2025-01-09 20:00:34 +08:00
}
}
2025-01-13 20:11:40 +08:00
void JsonData::CmtCmd_82(char* MeasurementID,char* send_data,int& channel,int& send_length)
2025-01-09 20:00:34 +08:00
{
FILE* pFile = NULL;
char* buffer = NULL;
int thisSize = 0;
WaveRes wave;
2025-01-14 14:27:20 +08:00
char whereCon[64]={0};
2025-01-09 20:00:34 +08:00
sprintf(whereCon,"MeasurementID = '%s'",MeasurementID);
vec_t vecRes = sqlite_db_ctrl::instance().GetDataSingleLine(T_SENSOR_INFO(TNAME), "samplingRate,ACCSampleTime",whereCon);
2025-01-13 20:11:40 +08:00
std::string strChannel = "";
if (channel == 1){
strChannel = "X";
}else if(channel == 2){
strChannel = "Y";
}else if (channel == 3){
strChannel = "Z";
}
2025-01-14 14:27:20 +08:00
std::string data_file = "/opt/data/" + std::string(MeasurementID) + "-" + strChannel + ".dat";
2025-01-09 20:00:34 +08:00
zlog_info(zct, "strFileName = %s", data_file.c_str());
2025-01-14 14:27:20 +08:00
2025-01-09 20:00:34 +08:00
pFile = fopen(data_file.c_str(), "rb");
if (pFile != NULL) {
while (fgetc(pFile) != EOF) {
++thisSize;
}
rewind(pFile);
2025-01-14 14:27:20 +08:00
fseek(pFile, 32, SEEK_SET);//跳过32个字节的时间戳
buffer = (char*)malloc(thisSize - 32);
fread(buffer, sizeof(char), thisSize - 32, pFile);
2025-01-09 20:00:34 +08:00
fclose(pFile);
}
2025-01-14 14:27:20 +08:00
wave.sampling_rate = atoi(vecRes[0].c_str());
2025-01-09 20:00:34 +08:00
wave.sampling_time = atoi(vecRes[1].c_str());
2025-01-13 20:11:40 +08:00
wave.version = 1;
2025-01-14 14:27:20 +08:00
send_length = sizeof(WaveRes) + thisSize - 32;
2025-01-13 20:11:40 +08:00
memcpy(send_data,(char*)&wave,sizeof(WaveRes));
2025-01-14 14:27:20 +08:00
memcpy(send_data + sizeof(WaveRes) ,buffer,thisSize - 32);
2025-01-09 20:00:34 +08:00
free(buffer);
2025-01-13 20:11:40 +08:00
}
2025-01-22 17:13:45 +08:00
void JsonData::CmtCmd_83(char* recv_body,int& count,char* send_data,int& send_length)
2025-01-13 20:11:40 +08:00
{
vec_t vecRes;
std::string filename = "";
char whereCon[128]={0};
2025-01-22 17:13:45 +08:00
if (recv_body){
printf("count = %d\n",count);
char MeasurementID_[256]={0};
for (size_t i = 0; i < count; i++){
2025-01-22 17:53:38 +08:00
char temp[21]={0};
2025-01-22 17:13:45 +08:00
memcpy(temp,recv_body + i * 20,20);
printf("short_addr = %s\n",temp);
strcat(MeasurementID_,"'");
strcat(MeasurementID_,temp);
strcat(MeasurementID_,"'");
if (i + 1 != count){
strcat(MeasurementID_ ,",");
}
}
sprintf(whereCon,"MeasurementID IN (%s)",MeasurementID_);
vecRes = sqlite_db_ctrl::instance().GetDataSingleLine(T_SENSOR_INFO(TNAME), "*", whereCon);
}else{
vecRes = sqlite_db_ctrl::instance().GetDataSingleLine(T_SENSOR_INFO(TNAME), "*", NULL);
}
2025-01-13 20:11:40 +08:00
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<std::string> 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());
char localtimestamp[32] = {0};
GetTimeNet(localtimestamp, 1);
2025-01-14 14:27:20 +08:00
filename = "/opt/DataNode/config_" + std::string(localtimestamp)+ ".csv";
2025-01-13 20:11:40 +08:00
std::ofstream csvFile(filename);
if (!csvFile.is_open()) {
std::cerr << "Error: Could not open file " << filename << std::endl;
return;
}
// 写入 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 ;
csvFile.close();
std::cout << "CSV file written to " << filename << std::endl;
}
send_length = sizeof(DownloadConfigRes);
DownloadConfigRes download_condfig_res;
2025-01-14 14:27:20 +08:00
download_condfig_res.version = 1;
2025-01-13 20:11:40 +08:00
memcpy(download_condfig_res.filename,filename.c_str(),sizeof(download_condfig_res));
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)
{
std::vector<DataNodeInfo> vecDataNode;
std::ifstream csv_data(filename, std::ios::in);
int iRet = 0;
if (!csv_data.is_open()) {
zlog_error(zct, "UpdataDataNodeConfig fail to open:%s", filename);
}
std::string line;
std::vector<std::string> words;
std::string word;
DataNodeInfo dataNode;
getline(csv_data, line);
std::istringstream sin;
while (getline(csv_data, line)) {
words.clear();
sin.clear();
sin.str(line);
while (getline(sin, word, ',')) {
words.push_back(word);
}
std::string mac = words[1];
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());
vecDataNode.push_back(dataNode);
}
csv_data.close();
if (vecDataNode.size() == 0) {
zlog_error(zct, "UpdataDataNodeConfig vecDataNode is 0");
}
char whereCon[1024] = {0};
char updateSql[1024] = {0};
for (size_t i = 0; i < vecDataNode.size(); i++) {
sprintf(updateSql, "featureInterVal='%d',waveInterVal='%d',range='%d',samplingRate='%d',AccSampleTime = '%d',viff ='%d' ,ZigbeePower = '%d',ZigbeeRetry = '%d',UpdateFlag = 0", vecDataNode[i].FeatureInterVal, vecDataNode[i].WaveInterVal, vecDataNode[i].Range,
vecDataNode[i].SamplingRate, vecDataNode[i].ACCSampleTime, vecDataNode[i].VIntegralFilterFrequency, vecDataNode[i].ZigbeePower, vecDataNode[i].ZigbeeRetry);
sprintf(whereCon, "dataNodeNo='%s'", vecDataNode[i].ZigbeeLongAddr.c_str());
iRet = sqlite_db_ctrl::instance().UpdateTableData(T_SENSOR_INFO(TNAME), updateSql, whereCon);
if (iRet != 0) {
zlog_error(zct, "UpdataDataNodeConfig UpdateTableData fail");
}
2025-01-14 14:27:20 +08:00
2025-01-13 20:11:40 +08:00
memset(whereCon, 0x00, sizeof(whereCon));
memset(updateSql, 0x00, sizeof(updateSql));
}
}
2025-01-22 17:13:45 +08:00
void JsonData::CmtCmd_85(char* filename,char* file_md5,char* send_data,int& send_length)
2025-01-13 20:11:40 +08:00
{
2025-01-22 17:13:45 +08:00
zlog_info(zct,"file_path = %s,file_md5 = %d",filename,file_md5);
sleep(3);
int iRet = system("/opt/opt.sh");
zlog_info(zct, "iRet = %d", iRet);
if (iRet == -1) {
zlog_error(zct, "system() error");
}
}
2025-01-13 20:11:40 +08:00
2025-01-22 17:13:45 +08:00
void JsonData::CmtCmd_86(char* recv_body,int& count,char* filename,char* file_md5,char* send_data,int& send_length)
{
zlog_info(zct,"file_path = %s,file_md5 = %d",filename,file_md5);
UpgradeSensorRes upgrade_sensor_res;
upgrade_sensor_res.code = 0;
sprintf(upgrade_sensor_res.message ,"%s","");
char cmd[128]={0};
char file_path[64]={0};
sprintf(cmd, "mv /opt/%s /opt/DataNode/",filename);
system(cmd);
sprintf(file_path, "/opt/DataNode/%s",filename);
FILE * pFile=NULL;
size_t thisSize = 0;
char *buffer=NULL;
pFile = fopen (file_path,"rb");
if (pFile==NULL) {
zlog_error(zct,"Error opening file");
}
else
{
while (fgetc(pFile) != EOF) {
++thisSize;
}
rewind(pFile);
buffer = (char*)malloc(thisSize);
fread (buffer, sizeof (char), thisSize, pFile);
fclose (pFile);
}
zlog_info(zct,"Read %zu bytes", thisSize);
char sensor_type[6] = {0};
char sf_version[10] = {0};
memcpy(sensor_type, buffer, 5);
zlog_info(zct,"model:%s", sensor_type);
char c[2] = {0};
c[0] = buffer[5];
uint8_t hw_ver = atoi(c);
c[0] = buffer[6];
uint8_t sf_ver_m = atoi(c);
c[0] = buffer[7];
uint8_t sf_ver_s = atoi(c);
sprintf(sf_version,"%d.%d",sf_ver_m,sf_ver_s);
unsigned char ch_crc = 0x00;
int packgeSize = 0;
ch_crc = buffer[12];
packgeSize = BUILD_UINT32(buffer[8],buffer[9],buffer[10],buffer[11]);
zlog_info(zct,"sf_ver_m = %d",sf_ver_m);
zlog_info(zct,"sf_ver_s = %d",sf_ver_s);
zlog_info(zct,"hw_ver = %d",hw_ver);
zlog_info(zct,"sensor_type = %s",sensor_type);
zlog_info(zct,"packgeSize = %d",packgeSize);
zlog_info(zct,"ch_crc = %02x",ch_crc);
unsigned char sum = 0x00;
for(size_t i = 13; i < thisSize;i++){
sum += buffer[i];
}
if (sum % 256 != ch_crc){
zlog_error(zct,"package CRC error,filename = %s",file_path);
upgrade_sensor_res.code = 1;
sprintf(upgrade_sensor_res.message ,"%s","package CRC error");
memcpy(send_data,(char*)&upgrade_sensor_res,sizeof(DownloadConfigRes));
return;
}
zlog_info(zct,"sum = %x\n",sum % 256);
char localtimestamp[32] = {0};
GetTimeNet(localtimestamp, 1);
for (size_t i = 0; i < count; i++)
{
char wherecon[100] = {0};
char insertSql[200] = {0};
char updateSql[100] = {0};
char short_addr_[21] = {0};
memcpy(short_addr_,recv_body + i * 4,4);
sprintf(wherecon," zigbeeShortAddr = '%s' ",short_addr_);
vec_t vecResult = sqlite_db_ctrl::instance().GetDataSingleLine(T_SENSOR_INFO(TNAME), " hardVersion,softVersion,ProductNo,zigbeeShortAddr ", wherecon);
if (hw_ver != atoi(vecResult[0].c_str())){
zlog_error(zct,"hardVersion error,filename = %s",file_path);
upgrade_sensor_res.code = 2;
sprintf(upgrade_sensor_res.message ,"%s","hardVersion error");
memcpy(send_data,(char*)&upgrade_sensor_res,sizeof(DownloadConfigRes));
return;
}
sprintf(insertSql, " '%s','%s','','','','','%d.%d','%s',1,'%s'",vecResult[3].c_str(),localtimestamp,sf_ver_m,sf_ver_s,vecResult[1].c_str(),filename);
sqlite_db_ctrl::instance().InsertData(" firmware_upgrade ", insertSql);
//0 默认状态1 升级中2 升级成功,3 升级失败
memset(wherecon,0,sizeof(wherecon));
memset(updateSql,0,sizeof(updateSql));
sprintf(wherecon," zigbeeShortAddr = '%s'",vecResult[3].c_str());
sprintf(updateSql, " upgradeStatus = %d ", 1);
sqlite_db_ctrl::instance().UpdateTableData(T_SENSOR_INFO(TNAME), updateSql,wherecon);
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));
}
free(buffer);
2025-01-14 14:27:20 +08:00
}
2025-01-22 17:13:45 +08:00
void JsonData::CmtCmd_88(char* recv_body,int& count)
2025-01-14 14:27:20 +08:00
{
2025-01-22 17:13:45 +08:00
}
std::string get_file_md5(const std::string& file_path) {
// 使用 md5sum 命令计算文件的 MD5 值
std::string command = "md5sum " + file_path + " | awk '{ print $1 }'";
// 使用 popen 执行命令并获取输出
std::array<char, 128> buffer;
std::string result;
std::shared_ptr<FILE> pipe(popen(command.c_str(), "r"), pclose);
if (!pipe) {
std::cerr << "Error: Unable to run md5sum command" << std::endl;
return "";
}
// 从命令的输出流中读取数据
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
result += buffer.data();
}
// 去除末尾的换行符
result.erase(result.find_last_not_of("\n") + 1);
return result;
2025-01-22 19:06:01 +08:00
}