This commit is contained in:
zhangsheng 2025-03-21 15:22:34 +08:00
parent 072cd7fd7c
commit 67151d7a47
2 changed files with 281 additions and 248 deletions

View File

@ -1,243 +1,276 @@
#include "thread_func.hpp" #include "thread_func.hpp"
#include <time.h> #include <time.h>
#include <math.h> #include <math.h>
#include <string.h> #include <string.h>
#include <cstdlib> #include <cstdlib>
#include <linux/types.h> #include <linux/types.h>
#include <sys/sysinfo.h> #include <sys/sysinfo.h>
#include <dirent.h> #include <dirent.h>
#include <map> #include <map>
#include <zlog.h> #include <zlog.h>
#include "mqttclient/mqtt_client.h" #include "mqttclient/mqtt_client.h"
#include "uart/uart.hpp" #include "uart/uart.hpp"
#include "common/common_func.hpp" #include "common/common_func.hpp"
#include "jsonparse/communication_cmd.hpp" #include "jsonparse/communication_cmd.hpp"
#include "wifi_5g/wpa_client.h" #include "wifi_5g/wpa_client.h"
extern zlog_category_t *zct; extern zlog_category_t *zct;
extern zlog_category_t *zbt; extern zlog_category_t *zbt;
void CheckThread() { void CheckThread() {
zlog_info(zbt, "ENTER CHECK THREAD "); zlog_info(zbt, "ENTER CHECK THREAD ");
int heart_count = 0; int heart_count = 0;
int time_check = 0; int time_check = 0;
int reset_flag = 0; int reset_flag = 0;
int online_check = 0; int online_check = 0;
int HardStatus = 0; int HardStatus = 0;
int logClean = 0; int logClean = 0;
int Battery = 0; int Battery = 0;
int commSignal = 0; int commSignal = 0;
int loose_check = 0; int loose_check = 0;
int mqttresend = 0; int mqttresend = 0;
int checkNet0 = 0;
while (GlobalConfig::QuitFlag_G) { int wifi_reconnect_count = 0;
GlobalConfig::threadStatus = 1; while (GlobalConfig::QuitFlag_G) {
sleep(1); GlobalConfig::threadStatus = 1;
if (10 == heart_count) { sleep(1);
if (GlobalConfig::LinkCount > 30) { if (10 == heart_count) {
zlog_error(zct, "MQTT connect failed "); if (GlobalConfig::LinkCount > 30) {
char connect[10] = {0x00}; zlog_error(zct, "MQTT connect failed ");
readStringValue("config", "connect", connect, (char *)GlobalConfig::Config_G.c_str()); }
if (atoi(connect)) { std::string ipTemp = IpAddrInit();
zlog_error(zct, "MQTT connect failed,reboot"); GlobalConfig::IpAddr_G = ipTemp;
exit(0); heart_count = 0;
} }
} if (7200 == time_check) { // 2h
std::string ipTemp = IpAddrInit(); time_check = 0;
GlobalConfig::IpAddr_G = ipTemp; char buf[256] = {0};
heart_count = 0; char buf2[256] = {0};
} sprintf(buf, "{\"dataNodeGatewayNo\":\"%s\",\"cmd\":\"12\",\"status\":\"REQ\"}", GlobalConfig::MacAddr_G.c_str());
if (7200 == time_check) { // 2h sprintf(buf2, "{\"dataWatchNo\":\"%s\",\"cmd\":\"12\",\"status\":\"REQ\"}", GlobalConfig::MacAddr_G.c_str());
time_check = 0; std::string str = std::string(buf);
char buf[256] = {0}; std::string str2 = std::string(buf2);
char buf2[256] = {0};
sprintf(buf, "{\"dataNodeGatewayNo\":\"%s\",\"cmd\":\"12\",\"status\":\"REQ\"}", GlobalConfig::MacAddr_G.c_str()); int iRet = data_publish(str.c_str(), GlobalConfig::Topic_G.mPubTiming.c_str());
sprintf(buf2, "{\"dataWatchNo\":\"%s\",\"cmd\":\"12\",\"status\":\"REQ\"}", GlobalConfig::MacAddr_G.c_str()); iRet = data_publish(str.c_str(), GlobalConfig::Topic_G.mPubCmd.c_str());
std::string str = std::string(buf); iRet = data_publish(str2.c_str(), GlobalConfig::Topic_G.mPubTiming.c_str());
std::string str2 = std::string(buf2); iRet = data_publish(str2.c_str(), GlobalConfig::Topic_G.mPubCmd.c_str());
if (iRet != 0) {
int iRet = data_publish(str.c_str(), GlobalConfig::Topic_G.mPubTiming.c_str()); zlog_error(zct, "MQTT connect failed ,time check");
iRet = data_publish(str.c_str(), GlobalConfig::Topic_G.mPubCmd.c_str()); #ifdef Q4G_MODULE
iRet = data_publish(str2.c_str(), GlobalConfig::Topic_G.mPubTiming.c_str()); char localtimestamp[32] = { 0 };
iRet = data_publish(str2.c_str(), GlobalConfig::Topic_G.mPubCmd.c_str()); GetTimeNet(localtimestamp, 1);
if (iRet != 0) { char whereCon[100]={0};
zlog_error(zct, "MQTT connect failed ,time check"); sprintf(whereCon,"DATE(timestamp, 'unixepoch') = DATE('now') ORDER BY timeStamp DESC limit 0,1");
char connect[10] = {0x00}; std::string strCount = sqlite_db_ctrl::instance().GetData(" reboot_record ", "count", whereCon);
readStringValue("config", "connect", connect, (char *)GlobalConfig::Config_G.c_str()); if (atoi(strCount.c_str()) < 3)//当天大于三次不再重启进程
if (atoi(connect)) { {
zlog_error(zct, "MQTT connect failed,time check ,reboot"); char insertSql[64] = {0};
exit(0); sprintf(insertSql, "'%s',%d,'%s'",localtimestamp,atoi(strCount.c_str())+1,"4G connect failed ");
} sqlite_db_ctrl::instance().InsertData(" reboot_record ", insertSql);
#ifdef NR5G_MODULE gpio_set(GlobalConfig::GPIO_G.commPower,0);
dial.closePort(); zlog_warn(zct,"commPower start\n");
gpio_set(GlobalConfig::GPIO_G.commRest, 0); sleep(2);
zlog_warn(zct, "GPIO 8 start"); gpio_set(GlobalConfig::GPIO_G.commPower,1);
sleep(2); zlog_warn(zct,"commPower End\n");
gpio_set(GlobalConfig::GPIO_G.commRest, 1); sleep(2);
zlog_warn(zct, "GPIO 8 End"); system("reboot");
sleep(20); }else{
dial.openPort("/dev/ttyUSB2"); zlog_error(zct,"Q4G_MODULE error\n");
dial.setState(); }
#endif #endif
#ifdef WIFI_MODULE #ifdef NR5G_MODULE
zlog_warn(zct, "WiFi reset!"); dial.closePort();
gpio_set(GlobalConfig::GPIO_G.wifiReset, 0); gpio_set(GlobalConfig::GPIO_G.commPower, 0);
sleep(5); zlog_warn(zct, "commPower start");
gpio_set(GlobalConfig::GPIO_G.wifiReset, 1); sleep(2);
sleep(5); gpio_set(GlobalConfig::GPIO_G.commPower, 1);
wifi::WPAClient wpa; zlog_warn(zct, "commPower End");
wpa.ReconnectWiFi(); sleep(20);
system("/etc/init.d/wpa_restart"); connectCount++;
system("udhcpc -i wlan2 &"); if (connectCount > 10) {
#endif zlog_error(zct, "5G reset error ,reboot!");
} system("reboot");
} }
if (HardStatus == 3600) { // one hour 3600 dial.openPort("/dev/ttyUSB2");
JsonData jd; dial.setState();
jd.JsonCmd_07();
HardStatus = 0; #endif
} #ifdef WIFI_MODULE
if (mqttresend == 7200) { zlog_warn(zct, "WiFi reset!");
mqttresend = 0; system("/etc/init.d/wpa_restart");
zlog_info(zct, "mqttresend check"); sleep(5);
Json::Value jsHeart; std::string strip = GetGwIp_("wlan0");
Json::FastWriter fw; zlog_warn(zct,"strip = %s", strip.c_str());
jsHeart["dataNodeGatewayNo"] = GlobalConfig::MacAddr_G;
jsHeart["status"] = "online_V" + GlobalConfig::Version; gpio_set(GlobalConfig::GPIO_G.wifiReset, 1);
jsHeart["IP"] = GlobalConfig::IpAddr_G; sleep(5);
std::string strJson = fw.write(jsHeart); gpio_set(GlobalConfig::GPIO_G.wifiReset, 0);
int iRet = data_publish(strJson.c_str(), GlobalConfig::Topic_G.mPubHeart.c_str()); sleep(30);
if (iRet == 0) { wifi::WPAClient wpa;
sqlite_db_ctrl::instance().QueryofflineData(); wpa.ReconnectWiFi();
} system("/etc/init.d/wpa_restart");
} sleep(5);
system("udhcpc -b -i wlan0 &");
if (1800 == online_check) {
online_check = 0; #endif
JsonData jd; }
int Count = sqlite_db_ctrl::instance().GetTableRows(T_SENSOR_INFO(TNAME), NULL); }
if (Count > 0) { if (HardStatus == 3600) { // one hour 3600
jd.DataNodeStatusCheck(); JsonData jd;
char localtimestamp[32] = {0}; jd.JsonCmd_07();
GetTimeNet(localtimestamp, 1); HardStatus = 0;
std::string nowTimetamp = std::string(localtimestamp); }
char selectCon[128] = {0}; if(checkNet0 == 5){
sprintf(selectCon, " sendMsg <> '' ORDER BY timeStamp DESC LIMIT 0,1"); checkNet0 = 0;
sleep(1); int iStatus = get_netlink_status("eth0");
std::string strTime = sqlite_db_ctrl::instance().GetData(T_DATASTATIC_INFO(TNAME), "timeStamp", selectCon); if(iStatus == 1 && GlobalConfig::net0Status == 0){
long lTime = atol(nowTimetamp.c_str()) - atol(strTime.c_str()); system("ifconfig eth0:1 192.168.188.188 netmask 255.255.255.0");
zlog_info(zct, "online check = %ld", lTime); }
} GlobalConfig::net0Status = iStatus;
}
} if (mqttresend == 7200) {
if (7200 == Battery) { mqttresend = 0;
Battery = 0; zlog_info(zct, "mqttresend check");
zlog_info(zct, "Battery"); Json::Value jsHeart;
sqlite_db_ctrl::instance().CalculateBattery(); Json::FastWriter fw;
JsonData jd; jsHeart["dataNodeGatewayNo"] = GlobalConfig::MacAddr_G;
jd.JsonCmd_07(); jsHeart["status"] = "online_V" + GlobalConfig::Version;
jd.JsonCmd_29(); jsHeart["IP"] = GlobalConfig::IpAddr_G;
jd.JsonCmd_30(); std::string strJson = fw.write(jsHeart);
jd.JsonCmd_31(); int iRet = data_publish(strJson.c_str(), GlobalConfig::Topic_G.mPubHeart.c_str());
} if (iRet == 0) {
sqlite_db_ctrl::instance().QueryofflineData();
if (3500 == loose_check) { }
zlog_info(zct, "loosecheck\n"); }
loose_check = 0;
sqlite_db_ctrl::instance().CalculateDip(); if (1800 == online_check) {
} online_check = 0;
int Count = sqlite_db_ctrl::instance().GetTableRows(T_SENSOR_INFO(TNAME), NULL);
if (18000 == commSignal) { // 5h if (Count > 0) {
Json::Value jsBody, jsonVal; JsonData jd;
Json::FastWriter showValue; jd.DataNodeStatusCheck();
if (GlobalConfig::NetStatus == "\"NOCONN\"" || GlobalConfig::NetStatus == "\"CONNECT\"") { char localtimestamp[32] = {0};
if (GlobalConfig::NetSignal == 0) { GetTimeNet(localtimestamp, 1);
jsBody["communicationSignal"] = "未知"; std::string nowTimetamp = std::string(localtimestamp);
} else if (GlobalConfig::NetSignal > -80) { char selectCon[128] = {0};
jsBody["commSignal"] = ""; sprintf(selectCon, " sendMsg <> '' ORDER BY timeStamp DESC LIMIT 0,1");
} else if (GlobalConfig::NetSignal > -90 && GlobalConfig::NetSignal < -80) { sleep(1);
jsBody["commSignal"] = ""; std::string strTime = sqlite_db_ctrl::instance().GetData(T_DATASTATIC_INFO(TNAME), "timeStamp", selectCon);
} else if (GlobalConfig::NetSignal > -105 && GlobalConfig::NetSignal < -90) { long lTime = atol(nowTimetamp.c_str()) - atol(strTime.c_str());
jsBody["commSignal"] = "一般"; zlog_info(zct, "online check = %ld", lTime);
} else if (GlobalConfig::NetSignal < -105) { }
jsBody["commSignal"] = ""; }
} else if (GlobalConfig::NetSignal < -115) { if (7200 == Battery) {
jsBody["commSignal"] = "不稳定"; Battery = 0;
} zlog_warn(zct, "Battery");
sqlite_db_ctrl::instance().CalculateBattery();
} else if (GlobalConfig::NetStatus == "\"SEARCH\"") { JsonData jd;
jsBody["commSignal"] = "搜索网络"; jd.JsonCmd_30();
} else if (GlobalConfig::NetStatus == "\"LIMSRV\"") { jd.JsonCmd_07();
jsBody["commSignal"] = "未插卡"; jd.JsonCmd_29();
} else { jd.JsonCmd_31();
jsBody["commSignal"] = "未知"; }
}
if (3500 == loose_check) {
jsonVal["cmd"] = "53"; zlog_warn(zct, "loosecheck\n");
std::string dataBody = showValue.write(jsBody); loose_check = 0;
jsonVal["cmdBody"] = dataBody; sqlite_db_ctrl::instance().CalculateDip();
data_publish(showValue.write(jsonVal).c_str(), GlobalConfig::Topic_G.mPubConfig.c_str()); }
}
if (21600 == logClean) { if (18000 == commSignal) { // 5h
logClean = 0; Json::Value jsBody, jsonVal;
char localtimestamp[32] = {0}; Json::FastWriter showValue;
GetTimeNet(localtimestamp, 1); if (GlobalConfig::NetStatus == "\"NOCONN\"" || GlobalConfig::NetStatus == "\"CONNECT\"") {
char whereCon[1024] = {0}; if (GlobalConfig::NetSignal == 0) {
sprintf(whereCon, " timestamp < '%ld' ", atol(localtimestamp) - 2592000); //删除1个月前的数据 jsBody["communicationSignal"] = "未知";
sqlite_db_ctrl::instance().DeleteTableData(" t_battery_history ", whereCon); } else if (GlobalConfig::NetSignal > -80) {
sqlite_db_ctrl::instance().DeleteTableData(" receive_wave_status ", whereCon); jsBody["commSignal"] = "";
} } else if (GlobalConfig::NetSignal > -90 && GlobalConfig::NetSignal < -80) {
#ifdef WIFI_MODULE jsBody["commSignal"] = "";
if (wifi_reconnect_count == 600) { // 每 10分钟重连WiFi网络 } else if (GlobalConfig::NetSignal > -105 && GlobalConfig::NetSignal < -90) {
wifi_reconnect_count = 0; jsBody["commSignal"] = "一般";
wifi::WPAClient wpa; } else if (GlobalConfig::NetSignal < -105) {
std::string netssid = wpa.GetNetSsid(); jsBody["commSignal"] = "";
std::string currentssid = ""; } else if (GlobalConfig::NetSignal < -115) {
if (0 == netssid.compare("")) { jsBody["commSignal"] = "不稳定";
netssid = wpa.GetNetSsid(); }
}
} else if (GlobalConfig::NetStatus == "\"SEARCH\"") {
if (netssid.length() > 0) { jsBody["commSignal"] = "搜索网络";
currentssid = wpa.GetCurrentSSID(); } else if (GlobalConfig::NetStatus == "\"LIMSRV\"") {
if (currentssid.length() > 0) { jsBody["commSignal"] = "未插卡";
char buf[64] = {0}; } else {
std::string rssiSend = ""; jsBody["commSignal"] = "未知";
rssiSend = "/opt/Cidn/wpa_cli signal_poll|grep RSSI | cut -f 2 -d '='"; }
system_custom(rssiSend.c_str(), buf);
std::string Rssi = std::string(buf); jsonVal["cmd"] = "53";
std::string dataBody = showValue.write(jsBody);
memset(buf, 0, 64); jsonVal["cmdBody"] = dataBody;
sprintf(buf, "wifi:true Rssi:%s", Rssi.c_str()); data_publish(showValue.write(jsonVal).c_str(), GlobalConfig::Topic_G.mPubConfig.c_str());
zlog_info(zct, "%s\n", buf); }
if (21600 == logClean) {
} else { logClean = 0;
std::string runinfo = "wifi:false\n"; char localtimestamp[32] = {0};
zlog_info(zct, "%s\n", runinfo.c_str()); GetTimeNet(localtimestamp, 1);
} char whereCon[1024] = {0};
} sprintf(whereCon, " timestamp < '%ld' ", atol(localtimestamp) - 2592000 * 2); //删除2个月前的数据
if (wpa.ReconnectWiFi()) { sqlite_db_ctrl::instance().DeleteTableData(" t_battery_history ", whereCon);
zlog_info(zct, "wifi reconnect ok\n");
} sqlite_db_ctrl::instance().DeleteTableData(" receive_wave_status ", whereCon);
} }
#endif #ifdef WIFI_MODULE
if (wifi_reconnect_count == 600) { // 每 10分钟重连WiFi网络
if (reset_flag > 7200) { wifi_reconnect_count = 0;
reset_flag = 0; wifi::WPAClient wpa;
} std::string netssid = wpa.GetNetSsid();
Battery++; std::string currentssid = "";
reset_flag++; if (0 == netssid.compare("")) {
time_check++; netssid = wpa.GetNetSsid();
heart_count++; }
online_check++;
HardStatus++; if (netssid.length() > 0) {
logClean++; currentssid = wpa.GetCurrentSSID();
loose_check++; if (currentssid.length() > 0) {
mqttresend++; char buf[64] = {0};
#ifdef WIFI_MODULE std::string rssiSend = "";
wifi_reconnect_count++; rssiSend = "/usr/sbin/wpa_cli signal_poll|grep RSSI | cut -f 2 -d '='";
#endif system_custom(rssiSend.c_str(), buf);
} std::string Rssi = std::string(buf);
}
memset(buf, 0, 64);
sprintf(buf, "wifi:true Rssi:%s", Rssi.c_str());
zlog_info(zct, "%s\n", buf);
} else {
std::string runinfo = "wifi:false\n";
zlog_info(zct, "%s\n", runinfo.c_str());
}
}
if (wpa.ReconnectWiFi()) {
zlog_info(zct, "wifi reconnect ok\n");
}
}
#endif
if (reset_flag > 7200) {
reset_flag = 0;
}
Battery++;
reset_flag++;
time_check++;
heart_count++;
online_check++;
HardStatus++;
logClean++;
loose_check++;
mqttresend++;
checkNet0 ++;
#ifdef WIFI_MODULE
wifi_reconnect_count++;
#endif
}
}

