modify blueteeth sensor
This commit is contained in:
parent
41fb6ab706
commit
eb48a66134
@ -215,6 +215,7 @@ typedef struct {
|
|||||||
|
|
||||||
struct TopicList {
|
struct TopicList {
|
||||||
std::string mPubData; //每秒特征数据上传主题
|
std::string mPubData; //每秒特征数据上传主题
|
||||||
|
std::string mPubBTData; //静态数据上传主题
|
||||||
std::string mPubStatus; //状态上传主题
|
std::string mPubStatus; //状态上传主题
|
||||||
std::string mPubHeart;
|
std::string mPubHeart;
|
||||||
std::string mPubConfig; //上传配置主题
|
std::string mPubConfig; //上传配置主题
|
||||||
|
|||||||
@ -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() {
|
int JsonData::JsonCmd_07() {
|
||||||
|
|||||||
@ -48,14 +48,14 @@ std::string JsonData::JsonCmd_Cgi_100(Param_100 ¶m){
|
|||||||
Json::Value jsSensorData;
|
Json::Value jsSensorData;
|
||||||
jsSensorData["mac"] = arrRes[i][0];
|
jsSensorData["mac"] = arrRes[i][0];
|
||||||
jsSensorData["name"] = arrRes[i][1];
|
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["status"] = atoi(arrRes[i][3].c_str());
|
||||||
jsSensorData["config_date"] = atoi(arrRes[i][4].c_str());
|
jsSensorData["config_date"] = atoi(arrRes[i][4].c_str());
|
||||||
std::string strMac = arrRes[i][0];
|
std::string strMac = arrRes[i][0];
|
||||||
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[7].c_str());
|
jsSensorData["env_temp"] = atof(vec_ret[5].c_str());
|
||||||
jsSensorData["dev_temp"] = atof(vec_ret[6].c_str());
|
jsSensorData["dev_temp"] = atof(vec_ret[6].c_str());
|
||||||
jsSensorData["battery_v"] = atof(vec_ret[4].c_str());
|
jsSensorData["battery_v"] = atof(vec_ret[4].c_str());
|
||||||
jsSensorData["rssi"] = atoi(vec_ret[2].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 packgeMax = 0;
|
||||||
int packgeNum = 0;
|
int packgeNum = 0;
|
||||||
jsonVal["package"] = packgeNo;
|
jsonVal["package"] = packgeNo;
|
||||||
int lastSize = rows % 550;
|
int lastSize = rows % 400;
|
||||||
int index = rows / 550;
|
int index = rows / 400;
|
||||||
if (lastSize > 0 && index > 0) {
|
if (lastSize > 0 && index > 0) {
|
||||||
packgeMax = index + 1;
|
packgeMax = index + 1;
|
||||||
if (packgeNo + 1 == packgeMax) {
|
if (packgeNo + 1 == packgeMax) {
|
||||||
packgeNum = rows - lastSize;
|
packgeNum = rows - lastSize;
|
||||||
jsonVal["packageMax"] = index + 1;
|
jsonVal["packageMax"] = index + 1;
|
||||||
} else {
|
} else {
|
||||||
packgeNum = (packgeNo)*550;
|
packgeNum = (packgeNo)*400;
|
||||||
jsonVal["packageMax"] = index + 1;
|
jsonVal["packageMax"] = index + 1;
|
||||||
}
|
}
|
||||||
} else if (lastSize == 0 && index > 0) {
|
} else if (lastSize == 0 && index > 0) {
|
||||||
packgeNum = (packgeNo + 1) * 550;
|
packgeNum = (packgeNo + 1) * 400;
|
||||||
packgeMax = index;
|
packgeMax = index;
|
||||||
jsonVal["packageMax"] = index;
|
jsonVal["packageMax"] = index;
|
||||||
} else if (lastSize > 0 && index == 0) {
|
} else if (lastSize > 0 && index == 0) {
|
||||||
@ -108,22 +108,26 @@ std::string JsonData::JsonCmd_Cgi_101(Param_101 ¶m){
|
|||||||
jsonVal["packageMax"] = index;
|
jsonVal["packageMax"] = index;
|
||||||
}
|
}
|
||||||
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(), 550, 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 ", "env_temp,chip_temp,volt,timestamp", whereCon);
|
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) {
|
if (arrRes.size() > 0) {
|
||||||
Json::Value jsSensorData;
|
Json::Value jsSensorData;
|
||||||
Json::Value iTem1, iTem2, iTem3;
|
Json::Value iTem1, iTem2, iTem3,iTem4;
|
||||||
for (size_t i = 0; i < arrRes.size(); i++) {
|
for (size_t i = 0; i < arrRes.size(); i++) {
|
||||||
iTem1.append(arrRes[i][0]);
|
iTem1.append(arrRes[i][0]);
|
||||||
iTem1.append(arrRes[i][3]);
|
iTem1.append(arrRes[i][4]);
|
||||||
iTem2.append(arrRes[i][1]);
|
iTem2.append(arrRes[i][1]);
|
||||||
iTem2.append(arrRes[i][3]);
|
iTem2.append(arrRes[i][4]);
|
||||||
iTem3.append(arrRes[i][2]);
|
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["env_temp"] = iTem1;
|
||||||
jsSensorData["chip_temp"] = iTem2;
|
jsSensorData["dev_temp"] = iTem2;
|
||||||
jsSensorData["volt"] = iTem3;
|
jsSensorData["battery_t"] = iTem3;
|
||||||
|
jsSensorData["rssi"] = iTem4;
|
||||||
jsBody.append(jsSensorData);
|
jsBody.append(jsSensorData);
|
||||||
jsonVal["content"] = jsBody;
|
jsonVal["content"] = jsBody;
|
||||||
} else {
|
} else {
|
||||||
@ -134,30 +138,31 @@ std::string JsonData::JsonCmd_Cgi_101(Param_101 ¶m){
|
|||||||
return show_value_.write(jsonVal);
|
return show_value_.write(jsonVal);
|
||||||
}
|
}
|
||||||
std::string JsonData::JsonCmd_Cgi_102(){
|
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)
|
for (auto it = device_map.begin(); it != device_map.end(); ++it)
|
||||||
{
|
{
|
||||||
Json::Value jsonVal;
|
Json::Value item;
|
||||||
jsonVal.clear();
|
item["mac"] = it->first;
|
||||||
jsonVal[JSON_FIELD_CMD] = "102";
|
|
||||||
jsonVal["success"] = true;
|
|
||||||
jsonVal["message"] = "获取传感器信息成功";
|
|
||||||
|
|
||||||
Json::Value jsBody;
|
|
||||||
jsBody["mac"] = it->first;
|
|
||||||
char whereCon[256] = {0x00};
|
char whereCon[256] = {0x00};
|
||||||
sprintf(whereCon, "mac = '%s'", it->first.c_str());
|
sprintf(whereCon, "mac = '%s'", it->first.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 > 1) {
|
if (rows > 0) {
|
||||||
jsBody["added"] = true;
|
item["added"] = true;
|
||||||
}else {
|
}else {
|
||||||
jsBody["added"] = false;
|
item["added"] = false;
|
||||||
}
|
}
|
||||||
jsBody["rssi"] = it->second;
|
item["rssi"] = it->second;
|
||||||
jsonVal["content"] = jsBody;
|
item["type"] = "DN-801";
|
||||||
|
jsBody.append(item);
|
||||||
return show_value_.write(jsonVal);
|
|
||||||
}
|
}
|
||||||
|
jsonVal["content"] = jsBody;
|
||||||
|
|
||||||
|
return show_value_.write(jsonVal);
|
||||||
}
|
}
|
||||||
std::string JsonData::JsonCmd_Cgi_103(Param_103 ¶m){
|
std::string JsonData::JsonCmd_Cgi_103(Param_103 ¶m){
|
||||||
Json::Value jsonVal;
|
Json::Value jsonVal;
|
||||||
@ -171,7 +176,7 @@ std::string JsonData::JsonCmd_Cgi_103(Param_103 ¶m){
|
|||||||
sprintf(whereCon, "mac = '%s'", param.mac.c_str());
|
sprintf(whereCon, "mac = '%s'", param.mac.c_str());
|
||||||
GetTimeNet(localtimestamp, 1);
|
GetTimeNet(localtimestamp, 1);
|
||||||
if (param.operate == "add") {
|
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);
|
sqlite_db_ctrl::instance().InsertData(T_SENSOR_BT_INFO(TNAME), insertSql);
|
||||||
} else if (param.operate == "update") {
|
} else if (param.operate == "update") {
|
||||||
sprintf(updateSql, "name = '%s'", param.name.c_str());
|
sprintf(updateSql, "name = '%s'", param.name.c_str());
|
||||||
|
|||||||
@ -548,6 +548,7 @@ std::string LocalServer::HandleCgi_cmd(std::string &pData) {
|
|||||||
JsonData jd;
|
JsonData jd;
|
||||||
Param_103 param;
|
Param_103 param;
|
||||||
param.mac = recvBody["mac"].asString();
|
param.mac = recvBody["mac"].asString();
|
||||||
|
param.name = recvBody["name"].asString();
|
||||||
param.operate = recvBody["operate"].asString();
|
param.operate = recvBody["operate"].asString();
|
||||||
std::string data = jd.JsonCmd_Cgi_103(param);
|
std::string data = jd.JsonCmd_Cgi_103(param);
|
||||||
return data;
|
return data;
|
||||||
|
|||||||
@ -109,6 +109,7 @@ void PlatformInit::Init() {
|
|||||||
|
|
||||||
void PlatformInit::TopicInit() {
|
void PlatformInit::TopicInit() {
|
||||||
GlobalConfig::Topic_G.mPubData = "wireless/statisticData/" + GlobalConfig::MacAddr_G;
|
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.mPubStatus = "wireless/status/" + GlobalConfig::MacAddr_G;
|
||||||
GlobalConfig::Topic_G.mPubHeart = "wireless/heart/" + GlobalConfig::MacAddr_G;
|
GlobalConfig::Topic_G.mPubHeart = "wireless/heart/" + GlobalConfig::MacAddr_G;
|
||||||
GlobalConfig::Topic_G.mPubConfig = "wireless/configureInfo/" + GlobalConfig::MacAddr_G;
|
GlobalConfig::Topic_G.mPubConfig = "wireless/configureInfo/" + GlobalConfig::MacAddr_G;
|
||||||
|
|||||||
@ -24,6 +24,8 @@ std::map<std::string, int8_t> device_map;
|
|||||||
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;
|
||||||
|
current_dev = ""; // Initialize current_dev to an empty string
|
||||||
|
ccurrent_time = "";
|
||||||
// Constructor initializes device_id and sock to invalid values
|
// Constructor initializes device_id and sock to invalid values
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -83,6 +85,7 @@ int ScanBlueteeth::InitDevice(){
|
|||||||
|
|
||||||
void ScanBlueteeth::StartScan(){
|
void ScanBlueteeth::StartScan(){
|
||||||
while (1) {
|
while (1) {
|
||||||
|
mssleep(100); // 每次扫描间隔100毫秒
|
||||||
if (sock < 0 || retry_count > max_retries) {
|
if (sock < 0 || retry_count > max_retries) {
|
||||||
zlog_error(zct,"Reinitializing HCI device...");
|
zlog_error(zct,"Reinitializing HCI device...");
|
||||||
|
|
||||||
@ -127,6 +130,7 @@ void ScanBlueteeth::StartScan(){
|
|||||||
ParseData(info->data, info->length,rssi);
|
ParseData(info->data, info->length,rssi);
|
||||||
ptr += sizeof(le_advertising_info) + info->length + 1; // 最后的 +1 是 RSSI 字节
|
ptr += sizeof(le_advertising_info) + info->length + 1; // 最后的 +1 是 RSSI 字节
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
hci_le_set_scan_enable(sock, 0x00, 0x00, 1000);
|
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 insertSql[512] = {0},whereCon[128] = {0};
|
||||||
char localtimestamp[32] = {0};
|
char localtimestamp[32] = {0};
|
||||||
memset(whereCon,0,sizeof(whereCon));
|
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);
|
GetTimeNet(localtimestamp, 1);
|
||||||
if (atol(localtimestamp) - atol(timestamp_last.c_str()) < 10) {
|
if (current_dev == mac && atol(localtimestamp) - atol(ccurrent_time.c_str()) < 10) {
|
||||||
zlog_info(zct, "blueteeth_info already exist, mac = %s, timeStamp = %s", mac, timestamp_last.c_str());
|
zlog_info(zct, "blueteeth_info already exist, mac = %s", mac);
|
||||||
return;
|
return; // 如果当前设备已经存在,则不再插入
|
||||||
} else {
|
} 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);
|
current_dev = mac; // 更新当前设备
|
||||||
sqlite_db_ctrl::instance().InsertData(" blueteeth_info ", insertSql);
|
|
||||||
}
|
}
|
||||||
|
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 root;
|
||||||
Json::Value valdatastatic;
|
Json::Value valdatastatic;
|
||||||
valdatastatic["TemperatureTop"] = env_temp;
|
valdatastatic["TemperatureTop"] = blueteeth_temp;
|
||||||
valdatastatic["TemperatureBot"] = chip_temp;
|
valdatastatic["TemperatureBot"] = chip_temp;
|
||||||
valdatastatic["Voltage"] = volt_;
|
valdatastatic["Voltage"] = volt_;
|
||||||
valdatastatic["ChannelId"] = mac;
|
valdatastatic["ChannelId"] = mac;
|
||||||
@ -204,7 +217,7 @@ void ScanBlueteeth::ParseData(unsigned char *data, int len,int8_t rssi){
|
|||||||
Json::FastWriter featureValue;
|
Json::FastWriter featureValue;
|
||||||
std::string strstatisticData = featureValue.write(root);
|
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));
|
device_map.insert(std::make_pair(std::string(mac), rssi));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
#define SCAN_BLUETEETH_H_
|
#define SCAN_BLUETEETH_H_
|
||||||
|
|
||||||
typedef signed char int8_t;
|
typedef signed char int8_t;
|
||||||
|
#include <string>
|
||||||
class ScanBlueteeth {
|
class ScanBlueteeth {
|
||||||
public:
|
public:
|
||||||
ScanBlueteeth();
|
ScanBlueteeth();
|
||||||
@ -14,6 +14,8 @@ public:
|
|||||||
int sock;
|
int sock;
|
||||||
int retry_count ;
|
int retry_count ;
|
||||||
int max_retries ;
|
int max_retries ;
|
||||||
|
std::string current_dev;
|
||||||
|
std::string ccurrent_time;
|
||||||
|
|
||||||
void ParseData(unsigned char *data, int len,int8_t rssi);
|
void ParseData(unsigned char *data, int len,int8_t rssi);
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user