fix bugs
This commit is contained in:
parent
17f11d876d
commit
100375db5f
@ -431,7 +431,7 @@ std::string JsonData::JsonCmd_Cgi_20(Param_20 ¶m) {
|
|||||||
}
|
}
|
||||||
if (GlobalConfig::NetStatus == "\"NOCONN\"" || GlobalConfig::NetStatus == "\"CONNECT\"") {
|
if (GlobalConfig::NetStatus == "\"NOCONN\"" || GlobalConfig::NetStatus == "\"CONNECT\"") {
|
||||||
if (GlobalConfig::NetSignal == 0) {
|
if (GlobalConfig::NetSignal == 0) {
|
||||||
jsSystemInfo["communicationSignal"] = GlobalConfig::NetType + ",未知";
|
jsSystemInfo["communicationSignal"] = GlobalConfig::NetType + ",unknown";
|
||||||
} else if (GlobalConfig::NetSignal > -80) {
|
} else if (GlobalConfig::NetSignal > -80) {
|
||||||
jsSystemInfo["communicationSignal"] = GlobalConfig::NetType + ",优";
|
jsSystemInfo["communicationSignal"] = GlobalConfig::NetType + ",优";
|
||||||
} else if (GlobalConfig::NetSignal > -90 && GlobalConfig::NetSignal < -80) {
|
} else if (GlobalConfig::NetSignal > -90 && GlobalConfig::NetSignal < -80) {
|
||||||
@ -449,7 +449,7 @@ std::string JsonData::JsonCmd_Cgi_20(Param_20 ¶m) {
|
|||||||
} else if (GlobalConfig::NetStatus == "\"LIMSRV\"") {
|
} else if (GlobalConfig::NetStatus == "\"LIMSRV\"") {
|
||||||
jsSystemInfo["communicationSignal"] = GlobalConfig::NetType + ",未插卡";
|
jsSystemInfo["communicationSignal"] = GlobalConfig::NetType + ",未插卡";
|
||||||
} else {
|
} else {
|
||||||
jsSystemInfo["communicationSignal"] = "未知";
|
jsSystemInfo["communicationSignal"] = "unknown";
|
||||||
}
|
}
|
||||||
|
|
||||||
jsBody["SystemInfo"] = jsSystemInfo;
|
jsBody["SystemInfo"] = jsSystemInfo;
|
||||||
|
|||||||
@ -5,10 +5,12 @@
|
|||||||
#include "common/global.hpp"
|
#include "common/global.hpp"
|
||||||
#include "dbaccess/sql_db.hpp"
|
#include "dbaccess/sql_db.hpp"
|
||||||
#include "platform/platform_init.hpp"
|
#include "platform/platform_init.hpp"
|
||||||
|
#include "wifi_5g/scan_blueteeth.h"
|
||||||
|
|
||||||
extern zlog_category_t *zct;
|
extern zlog_category_t *zct;
|
||||||
extern const char *JSON_FIELD_CMD;
|
extern const char *JSON_FIELD_CMD;
|
||||||
extern std::map<std::string, int8_t> device_map;
|
extern std::unordered_map<std::string, DeviceInfo> device_map;;
|
||||||
|
|
||||||
std::string JsonData::JsonCmd_Cgi_100(Param_100 ¶m){
|
std::string JsonData::JsonCmd_Cgi_100(Param_100 ¶m){
|
||||||
Json::Value jsonVal;
|
Json::Value jsonVal;
|
||||||
jsonVal.clear();
|
jsonVal.clear();
|
||||||
@ -55,11 +57,16 @@ std::string JsonData::JsonCmd_Cgi_100(Param_100 ¶m){
|
|||||||
char whereCon[256] = {0x00};
|
char whereCon[256] = {0x00};
|
||||||
sprintf(whereCon, "mac = '%s' order by timestamp desc limit 1 ", strMac.c_str());
|
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);
|
vec_t vec_ret = sqlite_db_ctrl::instance().GetDataSingleLine(" blueteeth_info ", "*", whereCon);
|
||||||
jsSensorData["env_temp"] = atof(vec_ret[5].c_str());
|
if (vec_ret.size() < 1) {
|
||||||
jsSensorData["dev_temp"] = atof(vec_ret[6].c_str());
|
zlog_error(zct, "bluetooth info not enough data, mac = %s", strMac.c_str());
|
||||||
jsSensorData["battery_v"] = atof(vec_ret[4].c_str());
|
continue;
|
||||||
jsSensorData["rssi"] = atoi(vec_ret[2].c_str());
|
}
|
||||||
jsSensorData["feature_date"] = vec_ret[8];
|
|
||||||
|
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);
|
jsBody.append(jsSensorData);
|
||||||
}
|
}
|
||||||
jsonVal["content"] = jsBody;
|
jsonVal["content"] = jsBody;
|
||||||
@ -71,6 +78,7 @@ std::string JsonData::JsonCmd_Cgi_100(Param_100 ¶m){
|
|||||||
|
|
||||||
return show_value_.write(jsonVal);
|
return show_value_.write(jsonVal);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string JsonData::JsonCmd_Cgi_101(Param_101 ¶m){
|
std::string JsonData::JsonCmd_Cgi_101(Param_101 ¶m){
|
||||||
Json::Value jsonVal;
|
Json::Value jsonVal;
|
||||||
jsonVal.clear();
|
jsonVal.clear();
|
||||||
@ -79,7 +87,8 @@ std::string JsonData::JsonCmd_Cgi_101(Param_101 ¶m){
|
|||||||
jsonVal["message"] = "获取传感器信息成功";
|
jsonVal["message"] = "获取传感器信息成功";
|
||||||
|
|
||||||
Json::Value jsBody;
|
Json::Value jsBody;
|
||||||
|
double min = -102;
|
||||||
|
double max = -60;
|
||||||
char selectCon[256] = {0}, szTableName[100] = {0x00}, whereCon[256] = {0x00};
|
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());
|
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 rows = sqlite_db_ctrl::instance().GetTableRows(" blueteeth_info ", whereCon);
|
||||||
@ -109,7 +118,7 @@ std::string JsonData::JsonCmd_Cgi_101(Param_101 ¶m){
|
|||||||
}
|
}
|
||||||
memset(whereCon, 0x00, sizeof(whereCon));
|
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);
|
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,timestamp", whereCon);
|
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());
|
zlog_info(zct, "arrRes size = %d", arrRes.size());
|
||||||
if (arrRes.size() > 0) {
|
if (arrRes.size() > 0) {
|
||||||
Json::Value jsSensorData;
|
Json::Value jsSensorData;
|
||||||
@ -144,19 +153,20 @@ std::string JsonData::JsonCmd_Cgi_102(){
|
|||||||
jsonVal["success"] = true;
|
jsonVal["success"] = true;
|
||||||
jsonVal["message"] = "获取传感器信息成功";
|
jsonVal["message"] = "获取传感器信息成功";
|
||||||
Json::Value jsBody;
|
Json::Value jsBody;
|
||||||
for (auto it = device_map.begin(); it != device_map.end(); ++it)
|
for (const auto& [mac, info] : device_map) {
|
||||||
{
|
std::cout << mac << " RSSI: " << info.rssi << "\n";
|
||||||
|
|
||||||
Json::Value item;
|
Json::Value item;
|
||||||
item["mac"] = it->first;
|
item["mac"] = mac;
|
||||||
char whereCon[256] = {0x00};
|
char whereCon[256] = {0x00};
|
||||||
sprintf(whereCon, "mac = '%s'", it->first.c_str());
|
sprintf(whereCon, "mac = '%s'", mac.c_str());
|
||||||
int rows = sqlite_db_ctrl::instance().GetTableRows(T_SENSOR_BT_INFO(TNAME), whereCon);
|
int rows = sqlite_db_ctrl::instance().GetTableRows(T_SENSOR_BT_INFO(TNAME), whereCon);
|
||||||
if (rows > 0) {
|
if (rows > 0) {
|
||||||
item["added"] = true;
|
item["added"] = true;
|
||||||
}else {
|
}else {
|
||||||
item["added"] = false;
|
item["added"] = false;
|
||||||
}
|
}
|
||||||
item["rssi"] = it->second;
|
item["rssi"] = info.rssi;
|
||||||
item["type"] = "DN-801";
|
item["type"] = "DN-801";
|
||||||
jsBody.append(item);
|
jsBody.append(item);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -19,8 +19,49 @@
|
|||||||
|
|
||||||
extern zlog_category_t *zct;
|
extern zlog_category_t *zct;
|
||||||
|
|
||||||
std::map<std::string, int8_t> device_map;
|
std::unordered_map<std::string, DeviceInfo> device_map;
|
||||||
|
std::mutex map_mutex;
|
||||||
|
|
||||||
|
|
||||||
|
double min = -102;
|
||||||
|
double max = -60;
|
||||||
|
// 将信号值 x 映射为百分比 [0, 100]
|
||||||
|
double signalToPercent(double x, double min, double max) {
|
||||||
|
if (x < min) x = min;
|
||||||
|
if (x > max) x = max;
|
||||||
|
return (x - min) / (max - min) * 100.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将百分比 [0, 100] 还原为信号值
|
||||||
|
double percentToSignal(double percent, double min, double max) {
|
||||||
|
if (percent < 0) percent = 0;
|
||||||
|
if (percent > 100) percent = 100;
|
||||||
|
return min + (percent / 100.0) * (max - min);
|
||||||
|
}
|
||||||
|
// 添加或更新设备信息
|
||||||
|
void updateDevice(const std::string& mac, int rssi) {
|
||||||
|
std::lock_guard<std::mutex> lock(map_mutex);
|
||||||
|
device_map[mac] = {
|
||||||
|
rssi,
|
||||||
|
steady_clock::now()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// 清除超过 timeout_ms 毫秒未更新的设备
|
||||||
|
void removeStaleDevices(int timeout_ms = 1000) {
|
||||||
|
std::lock_guard<std::mutex> lock(map_mutex);
|
||||||
|
auto now = steady_clock::now();
|
||||||
|
|
||||||
|
for (auto it = device_map.begin(); it != device_map.end(); ) {
|
||||||
|
auto elapsed = duration_cast<milliseconds>(now - it->second.last_seen).count();
|
||||||
|
if (elapsed > timeout_ms) {
|
||||||
|
std::cout << "[Remove] " << it->first << " (stale: " << elapsed << "ms)\n";
|
||||||
|
it = device_map.erase(it);
|
||||||
|
} else {
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
ScanBlueteeth::ScanBlueteeth() : device_id(-1), sock(-1) {
|
ScanBlueteeth::ScanBlueteeth() : device_id(-1), sock(-1) {
|
||||||
retry_count = 0;
|
retry_count = 0;
|
||||||
max_retries = 5;
|
max_retries = 5;
|
||||||
@ -179,7 +220,9 @@ void ScanBlueteeth::ParseData(unsigned char *data, int len,int8_t rssi){
|
|||||||
printf("%02x ", data[k]);
|
printf("%02x ", data[k]);
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
double rssi_percent = signalToPercent((double)rssi, min, max);
|
||||||
|
updateDevice(std::string(mac), (int)rssi_percent);
|
||||||
|
removeStaleDevices(1000*300); // 清除超过5分钟未更新的设备
|
||||||
//printf("time %s Bluetooth temp :%f ℃ , NST 1001 temp: %f ℃\n",GetCurrentTime().c_str(),(float)temp*0.01, ((float)temp1*0.0625) - 50.0625);
|
//printf("time %s Bluetooth temp :%f ℃ , NST 1001 temp: %f ℃\n",GetCurrentTime().c_str(),(float)temp*0.01, ((float)temp1*0.0625) - 50.0625);
|
||||||
//printf(" volt : %f v\n", (double)volt * 0.03125 );
|
//printf(" volt : %f v\n", (double)volt * 0.03125 );
|
||||||
char insertSql[512] = {0},whereCon[128] = {0};
|
char insertSql[512] = {0},whereCon[128] = {0};
|
||||||
@ -202,7 +245,7 @@ void ScanBlueteeth::ParseData(unsigned char *data, int len,int8_t rssi){
|
|||||||
zlog_info(zct, "device already exist, mac = %s", mac);
|
zlog_info(zct, "device already exist, mac = %s", mac);
|
||||||
}
|
}
|
||||||
|
|
||||||
sprintf(insertSql,"'%s','%s','%d',%d,'%.1f','%.1f','%.1f','%.1f','%s'",device_id,mac,rssi,count,volt_,blueteeth_temp,chip_temp,env_temp,localtimestamp);
|
sprintf(insertSql,"'%s','%s','%d','%d',%d,'%.1f','%.1f','%.1f','%.1f','%s'",device_id,mac,rssi,(int)rssi_percent,count,volt_,blueteeth_temp,chip_temp,env_temp,localtimestamp);
|
||||||
int ret = sqlite_db_ctrl::instance().InsertData(" blueteeth_info ", insertSql);
|
int ret = sqlite_db_ctrl::instance().InsertData(" blueteeth_info ", insertSql);
|
||||||
Json::Value root;
|
Json::Value root;
|
||||||
Json::Value valdatastatic;
|
Json::Value valdatastatic;
|
||||||
@ -218,7 +261,7 @@ void ScanBlueteeth::ParseData(unsigned char *data, int len,int8_t rssi){
|
|||||||
std::string strstatisticData = featureValue.write(root);
|
std::string strstatisticData = featureValue.write(root);
|
||||||
|
|
||||||
int iRet = data_publish(strstatisticData.c_str(), GlobalConfig::Topic_G.mPubBTData.c_str());
|
int iRet = data_publish(strstatisticData.c_str(), GlobalConfig::Topic_G.mPubBTData.c_str());
|
||||||
device_map.insert(std::make_pair(std::string(mac), rssi));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,6 +3,18 @@
|
|||||||
|
|
||||||
typedef signed char int8_t;
|
typedef signed char int8_t;
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <iostream>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <chrono>
|
||||||
|
#include <thread>
|
||||||
|
#include <string>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
|
using namespace std::chrono;
|
||||||
|
struct DeviceInfo {
|
||||||
|
double rssi;
|
||||||
|
steady_clock::time_point last_seen;
|
||||||
|
};
|
||||||
class ScanBlueteeth {
|
class ScanBlueteeth {
|
||||||
public:
|
public:
|
||||||
ScanBlueteeth();
|
ScanBlueteeth();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user