View File

@ -516,10 +516,10 @@ void Uart::DealRecvData(const char *pData) {
zlog_info(zct, "whereCon = %s", whereCon); zlog_info(zct, "whereCon = %s", whereCon);
array_t vecRes = sqlite_db_ctrl::instance().GetDataMultiLine(T_SENSOR_INFO(TNAME), " dataNodeNo, MeasurementID,hardVersion,softVersion", whereCon); array_t vecRes = sqlite_db_ctrl::instance().GetDataMultiLine(T_SENSOR_INFO(TNAME), " dataNodeNo, MeasurementID,hardVersion,softVersion", whereCon);
zlog_info(zct, "vecRes = %d", vecRes.size()); zlog_info(zct, "vecRes = %d", vecRes.size());
if(vecRes.size() == 0){ // if(vecRes.size() == 0){
zlog_warn(zct, "device info not found %02x%02x ", UINT16_HIGH(ushortAdd),UINT16_LOW(ushortAdd)); // zlog_warn(zct, "device info not found %02x%02x ", UINT16_HIGH(ushortAdd),UINT16_LOW(ushortAdd));
return; // return;
} // }
std::string hardVersion = vecRes[0][2]; std::string hardVersion = vecRes[0][2];
std::string softVersion = vecRes[0][3]; std::string softVersion = vecRes[0][3];
zlog_info(zct, "shortAdd = %02x%02x,command = %d ",UINT16_HIGH(ushortAdd),UINT16_LOW(ushortAdd),command); zlog_info(zct, "shortAdd = %02x%02x,command = %d ",UINT16_HIGH(ushortAdd),UINT16_LOW(ushortAdd),command);
@ -696,7 +696,7 @@ void Uart::DealDataNodeInfo(const char *pData) {
sprintf(buf, "%.1f", 0.1 * chTemp); sprintf(buf, "%.1f", 0.1 * chTemp);
dataNodeInfo.HardVersion = std::string(buf); dataNodeInfo.HardVersion = std::string(buf);
zlog_info(zct, "dataNodeInfo.HardVersion = %s", dataNodeInfo.HardVersion.c_str()); zlog_info(zct, "dataNodeInfo.HardVersion = %s", dataNodeInfo.HardVersion.c_str());
if (dataNodeInfo.HardVersion != "3.0" && dataNodeInfo.HardVersion != "4.0" && dataNodeInfo.HardVersion != "1.0") { if (dataNodeInfo.HardVersion != "3.0" && dataNodeInfo.HardVersion != "4.0" && dataNodeInfo.HardVersion != "1.0" && dataNodeInfo.HardVersion != "2.0") {
DealDataNodeFeatureTransition(pData,0); DealDataNodeFeatureTransition(pData,0);
zlog_info(zct, "DealDataNodeFeatureTransition"); zlog_info(zct, "DealDataNodeFeatureTransition");
return; return;