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::NetSignal == 0) {
|
||||
jsSystemInfo["communicationSignal"] = GlobalConfig::NetType + ",未知";
|
||||
jsSystemInfo["communicationSignal"] = GlobalConfig::NetType + ",unknown";
|
||||
} else if (GlobalConfig::NetSignal > -80) {
|
||||
jsSystemInfo["communicationSignal"] = GlobalConfig::NetType + ",优";
|
||||
} 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\"") {
|
||||
jsSystemInfo["communicationSignal"] = GlobalConfig::NetType + ",未插卡";
|
||||
} else {
|
||||
jsSystemInfo["communicationSignal"] = "未知";
|
||||
jsSystemInfo["communicationSignal"] = "unknown";
|
||||
}
|
||||
|
||||
jsBody["SystemInfo"] = jsSystemInfo;
|
||||
|
||||
@ -5,10 +5,12 @@
|
||||
#include "common/global.hpp"
|
||||
#include "dbaccess/sql_db.hpp"
|
||||
#include "platform/platform_init.hpp"
|
||||
#include "wifi_5g/scan_blueteeth.h"
|
||||
|
||||
extern zlog_category_t *zct;
|
||||
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){
|
||||
Json::Value jsonVal;
|
||||
jsonVal.clear();
|
||||
@ -55,11 +57,16 @@ std::string JsonData::JsonCmd_Cgi_100(Param_100 ¶m){
|
||||
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);
|
||||
jsSensorData["env_temp"] = atof(vec_ret[5].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[8];
|
||||
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;
|
||||
@ -71,6 +78,7 @@ std::string JsonData::JsonCmd_Cgi_100(Param_100 ¶m){
|
||||
|
||||
return show_value_.write(jsonVal);
|
||||
}
|
||||
|
||||
std::string JsonData::JsonCmd_Cgi_101(Param_101 ¶m){
|
||||
Json::Value jsonVal;
|
||||
jsonVal.clear();
|
||||
@ -79,7 +87,8 @@ std::string JsonData::JsonCmd_Cgi_101(Param_101 ¶m){
|
||||
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);
|
||||
@ -109,7 +118,7 @@ std::string JsonData::JsonCmd_Cgi_101(Param_101 ¶m){
|
||||
}
|
||||
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,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());
|
||||
if (arrRes.size() > 0) {
|
||||
Json::Value jsSensorData;
|
||||
@ -144,19 +153,20 @@ std::string JsonData::JsonCmd_Cgi_102(){
|
||||
jsonVal["success"] = true;
|
||||
jsonVal["message"] = "获取传感器信息成功";
|
||||
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;
|
||||
item["mac"] = it->first;
|
||||
item["mac"] = mac;
|
||||
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);
|
||||
if (rows > 0) {
|
||||
item["added"] = true;
|
||||
}else {
|
||||
item["added"] = false;
|
||||
}
|
||||
item["rssi"] = it->second;
|
||||
item["rssi"] = info.rssi;
|
||||
item["type"] = "DN-801";
|
||||
jsBody.append(item);
|
||||
}
|
||||
|
||||
@ -19,8 +19,49 @@
|
||||
|
||||
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) {
|
||||
retry_count = 0;
|
||||
max_retries = 5;
|
||||
@ -179,7 +220,9 @@ void ScanBlueteeth::ParseData(unsigned char *data, int len,int8_t rssi){
|
||||
printf("%02x ", data[k]);
|
||||
}
|
||||
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(" volt : %f v\n", (double)volt * 0.03125 );
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
Json::Value root;
|
||||
Json::Value valdatastatic;
|
||||
@ -218,7 +261,7 @@ void ScanBlueteeth::ParseData(unsigned char *data, int len,int8_t rssi){
|
||||
std::string strstatisticData = featureValue.write(root);
|
||||
|
||||
int iRet = data_publish(strstatisticData.c_str(), GlobalConfig::Topic_G.mPubBTData.c_str());
|
||||
device_map.insert(std::make_pair(std::string(mac), rssi));
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,6 +3,18 @@
|
||||
|
||||
typedef signed char int8_t;
|
||||
#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 {
|
||||
public:
|
||||
ScanBlueteeth();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user