WLG/jsonparse/cmt_parse.cpp
2025-01-09 20:00:34 +08:00

182 lines
8.8 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"
extern zlog_category_t *zct;
char *JsonData::CmtCmd_80(int& return_length)
{
zlog_info(zct,"CmtCmd_80");
GatewayVersion gateway_ver;
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.localIP,GlobalConfig::IpAddr_G.c_str(),sizeof(gateway_ver.localIP));
memcpy(gateway_ver.gateway_type,ReadStrByOpt(SYSTEMINFOFILE, "Version", "GateWayProduct").c_str(),sizeof(gateway_ver.gateway_type));
memcpy(gateway_ver.geteway_hw_ver,ReadStrByOpt(SYSTEMINFOFILE, "Version", "GateWayHwVesion").c_str(),sizeof(gateway_ver.geteway_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.harddisk_remain,gateway_ver.temperature);
char *retData = NULL;
retData = (char*)malloc(sizeof(GatewayVersion));
memcpy(retData,&gateway_ver,sizeof(GatewayVersion));
return_length = sizeof(GatewayVersion);
return retData;
}
char *JsonData::CmtCmd_81(int& return_length)
{
int featureInterVal;
int featureInterTime;
int waveInterVal;
int waveInterTime;
int maxSensorNum;
int sensorCount;
char *retData = NULL;
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].mac,arrRes[j][44].c_str(),sizeof(sensor_info[j].mac));
memcpy(sensor_info[j].short_addr,arrRes[j][30].c_str(),sizeof(sensor_info[j].short_addr));
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) {
memcpy(sensor_info[j].sensor_rssi,vParamRSSI[0].c_str(),sizeof(sensor_info[j].sensor_rssi));
memcpy(sensor_info[j].gateway_rssi,vParamRSSI[1].c_str(),sizeof(sensor_info[j].gateway_rssi));
} else {
memcpy(sensor_info[j].sensor_rssi,vParamRSSI[0].c_str(),sizeof(sensor_info[j].sensor_rssi));
memcpy(sensor_info[j].gateway_rssi,"80",sizeof(sensor_info[j].gateway_rssi));
}
std::vector<std::string> vParambattery;
boost::split(vParambattery, arrRes[j][43], boost::is_any_of(","), boost::token_compress_on);
if (vParambattery.size() > 1) {
float battery = float(atof(vParambattery[1].c_str())/atof(vParambattery[0].c_str()));
sprintf(sensor_info[j].battry,"%f",battery);
} else {
sprintf(sensor_info[j].battry ,"0.99",sizeof(sensor_info[j].battry));
}
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_no,arrRes[j][17].c_str(),sizeof(sensor_info[j].product_no));
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);
sensor_info[j].waveX_reportingrate = waveX_Count/(86400/waveInterVal);
sensor_info[j].waveY_reportingrate = waveY_Count/(86400/waveInterVal);
sensor_info[j].waveZ_reportingrate = waveZ_Count/(86400/waveInterVal);
sensor_info[j].eigenvalue_reportingrate = static_Count/(86400/featureInterVal);
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].integratRMS , integratRMS.c_str(),sizeof(sensor_info[j].integratRMS));;
sensor_info[j].upgrade_status = 0;
memcpy(sensor_info[j].upgrade_date,"2024-01-08 12:00:00",sizeof(sensor_info[j].upgrade_date));
}
retData = (char*)malloc(sizeof(SensorInfo) * j);
memcpy(retData,&sensor_info,sizeof(SensorInfo) * j);
return_length = sizeof(SensorInfo) * j;
}
return retData;
}
char *JsonData::CmtCmd_82(char* MeasurementID,char* channel,int& return_length)
{
FILE* pFile = NULL;
char* buffer = NULL;
char* retData = 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 data_file = "/opt/data/" + std::string(MeasurementID) + "-" + std::string(channel);
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);
}
wave.sampling_rate = atoi(vecRes[0].c_str());
wave.sampling_time = atoi(vecRes[1].c_str());
return_length = sizeof(WaveRes) + thisSize;
retData = (char*)malloc(return_length);
memcpy(retData,(char*)&wave,sizeof(WaveRes));
memcpy(retData + sizeof(WaveRes) ,&buffer,thisSize);
free(buffer);
return retData;
}