refine thread func file.

This commit is contained in:
pandx 2024-10-24 20:54:29 +08:00
parent 82d74b6f08
commit 2c29f2ec19

View File

@ -56,6 +56,9 @@ void HeartRep() {
jsHeart["IP"] = GlobalConfig::IpAddr_G; jsHeart["IP"] = GlobalConfig::IpAddr_G;
std::string strJson = fw.write(jsHeart); std::string strJson = fw.write(jsHeart);
int iRet = data_publish(strJson.c_str(), GlobalConfig::Topic_G.mPubHeart.c_str()); int iRet = data_publish(strJson.c_str(), GlobalConfig::Topic_G.mPubHeart.c_str());
if (iRet != 0) {
zlog_error(zct, "fail to send heart mqtt msg");
}
zlog_info(zct, "heart = %s,iRet = %d", strJson.c_str(), iRet); zlog_info(zct, "heart = %s,iRet = %d", strJson.c_str(), iRet);
if (iRet != 0) { if (iRet != 0) {
gpio_set(GlobalConfig::GPIO_G.errorLed, 1); gpio_set(GlobalConfig::GPIO_G.errorLed, 1);
@ -146,6 +149,7 @@ open4G:
} }
#endif #endif
} }
void Dial5G() { void Dial5G() {
sleep(2); sleep(2);
dial5G: dial5G:
@ -190,6 +194,7 @@ void UartStart() {
uart_inst::instance().UartRecv(uart_inst::instance().fd, 1, buffer); uart_inst::instance().UartRecv(uart_inst::instance().fd, 1, buffer);
sleep(1); sleep(1);
} }
void InitModule() { void InitModule() {
#ifdef NR5G_MODULE #ifdef NR5G_MODULE
#ifdef G2UL_GATEWAY #ifdef G2UL_GATEWAY
@ -201,12 +206,13 @@ void InitModule() {
InitGpio(GlobalConfig::GPIO_G.commRest, 1); InitGpio(GlobalConfig::GPIO_G.commRest, 1);
gpio_set(GlobalConfig::GPIO_G.commRest, 0); //高电平复位 gpio_set(GlobalConfig::GPIO_G.commRest, 0); //高电平复位
#endif #endif // G2UL_GATEWAY
#ifdef IMX6UL_GATEWAY #ifdef IMX6UL_GATEWAY
InitGpio(GlobalConfig::GPIO_G.commRest, 1); InitGpio(GlobalConfig::GPIO_G.commRest, 1);
gpio_set(GlobalConfig::GPIO_G.commRest, 1); // gpio_set(GlobalConfig::GPIO_G.commRest, 1); //
#endif #endif // IMX6UL_GATEWAY
#endif #endif // NR5G_MODULE
#ifdef Q4G_MODULE #ifdef Q4G_MODULE
#ifdef G2UL_GATEWAY #ifdef G2UL_GATEWAY
InitGpio(GlobalConfig::GPIO_G.commPower, 1); // 4G,5G模组供电 InitGpio(GlobalConfig::GPIO_G.commPower, 1); // 4G,5G模组供电
@ -218,8 +224,8 @@ void InitModule() {
std::string strAPN = ReadStrByOpt(SERVERCONFIG, "Server", "APN"); std::string strAPN = ReadStrByOpt(SERVERCONFIG, "Server", "APN");
sprintf(szquectel, "/opt/quectel-CM/quectel-CM -s %s &", strAPN.c_str()); sprintf(szquectel, "/opt/quectel-CM/quectel-CM -s %s &", strAPN.c_str());
system(szquectel); system(szquectel);
#endif #endif // G2UL_GATEWAY
#endif #endif // Q4G_MODULE
#ifdef WIFI_MODULE #ifdef WIFI_MODULE
zlog_info(zbt, "Init WiFi!"); zlog_info(zbt, "Init WiFi!");
@ -231,7 +237,7 @@ void InitModule() {
wpa.ReconnectWiFi(); wpa.ReconnectWiFi();
system("/etc/init.d/wpa_restart"); system("/etc/init.d/wpa_restart");
system("udhcpc -i wlan2 &"); system("udhcpc -i wlan2 &");
#endif #endif // IMX6UL_GATEWAY
#ifdef G2UL_GATEWAY #ifdef G2UL_GATEWAY
InitGpio(GlobalConfig::GPIO_G.wifiReset, 1); // WiFi模组复位1复位0取消复位 InitGpio(GlobalConfig::GPIO_G.wifiReset, 1); // WiFi模组复位1复位0取消复位
gpio_set(GlobalConfig::GPIO_G.wifiReset, 0); gpio_set(GlobalConfig::GPIO_G.wifiReset, 0);
@ -242,9 +248,10 @@ void InitModule() {
wpa.ReconnectWiFi(); wpa.ReconnectWiFi();
system("/usr/sbin/wpa_supplicant -Dnl80211 -iwlan0 -c/etc/wpa_supplicant.conf &"); system("/usr/sbin/wpa_supplicant -Dnl80211 -iwlan0 -c/etc/wpa_supplicant.conf &");
system("udhcpc -b -i wlan0 &"); system("udhcpc -b -i wlan0 &");
#endif #endif // G2UL_GATEWAY
#endif #endif // WIFI_MODULE
} }
void TestUart() { void TestUart() {
uart_inst::instance().InitTestUart(B115200); uart_inst::instance().InitTestUart(B115200);
uart_inst::instance().ReadTestUart(); uart_inst::instance().ReadTestUart();
@ -253,6 +260,7 @@ void TestUart() {
void UartStartWave() { uart_inst::instance().DealWaveThread(); } void UartStartWave() { uart_inst::instance().DealWaveThread(); }
void StartUdpSys() { udp_sys::instance().StartConnectSysUdp(); } void StartUdpSys() { udp_sys::instance().StartConnectSysUdp(); }
void WatchDog() { void WatchDog() {
int fd = OpenWatchDog(); int fd = OpenWatchDog();
while (1) { while (1) {
@ -261,22 +269,32 @@ void WatchDog() {
} }
close(fd); close(fd);
} }
//static const char *LOCAL_WILL_TOPIC = "up/uart/will";
void my_publish_callback(struct mosquitto *mosq, void *obj, int mid) {} void my_publish_callback(struct mosquitto *mosq, void *obj, int mid) {}
void my_connect_callback(struct mosquitto *mosq, void *obj, int result) { void my_connect_callback(struct mosquitto *mosq, void *obj, int result) {
struct userdata *ud; struct userdata *ud;
ud = (struct userdata *)obj; ud = (struct userdata *)obj;
if (result) {
if (!result) { zlog_error(zct, "%s", mosquitto_connack_string(result));
return;
}
int ret = 0;
for (int i = 0; i < ud->topic_count; i++) { for (int i = 0; i < ud->topic_count; i++) {
zlog_info(zct, "mosquitto_subscribe ud->topics[%d]:%s", i, ud->topics[i]); zlog_info(zct, "mosquitto_subscribe ud->topics[%d]:%s", i, ud->topics[i]);
int iret = mosquitto_subscribe(mosq, NULL, ud->topics[i], ud->topic_qos); ret = mosquitto_subscribe(mosq, NULL, ud->topics[i], ud->topic_qos);
zlog_info(zct,"mosquitto_subscribe ret:%d", iret); if (ret != MOSQ_ERR_SUCCESS) {
zlog_error(zct, "fail to subscribe, ret: [%s], topic: %s", mosquitto_strerror(ret), ud->topics[i]);
return;
}
} }
int iret = mosquitto_subscribe(mosq, NULL, GlobalConfig::Topic_G.mSubData.c_str(), 1); ret = mosquitto_subscribe(mosq, NULL, GlobalConfig::Topic_G.mSubData.c_str(), 1);
zlog_info(zct,"mosquitto_subscribe's return value: %d", iret); if (ret != MOSQ_ERR_SUCCESS) {
zlog_error(zct, "fail to subscribe, ret: [%s], topic: %s", mosquitto_strerror(ret), GlobalConfig::Topic_G.mSubData.c_str());
return;
}
zlog_info(zct, "mosquitto_subscribe's return value: %d", ret);
char gwTime[32] = {0}; char gwTime[32] = {0};
GetTimeNet(gwTime, 0); GetTimeNet(gwTime, 0);
@ -301,9 +319,9 @@ void my_connect_callback(struct mosquitto *mosq, void *obj, int result) {
std::string instr = std::string(reply_string); std::string instr = std::string(reply_string);
std::string topic = "equipment/state/" + GlobalConfig::MacAddr_G; std::string topic = "equipment/state/" + GlobalConfig::MacAddr_G;
int ret = data_publish(instr.c_str(), topic.c_str()); ret = data_publish(instr.c_str(), topic.c_str());
if (ret != MOSQ_ERR_SUCCESS) { if (ret != MOSQ_ERR_SUCCESS) {
zlog_info(zct,"Publish failed:%d, %s", ret, instr.c_str()); zlog_error(zct, "Publish failed:%d, %s", ret, instr.c_str());
disconnect(); disconnect();
} }
} }
@ -314,24 +332,6 @@ void my_connect_callback(struct mosquitto *mosq, void *obj, int result) {
sprintf(buf, "{\"dataNodeGatewayNo\":\"%s\",\"cmd\":\"12\",\"status\":\"REQ\"}", GlobalConfig::MacAddr_G.c_str()); sprintf(buf, "{\"dataNodeGatewayNo\":\"%s\",\"cmd\":\"12\",\"status\":\"REQ\"}", GlobalConfig::MacAddr_G.c_str());
std::string str = std::string(buf); std::string str = std::string(buf);
std::string runinfo = "本地服务器连接成功"; std::string runinfo = "本地服务器连接成功";
} else {
if (result && !ud->quiet) {
fprintf(stderr, "%s\n", mosquitto_connack_string(result));
if (result == 1) {
// Connection Refused: unacceptable protocol version
} else if (result == 2) {
// Connection Refused: identifier rejected
} else if (result == 3) {
// Connection Refused: server unavailable
} else if (result == 4) {
// Connection Refused: bad user name or password
// exit(0);
} else if (result == 5) {
// Connection Refused: not authorized
}
}
}
} }
void my_disconnect_callback(struct mosquitto *mosq, void *obj, int result) { void my_disconnect_callback(struct mosquitto *mosq, void *obj, int result) {
@ -420,9 +420,9 @@ void my_subscribe_callback(struct mosquitto *mosq, void *obj, int mid, int qos_c
struct userdata *ud; struct userdata *ud;
assert(obj); assert(obj);
ud = (struct userdata *)obj; ud = (struct userdata *)obj;
if (!ud->quiet) zlog_info(zct,"Subscribed (mid: %d): %d", mid, granted_qos[0]); zlog_info(zct, "Subscribed (mid: %d): %d", mid, granted_qos[0]);
for (i = 1; i < qos_count; i++) { for (i = 1; i < qos_count; i++) {
if (!ud->quiet) zlog_info(zct,", %d", granted_qos[i]); zlog_info(zct, ", %d", granted_qos[i]);
} }
} }
@ -487,9 +487,10 @@ void RecvUpdateFile() {
bzero(buffer, 1024); bzero(buffer, 1024);
fp = fopen("/tmp/upgrade.tar.gz", "w"); fp = fopen("/tmp/upgrade.tar.gz", "w");
if (NULL == fp) { if (NULL == fp) {
zlog_info(zct,"File:\t Can Not Open To Write"); zlog_error(zbt, "File:/tmp/upgrade.tar.gz Can Not Open");
exit(1); return;
} }
while ((len = socket.read_some(boost::asio::buffer(buffer), ec))) { while ((len = socket.read_some(boost::asio::buffer(buffer), ec))) {
write_len = fwrite(buffer, sizeof(char), len, fp); write_len = fwrite(buffer, sizeof(char), len, fp);
if (write_len < len) { if (write_len < len) {
@ -498,13 +499,14 @@ void RecvUpdateFile() {
} }
bzero(buffer, 1024); bzero(buffer, 1024);
} }
zlog_info(zct, "Receive File From Server Finished!"); zlog_info(zbt, "Receive File From Server Finished!");
fclose(fp); fclose(fp);
Json::Value jsData; Json::Value jsData;
Json::FastWriter fw; Json::FastWriter fw;
jsData["cmd"] = "03"; jsData["cmd"] = "03";
jsData["updatefilename"] = "updatefile"; jsData["updatefilename"] = "updatefile";
std::string str = fw.write(jsData); std::string str = fw.write(jsData);
zlog_warn(zbt, "==============upgrade start===============");
system("/etc/init.d/sysupgrade.sh"); system("/etc/init.d/sysupgrade.sh");
} }
} }