246 lines
9.1 KiB
C++
246 lines
9.1 KiB
C++
#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"
|
|
#include "wifi_5g/scan_blueteeth.h"
|
|
#include "uart/uart.hpp"
|
|
|
|
extern zlog_category_t *zct;
|
|
extern const char *JSON_FIELD_CMD;
|
|
extern std::unordered_map<std::string, DeviceInfo> device_map;;
|
|
|
|
std::string JsonData::JsonCmd_Cgi_100(Param_100 ¶m){
|
|
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);
|
|
int iResult = arrRes.size();
|
|
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;
|
|
}
|
|
for (size_t i = packgeNo * 10; i < packgeNum; i++) {
|
|
Json::Value jsSensorData;
|
|
jsSensorData["mac"] = arrRes[i][0];
|
|
jsSensorData["name"] = arrRes[i][1];
|
|
jsSensorData["type"] = arrRes[i][2].c_str();
|
|
jsSensorData["status"] = atoi(arrRes[i][3].c_str());
|
|
jsSensorData["config_date"] = atoi(arrRes[i][4].c_str());
|
|
std::string strMac = arrRes[i][0];
|
|
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);
|
|
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];
|
|
jsBody.append(jsSensorData);
|
|
}
|
|
jsonVal["content"] = jsBody;
|
|
} else {
|
|
jsonVal["success"] = false;
|
|
jsonVal["message"] = "没有传感器信息";
|
|
jsonVal["content"].resize(0);
|
|
}
|
|
|
|
return show_value_.write(jsonVal);
|
|
}
|
|
|
|
std::string JsonData::JsonCmd_Cgi_101(Param_101 ¶m){
|
|
Json::Value jsonVal;
|
|
jsonVal.clear();
|
|
jsonVal[JSON_FIELD_CMD] = "101";
|
|
jsonVal["success"] = true;
|
|
jsonVal["message"] = "获取传感器信息成功";
|
|
|
|
Json::Value jsBody;
|
|
double min = -102;
|
|
double max = -60;
|
|
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;
|
|
int lastSize = rows % 400;
|
|
int index = rows / 400;
|
|
if (lastSize > 0 && index > 0) {
|
|
packgeMax = index + 1;
|
|
if (packgeNo + 1 == packgeMax) {
|
|
packgeNum = rows - lastSize;
|
|
jsonVal["packageMax"] = index + 1;
|
|
} else {
|
|
packgeNum = (packgeNo)*400;
|
|
jsonVal["packageMax"] = index + 1;
|
|
}
|
|
} else if (lastSize == 0 && index > 0) {
|
|
packgeNum = (packgeNo + 1) * 400;
|
|
packgeMax = index;
|
|
jsonVal["packageMax"] = index;
|
|
} else if (lastSize > 0 && index == 0) {
|
|
packgeNum = 0;
|
|
packgeMax = index + 1;
|
|
jsonVal["packageMax"] = index;
|
|
}
|
|
memset(whereCon, 0x00, sizeof(whereCon));
|
|
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);
|
|
array_t arrRes = sqlite_db_ctrl::instance().GetDataMultiLine(" blueteeth_info ", "blueteeth_temp,chip_temp,volt,rssi_percent,timestamp", whereCon);
|
|
zlog_info(zct, "arrRes size = %d", arrRes.size());
|
|
if (arrRes.size() > 0) {
|
|
Json::Value jsSensorData;
|
|
Json::Value iTem1, iTem2, iTem3,iTem4;
|
|
for (size_t i = 0; i < arrRes.size(); i++) {
|
|
iTem1.append(arrRes[i][0]);
|
|
iTem1.append(arrRes[i][4]);
|
|
iTem2.append(arrRes[i][1]);
|
|
iTem2.append(arrRes[i][4]);
|
|
iTem3.append(arrRes[i][2]);
|
|
iTem3.append(arrRes[i][4]);
|
|
iTem4.append(arrRes[i][3]);
|
|
iTem4.append(arrRes[i][4]);
|
|
}
|
|
jsSensorData["env_temp"] = iTem1;
|
|
jsSensorData["dev_temp"] = iTem2;
|
|
jsSensorData["battery_t"] = iTem3;
|
|
jsSensorData["rssi"] = iTem4;
|
|
jsBody.append(jsSensorData);
|
|
jsonVal["content"] = jsBody;
|
|
} else {
|
|
jsonVal["success"] = false;
|
|
jsonVal["message"] = "没有传感器信息";
|
|
jsonVal["content"].resize(0);
|
|
}
|
|
return show_value_.write(jsonVal);
|
|
}
|
|
std::string JsonData::JsonCmd_Cgi_102(){
|
|
Json::Value jsonVal;
|
|
jsonVal.clear();
|
|
jsonVal[JSON_FIELD_CMD] = "102";
|
|
jsonVal["success"] = true;
|
|
jsonVal["message"] = "获取传感器信息成功";
|
|
Json::Value jsBody;
|
|
for (const auto& [mac, info] : device_map) {
|
|
std::cout << mac << " RSSI: " << info.rssi << "\n";
|
|
|
|
Json::Value item;
|
|
item["mac"] = mac;
|
|
char whereCon[256] = {0x00};
|
|
sprintf(whereCon, "mac = '%s'", mac.c_str());
|
|
int rows = sqlite_db_ctrl::instance().GetTableRows(T_SENSOR_BT_INFO(TNAME), whereCon);
|
|
if (rows > 0) {
|
|
item["added"] = true;
|
|
}else {
|
|
item["added"] = false;
|
|
}
|
|
item["rssi"] = info.rssi;
|
|
item["type"] = "DN-801";
|
|
jsBody.append(item);
|
|
}
|
|
jsonVal["content"] = jsBody;
|
|
|
|
return show_value_.write(jsonVal);
|
|
}
|
|
std::string JsonData::JsonCmd_Cgi_103(Param_103 ¶m){
|
|
Json::Value jsonVal;
|
|
jsonVal.clear();
|
|
jsonVal[JSON_FIELD_CMD] = "103";
|
|
jsonVal["success"] = true;
|
|
jsonVal["message"] = "获取传感器信息成功";
|
|
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);
|
|
if (param.operate == "add") {
|
|
sprintf(insertSql, "'%s','','DN-801','1','%s'", param.mac.c_str(),localtimestamp);
|
|
sqlite_db_ctrl::instance().InsertData(T_SENSOR_BT_INFO(TNAME), insertSql);
|
|
} else if (param.operate == "update") {
|
|
sprintf(updateSql, "name = '%s'", param.name.c_str());
|
|
sqlite_db_ctrl::instance().UpdateTableData(T_SENSOR_BT_INFO(TNAME), updateSql,whereCon);
|
|
} else if (param.operate == "delete") {
|
|
sqlite_db_ctrl::instance().DeleteTableData(T_SENSOR_BT_INFO(TNAME), whereCon);
|
|
}
|
|
|
|
return show_value_.write(jsonVal);
|
|
}
|
|
|
|
//报警灯
|
|
std::string JsonData::JsonCmd_Cgi_110(Param_110 ¶m){
|
|
Json::Value jsonVal;
|
|
jsonVal.clear();
|
|
jsonVal[JSON_FIELD_CMD] = "110";
|
|
jsonVal["success"] = true;
|
|
jsonVal["message"] = "设置报警灯成功";
|
|
uart_inst::instance().alarmFlag = true;
|
|
if(uart_inst::instance().bUpdate){
|
|
jsonVal["success"] = false;
|
|
jsonVal["message"] = "设置报警灯失败";
|
|
return show_value_.write(jsonVal);
|
|
}
|
|
mssleep(10000);
|
|
unsigned char command[11] = {0x00};
|
|
int data = (param.flashTime << 16) | (param.alarmMode & 0xFFFF);
|
|
command[0] = 0xA5;
|
|
command[1] = 0x5A;
|
|
command[2] = 0xA5;
|
|
command[3] = 0x02;
|
|
command[4] = 0x02;
|
|
command[5] = 0x03;
|
|
command[6] = data & 0xFF;
|
|
command[7] = (data >> 8) & 0xFF;
|
|
command[8] = (data >> 16) & 0xFF;
|
|
command[9] = (data >> 24) & 0xFF;
|
|
unsigned char tmp = 0x00;
|
|
for (int i = 0; i < 10; i++) {
|
|
tmp += command[i];
|
|
}
|
|
command[10] = tmp;
|
|
uart_inst::instance().WriteToUart((const char*)command, 11);
|
|
mssleep(15000);
|
|
char recvData[12] = {0x00};
|
|
int ret = uart_inst::instance().ReadFromAlarm();
|
|
if (ret == 2 ) {
|
|
jsonVal["message"] = "设置报警灯成功";
|
|
} else {
|
|
jsonVal["success"] = false;
|
|
jsonVal["message"] = "设置报警灯失败";
|
|
}
|
|
uart_inst::instance().alarmFlag = false;
|
|
printf("alarmFlag = %d\n",uart_inst::instance().alarmFlag);
|
|
return show_value_.write(jsonVal);
|
|
} |