WLG/uart/uart.cpp
2024-10-22 20:56:21 +08:00

1127 lines
52 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "uart.hpp"
#include <signal.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <boost/thread/thread.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/bind.hpp>
#include <boost/timer.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include "../serial/serial.h"
char g_UartRecvBuf[GENERAL_BUF_SIZE];
int offSize = 0;
pTestRecvCallBack pTestRecv;
int Uart::UartRecv(int fd, char srcshow, char *buffer) {
char buff[BUF_LENGTH];
int ret = 0;
int maxSize = 0;
int offSize = 0;
int timeoutflag = 0;
char head[] = {0xAA, 0x55, 0xAA};
char szbuffer[BUF_LENGTH] = {0x00};
while (1) {
if ((unsigned short)GlobalConfig::Zigbee_G.MyAddr == 0x9999) {
memset(buff, 0, sizeof(buff));
ret = read_data(fd, buff, BUF_LENGTH, 10);
if (ret <= 0) {
if (!bUpdate && !bUpdateconfig && GlobalConfig::EnterZigBeeWaveTransmittingCnt_G > 15) {
timeoutflag++;
if (timeoutflag > 300) {
LOG_DEBUG("===============0x9999 timeout= %d offSize = %d===============\n", timeoutflag, offSize);
print_info("0x9999 timeout %d===============Size = %d\n", timeoutflag, offSize);
FindRecvPackage(offSize, mUartRecvTmpBuf, head);
GlobalConfig::Zigbee_G.MyAddr = 0x8888;
timeoutflag = 0;
offSize = 0;
maxSize = 0;
tcflush(fd, TCIFLUSH);
bModifyAddr = true;
modify_LocalAddr(0x8888);
bSendTimeStamp = false;
mssleep(10000);
wave_trans_ = true;
memset(mUartRecvTmpBuf, 0, BUF_LENGTH);
GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = NO_ENTER_TRANSMITTING_STATUS;
GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0;
LOG_DEBUG("wave end\n");
}
mssleep(10000);
} else if (bUpdatePre || (bUpdateconfig && GlobalConfig::EnterZigBeeWaveTransmittingCnt_G > 15)) {
timeoutflag++;
if (timeoutflag > 300) {
print_info("bUpdateconfig %d===============\n", timeoutflag);
GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = NO_ENTER_TRANSMITTING_STATUS;
GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0;
timeoutflag = 0;
offSize = 0;
maxSize = 0;
bUpdate = false;
bUpdatePre = false;
bUpdateconfig = false;
bModifyAddr = true;
bSendTimeStamp = false;
modify_LocalAddr(0x8888);
mssleep(10000);
GlobalConfig::Zigbee_G.MyAddr = 0x8888;
}
mssleep(10000);
}
} else if (ret > 0) {
maxSize += ret;
timeoutflag = 0;
if ((bUpdatePre || bUpdateconfig)) {
for (int i = 0; i < ret; i++) {
printf("%02x ", buff[i] & 0xff);
}
FindRecvPackage(ret, buff, head);
} else {
m_TimeStamp = 0;
memcpy(mUartRecvTmpBuf + offSize, buff, ret);
offSize = offSize + ret;
if (offSize > BUF_LENGTH * 15) {
LOG_INFO("maxSize = %d\n", offSize);
memset(mUartRecvTmpBuf, 0, BUF_LENGTH);
timeoutflag = 0;
offSize = 0;
maxSize = 0;
tcflush(fd, TCIOFLUSH);
bModifyAddr = true;
modify_LocalAddr(0x8888);
GlobalConfig::Zigbee_G.MyAddr = 0x8888;
GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = NO_ENTER_TRANSMITTING_STATUS;
GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0;
}
}
}
} else if ((unsigned short)GlobalConfig::Zigbee_G.MyAddr == 0x8888) {
#ifdef IMX6UL_GATEWAY
memset(buff, 0, sizeof(buff));
ret = read_data(fd, buff, BUF_LENGTH, 50);
if (ret <= 0) {
continue;
}
if (srcshow) {
print_info("0x8888 ===str_recv===,ret = %d\n", ret);
for (int i = 0; i < ret; i++) {
print_debug("[%02x]", buff[i] & 0xff);
}
printf("\n");
FindRecvPackage(ret, buff, head);
}
#else if G2UL_GATEWAY
memset(buff, 0x00, sizeof(buff));
ret = read_data(fd, buff, BUF_LENGTH, 50);
if (ret <= 0) {
timeoutflag++;
if (timeoutflag > 5) {
FindRecvPackage(offSize, szbuffer, (char *)head);
memset(szbuffer, 0x00, sizeof(szbuffer));
timeoutflag = 0;
offSize = 0;
maxSize = 0;
mssleep(10000);
}
} else if (ret > 0) {
maxSize += ret;
print_debug("0x8888==str_recv===,ret = %d offSize = %d\n", ret, maxSize);
for (int i = 0; i < ret; i++) {
print_debug("%02x ", buff[i] & 0xff);
}
print_debug("\n");
timeoutflag = 0;
m_TimeStamp = 0;
memcpy(szbuffer + offSize, buff, ret);
offSize = offSize + ret;
}
#endif
}
}
}
Uart::Uart() : mUart(mIoSev), mStrand(mIoSev) {
mRdLength = 0;
m_TimeStamp = 0;
mlastSize = 0;
fd = 0;
waittime = 0;
mPackgeIndex = -1;
bUpdate = false;
m_strDestShortAddr = "";
memset(mUartRecvBuf, 0, BUF_LENGTH);
memset(mUartRecvTmpBuf, 0, BUF_LENGTH);
bTest = false;
TestFd = 0;
DataNodeUpdateFile = "";
strTimetamp = "";
bZigbeeSinal = false;
bModifyAddr = false;
bUpdatePre = false;
bSendTimeStamp = false;
m_waveCountX = 0;
m_waveCountY = 0;
m_waveCountZ = 0;
VecWaveDataX.reserve(1000);
VecWaveDataY.reserve(1000);
VecWaveDataZ.reserve(1500);
}
Uart::~Uart() {
close(fd);
close(TestFd);
if (mUart.is_open()) mUart.close();
}
void Uart::InitUart(speed_t speed) {
#ifdef G2UL_GATEWAY
fd = config_uart("/dev/ttySC2", speed);
#endif
#ifdef IMX6UL_GATEWAY
fd = config_uart("/dev/ttymxc4", speed);
#endif
print_info("InitUart fd = %d\n", fd);
if (fd < 0) {
printf("config_uart error\n");
}
}
void Uart::Close() {
close(fd);
fd = 0;
}
void Uart::InitZigbeeHW() {
InitUart(B115200);
UpdateZigbeeInfoCtrl();
sleep(2);
int iRet = ReadFromUart();
if (iRet <= 0) {
Close();
sleep(2);
print_info("Speed error\n");
InitUart(B57600);
UpdateZigbeeInfoCtrl();
sleep(2);
ReadFromUart();
print_info("modify speed\n");
WriteSpeed2Zigbee();
ReadFromUart();
}
Close();
sleep(1);
}
void TestRecv(int status) {
char szStatus[10] = {0x00};
sprintf(szStatus, "%d", status);
write_data(uart_inst::instance().TestFd, (char *)szStatus, strlen(szStatus));
}
void Regist(pTestRecvCallBack pTestRecv1) { pTestRecv = pTestRecv1; }
void Uart::InitTestUart(speed_t speed) {
print_info("InitTestUart\n");
TestFd = config_uart("/dev/ttymxc2", speed); // Test
if (TestFd < 0) {
printf("Test config_uart error\n");
}
Regist(TestRecv);
}
void Uart::ReadTestUart() {
char buff[BUF_LENGTH] = {0x00};
print_info("ReadTestUart\n");
while (1) {
int ret = read_data(TestFd, buff, BUF_LENGTH, 10);
if (ret > 0) {
print_info("buff = %s\n", buff);
if (!strcmp(buff, "0")) {
char buf[256] = {0};
sprintf(buf, "{\"dataNodeGatewayNo\":\"%s\",\"cmd\":\"12\",\"status\":\"REQ\"}", GlobalConfig::MacAddr_G.c_str());
std::string str = std::string(buf);
int iRet = data_publish(str.c_str(), GlobalConfig::Topic_G.mPubCmd.c_str());
if (iRet < 0) {
pTestRecv(1);
} else {
pTestRecv(0);
}
iRet = ZigbeeTest();
} else if (!strcmp(buff, "1")) {
string IP = GetGwIp_("eth0");
write_data(TestFd, (char *)IP.c_str(), IP.size());
IP = GetGwIp_("eth1");
write_data(TestFd, (char *)"|", 1);
write_data(TestFd, (char *)IP.c_str(), IP.size());
write_data(TestFd, (char *)"|", 1);
} else {
ModifyMac(buff);
pTestRecv(0);
}
}
mssleep(20000);
}
}
int Uart::ZigbeeTest() {
char buff[BUF_LENGTH] = {0x00};
modify_Localchannel(22);
mssleep(100000);
modify_LocalPanID(2222);
mssleep(100000);
modify_LocalAddr(6666);
mssleep(100000);
return 0;
}
void Uart::WriteToUart(const char *strSend, int pLen) {
if (!bUpdate) {
print_debug("Write To Uart Start:\n");
for (int i = 0; i < pLen; i++) {
print_debug("%02X ", *(strSend + i) & 0xFF);
}
print_debug("\nWrite To Uart End.\n");
}
int len = write_data(fd, (char *)strSend, pLen);
}
int Uart::ReadFromUart() {
char buffer[100] = {0x00};
int len = read_data(fd, (char *)buffer, 100, 10);
for (int i = 0; i < len; i++) print_debug("%02X ", buffer[i] & 0xFF);
print_info("\n");
print_info("==========ReadFromUart========len = %d\n", len);
if (len) {
char head[] = {0xAA, 0x55, 0xAA};
FindRecvPackage(len, buffer, head);
}
return len;
}
void Uart::Run() { mIoSev.run(); }
void Uart::Stop() {
if (mUart.is_open()) mUart.close();
mIoSev.stop();
}
void Uart::setCallBack(onReceiveUart _callback) { m_callback = _callback; }
void Uart::UpdateZigbeeInfo(const char *pData) {
print_info("update gateway zigbee info\n");
GlobalConfig::Zigbee_G = *((ZIGBEE *)pData);
char buff[8];
sprintf(buff, "%02d", pData[36] & 0xFF);
GlobalConfig::ZigbeeInfo_G.DevMode = atoi(buff);
memset(buff, 0, 8);
sprintf(buff, "%02d", pData[37] & 0xFF);
GlobalConfig::ZigbeeInfo_G.Channel = atoi(buff);
memset(buff, 0, 8);
sprintf(buff, "%02x%02x", pData[38] & 0xFF, pData[39] & 0xFF);
GlobalConfig::ZigbeeInfo_G.PanID = std::string(buff);
memset(buff, 0, 8);
sprintf(buff, "%02x%02x", pData[40] & 0xFF, pData[41] & 0xFF);
GlobalConfig::ZigbeeInfo_G.MyAddr = std::string(buff);
memset(buff, 0, 8);
sprintf(buff, "%02x%02x", pData[50] & 0xFF, pData[51] & 0xFF);
GlobalConfig::ZigbeeInfo_G.DstAddr = std::string(buff);
memset(buff, 0, 8);
sprintf(buff, "%d", pData[62]);
GlobalConfig::ZigbeeInfo_G.RetryNum = std::string(buff);
memset(buff, 0, 8);
sprintf(buff, "%d", pData[63]);
GlobalConfig::ZigbeeInfo_G.TranTimeout = std::string(buff);
LOG_INFO("local zigbee module info Mode : %d Chan : %d PanID : %s MyAddr : %s DstAddr : %s,RetryNum:%s,TranTimeout:%s\n", GlobalConfig::ZigbeeInfo_G.DevMode, GlobalConfig::ZigbeeInfo_G.Channel, GlobalConfig::ZigbeeInfo_G.PanID.c_str(),
GlobalConfig::ZigbeeInfo_G.MyAddr.c_str(), GlobalConfig::ZigbeeInfo_G.DstAddr.c_str(), GlobalConfig::ZigbeeInfo_G.RetryNum.c_str(), GlobalConfig::ZigbeeInfo_G.TranTimeout.c_str());
LOG_INFO("PanID = %s,MacAddr_G= %s\n", GlobalConfig::ZigbeeInfo_G.PanID.c_str(), GlobalConfig::MacAddr_G.c_str());
std::string strchan = ReadStrByOpt(ZIGBEECONFIG, "Zigbee", "channel");
LOG_INFO("Channel = %d,strchan = %s\n", GlobalConfig::ZigbeeInfo_G.Channel, strchan.c_str());
std::string strType = ReadStrByOpt(SYSTEMINFOFILE, "ZigbeeType", "type");
print_info("TranTimeout = %02x,strType = %s \n", GlobalConfig::Zigbee_G.TranTimeout, strType.c_str());
if (strType == "aw21") {
unsigned short TranTimeout = 0x0A;
WriteTranTimeout2Zigbee(TranTimeout);
}
if (strType == "zm5161") {
unsigned short TranTimeout = 0x03;
WriteTranTimeout2Zigbee(TranTimeout);
}
}
void int2bytes(int i, unsigned char *bytes, int size) {
memset(bytes, 0, sizeof(unsigned char) * size);
bytes[0] = (unsigned char)(0xff & i);
bytes[1] = (unsigned char)((0xff00 & i) >> 8);
bytes[2] = (unsigned char)((0xff0000 & i) >> 16);
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);
print_info("the data package type(1,2,3,4,5,6) is %s,%x\n", buf, pData[5] & 0xFF);
switch (flag) {
case 1: { // 0x01:设备信息
DealDataNodeInfo(pData);
} break;
case 2: { // 0x02:特征值
DealDataNodeFeature(pData, 0);
} break;
case 6: { // 0x06:特征值+长波形
DealDataNodeFeature(pData, 1);
} break;
case 32: { //升级
UpdateWirelessNode(ushortAdd);
} break;
case 7: {
DealDataNodeName(pData);
} break;
default: break;
}
}
void Uart::DealDataNodeName(const char *pData) {
bSendTimeStamp = false;
print_info("DealDataNodeName \n");
string strTime = GetLocalTimeWithMs();
char szShortAdd[8] = {0x00};
char shortAdd[8] = {0x00};
char NodeName[64] = {0x00};
sprintf(szShortAdd, "%02x%02x", pData[3] & 0xFF, pData[4] & 0xFF);
memcpy(NodeName, &pData[7], 64);
memcpy(shortAdd, &pData[3], 2);
char whereCon[64] = {0};
char uplCon[200] = {0x00};
int iRet = -1;
char nodeWaveSend[10] = {0x00};
UpdateWirelessNodeTime((unsigned char *)shortAdd, 1);
for (int i = 0; i < 64; i++) {
sprintf(&NodeName[i * 2], "%02X", pData[7 + i] & 0xFF);
}
char MeasurementID[100] = {0x00};
sprintf(MeasurementID, "%02x%02x%02x%02x%02x%02x%02x%02x", pData[71], pData[72], pData[73], pData[74], pData[75], pData[76], pData[77], pData[78]);
sprintf(nodeWaveSend, "%d,%d,%d", GET_BIT(pData[79], 0), GET_BIT(pData[79], 1), GET_BIT(pData[79], 2));
char gbkNodeName[128] = {0x00};
sprintf(whereCon, "zigbeeShortAddr='%s'", szShortAdd);
print_info("whereCon = %s\n", whereCon);
array_t vecRes = sqlite_db_ctrl::instance().GetDataMultiLine(T_SENSOR_INFO(TNAME), " dataNodeNo, MeasurementID,hardVersion,softVersion", whereCon);
if (vecRes.size() > 1) {
for (int i = 0; i < vecRes.size(); i++) {
if (vecRes[i][1] != "") {
char whereCon1[64] = {0};
sprintf(whereCon1, " dataNodeNo='%s' ", vecRes[i][0].c_str());
sqlite_db_ctrl::instance().DeleteTableData(T_SENSOR_INFO(TNAME), whereCon1);
sqlite_db_ctrl::instance().DeleteTableData(T_DATA_INFO(TNAME), whereCon1);
sqlite_db_ctrl::instance().DeleteTableData(T_DATASTATIC_INFO(TNAME), whereCon1);
sqlite_db_ctrl::instance().DeleteTableData(T_DATANODE_TIME(TNAME), whereCon1);
char szTableName[50] = {0x00};
sprintf(szTableName, "DROP TABLE t_data_%s", vecRes[i][0].c_str());
sqlite_db_ctrl::instance().CreateTable(szTableName, 0);
memset(szTableName, 0x00, sizeof(szTableName));
sprintf(szTableName, "DROP TABLE t_dataStatic_%s", vecRes[i][0].c_str());
sqlite_db_ctrl::instance().CreateTable(szTableName, 0);
}
}
}
string hardVersion = vecRes[0][2];
string softVersion = vecRes[0][3];
if ((hardVersion == "3.0" && compareVersions(softVersion, "3.6") == -1) || (hardVersion == "4.0" && compareVersions(softVersion, "4.6") == -1)) {
memcpy(MeasurementID, vecRes[0][0].c_str(), sizeof(MeasurementID));
}
std::string strNodeName(NodeName);
print_info("strNodeName = %s\n", strNodeName.c_str());
solve(gbkNodeName, NodeName);
print_info("gbkNodeName = %s\n", gbkNodeName);
string utfNodeName = GBKToUTF8(gbkNodeName);
print_info("NodeName = %s\n", NodeName);
print_info("whereCon = %s\n", whereCon);
sprintf(uplCon, "dataNodeName = '%s' , MeasurementID = '%s',NodeWaveSend = '%s'", gbkNodeName, MeasurementID, nodeWaveSend);
iRet = sqlite_db_ctrl::instance().UpdateTableData(T_SENSOR_INFO(TNAME), uplCon, whereCon, 0);
string strData = sqlite_db_ctrl::instance().GetNodeConfigureInfor(whereCon);
iRet = data_publish(strData.c_str(), GlobalConfig::Topic_G.mPubConfig.c_str());
}
void Uart::DealDataNodeInfo(const char *pData) {
print_info("recv remote zigbee module info\n");
RecvData *pRecvData = (RecvData *)pData;
char buf[32] = {0};
char chTemp = pRecvData->Data[0]; //设备状态标志 1 byte
DataNodeInfo dataNodeInfo;
dataNodeInfo.EquipSta = GET_BIT(chTemp, 2);
dataNodeInfo.TemTopFlag = GET_BIT(chTemp, 3);
dataNodeInfo.TemBotFlag = GET_BIT(chTemp, 4);
dataNodeInfo.ZigbeeFlag = GET_BIT(chTemp, 5);
dataNodeInfo.AccFlag = GET_BIT(chTemp, 6);
dataNodeInfo.InitFlag = GET_BIT(chTemp, 7);
sprintf(buf, "%02x%02x%02x%02x%02x%02x%02x%02x", pRecvData->Data[1], pRecvData->Data[2], pRecvData->Data[3], pRecvData->Data[4], pRecvData->Data[5], pRecvData->Data[6], pRecvData->Data[7], pRecvData->Data[8]);
dataNodeInfo.ZigbeeLongAddr = std::string(buf); // Zigbee MAC 8 byte
chTemp = pRecvData->Data[9]; //硬件版本 1 byte
memset(buf, 0, 32);
sprintf(buf, "%.1f", 0.1 * chTemp);
dataNodeInfo.HardVersion = std::string(buf);
chTemp = pRecvData->Data[10]; //软件版本 1 byte
memset(buf, 0, 32);
// sprintf(buf, "%02x", chTemp);
sprintf(buf, "%.1f", 0.1 * chTemp);
dataNodeInfo.SoftVersion = std::string(buf);
memset(buf, 0, 32);
sprintf(buf, "%d", BUILD_UINT32(pRecvData->Data[11], pRecvData->Data[12], pRecvData->Data[13], pRecvData->Data[14]));
dataNodeInfo.BpNo = std::string(buf); //生产批号 4 byte
memset(buf, 0, 32);
sprintf(buf, "%d", BUILD_UINT32(pRecvData->Data[15], pRecvData->Data[16], pRecvData->Data[17], pRecvData->Data[18]));
dataNodeInfo.SerialNo = std::string(buf); //生产序列号 4 byte
memset(buf, 0, 32);
sprintf(buf, "%d", BUILD_UINT32(pRecvData->Data[19], pRecvData->Data[20], pRecvData->Data[21], pRecvData->Data[22]));
dataNodeInfo.FirstPowerTime = std::string(buf); //首次上电日期 4 byte
// 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38=>序号13 无线信号强度
memset(buf, 0, 32);
sprintf(buf, "%d", BUILD_UINT32(pRecvData->Data[23], pRecvData->Data[24], pRecvData->Data[25], pRecvData->Data[26]));
dataNodeInfo.WakeupTime = atoi(buf); //唤醒次数 4 byte
printf("WakeupTime = %d\n", dataNodeInfo.WakeupTime);
memset(buf, 0, 32);
sprintf(buf, "%d", BUILD_UINT32(pRecvData->Data[27], pRecvData->Data[28], pRecvData->Data[29], pRecvData->Data[30]));
dataNodeInfo.StaticTime = atoi(buf); //特征值发送次数 4 byte
printf("StaticTime = %d\n", dataNodeInfo.StaticTime);
memset(buf, 0, 32);
dataNodeInfo.WaveTime = BUILD_UINT32(pRecvData->Data[31], pRecvData->Data[32], pRecvData->Data[33], pRecvData->Data[34]); //原始波形发送次数 4 byte
memset(buf, 0, 32);
dataNodeInfo.BateryV = BUILD_UINT16(pRecvData->Data[35], pRecvData->Data[36]); //电池电压 2 byte
memset(buf, 0, 32);
sprintf(buf, "%02x", pRecvData->Data[37]);
dataNodeInfo.ProductNo = std::string(buf); //产品型号 1 byte
// 获取 RSSI
chTemp = pRecvData->Data[38];
memset(buf, 0, 32);
sprintf(buf, "%d", chTemp);
dataNodeInfo.RSSI = atoi(buf); //无线信号强度 1 byte
chTemp = pRecvData->Data[39];
memset(buf, 0, 32);
sprintf(buf, "%02x", chTemp);
dataNodeInfo.ConfigFlag = ((0 == std::string(buf).compare("aa")) ? 1 : 0); //配置标志 1 byte
chTemp = pRecvData->Data[40];
memset(buf, 0, 32);
sprintf(buf, "%d", chTemp);
dataNodeInfo.FeatureInterVal = atoi(buf); //唤醒周期 1 byte
memset(buf, 0, 32);
// yxq
sprintf(buf, "%u%u", pRecvData->Data[41], pRecvData->Data[42]);
dataNodeInfo.WaveInterVal = atoi(buf); //原始波形发送周期 2 byte
memset(buf, 0, 32);
sprintf(buf, "%02x%02x", pRecvData->Data[43], pRecvData->Data[44]); // Zigbee PID 2 byte
dataNodeInfo.ZigbeePanId = std::string(buf);
chTemp = pRecvData->Data[45];
memset(buf, 0, 32);
sprintf(buf, "%d", chTemp);
dataNodeInfo.ZigbeeChannel = atoi(buf); // Zigbee 信道 1 byte
memset(buf, 0, 32);
sprintf(buf, "%02x%02x", pRecvData->Data[46], pRecvData->Data[47]); // Zigbee 本地地址 2 byte
dataNodeInfo.ZigbeeShortAddr = std::string(buf);
unsigned char shortAddr[2] = {0x00};
memcpy(shortAddr, (unsigned char *)&pRecvData->Data[46], 2);
modify_DistAddr(shortAddr); //修改目标地址
string strTime = GetLocalTimeWithMs();
memset(buf, 0, 32);
sprintf(buf, "%02x%02x", pRecvData->Data[48], pRecvData->Data[49]); // Zigbee 目标地址 2 byte
dataNodeInfo.ZigbeeDesAddr = std::string(buf);
// 50 51 52=》序号23 zigbee重试间隔
memset(buf, 0, 32);
sprintf(buf, "%02x", pRecvData->Data[50]); // Zigbee 发射功率 1 byte
dataNodeInfo.ZigbeePower = atoi(buf);
memset(buf, 0, 32);
sprintf(buf, "%d", BUILD_UINT16(00, pRecvData->Data[51])); // Zigbee 重试次数 1 byte
dataNodeInfo.ZigbeeRetry = atoi(buf);
memset(buf, 0, 32);
sprintf(buf, "%d", BUILD_UINT16(00, pRecvData->Data[52])); // Zigbee 重试间隔 1 byte
dataNodeInfo.ZigbeeRetryGap = atoi(buf);
if (dataNodeInfo.ProductNo == "01") {
chTemp = pRecvData->Data[53];
unsigned char range = (chTemp >> 2) & 0x3;
dataNodeInfo.Range = range;
int SamplingRate = (chTemp & 0x3);
print_info("SamplingRate = %d\n", SamplingRate);
if (SamplingRate == 0) {
dataNodeInfo.SamplingRate = 3200;
} else if (SamplingRate == 1) {
dataNodeInfo.SamplingRate = 6400;
} else if (SamplingRate == 2) {
dataNodeInfo.SamplingRate = 12800;
} else if (SamplingRate == 3) {
dataNodeInfo.SamplingRate = 25600;
}
} else if (dataNodeInfo.ProductNo == "02") {
chTemp = pRecvData->Data[53];
unsigned char range = (chTemp >> 3) & 0x7;
dataNodeInfo.Range = range;
int SamplingRate = (chTemp & 0x7);
print_info("SamplingRate = %d\n", SamplingRate);
if (SamplingRate == 0) {
dataNodeInfo.SamplingRate = 8000;
} else if (SamplingRate == 1) {
dataNodeInfo.SamplingRate = 16000;
} else if (SamplingRate == 2) {
dataNodeInfo.SamplingRate = 24000;
} else if (SamplingRate == 3) {
dataNodeInfo.SamplingRate = 32000;
} else if (SamplingRate == 4) {
dataNodeInfo.SamplingRate = 48000;
} else if (SamplingRate == 5) {
dataNodeInfo.SamplingRate = 96000;
} else if (SamplingRate == 6) {
dataNodeInfo.SamplingRate = 192000;
}
}
// 54=》序号25 ACC采样时间
memset(buf, 0, 32);
sprintf(buf, "%u", pRecvData->Data[54]); // ACC 采样时间 1 byte
dataNodeInfo.ACCSampleTime = atoi(buf);
int iTemp = 0;
//使用了55 56 59 60 63 64 67 68 71 72
for (int i = 0; i < 5; i++) {
memset(buf, 0, 32);
sprintf(buf, "%02x%02x", pRecvData->Data[55 + i * 4], pRecvData->Data[55 + i * 4 + 1]);
iTemp = (int)strtol(buf, NULL, 16);
if (0 == i) {
dataNodeInfo.StartBrands = to_string(iTemp);
} else {
dataNodeInfo.StartBrands += ("," + to_string(iTemp));
}
}
//使用了57 58 61 62 65 66 69 70 73 74
for (int j = 0; j < 5; j++) {
memset(buf, 0, 32);
sprintf(buf, "%02x%02x", pRecvData->Data[57 + j * 4], pRecvData->Data[57 + j * 4 + 1]);
iTemp = (int)strtol(buf, NULL, 16);
if (0 == j) {
dataNodeInfo.StopBrands = to_string(iTemp);
} else {
dataNodeInfo.StopBrands += ("," + to_string(iTemp));
}
}
memset(buf, 0, 32);
sprintf(buf, "%02x%02x", pRecvData->Data[75], pRecvData->Data[76]);
iTemp = (int)strtol(buf, NULL, 16);
dataNodeInfo.EnvelopeBandPass = to_string(iTemp);
memset(buf, 0, 32);
sprintf(buf, "%02x%02x", pRecvData->Data[77], pRecvData->Data[78]);
iTemp = (int)strtol(buf, NULL, 16);
dataNodeInfo.EnvelopeBandPass += ("," + to_string(iTemp));
//使用了79 80 81 82 83 84 85 86
for (int j = 0; j < 4; j++) {
memset(buf, 0, 32);
sprintf(buf, "%02x%02x", pRecvData->Data[79 + j * 2], pRecvData->Data[79 + j * 2 + 1]);
iTemp = (int)strtol(buf, NULL, 16);
if (0 == j) {
dataNodeInfo.FaultFrequency = to_string(iTemp);
} else {
dataNodeInfo.FaultFrequency += ("," + to_string(iTemp));
}
}
memset(buf, 0, 32);
sprintf(buf, "%02x%02x%02x%02x", pRecvData->Data[87], pRecvData->Data[88], pRecvData->Data[89], pRecvData->Data[90]);
long lTimeStamp = strtol(buf, NULL, 16);
dataNodeInfo.ConfigDate = to_string(lTimeStamp);
chTemp = pRecvData->Data[91];
memset(buf, 0, 32);
sprintf(buf, "%d", chTemp);
dataNodeInfo.VIntegralFilterFrequency = atoi(buf);
char whereCon[64] = {0};
sprintf(whereCon, "dataNodeNo='%s'", dataNodeInfo.ZigbeeLongAddr.c_str());
if (sqlite_db_ctrl::instance().GetTableRows(T_SENSOR_INFO(TNAME), whereCon) > 0) {
char updateSql[1024] = {0};
sprintf(updateSql, " initFlag = '%d',accFlag = '%d',zigbeeFlag = '%d',temTopFlag = '%d',temBotFlag = '%d',equipSta = '%d',\
hardVersion = '%s',softVersion = '%s',bpNo = '%s',serialNo = '%s',firstPowerTime = '%s',WakeupTime = '%d',\
StaticTime = '%d',WaveTime = '%d',BateryV = '%d',ProductNo = '%s',configFlag = '%d',startBrands = '%s',\
stopBrands = '%s',featureInterVal = '%u',waveInterVal = '%d',samplingRate = '%d',scope = '%s',range = '%d',envelopeBandPass = '%s',faultFrequency = '%s',\
zigbeePanId = '%s',zigbeeChannel = '%d',zigbeeShortAddr = '%s',zigbeeLongAddr = '%s',zigbeeDesAddr = '%s',\
ZigbeePower = '%d',ZigbeeRetry = '%d',ZigbeeRetryGap = '%d',ACCSampleTime = '%d',status = '%s',timeStamp = '%s',viff = '%d',RSSI = '0,%d',UpdateFlag = 1",
dataNodeInfo.InitFlag, dataNodeInfo.AccFlag, dataNodeInfo.ZigbeeFlag, dataNodeInfo.TemTopFlag, dataNodeInfo.TemBotFlag, dataNodeInfo.EquipSta, dataNodeInfo.HardVersion.c_str(), dataNodeInfo.SoftVersion.c_str(), dataNodeInfo.BpNo.c_str(),
dataNodeInfo.SerialNo.c_str(), dataNodeInfo.FirstPowerTime.c_str(), dataNodeInfo.WakeupTime, dataNodeInfo.StaticTime, dataNodeInfo.WaveTime, dataNodeInfo.BateryV, dataNodeInfo.ProductNo.c_str(), dataNodeInfo.ConfigFlag, dataNodeInfo.StartBrands.c_str(),
dataNodeInfo.StopBrands.c_str(), dataNodeInfo.FeatureInterVal, dataNodeInfo.WaveInterVal, dataNodeInfo.SamplingRate, "", dataNodeInfo.Range, dataNodeInfo.EnvelopeBandPass.c_str(), dataNodeInfo.FaultFrequency.c_str(), dataNodeInfo.ZigbeePanId.c_str(),
dataNodeInfo.ZigbeeChannel, dataNodeInfo.ZigbeeShortAddr.c_str(), dataNodeInfo.ZigbeeLongAddr.c_str(), dataNodeInfo.ZigbeeDesAddr.c_str(), dataNodeInfo.ZigbeePower, dataNodeInfo.ZigbeeRetry, dataNodeInfo.ZigbeeRetryGap, dataNodeInfo.ACCSampleTime, "1",
dataNodeInfo.ConfigDate.c_str(), dataNodeInfo.VIntegralFilterFrequency, dataNodeInfo.RSSI);
sprintf(whereCon, "dataNodeNo = '%s'", dataNodeInfo.ZigbeeLongAddr.c_str());
sqlite_db_ctrl::instance().UpdateTableData(T_SENSOR_INFO(TNAME), updateSql, whereCon);
} else {
char insertSql[1024] = {0};
sprintf(insertSql, " '%s','%s','%d','%d','%d','%d','%d','%d',\
'%s','%s','%s','%s','%s','%d',\
'%d','%d','%d','%s','%d','%s',\
'%s','%u','%d','%d','%s','%d', '%s', '%s',\
'%s','%d','%s','%s','%s',\
'%d','%d','%d','%d','%s','%s', '%d', '0,%d','1','0,0','','',''",
dataNodeInfo.ZigbeeLongAddr.c_str(), " ", dataNodeInfo.InitFlag, dataNodeInfo.AccFlag, dataNodeInfo.ZigbeeFlag, dataNodeInfo.TemTopFlag, dataNodeInfo.TemBotFlag, dataNodeInfo.EquipSta, dataNodeInfo.HardVersion.c_str(), dataNodeInfo.SoftVersion.c_str(),
dataNodeInfo.BpNo.c_str(), dataNodeInfo.SerialNo.c_str(), dataNodeInfo.FirstPowerTime.c_str(), dataNodeInfo.WakeupTime, dataNodeInfo.StaticTime, dataNodeInfo.WaveTime, dataNodeInfo.BateryV, dataNodeInfo.ProductNo.c_str(), dataNodeInfo.ConfigFlag,
dataNodeInfo.StartBrands.c_str(), dataNodeInfo.StopBrands.c_str(), dataNodeInfo.FeatureInterVal, dataNodeInfo.WaveInterVal, dataNodeInfo.SamplingRate, "", dataNodeInfo.Range, dataNodeInfo.EnvelopeBandPass.c_str(), dataNodeInfo.FaultFrequency.c_str(),
dataNodeInfo.ZigbeePanId.c_str(), dataNodeInfo.ZigbeeChannel, dataNodeInfo.ZigbeeShortAddr.c_str(), dataNodeInfo.ZigbeeLongAddr.c_str(), dataNodeInfo.ZigbeeDesAddr.c_str(), dataNodeInfo.ZigbeePower, dataNodeInfo.ZigbeeRetry, dataNodeInfo.ZigbeeRetryGap,
dataNodeInfo.ACCSampleTime, "1", dataNodeInfo.ConfigDate.c_str(), dataNodeInfo.VIntegralFilterFrequency, dataNodeInfo.RSSI);
sqlite_db_ctrl::instance().InsertData(T_SENSOR_INFO(TNAME), insertSql);
}
char szTableName[50] = {0x00};
sprintf(szTableName, "t_data_%s", dataNodeInfo.ZigbeeLongAddr.c_str());
sqlite_db_ctrl::instance().Createtable(szTableName);
memset(szTableName, 0x00, sizeof(szTableName));
sprintf(szTableName, "t_dataStatic_%s", dataNodeInfo.ZigbeeLongAddr.c_str());
sqlite_db_ctrl::instance().CreatedataStatictable(szTableName);
Json::Value jsonVal;
jsonVal.clear();
jsonVal["cmd"] = "26";
Json::Value jsBody;
jsBody["dataNodeGatewayNo"] = GlobalConfig::MacAddr_G;
jsBody["dataNodeNo"] = dataNodeInfo.ZigbeeLongAddr;
jsBody["type"] = "insert";
Json::Value jsSensorData;
jsSensorData["dataNodeNo"] = dataNodeInfo.ZigbeeLongAddr;
jsSensorData["dataNodeName"] = "";
jsSensorData["initFlag"] = dataNodeInfo.InitFlag;
jsSensorData["accFlag"] = dataNodeInfo.AccFlag;
jsSensorData["zigbeeFlag"] = dataNodeInfo.ZigbeeFlag;
jsSensorData["temTopFlag"] = dataNodeInfo.TemTopFlag;
jsSensorData["temBotFlag"] = dataNodeInfo.TemBotFlag;
jsSensorData["equipsta"] = dataNodeInfo.EquipSta;
jsSensorData["hardVersion"] = dataNodeInfo.HardVersion;
jsSensorData["softVersion"] = dataNodeInfo.SoftVersion;
jsSensorData["bpNo"] = dataNodeInfo.BpNo;
jsSensorData["serialNo"] = dataNodeInfo.SerialNo;
jsSensorData["firstPowerTime"] = dataNodeInfo.FirstPowerTime;
jsSensorData["configFlag"] = dataNodeInfo.ConfigFlag;
jsSensorData["startBrands"] = dataNodeInfo.StartBrands;
jsSensorData["stopBrands"] = dataNodeInfo.StopBrands;
jsSensorData["featureInterVal"] = dataNodeInfo.FeatureInterVal;
jsSensorData["waveInterVal"] = dataNodeInfo.WaveInterVal;
jsSensorData["samplingRate"] = dataNodeInfo.SamplingRate;
jsSensorData["range"] = dataNodeInfo.Range;
jsSensorData["envelopeBandPass"] = dataNodeInfo.EnvelopeBandPass;
jsSensorData["faultFrequency"] = dataNodeInfo.FaultFrequency;
jsSensorData["zigbeePanId"] = dataNodeInfo.ZigbeePanId;
jsSensorData["zigbeeChannel"] = dataNodeInfo.ZigbeeChannel;
jsSensorData["zigbeeAddr"] = dataNodeInfo.ZigbeeLongAddr;
jsSensorData["zigbeeDesAddr"] = dataNodeInfo.ZigbeeDesAddr;
jsSensorData["status"] = 1;
jsSensorData["timeStamp"] = dataNodeInfo.ConfigDate;
jsSensorData["VIntegralFilterFrequency"] = dataNodeInfo.VIntegralFilterFrequency;
jsSensorData["RSSI"] = dataNodeInfo.RSSI;
jsBody["dataNodeInfo"] = jsSensorData;
Json::FastWriter showValue;
std::string dataBody = showValue.write(jsBody);
jsonVal["cmdBody"] = dataBody;
std::string strCmd26 = showValue.write(jsonVal);
// 传感器发来的数据包中的表示设备信息的数据转化为json格式后通过调用data_publish将数据传给mqttclient Topicwireless/cmd/60294D203717
data_publish(strCmd26.c_str(), GlobalConfig::Topic_G.mPubCmd.c_str());
print_info("remote wireless sensor device info AccFlag : %d EquipSta : %d BpNo : %s ConfigFlag : %d EnvelopeBandPass : %s FaultFrequency : %s FeatureInterVal : %u FirstPowerTime : %s HardVersion : %s InitFlag : %d SamplingRate : %d range : %d SerialNo : %s\
SoftVersion : %s StartBrands : %s StopBrands : %s TemBotFlag : %d TemTopFlag : %d WaveInterVal : %d ZigbeeChannel : %d ZigbeeDesAddr : %s ZigbeeFlag : %d ZigbeeLongAddr : %s panid : %s ZigbeeShortAddr : %s Configdate : %s vintegralfilterfrequency : %d RSSI : %d \n",
dataNodeInfo.AccFlag, dataNodeInfo.EquipSta, dataNodeInfo.BpNo.c_str(), dataNodeInfo.ConfigFlag, dataNodeInfo.EnvelopeBandPass.c_str(), dataNodeInfo.FaultFrequency.c_str(), dataNodeInfo.FeatureInterVal, dataNodeInfo.FirstPowerTime.c_str(),
dataNodeInfo.HardVersion.c_str(), dataNodeInfo.InitFlag, dataNodeInfo.SamplingRate, dataNodeInfo.Range, dataNodeInfo.SerialNo.c_str(), dataNodeInfo.SoftVersion.c_str(), dataNodeInfo.StartBrands.c_str(), dataNodeInfo.StopBrands.c_str(), dataNodeInfo.TemBotFlag,
dataNodeInfo.TemTopFlag, dataNodeInfo.WaveInterVal, dataNodeInfo.ZigbeeChannel, dataNodeInfo.ZigbeeDesAddr.c_str(), dataNodeInfo.ZigbeeFlag, dataNodeInfo.ZigbeeLongAddr.c_str(), dataNodeInfo.ZigbeePanId.c_str(), dataNodeInfo.ZigbeeShortAddr.c_str(),
dataNodeInfo.ConfigDate.c_str(), dataNodeInfo.VIntegralFilterFrequency, dataNodeInfo.RSSI);
if (g_mapCompress.find(dataNodeInfo.ZigbeeShortAddr) == g_mapCompress.end()) {
compressWaveChannel tempchannel;
g_mapCompress.insert(std::make_pair(dataNodeInfo.ZigbeeShortAddr, tempchannel));
print_info("new Node,size = %d\n", g_mapCompress.size());
}
LOG_INFO("DealDataNodeInfo %s \n", dataNodeInfo.ZigbeeShortAddr.c_str());
}
void Uart::ZigbeeParameterConfig() {
std::string strPanId = sqlite_db_ctrl::instance().GetData("t_gateway_info", "zigbeePanID", NULL);
{
unsigned short shortAddr = 0x8888;
WriteShortAddr2Zigbee(shortAddr);
mssleep(100000);
}
print_info("strPanId : %s\n", strPanId.c_str());
print_info("MacAddr_G : %s\n", GlobalConfig::MacAddr_G.c_str());
std::string strchan = ReadStrByOpt(ZIGBEECONFIG, "Zigbee", "channel");
unsigned short Chan = (unsigned short)strtol(strchan.c_str(), NULL, 10);
print_info("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%the chan = %u\n", Chan);
print_info("ZigbeeInfo_G.Channel=%d\n", GlobalConfig::ZigbeeInfo_G.Channel);
if (Chan > 10 && Chan < 27) {
WriteChanl2Zigbee(Chan);
mssleep(100000);
}
print_info("PanID1 = %s,strPanId = %s\n", GlobalConfig::ZigbeeInfo_G.PanID.c_str(), strPanId.c_str());
long lShortAddr = strtol(strPanId.c_str(), NULL, 16);
unsigned short panid = lShortAddr & 0xffff;
WritePanId2Zigbee(panid);
mssleep(100000);
print_info("ZigbeeInfo_G.MyAddr=%s\n", GlobalConfig::ZigbeeInfo_G.MyAddr.c_str());
}
int Uart::FindRecvPackage(int bytesRead, char *mUartRecvBuf, char *head) {
string strTime = GetLocalTimeWithMs();
char head1[] = {0xAB, 0xBC, 0xCD};
char head2[] = {0xDE, 0xDF, 0xEF};
int lastSize = 0;
char UartRecvBuf[BUF_LENGTH * 15] = {0x00};
char RecvBuf[200] = {0x00};
if (mlastSize > 0) {
print_info("mlastSize = %d\n", mlastSize);
memcpy(UartRecvBuf, mUartRecvTmpBuf, mlastSize);
for (int i = 0; i < mlastSize; i++) {
print_info("%02x ", UartRecvBuf[i]);
}
print_info("\n ");
memset(mUartRecvTmpBuf, 0x00, sizeof(mUartRecvTmpBuf));
}
memcpy(UartRecvBuf + mlastSize, mUartRecvBuf, bytesRead);
bytesRead = bytesRead + mlastSize;
for (int i = 0; i < bytesRead; i++) {
if (UartRecvBuf[i] == head[0]) {
char buf[6] = {0x00};
char ShortAddr[8] = {0x00};
sprintf(&buf[0], "%02X", UartRecvBuf[i] & 0xFF);
sprintf(&buf[2], "%02X", UartRecvBuf[i + 1] & 0xFF);
sprintf(&buf[4], "%02X", UartRecvBuf[i + 2] & 0xFF);
sprintf(ShortAddr, "%02x%02x", UartRecvBuf[i + 3] & 0xFF, UartRecvBuf[i + 4] & 0xFF);
std::string strShortAddr(ShortAddr);
std::string strHeadFlag(buf);
if (0 == strHeadFlag.compare("AA55AA")) {
char buf[8] = {0x00};
sprintf(buf, "%02d", UartRecvBuf[i + 5] & 0xFF);
int command = atoi(buf);
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) {
LOG_ERROR("mPackgeIndex same index1:%d,index2:%02d ShortAddr :%s \n", mPackgeIndex, UartRecvBuf[i + 6] & 0xff, strShortAddr.c_str());
continue;
} else if ((unsigned int)mPackgeIndex + 1 != (unsigned int)UartRecvBuf[i + 6] && mPackgeIndex != -1 && (!bUpdatePre && !bUpdateconfig) && command != 2) {
m_TimeStamp = 0;
LOG_ERROR("mPackgeIndex error index1:%d,index2:%02d ShortAddr :%s \n", mPackgeIndex, UartRecvBuf[i + 6] & 0xff, strShortAddr.c_str());
mPackgeIndex = -1;
print_error("mPackgeIndex error ShortAddr :%s \n", strShortAddr.c_str());
char tmp[10] = {0x00};
char tmp2[10] = {0x00};
for (int j = 0; j < 100; j++) {
sprintf(tmp, "%02x ", UartRecvBuf[i + j] & 0xff);
strcat(tmp2, tmp);
}
LOG_ERROR("error str = %s\n", tmp2);
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;
}
if (command == 32) {
LOG_INFO("bUpdatepackge command = %d ShortAddr :%s\n", command, strShortAddr.c_str());
memcpy(RecvBuf, (char *)&UartRecvBuf[i], 12);
if (!CheckCrc(RecvBuf, 11)) {
LOG_INFO("CheckCrc error command 20 \n");
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 (!CheckCrc(&UartRecvBuf[i], 99)) {
m_TimeStamp = 0;
mPackgeIndex = -1;
LOG_INFO("CheckCrc error ShortAddr :%s command = %d\n", strShortAddr.c_str(), command);
print_error("CheckCrc error ShortAddr :%s \n", strShortAddr.c_str());
char tmp[10] = {0x00};
char tmp2[10] = {0x00};
for (int j = 0; j < 100; j++) {
sprintf(tmp, "%02x ", UartRecvBuf[i + j] & 0xff);
strcat(tmp2, tmp);
}
LOG_ERROR("error str = %s\n", tmp2);
print_info("\n");
GlobalConfig::Zigbee_G.MyAddr = 0x8888;
tcflush(fd, TCIOFLUSH);
sleep(1);
modify_LocalAddr(0x8888);
bModifyAddr = true;
sleep(1);
bSendTimeStamp = false;
GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = NO_ENTER_TRANSMITTING_STATUS;
GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0;
m_waveCountX = 0;
m_waveCountY = 0;
m_waveCountZ = 0;
g_VecWaveDataX.clear();
g_VecWaveDataY.clear();
g_VecWaveDataZ.clear();
break;
}
mlastSize = 0;
lastSize = bytesRead - i;
if (lastSize < 100 && lastSize > 0) {
memcpy(mUartRecvTmpBuf, (char *)&UartRecvBuf[bytesRead - lastSize], lastSize);
mlastSize = lastSize;
break;
}
memcpy(RecvBuf, (char *)&UartRecvBuf[i], 100);
DealDataNodeWave(RecvBuf, command);
} else if (!bUpdate && !bUpdateconfig && (command == 1 || command == 2 || command == 6 || command == 7)) {
char RecvBuf[100] = {0x00};
memcpy(RecvBuf, &UartRecvBuf[i], 100);
if (!CheckCrc(RecvBuf, 99)) {
LOG_INFO("CheckCrc error ShortAddr :%s command = %d \n", strShortAddr.c_str(), command);
break;
}
DealRecvData(RecvBuf);
} else if (bUpdate && !bUpdateconfig && (command == 1 || command == 2 || command == 6 || command == 7)) {
print_info("m_strDestShortAddr = %s,strShortAddr = %s,waittime = %d\n", m_strDestShortAddr.c_str(), strShortAddr.c_str(), waittime);
{
char RecvBuf[100] = {0x00};
memcpy(RecvBuf, &UartRecvBuf[i], 100);
DealRecvData(RecvBuf);
LOG_INFO("Online = %s,command = %d\n", 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) {
LOG_INFO("bUpdateconfig command = %d ShortAddr :%s\n", command, strShortAddr.c_str());
memset(RecvBuf, 0x00, sizeof(RecvBuf));
print_info("bUpdateconfig ShortAddr :%s\n", 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) {
char signalNode[10] = {0x00};
sprintf(signalNode, "%02d", UartRecvBuf[i + 14] & 0xFF);
if (!strcmp(signalNode, "00") || !strcmp(signalNode, "0")) {
char errorInfo[100] = {0x00};
sprintf(errorInfo, "未检测到信号!%s", signalNode);
LOG_ERROR(errorInfo);
} else {
char whereCon[1024] = {0};
char updateSql[1024] = {0};
char tableName[100] = {0x00};
sprintf(whereCon, "zigbeeShortAddr='%s'", strShortAddr.c_str());
vec_t vecDataNodeNo = sqlite_db_ctrl::instance().GetDataSingleLine(T_SENSOR_INFO(TNAME), " dataNodeNo,RSSI ", whereCon);
memset(whereCon, 0x00, sizeof(whereCon));
sprintf(updateSql, "zigbeeSignalNode = '%02d' ", UartRecvBuf[i + 14] & 0xFF); // zigbeeRSSIType = 0 传感器获取网关信号强度
sprintf(whereCon, "dataNodeNo='%s' and timeStamp = '%s'", (char *)vecDataNodeNo[0].c_str(), strTimetamp.c_str());
sprintf(tableName, "t_dataStatic_%s", (char *)vecDataNodeNo[0].c_str());
sqlite_db_ctrl::instance().UpdateTableData(tableName, updateSql, whereCon);
vector<string> vParamRSSI;
boost::split(vParamRSSI, vecDataNodeNo[1], boost::is_any_of(","), boost::token_compress_on);
if (vParamRSSI.size() > 0) {
sprintf(updateSql, "RSSI = '%s,%02d' ", vParamRSSI[0].c_str(), UartRecvBuf[i + 14] & 0xFF);
// LOG_INFO(updateSql);
sprintf(whereCon, "dataNodeNo='%s'", (char *)vecDataNodeNo[0].c_str());
sqlite_db_ctrl::instance().UpdateTableData(T_SENSOR_INFO(TNAME), updateSql, whereCon);
}
}
}
mPackgeIndex = (unsigned int)UartRecvBuf[i + 6];
} else {
continue;
}
} else if (UartRecvBuf[i] == head1[0]) {
char buf[6] = {0x00};
sprintf(&buf[0], "%02X", UartRecvBuf[i] & 0xFF);
sprintf(&buf[2], "%02X", UartRecvBuf[i + 1] & 0xFF);
sprintf(&buf[4], "%02X", UartRecvBuf[i + 2] & 0xFF);
std::string strHeadFlag(buf);
if (0 == strHeadFlag.compare("ABBCCD")) {
char buf[8] = {0x00};
sprintf(buf, "%02d", UartRecvBuf[i + 3] & 0xFF);
int command = atoi(buf);
print_info("command = %d\n", command);
if (command == 209) {
UpdateZigbeeInfo(&UartRecvBuf[i]);
} else if (command == 220) { // DC
bModifyAddr = false;
LOG_INFO("zigbeeShortAddr = %s , ret = %02d\n", m_strDestShortAddr.c_str(), UartRecvBuf[i + 6] & 0xFF);
if (UartRecvBuf[i + 6] & 0xFF != 00) {
modify_LocalAddr(0x8888);
}
}
}
} else if (UartRecvBuf[i] == head2[0]) {
char buf[6] = {0x00};
sprintf(&buf[0], "%02X", UartRecvBuf[i] & 0xFF);
sprintf(&buf[2], "%02X", UartRecvBuf[i + 1] & 0xFF);
sprintf(&buf[4], "%02X", UartRecvBuf[i + 2] & 0xFF);
std::string strHeadFlag(buf);
if (0 == strHeadFlag.compare("DEDFEF")) {
char buf[8] = {0x00};
sprintf(buf, "%02d", UartRecvBuf[i + 3] & 0xFF);
int command = atoi(buf);
print_info("command = %d\n", command);
char tmp[16] = {0x00};
if (command == 209) { // D1
pTestRecv(command);
} else if (command == 219) { // DB
pTestRecv(command);
} else if (command == 220) { // DC
bModifyAddr = false;
print_info("%02x,%02x,%02x,%02x,%02x \n", UartRecvBuf[i], UartRecvBuf[i + 1], UartRecvBuf[i + 2], UartRecvBuf[i + 3], UartRecvBuf[i + 4]);
LOG_INFO("zigbeeShortAddr = %s , ret = %02d\n", m_strDestShortAddr.c_str(), UartRecvBuf[i + 4] & 0xFF);
} else if (command == 218) { // DA
char whereCon[1024] = {0};
char updateSql[1024] = {0};
char tableName[100] = {0x00};
bZigbeeSinal = false;
sprintf(whereCon, "zigbeeShortAddr='%s'", m_strDestShortAddr.c_str());
vec_t vecDataNodeNo = sqlite_db_ctrl::instance().GetDataSingleLine(T_SENSOR_INFO(TNAME), " dataNodeNo,LooseValue,RSSI ", whereCon);
memset(whereCon, 0x00, sizeof(whereCon));
sprintf(updateSql, "zigbeeSignal = '%02d' ", UartRecvBuf[i + 6] & 0xFF); // zigbeeRSSIType = 1 网关获取传感器信号强度
sprintf(whereCon, "dataNodeNo='%s' and timeStamp = '%s'", (char *)vecDataNodeNo[0].c_str(), strTimetamp.c_str());
sprintf(tableName, "t_dataStatic_%s", (char *)vecDataNodeNo[0].c_str());
sqlite_db_ctrl::instance().UpdateTableData(tableName, updateSql, whereCon);
Json::Value jsBody, jsonVal;
Json::FastWriter showValue;
char looseValue[10] = {0x00};
readStringValue("config", "loose", looseValue, (char *)GlobalConfig::Config_G.c_str());
if (atof(looseValue) < atof(vecDataNodeNo[1].c_str())) {
jsBody["looseStatus"] = "1";
} else {
jsBody["looseStatus"] = "0";
}
vector<string> vParamRSSI;
boost::split(vParamRSSI, vecDataNodeNo[2], boost::is_any_of(","), boost::token_compress_on);
if (vParamRSSI.size() == 1) {
sprintf(updateSql, "RSSI = '%02d,%s' ", UartRecvBuf[i + 6] & 0xFF, vParamRSSI[0].c_str());
} else if (vParamRSSI.size() == 2) {
sprintf(updateSql, "RSSI = '%02d,%s' ", UartRecvBuf[i + 6] & 0xFF, vParamRSSI[1].c_str());
}
sprintf(whereCon, "dataNodeNo='%s'", (char *)vecDataNodeNo[0].c_str());
sqlite_db_ctrl::instance().UpdateTableData(T_SENSOR_INFO(TNAME), updateSql, whereCon);
jsonVal["cmd"] = "52";
jsBody["timeStamp"] = strTimetamp;
jsBody["dataNodeNo"] = vecDataNodeNo[0];
jsBody["zigbeeSignal"] = UartRecvBuf[i + 6] & 0xFF;
std::string dataBody = showValue.write(jsBody);
jsonVal["cmdBody"] = dataBody;
data_publish(showValue.write(jsonVal).c_str(), GlobalConfig::Topic_G.mPubConfig.c_str());
}
}
}
}
}