WLG/jsonparse/web_cmd_parse2.cpp

875 lines
35 KiB
C++
Raw Normal View History

2024-10-22 19:04:25 +08:00
#include "communication_cmd.hpp"
2024-10-22 20:56:21 +08:00
#include "dbaccess/sql_db.hpp"
#include "platform/platform_init.hpp"
#include "MD5/md5.h"
2024-10-22 19:04:25 +08:00
std::string JsonData::JsonCmd_Cgi_26(Param_26 &param) {
// LOG_INFO("26 start\n");
Json::Value jsonVal;
jsonVal.clear();
jsonVal["cmd"] = "26";
jsonVal["dataNodeGatewayNo"] = GlobalConfig::MacAddr_G;
jsonVal["success"] = true;
jsonVal["message"] = " ";
char looseValue[10] = {0x00};
readStringValue("config", "loose", looseValue, (char *)GlobalConfig::Config_G.c_str());
// print_debug("loose = %f \n",atof(looseValue));
Json::Value jsArray;
array_t arrRes;
2024-10-22 20:56:21 +08:00
arrRes = sqlite_db_ctrl::instance().GetDataMultiLineTransaction(T_SENSOR_INFO(TNAME), "*", NULL);
2024-10-22 19:04:25 +08:00
int iResult = arrRes.size();
if (iResult > 0) {
int packgeNo = param.mPackageFlag;
int packgeMax = 0;
int packgeNum = 0;
jsonVal["package"] = packgeNo;
int lastSize = iResult % 10;
int index = iResult / 10;
if (lastSize > 0 && index > 0) {
packgeMax = index + 1;
if (packgeNo + 1 == packgeMax) {
packgeNum = iResult;
jsonVal["packageMax"] = index + 1;
} else {
packgeNum = (packgeNo + 1) * 10;
jsonVal["packageMax"] = index + 1;
}
} else if (lastSize == 0 && index > 0) {
packgeNum = (packgeNo + 1) * 10;
packgeMax = index;
jsonVal["packageMax"] = index;
} else if (lastSize > 0 && index == 0) {
packgeNum = lastSize;
packgeMax = index + 1;
jsonVal["packageMax"] = index;
}
printf("26 packgeNo = %d,packgeNum = %d,lastSize = %d,index = %d\n", packgeNo, packgeNum, lastSize, index);
for (int j = packgeNo * 10; j < packgeNum; j++) {
Json::Value jsSensorData;
jsSensorData["dataNodeNo"] = arrRes[j][0];
jsSensorData["dataNodeName"] = arrRes[j][1];
jsSensorData["initFlag"] = atoi(arrRes[j][2].c_str());
jsSensorData["accFlag"] = atoi(arrRes[j][3].c_str());
jsSensorData["zigbeeFlag"] = atoi(arrRes[j][4].c_str());
jsSensorData["temTopFlag"] = atoi(arrRes[j][5].c_str());
jsSensorData["temBotFlag"] = atoi(arrRes[j][6].c_str());
jsSensorData["hardVersion"] = arrRes[j][8];
jsSensorData["softVersion"] = arrRes[j][9];
jsSensorData["bpNo"] = arrRes[j][10];
jsSensorData["serialNo"] = arrRes[j][11];
jsSensorData["firstPowerTime"] = arrRes[j][12];
jsSensorData["WakeupTime"] = atoi(arrRes[j][13].c_str());
jsSensorData["StaticTime"] = atoi(arrRes[j][14].c_str());
jsSensorData["WaveTime"] = atoi(arrRes[j][15].c_str());
jsSensorData["BateryV"] = arrRes[j][16];
jsSensorData["ProductNo"] = arrRes[j][17];
jsSensorData["configFlag"] = atoi(arrRes[j][18].c_str());
jsSensorData["startBrands"] = arrRes[j][19];
jsSensorData["stopBrands"] = arrRes[j][20];
jsSensorData["featureInterVal"] = atoi(arrRes[j][21].c_str());
jsSensorData["waveInterVal"] = atoi(arrRes[j][22].c_str());
jsSensorData["samplingRate"] = atoi(arrRes[j][23].c_str());
// jsSensorData["scope"] = atoi(arrRes[j][24]);
jsSensorData["range"] = atoi(arrRes[j][25].c_str());
jsSensorData["envelopeBandPass"] = arrRes[j][26];
jsSensorData["faultFrequency"] = arrRes[j][27];
jsSensorData["zigbeePanId"] = arrRes[j][28];
jsSensorData["zigbeeChannel"] = (arrRes[j][29]);
jsSensorData["zigbeeAddr"] = arrRes[j][30];
jsSensorData["zigbeeLongAddr"] = arrRes[j][31];
jsSensorData["zigbeeDesAddr"] = arrRes[j][32];
jsSensorData["ZigbeePower"] = atoi(arrRes[j][33].c_str());
jsSensorData["ZigbeeRetry"] = atoi(arrRes[j][34].c_str());
jsSensorData["ZigbeeRetryGap"] = atoi(arrRes[j][35].c_str());
jsSensorData["ACCSampleTime"] = atoi(arrRes[j][36].c_str());
jsSensorData["status"] = atoi(arrRes[j][37].c_str());
jsSensorData["timeStamp"] = arrRes[j][38];
jsSensorData["viff"] = atoi(arrRes[j][39].c_str());
vector<string> vParam;
print_info("loose = %s\n", arrRes[j][42].c_str());
boost::split(vParam, arrRes[j][42], boost::is_any_of(","), boost::token_compress_on);
if (vParam.size() > 1)
jsSensorData["loose"] = vParam[1];
else
jsSensorData["loose"] = "0";
vector<string> vParamRSSI;
boost::split(vParamRSSI, arrRes[j][40], boost::is_any_of(","), boost::token_compress_on);
if (vParamRSSI.size() > 1) {
jsSensorData["RSSI"] = arrRes[j][40];
} else {
jsSensorData["RSSI"] = "0," + arrRes[j][40];
}
jsSensorData["update"] = atoi(arrRes[j][41].c_str());
jsSensorData["MeasurementID"] = arrRes[j][44];
jsSensorData["battery"] = arrRes[j][43];
jsSensorData["nodeWaveSend"] = arrRes[j][45];
jsArray.append(jsSensorData);
}
} else {
jsArray.resize(0);
jsonVal["success"] = true;
}
// LOG_INFO("26 end\n");
jsonVal["gatewayMac"] = GlobalConfig::MacAddr_G;
jsonVal["gatewayIP"] = GlobalConfig::IpAddr_G;
jsonVal["content"]["dataNodeArray"] = jsArray;
2024-10-22 20:56:21 +08:00
return show_value_.write(jsonVal);
2024-10-22 19:04:25 +08:00
}
std::string JsonData::JsonCmd_Cgi_54(Param_54 &param) {
Json::Value jsonVal;
jsonVal.clear();
jsonVal["cmd"] = "54";
jsonVal["dataNodeGatewayNo"] = GlobalConfig::MacAddr_G;
jsonVal["success"] = true;
jsonVal["message"] = " ";
Json::Value jsArray, nodeArray, gatewayArray;
array_t arrRes;
2024-10-22 20:56:21 +08:00
arrRes = sqlite_db_ctrl::instance().GetDataMultiLineTransaction(T_SENSOR_INFO(TNAME), "*", NULL);
2024-10-22 19:04:25 +08:00
int iResult = arrRes.size();
if (iResult > 0) {
int packgeNo = param.mPackageFlag;
int packgeMax = 0;
int packgeNum = 0;
jsonVal["package"] = packgeNo;
int lastSize = iResult % 10;
int index = iResult / 10;
if (lastSize > 0 && index > 0) {
packgeMax = index + 1;
if (packgeNo + 1 == packgeMax) {
packgeNum = iResult;
jsonVal["packageMax"] = index + 1;
} else {
packgeNum = (packgeNo + 1) * 10;
jsonVal["packageMax"] = index + 1;
}
} else if (lastSize == 0 && index > 0) {
packgeNum = (packgeNo + 1) * 10;
packgeMax = index;
jsonVal["packageMax"] = index;
} else if (lastSize > 0 && index == 0) {
packgeNum = lastSize;
packgeMax = index + 1;
jsonVal["packageMax"] = index;
}
for (int j = packgeNo * 10; j < packgeNum; j++) {
Json::Value nodeArray;
char hex[100];
nodeArray.append(arrRes[j][0]);
stringToHex(arrRes[j][1].c_str(), hex);
print_info("hex = %s\n", hex);
nodeArray.append(hex);
nodeArray.append(arrRes[j][2]);
nodeArray.append(arrRes[j][3]);
nodeArray.append(arrRes[j][4]);
nodeArray.append(arrRes[j][5]);
nodeArray.append(arrRes[j][6]);
nodeArray.append(arrRes[j][7]);
nodeArray.append(arrRes[j][8]);
nodeArray.append(arrRes[j][9]);
nodeArray.append(arrRes[j][10]);
nodeArray.append(arrRes[j][11]);
nodeArray.append(arrRes[j][12]);
nodeArray.append(arrRes[j][13]);
nodeArray.append(arrRes[j][14]);
nodeArray.append(arrRes[j][15]);
nodeArray.append(arrRes[j][16]);
nodeArray.append(arrRes[j][17]);
nodeArray.append(arrRes[j][18]);
nodeArray.append(arrRes[j][19]);
nodeArray.append(arrRes[j][20]);
nodeArray.append(arrRes[j][21]);
nodeArray.append(arrRes[j][22]);
nodeArray.append(arrRes[j][23]);
nodeArray.append(arrRes[j][24]);
nodeArray.append(arrRes[j][25]);
nodeArray.append(arrRes[j][26]);
nodeArray.append(arrRes[j][27]);
nodeArray.append(arrRes[j][28]);
nodeArray.append(arrRes[j][29]);
nodeArray.append(arrRes[j][30]);
nodeArray.append(arrRes[j][31]);
nodeArray.append(arrRes[j][32]);
nodeArray.append(arrRes[j][33]);
nodeArray.append(arrRes[j][34]);
nodeArray.append(arrRes[j][35]);
nodeArray.append(arrRes[j][36]);
nodeArray.append(arrRes[j][37]);
nodeArray.append(arrRes[j][38]);
nodeArray.append(arrRes[j][39]);
nodeArray.append(arrRes[j][40]);
nodeArray.append(arrRes[j][41]);
nodeArray.append(arrRes[j][42]);
nodeArray.append(arrRes[j][43]);
nodeArray.append(arrRes[j][44]);
jsArray.append(nodeArray);
}
} else {
jsArray.resize(0);
jsonVal["success"] = true;
}
Json::Value jsSystemSetting, jsonValnet, jsonValnet1, jsSystemInfo, jsonValZigbee;
Json::Value jsBody;
jsSystemSetting["ServerIpAddress"] = ReadStrByOpt(SERVERCONFIG, "Server", "localServerIpAddress");
jsSystemSetting["ServerPort"] = (ReadStrByOpt(SERVERCONFIG, "Server", "localServerPort").c_str());
jsSystemSetting["CommMode"] = (ReadStrByOpt(SERVERCONFIG, "Server", "CommMode").c_str());
jsSystemSetting["UserName"] = ReadStrByOpt(SERVERCONFIG, "Server", "UserName");
jsSystemSetting["Password"] = ReadStrByOpt(SERVERCONFIG, "Server", "Password");
jsSystemSetting["APN"] = ReadStrByOpt(SERVERCONFIG, "Server", "APN");
jsBody["ServerConfig"] = jsSystemSetting;
jsonVal["content"] = jsBody;
#ifdef G2UL_GATEWAY
jsonValnet1["dnsName"] = ReadStrByOpt(NETWORKCONFIG, "eth1", "dnsName");
jsonValnet1["networkPortStatus"] = ReadStrByOpt(NETWORKCONFIG, "eth1", "networkPortStatus");
jsonValnet1["gateway"] = ReadStrByOpt(NETWORKCONFIG, "eth1", "gateway");
jsonValnet1["subnetMask"] = ReadStrByOpt(NETWORKCONFIG, "eth1", "subnetMask");
jsonValnet1["dataWatchIpAddress"] = ReadStrByOpt(NETWORKCONFIG, "eth1", "ipAddress");
jsonValnet1["hostName"] = ReadStrByOpt(NETWORKCONFIG, "eth1", "hostName");
jsonVal["content"]["eth1"] = jsonValnet1;
jsonVal["content"]["gatewaytype"] = 1;
#endif
jsSystemInfo["GateWayVersion"] = ReadStrByOpt(SYSTEMINFOFILE, "Version", "GateWayVersion");
jsSystemInfo["SystemVersion"] = ReadStrByOpt(SYSTEMINFOFILE, "Version", "SystemVersion");
jsSystemInfo["WebVersion"] = ReadStrByOpt(SYSTEMINFOFILE, "Version", "WebVersion");
jsSystemInfo["GateWayHwVesion"] = ReadStrByOpt(SYSTEMINFOFILE, "Version", "GateWayHwVesion");
jsSystemInfo["GateWayProduct"] = ReadStrByOpt(SYSTEMINFOFILE, "Version", "GateWayProduct");
jsonVal["content"]["SystemInfo"] = jsSystemInfo;
jsonValZigbee["channel"] = ReadStrByOpt(ZIGBEECONFIG, "Zigbee", "channel");
jsonValZigbee["PanID"] = ReadStrByOpt(ZIGBEECONFIG, "Zigbee", "PanID");
jsonVal["content"]["zigbee"] = jsonValZigbee;
jsonVal["gatewayMac"] = GlobalConfig::MacAddr_G;
jsonVal["gatewayIP"] = GlobalConfig::IpAddr_G;
jsonVal["content"]["dataNodeArray"] = jsArray;
jsonVal["content"]["gateWay"] = gatewayArray;
2024-10-22 20:56:21 +08:00
return show_value_.write(jsonVal);
2024-10-22 19:04:25 +08:00
}
std::string JsonData::JsonCmd_Cgi_27(Param_27 &param) {
Json::Value jsonVal;
jsonVal.clear();
jsonVal["cmd"] = "27";
jsonVal["dataNodeGatewayNo"] = GlobalConfig::MacAddr_G;
jsonVal["success"] = true;
jsonVal["message"] = " ";
char whereCon[128] = {0};
char localtimestamp[32] = {0};
if (param.mDataNodeNo.length() > 0 && param.mType == "DELETE") {
sprintf(whereCon, "%s= '%s'", T_SENSOR_INFO(DATANODENO), param.mDataNodeNo.c_str());
2024-10-22 20:56:21 +08:00
sqlite_db_ctrl::instance().DeleteTableData(T_SENSOR_INFO(TNAME), whereCon);
sqlite_db_ctrl::instance().DeleteTableData(T_DATA_INFO(TNAME), whereCon);
sqlite_db_ctrl::instance().DeleteTableData(T_DATASTATIC_INFO(TNAME), whereCon);
sqlite_db_ctrl::instance().DeleteTableData(T_DATANODE_TIME(TNAME), whereCon);
sqlite_db_ctrl::instance().DeleteTableData(T_BATTERY_INFO(TNAME), whereCon);
2024-10-22 19:04:25 +08:00
char szTableName[50] = {0x00};
sprintf(szTableName, "DROP TABLE t_data_%s", param.mDataNodeNo.c_str());
2024-10-22 20:56:21 +08:00
sqlite_db_ctrl::instance().CreateTable(szTableName, 0);
2024-10-22 19:04:25 +08:00
memset(szTableName, 0x00, sizeof(szTableName));
sprintf(szTableName, "DROP TABLE t_dataStatic_%s", param.mDataNodeNo.c_str());
2024-10-22 20:56:21 +08:00
sqlite_db_ctrl::instance().CreateTable(szTableName, 0);
2024-10-22 19:04:25 +08:00
} else if (param.mDataNodeNo.length() > 0 && param.mType == "CORRECT") {
char updateSql[1024] = {0};
sprintf(whereCon, "%s= '%s'", T_SENSOR_INFO(DATANODENO), param.mDataNodeNo.c_str());
GetTimeNet(localtimestamp, 1);
sprintf(updateSql, "LooseValue = '0,2,");
string strUpdateSql = string(updateSql) + string(localtimestamp) + "' ";
print_info("updateSql = %s\n", strUpdateSql.c_str());
2024-10-22 20:56:21 +08:00
sqlite_db_ctrl::instance().UpdateTableData(T_SENSOR_INFO(TNAME), strUpdateSql.c_str(), whereCon);
2024-10-22 19:04:25 +08:00
} else {
jsonVal["success"] = false;
jsonVal["message"] = "没有传感器号";
}
2024-10-22 20:56:21 +08:00
return show_value_.write(jsonVal);
2024-10-22 19:04:25 +08:00
}
std::string JsonData::JsonCmd_Cgi_28(Param_28 &param) {
Json::Value jsonVal;
jsonVal.clear();
jsonVal["cmd"] = "28";
jsonVal["dataNodeGatewayNo"] = GlobalConfig::MacAddr_G;
jsonVal["success"] = true;
jsonVal["message"] = "";
if (param.mDataNodeNo.length() > 0) {
char updateColumn[128] = {0};
char whereCon[64] = {0};
sprintf(whereCon, "dataNodeNo='%s'", param.mDataNodeNo.c_str());
sprintf(updateColumn, "dataNodeName='%s'", param.mDataNodeName.c_str());
2024-10-22 20:56:21 +08:00
sqlite_db_ctrl::instance().UpdateTableData(T_SENSOR_INFO(TNAME), updateColumn, whereCon);
2024-10-22 19:04:25 +08:00
} else {
jsonVal["success"] = false;
jsonVal["message"] = "没有传感器号";
}
2024-10-22 20:56:21 +08:00
return show_value_.write(jsonVal);
2024-10-22 19:04:25 +08:00
}
std::string JsonData::JsonCmd_Cgi_29(Param_29 &param) {
Json::Value jsonVal;
Json::Value jsBody;
Json::Value SystemSetting;
jsonVal.clear();
jsonVal["cmd"] = "29";
jsonVal["dataNodeGatewayNo"] = GlobalConfig::MacAddr_G;
jsonVal["success"] = true;
jsonVal["message"] = " ";
std::vector<float> vecWave;
/* 新增代码 */
char whereCon[64] = {};
sprintf(whereCon, "dataNodeNo='%s'", param.mDataNodeNo.c_str());
2024-10-22 20:56:21 +08:00
vec_t res = sqlite_db_ctrl::instance().GetDataSingleLine(T_SENSOR_INFO(TNAME), " * ", whereCon);
2024-10-22 19:04:25 +08:00
char localtimestamp[32] = {0};
std::string strWaveData = "";
std::string filename = "/opt/data/" + param.mChannelId + ".dat";
if (access(filename.c_str(), 0) >= 0) {
std::ifstream inFile(filename.c_str(), ios::in | ios::binary);
if (!inFile) {
print_error("read channel data error\n");
jsonVal["success"] = false;
jsonVal["message"] = "error";
} else {
float fTemp = 0;
// std::vector<float> hanningWave;
inFile.read((char *)localtimestamp, sizeof(localtimestamp));
while (inFile.read((char *)&fTemp, sizeof(fTemp))) {
vecWave.push_back(fTemp);
}
//测试正弦波
// pCalculation->GenerateSin(vecWave);
//添加汉宁窗
/*pCalculation->Hanning(vecWave, hanningWave);
for(int i = 0; i < vecWave.size();i++){
vecWave[i] = (vecWave[i]*hanningWave[i]);
}*/
int flag = param.mPackageFlag;
flag = (flag + 1) * 1024;
int number = vecWave.size();
int start = param.mPackageFlag * 1024;
if (number < 1024) {
flag = number;
start = 0;
}
char buf[32];
for (int i = start; i < flag; i++) {
if (i == start) {
memset(buf, 0, 32);
sprintf(buf, "%.2f", vecWave[i]);
std::string waveTemp(buf);
strWaveData = waveTemp;
} else {
memset(buf, 0, 32);
sprintf(buf, "%.2f", vecWave[i]);
std::string waveTemp(buf);
strWaveData = strWaveData + "," + waveTemp;
}
}
int max = number / 1024;
if (max == 0 && number > 0) {
max = 1;
}
jsBody["packageMax"] = max;
}
} else {
jsonVal["success"] = false;
jsonVal["message"] = "没有数据文件";
}
jsBody["channelId"] = param.mChannelId;
jsBody["package"] = param.mPackageFlag;
print_info("vecWave.size() = %d,sample = %d,second = %f\n", vecWave.size(), atoi(res[23].c_str()), float(vecWave.size() / atoi(res[23].c_str())));
string::size_type comper = param.mChannelId.find("Z");
if (comper != string::npos && res[17] == "02") {
jsBody["second"] = float((float)vecWave.size() / (float)atoi(res[23].c_str()));
} else if (res[17] == "01") {
jsBody["second"] = float((float)vecWave.size() / (float)atoi(res[23].c_str()));
} else {
jsBody["second"] = 1;
}
jsBody["Data"] = strWaveData;
jsBody["timestamp"] = string(localtimestamp);
jsonVal["content"] = jsBody;
2024-10-22 20:56:21 +08:00
return show_value_.write(jsonVal);
2024-10-22 19:04:25 +08:00
}
std::string JsonData::JsonCmd_Cgi_30(Param_30 &param) {
Json::Value jsonVal;
Json::Value jsBody;
Json::Value SystemSetting;
jsonVal.clear();
jsonVal["cmd"] = "30";
jsonVal["dataNodeGatewayNo"] = GlobalConfig::MacAddr_G;
jsonVal["success"] = true;
jsonVal["message"] = "";
int i = 0;
std::string strWaveData;
std::string filename = "/opt/data/" + param.mChannelId + ".dat";
char localtimestamp[32] = {0};
std::vector<float> vecWave;
std::vector<float> hanningWave;
std::vector<float> addhanningWave;
std::vector<float> fftWave;
int sampleRateReference = 0;
/* 新增代码 */
char whereCon[64] = {};
sprintf(whereCon, "dataNodeNo='%s'", param.mDataNodeNo.c_str());
2024-10-22 20:56:21 +08:00
vec_t res = sqlite_db_ctrl::instance().GetDataSingleLine(T_SENSOR_INFO(TNAME), " * ", whereCon);
2024-10-22 19:04:25 +08:00
if (access(filename.c_str(), 0) >= 0) {
std::ifstream inFile(filename.c_str(), ios::in | ios::binary);
if (!inFile) {
print_error("read channel data error\n");
jsonVal["success"] = "false";
jsonVal["message"] = "error";
} else {
float fTemp = 0;
inFile.read((char *)localtimestamp, sizeof(localtimestamp));
string::size_type comper = param.mChannelId.find("Z");
if (comper != string::npos && res[17] == "02") {
while (inFile.read((char *)&fTemp, sizeof(fTemp))) {
vecWave.push_back(fTemp);
}
//进行傅立叶变换
pCalculation->FFTSpec(vecWave, fftWave);
sampleRateReference = 1000;
} else {
while (inFile.read((char *)&fTemp, sizeof(fTemp))) { // 取8K进行计算
// if(i < 8192)
{ vecWave.push_back(fTemp); }
// else{
// break;
// }
// i++;
}
//测试正弦波
// pCalculation->GenerateSin(vecWave);
// if(vecWave.size() < 8192){
// for(int i = vecWave.size(); i < 8192;i++){
// vecWave.push_back(0);
// }
// }
//添加汉宁窗
// pCalculation->Hanning(vecWave, hanningWave);
// for(int i = 0; i < vecWave.size();i++){
// addhanningWave.push_back(vecWave[i]*hanningWave[i]);
// }
//进行傅立叶变换
pCalculation->FFTSpec(vecWave, fftWave);
sampleRateReference = 1024;
}
/*for(int i = 0; i < fftWave.size();i++){
fftWave[i] = fftWave[i]*2;
}*/
printf("2---------------------------------------------->vecWave = %d,fftWave = %d\n", vecWave.size(), fftWave.size());
int flag = param.mPackageFlag;
flag = (flag + 1) * sampleRateReference;
int number = fftWave.size();
printf("number---------------------------------------------->%d\n", number);
int start = param.mPackageFlag * sampleRateReference;
printf("param.mPackageFlag = %d\n", param.mPackageFlag);
printf("param.start = %d\n", start);
printf("param.flag = %d\n", flag);
if (number < sampleRateReference) {
flag = number;
start = 0;
}
char buf[32];
for (int i = start; i < flag; i++) {
if (i == start) {
memset(buf, 0, 32);
sprintf(buf, "%.6f", fftWave[i]);
std::string waveTemp(buf);
strWaveData = waveTemp;
} else {
memset(buf, 0, 32);
sprintf(buf, "%.6f", fftWave[i]);
std::string waveTemp(buf);
strWaveData = strWaveData + "," + waveTemp;
}
}
int max = number / sampleRateReference;
print_info("max = %d\n", max);
if (max == 0 && number > 0) {
max = 1;
}
jsBody["packageMax"] = max;
}
} else {
jsonVal["success"] = false;
jsonVal["message"] = "没有数据文件";
}
jsBody["channelId"] = param.mChannelId;
jsBody["package"] = param.mPackageFlag;
jsBody["timestamp"] = string(localtimestamp);
jsBody["Data"] = strWaveData;
double resolution = 0.0;
int SampleRate = 0;
print_info("sensor type %s\n", res[17].c_str());
if (res[17] == "01") {
SampleRate = atoi(res[23].c_str());
printf("@@@@@@@@@@@@@@@@@@@@sample_rate=%d\n", SampleRate);
resolution = (((double)SampleRate / 1000) * 1016) / vecWave.size();
// resolution = (double)SampleRate/vecWave.size();
} else if (res[17] == "02") {
string::size_type comper = param.mChannelId.find("Z");
if (comper != string::npos) {
SampleRate = atoi(res[23].c_str());
resolution = (double)SampleRate / vecWave.size();
// resolution = (((double)vecWave.size()/1000)*1024)/ (SampleRate * ((vecWave.size()/SampleRate)));
printf("@@@@@@@@@@@@@@@@@@@@sample_rate=%d,resolution = %f\n", SampleRate, resolution);
} else {
SampleRate = 8192;
printf("@@@@@@@@@@@@@@@@@@@@sample_rate=%d\n", SampleRate);
if (vecWave.size() < 8192) {
resolution = (double)SampleRate / vecWave.size();
} else {
resolution = (double)SampleRate / 8192;
}
}
}
print_info("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@the sample rate is %d,the resolution %f\n", SampleRate, resolution);
char buf[32];
memset(buf, 0, 32);
sprintf(buf, "%f", resolution);
jsBody["resolution"] = std::string(buf);
jsonVal["content"] = jsBody;
2024-10-22 20:56:21 +08:00
return show_value_.write(jsonVal);
2024-10-22 19:04:25 +08:00
}
std::string JsonData::JsonCmd_Cgi_40(Param_40 &param) {
Json::Value jsonVal;
Json::Value jsBody;
Json::Value SystemSetting;
jsonVal.clear();
jsonVal["cmd"] = "40";
jsonVal["dataNodeGatewayNo"] = GlobalConfig::MacAddr_G;
jsonVal["success"] = true;
jsonVal["message"] = "";
int i = 0;
int sampleRateReference = 0;
std::vector<float> vecWave;
/* 新增代码 */
char whereCon[64] = {};
sprintf(whereCon, "dataNodeNo='%s'", param.mDataNodeNo.c_str());
2024-10-22 20:56:21 +08:00
vec_t res = sqlite_db_ctrl::instance().GetDataSingleLine(T_SENSOR_INFO(TNAME), " * ", whereCon);
2024-10-22 19:04:25 +08:00
std::string strWaveData;
std::string filename = "/opt/data/" + param.mChannelId + ".dat";
char localtimestamp[32] = {0};
if (access(filename.c_str(), 0) >= 0) {
std::ifstream inFile(filename.c_str(), ios::in | ios::binary);
if (!inFile) {
print_error("read channel data error\n");
jsonVal["success"] = "false";
jsonVal["message"] = "error";
} else {
float fTemp = 0;
std::vector<float> envWave;
std::vector<float> hanningWave;
std::vector<float> addhanningWave;
inFile.read((char *)localtimestamp, sizeof(localtimestamp));
string::size_type comper = param.mChannelId.find("Z");
if (comper != string::npos && res[17] == "02") {
while (inFile.read((char *)&fTemp, sizeof(fTemp))) {
vecWave.push_back(fTemp);
}
//添加汉宁窗
pCalculation->Hanning(vecWave, hanningWave);
for (int i = 0; i < vecWave.size(); i++) {
addhanningWave.push_back(vecWave[i] * hanningWave[i]);
}
//进行傅立叶变换
if (param.StartFrequency == 0) {
param.StartFrequency = 3000;
param.EndFrequency = 5000;
}
pCalculation->envSpec(addhanningWave, envWave, param.StartFrequency, param.EndFrequency);
sampleRateReference = 1000;
} else {
while (inFile.read((char *)&fTemp, sizeof(fTemp))) { // 取8K进行计算
if (i < 8192) {
vecWave.push_back(fTemp);
} else {
break;
}
i++;
}
if (vecWave.size() < 8192) {
for (int i = vecWave.size(); i < 8192; i++) {
vecWave.push_back(0);
}
}
//添加汉宁窗
pCalculation->Hanning(vecWave, hanningWave);
for (int i = 0; i < vecWave.size(); i++) {
addhanningWave.push_back(vecWave[i] * hanningWave[i]);
}
//进行傅立叶变换
if (param.StartFrequency == 0) {
param.StartFrequency = 3000;
param.EndFrequency = 5000;
}
pCalculation->envSpec(addhanningWave, envWave, param.StartFrequency, param.EndFrequency);
sampleRateReference = 1024;
}
/*for(int i = 0; i < envWave.size();i++){
envWave[i] = envWave[i]*2;
}*/
printf("2---------------------------------------------->%d\n", envWave.size());
printf("2---------------------------------------------->%f\n", envWave[10]);
print_info("after fft--------------------------------------------------->fftWave.size()=%d\n", envWave.size());
int flag = param.mPackageFlag;
flag = (flag + 1) * sampleRateReference;
int number = envWave.size();
int start = param.mPackageFlag * sampleRateReference;
printf("param.mPackageFlag = %d\n", param.mPackageFlag);
printf("param.start = %d\n", start);
printf("param.flag = %d\n", flag);
if (number < sampleRateReference) {
flag = number;
start = 0;
}
char buf[32];
for (int i = start; i < flag; i++) {
if (i == start) {
memset(buf, 0, 32);
sprintf(buf, "%.6f", envWave[i]);
std::string waveTemp(buf);
strWaveData = waveTemp;
} else {
memset(buf, 0, 32);
sprintf(buf, "%.6f", envWave[i]);
std::string waveTemp(buf);
strWaveData = strWaveData + "," + waveTemp;
}
}
int max = number / sampleRateReference;
if (max == 0 && number > 0) {
max = 1;
}
jsBody["packageMax"] = max;
}
} else {
jsonVal["success"] = false;
jsonVal["message"] = "没有数据文件";
}
jsBody["channelId"] = param.mChannelId;
jsBody["package"] = param.mPackageFlag;
jsBody["timestamp"] = string(localtimestamp);
jsBody["Data"] = strWaveData;
double resolution = 0.0;
int SampleRate = 0;
print_info("sensor type %s\n", res[17].c_str());
if (res[17] == "01") {
SampleRate = atoi(res[23].c_str());
printf("@@@@@@@@@@@@@@@@@@@@sample_rate=%d\n", SampleRate);
resolution = (((double)SampleRate / 1000) * 1016) / 8192;
} else if (res[17] == "02") {
string::size_type comper = param.mChannelId.find("Z");
if (comper != string::npos) {
SampleRate = atoi(res[23].c_str());
resolution = (double)SampleRate / vecWave.size();
printf("@@@@@@@@@@@@@@@@@@@@sample_rate=%d,resolution = %f\n", SampleRate, resolution);
} else {
SampleRate = 8000;
printf("@@@@@@@@@@@@@@@@@@@@sample_rate=%d\n", SampleRate);
resolution = (((double)SampleRate / 1000) * 1024) / 8192;
}
}
printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@the sample rate is %d,the resolution %f\n", SampleRate, resolution);
char buf[32];
memset(buf, 0, 32);
sprintf(buf, "%f", resolution);
jsBody["resolution"] = std::string(buf);
jsonVal["content"] = jsBody;
2024-10-22 20:56:21 +08:00
return show_value_.write(jsonVal);
2024-10-22 19:04:25 +08:00
}
std::string JsonData::JsonCmd_Cgi_41(std::vector<Param_41> &param, int isServer) {
Json::Value jsonVal;
jsonVal.clear();
jsonVal["cmd"] = "41";
jsonVal["dataNodeGatewayNo"] = GlobalConfig::MacAddr_G;
jsonVal["success"] = true;
jsonVal["message"] = " ";
char whereCon[1024] = {0};
char updateSql[1024] = {0};
for (int i = 0; i < param.size(); i++) {
char dataNodeName[100] = {0x00};
hexToAscii(param[i].mdataNodeName.c_str(), dataNodeName);
print_info("str = %s\n", dataNodeName);
print_info("dataNodeName = %s\n", param[i].mdataNodeName.c_str());
int dataNodeNameLength = param[i].mdataNodeName.length();
if (dataNodeNameLength != 0) {
sprintf(updateSql, "dataNodeName = '%s',ZigbeePower = '%d',ZigbeeRetry = '%d',featureInterVal='%d',waveInterVal='%d',range='%d',samplingRate='%d',AccSampleTime = '%d',\
startBrands = '%s',stopBrands = '%s',envelopeBandPass = '%s',viff = '%d',faultFrequency = '%s' ,MeasurementID = '%s' ,UpdateFlag = -1",
dataNodeName, param[i].ZigbeePower, param[i].ZigbeeRetry, param[i].mfeatureInterVal, param[i].mwaveInterVal, param[i].mrange, param[i].msamplingRate, param[i].mAccSampleTime, param[i].mstartBrands.c_str(), param[i].mstopBrands.c_str(),
param[i].menvelopeBandPass.c_str(), param[i].mviff, param[i].mfaultFrequency.c_str(), param[i].mMeasurementID.c_str());
} else if (dataNodeNameLength == 0) {
sprintf(updateSql, "ZigbeePower = '%d',ZigbeeRetry = '%d',featureInterVal='%d',waveInterVal='%d',range='%d',samplingRate='%d',AccSampleTime = '%d',\
startBrands = '%s',stopBrands = '%s',envelopeBandPass = '%s',viff = '%d',faultFrequency = '%s',MeasurementID = '%s',NodeWaveSend = '%s',UpdateFlag = 0",
param[i].ZigbeePower, param[i].ZigbeeRetry, param[i].mfeatureInterVal, param[i].mwaveInterVal, param[i].mrange, param[i].msamplingRate, param[i].mAccSampleTime, param[i].mstartBrands.c_str(), param[i].mstopBrands.c_str(), param[i].menvelopeBandPass.c_str(),
param[i].mviff, param[i].mfaultFrequency.c_str(), param[i].mMeasurementID.c_str(), param[i].nodeWaveSend.c_str());
}
if (!isServer)
sprintf(whereCon, "dataNodeNo='%s'", param[i].mdataNodeNo.c_str());
else
sprintf(whereCon, "MeasurementID ='%s'", param[i].mdataNodeNo.c_str());
2024-10-22 20:56:21 +08:00
int iRet = sqlite_db_ctrl::instance().UpdateTableData(T_SENSOR_INFO(TNAME), updateSql, whereCon);
2024-10-22 19:04:25 +08:00
memset(whereCon, 0x00, sizeof(whereCon));
memset(updateSql, 0x00, sizeof(updateSql));
if (iRet != 0) jsonVal["success"] = false;
}
2024-10-22 20:56:21 +08:00
return show_value_.write(jsonVal);
2024-10-22 19:04:25 +08:00
}
std::string JsonData::JsonCmd_Cgi_42(Param_42 &param) {
Json::Value jsonVal;
jsonVal.clear();
jsonVal["cmd"] = "42";
jsonVal["dataNodeGatewayNo"] = GlobalConfig::MacAddr_G;
jsonVal["success"] = true;
jsonVal["message"] = "上传成功!";
char cmd[100] = {0x00};
strcpy(cmd, "rm -rf /tmp/cgic*");
system(cmd);
if (param.fileName == "DataNode.tar") {
string strcmd = "tar xvf /opt/";
strcmd = strcmd + param.fileName;
strcmd = strcmd + " -C /opt/";
print_info("strcmd = %s\n", strcmd.c_str());
system(strcmd.c_str());
} else if (param.fileName == "rfsbu.tar") {
LOG_INFO("update rfsbu.tar\n");
sleep(3);
print_info("strcmd = %s\n", param.fileName.c_str());
int iRet = system("/opt/opt.sh");
print_info("iRet = %d\n", iRet);
if (iRet == -1) {
perror("system() error");
}
} else if (param.fileName == "update.json") {
print_info("strcmd = %s\n", param.fileName.c_str());
ReadStrConfig("/opt/update.json");
} else if (param.fileName == "backup.json") {
ImportConfig("/opt/backup.json");
} else if (param.fileName == "DataNode.csv") {
print_info("strcmd = %s\n", param.fileName.c_str());
int iRet = UpdataDataNodeConfig("/opt/DataNode.csv");
string str = to_string(iRet) + "个传感器更新成功";
if (iRet < 0) {
jsonVal["success"] = false;
jsonVal["message"] = "更新失败!";
} else {
jsonVal["success"] = true;
jsonVal["message"] = str;
}
} else {
jsonVal["success"] = false;
jsonVal["message"] = "文件名错误!";
}
2024-10-22 20:56:21 +08:00
return show_value_.write(jsonVal);
2024-10-22 19:04:25 +08:00
}
std::string JsonData::JsonCmd_Cgi_31(Param_31 &param) {
Json::Value jsonVal;
jsonVal.clear();
jsonVal["cmd"] = "31";
if (0 == param.mMode) {
WriteStr2Config(ZIGBEECONFIG, "Zigbee", "channel", param.mChannelId);
WriteStr2Config(ZIGBEECONFIG, "Zigbee", "PanID", param.mPanID);
}
Json::Value jsBody;
jsonVal["message"] = "设置完成";
jsonVal["success"] = true;
jsBody["channel"] = ReadStrByOpt(ZIGBEECONFIG, "Zigbee", "channel");
jsBody["panID"] = GlobalConfig::ZigbeeInfo_G.PanID;
jsBody["RetryNum"] = GlobalConfig::ZigbeeInfo_G.RetryNum;
jsBody["TranTimeout"] = GlobalConfig::ZigbeeInfo_G.TranTimeout;
jsBody["status"] = "1";
jsonVal["content"] = jsBody;
2024-10-22 20:56:21 +08:00
return show_value_.write(jsonVal);
2024-10-22 19:04:25 +08:00
}
std::string JsonData::JsonCmd_Cgi_32(Param_32 &param) {
Json::Value jsonVal;
jsonVal.clear();
jsonVal["cmd"] = "32";
jsonVal["success"] = true;
jsonVal["message"] = "";
Json::Value jsBody;
std::string strWaveData;
jsBody["channelId"] = param.mChannelId;
jsBody["package"] = param.mPackageFlag;
jsBody["Data"] = strWaveData;
jsonVal["content"] = jsBody;
2024-10-22 20:56:21 +08:00
return show_value_.write(jsonVal);
2024-10-22 19:04:25 +08:00
}
std::string JsonData::JsonCmd_Cgi_43() {
Json::Value jsonVal;
jsonVal.clear();
jsonVal["success"] = true;
jsonVal["message"] = "";
2024-10-22 20:56:21 +08:00
return show_value_.write(jsonVal);
2024-10-22 19:04:25 +08:00
}