From e15ad5cc4a05217586b7bb99db3c35b762b6f5f3 Mon Sep 17 00:00:00 2001 From: zhangsheng Date: Fri, 20 Sep 2024 17:30:40 +0800 Subject: [PATCH] =?UTF-8?q?3.2.4=20beta1=20=E4=BC=98=E5=8C=96=E6=B3=A2?= =?UTF-8?q?=E5=BD=A2=E6=8E=A5=E6=94=B6=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= =?UTF-8?q?=EF=BC=8C=E5=AE=9A=E6=97=B6=E5=99=A8=E7=BA=BF=E7=A8=8B=E5=9C=A8?= =?UTF-8?q?=E9=81=87=E5=88=B0=E6=B3=A2=E5=BD=A2=E4=BC=A0=E8=BE=93=E6=97=B6?= =?UTF-8?q?=E5=B0=86=E4=B8=8D=E5=A4=84=E7=90=86=EF=BC=8C=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E8=AE=B0=E5=BD=95=E6=89=A9=E5=A4=A7=E5=88=B0321k?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/SH_CommonFunc.cpp | 11 ++ common/SH_CommonFunc.hpp | 1 + common/SH_global.h | 2 +- dbaccess/SH_SqlDB.cpp | 132 ++++++++++--------- dial5G/Dial.cpp | 4 +- jsonparse/SH_JsonCmd.cpp | 6 +- localserver/SH_LocalServer.cpp | 8 +- main.cpp | 3 +- platform/SH_PlatformInit.cpp | 2 +- threadfunc/SH_ThreadFunc.cpp | 74 +++++------ uart/SH_Uart.cpp | 233 ++++++++++++++++++++------------- uart/SH_Uart.hpp | 1 + wifi/wpa_client.cpp | 2 +- 13 files changed, 276 insertions(+), 203 deletions(-) diff --git a/common/SH_CommonFunc.cpp b/common/SH_CommonFunc.cpp index 700cdd6..36a89b3 100644 --- a/common/SH_CommonFunc.cpp +++ b/common/SH_CommonFunc.cpp @@ -482,6 +482,17 @@ int ModifyMac(char* buff) system("cp /opt/system/mac /opt/system/macbak"); system("/opt/Cidn/init.sh"); } +void mssleep(unsigned long microseconds) +{ + + struct timespec req; + struct timespec rem; + + req.tv_sec = microseconds / 1000000; + req.tv_nsec = (microseconds % 1000000) * 1000; + + nanosleep(&req, &rem); +} std::string GetCurrentTime() { struct tm nowtime; diff --git a/common/SH_CommonFunc.hpp b/common/SH_CommonFunc.hpp index d075bd4..f7518b3 100644 --- a/common/SH_CommonFunc.hpp +++ b/common/SH_CommonFunc.hpp @@ -616,6 +616,7 @@ extern int config_uart(const char* Port,speed_t speed); extern int write_data(int fd, char *buff, int len); extern int read_data(int fd, char *buff, int len, int timeout); extern int ModifyMac(char* buff); +extern void mssleep(unsigned long microseconds); //extern int str_recv(int fd, char srcshow,char* buffer); /** * @brief 系统运行时输入入参进行系统软件版本查询 diff --git a/common/SH_global.h b/common/SH_global.h index c67b7d6..2c373c8 100644 --- a/common/SH_global.h +++ b/common/SH_global.h @@ -20,7 +20,7 @@ enum enumZigBeeTransmitStatus { //#define NR5G_MODULE -#define Q4G_MODULE +//#define Q4G_MODULE //#define WIFI_MODULE //#define NR5G_MEIGE //#define G2UL_GATEWAY diff --git a/dbaccess/SH_SqlDB.cpp b/dbaccess/SH_SqlDB.cpp index 3d00d13..33f8d7d 100644 --- a/dbaccess/SH_SqlDB.cpp +++ b/dbaccess/SH_SqlDB.cpp @@ -1179,65 +1179,78 @@ int SqliteDB::InintGateway() std::string SqliteDB::GetNodeConfigureInfor(const char* whereCon) { - vec_t vecRes = GetDataSingleLine(T_SENSOR_INFO(TNAME), "*", whereCon); - if(vecRes.size() < 1) - return ""; - Json::Value jsSensorData; - Json::Value jsonVal; - jsonVal.clear(); - jsonVal["dataNodeGatewayNo"] = GlobalConfig::MacAddr_G; - jsonVal["cmd"] = "26"; - jsSensorData["dataNodeNo"] = vecRes[44]; - jsSensorData["dataNodeName"] = vecRes[1]; - jsSensorData["initFlag"] = atoi(vecRes[2].c_str()); - jsSensorData["accFlag"] = atoi(vecRes[3].c_str()); - jsSensorData["zigbeeFlag"] = atoi(vecRes[4].c_str()); - jsSensorData["temTopFlag"] = atoi(vecRes[5].c_str()); - jsSensorData["temBotFlag"] = atoi(vecRes[6].c_str()); - jsSensorData["hardVersion"] = vecRes[8]; - jsSensorData["softVersion"] = vecRes[9]; - jsSensorData["bpNo"] = vecRes[10]; - jsSensorData["serialNo"] = vecRes[11]; - jsSensorData["firstPowerTime"] = vecRes[12]; - jsSensorData["WakeupTime"] = atoi(vecRes[13].c_str()); - jsSensorData["StaticTime"] = atoi(vecRes[14].c_str()); - jsSensorData["WaveTime"] = atoi(vecRes[15].c_str()); - jsSensorData["BateryV"] = vecRes[16]; - jsSensorData["ProductNo"] = vecRes[17]; - jsSensorData["configFlag"] = atoi(vecRes[18].c_str()); - jsSensorData["startBrands"] = vecRes[19]; - jsSensorData["stopBrands"] = vecRes[20]; - jsSensorData["featureInterVal"] = (vecRes[21]); - jsSensorData["waveInterVal"] = atoi(vecRes[22].c_str()); - jsSensorData["samplingRate"] = atoi(vecRes[23].c_str()); - jsSensorData["range"] = atoi(vecRes[25].c_str()); - jsSensorData["envelopeBandPass"] = vecRes[26]; - jsSensorData["faultFrequency"] = vecRes[27]; - jsSensorData["zigbeePanId"] = vecRes[28]; - jsSensorData["zigbeeChannel"] = (vecRes[29]); - jsSensorData["zigbeeAddr"] = vecRes[30]; - jsSensorData["zigbeeLongAddr"] = vecRes[31]; - jsSensorData["zigbeeDesAddr"] = vecRes[32]; - jsSensorData["ZigbeePower"] = atoi(vecRes[33].c_str()); - jsSensorData["ZigbeeRetry"] = atoi(vecRes[34].c_str()); - jsSensorData["ZigbeeRetryGap"] = atoi(vecRes[35].c_str()); - jsSensorData["ACCSampleTime"] = atoi(vecRes[36].c_str()); - jsSensorData["status"] = atoi(vecRes[37].c_str()); - jsSensorData["timeStamp"] = vecRes[38]; - jsSensorData["viff"] = atoi(vecRes[39].c_str()); - jsSensorData["RSSI"] = atoi(vecRes[40].c_str()); - jsSensorData["Update"] = atoi(vecRes[41].c_str()); - jsSensorData["LooseValue"] = (vecRes[42].c_str()); - jsSensorData["batteryPower"] = (vecRes[43].c_str()); - - - Json::FastWriter showValue; - Json::Value jsBody; - jsBody["dataNodeGatewayNo"] = GlobalConfig::MacAddr_G; - jsBody["dataNodeData"] = jsSensorData; - std::string dataBody = showValue.write(jsBody); - jsonVal["cmdBody"] = dataBody; - return showValue.write(jsonVal); + Json::Value jsonVal; + Json::FastWriter showValue; + jsonVal.clear(); + jsonVal["cmd"] = "26"; + jsonVal["dataNodeGatewayNo"] = GlobalConfig::MacAddr_G; + jsonVal["success"] = true; + jsonVal["message"] = "查询成功"; + Json::Value jsArray; + array_t arrRes; + arrRes = sql_ctl->GetDataMultiLine(T_SENSOR_INFO(TNAME), "*", whereCon); + int iResult = arrRes.size(); + if (iResult > 0) { + for (int j = 0; j < iResult; j++) { + Json::Value jsSensorData; + jsSensorData["dataNodeNo"] = arrRes[j][44]; + jsSensorData["dataNodeName"] = arrRes[j][1]; + jsSensorData["initFlag"] = atoi(arrRes[j][2].c_str()); + jsSensorData["accFlag"] = atoi(arrRes[j][3].c_str()); + jsSensorData["zigbeeFlag"] = atoi(arrRes[j][4].c_str()); + jsSensorData["temTopFlag"] = atoi(arrRes[j][5].c_str()); + jsSensorData["temBotFlag"] = atoi(arrRes[j][6].c_str()); + jsSensorData["equipsta"] = atoi(arrRes[j][7].c_str()); + jsSensorData["hardVersion"] = arrRes[j][8]; + jsSensorData["softVersion"] = arrRes[j][9]; + jsSensorData["bpNo"] = arrRes[j][10]; + jsSensorData["serialNo"] = arrRes[j][11]; + jsSensorData["firstPowerTime"] = arrRes[j][12]; + jsSensorData["WakeupTime"] = atoi(arrRes[j][13].c_str()); + jsSensorData["StaticTime"] = atoi(arrRes[j][14].c_str()); + jsSensorData["WaveTime"] = atoi(arrRes[j][15].c_str()); + jsSensorData["BateryV"] = arrRes[j][16]; + jsSensorData["ProductNo"] = arrRes[j][17]; + jsSensorData["configFlag"] = atoi(arrRes[j][18].c_str()); + jsSensorData["startBrands"] = arrRes[j][19]; + jsSensorData["stopBrands"] = arrRes[j][20]; + jsSensorData["featureInterVal"] = (arrRes[j][21]); + jsSensorData["waveInterVal"] = atoi(arrRes[j][22].c_str()); + jsSensorData["samplingRate"] = atoi(arrRes[j][23].c_str()); + jsSensorData["range"] = atoi(arrRes[j][25].c_str()); + jsSensorData["envelopeBandPass"] = arrRes[j][26]; + jsSensorData["faultFrequency"] = arrRes[j][27]; + jsSensorData["zigbeePanId"] = arrRes[j][28]; + jsSensorData["zigbeeChannel"] = (arrRes[j][29]); + jsSensorData["zigbeeAddr"] = arrRes[j][30]; + jsSensorData["zigbeeLongAddr"] = arrRes[j][31]; + jsSensorData["zigbeeDesAddr"] = arrRes[j][32]; + jsSensorData["ZigbeePower"] = atoi(arrRes[j][33].c_str()); + jsSensorData["ZigbeeRetry"] = atoi(arrRes[j][34].c_str()); + jsSensorData["ZigbeeRetryGap"] = atoi(arrRes[j][35].c_str()); + jsSensorData["ACCSampleTime"] = atoi(arrRes[j][36].c_str()); + jsSensorData["status"] = atoi(arrRes[j][37].c_str()); + jsSensorData["timeStamp"] = arrRes[j][38]; + jsSensorData["viff"] = atoi(arrRes[j][39].c_str()); + jsSensorData["RSSI"] = arrRes[j][40]; + jsSensorData["Update"] = atoi(arrRes[j][41].c_str()); + jsSensorData["looseValue"] = arrRes[j][42]; + jsSensorData["battery"] = arrRes[j][43]; + jsSensorData["MeasurementID"] = arrRes[j][44]; + jsArray.append(jsSensorData); + } + } else { + jsArray.resize(0); + jsonVal["success"] = false; + jsonVal["message"] = "查询失败"; + } + + Json::Value jsBody; + jsBody["dataNodeGatewayNo"] = GlobalConfig::MacAddr_G; + jsBody["dataNodeArray"] = jsArray; + std::string dataBody = showValue.write(jsBody); + jsonVal["cmdBody"] = dataBody; + return showValue.write(jsonVal); } int SqliteDB::QueryofflineData() @@ -1317,7 +1330,6 @@ int SqliteDB::QueryofflineData() sql_ctl->UpdateTableData(dataTableName, updateSql, whereCon); LOG_INFO("resend success dataNodeNo = %s,TimeStamp = '%s'\n",arrRetData[j][0].c_str(),arrRetData[j][8].c_str()); } - sleep(1); } } char whereCon[1024] = {0x00}; diff --git a/dial5G/Dial.cpp b/dial5G/Dial.cpp index d1158f1..222ff65 100644 --- a/dial5G/Dial.cpp +++ b/dial5G/Dial.cpp @@ -203,7 +203,7 @@ int Dial::recvData() memset(szbuffer,0x00,sizeof(szbuffer)); offSize = 0; } - usleep(100); + mssleep(100); }else if(ret > 0){ print_info("ret = %d,buff = %s\n",ret,buff); memcpy(szbuffer + offSize,buff,ret); @@ -211,7 +211,7 @@ int Dial::recvData() print_info("szbuffer = %s\n",szbuffer); continue; } - usleep(500000); + mssleep(500000); } } int Dial::queryPin() diff --git a/jsonparse/SH_JsonCmd.cpp b/jsonparse/SH_JsonCmd.cpp index 158d2f7..203f424 100644 --- a/jsonparse/SH_JsonCmd.cpp +++ b/jsonparse/SH_JsonCmd.cpp @@ -473,7 +473,7 @@ std::string JsonData::JsonCmd_52() sql_ctl->DeleteTableData(T_DATA_INFO(TNAME),whereCon,0); sql_ctl->DeleteTableData(T_DATASTATIC_INFO(TNAME),whereCon,0); } - usleep(100); + mssleep(100); } return ""; } @@ -665,7 +665,7 @@ std::string JsonData::JsonCmd_Cgi_02(Param_02 ¶m) jsonVal["type"] = "SET"; if(param.mMode == 0){//本地校时 SetTime((unsigned long)param.mTimeStamp); - usleep(200); + mssleep(200); system("hwclock -w"); }else if(param.mMode == 1){//与服务器校时 char buf[256] = {0}; @@ -1077,7 +1077,7 @@ std::string JsonData::JsonCmd_Cgi_20(Param_20 ¶m) jsSystemInfo["zigbeePanId"] = GlobalConfig::ZigbeeInfo_G.PanID; jsSystemInfo["zigbeeChannel"] = GlobalConfig::ZigbeeInfo_G.Channel; jsSystemInfo["zigbeeAddr"] = GlobalConfig::ZigbeeInfo_G.MyAddr; - jsSystemInfo["GateWayVersion"] = ReadStrByOpt(SYSTEMINFOFILE, "Version", "GateWayVersion"); + jsSystemInfo["GateWayVersion"] = GlobalConfig::Version; jsSystemInfo["SystemVersion"] = ReadStrByOpt(SYSTEMINFOFILE, "Version", "SystemVersion"); jsSystemInfo["WebVersion"] = ReadStrByOpt(SYSTEMINFOFILE, "Version", "WebVersion"); jsSystemInfo["GateWayHwVesion"] = ReadStrByOpt(SYSTEMINFOFILE, "Version", "GateWayHwVesion"); diff --git a/localserver/SH_LocalServer.cpp b/localserver/SH_LocalServer.cpp index 8a1b061..dc3cd6e 100644 --- a/localserver/SH_LocalServer.cpp +++ b/localserver/SH_LocalServer.cpp @@ -72,7 +72,7 @@ void LocalServer::HandleFromServer(const char *pData_r, int pLen, const char *to std::string timestamp = recvBody["timestamp"].asString(); unsigned long itimestamp = atol(timestamp.c_str()); SetTime(itimestamp); - usleep(200); + mssleep(200); system("hwclock -w"); if (!recvValue["ZoneId"].isNull() && recvValue["ZoneId"].asString().length() > 0) { std::string zoneid = recvValue["ZoneId"].asString(); @@ -222,7 +222,7 @@ void LocalServer::HandleFromServer(const char *pData_r, int pLen, const char *to } std::string data = jd.JsonCmd_Cgi_41(param41,1); char whereCon[64] = { 0 }; - sprintf(whereCon, "dataNodeNo='%s'", param.mdataNodeNo.c_str()); + sprintf(whereCon, " UpdateFlag <> 1 "); string strData = sql_ctl->GetNodeConfigureInfor(whereCon); data_publish(strData.c_str(), GlobalConfig::Topic_G.mPubConfig.c_str()); @@ -528,7 +528,7 @@ try{ } std::string data = jd.JsonCmd_Cgi_41(param41); char whereCon[64] = { 0 }; - sprintf(whereCon, "dataNodeNo='%s'", param.mdataNodeNo.c_str()); + sprintf(whereCon, " UpdateFlag <> 1 "); string strData = sql_ctl->GetNodeConfigureInfor(whereCon); data_publish(strData.c_str(), GlobalConfig::Topic_G.mPubConfig.c_str()); return data; @@ -604,7 +604,7 @@ try{ } std::string data = jd.JsonCmd_Cgi_53(param53); char whereCon[64] = { 0 }; - sprintf(whereCon, "dataNodeNo='%s'", param.mdataNodeNo.c_str()); + sprintf(whereCon, "UpdateFlag <> 0 "); string strData = sql_ctl->GetNodeConfigureInfor(whereCon); data_publish(strData.c_str(), GlobalConfig::Topic_G.mPubConfig.c_str()); return data; diff --git a/main.cpp b/main.cpp index ba0492f..59651fe 100644 --- a/main.cpp +++ b/main.cpp @@ -153,11 +153,10 @@ int main(int argc, char *argv[]) GlobalConfig::threadStatus = 0; count = 0; } - if(count >= 180){ + if(count >= 30){ LOG_ERROR("===========threadStatus ========failed \n"); break; } - // data_publish(senddata.c_str(), GlobalConfig::Topic_G.mPubData.c_str()); } return 0; diff --git a/platform/SH_PlatformInit.cpp b/platform/SH_PlatformInit.cpp index 154fbb8..20dc19b 100644 --- a/platform/SH_PlatformInit.cpp +++ b/platform/SH_PlatformInit.cpp @@ -12,7 +12,7 @@ int GlobalConfig::LinkCount = 0; int GlobalConfig::net0Status = 1; -std::string GlobalConfig::Version = "3.2.3"; +std::string GlobalConfig::Version = "3.2.4"; std::string GlobalConfig::MacAddr_G = ""; std::string GlobalConfig::MacAddr_G2 = ""; std::string GlobalConfig::IpAddr_G = ""; diff --git a/threadfunc/SH_ThreadFunc.cpp b/threadfunc/SH_ThreadFunc.cpp index 6609522..94bf8c9 100644 --- a/threadfunc/SH_ThreadFunc.cpp +++ b/threadfunc/SH_ThreadFunc.cpp @@ -51,8 +51,37 @@ void CheckThread() while (GlobalConfig::QuitFlag_G) { GlobalConfig::threadStatus = 1; + sleep(1); + if(GlobalConfig::EnterZigBeeWaveTransmittingFlag_G == ENTER_TRANSMITTING_STATUS) { + GlobalConfig::EnterZigBeeWaveTransmittingCnt_G ++; + if(GlobalConfig::EnterZigBeeWaveTransmittingCnt_G >= 180) { + GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = NO_ENTER_TRANSMITTING_STATUS; + GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0; + LOG_ERROR("[---- ZigBee error--!] ZigBee PanID is 9999 over time for 3 minutes !\n"); + // 重新写入 0x8888 + unsigned short shortAddr = 0x8888; + pUart->modify_LocalAddr(0x8888); + GlobalConfig::Zigbee_G.MyAddr = 0x8888; + // 延时1秒 + sleep(1); + // 重新读回 GlobalConfig::ZigbeeInfo_G.PanID + pUart->UpdateZigbeeInfoCtrl(); + pUart->bUpdateconfig = false; + pUart->bUpdate = false; + pUart->bSendTimeStamp = false; + // 延时1秒 + sleep(1); + std::string str("8888"); + if( GlobalConfig::ZigbeeInfo_G.MyAddr.compare(str) == 0 ){ + LOG_INFO("[---- ZigBee INFO ----!] ZigBee PanID come back to be 8888 !\n"); + } + else { + LOG_ERROR("[---- ZigBee error--!] ZigBee PanID cannot come back to be 8888 !\n"); + } + } + continue; + } if (10 == heart_count) { - // StatusPub(); if (GlobalConfig::LinkCount > 30) { LOG_ERROR("MQTT connect failed\n"); #ifdef IMX6UL_GATEWAY @@ -106,7 +135,6 @@ void CheckThread() exit(0); } #endif - //bool status = Ping( GlobalConfig::ServerIP.c_str(), 10000); #ifdef NR5G_MODULE #ifdef IMX6UL_GATEWAY dial.closePort(); @@ -238,10 +266,7 @@ void CheckThread() LOG_INFO("Battery\n"); sql_ctl->CalculateBattery(); } -// if(10800 == UpdateZigbee){ -// UpdateZigbee = 0; -// pUart->UpdateZigbeeInfoCtrl(); -// } + if(3500 == loose_check){ LOG_INFO("loosecheck\n"); loose_check = 0; @@ -323,7 +348,12 @@ void CheckThread() char localtimestamp[32] = { 0 }; GetTimeNet(localtimestamp, 1); char whereCon[1024] = {0}; +#ifdef G2UL_GATEWAY + sprintf(whereCon," timestamp < '%ld' ",atol(localtimestamp) - 2592000 * 2); //删除2个月前的数据 +#endif +#ifdef IMX6UL_GATEWAY sprintf(whereCon," timestamp < '%ld' ",atol(localtimestamp) - 2592000); //删除1个月前的数据 +#endif sql_ctl->DeleteTableData(" t_battery_history ",whereCon); /*staticData = 0; Json::Value jsHeart; @@ -399,7 +429,6 @@ void CheckThread() heart_count++; online_check++; HardStatus ++; - //UpdateZigbee ++; logClean ++ ; loose_check ++; ModifyAddr ++; @@ -411,37 +440,6 @@ void CheckThread() wifi_reconnect_count ++; #endif - //rebootsystem ++; - if(GlobalConfig::EnterZigBeeWaveTransmittingFlag_G == ENTER_TRANSMITTING_STATUS) { - GlobalConfig::EnterZigBeeWaveTransmittingCnt_G ++; - if(GlobalConfig::EnterZigBeeWaveTransmittingCnt_G >= 180) { - GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = NO_ENTER_TRANSMITTING_STATUS; - GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0; - LOG_ERROR("[---- ZigBee error--!] ZigBee PanID is 9999 over time for 3 minutes !\n"); - // 重新写入 0x8888 - unsigned short shortAddr = 0x8888; - pUart->modify_LocalAddr(0x8888); - GlobalConfig::Zigbee_G.MyAddr = 0x8888; -// pUart->WriteShortAddr2Zigbee(shortAddr); - // 延时1秒 - sleep(1); - // 重新读回 GlobalConfig::ZigbeeInfo_G.PanID - pUart->UpdateZigbeeInfoCtrl(); - pUart->bUpdateconfig = false; - pUart->bUpdate = false; - pUart->bSendTimeStamp = false; - // 延时1秒 - sleep(1); - std::string str("8888"); - if( GlobalConfig::ZigbeeInfo_G.MyAddr.compare(str) == 0 ){ - LOG_INFO("[---- ZigBee INFO ----!] ZigBee PanID come back to be 8888 !\n"); - } - else { - LOG_ERROR("[---- ZigBee error--!] ZigBee PanID cannot come back to be 8888 !\n"); - } - } - } - sleep(1);; } } diff --git a/uart/SH_Uart.cpp b/uart/SH_Uart.cpp index 5e05978..b9552c9 100644 --- a/uart/SH_Uart.cpp +++ b/uart/SH_Uart.cpp @@ -44,6 +44,7 @@ int Uart::UartRecv(int fd, char srcshow,char* buffer) { if((unsigned short)GlobalConfig::Zigbee_G.MyAddr == 0x9999){ + memset(buff, 0, sizeof(buff)); ret = read_data(fd, buff, BUF_LENGTH, 10); if (ret <= 0 ){ @@ -61,14 +62,14 @@ int Uart::UartRecv(int fd, char srcshow,char* buffer) bModifyAddr = true; modify_LocalAddr(0x8888); bSendTimeStamp = false; - usleep(10000); - + mssleep(10000); + m_waveTrans = true; GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = NO_ENTER_TRANSMITTING_STATUS; GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0; - DealWave(); + LOG_DEBUG("wave end\n"); } - usleep(10000); + mssleep(10000); }else if(bUpdatePre || (bUpdateconfig && GlobalConfig::EnterZigBeeWaveTransmittingCnt_G > 15)){ timeoutflag ++; if(timeoutflag > 300){ @@ -84,15 +85,15 @@ int Uart::UartRecv(int fd, char srcshow,char* buffer) bModifyAddr = true; bSendTimeStamp = false; modify_LocalAddr(0x8888); - usleep(10000); + mssleep(10000); GlobalConfig::Zigbee_G.MyAddr = 0x8888; } - usleep(10000); + mssleep(10000); } } else if(ret > 0){ maxSize += ret; - //print_debug("0x9999===str_recv===,ret = %d offSize = %d,bUpdatePre = %d,bUpdateconfig = %d\n",ret,maxSize,bUpdatePre,bUpdateconfig); + print_debug("0x9999===str_recv===,ret = %d offSize = %d,bUpdatePre = %d,bUpdateconfig = %d\n",ret,maxSize,bUpdatePre,bUpdateconfig); // for(int i = 0; i < ret;i++){ // printf("[%02x]", buff[i]&0xff); // } @@ -161,7 +162,7 @@ int Uart::UartRecv(int fd, char srcshow,char* buffer) timeoutflag = 0; offSize = 0; maxSize = 0; - usleep(10000); + mssleep(10000); } }else if(ret > 0){ maxSize += ret; @@ -177,7 +178,7 @@ int Uart::UartRecv(int fd, char srcshow,char* buffer) } #endif } - usleep(50); + //mssleep(50); } } @@ -324,7 +325,7 @@ void Uart::ReadTestUart() pTestRecv(0); } } - usleep(20000); + mssleep(20000); } } @@ -332,14 +333,14 @@ int Uart::ZigbeeTest() { char buff[BUF_LENGTH] = {0x00}; modify_Localchannel(22); - usleep(100000); + mssleep(100000); modify_LocalPanID(2222); - usleep(100000); + mssleep(100000); modify_LocalAddr(6666); - usleep(100000); + mssleep(100000); return 0; } @@ -577,7 +578,7 @@ int Uart::FindRecvPackage(int bytesRead, char* mUartRecvBuf,char* head) GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0; //break; }/*else if(m_strDestShortAddr != strShortAddr){ - usleep(100000); + mssleep(100000); waittime ++; }*/ @@ -806,7 +807,7 @@ void Uart::ReadHandle(char* pUartRecvBuf,size_t bytesRead) bUpdate = false; m_strDestShortAddr = ""; }else if(m_strDestShortAddr != strShortAddr){ - usleep(100000); + mssleep(100000); waittime ++; return ; } @@ -954,7 +955,7 @@ void Uart::UpdateZigbeeInfo(const char *pData) // unsigned short panid = lShortAddr & 0xffff; // //modify_LocalPanID(panid); // WritePanId2Zigbee(panid); -// usleep(100000); +// mssleep(100000); // LOG_ERROR("PanID error"); // } @@ -967,7 +968,7 @@ void Uart::UpdateZigbeeInfo(const char *pData) // { // //modify_Localchannel(Chan); // WriteChanl2Zigbee(Chan); -// usleep(100000); +// mssleep(100000); // } // LOG_ERROR("channel error"); // } @@ -1086,6 +1087,12 @@ bool Uart::ReadUpdatePackge(unsigned char* pDestShortAddr) Data[11]=tmp; sleep(1); WriteToUart((const char*)Data,12); + int iRet = CheckZigbeeACK(); + if(iRet == 0){ + LOG_DEBUG("Packge ACK send success,shortAddr = %02x%02x\n",pDestShortAddr[0],pDestShortAddr[1]); + }else{ + LOG_ERROR("Packge ACK send failed,shortAddr = %02x%02x\n",pDestShortAddr[0],pDestShortAddr[1]); + } string strTime = GetLocalTimeWithMs(); LOG_INFO("ReadUpdatePackge strTime = %s\n",strTime.c_str()); return true; @@ -1131,7 +1138,7 @@ void Uart::UpdateWirelessNode(unsigned short shortAdd) unsigned char tmp = 0x00; gpio_set(GlobalConfig::GPIO_G.zigAckreset,0); //boost::this_thread::sleep(boost::posix_time::milliseconds(1)); - usleep(1000); + mssleep(1000); gpio_set(GlobalConfig::GPIO_G.zigAckreset,1); for(int j = 0; j < Count;j++){ int time ,value; @@ -1157,7 +1164,7 @@ void Uart::UpdateWirelessNode(unsigned short shortAdd) value = gpio_read(GlobalConfig::GPIO_G.zigAckrep); if(value == 49) break; - usleep(10000); + mssleep(10000); time += 1; }while(time < 150); if(time >= 150){ @@ -1174,12 +1181,12 @@ void Uart::UpdateWirelessNode(unsigned short shortAdd) } } gpio_set(GlobalConfig::GPIO_G.zigAckreset,0); - usleep(2000); + mssleep(2000); // if(gpio_read(GlobalConfig::GPIO_G.zigAckrep) == 48) // gpio_set(GlobalConfig::GPIO_G.zigAckreset,1); memset(UpdateData,0x00,sizeof(UpdateData)); //boost::this_thread::sleep(boost::posix_time::milliseconds(5)); - usleep(5000); + mssleep(5000); } if(gpio_read(GlobalConfig::GPIO_G.zigAckrep) == 48) gpio_set(GlobalConfig::GPIO_G.zigAckreset,1); @@ -1210,12 +1217,12 @@ void Uart::UpdateWirelessNode(unsigned short shortAdd) int value = gpio_read(GlobalConfig::GPIO_G.zigAckrep); if(value == 49){ gpio_set(GlobalConfig::GPIO_G.zigAckreset,0); - usleep(10000); + mssleep(10000); if(gpio_read(GlobalConfig::GPIO_G.zigAckrep) == 48) gpio_set(GlobalConfig::GPIO_G.zigAckreset,1); break; } - usleep(10000); + mssleep(10000); time += 1; }while(time < 150); if(time >= 150){ @@ -1412,6 +1419,12 @@ int Uart::UpdateConfig(unsigned char* pDestShortAddr) UpdateData[99] = tmp1; tcflush(fd,TCIFLUSH); WriteToUart((const char*)UpdateData,100); + int iRet = CheckZigbeeACK(); + if(iRet == 0){ + LOG_DEBUG("updataconfig ACK send success,shortAddr = %02x%02x\n",pDestShortAddr[0],pDestShortAddr[1]); + }else{ + LOG_ERROR("updataconfig ACK send failed,shortAddr = %02x%02x\n",pDestShortAddr[0],pDestShortAddr[1]); + } return 0; }else if(vecResultNode[41] == "-1"){ @@ -1451,6 +1464,12 @@ int Uart::UpdateConfig(unsigned char* pDestShortAddr) UpdateData[99] = tmp1; tcflush(fd,TCIFLUSH); WriteToUart((const char*)UpdateData,100); + int iRet = CheckZigbeeACK(); + if(iRet == 0){ + LOG_DEBUG("updataname ACK send success,shortAddr = %02x%02x\n",pDestShortAddr[0],pDestShortAddr[1]); + }else{ + LOG_ERROR("updataname ACK send failed,shortAddr = %02x%02x\n",pDestShortAddr[0],pDestShortAddr[1]); + } return 0; } @@ -1466,7 +1485,7 @@ void Uart::UpdateWirelessNodeTime(unsigned char* pDestShortAddr,int modifyaddr/* { if(modifyaddr) modify_DistAddr(pDestShortAddr); - usleep(10000); + mssleep(10000); //print_info("nodewaveindex = %d,nodetime = %d,nodeindex = %d\n",nodewaveindex,nodetime,nodeindex); char localtimestamp[32]={0x00}; int millisecond = 0; @@ -1504,6 +1523,12 @@ void Uart::UpdateWirelessNodeTime(unsigned char* pDestShortAddr,int modifyaddr/* } UpdateData[99] = tmp; WriteToUart((const char*)UpdateData,100); + int iRet = CheckZigbeeACK(); + if(iRet == 0){ + LOG_DEBUG("NodeTime ACK send success,shortAddr = %02x%02x\n",pDestShortAddr[0],pDestShortAddr[1]); + }else{ + LOG_ERROR("NodeTime ACK send failed,shortAddr = %02x%02x\n",pDestShortAddr[0],pDestShortAddr[1]); + } } void Uart::DealRecvData(const char *pData) { @@ -2000,17 +2025,17 @@ void Uart::DealDataNodeFeature(const char *pData, int flag) { bSendTimeStamp = true; modify_distaddr_info(0x9999,"",pRecvData->ShortAddr);//临时参数配置 - usleep(10000); + mssleep(10000); LOG_DEBUG("Zigbee Signal !\n"); int Times = 0; - usleep(20000); + mssleep(20000); while(Times < 3 ){ getZigbeeSignal(pRecvData->ShortAddr); Times ++ ; - usleep(20000); + mssleep(20000); } - usleep(10000); + mssleep(10000); UpdateWirelessNodeTime((unsigned char*)pRecvData->ShortAddr,0); }else { @@ -3463,49 +3488,50 @@ void Uart::DealWaveThread() //连续三秒没有原始数据,则处理缓存 while (1) { - // 接收到原始波形,则 m_TimeStamp 不为零 - // 如果当前时间与记录时间超过3秒,要求,m_TimeStamp不变化,而数据在传输,则一定小于3秒 - if (0 == m_TimeStamp) { - sleep(1); - continue; - } - char localtimestamp[32] = { 0 }; - GetTimeNet(localtimestamp, 1); - nowTimeStamp = strtol(localtimestamp, NULL, 10); - // 避免在未同步时钟导致数据错误 - if(nowTimeStamp >= m_TimeStamp) { - tmpTimeStamp = nowTimeStamp - m_TimeStamp; - } - else { - tmpTimeStamp = m_TimeStamp - nowTimeStamp; - } - //if ((nowTimeStamp - m_TimeStamp) > 3) { 时间戳需要修改为绝对值,可能丢失时钟,或工作一会儿,才同步时钟,可能减出异常值 - if (tmpTimeStamp > 3 ) { // TODO: 时间戳需要修改为绝对值,可能丢失时钟,或工作一会儿,才同步时钟,可能减出异常值 print_info("yes!The time difference is more than 3,nowTimeStamp : %ld m_TimeStamp : %ld\n", nowTimeStamp, m_TimeStamp); - //DealWave(); - m_TimeStamp = 0; - offSize = 0; - GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = NO_ENTER_TRANSMITTING_STATUS; - GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0; - // 准备重新恢复到 8888 PanID,正常接收特征数据 - mPackgeIndex = -1; - //WriteLocalAddr(0x8888); - //GlobalConfig::Zigbee_G.MyAddr = 0x8888; +// // 接收到原始波形,则 m_TimeStamp 不为零 +// // 如果当前时间与记录时间超过3秒,要求,m_TimeStamp不变化,而数据在传输,则一定小于3秒 +// if (0 == m_TimeStamp) { +// sleep(1); +// continue; +// } +// char localtimestamp[32] = { 0 }; +// GetTimeNet(localtimestamp, 1); +// nowTimeStamp = strtol(localtimestamp, NULL, 10); +// // 避免在未同步时钟导致数据错误 +// if(nowTimeStamp >= m_TimeStamp) { +// tmpTimeStamp = nowTimeStamp - m_TimeStamp; +// } +// else { +// tmpTimeStamp = m_TimeStamp - nowTimeStamp; +// } +// //if ((nowTimeStamp - m_TimeStamp) > 3) { 时间戳需要修改为绝对值,可能丢失时钟,或工作一会儿,才同步时钟,可能减出异常值 +// if (tmpTimeStamp > 3 ) { // TODO: 时间戳需要修改为绝对值,可能丢失时钟,或工作一会儿,才同步时钟,可能减出异常值 print_info("yes!The time difference is more than 3,nowTimeStamp : %ld m_TimeStamp : %ld\n", nowTimeStamp, m_TimeStamp); +// //DealWave(); +// m_TimeStamp = 0; +// offSize = 0; +// GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = NO_ENTER_TRANSMITTING_STATUS; +// GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0; +// // 准备重新恢复到 8888 PanID,正常接收特征数据 +// mPackgeIndex = -1; +// //WriteLocalAddr(0x8888); +// //GlobalConfig::Zigbee_G.MyAddr = 0x8888; -// WriteShortAddr2Zigbee(0x8888); -// UpdateZigbeeInfoCtrl(); - - } else { - print_info("NO! The time difference is less than 3,nowTimeStamp : %ld m_TimeStamp : %ld\n", nowTimeStamp, m_TimeStamp); - } +// // WriteShortAddr2Zigbee(0x8888); +// // UpdateZigbeeInfoCtrl(); +// } else { +// print_info("NO! The time difference is less than 3,nowTimeStamp : %ld m_TimeStamp : %ld\n", nowTimeStamp, m_TimeStamp); +// } + DealWave(); sleep(1); } + } void Uart::DealWave() { - LOG_DEBUG("begin deal Wave data !\n"); - print_blue("wave data size is(m_VecWaveData.size) : %d\n",m_VecWaveData.size()); + //LOG_DEBUG("begin deal Wave data !\n"); + //print_blue("wave data size is(m_VecWaveData.size) : %d\n",m_VecWaveData.size()); std::string strShortAddr = ""; std::string strShortAddrTemp; std::string strLongAddr = ""; @@ -3518,7 +3544,7 @@ void Uart::DealWave() char buf[8]; RecvData recvTemp; - LOG_INFO("all wave data size is(m_VecWaveData.size) : %d \n",\ + //LOG_INFO("all wave data size is(m_VecWaveData.size) : %d \n",\ m_VecWaveData.size()); while (m_VecWaveData.size() > 0) { //对每个传感器的每个通道进行遍历然后处理数据,例如:传感器1x轴的数据处理完后,再去处理y轴的。传感器1的所有数据处理完后,再处理传感器2的 std::vector::iterator iter = m_VecWaveData.begin(); @@ -3715,6 +3741,11 @@ void Uart::DealWave() strWaveData = waveTemp; else strWaveData = strWaveData + "," + waveTemp; + + if (i % 100 == 0) + { + mssleep(5000); + } } fclose(fp); @@ -3734,29 +3765,29 @@ void Uart::DealWave() if(res[17] == "01"){ //print_blue("vecData.size() = %d,sampleRate * ACCSampleTime = %d,iChannel = %d\n",vecData.size(),sampleRate * ACCSampleTime,iChannel); - if(vecData.size() < sampleRate * ACCSampleTime && iChannel == 3 ){//过滤数据包结尾空数据 + if(vecData.size() != sampleRate * ACCSampleTime && iChannel == 3 ){//过滤数据包结尾空数据 print_blue("%d vecData.size() == %d,sampleRate * ACCSampleTime = %d\n",iChannel,vecData.size(),sampleRate * ACCSampleTime); std::vector().swap(vecData); continue; } - if(vecData.size() < sampleRate * ACCSampleTime && iChannel == 4 ){//过滤数据包结尾空数据 + if(vecData.size() != sampleRate * ACCSampleTime && iChannel == 4 ){//过滤数据包结尾空数据 print_blue("%d vecData.size() == %d,sampleRate * ACCSampleTime = %d\n",iChannel,vecData.size(),sampleRate * ACCSampleTime); std::vector().swap(vecData); continue; } - if(vecData.size() < sampleRate * ACCSampleTime && iChannel == 5 ){//过滤数据包结尾空数据 + if(vecData.size() != sampleRate * ACCSampleTime && iChannel == 5 ){//过滤数据包结尾空数据 print_blue("%d vecData.size() == %d,sampleRate * ACCSampleTime = %d\n",iChannel,vecData.size(),sampleRate * ACCSampleTime); std::vector().swap(vecData); continue; } }else if(res[17] == "02"){ - if(vecData.size() < 8192 && iChannel == 3 ){//过滤数据包结尾空数据 + if(vecData.size() != 8192 && iChannel == 3 ){//过滤数据包结尾空数据 continue; } - if(vecData.size() < 8192 && iChannel == 4 ){//过滤数据包结尾空数据 + if(vecData.size() != 8192 && iChannel == 4 ){//过滤数据包结尾空数据 continue; } - if(vecData.size() < sampleRate * ACCSampleTime && iChannel == 5 ){//过滤数据包结尾空数据 + if(vecData.size() != sampleRate * ACCSampleTime && iChannel == 5 ){//过滤数据包结尾空数据 continue; } } @@ -3885,12 +3916,17 @@ void Uart::DealWave() std::vector().swap(vecData); sleep(1); } - std::vector().swap(m_VecWaveData); - char whereCon[1024] = {0x00}; - char updateSql[1024] = {0x00}; - sprintf(whereCon, "dataNodeNo='%s'", strLongAddr.c_str()); - sprintf(updateSql, "WaveTime = WaveTime + 1"); - sql_ctl->UpdateTableData(T_SENSOR_INFO(TNAME), updateSql, whereCon); + if (strLongAddr != "") + { + std::vector().swap(m_VecWaveData); + char whereCon[1024] = {0x00}; + char updateSql[1024] = {0x00}; + sprintf(whereCon, "dataNodeNo='%s'", strLongAddr.c_str()); + sprintf(updateSql, "WaveTime = WaveTime + 1"); + sql_ctl->UpdateTableData(T_SENSOR_INFO(TNAME), updateSql, whereCon); + } + + //string strData = sql_ctl->GetNodeConfigureInfor(whereCon); //data_publish(strData.c_str(), GlobalConfig::Topic_G.mPubConfig.c_str()); @@ -4003,7 +4039,7 @@ void Uart::modify_distaddr_info(unsigned short id, char * zigbee,unsigned char* command[5] = ((char *)&id)[0]; WriteToUart(command, 6); - usleep(10000); + mssleep(10000); memset(command,0x00,sizeof(command)); command[0] = 0xDE; command[1] = 0xDF; @@ -4017,7 +4053,7 @@ void Uart::modify_distaddr_info(unsigned short id, char * zigbee,unsigned char* } void Uart::getZigbeeSignal(unsigned char* distAddr) { - usleep(10000); + mssleep(10000); char command[6] = {0x00}; command[0] = 0xDE; command[1] = 0xDF; @@ -4138,7 +4174,7 @@ void Uart::WriteChanl2Zigbee(unsigned char pad) modify_info(tmp, (char *)& GlobalConfig::Zigbee_G); gpio_set(GlobalConfig::GPIO_G.zigReset,0); - usleep(10000); + mssleep(10000); gpio_set(GlobalConfig::GPIO_G.zigReset,1); } @@ -4154,7 +4190,7 @@ void Uart::WritePanId2Zigbee(unsigned short pad) modify_info(tmp, (char *)&GlobalConfig::Zigbee_G); gpio_set(GlobalConfig::GPIO_G.zigReset,0); - usleep(10000); + mssleep(10000); gpio_set(GlobalConfig::GPIO_G.zigReset,1); } void Uart::WriteSpeed2Zigbee() @@ -4166,13 +4202,13 @@ void Uart::WriteSpeed2Zigbee() tmp = GlobalConfig::Zigbee_G.MyAddr; modify_info(tmp, (char *)&GlobalConfig::Zigbee_G); gpio_set(GlobalConfig::GPIO_G.zigReset,0); - usleep(10000); + mssleep(10000); gpio_set(GlobalConfig::GPIO_G.zigReset,1); } /*void Uart::WriteLocalAddr(unsigned short id) { gpio_set(116,0); - usleep(10000); + mssleep(10000); gpio_set(116,1); sleep(1); char command[8]={0x00}; @@ -4185,9 +4221,9 @@ void Uart::WriteSpeed2Zigbee() command[6] = 0x00; command[7] = 0xaa; WriteToUart(command, 8); - usleep(600000); + mssleep(600000); gpio_set(116,0); - usleep(10000); + mssleep(10000); gpio_set(116,1); }*/ void Uart::WriteTranTimeout2Zigbee(unsigned char Time) @@ -4202,7 +4238,7 @@ void Uart::WriteTranTimeout2Zigbee(unsigned char Time) modify_info(tmp, (char *)&GlobalConfig::Zigbee_G); gpio_set(GlobalConfig::GPIO_G.zigReset,0); - usleep(10000); + mssleep(10000); gpio_set(GlobalConfig::GPIO_G.zigReset,1); } @@ -4217,7 +4253,7 @@ void Uart::WriteShortAddr2Zigbee(unsigned short pad) modify_info(tmp, (char *)& GlobalConfig::Zigbee_G); gpio_set(GlobalConfig::GPIO_G.zigReset,0); - usleep(10000); + mssleep(10000); gpio_set(GlobalConfig::GPIO_G.zigReset,1); sleep(1); } @@ -4239,10 +4275,25 @@ void Uart::WriteShortAddr_DistAddr2Zigbee(unsigned short pad,unsigned char* pDes modify_info(tmp, (char *)& GlobalConfig::Zigbee_G); gpio_set(GlobalConfig::GPIO_G.zigReset,0); - usleep(10000); + mssleep(10000); gpio_set(GlobalConfig::GPIO_G.zigReset,1); } - +int Uart::CheckZigbeeACK() +{ + if(gpio_read(GlobalConfig::GPIO_G.zigAckrep) == 48) + gpio_set(GlobalConfig::GPIO_G.zigAckreset,1); + int time = 0,value = 0,iRet = -1; + do{ + value = gpio_read(GlobalConfig::GPIO_G.zigAckrep); + if(value == 49) + { + iRet = 0; + break; + } + mssleep(10000); + }while(time < 150); + return iRet; +} void Uart::ZigbeeInit() { std::string strPanId = sql_ctl->GetData("t_gateway_info","zigbeePanID",NULL); @@ -4252,7 +4303,7 @@ void Uart::ZigbeeInit() //GlobalConfig::Zigbee_G.MyAddr = shortAddr; //WriteLocalAddr(shortAddr); WriteShortAddr2Zigbee(shortAddr); - usleep(100000); + mssleep(100000); // 更新GlobalConfig::ZigbeeInfo_G.MyAddr,用于外部显示 } @@ -4272,7 +4323,7 @@ void Uart::ZigbeeInit() { WriteChanl2Zigbee(Chan); //modify_Localchannel(Chan); - usleep(100000); + mssleep(100000); } } print_info("PanID1 = %s,strPanId = %s\n",GlobalConfig::ZigbeeInfo_G.PanID.c_str(),strPanId.c_str()); @@ -4283,7 +4334,7 @@ void Uart::ZigbeeInit() //modify_LocalPanID(panid); WritePanId2Zigbee(panid); - usleep(100000); + mssleep(100000); } //if (0 != GlobalConfig::ZigbeeInfo_G.MyAddr.compare("8888")) diff --git a/uart/SH_Uart.hpp b/uart/SH_Uart.hpp index 62c4a9b..ab227e6 100644 --- a/uart/SH_Uart.hpp +++ b/uart/SH_Uart.hpp @@ -104,6 +104,7 @@ public : virtual void ThreadInit(); int UartRecv(int fd, char srcshow,char* buffer); void openSwitch(); + int CheckZigbeeACK(); private : void ReadHandle(char* pUartRecvBuf,size_t bytesRead); void WriteHandle(const char *strSend,const boost::system::error_code &ec,size_t bytesWrite); diff --git a/wifi/wpa_client.cpp b/wifi/wpa_client.cpp index 51a43c3..539cb39 100644 --- a/wifi/wpa_client.cpp +++ b/wifi/wpa_client.cpp @@ -87,7 +87,7 @@ bool MXDHCP::Start(const std::string & net_interface) } std::string cmd = "udhcpc -b -i " + net_interface + " &"; system("killall -9 udhcpc &"); - usleep(1000*100); + mssleep(1000*100); pstream = popen(cmd.data(),"r"); if(pstream == NULL) {