diff --git a/common/common_func.hpp b/common/common_func.hpp index 1d469e1..75ab71f 100644 --- a/common/common_func.hpp +++ b/common/common_func.hpp @@ -118,16 +118,6 @@ struct sys_data { char data[10240]; }; -typedef struct { - int total; - int count; - int type; - int number; - int flag; - char channelId[16]; - char SensorEngineeringUnit[32]; - float waveData[32000]; -} WAVE_CONTAIN; struct ZigbeeInfo { int DevMode; @@ -217,16 +207,16 @@ struct DataNodeInfo { } }; -struct DataRecvStatic { +typedef struct { float TemTop; float TemBot; int Dip; int Voltage; float nodeWorkTime; float nodeSendTime; -}; +}DataRecvStatic; -struct DataRecvDym { +typedef struct { float DiagnosisPk; float IntegratPk; float IntegratRMS; @@ -242,7 +232,7 @@ struct DataRecvDym { float Phase2; float Phase3; float Phase4; -}; +}DataRecvDym; struct TopicList { std::string mPubData; //每秒特征数据上传主题 diff --git a/jsonparse/web_cmd_parse.cpp b/jsonparse/web_cmd_parse.cpp index e59b47d..711ea8a 100644 --- a/jsonparse/web_cmd_parse.cpp +++ b/jsonparse/web_cmd_parse.cpp @@ -77,19 +77,9 @@ std::string JsonData::JsonCmd_Cgi_02(Param_02 ¶m) { if (0 == param.mMode) { jsonVal["type"] = "SET"; if (param.mMode == 0) { //本地校时 - unsigned long set_timestamp = (unsigned long)param.mTimeStamp; - char localtimestamp[32] = {0}; - GetTimeNet(localtimestamp, 1); - std::string current_system_timestamp = std::string(localtimestamp); - unsigned long current_timestamp = boost::lexical_cast(current_system_timestamp); - unsigned long diff_seconds = abs(current_timestamp - set_timestamp); - if (diff_seconds > 5) { - SetTime((unsigned long)param.mTimeStamp); - mssleep(200); - system("hwclock -w"); - } else { - zlog_warn(zct, "diff seconds:%ld lesser than 5, not to modify", diff_seconds); - } + SetTime((unsigned long)param.mTimeStamp); + mssleep(200); + system("hwclock -w"); } else if (param.mMode == 1) { //与服务器校时 char buf[256] = {0}; sprintf(buf, "{\"dataNodeGatewayNo\":\"%s\",\"cmd\":\"12\",\"status\":\"REQ\"}", GlobalConfig::MacAddr_G.c_str()); diff --git a/main.cpp b/main.cpp index 913747e..0ff235b 100644 --- a/main.cpp +++ b/main.cpp @@ -9,6 +9,7 @@ #include #include #include +#include "minilzo/minilzo.h" #include "platform/platform_init.hpp" #include "common/common_func.hpp" #include "common/global.hpp" @@ -17,7 +18,6 @@ #include "utility/aes.h" #include "dbaccess/sql_db.hpp" #include "uart/uart.hpp" -#include "minilzo/minilzo.h" extern std::vector g_VecWaveDataX; extern std::vector g_VecWaveDataY; @@ -27,7 +27,7 @@ zlog_category_t *zct = NULL; zlog_category_t *zbt = NULL; int main(int argc, char *argv[]) { - int rc = zlog_init("/opt/configenv/wlg.conf"); + int rc = zlog_init("/opt/wlg.conf"); if (rc) { printf("init failed\n"); return -1; @@ -41,8 +41,8 @@ int main(int argc, char *argv[]) { return -2; } - zlog_info(zbt, " Firmware compile time:%s %s,version %s", __DATE__, __TIME__, GlobalConfig::Version.c_str()); - zlog_info(zbt, "####CIDNSOFT start####"); + zlog_info(zbt, " Firmware compile time:%s %s,version %s\n", __DATE__, __TIME__, GlobalConfig::Version.c_str()); + zlog_info(zbt, "####CIDNSOFT start####\n"); g_VecWaveDataX.reserve(1000); g_VecWaveDataY.reserve(1000); @@ -111,10 +111,6 @@ int main(int argc, char *argv[]) { boost::thread normalCheckThread(attrs, CheckThread); normalCheckThread.detach(); - //启动软件升级线程 - boost::thread StartDownloadThread(attrs, RecvUpdateFile); - StartDownloadThread.detach(); - //启动cgi server boost::thread startTcpCgi(attrs, StartCgiServer); startTcpCgi.detach(); diff --git a/mqttclient/mqtt_client.cpp b/mqttclient/mqtt_client.cpp index 4fc356b..4738a76 100644 --- a/mqttclient/mqtt_client.cpp +++ b/mqttclient/mqtt_client.cpp @@ -237,18 +237,6 @@ int data_publish(const char *str, const char *topic) { return ret; } -int data_publish_wave(WAVE_CONTAIN *str, const char *topic) { - int ret = -1; - if (mosq != NULL) { - zlog_info(zct, "data_publish:Unit : %s type:%d flag : %d channelid : %s total : %d count : %d number :%d floats:%f floate:%f\n", str->SensorEngineeringUnit, str->type, str->flag, str->channelId, str->total, str->count, str->number, str->waveData[0], - str->waveData[(str->number - 2)]); - ret = mosquitto_publish(mosq, &mid_sent, topic, sizeof(WAVE_CONTAIN), str, ud.topic_qos, false); - if (ret != MOSQ_ERR_SUCCESS) { - zlog_error(zct, "fail to send mqtt msg, ret: [%s], topic: %s", mosquitto_strerror(ret), topic); - } - } - return ret; -} int subscribe(const char *topic, int qos) { int ret = mosquitto_subscribe(mosq, NULL, topic, qos); diff --git a/mqttclient/mqtt_client.h b/mqttclient/mqtt_client.h index b5237bd..434b67b 100644 --- a/mqttclient/mqtt_client.h +++ b/mqttclient/mqtt_client.h @@ -51,7 +51,6 @@ typedef void (*publish_callback)(struct mosquitto *mosq, void *obj, int mid); // mqtt注册 void register_collback(connect_callback connect_c, message_callback message_c, subscribe_callback subscribe_c, log_callback log_c, disconnect_callback disconnect_c, publish_callback publish_c); int data_publish(const char *str, const char *topic); -int data_publish_wave(WAVE_CONTAIN *str, const char *topic); int disconnect(); int reconnect(); int subscribe(const char *topic, int qos); diff --git a/uart/uart.cpp b/uart/uart.cpp index 413e321..ebc3cbc 100644 --- a/uart/uart.cpp +++ b/uart/uart.cpp @@ -37,17 +37,18 @@ int Uart::UartRecv(int fd, char srcshow, char *buffer) { char head[] = {0xAA, 0x55, 0xAA}; char szbuffer[BUF_LENGTH] = {0x00}; while (1) { - if ((unsigned short)GlobalConfig::Zigbee_G.MyAddr == 0x9999) { + if (now_task == kWaveForm) { memset(buff, 0, sizeof(buff)); ret = read_data(fd, buff, BUF_LENGTH, 10); if (ret <= 0) { - if (!bUpdate && !bUpdateconfig && GlobalConfig::EnterZigBeeWaveTransmittingCnt_G > 15) { + if (!bUpdate && !bUpdateconfig) { timeoutflag++; if (timeoutflag > 300) { zlog_info(zct, "===============0x9999 timeout= %d offSize = %d===============", timeoutflag, offSize); zlog_info(zct, "0x9999 timeout %d===============Size = %d", timeoutflag, offSize); FindRecvPackage(offSize, mUartRecvTmpBuf, head); GlobalConfig::Zigbee_G.MyAddr = 0x8888; + now_task = -1; timeoutflag = 0; offSize = 0; maxSize = 0; @@ -392,31 +393,69 @@ void int2bytes(int i, unsigned char *bytes, int size) { bytes[3] = (unsigned char)((0xff000000 & i) >> 24); } -void Uart::DealRecvData(const char *pData) { - char buf[8]; - char shortAdd[8] = {0x00}; - sprintf(buf, "%02d", pData[5] & 0xFF); - sprintf(shortAdd, "%02x%02x", pData[3] & 0xFF, pData[4] & 0xFF); - unsigned short ushortAdd = BUILD_UINT16(pData[3] & 0xFF, pData[4] & 0xFF); - int flag = atoi(buf); - zlog_info(zct, "the data package type(1,2,3,4,5,6) is %s", buf); +int Uart::DealAskTask(unsigned short ushortAdd){ - switch (flag) { - case 1: { // 0x01:设备信息 - DealDataNodeInfo(pData); - } break; - case 2: { // 0x02:特征值 + //1.特征值 + //2.波形 + now_task = kWaveForm; + //3.升级 + //4.更新配置 + return 0; +} + +int Uart::DealException(const char* pData){ + return 0; +} + +int Uart::DealReviveDuration(uint16_t ushortAdd){ + return 0; +} + +int Uart::DealConfig(uint16_t ushortAdd){ + + char whereCon[1024] = {0}; + char updateSql[1024] = { 0 }; + sprintf(updateSql, "UpdateFlag = UpdateFlag + 1"); + sprintf(whereCon, "zigbeeShortAddr='%hu'", ushortAdd); + sqlite_db_ctrl::instance().UpdateTableData(T_SENSOR_INFO(TNAME), updateSql, whereCon); + return 0; +} + +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; + if (recvcode != 0) + { + zlog_error(zct, "shortAdd = %d,command = %d,recvcode = %d ",ushortAdd,command,recvcode); + } + zlog_info(zct, "shortAdd = %d,command = %d,recvcode = %d ",ushortAdd,command,recvcode); + switch (command) { + // case 1: { // 0x01:设备信息 + // DealDataNodeInfo(pData); + // } break; + // case 7: { + // DealDataNodeName(pData); + // } break; + case kAskTask: + DealAskTask(ushortAdd); + break; + case kException: + DealException(pData); + break; + case kEigenValue: DealDataNodeFeature(pData, 0); - } break; - case 6: { // 0x06:特征值+长波形 - DealDataNodeFeature(pData, 1); - } break; - case 32: { //升级 + break; + case kReviveDuration: + DealReviveDuration(ushortAdd); + break; + case kConfig: + DealConfig(ushortAdd); + break; + case kUpgrade: UpdateWirelessNode(ushortAdd); - } break; - case 7: { - DealDataNodeName(pData); - } break; + break; default: break; } } @@ -861,9 +900,9 @@ int Uart::FindRecvPackage(int bytesRead, char *mUartRecvBuf, char *head) { std::string strHeadFlag(buf); if (0 == strHeadFlag.compare("AA55AA")) { char buf[8] = {0x00}; - sprintf(buf, "%02d", UartRecvBuf[i + 5] & 0xFF); + sprintf(buf, "%02d", UartRecvBuf[i + 5]); int command = atoi(buf); - + zlog_info(zct, "command = %d ShortAddr :%s", command, strShortAddr.c_str()); if ((mPackgeIndex == -1 || (unsigned int)UartRecvBuf[i + 6] == 0) && (!bUpdatePre && !bUpdateconfig)) { mPackgeIndex = UartRecvBuf[i + 6] & 0xFF; } else if ((unsigned int)mPackgeIndex == (unsigned int)UartRecvBuf[i + 6] && mPackgeIndex != -1 && (!bUpdatePre && !bUpdateconfig) && command != 2) { @@ -897,29 +936,7 @@ int Uart::FindRecvPackage(int bytesRead, char *mUartRecvBuf, char *head) { break; } - if (command == 32) { - zlog_info(zct, "bUpdatepackge command = %d ShortAddr :%s", command, strShortAddr.c_str()); - memcpy(RecvBuf, (char *)&UartRecvBuf[i], 12); - - if (!CheckCrc(RecvBuf, 11)) { - zlog_info(zct, "CheckCrc error command 20 "); - mPackgeIndex = -1; - GlobalConfig::Zigbee_G.MyAddr = 0x8888; - tcflush(fd, TCIOFLUSH); - sleep(1); - modify_LocalAddr(0x8888); - sleep(1); - bModifyAddr = true; - bSendTimeStamp = false; - GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = NO_ENTER_TRANSMITTING_STATUS; - GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0; - break; - } - bUpdate = true; - m_TimeStamp = 0; - DealRecvData(RecvBuf); - break; - } else if (!bUpdatePre && !bUpdateconfig && (command == 3 || command == 4 || command == 5)) { + if (now_task == kWaveForm && (command == 3 || command == 4 || command == 5)) { if (!CheckCrc(&UartRecvBuf[i], 99)) { m_TimeStamp = 0; mPackgeIndex = -1; @@ -960,7 +977,7 @@ int Uart::FindRecvPackage(int bytesRead, char *mUartRecvBuf, char *head) { } memcpy(RecvBuf, (char *)&UartRecvBuf[i], 100); DealDataNodeWave(RecvBuf, command); - } else if (!bUpdate && !bUpdateconfig && (command == 1 || command == 2 || command == 6 || command == 7)) { + } else if (now_task != kWaveForm && (command == 1 || command == 4 || command == 11 || command == 21 || command == 22 || command == 24)) { char RecvBuf[100] = {0x00}; memcpy(RecvBuf, &UartRecvBuf[i], 100); if (!CheckCrc(RecvBuf, 99)) { @@ -969,54 +986,7 @@ int Uart::FindRecvPackage(int bytesRead, char *mUartRecvBuf, char *head) { } DealRecvData(RecvBuf); - } else if (bUpdate && !bUpdateconfig && (command == 1 || command == 2 || command == 6 || command == 7)) { - zlog_info(zct, "m_strDestShortAddr = %s,strShortAddr = %s,waittime = %d", m_strDestShortAddr.c_str(), strShortAddr.c_str(), waittime); - - { - char RecvBuf[100] = {0x00}; - memcpy(RecvBuf, &UartRecvBuf[i], 100); - DealRecvData(RecvBuf); - zlog_info(zct, "Online = %s,command = %d", strShortAddr.c_str(), command); - waittime = 0; - bUpdate = false; - bUpdatePre = false; - m_strDestShortAddr = ""; - GlobalConfig::Zigbee_G.MyAddr = 0x8888; - GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = NO_ENTER_TRANSMITTING_STATUS; - GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0; - } - - } else if (command == 34 && bUpdateconfig) { - zlog_info(zct, "bUpdateconfig command = %d ShortAddr :%s", command, strShortAddr.c_str()); - memset(RecvBuf, 0x00, sizeof(RecvBuf)); - zlog_info(zct, "bUpdateconfig ShortAddr :%s", strShortAddr.c_str()); - - m_TimeStamp = 0; - { - char whereCon[1024] = {0}; - char updateSql[1024] = {0}; - char buf[20] = {0x00}; - sprintf(buf, "%02x%02x", UartRecvBuf[i + 3] & 0xFF, UartRecvBuf[i + 4] & 0xFF); // Zigbee 本地地址 2 byte - sprintf(updateSql, "UpdateFlag = UpdateFlag + 1"); - sprintf(whereCon, "zigbeeShortAddr='%s'", buf); - sqlite_db_ctrl::instance().UpdateTableData(T_SENSOR_INFO(TNAME), updateSql, whereCon); - - GlobalConfig::Zigbee_G.MyAddr = 0x8888; - bUpdateconfig = false; - mPackgeIndex = -1; - tcflush(fd, TCIOFLUSH); - sleep(1); - modify_LocalAddr(0x8888); - bModifyAddr = true; - bSendTimeStamp = false; - sleep(1); - - GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = NO_ENTER_TRANSMITTING_STATUS; - GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0; - break; - } - break; - } else if (command == 35) { + } else if (command == 35) { char signalNode[10] = {0x00}; sprintf(signalNode, "%02d", UartRecvBuf[i + 14] & 0xFF); if (!strcmp(signalNode, "00") || !strcmp(signalNode, "0")) { diff --git a/uart/uart.hpp b/uart/uart.hpp index a2b6fe8..8694694 100644 --- a/uart/uart.hpp +++ b/uart/uart.hpp @@ -11,6 +11,126 @@ typedef void (*pTestRecvCallBack)(int Status); +// 无关网关与传感器交互命令 1 ~ 255 +enum InteractiveCommand { + // 传感器发给网关 + kAskTask = 1, // 请求任务 + kReviveConfig = 2, // 请求基本的复活配置, 使用新的传感器时 + kVersionInfo = 3, // 版本信息 + kException = 4, // 异常: 外设 + + // 交互命令 + kEigenValue = 11, // 特征值 + kWaveForm = 12, // 波形 + // kBatteryLevel = 14, // 电池电量 特征值里面带 + + // 网关发给传感器 + kReviveDuration = 21, // 复活时长 + kConfig = 22, // 配置 + // kExecuteTask = 23, // 需要无线传感器执行的任务 + kUpgrade = 24, // 升级 + kSignalStrength = 25, // 信号强度 +}; +// 无线传感器请求任务 +typedef struct { + uint8_t cmd; // kAskTask + uint16_t sensor_id; +} AskTaskReq; + +typedef struct { + uint8_t cmd; // kAskTask + uint8_t task_id; // 任务编号, 参考InteractiveCommand +} AskTaskResp; + +// 无线传感器请求复活配置 +typedef struct { + uint8_t cmd; + uint16_t sensor_id; +} ReviveConfigReq; + +typedef struct { + uint8_t cmd; + uint16_t eigen_duration; // 如果连不上网关,一直以此间隔复活,并且只连3秒时间 +} ReviveConfigResp; + +// 无线传感器异常反馈 +typedef struct { + uint8_t cmd; + uint16_t sensor_id; + uint8_t exception_id; +} ExceptionFeedback; + +//===================================================== +//// 交互命令 +// 无线传感器发送特征值 +typedef struct { + uint8_t cmd; + uint16_t sensor_id; + DataRecvStatic data_static; + DataRecvDym data_dym; +} EigenValueReport; + +// 无线传感器发送波形 +typedef struct { + uint8_t cmd; + uint16_t sensor_id; + uint16_t len; + uint16_t total; + uint16_t current; + char data[0]; +} WaveFormReport; +// 网关发送给传感器的请求 +// 配置传感器下次复活相对时间 +typedef struct { + uint8_t cmd; + uint16_t shortAddr; + uint16_t duration; +} ReviveDuration; + +typedef enum { + kSamplingRate12800 = 1, + kSamplingRate25600 = 2, +} SamplingRate; + +typedef enum { + kRange64g = 1, + kRange128g = 2 +} MeasurementRange; + +// 传感器配置 +typedef struct { + uint8_t cmd; + uint16_t freq_band_energy_begin[4]; // 频带能量开始 + uint16_t freq_band_energy_end[4]; // 频带能量结束 + uint16_t falut[4]; // 故障频率 + uint16_t impact[2]; // 冲击带通频率 + uint8_t sampling_rate; // 采样率, 参考:SamplingRate + uint8_t range; // 量程, 参考:MeasurementRange + uint8_t sampling_time; // 采样时间 + uint16_t velocity_start_frequency; + uint8_t wave_x_y_z[3]; // 分别为x, y, z +} SensorConfig; + +// 下发任务 +typedef struct { + uint8_t cmd; + uint16_t shortAddr; + uint8_t to_exec_cmd; // 需要传感器立即执行的任务 +} ScheduleTask; + +// 升级任务 +typedef struct { + uint8_t cmd; + uint16_t len; + uint8_t encrypt_type; // 加密类型 0: 不加密 + uint8_t total; + uint8_t data[0]; +} UpgradeData; +// 对任何请求的一般反馈 +typedef struct { + uint8_t cmd; + uint8_t success; // 0:成功, 1:其它失败,需要重发 +} NormalResp; class Uart { public: Uart(); @@ -37,6 +157,11 @@ public: int UartRecv(int fd, char srcshow, char* buffer); int FindRecvPackage(int bytesRead, char* mUartRecvBuf, char* head); + int DealAskTask(unsigned short ushortAdd); + int DealException(const char* pData); + int DealReviveDuration(uint16_t ushortAdd); + int DealConfig(uint16_t ushortAdd); + // feature parse void DealDataNodeFeature(const char* pData, int flag); void RecordBattery(std::string& strLongAddr, DataRecvStatic& dataStatic, std::string& nowTimetamp); @@ -71,6 +196,8 @@ public: int UpdateWirelessNodeTime(unsigned char* pDestShortAddr, int modifyaddr); int UpdateConfig(unsigned char* pDestShortAddr); bool ReadUpdatePackge(unsigned char* shortAddr); + int TaskResp(ScheduleTask scheduleTask); + int SendReviveDuration(ReviveDuration recvDuration); void openSwitch(); int CheckZigbeeACK(); @@ -95,6 +222,7 @@ private: std::string strTimetamp; std::string m_strDestShortAddr; + int now_task; int waittime; enum { BUF_LENGTH = 40960 }; diff --git a/uart/uart_parameter_config.cpp b/uart/uart_parameter_config.cpp index 4fcd511..77d020a 100644 --- a/uart/uart_parameter_config.cpp +++ b/uart/uart_parameter_config.cpp @@ -115,6 +115,7 @@ bool Uart::ReadUpdatePackge(unsigned char* pDestShortAddr) { } void Uart::UpdateWirelessNode(unsigned short shortAdd) { + std::string strTime = GetLocalTimeWithMs(); zlog_info(zct, "UpdateWirelessNode start = %s UpdateWirelessNode id = %02x %02x", strTime.c_str(), UINT16_HIGH(shortAdd), UINT16_LOW(shortAdd)); @@ -517,7 +518,59 @@ int Uart::UpdateWirelessNodeTime(unsigned char* pDestShortAddr, int modifyaddr / } return iRet; } +int Uart::TaskResp(ScheduleTask scheduleTask){ + unsigned char UpdateData[100] = {0}; + UpdateData[0] = 0xAA; + UpdateData[1] = 0x55; + UpdateData[2] = 0xAA; + UpdateData[3] = (scheduleTask.shortAddr >> 8) & 0xFF; + UpdateData[4] = scheduleTask.shortAddr & 0xFF; + UpdateData[5] = scheduleTask.cmd & 0xFF; + UpdateData[6] = 0x00; + UpdateData[7] = scheduleTask.to_exec_cmd & 0xFF; + + unsigned char tmp = 0x00; + for (int k = 0; k < 99; k++) { + tmp += UpdateData[k]; + } + UpdateData[99] = tmp; + WriteToUart((const char*)UpdateData, 100); + int iRet = CheckZigbeeACK(); + if (iRet == 0) { + zlog_info(zct, "TaskResp ACK send success,shortAddr = %d", scheduleTask.shortAddr); + } else { + zlog_error(zct, "TaskResp ACK send failed,shortAddr = %d", scheduleTask.shortAddr); + } + return iRet; +} +int Uart::SendReviveDuration(ReviveDuration recvDuration){ + + unsigned char UpdateData[100] = {0}; + UpdateData[0] = 0xAA; + UpdateData[1] = 0x55; + UpdateData[2] = 0xAA; + UpdateData[3] = (recvDuration.shortAddr >> 8) & 0xFF; + UpdateData[4] = recvDuration.shortAddr & 0xFF; + UpdateData[5] = recvDuration.cmd; + UpdateData[6] = 0x00; + UpdateData[7] = (recvDuration.duration >> 8) & 0xFF; + UpdateData[8] = recvDuration.duration & 0xFF; + + unsigned char tmp = 0x00; + for (int k = 0; k < 99; k++) { + tmp += UpdateData[k]; + } + UpdateData[99] = tmp; + WriteToUart((const char*)UpdateData, 100); + int iRet = CheckZigbeeACK(); + if (iRet == 0) { + zlog_info(zct, "SendReviveDuration ACK send success,shortAddr = %d", recvDuration.shortAddr); + } else { + zlog_error(zct, "SendReviveDuration ACK send failed,shortAddr = %d", recvDuration.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;