WLG/jsonparse/web_cmd_parse4.cpp

199 lines
7.6 KiB
C++
Raw Permalink Normal View History

2025-06-29 11:25:40 +08:00
#include "communication_cmd.hpp"
#include <fstream>
#include <boost/algorithm/string.hpp>
#include <zlog.h>
#include "common/global.hpp"
#include "dbaccess/sql_db.hpp"
#include "platform/platform_init.hpp"
2025-07-23 22:00:44 +08:00
#include "wifi_5g/scan_blueteeth.h"
2025-06-29 11:25:40 +08:00
extern zlog_category_t *zct;
extern const char *JSON_FIELD_CMD;
2025-07-23 22:00:44 +08:00
extern std::unordered_map<std::string, DeviceInfo> device_map;;
2025-06-29 11:25:40 +08:00
std::string JsonData::JsonCmd_Cgi_100(Param_100 &param){
Json::Value jsonVal;
jsonVal.clear();
jsonVal[JSON_FIELD_CMD] = "100";
jsonVal["success"] = true;
jsonVal["message"] = "获取传感器信息成功";
Json::Value jsBody;
array_t arrRes = sqlite_db_ctrl::instance().GetDataMultiLine(T_SENSOR_BT_INFO(TNAME), "*", NULL);
2025-06-29 14:08:45 +08:00
int iResult = arrRes.size();
2025-06-29 11:25:40 +08:00
if (arrRes.size() > 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;
}
2025-06-29 14:08:45 +08:00
for (size_t i = packgeNo * 10; i < packgeNum; i++) {
2025-06-29 11:25:40 +08:00
Json::Value jsSensorData;
jsSensorData["mac"] = arrRes[i][0];
jsSensorData["name"] = arrRes[i][1];
2025-07-14 20:24:40 +08:00
jsSensorData["type"] = arrRes[i][2].c_str();
2025-06-29 11:25:40 +08:00
jsSensorData["status"] = atoi(arrRes[i][3].c_str());
jsSensorData["config_date"] = atoi(arrRes[i][4].c_str());
std::string strMac = arrRes[i][0];
2025-06-29 14:08:45 +08:00
char whereCon[256] = {0x00};
sprintf(whereCon, "mac = '%s' order by timestamp desc limit 1 ", strMac.c_str());
vec_t vec_ret = sqlite_db_ctrl::instance().GetDataSingleLine(" blueteeth_info ", "*", whereCon);
2025-07-23 22:00:44 +08:00
if (vec_ret.size() < 1) {
zlog_error(zct, "bluetooth info not enough data, mac = %s", strMac.c_str());
continue;
}
jsSensorData["env_temp"] = atof(vec_ret[6].c_str());
jsSensorData["dev_temp"] = atof(vec_ret[7].c_str());
jsSensorData["battery_v"] = atof(vec_ret[5].c_str());
jsSensorData["rssi"] = atoi(vec_ret[3].c_str());
jsSensorData["feature_date"] = vec_ret[9];
2025-06-29 11:25:40 +08:00
jsBody.append(jsSensorData);
}
jsonVal["content"] = jsBody;
} else {
jsonVal["success"] = false;
jsonVal["message"] = "没有传感器信息";
jsonVal["content"].resize(0);
}
return show_value_.write(jsonVal);
}
2025-07-23 22:00:44 +08:00
2025-06-29 11:25:40 +08:00
std::string JsonData::JsonCmd_Cgi_101(Param_101 &param){
Json::Value jsonVal;
jsonVal.clear();
jsonVal[JSON_FIELD_CMD] = "101";
jsonVal["success"] = true;
jsonVal["message"] = "获取传感器信息成功";
Json::Value jsBody;
2025-07-23 22:00:44 +08:00
double min = -102;
double max = -60;
2025-06-29 11:25:40 +08:00
char selectCon[256] = {0}, szTableName[100] = {0x00}, whereCon[256] = {0x00};
sprintf(whereCon, "mac = '%s' and timeStamp < '%s' and timeStamp > '%s'", param.mac.c_str(),param.timeEnd.c_str(), param.timeStart.c_str());
int rows = sqlite_db_ctrl::instance().GetTableRows(" blueteeth_info ", whereCon);
int packgeNo = param.mPackageFlag;
int packgeMax = 0;
int packgeNum = 0;
jsonVal["package"] = packgeNo;
2025-07-14 20:24:40 +08:00
int lastSize = rows % 400;
int index = rows / 400;
2025-06-29 11:25:40 +08:00
if (lastSize > 0 && index > 0) {
packgeMax = index + 1;
if (packgeNo + 1 == packgeMax) {
packgeNum = rows - lastSize;
jsonVal["packageMax"] = index + 1;
} else {
2025-07-14 20:24:40 +08:00
packgeNum = (packgeNo)*400;
2025-06-29 11:25:40 +08:00
jsonVal["packageMax"] = index + 1;
}
} else if (lastSize == 0 && index > 0) {
2025-07-14 20:24:40 +08:00
packgeNum = (packgeNo + 1) * 400;
2025-06-29 11:25:40 +08:00
packgeMax = index;
jsonVal["packageMax"] = index;
} else if (lastSize > 0 && index == 0) {
packgeNum = 0;
packgeMax = index + 1;
jsonVal["packageMax"] = index;
}
memset(whereCon, 0x00, sizeof(whereCon));
2025-07-14 20:24:40 +08:00
sprintf(whereCon, "mac = '%s' and timeStamp < '%s' and timeStamp > '%s' order by timeStamp asc LIMIT %d OFFSET %d", param.mac.c_str(), param.timeEnd.c_str(), param.timeStart.c_str(), 400, packgeNum);
2025-07-23 22:00:44 +08:00
array_t arrRes = sqlite_db_ctrl::instance().GetDataMultiLine(" blueteeth_info ", "blueteeth_temp,chip_temp,volt,rssi_percent,timestamp", whereCon);
2025-07-14 20:24:40 +08:00
zlog_info(zct, "arrRes size = %d", arrRes.size());
2025-06-29 14:08:45 +08:00
if (arrRes.size() > 0) {
2025-06-29 11:25:40 +08:00
Json::Value jsSensorData;
2025-07-14 20:24:40 +08:00
Json::Value iTem1, iTem2, iTem3,iTem4;
2025-06-29 14:08:45 +08:00
for (size_t i = 0; i < arrRes.size(); i++) {
iTem1.append(arrRes[i][0]);
2025-07-14 20:24:40 +08:00
iTem1.append(arrRes[i][4]);
2025-06-29 14:08:45 +08:00
iTem2.append(arrRes[i][1]);
2025-07-14 20:24:40 +08:00
iTem2.append(arrRes[i][4]);
2025-06-29 14:08:45 +08:00
iTem3.append(arrRes[i][2]);
2025-07-14 20:24:40 +08:00
iTem3.append(arrRes[i][4]);
iTem4.append(arrRes[i][3]);
iTem4.append(arrRes[i][4]);
2025-06-29 11:25:40 +08:00
}
jsSensorData["env_temp"] = iTem1;
2025-07-14 20:24:40 +08:00
jsSensorData["dev_temp"] = iTem2;
jsSensorData["battery_t"] = iTem3;
jsSensorData["rssi"] = iTem4;
2025-06-29 11:25:40 +08:00
jsBody.append(jsSensorData);
jsonVal["content"] = jsBody;
} else {
jsonVal["success"] = false;
jsonVal["message"] = "没有传感器信息";
jsonVal["content"].resize(0);
}
2025-06-29 14:08:45 +08:00
return show_value_.write(jsonVal);
2025-06-29 11:25:40 +08:00
}
std::string JsonData::JsonCmd_Cgi_102(){
2025-07-14 20:24:40 +08:00
Json::Value jsonVal;
jsonVal.clear();
jsonVal[JSON_FIELD_CMD] = "102";
jsonVal["success"] = true;
jsonVal["message"] = "获取传感器信息成功";
Json::Value jsBody;
2025-07-23 22:00:44 +08:00
for (const auto& [mac, info] : device_map) {
std::cout << mac << " RSSI: " << info.rssi << "\n";
2025-07-14 20:24:40 +08:00
Json::Value item;
2025-07-23 22:00:44 +08:00
item["mac"] = mac;
2025-06-29 14:08:45 +08:00
char whereCon[256] = {0x00};
2025-07-23 22:00:44 +08:00
sprintf(whereCon, "mac = '%s'", mac.c_str());
2025-06-29 14:08:45 +08:00
int rows = sqlite_db_ctrl::instance().GetTableRows(T_SENSOR_BT_INFO(TNAME), whereCon);
2025-07-14 20:24:40 +08:00
if (rows > 0) {
item["added"] = true;
2025-06-29 14:08:45 +08:00
}else {
2025-07-14 20:24:40 +08:00
item["added"] = false;
2025-06-29 11:25:40 +08:00
}
2025-07-23 22:00:44 +08:00
item["rssi"] = info.rssi;
2025-07-14 20:24:40 +08:00
item["type"] = "DN-801";
jsBody.append(item);
2025-06-29 11:25:40 +08:00
}
2025-07-14 20:24:40 +08:00
jsonVal["content"] = jsBody;
return show_value_.write(jsonVal);
2025-06-29 11:25:40 +08:00
}
std::string JsonData::JsonCmd_Cgi_103(Param_103 &param){
Json::Value jsonVal;
jsonVal.clear();
jsonVal[JSON_FIELD_CMD] = "103";
jsonVal["success"] = true;
jsonVal["message"] = "获取传感器信息成功";
2025-06-29 14:08:45 +08:00
char insertSql[256] = {0x00},updateSql[256] = {0x00},
whereCon[256] = {0x00};
char localtimestamp[32] = {0};
sprintf(whereCon, "mac = '%s'", param.mac.c_str());
GetTimeNet(localtimestamp, 1);
2025-06-29 11:25:40 +08:00
if (param.operate == "add") {
2025-07-14 20:24:40 +08:00
sprintf(insertSql, "'%s','','DN-801','1','%s'", param.mac.c_str(),localtimestamp);
2025-06-29 14:08:45 +08:00
sqlite_db_ctrl::instance().InsertData(T_SENSOR_BT_INFO(TNAME), insertSql);
2025-06-29 11:25:40 +08:00
} else if (param.operate == "update") {
2025-06-29 14:08:45 +08:00
sprintf(updateSql, "name = '%s'", param.name.c_str());
sqlite_db_ctrl::instance().UpdateTableData(T_SENSOR_BT_INFO(TNAME), updateSql,whereCon);
2025-06-29 11:25:40 +08:00
} else if (param.operate == "delete") {
2025-06-29 14:08:45 +08:00
sqlite_db_ctrl::instance().DeleteTableData(T_SENSOR_BT_INFO(TNAME), whereCon);
2025-06-29 11:25:40 +08:00
}
return show_value_.write(jsonVal);
}