diff --git a/uart/uart.cpp b/uart/uart.cpp index b46b7d6..52ab60d 100644 --- a/uart/uart.cpp +++ b/uart/uart.cpp @@ -46,7 +46,7 @@ int Uart::UartRecv(int fd, char srcshow, char *buffer) { ret = read_data(fd, buff, BUF_LENGTH, 10); if (ret <= 0) { timeoutflag++; - if (timeoutflag > 500) { + if (timeoutflag > 300) { DealReviveDuration(wave_shortAddr); zlog_warn(zct, "===============0x9999 timeout= %d offSize = %d===============shortAddr = %02x%02x", timeoutflag, offSize,UINT16_HIGH(wave_shortAddr),UINT16_LOW(wave_shortAddr)); zlog_warn(zct, "0x9999 timeout %d===============Size = %d", timeoutflag, offSize); @@ -75,7 +75,7 @@ int Uart::UartRecv(int fd, char srcshow, char *buffer) { maxSize = 0; tcflush(fd, TCIOFLUSH); } - + zlog_info(zct, "0x8888 ===str_recv===,offSize = %d,ret = %d", offSize,ret); } } else { @@ -321,7 +321,9 @@ void Uart::GetLocalZigbeeRSSI(uint16_t ushortAdd){ zlog_info(zct, "Local Zigbee Signal !\n"); mssleep(20000); getZigbeeSignal(ushortAdd); - mssleep(10000); + mssleep(20000); + getZigbeeSignal(ushortAdd); + mssleep(20000); } int Uart::DealAskTask(uint16_t ushortAdd){ char wherecon[50]={0}; @@ -333,50 +335,63 @@ int Uart::DealAskTask(uint16_t ushortAdd){ return 1; } ModifyDistAddr(ushortAdd); - int next_duration = 0; + int next_duration = 0,next_task_id = 0; int taskID; - scheduler::instance().StartSchedule(ushortAdd,next_duration, taskID); ScheduleTask scheduleTask; - zlog_info(zct, "taskID = %d ", taskID); - if (taskID == kScheduleEigenValue) //1.特征值 - { - TaskResp(scheduleTask); - } - else if (taskID == kScheduleWaveForm) //2.波形 - { - wave_shortAddr = ushortAdd; - GetLocalZigbeeRSSI(ushortAdd); - scheduleTask.cmd = SIGNAL_STRENGTH; - scheduleTask.shortAddr = ushortAdd; - scheduleTask.duration = next_duration; - TaskResp(scheduleTask); - - } - else if (taskID == kScheduleUpgrade) //3.升级 - { - UpdateWirelessNode(ushortAdd); - }else if (taskID == kScheduleConfigSensor) //4.更新配置 + taskID = scheduler::instance().StartSchedule(ushortAdd,next_duration, next_task_id); + zlog_info(zct, "taskID = %d next_duration = %d next_task_id = %d", taskID, next_duration, next_task_id); + if (taskID == kScheduleConfigSensor) { scheduleTask.cmd = CONFIG; + scheduleTask.duration = next_duration; + scheduleTask.next_taskID = next_task_id; scheduleTask.shortAddr = ushortAdd; mssleep(50000); TaskResp(scheduleTask); mssleep(50000); UpdateConfig(ushortAdd); - }else if (taskID == kScheduleWrongTime) //5.异常连接 - { + } + if(next_task_id == kScheduleWaveForm){ scheduleTask.cmd = REVIVE_DURATION; scheduleTask.shortAddr = ushortAdd; scheduleTask.duration = next_duration; - zlog_info(zct, "next_duration = %d ", next_duration); + scheduleTask.next_taskID = WAVE_CMD; TaskResp(scheduleTask); - }else if (taskID == 13) //6.RSSI - { - scheduleTask.cmd = SIGNAL_STRENGTH; + }else if (next_task_id == kScheduleEigenValue) { + scheduleTask.cmd = REVIVE_DURATION; scheduleTask.shortAddr = ushortAdd; scheduleTask.duration = next_duration; + scheduleTask.next_taskID = next_task_id; TaskResp(scheduleTask); } + + // if (next_task_id == kScheduleEigenValue) //1.特征值 + // { + + // } + // else if (next_task_id == kScheduleWaveForm) //2.波形 + // { + // DealReviveDuration(ushortAdd); + // } + // else if (next_task_id == kScheduleUpgrade) //3.升级 + // { + // UpdateWirelessNode(ushortAdd); + // }else if (taskID == kScheduleConfigSensor) //4.更新配置 + // { + // scheduleTask.cmd = CONFIG; + // scheduleTask.shortAddr = ushortAdd; + // mssleep(50000); + // TaskResp(scheduleTask); + // mssleep(50000); + // UpdateConfig(ushortAdd); + // }else if (taskID == kScheduleWrongTime) //5.异常连接 + // { + // scheduleTask.cmd = REVIVE_DURATION; + // scheduleTask.shortAddr = ushortAdd; + // scheduleTask.duration = next_duration; + // zlog_info(zct, "next_duration = %d ", next_duration); + // TaskResp(scheduleTask); + // } zlog_info(zct, " DealAskTask end" ); return 0; } @@ -386,21 +401,14 @@ int Uart::DealException(const char* pData){ } int Uart::DealReviveDuration(uint16_t ushortAdd){ - - char localtimestamp[32] = {0x00}; ScheduleTask scheduleTask; int next_taskID = 0; uint16_t next_duration = scheduler::instance().GetNextDuration(ushortAdd,next_taskID); - next_taskID = MEAS_EVAL; - zlog_info(zct, "next_duration = %d ", next_duration); + zlog_info(zct, "next_duration = %d next_taskID = %d", next_duration,next_taskID); scheduleTask.cmd = REVIVE_DURATION; scheduleTask.shortAddr = ushortAdd; scheduleTask.duration = next_duration; scheduleTask.next_taskID = next_taskID & 0xFF; - int millisecond = 0; - std::string rtcTime = GetRTC(localtimestamp, millisecond); - scheduleTask.timeStamp = atoi(localtimestamp); - scheduleTask.millisecond = millisecond; TaskResp(scheduleTask); return 0; } @@ -419,6 +427,7 @@ int Uart::DealWaveCompress(const char *pData,uint16_t ushortAdd){ zlog_info(zct, "DealWaveCompress "); now_task = WAVE_CMD; + WaveResp(ushortAdd); char buf[20] = {0x00}; sprintf(buf, "%02x%02x", (ushortAdd >> 8) & 0xFF, ushortAdd & 0xFF); std::string strShortAddr = std::string(buf); @@ -432,7 +441,7 @@ int Uart::DealWaveCompress(const char *pData,uint16_t ushortAdd){ tempchannel.CountZ = BUILD_UINT32(pData[21], pData[20],pData[19],pData[18]); g_mapCompress[strShortAddr] = tempchannel; - + wave_shortAddr = ushortAdd; char sensor_rssi[10] = {0x00}; sprintf(sensor_rssi, "%02d", pData[22] & 0xFF); if (!strcmp(sensor_rssi, "00") || !strcmp(sensor_rssi, "0")) { @@ -440,7 +449,7 @@ int Uart::DealWaveCompress(const char *pData,uint16_t ushortAdd){ sprintf(errorInfo, "No RSSI %s", sensor_rssi); zlog_error(zct, errorInfo); } else { - char tableName[100] = {0x00}; + char tableName[100] = {0x00}; char whereCon[100] = {0}; char updateSql[100] = {0}; sprintf(whereCon, "zigbeeShortAddr='%02x%02x'", UINT16_HIGH(ushortAdd),UINT16_LOW(ushortAdd)); @@ -459,7 +468,7 @@ int Uart::DealWaveCompress(const char *pData,uint16_t ushortAdd){ memset(updateSql,0,sizeof(updateSql)); memset(whereCon,0,sizeof(whereCon)); if (vParamRSSI.size() > 0) { - sprintf(updateSql, "RSSI = '%s,%02d' ", vParamRSSI[0].c_str(), pData[7] & 0xFF); + sprintf(updateSql, "RSSI = '%s,%02d' ", vParamRSSI[0].c_str(), pData[22] & 0xFF); sprintf(whereCon, "dataNodeNo='%s'", (char *)vecDataNodeNo[0].c_str()); sqlite_db_ctrl::instance().UpdateTableData(T_SENSOR_INFO(TNAME), updateSql, whereCon); } @@ -542,7 +551,9 @@ void Uart::DealRecvData(const char *pData) { uint16_t ushortAdd = BUILD_UINT16(pData[3] & 0xFF, pData[4] & 0xFF); uint8_t command = pData[5] & 0xFF; uint8_t recvcode = pData[7] & 0xFF; - + int next_duration = 0,next_task_id = 0; + int taskID = 0; + ScheduleTask scheduleTask; zlog_info(zct, "shortAdd = %02x%02x,command = %d ",UINT16_HIGH(ushortAdd),UINT16_LOW(ushortAdd),command); switch (command) { case DEVICE_INF: @@ -558,8 +569,10 @@ void Uart::DealRecvData(const char *pData) { DealException(pData); break; case MEAS_EVAL: - DealReviveDuration(ushortAdd); + DealAskTask(ushortAdd); DealDataNodeFeature(pData, 0); + + break; case UPGRADE: if (recvcode == 0){ @@ -586,10 +599,21 @@ void Uart::DealRecvData(const char *pData) { } break; case WAVE_COMPRESS: + GetLocalZigbeeRSSI(ushortAdd); DealWaveCompress(pData,ushortAdd); break; - case SIGNAL_STRENGTH: - DealSensorRSSI(pData,ushortAdd); + case UPGRADE_ASK: + taskID = scheduler::instance().StartSchedule(ushortAdd,next_duration, next_task_id); + if (taskID == kScheduleUpgrade){ + UpdateWirelessNode(ushortAdd); + }else if(taskID == kScheduleResultNone){ + scheduleTask.cmd = REVIVE_DURATION; + scheduleTask.shortAddr = ushortAdd; + scheduleTask.duration = next_duration; + scheduleTask.next_taskID = next_task_id; + TaskResp(scheduleTask); + } + break; default: break; } @@ -697,6 +721,7 @@ void Uart::DealDataNodeInfo(const char *pData) { zlog_warn(zct, "recv remote zigbee module info shortAddr %02x%02x", pRecvData->Data[46], pRecvData->Data[47]); char buf[32] = {0}; char whereCon[64] = {0}; + uint16_t ushortAdd = BUILD_UINT16(pData[3] & 0xFF, pData[4] & 0xFF); char chTemp = pRecvData->Data[0]; //设备状态标志 1 byte DataNodeInfo dataNodeInfo; dataNodeInfo.EquipSta = GET_BIT(chTemp, 2); @@ -843,7 +868,6 @@ void Uart::DealDataNodeInfo(const char *pData) { zlog_info(zct, "upgrade = %d", upgrade); if (upgrade == 1) { - uint16_t ushortAdd = BUILD_UINT16(pData[3] & 0xFF, pData[4] & 0xFF); sprintf(whereCon, "dataNodeNo='%s'", dataNodeInfo.ZigbeeLongAddr.c_str()); std::string soft_version = sqlite_db_ctrl::instance().GetData(T_SENSOR_INFO(TNAME), " softVersion ", whereCon); if (dataNodeInfo.SoftVersion != soft_version) { @@ -1204,7 +1228,7 @@ int Uart::FindRecvPackage(int bytesRead, char *mUartRecvBuf, char *head) { memcpy(RecvBuf, (char *)&UartRecvBuf[i], 100); DealDataNodeWave(RecvBuf, command); mPackgeIndex = (unsigned int)UartRecvBuf[i + 6]; - } else if (now_task != WAVE_CMD && (command == ASK_TASK || command == DEVICE_INF || command == MEAS_EVAL || command == CONFIG || command == UPGRADE || command == DEVICE_INF2 || command == SIGNAL_STRENGTH || command == DEVICE_EXCEPTION || command == WAVE_COMPRESS)) { + } else if (now_task != WAVE_CMD && (command == ASK_TASK || command == DEVICE_INF || command == MEAS_EVAL || command == CONFIG || command == UPGRADE || command == DEVICE_INF2 || command == SIGNAL_STRENGTH || command == DEVICE_EXCEPTION)) { char RecvBuf[100] = {0x00}; if (command == ASK_TASK ) { @@ -1214,7 +1238,7 @@ int Uart::FindRecvPackage(int bytesRead, char *mUartRecvBuf, char *head) { break; } } - if (command == CONFIG || command == CONFIG_INF2 || command == UPGRADE || command == SIGNAL_STRENGTH || command == DEVICE_EXCEPTION || command == WAVE_COMPRESS) + if (command == CONFIG || command == CONFIG_INF2 || command == UPGRADE || command == SIGNAL_STRENGTH || command == DEVICE_EXCEPTION) { memcpy(RecvBuf, &UartRecvBuf[i], 9); if (!CheckCrc(RecvBuf, 8)) { @@ -1232,9 +1256,9 @@ int Uart::FindRecvPackage(int bytesRead, char *mUartRecvBuf, char *head) { } DealRecvData(RecvBuf); - }else if(now_task == WAVE_CMD && command == WAVE_COMPRESS){ - memcpy(RecvBuf, &UartRecvBuf[i], 23); - if (!CheckCrc(RecvBuf, 22)) { + }else if( command == WAVE_COMPRESS && now_task != WAVE_CMD) { + memcpy(RecvBuf, &UartRecvBuf[i], 24); + if (!CheckCrc(RecvBuf, 23)) { zlog_warn(zct, "CheckCrc error ShortAddr :%s command = %d ", strShortAddr.c_str(), command); break; } @@ -1376,7 +1400,6 @@ bool Uart::CheckCrc(char *pCheckBuff, int No) { for (int i = 0; i < No; i++) { tmp += (unsigned char)pCheckBuff[i]; } - if ((unsigned char)pCheckBuff[No] != (unsigned char)tmp) return false; return true; } diff --git a/uart/uart.hpp b/uart/uart.hpp index f492b66..1b94231 100644 --- a/uart/uart.hpp +++ b/uart/uart.hpp @@ -26,10 +26,12 @@ enum InteractiveCommand { CONFIG_INF2 = 10, // 配置 只从无线网关发出 测点名称,测点编号 DEVICE_INF2 = 11, // 测点名称,测点编号 UPGRADE = 12, // 升级, 升级给这条信息 - SIGNAL_STRENGTH = 13, // 信号强度 + SIGNAL_STRENGTH = 13, // 信号强度 --不再使用 DEVICE_EXCEPTION = 14, // 异常: 外设 WAVE_COMPRESS = 15, // 波形数据压缩 - UPGRADE_FIRMWARE = 16 //固件升级内容 + UPGRADE_FIRMWARE = 16, //固件升级内容 + UPGRADE_ASK = 17, //固件升级请求 + WAVE_RESP = 18 // 波形数据回复 }; // 无线传感器请求任务 @@ -218,6 +220,7 @@ public: int UpdateConfig(uint16_t ushortAdd); int TaskResp(ScheduleTask scheduleTask); int SendReviveDuration(ReviveDuration recvDuration); + int WaveResp(uint16_t shortAddr); void openSwitch(); int CheckZigbeeACK(); diff --git a/uart/uart_feature_parse.cpp b/uart/uart_feature_parse.cpp index 02f6c37..103b080 100644 --- a/uart/uart_feature_parse.cpp +++ b/uart/uart_feature_parse.cpp @@ -143,17 +143,18 @@ void Uart::DealDataNodeFeature(const char *pData, int flag) { memcpy(szTableNameStatic, szTableName, sizeof(szTableNameStatic)); memset(whereCon, 0x00, sizeof(whereCon)); - // sprintf(whereCon, "StaticIndex = %ld", staticIndex); - // int count = sqlite_db_ctrl::instance().GetTableRows(szTableNameStatic, whereCon); //避免重复数据 - // sprintf(szTableNameData, "t_data_%s", strMeasurementID.c_str()); + //时间戳判断,是否重包 + sprintf(whereCon, "timeStamp = '%s'", localtimestamp); + int count = sqlite_db_ctrl::instance().GetTableRows(szTableNameStatic, whereCon); //避免重复数据 + sprintf(szTableNameData, "t_data_%s", strMeasurementID.c_str()); - // int count2 = sqlite_db_ctrl::instance().GetTableRows(szTableNameData, whereCon); - // if (count > 0 || count2 > 0) { - // char logInfo[100] = {0x00}; - // sprintf(logInfo, "ShortAddr = %s,staticIndex = %ld,staticData = %d, data = %d", strShortAddr.c_str(), staticIndex, count, count2); - // zlog_info(zct, logInfo); - // return; - // } + int count2 = sqlite_db_ctrl::instance().GetTableRows(szTableNameData, whereCon); + if (count > 0 || count2 > 0) { + char logInfo[100] = {0x00}; + sprintf(logInfo, "ShortAddr = %s,localtimestamp = %s,staticData = %d, data = %d", strShortAddr.c_str(), localtimestamp, count, count2); + zlog_info(zct, logInfo); + return; + } memset(whereCon, 0x00, sizeof(whereCon)); sprintf(szTableNameData, "t_data_%s", strMeasurementID.c_str()); ///////////////////////////////////////////////////////////// for V2.0.3 upgrade to V3.0 @@ -1040,9 +1041,7 @@ void Uart::WriteDatFile(int sampleRate, std::string &strMeasurementID, int iChan } //传感器发来的数据包中的表示设备信息的数据转化为json格式后,通过调用data_publish将数据传给mqttclient : Topic:wireless/cmd/60294D203717 - int iRet = data_publish(WaveData.c_str( - - ), GlobalConfig::Topic_G.mPubWaveData.c_str()); + int iRet = data_publish(WaveData.c_str(), GlobalConfig::Topic_G.mPubWaveData.c_str()); if (iRet != 0) { char whereCon[1024] = {0x00}; char updateSql[1024] = {0x00}; diff --git a/uart/uart_parameter_config.cpp b/uart/uart_parameter_config.cpp index 338516e..576072e 100644 --- a/uart/uart_parameter_config.cpp +++ b/uart/uart_parameter_config.cpp @@ -558,20 +558,24 @@ int Uart::TaskResp(ScheduleTask scheduleTask){ UpdateData[4] = scheduleTask.shortAddr & 0xFF; UpdateData[5] = scheduleTask.cmd & 0xFF; UpdateData[6] = 0x00; - if (scheduleTask.cmd == MEAS_EVAL) - { - UpdateData[7] = UINT16_LOW(scheduleTask.duration); - UpdateData[8] = UINT16_HIGH(scheduleTask.duration); - UpdateData[9] = UINT32_LOW_2(scheduleTask.timeStamp); - UpdateData[10] = UINT32_LOW_1(scheduleTask.timeStamp); - UpdateData[11] = UINT32_HIGH_2(scheduleTask.timeStamp); - UpdateData[12] = UINT32_HIGH_1(scheduleTask.timeStamp); - UpdateData[20] = UINT16_HIGH(scheduleTask.acc_z); - UpdateData[21] = UINT16_HIGH(scheduleTask.next_taskID); - UpdateData[22] = UINT32_LOW_2(scheduleTask.millisecond); - UpdateData[23] = UINT32_LOW_1(scheduleTask.millisecond); + char localtimestamp[32] = {0x00}; + int millisecond = 0; + std::string rtcTime = GetRTC(localtimestamp, millisecond); + scheduleTask.timeStamp = atoi(localtimestamp); + zlog_info(zct,"next taskID = %d\n",scheduleTask.next_taskID); - }else if (scheduleTask.cmd == WAVE_CMD) + UpdateData[7] = UINT16_LOW(scheduleTask.duration); + UpdateData[8] = UINT16_HIGH(scheduleTask.duration); + UpdateData[9] = UINT32_LOW_2(scheduleTask.timeStamp); + UpdateData[10] = UINT32_LOW_1(scheduleTask.timeStamp); + UpdateData[11] = UINT32_HIGH_2(scheduleTask.timeStamp); + UpdateData[12] = UINT32_HIGH_1(scheduleTask.timeStamp); + UpdateData[20] = scheduleTask.acc_z; + UpdateData[21] = scheduleTask.next_taskID & 0xFF; + UpdateData[22] = UINT16_LOW(scheduleTask.millisecond); + UpdateData[23] = UINT16_HIGH(scheduleTask.millisecond); + + if (scheduleTask.next_taskID == WAVE_CMD) { uint8_t x,y,z; wave_feature_set_inst::instance().GetWaveCfg(scheduleTask.shortAddr,x,y,z); @@ -622,6 +626,29 @@ int Uart::SendReviveDuration(ReviveDuration recvDuration){ } return iRet; } +int Uart::WaveResp(uint16_t shortAddr){ + unsigned char sendData[8] = {0}; + sendData[0] = 0xAA; + sendData[1] = 0x55; + sendData[2] = 0xAA; + sendData[3] = (shortAddr >> 8) & 0xFF; + sendData[4] = shortAddr & 0xFF; + sendData[5] = 0x12; + sendData[6] = 0x00; + unsigned char tmp = 0x00; + for (int k = 0; k < 7; k++) { + tmp += sendData[k]; + } + sendData[7] = tmp; + WriteToUart((const char*)sendData, 8); + int iRet = CheckZigbeeACK(); + if (iRet == 0) { + zlog_info(zct, "SendReviveDuration ACK send success,shortAddr = %d", shortAddr); + } else { + zlog_warn(zct, "SendReviveDuration ACK send failed,shortAddr = %d", shortAddr); + } + return iRet; +} 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;