From eb48a6613404019f20fd5f5c1ce0ce72649d89bc Mon Sep 17 00:00:00 2001 From: zhangsheng Date: Mon, 14 Jul 2025 20:24:40 +0800 Subject: [PATCH] modify blueteeth sensor --- common/common_func.hpp | 1 + jsonparse/mqtt_cmd_parse.cpp | 27 ++++++++++++++ jsonparse/web_cmd_parse4.cpp | 69 +++++++++++++++++++----------------- localserver/web_cmd.cpp | 1 + platform/platform_init.cpp | 1 + wifi_5g/scan_blueteeth.cpp | 33 +++++++++++------ wifi_5g/scan_blueteeth.h | 4 ++- 7 files changed, 93 insertions(+), 43 deletions(-) diff --git a/common/common_func.hpp b/common/common_func.hpp index 62891b1..9b79d29 100644 --- a/common/common_func.hpp +++ b/common/common_func.hpp @@ -215,6 +215,7 @@ typedef struct { struct TopicList { std::string mPubData; //每秒特征数据上传主题 + std::string mPubBTData; //静态数据上传主题 std::string mPubStatus; //状态上传主题 std::string mPubHeart; std::string mPubConfig; //上传配置主题 diff --git a/jsonparse/mqtt_cmd_parse.cpp b/jsonparse/mqtt_cmd_parse.cpp index b7036f6..68d57c5 100644 --- a/jsonparse/mqtt_cmd_parse.cpp +++ b/jsonparse/mqtt_cmd_parse.cpp @@ -729,6 +729,33 @@ void JsonData::DataNodeStatusCheck() { } } } + array_t vetRes_bt = sqlite_db_ctrl::instance().GetDataMultiLine(T_SENSOR_BT_INFO(TNAME), "*", NULL); + int nSize_bt = vetRes_bt.size(); + if(nSize_bt > 0){ + for (int i = 0; i < nSize_bt; i++) { + std::string mac = vetRes_bt[i][0]; + char whereCon[512] = {0x00}; + sprintf(whereCon, "mac='%s' ORDER BY timeStamp DESC LIMIT 0,1", mac.c_str()); + std::string strTimeRes = sqlite_db_ctrl::instance().GetData("blueteeth_info", "timeStamp", whereCon); + if (strTimeRes.length() > 0) { + int llastTime = atoi(strTimeRes.c_str()); + int lTimeTemp = lNowTime - llastTime; + lTimeTemp = abs(lTimeTemp); + if (lTimeTemp > onlineCheck) { + zlog_warn(zct, "offline DataNodeStatusCheck mac = %s lNowTime = %d,llastTime = %d,interval = %s", mac.c_str(),lNowTime, llastTime); + char whereCon[32] = {0}; + sprintf(whereCon, "dataNodeNo='%s'", mac.c_str()); + sqlite_db_ctrl::instance().UpdateTableData(T_SENSOR_BT_INFO(TNAME), "status='0'", whereCon); + count++; + } else { + char whereCon[32] = {0}; + sprintf(whereCon, "dataNodeNo='%s'", mac.c_str()); + sqlite_db_ctrl::instance().UpdateTableData(T_SENSOR_BT_INFO(TNAME), "status='1'", whereCon); + } + } + } + } + } int JsonData::JsonCmd_07() { diff --git a/jsonparse/web_cmd_parse4.cpp b/jsonparse/web_cmd_parse4.cpp index 25038c9..36d928d 100644 --- a/jsonparse/web_cmd_parse4.cpp +++ b/jsonparse/web_cmd_parse4.cpp @@ -48,14 +48,14 @@ std::string JsonData::JsonCmd_Cgi_100(Param_100 ¶m){ Json::Value jsSensorData; jsSensorData["mac"] = arrRes[i][0]; jsSensorData["name"] = arrRes[i][1]; - jsSensorData["type"] = atoi(arrRes[i][2].c_str()); + 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); - jsSensorData["env_temp"] = atof(vec_ret[7].c_str()); + 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()); @@ -87,19 +87,19 @@ std::string JsonData::JsonCmd_Cgi_101(Param_101 ¶m){ int packgeMax = 0; int packgeNum = 0; jsonVal["package"] = packgeNo; - int lastSize = rows % 550; - int index = rows / 550; + 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)*550; + packgeNum = (packgeNo)*400; jsonVal["packageMax"] = index + 1; } } else if (lastSize == 0 && index > 0) { - packgeNum = (packgeNo + 1) * 550; + packgeNum = (packgeNo + 1) * 400; packgeMax = index; jsonVal["packageMax"] = index; } else if (lastSize > 0 && index == 0) { @@ -108,22 +108,26 @@ std::string JsonData::JsonCmd_Cgi_101(Param_101 ¶m){ 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); + 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); + zlog_info(zct, "arrRes size = %d", arrRes.size()); if (arrRes.size() > 0) { Json::Value jsSensorData; - Json::Value iTem1, iTem2, iTem3; + Json::Value iTem1, iTem2, iTem3,iTem4; for (size_t i = 0; i < arrRes.size(); i++) { iTem1.append(arrRes[i][0]); - iTem1.append(arrRes[i][3]); + iTem1.append(arrRes[i][4]); iTem2.append(arrRes[i][1]); - iTem2.append(arrRes[i][3]); + iTem2.append(arrRes[i][4]); iTem3.append(arrRes[i][2]); - iTem3.append(arrRes[i][3]); + iTem3.append(arrRes[i][4]); + iTem4.append(arrRes[i][3]); + iTem4.append(arrRes[i][4]); } jsSensorData["env_temp"] = iTem1; - jsSensorData["chip_temp"] = iTem2; - jsSensorData["volt"] = iTem3; + jsSensorData["dev_temp"] = iTem2; + jsSensorData["battery_t"] = iTem3; + jsSensorData["rssi"] = iTem4; jsBody.append(jsSensorData); jsonVal["content"] = jsBody; } else { @@ -134,30 +138,31 @@ std::string JsonData::JsonCmd_Cgi_101(Param_101 ¶m){ 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 (auto it = device_map.begin(); it != device_map.end(); ++it) - { - Json::Value jsonVal; - jsonVal.clear(); - jsonVal[JSON_FIELD_CMD] = "102"; - jsonVal["success"] = true; - jsonVal["message"] = "获取传感器信息成功"; - - Json::Value jsBody; - jsBody["mac"] = it->first; + { + Json::Value item; + item["mac"] = it->first; char whereCon[256] = {0x00}; sprintf(whereCon, "mac = '%s'", it->first.c_str()); int rows = sqlite_db_ctrl::instance().GetTableRows(T_SENSOR_BT_INFO(TNAME), whereCon); - if (rows > 1) { - jsBody["added"] = true; + if (rows > 0) { + item["added"] = true; }else { - jsBody["added"] = false; + item["added"] = false; } - jsBody["rssi"] = it->second; - jsonVal["content"] = jsBody; - - return show_value_.write(jsonVal); + item["rssi"] = it->second; + 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; @@ -171,7 +176,7 @@ std::string JsonData::JsonCmd_Cgi_103(Param_103 ¶m){ sprintf(whereCon, "mac = '%s'", param.mac.c_str()); GetTimeNet(localtimestamp, 1); if (param.operate == "add") { - sprintf(insertSql, "'%s','','DN-801','0','%s'", param.mac.c_str(),localtimestamp); + 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()); diff --git a/localserver/web_cmd.cpp b/localserver/web_cmd.cpp index 62904f6..db9fd2a 100644 --- a/localserver/web_cmd.cpp +++ b/localserver/web_cmd.cpp @@ -548,6 +548,7 @@ std::string LocalServer::HandleCgi_cmd(std::string &pData) { JsonData jd; Param_103 param; param.mac = recvBody["mac"].asString(); + param.name = recvBody["name"].asString(); param.operate = recvBody["operate"].asString(); std::string data = jd.JsonCmd_Cgi_103(param); return data; diff --git a/platform/platform_init.cpp b/platform/platform_init.cpp index b5a8993..69e4148 100644 --- a/platform/platform_init.cpp +++ b/platform/platform_init.cpp @@ -109,6 +109,7 @@ void PlatformInit::Init() { void PlatformInit::TopicInit() { GlobalConfig::Topic_G.mPubData = "wireless/statisticData/" + GlobalConfig::MacAddr_G; + GlobalConfig::Topic_G.mPubBTData = "wireless/statisticBTData/" + GlobalConfig::MacAddr_G; GlobalConfig::Topic_G.mPubStatus = "wireless/status/" + GlobalConfig::MacAddr_G; GlobalConfig::Topic_G.mPubHeart = "wireless/heart/" + GlobalConfig::MacAddr_G; GlobalConfig::Topic_G.mPubConfig = "wireless/configureInfo/" + GlobalConfig::MacAddr_G; diff --git a/wifi_5g/scan_blueteeth.cpp b/wifi_5g/scan_blueteeth.cpp index d28c373..3b1c020 100644 --- a/wifi_5g/scan_blueteeth.cpp +++ b/wifi_5g/scan_blueteeth.cpp @@ -24,6 +24,8 @@ std::map device_map; ScanBlueteeth::ScanBlueteeth() : device_id(-1), sock(-1) { retry_count = 0; max_retries = 5; + current_dev = ""; // Initialize current_dev to an empty string + ccurrent_time = ""; // Constructor initializes device_id and sock to invalid values } @@ -83,6 +85,7 @@ int ScanBlueteeth::InitDevice(){ void ScanBlueteeth::StartScan(){ while (1) { + mssleep(100); // 每次扫描间隔100毫秒 if (sock < 0 || retry_count > max_retries) { zlog_error(zct,"Reinitializing HCI device..."); @@ -127,6 +130,7 @@ void ScanBlueteeth::StartScan(){ ParseData(info->data, info->length,rssi); ptr += sizeof(le_advertising_info) + info->length + 1; // 最后的 +1 是 RSSI 字节 } + } hci_le_set_scan_enable(sock, 0x00, 0x00, 1000); @@ -181,19 +185,28 @@ void ScanBlueteeth::ParseData(unsigned char *data, int len,int8_t rssi){ char insertSql[512] = {0},whereCon[128] = {0}; char localtimestamp[32] = {0}; memset(whereCon,0,sizeof(whereCon)); - sprintf(whereCon, "mac='%s' order by timeStamp desc limit 1", mac); - std::string timestamp_last = sqlite_db_ctrl::instance().GetData(" blueteeth_info ", " timeStamp ", whereCon); GetTimeNet(localtimestamp, 1); - if (atol(localtimestamp) - atol(timestamp_last.c_str()) < 10) { - zlog_info(zct, "blueteeth_info already exist, mac = %s, timeStamp = %s", mac, timestamp_last.c_str()); - return; - } else { - sprintf(insertSql,"'%s','%s','%d',%d,'%.1f','%.1f','%.1f','%.1f','%s'",device_id,mac,rssi,count,volt_,blueteeth_temp,chip_temp,env_temp,localtimestamp); - sqlite_db_ctrl::instance().InsertData(" blueteeth_info ", insertSql); + if (current_dev == mac && atol(localtimestamp) - atol(ccurrent_time.c_str()) < 10) { + zlog_info(zct, "blueteeth_info already exist, mac = %s", mac); + return; // 如果当前设备已经存在,则不再插入 + } else{ + current_dev = mac; // 更新当前设备 } + ccurrent_time = std::string(localtimestamp); + sprintf(whereCon, "mac='%s'", mac); + int count_ = sqlite_db_ctrl::instance().GetTableRows(T_SENSOR_BT_INFO(TNAME), whereCon); + if (count_ < 1) { + zlog_info(zct, "device not exist, mac = %s", mac); + return; + } else { + 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); + int ret = sqlite_db_ctrl::instance().InsertData(" blueteeth_info ", insertSql); Json::Value root; Json::Value valdatastatic; - valdatastatic["TemperatureTop"] = env_temp; + valdatastatic["TemperatureTop"] = blueteeth_temp; valdatastatic["TemperatureBot"] = chip_temp; valdatastatic["Voltage"] = volt_; valdatastatic["ChannelId"] = mac; @@ -204,7 +217,7 @@ void ScanBlueteeth::ParseData(unsigned char *data, int len,int8_t rssi){ Json::FastWriter featureValue; std::string strstatisticData = featureValue.write(root); - int iRet = data_publish(strstatisticData.c_str(), GlobalConfig::Topic_G.mPubData.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; } diff --git a/wifi_5g/scan_blueteeth.h b/wifi_5g/scan_blueteeth.h index 1aabea3..c386811 100644 --- a/wifi_5g/scan_blueteeth.h +++ b/wifi_5g/scan_blueteeth.h @@ -2,7 +2,7 @@ #define SCAN_BLUETEETH_H_ typedef signed char int8_t; - +#include class ScanBlueteeth { public: ScanBlueteeth(); @@ -14,6 +14,8 @@ public: int sock; int retry_count ; int max_retries ; + std::string current_dev; + std::string ccurrent_time; void ParseData(unsigned char *data, int len,int8_t rssi); };