346 lines
17 KiB
C++
346 lines
17 KiB
C++
#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"
|
|
#include "zlog.h"
|
|
#include <fstream>
|
|
|
|
extern zlog_category_t *zct;
|
|
|
|
void JsonData::CmtCmd_80(char* send_data,int& send_length)
|
|
{
|
|
zlog_info(zct,"CmtCmd_80");
|
|
GatewayVersion gateway_ver;
|
|
gateway_ver.version = 1;
|
|
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));
|
|
memcpy(gateway_ver.ip,GlobalConfig::IpAddr_G.c_str(),sizeof(gateway_ver.ip));
|
|
memcpy(gateway_ver.gateway_type,ReadStrByOpt(SYSTEMINFOFILE, "Version", "GateWayProduct").c_str(),sizeof(gateway_ver.gateway_type));
|
|
memcpy(gateway_ver.gateway_hw_ver,ReadStrByOpt(SYSTEMINFOFILE, "Version", "GateWayHwVesion").c_str(),sizeof(gateway_ver.gateway_hw_ver));
|
|
#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));
|
|
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);
|
|
}
|
|
|
|
void JsonData::CmtCmd_81(char* send_data,int& send_length)
|
|
{
|
|
int featureInterVal;
|
|
int featureInterTime;
|
|
int waveInterVal;
|
|
int waveInterTime;
|
|
int maxSensorNum;
|
|
int sensorCount;
|
|
array_t arrRes;
|
|
arrRes = sqlite_db_ctrl::instance().GetDataMultiLineTransaction(T_SENSOR_INFO(TNAME), "*", NULL);
|
|
int iResult = arrRes.size();
|
|
size_t j = 0;
|
|
if (iResult > 0) {
|
|
SensorInfo sensor_info[iResult];
|
|
for (; j < iResult; j++)
|
|
{
|
|
memcpy(sensor_info[j].measurement_id,arrRes[j][44].c_str(),sizeof(sensor_info[j].measurement_id));
|
|
sensor_info[j].short_addr = atoi(arrRes[j][30].c_str());
|
|
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) {
|
|
sensor_info[j].gateway_rssi = (atof(vParamRSSI[0].c_str())/255) * 100;
|
|
sensor_info[j].sensor_rssi = (atof(vParamRSSI[1].c_str())/255) * 100;
|
|
} else {
|
|
sensor_info[j].gateway_rssi = (atof(vParamRSSI[0].c_str())/255) * 100;
|
|
sensor_info[j].sensor_rssi = 99;
|
|
}
|
|
std::vector<std::string> vParambattery;
|
|
boost::split(vParambattery, arrRes[j][43], boost::is_any_of(","), boost::token_compress_on);
|
|
if (vParambattery.size() > 1) {
|
|
sensor_info[j].battry = (atof(vParambattery[1].c_str())/atof(vParambattery[0].c_str())) * 100;
|
|
} else {
|
|
sensor_info[j].battry = 99;
|
|
}
|
|
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;
|
|
}
|
|
|
|
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));
|
|
}
|
|
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);
|
|
zlog_info(zct,"wavex = %d,featureInterVal = %d,waveInterVal",waveX_Count,featureInterVal);
|
|
sensor_info[j].wave_x_reporting_rate = (float(waveX_Count)/(86400/(float)waveInterVal)) * 100;
|
|
sensor_info[j].wave_y_reporting_rate = (float(waveY_Count)/(86400/(float)waveInterVal)) * 100;
|
|
sensor_info[j].wave_z_reporting_rate = (float(waveZ_Count)/(86400/(float)waveInterVal)) * 100;
|
|
sensor_info[j].eigen_value_reporting_rate = (float(static_Count)/(86400/(float)featureInterVal)) *100;
|
|
|
|
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);
|
|
|
|
memcpy(sensor_info[j].velocity_rms , integratRMS.c_str(),sizeof(sensor_info[j].velocity_rms));;
|
|
sensor_info[j].upgrade_status = 0;
|
|
memcpy(sensor_info[j].upgrade_time,"2024-01-08 12:00:00",sizeof(sensor_info[j].upgrade_time));
|
|
sensor_info[j].version = 1;
|
|
}
|
|
memcpy(send_data,&sensor_info,sizeof(SensorInfo) * j);
|
|
memcpy(send_data + sizeof(SensorInfo) * j,&sensor_info,sizeof(SensorInfo) * j);
|
|
memcpy(send_data + sizeof(SensorInfo) * j * 2,&sensor_info,sizeof(SensorInfo) * j);
|
|
memcpy(send_data + sizeof(SensorInfo) * j * 3,&sensor_info,sizeof(SensorInfo) * j);
|
|
memcpy(send_data + sizeof(SensorInfo) * j * 4,&sensor_info,sizeof(SensorInfo) * j);
|
|
send_length = sizeof(SensorInfo) * j * 5;
|
|
zlog_info(zct,"sizeof(SensorInfo) = %d",sizeof(SensorInfo)*j);
|
|
zlog_info(zct,"send_length = %d",send_length);
|
|
}
|
|
|
|
}
|
|
|
|
void JsonData::CmtCmd_82(char* MeasurementID,char* send_data,int& channel,int& send_length)
|
|
{
|
|
FILE* pFile = NULL;
|
|
char* buffer = NULL;
|
|
int thisSize = 0;
|
|
WaveRes wave;
|
|
char whereCon[32]={0};
|
|
sprintf(whereCon,"MeasurementID = '%s'",MeasurementID);
|
|
vec_t vecRes = sqlite_db_ctrl::instance().GetDataSingleLine(T_SENSOR_INFO(TNAME), "samplingRate,ACCSampleTime",whereCon);
|
|
std::string strChannel = "";
|
|
|
|
if (channel == 1){
|
|
strChannel = "X";
|
|
}else if(channel == 2){
|
|
strChannel = "Y";
|
|
}else if (channel == 3){
|
|
strChannel = "Z";
|
|
}
|
|
std::string data_file = "/opt/data/" + std::string(MeasurementID) + "-" + strChannel;
|
|
zlog_info(zct, "strFileName = %s", data_file.c_str());
|
|
pFile = fopen(data_file.c_str(), "rb");
|
|
if (pFile != NULL) {
|
|
while (fgetc(pFile) != EOF) {
|
|
++thisSize;
|
|
}
|
|
rewind(pFile);
|
|
buffer = (char*)malloc(thisSize);
|
|
fread(buffer, sizeof(char), thisSize, pFile);
|
|
fclose(pFile);
|
|
}
|
|
char test_buf[12800 * 4 + 1]={0};
|
|
for (int i = 0; i < 12800; i++)
|
|
{
|
|
char temp[5]={0};
|
|
sprintf(temp,"%d",i);
|
|
memcpy(test_buf + i * 4, temp, 4);
|
|
}
|
|
|
|
//wave.sampling_rate = atoi(vecRes[0].c_str());
|
|
wave.sampling_rate = 12800;
|
|
wave.sampling_time = atoi(vecRes[1].c_str());
|
|
wave.version = 1;
|
|
send_length = sizeof(WaveRes) + 51200;
|
|
memcpy(send_data,(char*)&wave,sizeof(WaveRes));
|
|
memcpy(send_data + sizeof(WaveRes) ,test_buf,51200);
|
|
free(buffer);
|
|
}
|
|
|
|
void JsonData::CmtCmd_83(char* MeasurementID,char* send_data,int& send_length)
|
|
{
|
|
vec_t vecRes;
|
|
std::string filename = "";
|
|
char whereCon[128]={0};
|
|
sprintf(whereCon,"MeasurementID = '%s'",MeasurementID);
|
|
vecRes = sqlite_db_ctrl::instance().GetDataSingleLine(T_SENSOR_INFO(TNAME), "*", whereCon);
|
|
if (vecRes.size() > 0){
|
|
DownloadConfig download_config;
|
|
download_config.version = 1;
|
|
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);
|
|
filename = "config_" + std::string(localtimestamp);
|
|
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;
|
|
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");
|
|
}
|
|
memset(whereCon, 0x00, sizeof(whereCon));
|
|
memset(updateSql, 0x00, sizeof(updateSql));
|
|
}
|
|
|
|
}
|
|
|
|
void JsonData::CmtCmd_85(char* filename)
|
|
{
|
|
|
|
} |