174 lines
6.8 KiB
C++
174 lines
6.8 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"
|
|
|
|
extern zlog_category_t *zct;
|
|
extern const char *JSON_FIELD_CMD;
|
|
extern std::map<std::string, std::string> 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);
|
|
|
|
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 packgeNo * 10; j < packgeNum; i++) {
|
|
Json::Value jsSensorData;
|
|
jsSensorData["mac"] = arrRes[i][0];
|
|
jsSensorData["name"] = arrRes[i][1];
|
|
jsSensorData["type"] = atoi(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];
|
|
vec_t vec_ret = sqlite_db_ctrl::instance().GetDataMultiLine(" blueteeth_info ", "*", " oder by timestamp desc limit 1 ");
|
|
jsSensorData["env_temp"] = atof(vec_ret[7].c_str());
|
|
jsSensorData["dev_temp"] = atof(vec_ret[6].c_str());
|
|
jsSensorData["battery_v"] = atof(vec_ret[4].c_str());
|
|
jsSensorData["rssi"] = atoi(vec_ret[2].c_str());
|
|
jsSensorData["feature_date"] = vec_ret[i][8];
|
|
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;
|
|
|
|
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 % 550;
|
|
int index = rows / 550;
|
|
if (lastSize > 0 && index > 0) {
|
|
packgeMax = index + 1;
|
|
if (packgeNo + 1 == packgeMax) {
|
|
packgeNum = rows - lastSize;
|
|
jsonVal["packageMax"] = index + 1;
|
|
} else {
|
|
packgeNum = (packgeNo)*550;
|
|
jsonVal["packageMax"] = index + 1;
|
|
}
|
|
} else if (lastSize == 0 && index > 0) {
|
|
packgeNum = (packgeNo + 1) * 550;
|
|
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(), 550, packgeNum);
|
|
array_t arrRes = sqlite_db_ctrl::instance().GetDataMultiLine(" blueteeth_info ", "env_temp,chip_temp,volt,timestamp", whereCon);
|
|
zlog_info(zct, "vecRes = %d", vecRes.size());
|
|
if (vecRes.size() > 0) {
|
|
Json::Value jsSensorData;
|
|
for (size_t i = 0; i < vecRes.size(); i++) {
|
|
|
|
Json::Value iTem1, iTem2, iTem3;
|
|
iTem1.append(vecRes[i][0]);
|
|
iTem1.append(vecRes[i][3]);
|
|
iTem2.append(vecRes[i][1]);
|
|
iTem2.append(vecRes[i][3]);
|
|
iTem3.append(vecRes[i][2]);
|
|
iTem3.append(vecRes[i][3]);
|
|
}
|
|
jsSensorData["env_temp"] = iTem1;
|
|
jsSensorData["chip_temp"] = iTem2;
|
|
jsSensorData["volt"] = iTem3;
|
|
jsBody.append(jsSensorData);
|
|
jsonVal["content"] = jsBody;
|
|
} else {
|
|
jsonVal["success"] = false;
|
|
jsonVal["message"] = "没有传感器信息";
|
|
jsonVal["content"].resize(0);
|
|
}
|
|
}
|
|
std::string JsonData::JsonCmd_Cgi_102(){
|
|
for (size_t i = 0; i < device_map.size; i++)
|
|
{
|
|
Json::Value jsonVal;
|
|
jsonVal.clear();
|
|
jsonVal[JSON_FIELD_CMD] = "102";
|
|
jsonVal["success"] = true;
|
|
jsonVal["message"] = "获取传感器信息成功";
|
|
|
|
Json::Value jsBody;
|
|
jsBody["mac"] = device_map[i].first;
|
|
sqlite_db_ctrl::instance().GetRowCount(T_SENSOR_BT_INFO(TNAME), "count(*)", "mac = '" + device_map[i].first + "'");
|
|
if (rows > 1) {
|
|
jsBody["added"] = true;
|
|
}
|
|
jsBody["rssi"] = device_map[i].second;
|
|
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"] = "获取传感器信息成功";
|
|
|
|
if (param.operate == "add") {
|
|
sqlite_db_ctrl::instance().InsertData(T_SENSOR_BT_INFO(TNAME), "mac,name,type,status,config_date",
|
|
"'" + param.mac + "','" + param.name + "'," + std::to_string(param.type) + "," + std::to_string(param.status) + "," + std::to_string(param.config_date));
|
|
} else if (param.operate == "update") {
|
|
sqlite_db_ctrl::instance().UpdateTableData(T_SENSOR_BT_INFO(TNAME), "name = '" + param.name ,"mac = '" + param.mac + "'");
|
|
} else if (param.operate == "delete") {
|
|
sqlite_db_ctrl::instance().DeleteData(T_SENSOR_BT_INFO(TNAME), "mac = '" + param.mac + "'");
|
|
}
|
|
|
|
return show_value_.write(jsonVal);
|
|
} |