wirelessgateway/uart/SH_Uart.cpp

4290 lines
150 KiB
C++
Raw Normal View History

2021-09-18 13:45:24 +08:00
#include <signal.h>
2024-07-09 09:49:42 +08:00
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>
2021-09-18 13:45:24 +08:00
#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 "../uart/SH_Uart.hpp"
2024-07-09 09:49:42 +08:00
#include "../serial/serial.h"
2021-09-18 13:45:24 +08:00
namespace{
Uart *pUart = Uart::instance();
2024-07-09 09:49:42 +08:00
Calculation *pCalculation = Calculation::instance();
2021-09-18 13:45:24 +08:00
}
char g_UartRecvBuf[GENERAL_BUF_SIZE];
int offSize = 0;
2024-07-09 09:49:42 +08:00
pTestRecvCallBack pTestRecv ;
2021-09-18 13:45:24 +08:00
// namespace{
// PlatformInit *platform = PlatformInit::instance();
// LocalServer *wlServer = LocalServer::instance();
// GenericFunc *genericFunc = GenericFunc::instance();
// }
using namespace boost::asio;
const UINT CharSize = 8;
const UINT UartBaud = 115200;
2024-07-09 09:49:42 +08:00
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};
2024-07-09 09:50:10 +08:00
char szbuffer[BUF_LENGTH]={0x00};
2024-07-09 09:49:42 +08:00
while(1)
{
if((unsigned short)GlobalConfig::Zigbee_G.MyAddr == 0x9999){
memset(buff, 0, sizeof(buff));
2024-08-05 10:25:50 +08:00
ret = read_data(fd, buff, BUF_LENGTH, 10);
2024-07-09 09:49:42 +08:00
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;
usleep(10000);
GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = NO_ENTER_TRANSMITTING_STATUS;
GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0;
DealWave();
LOG_DEBUG("wave end\n");
}
usleep(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);
usleep(100000);
GlobalConfig::Zigbee_G.MyAddr = 0x8888;
}
usleep(100000);
}
}
else if(ret > 0){
maxSize += ret;
//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);
// }
//print_debug("maxSize = %d\n",maxSize);
//print_debug("\n");
timeoutflag = 0;
if((bUpdatePre || bUpdateconfig))
{
for(int i = 0; i < ret;i++){
printf("%02x ", buff[i]&0xff);
}
//print_debug("maxSize111 = %d\n",maxSize);
// string strTime = GetLocalTimeWithMs();
// LOG_INFO("str_recv strTime1 = %s\n",strTime.c_str());
FindRecvPackage(ret,buff,head);
// ReadHandle(buff,ret);
// strTime = GetLocalTimeWithMs();
// LOG_INFO("str_recv strTime2 = %s\n",strTime.c_str());
}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;
}
2024-08-05 10:25:50 +08:00
//print_debug("offSize = %d\n",offSize);
2024-07-09 09:49:42 +08:00
}
}
}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;
usleep(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
}
usleep(50);
}
}
2021-09-18 13:45:24 +08:00
Uart::Uart():mUart(mIoSev),mStrand(mIoSev)
{
2024-07-09 09:49:42 +08:00
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;
}
Uart::~Uart()
{
close(fd);
close(TestFd);
if (mUart.is_open())
mUart.close();
2021-09-18 13:45:24 +08:00
}
2024-07-09 09:49:42 +08:00
void Uart::openSwitch()
{
char buffer[100]={0x00};
int len;
int fdSwitch = config_uart("/dev/ttymxc1",B38400);
char strSend[8]={0x01,0x06,0x00,0x00,0x00,0x00,0x89,0xCA};
len = write_data(fdSwitch,(char*)strSend,8);
sleep(1);
len = read_data(fdSwitch,(char*)buffer,100,10);
for ( int i = 0; i < len; i++)
print_debug("%02X ",buffer[i]&0xFF);
char strSend2[8]={0x01,0x06,0x00,0x00,0x00,0x01,0x48,0x0A};
len = write_data(fdSwitch,(char*)strSend2,8);
sleep(1);
len = read_data(fdSwitch,(char*)buffer,100,10);
for ( int i = 0; i < len; i++)
print_debug("%02X ",buffer[i]&0xFF);
close(fdSwitch);
}
void Uart::InitUart(speed_t speed)
2021-09-18 13:45:24 +08:00
{
2024-07-09 09:49:42 +08:00
#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");
}
2021-09-18 13:45:24 +08:00
}
2024-07-09 09:49:42 +08:00
void Uart::Close()
{
close(fd);
fd = 0;
}
void Uart::InitZigbee()
{
2021-09-18 13:45:24 +08:00
2024-07-09 09:49:42 +08:00
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)
2021-09-18 13:45:24 +08:00
{
2024-07-09 09:49:42 +08:00
char szStatus[10]={0x00};
sprintf(szStatus,"%d",status);
write_data(pUart->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);
}
}
usleep(20000);
}
}
int Uart::ZigbeeTest()
{
char buff[BUF_LENGTH] = {0x00};
modify_Localchannel(22);
usleep(100000);
modify_LocalPanID(2222);
usleep(100000);
modify_LocalAddr(6666);
usleep(100000);
return 0;
2021-09-18 13:45:24 +08:00
}
void Uart::WriteToUart(const char *strSend,int pLen)
{
//#ifdef ZIGBEE_TEST
2024-07-09 09:49:42 +08:00
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");
}
2021-09-18 13:45:24 +08:00
//#endif
2024-07-09 09:49:42 +08:00
/* if (mUart.is_open()) {
2021-09-18 13:45:24 +08:00
mUart.async_write_some(buffer(strSend,pLen),
mStrand.wrap(
boost::bind(&Uart::WriteHandle,this,strSend,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred))
);
2024-07-09 09:49:42 +08:00
}*/
int len = write_data(fd,(char*)strSend,pLen);
// printf("len = %d\n",len);
// ReadFromUart();
2021-09-18 13:45:24 +08:00
}
2024-07-09 09:49:42 +08:00
int Uart::ReadFromUart()
2021-09-18 13:45:24 +08:00
{
2024-07-09 09:49:42 +08:00
/* if (mUart.is_open()) {
print_info("async_read_some,wait......................................\n");
mUart.async_read_some(buffer(&mUartRecvBuf[mRdLength], BUF_LENGTH-mRdLength-offSize),
2021-09-18 13:45:24 +08:00
mStrand.wrap(
boost::bind(&Uart::ReadHandle,this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred))
);
2024-07-09 09:49:42 +08:00
}*/
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;
/*if(len > 0){
ReadHandle(buffer,len);
}*/
2021-09-18 13:45:24 +08:00
}
void Uart::Run()
{
mIoSev.run();
}
void Uart::Stop()
{
if (mUart.is_open())
mUart.close();
mIoSev.stop();
}
2024-07-09 09:49:42 +08:00
int Uart::FindRecvPackage(int bytesRead, char* mUartRecvBuf,char* head)
{
string strTime = GetLocalTimeWithMs();
char head1[] = {0xAB,0xBC,0xCD};
char head2[] = {0xDE,0xDF,0xEF};
//LOG_INFO("m_VecWaveData= %d\n",m_VecWaveData.size());
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);
// for(int i = 0 ; i < bytesRead;i++){
// if(!(i % 100))
// printf("\n");
// printf("%02x ",mUartRecvBuf[i]);
// }
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);
//print_info("command = %d\n",command);
//print_info("index = %d\n",UartRecvBuf[i+6]&0xFF);
//print_info("mPackgeIndex1 = %d\n",mPackgeIndex);
if((mPackgeIndex == -1 || (unsigned int)UartRecvBuf[i+6] == 0) && (!bUpdatePre && !bUpdateconfig)){
//print_info("mPackgeIndex2 = %d\n",mPackgeIndex);
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;
//print_info("mPackgeIndex3 = %d\n",mPackgeIndex);
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);
2024-08-05 10:25:50 +08:00
GlobalConfig::Zigbee_G.MyAddr = 0x8888;
2024-07-09 09:49:42 +08:00
tcflush(fd,TCIOFLUSH);
sleep(1);
modify_LocalAddr(0x8888);
sleep(1);
bModifyAddr = true;
2024-08-05 10:25:50 +08:00
bSendTimeStamp = false;
2024-07-09 09:49:42 +08:00
GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = NO_ENTER_TRANSMITTING_STATUS;
GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0;
std::vector<RecvData>().swap(m_VecWaveData);
break;
}
2021-09-18 13:45:24 +08:00
2024-07-09 09:49:42 +08:00
if(command == 32){
LOG_INFO("bUpdateconfig command = %d ShortAddr :%s\n",command,strShortAddr.c_str());
memcpy(RecvBuf,(char*)&UartRecvBuf[i],12);
//for(int j = i; j < i+12;j++){
// print_info("%02X ",UartRecvBuf[j]&0xFF);
//}
if(!CheckCrc(RecvBuf,11)){
LOG_INFO("CheckCrc error command 20 \n");
mPackgeIndex = -1;
2024-08-05 10:25:50 +08:00
GlobalConfig::Zigbee_G.MyAddr = 0x8888;
2024-07-09 09:49:42 +08:00
tcflush(fd,TCIOFLUSH);
sleep(1);
modify_LocalAddr(0x8888);
sleep(1);
bModifyAddr = true;
2024-08-05 10:25:50 +08:00
bSendTimeStamp = false;
2024-07-09 09:49:42 +08:00
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");
2024-08-05 10:25:50 +08:00
GlobalConfig::Zigbee_G.MyAddr = 0x8888;
2024-07-09 09:49:42 +08:00
tcflush(fd,TCIOFLUSH);
sleep(1);
modify_LocalAddr(0x8888);
bModifyAddr = true;
sleep(1);
2024-08-05 10:25:50 +08:00
bSendTimeStamp = false;
2024-07-09 09:49:42 +08:00
GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = NO_ENTER_TRANSMITTING_STATUS;
GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0;
std::vector<RecvData>().swap(m_VecWaveData);
break;
}
mlastSize = 0;
lastSize = bytesRead - i;
//print_info("laseSize = %d , i = %d\n",lastSize,i);
if(lastSize < 100 && lastSize > 0){
memcpy(mUartRecvTmpBuf,(char*)&UartRecvBuf[bytesRead-lastSize],lastSize);
mlastSize = lastSize;
break;
}
memcpy(RecvBuf,(char*)&UartRecvBuf[i],100);
DealDataNodeWave(RecvBuf);
}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);
//if(waittime >= 2 || m_strDestShortAddr == strShortAddr)
{
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;
//break;
}/*else if(m_strDestShortAddr != strShortAddr){
usleep(100000);
waittime ++;
}*/
}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());
//memcpy(RecvBuf,(char*)&UartRecvBuf[i],100);
/*for(int j = i; j < i+100;j++){
print_info("%02X ",UartRecvBuf[j]&0xFF);
}*/
m_TimeStamp = 0;
//if(!CheckCrc((char*)&UartRecvBuf[i],99))
{
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
2024-08-05 10:25:50 +08:00
sprintf(updateSql, "UpdateFlag = UpdateFlag + 1");
2024-07-09 09:49:42 +08:00
sprintf(whereCon, "zigbeeShortAddr='%s'", buf);
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());
2024-08-05 10:25:50 +08:00
GlobalConfig::Zigbee_G.MyAddr = 0x8888;
2024-07-09 09:49:42 +08:00
bUpdateconfig = false;
mPackgeIndex = -1;
tcflush(fd,TCIOFLUSH);
sleep(1);
modify_LocalAddr(0x8888);
bModifyAddr = true;
bSendTimeStamp = false;
2024-07-09 09:49:42 +08:00
sleep(1);
2024-08-05 10:25:50 +08:00
2024-07-09 09:49:42 +08:00
GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = NO_ENTER_TRANSMITTING_STATUS;
GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0;
break;
}
break;
}else if(command == 35){
2024-08-05 10:25:50 +08:00
LOG_INFO("command = %d ShortAddr :%s,Timetamp=%s\n",command,strShortAddr.c_str(),strTimetamp.c_str());
// for(int j = i; j < i+100;j++){
// printf("%02X ",UartRecvBuf[j]&0xFF);
// }
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 = sql_ctl->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());
sql_ctl->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());
sql_ctl->UpdateTableData(T_SENSOR_INFO(TNAME), updateSql, whereCon);
}
2024-07-09 09:49:42 +08:00
}
2024-08-05 10:25:50 +08:00
2024-07-09 09:49:42 +08:00
}
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);
//pTestRecv(command);
}else if(command == 218){//DA
2024-08-05 10:25:50 +08:00
LOG_INFO("command = %d,zigbeeShortAddr = %s , signal = %02d,strTimetamp = %s\n",command,m_strDestShortAddr.c_str(),UartRecvBuf[i+6]&0xFF,strTimetamp.c_str());
2024-07-09 09:49:42 +08:00
char whereCon[1024] = {0};
char updateSql[1024] = { 0 };
char tableName[100]={0x00};
bZigbeeSinal = false;
sprintf(whereCon, "zigbeeShortAddr='%s'", m_strDestShortAddr.c_str());
2024-08-05 10:25:50 +08:00
vec_t vecDataNodeNo = sql_ctl->GetDataSingleLine(T_SENSOR_INFO(TNAME), " dataNodeNo,LooseValue,RSSI ", whereCon);
2024-07-09 09:49:42 +08:00
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());
sql_ctl->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";
}
2024-08-05 10:25:50 +08:00
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());
LOG_INFO(updateSql);
}else if(vParamRSSI.size() == 2){
sprintf(updateSql, "RSSI = '%02d,%s' ",UartRecvBuf[i+6]&0xFF,vParamRSSI[1].c_str());
LOG_INFO(updateSql);
}
sprintf(whereCon, "dataNodeNo='%s'", (char*)vecDataNodeNo[0].c_str());
sql_ctl->UpdateTableData(T_SENSOR_INFO(TNAME), updateSql, whereCon);
2024-07-09 09:49:42 +08:00
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());
}
}
}
}
strTime = GetLocalTimeWithMs();
// LOG_INFO("findRecvPackage strTime2 = %s\n",strTime.c_str());
}
void Uart::ReadHandle(char* pUartRecvBuf,size_t bytesRead)
2021-09-18 13:45:24 +08:00
{
2024-07-09 09:49:42 +08:00
/*try{
2021-09-18 13:45:24 +08:00
if (ec) {
mRdLength = 0;
2024-07-09 09:49:42 +08:00
ReadFromUart();
2021-09-18 13:45:24 +08:00
return ;
}
2024-07-09 09:49:42 +08:00
else*/
2021-09-18 13:45:24 +08:00
{ //长度检测
2024-07-09 09:49:42 +08:00
char UartRecvBuf[BUF_LENGTH]={0x00};
memcpy(UartRecvBuf,pUartRecvBuf,bytesRead);
print_info("recv uart len in ZigBee short address %4x : %d\n recv uart data:",
2021-09-18 13:45:24 +08:00
(unsigned short)GlobalConfig::Zigbee_G.MyAddr, bytesRead);
2024-07-09 09:49:42 +08:00
// for ( int i = 0; i < bytesRead; i++)
// print_debug("%02X ",mUartRecvBuf[i]&0xFF);
2021-09-18 13:45:24 +08:00
int iPackageSize = bytesRead / 100;
if (0 == iPackageSize) {
iPackageSize = 1;
}
int Count = bytesRead;
/*每次看到这块代码时总感觉不妥因为bytesRead是100的整数倍时对结果不影响如果不是则会造成数据丢失例如1024时iPackageSize的值
1024线 线线
while循环bytesRead是否大于03
ABBCCD还是AA55AAABBCCD就处理74字节数据AA55AA就处理100自己数据buf减去处理的大小数据
while循环*/
if((unsigned short)GlobalConfig::Zigbee_G.MyAddr == (unsigned short)0x9999){
}else{
2021-09-18 13:45:24 +08:00
for (int j = 0; j < iPackageSize; j++) {
2024-07-09 09:49:42 +08:00
char buf[6];
char mUartRecvPackage[100] = {0};
char ShortAddr[8];
// 多包分包,解包,
if (Count < 100) {
memcpy(mUartRecvPackage, &UartRecvBuf[j * 100], Count);
} else {
memcpy(mUartRecvPackage, &UartRecvBuf[j * 100], 100);
Count = Count - 100;
}
2021-09-18 13:45:24 +08:00
2024-07-09 09:49:42 +08:00
sprintf(&buf[0], "%02X", mUartRecvPackage[0]&0xFF);
sprintf(&buf[2], "%02X", mUartRecvPackage[1]&0xFF);
sprintf(&buf[4], "%02X", mUartRecvPackage[2]&0xFF);
sprintf(ShortAddr, "%02x%02x", mUartRecvPackage[3]&0xFF, mUartRecvPackage[4]&0xFF);
if(bUpdate){//waitting for the upated node 4s
std::string strShortAddr(ShortAddr);
print_info("m_strDestShortAddr = %s,strShortAddr = %s,waittime = %d\n",m_strDestShortAddr.c_str(),strShortAddr.c_str(),waittime);
if(waittime >= 40 || m_strDestShortAddr == strShortAddr){
LOG_INFO("Online = %s\n",strShortAddr.c_str());
waittime = 0;
bUpdate = false;
m_strDestShortAddr = "";
}else if(m_strDestShortAddr != strShortAddr){
usleep(100000);
waittime ++;
return ;
}
}
2021-09-18 13:45:24 +08:00
std::string strHeadFlag(buf);
print_info("data package head command type:%s\n", strHeadFlag.c_str());
if ( 0 == strHeadFlag.compare("ABBCCD") && (0xD1 == (mUartRecvPackage[3]&0xFF))) {
UpdateZigbeeInfo(mUartRecvPackage);
}
if ( 0 == strHeadFlag.compare("DEDFEF") ) {
LOG_INFO("ReadHandle flag = %s\n",strHeadFlag.c_str());
2021-09-18 13:45:24 +08:00
}
if ( 0 == strHeadFlag.compare("AA55AA") ) {
DealRecvData(mUartRecvPackage);
}
}}
2021-09-18 13:45:24 +08:00
}
2024-07-09 09:49:42 +08:00
/* //读串口循环
2021-09-18 13:45:24 +08:00
ReadFromUart();
} catch(...) {
print_error("PlatFormInit exception happend.\n");
// LOG_ERROR("%s,%d\n", __FUNCTION__, __LINE__);
2024-07-09 09:49:42 +08:00
}*/
2021-09-18 13:45:24 +08:00
}
void Uart::setCallBack(onReceiveUart _callback)
{
m_callback = _callback;
}
void Uart::DataAnalysis_R(DevDataOfGwid &pData)
{
mLocalTime = boost::posix_time::microsec_clock::local_time();
m_callback(pData);
mLocalTime = boost::posix_time::microsec_clock::local_time();
}
void Uart::WriteHandle(const char *strSend,const boost::system::error_code &ec,size_t bytesWrite)
{
if (ec) {
print_error("Fail To Write Uart! ]\n");
// LOG_ERROR("Fail To Write Uart! ]\n");
} else {
// print_info("[ To Uart ][ Bytes:%d ]\n",bytesWrite);
2021-09-18 13:45:24 +08:00
}
}
void Uart::ThreadInit()
{
// SignalInit();
}
void Uart::DataAnalysis_W(DevData &pData,bool pFlag)
{
this->WriteToUart(pData.mData,pData.mLen);
// LOG_INFO("WriteToUart:%s", pData.mDataMing);
boost::this_thread::sleep(boost::posix_time::milliseconds(110));
}
void Uart::UpdateZigbeeInfoCtrl()
{
2024-07-09 09:49:42 +08:00
/* char buf[5] = {0xAB, 0xBC, 0xCD, 0xD1, 0x05};
2021-09-18 13:45:24 +08:00
WriteToUart(buf, 5);
2024-07-09 09:49:42 +08:00
sleep(1);*/
// ReadFromUart();
char command[5]={0x00};
unsigned char command1[100]={0x00};
command[0] = 0xab;
command[1] = 0xbc;
command[2] = 0xcd;
command[3] = 0xd1;
command[4] = 0xaa;
WriteToUart(command,5);
LOG_INFO("UpdateZigbeeInfoCtrl \n");
/*for ( int i = 0; i < 20; i++)
print_info("%02X ",command[i]);
print_info("\n");*/
/*int ret = write_data(fd, (char*)command, 5);
if (ret < 0) {
perror("write");
return ;
}
// sleep(1);
/*ret = read_data(fd, (char *)command1, 100, 10);
printf("ret = %d\n",ret);
if (ret <= 4) {
printf("info get error!\n");
return ;
}
UpdateZigbeeInfo((char*)command1);*/
2021-09-18 13:45:24 +08:00
}
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);
2024-07-09 09:49:42 +08:00
GlobalConfig::ZigbeeInfo_G.DevMode = atoi(buff);
2021-09-18 13:45:24 +08:00
memset(buff, 0, 8);
sprintf(buff, "%02d", pData[37]&0xFF);
2024-07-09 09:49:42 +08:00
GlobalConfig::ZigbeeInfo_G.Channel = atoi(buff);
2021-09-18 13:45:24 +08:00
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);
2024-07-09 09:49:42 +08:00
GlobalConfig::ZigbeeInfo_G.DstAddr = std::string(buff);
2021-09-18 13:45:24 +08:00
2024-07-09 09:49:42 +08:00
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());
// if(GlobalConfig::ZigbeeInfo_G.MyAddr != "8888"){
// modify_LocalAddr(0x8888);
//
// LOG_INFO("zigbee Update \n");
// GlobalConfig::ZigbeeInfo_G.MyAddr = "8888";
// }
LOG_INFO("PanID = %s,MacAddr_G= %s\n",GlobalConfig::ZigbeeInfo_G.PanID.c_str(),GlobalConfig::MacAddr_G.c_str());
// if(GlobalConfig::ZigbeeInfo_G.PanID != GlobalConfig::MacAddr_G.substr(8)){
// string strPanId = GlobalConfig::MacAddr_G.substr(8);
// long lShortAddr = strtol(strPanId.c_str(), NULL, 16);
// unsigned short panid = lShortAddr & 0xffff;
// //modify_LocalPanID(panid);
// WritePanId2Zigbee(panid);
// usleep(100000);
// LOG_ERROR("PanID error");
// }
std::string strchan = ReadStrByOpt(ZIGBEECONFIG, "Zigbee", "channel");
LOG_INFO("Channel = %d,strchan = %s\n",GlobalConfig::ZigbeeInfo_G.Channel,strchan.c_str());
// if(GlobalConfig::ZigbeeInfo_G.Channel != atoi(strchan.c_str())){
// unsigned short Chan = (unsigned short)strtol(strchan.c_str(), NULL, 10);
// if(Chan > 10 && Chan < 27)
// {
// //modify_Localchannel(Chan);
// WriteChanl2Zigbee(Chan);
// usleep(100000);
// }
// LOG_ERROR("channel error");
// }
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);
}
// LOG_INFO("[UpdateZigbeeInfo---] ZigBee PanID: %s ; Channel: %d ; MyAddr : %s ",GlobalConfig::ZigbeeInfo_G.PanID.c_str(),GlobalConfig::ZigbeeInfo_G.Channel,GlobalConfig::ZigbeeInfo_G.MyAddr.c_str());
2021-09-18 13:45:24 +08:00
}
void int2bytes(int i, unsigned char* bytes, int size) {
// byte[] bytes = new byte[4];
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);
}
bool Uart::ReadUpdatePackge(unsigned char* pDestShortAddr)
{
//compare hardversion in update list
std::vector<std::string> strHWversion;
2024-07-09 09:49:42 +08:00
std::string strFileName = "",strSoftVersion = "";
std::vector <DataNodeUpdate> vecDataNodeUpdate;
vecDataNodeUpdate = ReadStrUpdate("/opt/DataNode/config.json");
char gethardVersion_sql[32] = { 0 };
2024-07-09 09:49:42 +08:00
char selectsql[1024]={ 0 };
sprintf(gethardVersion_sql, "zigbeeShortAddr='%02x%02x'", pDestShortAddr[0],pDestShortAddr[1]);
2024-07-09 09:49:42 +08:00
sprintf(selectsql,"%s,%s",T_SENSOR_INFO(HARDVERSION),T_SENSOR_INFO(SOFTVERSION));
vec_t vecResult = sql_ctl->GetDataSingleLine(T_SENSOR_INFO(TNAME), selectsql, gethardVersion_sql);
if(vecResult.size() < 1){
return false;
}
print_info("hardversion %s,softversion %s\n",vecResult[0].c_str(),vecResult[1].c_str());
int thisindex = -1;
2024-07-09 09:49:42 +08:00
int flag = -1;
for(int j = 0; j < vecDataNodeUpdate.size();j++){
for(int i = 0; i < vecDataNodeUpdate[j].hwVersion.size();i++){
if(vecResult[0] == vecDataNodeUpdate[j].hwVersion[i]){
if(vecResult[1] == vecDataNodeUpdate[j].strSoftVersion){
flag = 0;
break;
}else{
thisindex = i;
strFileName = vecDataNodeUpdate[j].strUpdataFileName;
break;
}
}
}
2024-07-09 09:49:42 +08:00
if(thisindex >= 0 || flag == 0)
break;
}
2024-07-09 09:49:42 +08:00
if(thisindex < 0)
return false;
2024-07-09 09:49:42 +08:00
// if(!strncmp(vecResult[0].c_str(),"3",1)){
// WriteTranTimeout2Zigbee(0x03);
// }else if(!strncmp(vecResult[0].c_str(),"4",1)){
// WriteTranTimeout2Zigbee(0x0A);
// }
print_info("thisindex = %d\n",thisindex);
FILE * pFile=NULL;
int thisSize = 0;
2024-07-09 09:49:42 +08:00
DataNodeUpdateFile = "/opt/DataNode/" + strFileName;
print_info("strFileName = %s\n",DataNodeUpdateFile.c_str());
pFile = fopen (DataNodeUpdateFile.c_str(),"rb");
if (pFile==NULL){
return false;
}
else
{
while (fgetc(pFile) != EOF) {
++thisSize;
}
fclose (pFile);
}
2024-07-09 09:49:42 +08:00
// WriteShortAddr_DistAddr2Zigbee(localAddr,pDestShortAddr);//永久参数配置
/* char tmpbuf[8] = {0x00};
* unsigned short localAddr = 0x9999;
sprintf(tmpbuf,"%02x%02x",pDestShortAddr[0],pDestShortAddr[1]);
m_strDestShortAddr = std::string(tmpbuf);
modify_distaddr_info(localAddr,"",pDestShortAddr);//临时参数配置
GlobalConfig::Zigbee_G.MyAddr = 0x9999;*/
unsigned char Data[12]={0x00};
unsigned char size[4]={0x00};
2024-07-09 09:49:42 +08:00
print_info("thisSize = %d\n",thisSize);
//帧头[3byte] 节点地址[2byte] 数据类型[1byte] 序号[1byte] 升级包大小[4byte] CRC校验[1byte]
Data[0]=0xAA;
Data[1]=0x55;
Data[2]=0xAA;
Data[3]=pDestShortAddr[0];
Data[4]=pDestShortAddr[1];
2024-07-09 09:49:42 +08:00
Data[5]=0x20;
Data[6]=0x00;
int2bytes(thisSize,size,4);
Data[7]=size[3];
Data[8]=size[2];
Data[9]=size[1];
Data[10]=size[0];
unsigned char tmp = 0x00;
for(int i = 0 ; i < 11;i++){
tmp +=Data[i];
}
Data[11]=tmp;
2024-07-09 09:49:42 +08:00
sleep(1);
WriteToUart((const char*)Data,12);
2024-07-09 09:49:42 +08:00
string strTime = GetLocalTimeWithMs();
LOG_INFO("ReadUpdatePackge strTime = %s\n",strTime.c_str());
return true;
}
void Uart::UpdateWirelessNode(unsigned short shortAdd)
{
2024-07-09 09:49:42 +08:00
string strTime = GetLocalTimeWithMs();
LOG_INFO("UpdateWirelessNode start = %s UpdateWirelessNode id = %02x %02x\n",strTime.c_str(),UINT16_HIGH(shortAdd),UINT16_LOW(shortAdd));
/*std::string strFileName = "",strSoftVersion = "";
std::vector <DataNodeUpdate> vecDataNodeUpdate;
vecDataNodeUpdate = ReadStrUpdate("/opt/DataNode/config.json");
char gethardVersion_sql[32] = { 0 };
sprintf(gethardVersion_sql, "zigbeeShortAddr='%02x%02x'", UINT16_HIGH(shortAdd),UINT16_LOW(shortAdd));
std::string SoftVersion = sql_ctl->GetData(T_SENSOR_INFO(TNAME), "softVersion", gethardVersion_sql);
print_info("SoftVersion = %s\n",SoftVersion.c_str());
LOG_INFO(" NOW SoftVersion = %s\n",SoftVersion.c_str());*/
//////
//////
//strFileName = "/opt/DataNode/" + strFileName;
FILE * pFile=NULL;
int thisSize = 0;
char *buffer=NULL;
int resendCount = 0;
pFile = fopen (DataNodeUpdateFile.c_str(),"rb");
if (pFile==NULL) perror ("Error opening file");
else
{
while (fgetc(pFile) != EOF) {
++thisSize;
}
rewind(pFile);
buffer = (char*)malloc(thisSize);//
fread (buffer, sizeof (char), thisSize, pFile);
fclose (pFile);
int Count = thisSize / 92;
int lastSize = thisSize % 92;
unsigned char UpdateData[100]={0x00};
//帧头[3byte] 节点地址[2byte] 数据类型[1byte] 序号[1byte] 数据包[92byte] CRC校验[1byte]
print_info("Start Update!!! file Size = %d\n",thisSize);
unsigned char tmp = 0x00;
gpio_set(GlobalConfig::GPIO_G.zigAckreset,0);
//boost::this_thread::sleep(boost::posix_time::milliseconds(1));
usleep(1000);
gpio_set(GlobalConfig::GPIO_G.zigAckreset,1);
for(int j = 0; j < Count;j++){
int time ,value;
UpdateData[0]=0xAA;
UpdateData[1]=0x55;
UpdateData[2]=0xAA;
UpdateData[3]=UINT16_HIGH(shortAdd);
UpdateData[4]=UINT16_LOW(shortAdd);
UpdateData[5]=0x21;
UpdateData[6]=0xff & j;
memcpy(&UpdateData[7],buffer+92*j,92);
tmp = 0x00;
for(int k = 0; k < 99;k++){
tmp +=UpdateData[k];
}
UpdateData[99] = tmp;
// RESEND:
WriteToUart((const char*)UpdateData,100);
if(gpio_read(GlobalConfig::GPIO_G.zigAckrep) == 48)
gpio_set(GlobalConfig::GPIO_G.zigAckreset,1);
time = 0;
do{
value = gpio_read(GlobalConfig::GPIO_G.zigAckrep);
if(value == 49)
break;
usleep(10000);
time += 1;
}while(time < 150);
if(time >= 150){
resendCount++;
if(resendCount < 5){
LOG_INFO(" RESEND gpio_read failed shortAdd %x %x,error index %d\n",UINT16_HIGH(shortAdd),UINT16_LOW(shortAdd),j);
}else{
LOG_INFO("gpio_read failed shortAdd %x %x,error index %d\n",UINT16_HIGH(shortAdd),UINT16_LOW(shortAdd),j);
print_red("gpio_read failed \n");
GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = NO_ENTER_TRANSMITTING_STATUS;
GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0;
bUpdate = false;
goto endUpdate;
}
2024-07-09 09:49:42 +08:00
}
gpio_set(GlobalConfig::GPIO_G.zigAckreset,0);
usleep(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);
}
if(gpio_read(GlobalConfig::GPIO_G.zigAckrep) == 48)
gpio_set(GlobalConfig::GPIO_G.zigAckreset,1);
// printf("Count =%d,lastSize = %d\n",Count,lastSize);
if(lastSize > 0){
UpdateData[0]=0xAA;
UpdateData[1]=0x55;
UpdateData[2]=0xAA;
UpdateData[3]=UINT16_HIGH(shortAdd);
UpdateData[4]=UINT16_LOW(shortAdd);
UpdateData[5]=0x21;
UpdateData[6]=0xff & Count;
memcpy(&UpdateData[7],buffer+92*Count,lastSize);
tmp = 0x00;
for(int k = 0; k < 99;k++){
tmp +=UpdateData[k];
}
UpdateData[99] = tmp;
WriteToUart((const char*)UpdateData,100);
boost::this_thread::sleep(boost::posix_time::milliseconds(10));
// if(gpio_read(GlobalConfig::GPIO_G.zigAckrep) == 49){
// gpio_set(GlobalConfig::GPIO_G.zigAckreset,0);
// boost::this_thread::sleep(boost::posix_time::milliseconds(1));
// if(gpio_read(GlobalConfig::GPIO_G.zigAckrep) == 48)gpio_set(GlobalConfig::GPIO_G.zigAckreset,1);
// }
int time = 0;
do{
int value = gpio_read(GlobalConfig::GPIO_G.zigAckrep);
if(value == 49){
gpio_set(GlobalConfig::GPIO_G.zigAckreset,0);
usleep(10000);
if(gpio_read(GlobalConfig::GPIO_G.zigAckrep) == 48)
gpio_set(GlobalConfig::GPIO_G.zigAckreset,1);
break;
}
usleep(10000);
2024-07-09 09:49:42 +08:00
time += 1;
}while(time < 150);
if(time >= 150){
2024-07-09 09:49:42 +08:00
LOG_INFO("gpio_read failed shortAdd %x %x\n",UINT16_HIGH(shortAdd),UINT16_LOW(shortAdd));
print_info("gpio_read failed \n");
GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = NO_ENTER_TRANSMITTING_STATUS;
GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0;
bUpdate = false;
goto endUpdate;
}
memset(UpdateData,0x00,sizeof(UpdateData));
}
print_info("Update END!!! file Size = %d\n",thisSize);
}
endUpdate:
resendCount = 0;
2024-07-09 09:49:42 +08:00
free(buffer);
tcflush(fd,TCIFLUSH);
boost::this_thread::sleep(boost::posix_time::seconds(1));
bUpdate = false;
bSendTimeStamp = false;
modify_LocalAddr(0x8888);
bModifyAddr = true;
boost::this_thread::sleep(boost::posix_time::milliseconds(100));
GlobalConfig::Zigbee_G.MyAddr = 0x8888;
DataNodeUpdateFile = "";
std::vector<RecvData>().swap(m_VecWaveData);
// WriteShortAddr2Zigbee(shortAddr);
// UpdateZigbeeInfoCtrl();
GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = NO_ENTER_TRANSMITTING_STATUS;
GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0;
LOG_INFO("UpdateWirelessNode end");
}
2024-08-05 10:25:50 +08:00
2024-07-09 09:49:42 +08:00
int Uart::UpdateConfig(unsigned char* pDestShortAddr)
{
char whereCon[64] = { 0 };
char selCon[100]={0x00};
sprintf(whereCon, "zigbeeShortAddr='%02x%02x'", pDestShortAddr[0],pDestShortAddr[1]);
//std::string strUpdate = sql_ctl->GetData(T_SENSOR_INFO(TNAME), "UpdateFlag", whereCon);
vec_t vecResultNode = sql_ctl->GetDataSingleLine(T_SENSOR_INFO(TNAME), "*", whereCon);
if(vecResultNode.size() <= 0)
return -1;
if(vecResultNode[41] == "0")
{
2024-07-09 09:49:42 +08:00
// if(!strncmp(vecResult[1].c_str(),"3",1)){//zm5161
// WriteTranTimeout2Zigbee(0x03);
// }else if(!strncmp(vecResult[1].c_str(),"4",1)){//aw21
// WriteTranTimeout2Zigbee(0x0A);
// }
print_info("UpdateConfig\n");
bUpdateconfig = true;
unsigned short localAddr = 0x9999;
GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = ENTER_TRANSMITTING_STATUS;
GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0;
2024-07-09 09:49:42 +08:00
// WriteShortAddr_DistAddr2Zigbee(localAddr,pDestShortAddr);//永久参数配置
char tmpbuf[8] = {0x00};
sprintf(tmpbuf,"%02x%02x",pDestShortAddr[0],pDestShortAddr[1]);
m_strDestShortAddr = std::string(tmpbuf);
2024-08-05 10:25:50 +08:00
//modify_distaddr_info(localAddr,"",pDestShortAddr);//临时参数配置
2024-07-09 09:49:42 +08:00
GlobalConfig::Zigbee_G.MyAddr = 0x9999;
boost::this_thread::sleep(boost::posix_time::milliseconds(500));
vec_t vecResult;
sprintf(selCon,"featureInterVal,waveInterVal,range,samplingRate,ACCSampleTime,startBrands,stopBrands,\
2024-08-05 10:25:50 +08:00
envelopeBandPass,faultFrequency,timeStamp,viff,ZigbeePower,ZigbeeRetry,MeasurementID");
2024-07-09 09:49:42 +08:00
vecResult = sql_ctl->GetDataSingleLine(T_SENSOR_INFO(TNAME),selCon,whereCon);
print_info("vecResult size = %d\n",vecResult.size());
if(vecResult.size() < 1){
return -1;
}
unsigned char UpdateData[100]={0x00};
//帧头[3byte] 节点地址[2byte] 数据类型[1byte] 序号[1byte] 数据包[92byte] CRC校验[1byte]
2024-07-09 09:49:42 +08:00
UpdateData[0]=0xAA;
UpdateData[1]=0x55;
UpdateData[2]=0xAA;
UpdateData[3]=pDestShortAddr[0];
UpdateData[4]=pDestShortAddr[1];
UpdateData[5]=0x22;
UpdateData[6]=0x00;
2024-08-05 10:25:50 +08:00
UpdateData[7]=0x01;
2024-07-09 09:49:42 +08:00
UpdateData[8]=UINT16_LOW(atoi(vecResult[0].c_str()));
UpdateData[9]=UINT16_HIGH(atoi(vecResult[1].c_str()));
UpdateData[10]=UINT16_LOW(atoi(vecResult[1].c_str()));
int y = 0;
if(vecResultNode[17] == "01"){
if(vecResult[3]=="3200"){
y = 0;
}else if(vecResult[3]=="6400"){
y = 1;
}else if(vecResult[3]=="12800"){
y = 2;
}else if(vecResult[3]=="25600"){
y = 3;
}
2024-07-09 09:49:42 +08:00
}else if(vecResultNode[17] == "02"){
if(vecResult[3]=="8000"){
y = 0;
}else if(vecResult[3]=="16000"){
y = 1;
}else if(vecResult[3]=="24000"){
y = 2;
}else if(vecResult[3]=="32000"){
y = 3;
}else if(vecResult[3]=="48000"){
y = 4;
}else if(vecResult[3]=="96000"){
y = 5;
}else if(vecResult[3]=="192000"){
y = 6;
}
}
2024-08-05 10:25:50 +08:00
UpdateData[18] = atoi(vecResult[11].c_str()) & 0xFF;;
UpdateData[19] = atoi(vecResult[12].c_str()) & 0xFF;;
2024-07-09 09:49:42 +08:00
int x = atoi(vecResult[2].c_str());
UpdateData[21] = BUILD_UINT2(x,y);
UpdateData[22] = atoi(vecResult[4].c_str()) & 0xFF;
print_info("vecResult size = %s==%s==%s==%s\n",vecResult[5].c_str(),vecResult[6].c_str(),vecResult[7].c_str(),vecResult[8].c_str());
vector<string> vStart,vStop,vEnvelopeBandPass,vfaultFrequency;
boost::split( vStart, vecResult[5], boost::is_any_of( "," ), boost::token_compress_on );
boost::split( vStop, vecResult[6], boost::is_any_of( "," ), boost::token_compress_on );
boost::split( vEnvelopeBandPass, vecResult[7], boost::is_any_of( "," ), boost::token_compress_on );
boost::split( vfaultFrequency, vecResult[8], boost::is_any_of( "," ), boost::token_compress_on );
UpdateData[23] = UINT16_HIGH(atoi(vStart[0].c_str()));
UpdateData[24] = UINT16_LOW(atoi(vStart[0].c_str()));
UpdateData[25] = UINT16_HIGH(atoi(vStop[0].c_str()));
UpdateData[26] = UINT16_LOW(atoi(vStop[0].c_str()));
UpdateData[27] = UINT16_HIGH(atoi(vStart[1].c_str()));
UpdateData[28] = UINT16_LOW(atoi(vStart[1].c_str()));
UpdateData[29] = UINT16_HIGH(atoi(vStop[1].c_str()));
UpdateData[30] = UINT16_LOW(atoi(vStop[1].c_str()));
UpdateData[31] = UINT16_HIGH(atoi(vStart[2].c_str()));
UpdateData[32] = UINT16_LOW(atoi(vStart[2].c_str()));
UpdateData[33] = UINT16_HIGH(atoi(vStop[2].c_str()));
UpdateData[34] = UINT16_LOW(atoi(vStop[2].c_str()));
UpdateData[35] = UINT16_HIGH(atoi(vStart[3].c_str()));
UpdateData[36] = UINT16_LOW(atoi(vStart[3].c_str()));
UpdateData[37] = UINT16_HIGH(atoi(vStop[3].c_str()));
UpdateData[38] = UINT16_LOW(atoi(vStop[3].c_str()));
UpdateData[39] = UINT16_HIGH(atoi(vStart[4].c_str()));
UpdateData[40] = UINT16_LOW(atoi(vStart[4].c_str()));
UpdateData[41] = UINT16_HIGH(atoi(vStop[4].c_str()));
UpdateData[42] = UINT16_LOW(atoi(vStop[4].c_str()));
UpdateData[43] = UINT16_HIGH(atoi(vEnvelopeBandPass[0].c_str()));
UpdateData[44] = UINT16_LOW(atoi(vEnvelopeBandPass[0].c_str()));
UpdateData[45] = UINT16_HIGH(atoi(vEnvelopeBandPass[1].c_str()));
UpdateData[46] = UINT16_LOW(atoi(vEnvelopeBandPass[1].c_str()));
UpdateData[47] = UINT16_HIGH(atoi(vfaultFrequency[0].c_str()));
UpdateData[48] = UINT16_LOW(atoi(vfaultFrequency[0].c_str()));
UpdateData[49] = UINT16_HIGH(atoi(vfaultFrequency[1].c_str()));
UpdateData[50] = UINT16_LOW(atoi(vfaultFrequency[1].c_str()));
UpdateData[51] = UINT16_HIGH(atoi(vfaultFrequency[2].c_str()));
UpdateData[52] = UINT16_LOW(atoi(vfaultFrequency[2].c_str()));
UpdateData[53] = UINT16_HIGH(atoi(vfaultFrequency[3].c_str()));
UpdateData[54] = UINT16_LOW(atoi(vfaultFrequency[3].c_str()));
UpdateData[55] = UINT32_HIGH_1(atoi(vecResult[9].c_str()));
UpdateData[56] = UINT32_HIGH_2(atoi(vecResult[9].c_str()));
UpdateData[57] = UINT32_LOW_1(atoi(vecResult[9].c_str()));
UpdateData[58] = UINT32_LOW_2(atoi(vecResult[9].c_str()));
UpdateData[59] = (atoi(vecResult[10].c_str())) & 0xFF;
2024-08-05 10:25:50 +08:00
size_t bytesSize = strlen(vecResult[13].c_str()) / 2;
unsigned char* bytes = (unsigned char*)malloc(bytesSize);
if (hexStringToBytes(vecResult[13].c_str(), bytes, bytesSize) != 0) {
free(bytes);
}else{
UpdateData[60] = bytes[0];
UpdateData[61] = bytes[1];
UpdateData[62] = bytes[2];
UpdateData[63] = bytes[3];
UpdateData[64] = bytes[4];
UpdateData[65] = bytes[5];
UpdateData[66] = bytes[6];
UpdateData[67] = bytes[7];
}
free(bytes);
unsigned char tmp1 = 0x00;
2024-07-09 09:49:42 +08:00
for(int k = 0; k < 99;k++){
2024-08-05 10:25:50 +08:00
tmp1 +=UpdateData[k];
2024-07-09 09:49:42 +08:00
}
2024-08-05 10:25:50 +08:00
UpdateData[99] = tmp1;
2024-07-09 09:49:42 +08:00
tcflush(fd,TCIFLUSH);
WriteToUart((const char*)UpdateData,100);
return 0;
2024-08-05 10:25:50 +08:00
}else if(vecResultNode[41] == "-1"){
bUpdateconfig = true;
unsigned short localAddr = 0x9999;
GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = ENTER_TRANSMITTING_STATUS;
GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0;
string strName = sql_ctl->GetData(T_SENSOR_INFO(TNAME)," dataNodeName ",whereCon);
unsigned char UpdateData[100]={0x00};
//帧头[3byte] 节点地址[2byte] 数据类型[1byte] 序号[1byte] 数据包[92byte] CRC校验[1byte]
UpdateData[0]=0xAA;
UpdateData[1]=0x55;
UpdateData[2]=0xAA;
UpdateData[3]=pDestShortAddr[0];
UpdateData[4]=pDestShortAddr[1];
UpdateData[5]=0x22;
UpdateData[6]=0x00;
UpdateData[7]=0x02;
char hex[200]={0X00};
stringToHex(strName.c_str(),hex);
size_t bytesSize = strlen(hex) / 2;
unsigned char* bytes = (unsigned char*)malloc(bytesSize);
if (hexStringToBytes(hex, bytes, bytesSize) != 0) {
free(bytes);
}else{
for (size_t i = 0; i < bytesSize; i++)
{
UpdateData[8 + i] = bytes[i];
}
free(bytes);
unsigned char tmp1 = 0x00;
for(int k = 0; k < 99;k++){
tmp1 += UpdateData[k];
}
UpdateData[99] = tmp1;
tcflush(fd,TCIFLUSH);
WriteToUart((const char*)UpdateData,100);
return 0;
}
2024-07-09 09:49:42 +08:00
}else{
return -1;
}
2024-07-09 09:49:42 +08:00
}
void Uart::UpdateWirelessNodeTime(unsigned char* pDestShortAddr,int modifyaddr/*,int nodewaveindex,int nodetime,int nodeindex*/)
{
if(modifyaddr)
modify_DistAddr(pDestShortAddr);
usleep(10000);
//print_info("nodewaveindex = %d,nodetime = %d,nodeindex = %d\n",nodewaveindex,nodetime,nodeindex);
char localtimestamp[32]={0x00};
int millisecond = 0;
string rtcTime = GetRTC(localtimestamp,millisecond);
LOG_INFO("ShortAddr = %02x%02x,rtcTime = %s,localtimestamp = %s,millisecond = %d,bSendTimeStamp = %d \n",pDestShortAddr[0],pDestShortAddr[1],rtcTime.c_str(),localtimestamp,millisecond,bSendTimeStamp);
// struct timeval tv;
// gettimeofday(&tv, NULL);
// int millisecond = tv.tv_usec / 1000;
// sprintf(localtimestamp, "%ld", tv.tv_sec);
unsigned char UpdateData[100]={0x00};
UpdateData[0]=0xAA;
UpdateData[1]=0x55;
UpdateData[2]=0xAA;
UpdateData[3]=pDestShortAddr[0];
UpdateData[4]=pDestShortAddr[1];
UpdateData[5]=0x23;
UpdateData[6]=0x00;
UpdateData[7]=0x00;
UpdateData[8]=UINT32_LOW_2(atol(localtimestamp));
UpdateData[9]=UINT32_LOW_1(atol(localtimestamp));
UpdateData[10]=UINT32_HIGH_2(atol(localtimestamp));
UpdateData[11]=UINT32_HIGH_1(atol(localtimestamp));
UpdateData[12]=UINT16_LOW(millisecond);
UpdateData[13]=UINT16_HIGH(millisecond);
//UpdateData[13]=UINT16_HIGH(nodetime);
//UpdateData[14]=UINT16_LOW(nodetime);
//UpdateData[15]=UINT16_LOW(nodewaveindex);
//UpdateData[16]=UINT16_LOW(nodeindex);
unsigned char tmp = 0x00;
for(int k = 0; k < 99;k++){
tmp += UpdateData[k];
}
UpdateData[99] = tmp;
WriteToUart((const char*)UpdateData,100);
}
2021-09-18 13:45:24 +08:00
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);
2024-07-09 09:49:42 +08:00
int flag = atoi(buf);
2021-09-18 13:45:24 +08:00
print_info("the data package type(1,2,3,4,5,6) is %s,%x\n",buf,pData[5]&0xFF);
2021-09-18 13:45:24 +08:00
switch (flag)
{
case 1:{//0x01:设备信息
2021-09-18 13:45:24 +08:00
DealDataNodeInfo(pData);
}
break;
case 2:{//0x02:特征值
2021-09-18 13:45:24 +08:00
DealDataNodeFeature(pData, 0);
}
break;
2024-07-09 09:49:42 +08:00
/* case 3:{//0x03:长波形X轴
2021-09-18 13:45:24 +08:00
DealDataNodeWave(pData);
}
break;
case 4:{//0x04:长波形Y轴
2021-09-18 13:45:24 +08:00
DealDataNodeWave(pData);
}
break;
case 5:{//0x05:长波形Z轴
2021-09-18 13:45:24 +08:00
DealDataNodeWave(pData);
}
2024-07-09 09:49:42 +08:00
break;*/
case 6:{//0x06:特征值+长波形
2024-07-09 09:49:42 +08:00
//LOG_INFO("DealDataNodeFeature 06");
2021-09-18 13:45:24 +08:00
DealDataNodeFeature(pData, 1);
}
break;
2024-07-09 09:49:42 +08:00
case 32:{//升级
UpdateWirelessNode(ushortAdd);
}
break;
2024-07-09 09:49:42 +08:00
case 7:{
DealDataNodeName(pData);
}
break;
2021-09-18 13:45:24 +08:00
default:
break;
}
}
2024-07-09 09:49:42 +08:00
void Uart::DealDataNodeName(const char* pData)
{
bSendTimeStamp = false;
2024-08-05 10:25:50 +08:00
print_info("DealDataNodeName \n");
2024-07-09 09:49:42 +08:00
string strTime = GetLocalTimeWithMs();
//LOG_INFO("DealDataNodeName Time = %s\n",strTime.c_str());
2024-08-05 10:25:50 +08:00
char szShortAdd[8]={0x00};
char shortAdd[8]={0x00};
2024-07-09 09:49:42 +08:00
char NodeName[64]={0x00};
2024-08-05 10:25:50 +08:00
sprintf(szShortAdd, "%02x%02x", pData[3]&0xFF,pData[4]&0xFF);
memcpy(NodeName,&pData[7],64);
2024-07-09 09:49:42 +08:00
memcpy(shortAdd,&pData[3],2);
2024-08-05 10:25:50 +08:00
char whereCon[64] = { 0 };
char uplCon[200]={0x00};
int iRet = -1;
2024-07-09 09:49:42 +08:00
UpdateWirelessNodeTime((unsigned char*)shortAdd,1);
for(int i = 0; i < 64;i++){
sprintf(&NodeName[i * 2], "%02X", pData[7 + i]&0xFF);
}
2024-08-05 10:25:50 +08:00
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]);
2024-07-09 09:49:42 +08:00
char gbkNodeName[128]={0x00};
2024-08-05 10:25:50 +08:00
sprintf(whereCon, "zigbeeShortAddr='%s'", szShortAdd);
print_info("whereCon = %s\n",whereCon);
array_t vecRes = sql_ctl->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());
sql_ctl->DeleteTableData(T_SENSOR_INFO(TNAME), whereCon1);
sql_ctl->DeleteTableData(T_DATA_INFO(TNAME), whereCon1);
sql_ctl->DeleteTableData(T_DATASTATIC_INFO(TNAME), whereCon1);
sql_ctl->DeleteTableData(T_DATANODE_TIME(TNAME), whereCon1);
char szTableName[50]={0x00};
sprintf(szTableName,"DROP TABLE t_data_%s",vecRes[i][0].c_str());
sql_ctl->CreateTable(szTableName, 0);
memset(szTableName,0x00,sizeof(szTableName));
sprintf(szTableName,"DROP TABLE t_dataStatic_%s",vecRes[i][0].c_str());
sql_ctl->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));
}
2024-07-09 09:49:42 +08:00
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);
2024-07-09 09:49:42 +08:00
print_info("whereCon = %s\n",whereCon);
2024-08-05 10:25:50 +08:00
sprintf(uplCon,"dataNodeName = '%s' , MeasurementID = '%s'",gbkNodeName,MeasurementID);
iRet = sql_ctl->UpdateTableData(T_SENSOR_INFO(TNAME),uplCon,whereCon,0);
2024-07-09 09:49:42 +08:00
string strData = sql_ctl->GetNodeConfigureInfor(whereCon);
iRet = data_publish(strData.c_str(), GlobalConfig::Topic_G.mPubConfig.c_str());
2021-09-18 13:45:24 +08:00
2024-07-09 09:49:42 +08:00
}
2024-08-05 10:25:50 +08:00
2021-09-18 13:45:24 +08:00
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
2021-09-18 13:45:24 +08:00
DataNodeInfo dataNodeInfo;
2024-07-09 09:49:42 +08:00
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);
2021-09-18 13:45:24 +08:00
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
2021-09-18 13:45:24 +08:00
chTemp = pRecvData->Data[9];//硬件版本 1 byte
2021-09-18 13:45:24 +08:00
memset(buf, 0, 32);
2024-07-09 09:49:42 +08:00
sprintf(buf, "%.1f", 0.1*chTemp);
2021-09-18 13:45:24 +08:00
dataNodeInfo.HardVersion = std::string(buf);
chTemp = pRecvData->Data[10];//软件版本 1 byte
2021-09-18 13:45:24 +08:00
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
2021-09-18 13:45:24 +08:00
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
2021-09-18 13:45:24 +08:00
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
2021-09-18 13:45:24 +08:00
//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]));
2024-07-09 09:49:42 +08:00
dataNodeInfo.WakeupTime = atoi(buf);//唤醒次数 4 byte
2021-11-13 15:38:32 +08:00
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]));
2024-07-09 09:49:42 +08:00
dataNodeInfo.StaticTime = atoi(buf);//特征值发送次数 4 byte
2021-11-13 15:38:32 +08:00
printf("StaticTime = %d\n",dataNodeInfo.StaticTime);
memset(buf, 0, 32);
2021-11-13 15:38:32 +08:00
// sprintf(buf, "%d", BUILD_UINT32(pRecvData->Data[31], pRecvData->Data[32], pRecvData->Data[33], pRecvData->Data[34]));
2021-11-13 15:38:32 +08:00
dataNodeInfo.WaveTime = BUILD_UINT32(pRecvData->Data[31], pRecvData->Data[32], pRecvData->Data[33], pRecvData->Data[34]);//原始波形发送次数 4 byte
2024-07-09 09:49:42 +08:00
memset(buf, 0, 32);
2021-11-13 15:38:32 +08:00
// sprintf(buf, "%02x%02x", pRecvData->Data[35], pRecvData->Data[36]);
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
2021-09-18 13:45:24 +08:00
// 获取 RSSI
chTemp = pRecvData->Data[38];
memset(buf, 0, 32);
sprintf(buf, "%d", chTemp);
2024-07-09 09:49:42 +08:00
dataNodeInfo.RSSI = atoi(buf); //无线信号强度 1 byte
2021-09-18 13:45:24 +08:00
chTemp = pRecvData->Data[39];
memset(buf, 0, 32);
sprintf(buf, "%02x", chTemp);
dataNodeInfo.ConfigFlag = ((0 == std::string(buf).compare("aa")) ? 1 : 0); //配置标志 1 byte
2021-09-18 13:45:24 +08:00
2024-07-09 09:49:42 +08:00
chTemp = pRecvData->Data[40];
memset(buf, 0, 32);
sprintf(buf, "%d", chTemp);
dataNodeInfo.FeatureInterVal = atoi(buf); //唤醒周期 1 byte
2021-09-18 13:45:24 +08:00
memset(buf, 0, 32);
2024-07-09 09:49:42 +08:00
// yxq
sprintf(buf, "%u%u", pRecvData->Data[41],pRecvData->Data[42]);
dataNodeInfo.WaveInterVal = atoi(buf);//原始波形发送周期 2 byte
2021-09-18 13:45:24 +08:00
memset(buf, 0, 32);
sprintf(buf, "%02x%02x", pRecvData->Data[43], pRecvData->Data[44]); //Zigbee PID 2 byte
2021-09-18 13:45:24 +08:00
dataNodeInfo.ZigbeePanId = std::string(buf);
chTemp = pRecvData->Data[45];
memset(buf, 0, 32);
sprintf(buf, "%d", chTemp);
2024-07-09 09:49:42 +08:00
dataNodeInfo.ZigbeeChannel = atoi(buf);//Zigbee 信道 1 byte
2021-09-18 13:45:24 +08:00
memset(buf, 0, 32);
sprintf(buf, "%02x%02x", pRecvData->Data[46], pRecvData->Data[47]);//Zigbee 本地地址 2 byte
2024-07-09 09:49:42 +08:00
2021-09-18 13:45:24 +08:00
dataNodeInfo.ZigbeeShortAddr = std::string(buf);
2024-07-09 09:49:42 +08:00
unsigned char shortAddr[2] = {0x00};
memcpy(shortAddr,(unsigned char*)&pRecvData->Data[46],2);
modify_DistAddr(shortAddr);//修改目标地址
string strTime = GetLocalTimeWithMs();
//LOG_INFO("DealDataNodeInfo modify_DistAddr Time = %s\n",strTime.c_str());
2021-09-18 13:45:24 +08:00
2024-07-09 09:49:42 +08:00
//LOG_INFO("ZigbeeShortAddr = %s,SoftVersion = %s\n",dataNodeInfo.ZigbeeShortAddr.c_str(),dataNodeInfo.SoftVersion.c_str());
2021-09-18 13:45:24 +08:00
memset(buf, 0, 32);
sprintf(buf, "%02x%02x", pRecvData->Data[48], pRecvData->Data[49]);//Zigbee 目标地址 2 byte
2021-09-18 13:45:24 +08:00
dataNodeInfo.ZigbeeDesAddr = std::string(buf);
2024-07-09 09:49:42 +08:00
//print_info("ZigbeeDesAddr = %s\n",buf);
2021-09-18 13:45:24 +08:00
//50 51 52=》序号23 zigbee重试间隔
memset(buf, 0, 32);
sprintf(buf, "%02x", pRecvData->Data[50]);//Zigbee 发射功率 1 byte
2024-07-09 09:49:42 +08:00
dataNodeInfo.ZigbeePower = atoi(buf);
//print_info("ZigbeePower = %s\n",buf);
memset(buf, 0, 32);
2021-11-13 15:38:32 +08:00
sprintf(buf, "%d", BUILD_UINT16(00,pRecvData->Data[51]));//Zigbee 重试次数 1 byte
2024-07-09 09:49:42 +08:00
dataNodeInfo.ZigbeeRetry = atoi(buf);
//print_info("ZigbeeRetry = %s\n",buf);
memset(buf, 0, 32);
2024-07-09 09:49:42 +08:00
sprintf(buf, "%d", BUILD_UINT16(00,pRecvData->Data[52]));//Zigbee 重试间隔 1 byte
dataNodeInfo.ZigbeeRetryGap = atoi(buf);
//print_info("ZigbeeRetryGap = %s\n",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;
}
}
2021-09-18 13:45:24 +08:00
// 54=》序号25 ACC采样时间
memset(buf, 0, 32);
2024-07-09 09:49:42 +08:00
sprintf(buf, "%u", pRecvData->Data[54]);//ACC 采样时间 1 byte
dataNodeInfo.ACCSampleTime = atoi(buf);
2021-09-18 13:45:24 +08:00
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) {
2024-07-09 09:49:42 +08:00
dataNodeInfo.StartBrands = to_string(iTemp);
2021-09-18 13:45:24 +08:00
} else {
2024-07-09 09:49:42 +08:00
dataNodeInfo.StartBrands += ("," + to_string(iTemp));
2021-09-18 13:45:24 +08:00
}
}
//使用了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) {
2024-07-09 09:49:42 +08:00
dataNodeInfo.StopBrands = to_string(iTemp);
2021-09-18 13:45:24 +08:00
} else {
2024-07-09 09:49:42 +08:00
dataNodeInfo.StopBrands += ("," + to_string(iTemp));
2021-09-18 13:45:24 +08:00
}
}
memset(buf, 0, 32);
sprintf(buf, "%02x%02x", pRecvData->Data[75], pRecvData->Data[76]);
iTemp = (int)strtol(buf, NULL, 16);
2024-07-09 09:49:42 +08:00
dataNodeInfo.EnvelopeBandPass = to_string(iTemp);
2021-09-18 13:45:24 +08:00
memset(buf, 0, 32);
sprintf(buf, "%02x%02x", pRecvData->Data[77], pRecvData->Data[78]);
iTemp = (int)strtol(buf, NULL, 16);
2024-07-09 09:49:42 +08:00
dataNodeInfo.EnvelopeBandPass += ("," + to_string(iTemp));
2021-09-18 13:45:24 +08:00
//使用了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) {
2024-07-09 09:49:42 +08:00
dataNodeInfo.FaultFrequency = to_string(iTemp);
2021-09-18 13:45:24 +08:00
} else {
2024-07-09 09:49:42 +08:00
dataNodeInfo.FaultFrequency += ("," + to_string(iTemp));
2021-09-18 13:45:24 +08:00
}
}
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);
2024-07-09 09:49:42 +08:00
dataNodeInfo.ConfigDate = to_string(lTimeStamp);
2021-09-18 13:45:24 +08:00
chTemp = pRecvData->Data[91];
memset(buf, 0, 32);
sprintf(buf, "%d", chTemp);
2024-07-09 09:49:42 +08:00
dataNodeInfo.VIntegralFilterFrequency = atoi(buf);
2021-09-18 13:45:24 +08:00
char whereCon[64] = {0};
sprintf(whereCon, "dataNodeNo='%s'", dataNodeInfo.ZigbeeLongAddr.c_str());
if (sql_ctl->GetTableRows(T_SENSOR_INFO(TNAME), whereCon) > 0) {
2024-07-09 09:49:42 +08:00
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',\
2024-08-05 10:25:50 +08:00
ZigbeePower = '%d',ZigbeeRetry = '%d',ZigbeeRetryGap = '%d',ACCSampleTime = '%d',status = '%s',timeStamp = '%s',viff = '%d',RSSI = '0,%d',UpdateFlag = 1",
2024-07-09 09:49:42 +08:00
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());
sql_ctl->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',\
2024-08-05 10:25:50 +08:00
'%d','%d','%d','%d','%s','%s', '%d', '0,%d','1','0,0','',''",
2024-07-09 09:49:42 +08:00
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);
sql_ctl->InsertData(T_SENSOR_INFO(TNAME), insertSql);
}
char szTableName[50]={0x00};
sprintf(szTableName,"t_data_%s",dataNodeInfo.ZigbeeLongAddr.c_str());
sql_ctl->Createtable(szTableName);
memset(szTableName,0x00,sizeof(szTableName));
sprintf(szTableName,"t_dataStatic_%s",dataNodeInfo.ZigbeeLongAddr.c_str());
sql_ctl->CreatedataStatictable(szTableName);
2021-09-18 13:45:24 +08:00
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;
2024-07-09 09:49:42 +08:00
std::string strCmd26 = showValue.write(jsonVal);
2021-09-18 13:45:24 +08:00
//传感器发来的数据包中的表示设备信息的数据转化为json格式后通过调用data_publish将数据传给mqttclient Topicwireless/cmd/60294D203717
2024-07-09 09:49:42 +08:00
data_publish(strCmd26.c_str(), GlobalConfig::Topic_G.mPubCmd.c_str());
2021-09-18 13:45:24 +08:00
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);
2024-07-09 09:49:42 +08:00
2021-09-18 13:45:24 +08:00
}
2024-08-05 10:25:50 +08:00
void Uart::RecordBattery(string & strLongAddr,DataRecvStatic& dataStatic,string& nowTimetamp)
{
char insertSql[1024] = { 0 };
sprintf(insertSql, "'%s','%d','%f','%f','%f','%d','','','%s'",
strLongAddr.c_str(), dataStatic.Dip, dataStatic.TemBot,dataStatic.nodeWorkTime,dataStatic.nodeSendTime, dataStatic.Voltage,nowTimetamp.c_str());
sql_ctl->InsertData(T_BATTERY_INFO(TNAME), insertSql);
}
2021-09-18 13:45:24 +08:00
void Uart::DealDataNodeFeature(const char *pData, int flag)
{
print_info("recv feature\n");
RecvData * pRecvData = (RecvData *)pData;
2024-07-09 09:49:42 +08:00
char whereCon[1024] = {0};
char updateSql[1024] = { 0 };
char buf[20] = {0x00};
int nodeResend = 0;
sprintf(buf, "%02x%02x", pRecvData->ShortAddr[0], pRecvData->ShortAddr[1]);
2024-07-09 09:49:42 +08:00
//LOG_INFO("DealDataNodeFeature %02x%02x\n",pRecvData->ShortAddr[0], pRecvData->ShortAddr[1]);
std::string strShortAddr = std::string(buf);
print_info("zigbeeShortAddr='%s'\n", strShortAddr.c_str());
2024-07-09 09:49:42 +08:00
char getLongAddr_sql[32] = { 0 };
//根据数据包中的传感器的短地址获取数据库中长地址MAC在下面判断该传感器是否存在如果不存在则把数据包丢弃
sprintf(getLongAddr_sql, "zigbeeShortAddr='%s'", strShortAddr.c_str());
//std::string strLongAddr = sql_ctl->GetData(T_SENSOR_INFO(TNAME), T_SENSOR_INFO(ZIGBEELONGADDR), getLongAddr_sql);
2024-08-05 10:25:50 +08:00
vec_t vecResult = sql_ctl->GetDataSingleLine(T_SENSOR_INFO(TNAME)," softVersion,dataNodeNo,MeasurementID ",getLongAddr_sql);
2024-07-09 09:49:42 +08:00
if (vecResult.size() < 1) {
LOG_ERROR("device info not found %02x%02x\n",pRecvData->ShortAddr[0], pRecvData->ShortAddr[1]);
print_error("device info not found\n");
return;
}
print_info("--------->the remote sensor short addr:%s strLongAddr=%s,softVersion = %s\n",buf,vecResult[1].c_str(),vecResult[0].c_str());
std::string strLongAddr = vecResult[1];
2024-08-05 10:25:50 +08:00
std::string strMeasurementID = vecResult[2];
2021-09-18 13:45:24 +08:00
if (1 == flag) {
2024-07-09 09:49:42 +08:00
tcflush(fd,TCIOFLUSH);
if (!bSendTimeStamp)
{
bSendTimeStamp = true;
modify_distaddr_info(0x9999,"",pRecvData->ShortAddr);//临时参数配置
usleep(10000);
UpdateWirelessNodeTime((unsigned char*)pRecvData->ShortAddr,0);
}else
{
return;
}
//modify_distaddr_info(0x9999,"",pRecvData->ShortAddr);//临时参数配置
GlobalConfig::ZigbeeInfo_G.MyAddr = "9999";
GlobalConfig::Zigbee_G.MyAddr = 0x9999;
string strTime = GetLocalTimeWithMs();
m_strDestShortAddr = std::string(buf);
bool isUpdate = ReadUpdatePackge(pRecvData->ShortAddr);
bUpdatePre = isUpdate;
if(!isUpdate){
int iRet = UpdateConfig(pRecvData->ShortAddr);
}
2024-07-09 09:49:42 +08:00
if(isUpdate || bUpdateconfig){
}else{
LOG_DEBUG("DealDataNodeFeature %02x%02x,localaddr %02x%02x \n",pRecvData->ShortAddr[0], pRecvData->ShortAddr[1],\
UINT16_HIGH(GlobalConfig::Zigbee_G.MyAddr),UINT16_LOW(GlobalConfig::Zigbee_G.MyAddr));
// 进入传输原始数据状态,启动计数 60秒
GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = ENTER_TRANSMITTING_STATUS;
GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0;
}
}else{
memset(whereCon,0x00,sizeof(whereCon));
memset(updateSql,0x00,sizeof(updateSql));
sprintf(whereCon, "zigbeeShortAddr='%s'", strShortAddr.c_str());
sprintf(updateSql, " StaticTime = StaticTime + 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());
2021-09-18 13:45:24 +08:00
}
2024-07-09 09:49:42 +08:00
if (vecResult[0] == "3.0" || vecResult[0] == "4.0"){
return;
}
long staticIndex = BUILD_UINT32(pRecvData->Data[29], pRecvData->Data[28],pRecvData->Data[27], pRecvData->Data[26]);
2024-07-09 09:49:42 +08:00
print_info("staticIndex = %d\n",staticIndex);
//LOG_INFO("staticIndex = %d\n",staticIndex);
2021-09-18 13:45:24 +08:00
char localtimestamp[32] = { 0 };
GetTimeNet(localtimestamp, 1);
2024-07-09 09:49:42 +08:00
std::string nowTimetamp = std::string(localtimestamp);
strTimetamp = nowTimetamp;
2021-09-18 13:45:24 +08:00
int iTemp = 0;
unsigned char highbit = 0;
unsigned int lowbit = 0;
float n = 0;
DataRecvStatic dataStatic;
2024-07-09 09:49:42 +08:00
memset(buf, 0, sizeof(buf));
2021-09-18 13:45:24 +08:00
sprintf(buf, "%02x%02x", pRecvData->Data[5], pRecvData->Data[4]);
print_blue("!!!!!!!!!!!!!!!!!!!!!!%s\n",buf);
iTemp = (int)strtol(buf, NULL, 16);
dataStatic.Dip = iTemp;
2024-07-09 09:49:42 +08:00
int fTemp = 0;
memset(buf, 0, sizeof(buf));
2021-09-18 13:45:24 +08:00
sprintf(buf, "%02x%02x", pRecvData->Data[1], pRecvData->Data[0]);
print_blue("@@@@@@@@@@@@@@@@@%s\n",buf);
iTemp = (int)strtol(buf, NULL, 16);
2024-07-09 09:49:42 +08:00
if (iTemp < 0x8000) {
fTemp = iTemp ;
} else {
fTemp = (((~iTemp)&0xffff) + 1)* (-1);
}
dataStatic.TemBot = fTemp * 0.0625;//设备温度
fTemp = 0;
memset(buf, 0, sizeof(buf));
2021-09-18 13:45:24 +08:00
sprintf(buf, "%02x%02x", pRecvData->Data[3], pRecvData->Data[2]);
iTemp = (int)strtol(buf, NULL, 16);
2024-07-09 09:49:42 +08:00
if (iTemp < 0x8000) {
fTemp = iTemp ;
} else {
fTemp = (((~iTemp)&0xffff) + 1) * (-1);
}
dataStatic.TemTop = fTemp * 0.0625;//环境温度
2021-09-18 13:45:24 +08:00
2024-07-09 09:49:42 +08:00
memset(buf, 0, sizeof(buf));
2021-09-18 13:45:24 +08:00
sprintf(buf, "%02x%02x", pRecvData->Data[7], pRecvData->Data[6]);
iTemp = (int)strtol(buf, NULL, 16);
dataStatic.Voltage = iTemp;
2024-08-05 10:25:50 +08:00
memset(buf, 0, sizeof(buf));
sprintf(buf, "%02x%02x", pRecvData->Data[31], pRecvData->Data[30]);
iTemp = (unsigned int)strtol(buf, NULL, 16);
highbit = iTemp >> 14 & 0x3;
lowbit = iTemp & 0x3fff;
switch (highbit)
{
case 0:
n = 0.0001;
break;
case 1:
n = 0.01;
break;
case 2:
n = 1;
break;
case 3:
n = 100;
break;
}
dataStatic.nodeWorkTime = lowbit * n;
print_info("workTime = %f\n",dataStatic.nodeWorkTime);
memset(buf, 0, sizeof(buf));
sprintf(buf, "%02x%02x", pRecvData->Data[33], pRecvData->Data[32]);
iTemp = (unsigned int)strtol(buf, NULL, 16);
highbit = iTemp >> 14 & 0x3;
lowbit = iTemp & 0x3fff;
switch (highbit)
{
case 0:
n = 0.0001;
break;
case 1:
n = 0.01;
break;
case 2:
n = 1;
break;
case 3:
n = 100;
break;
}
dataStatic.nodeSendTime = lowbit * n;
print_info("SendTime = %f\n",dataStatic.nodeSendTime );
dataStatic.nodeWorkTime = dataStatic.nodeWorkTime - dataStatic.nodeSendTime;
RecordBattery(strLongAddr,dataStatic,nowTimetamp);
2021-09-18 13:45:24 +08:00
2024-07-09 09:49:42 +08:00
char szTableName[50]={0x00},szTableNameStatic[50]={0x00},szTableNameData[50]={0x00};
sprintf(szTableName,"t_dataStatic_%s",strLongAddr.c_str());
memcpy(szTableNameStatic,szTableName,sizeof(szTableNameStatic));
memset(whereCon,0x00,sizeof(whereCon));
sprintf(whereCon,"StaticIndex = %d",staticIndex);
int count = sql_ctl->GetTableRows(szTableNameStatic, whereCon);//避免重复数据
sprintf(szTableNameData,"t_data_%s",strLongAddr.c_str());
int count2 = sql_ctl->GetTableRows(szTableNameData, whereCon);
if(count > 0 || count2 > 0){
char logInfo[20]={0x00};
sprintf(logInfo,"ShortAddr = %s,staticIndex = %d,staticData = %d, data = %d",strShortAddr.c_str(),staticIndex,count,count2);
LOG_DEBUG(logInfo);
return;
}
memset(whereCon,0x00,sizeof(whereCon));
///////////////////////////////////////////////////////////// for V2.0.3 upgrade to V3.0
std::string strTmp = "";
char sztmp[100]={0x00};
strTmp = "name = '" + string(szTableNameStatic)+ "' and sql LIKE '%nodeResend%' ";
int row = sql_ctl->GetTableRows(" sqlite_master ",strTmp.c_str());
print_info("row1 = %d\n",row);
if(row == 0){
memset(sztmp,0x00,sizeof(sztmp));
sprintf(sztmp,"ALTER TABLE %s ADD COLUMN 'nodeResend'",szTableNameStatic);
sql_ctl->CreateTable(sztmp);
}
// strTmp = "name = '" + string(szTableNameStatic)+ "' and sql LIKE '%zigbeeRSSIType%' ";
// row = sql_ctl->GetTableRows(" sqlite_master ",strTmp.c_str());
// print_info("row2 = %d\n",row);
// if(row == 0){
// memset(sztmp,0x00,sizeof(sztmp));
// sprintf(sztmp,"ALTER TABLE %s ADD COLUMN 'zigbeeRSSIType'",szTableNameStatic);
// sql_ctl->CreateTable(sztmp);
// }
strTmp = "name = '" + string(szTableNameData)+ "' and sql LIKE '%nodeResend%' ";
row = sql_ctl->GetTableRows(" sqlite_master ",strTmp.c_str());
print_info("row2 = %d\n",row);
if(row == 0){
memset(sztmp,0x00,sizeof(sztmp));
sprintf(sztmp,"ALTER TABLE %s ADD COLUMN 'nodeResend'",szTableNameData);
sql_ctl->CreateTable(sztmp);
}
2024-08-05 10:25:50 +08:00
////////////////////////////////////////////////////////////更换电池判断
sprintf(whereCon," dataNodeNo = '%s' and StaticIndex > 0 order by StaticIndex desc LIMIT 0 , 1 ",strLongAddr.c_str());
std::string strStaticIndex = sql_ctl->GetData(szTableNameStatic, "StaticIndex", whereCon);
if(atol(strStaticIndex.c_str()) - staticIndex > 100){
sql_ctl->Deletetable(szTableNameStatic);
sql_ctl->Deletetable(szTableNameData);
LOG_INFO("staticIndexNOW = %d,strStaticIndexLast = %s\n",staticIndex,strStaticIndex.c_str());
}
print_info("NowstaticIndex = %d,RecordStaticIndex = %d",staticIndex,atol(strStaticIndex.c_str()));
2024-07-09 09:49:42 +08:00
if(staticIndex != atol(strStaticIndex.c_str() + 1) &&
strStaticIndex != "" &&
staticIndex < atol(strStaticIndex.c_str()))
{
sprintf(whereCon,"StaticIndex = %d order by StaticIndex desc LIMIT 0 , 1",atol(strStaticIndex.c_str()));
vec_t vecResult = sql_ctl->GetDataSingleLine(szTableNameStatic, "timeStamp,StaticIndex", whereCon);
2024-08-05 10:25:50 +08:00
if(vecResult.size() > 0){
memset(whereCon,0x00,sizeof(whereCon));
sprintf(whereCon,"dataNodeNo = '%s'",strLongAddr.c_str());
string staticInterval = sql_ctl->GetData(T_SENSOR_INFO(TNAME), "featureInterval", whereCon);
long nNowTimetamp = atol(vecResult[0].c_str()) - (atol(staticInterval.c_str()) * \
(atol(vecResult[1].c_str()) - staticIndex)) * 60;
char tmp[10]={0x00};
sprintf(tmp,"%ld",nNowTimetamp);
nowTimetamp = string(tmp);
nodeResend = 1;
}
2024-07-09 09:49:42 +08:00
}
print_info("nowTimetamp = %s",nowTimetamp.c_str());
// save dataStatic of 7 days
char selectCon[128] = { 0 };
2024-08-05 10:25:50 +08:00
sprintf(selectCon, "channelID='%s' and sendMsg = '1' ORDER BY timeStamp ASC LIMIT 0,1",(strMeasurementID + "-S").c_str());
2024-07-09 09:49:42 +08:00
std::string strTime = sql_ctl->GetData(szTableName, "timeStamp", selectCon);
int Count = sql_ctl->GetTableRows(szTableName, NULL);
if(Count == -1){
sql_ctl->CreatedataStatictable(szTableName);
}
print_info("strLongAddr = %s,strTime = %s\n",strLongAddr.c_str(),strTime.c_str());
long lTime = atol(nowTimetamp.c_str())-atol(strTime.c_str());
print_info("lTime = %d,OneWeek = %d\n",lTime,OneWeek);
2021-09-18 13:45:24 +08:00
print_info("dataStatic.TemTop : %f dataStatic.TemBot : %f dataStatic.Dip :%d dataStatic.Voltage : %d\n", dataStatic.TemTop\
, dataStatic.TemBot, dataStatic.Dip, dataStatic.Voltage);
2024-07-09 09:49:42 +08:00
2024-08-05 10:25:50 +08:00
sprintf(updateSql, "temTop='%f',temBot='%f',dip='%d',voltage='%d',timeStamp='%s',StaticIndex = %d, nodeResend = %d,zigbeeSignal = '',zigbeeSignalNode = '' ",\
2024-07-09 09:49:42 +08:00
dataStatic.TemTop, dataStatic.TemBot, dataStatic.Dip, dataStatic.Voltage, nowTimetamp.c_str(),staticIndex,nodeResend);
2024-08-05 10:25:50 +08:00
sprintf(whereCon, "channelID='%s' and sendMsg = '1'", (strMeasurementID + "-S").c_str());
2024-07-09 09:49:42 +08:00
if ( /*0 == sql_ctl->GetTableRows(T_DATASTATIC_INFO(TNAME), whereCon)*/ (Count * 3 < SAVE_COUNT && lTime < OneWeek ) || strTime.size() == 0 ) {
2021-09-18 13:45:24 +08:00
print_info("insert static data to sql\n");
char insertSql[1024] = { 0 };
sprintf(insertSql, "'%s','%s','%f','%f','%d','%d','',%d,'%s','1',%d,''",
2024-08-05 10:25:50 +08:00
strLongAddr.c_str(), (strMeasurementID + "-S").c_str(), dataStatic.TemTop, dataStatic.TemBot, dataStatic.Dip, dataStatic.Voltage, staticIndex,nowTimetamp.c_str(),nodeResend);
2024-07-09 09:49:42 +08:00
sql_ctl->InsertData(szTableName, insertSql);
if(0 == sql_ctl->GetTableRows(T_DATASTATIC_INFO(TNAME), whereCon)){ // First Connect
char insertSql[1024] = { 0 };
sprintf(insertSql, "'%s','%s','%f','%f','%d','%d','',%d,'%s','1',%d",
2024-08-05 10:25:50 +08:00
strLongAddr.c_str(), (strMeasurementID + "-S").c_str(), dataStatic.TemTop, dataStatic.TemBot, dataStatic.Dip, dataStatic.Voltage, staticIndex,nowTimetamp.c_str(),nodeResend);
2024-07-09 09:49:42 +08:00
sql_ctl->InsertData(T_DATASTATIC_INFO(TNAME), insertSql);
sql_ctl->CalculateBattery();
}
2024-08-05 10:25:50 +08:00
else{
memset(updateSql,0x00,sizeof(updateSql));
sprintf(updateSql, "temTop='%f',temBot='%f',dip='%d',voltage='%d',timeStamp='%s',StaticIndex = %d ",\
dataStatic.TemTop, dataStatic.TemBot, dataStatic.Dip, dataStatic.Voltage, nowTimetamp.c_str(),staticIndex,nodeResend);
2024-07-09 09:49:42 +08:00
sql_ctl->UpdateTableData(T_DATASTATIC_INFO(TNAME), updateSql, whereCon);
2024-08-05 10:25:50 +08:00
}
2024-07-09 09:49:42 +08:00
} else {
memset(whereCon,0x00,sizeof(whereCon));
2024-08-05 10:25:50 +08:00
sprintf(whereCon, "channelID='%s' and sendMsg = '1' and timeStamp = '%s'", (strMeasurementID + "-S").c_str(),strTime.c_str());
2024-07-09 09:49:42 +08:00
print_info("update static data to sql\n");
sql_ctl->UpdateTableData(szTableName, updateSql, whereCon);
memset(whereCon,0x00,sizeof(whereCon));
2024-08-05 10:25:50 +08:00
sprintf(whereCon, "channelID='%s' ", (strMeasurementID + "-S").c_str());
memset(updateSql,0x00,sizeof(updateSql));
sprintf(updateSql, "temTop='%f',temBot='%f',dip='%d',voltage='%d',timeStamp='%s',StaticIndex = %d ",\
dataStatic.TemTop, dataStatic.TemBot, dataStatic.Dip, dataStatic.Voltage, nowTimetamp.c_str(),staticIndex,nodeResend);
2021-09-18 13:45:24 +08:00
sql_ctl->UpdateTableData(T_DATASTATIC_INFO(TNAME), updateSql, whereCon);
2024-07-09 09:49:42 +08:00
}
memset(szTableName,0x00,sizeof(szTableName));
sprintf(szTableName,"t_data_%s",strLongAddr.c_str());
if(Count == -1){
sql_ctl->Createtable(szTableName);
}
2021-09-18 13:45:24 +08:00
DataRecvDym dataDymX;
2024-07-09 09:49:42 +08:00
memset(buf, 0, sizeof(buf));
2021-09-18 13:45:24 +08:00
sprintf(buf, "%02x%02x", pRecvData->Data[9], pRecvData->Data[8]);
iTemp = (unsigned int)strtol(buf, NULL, 16);
highbit = iTemp >> 14 & 0x3;
lowbit = iTemp & 0x3fff;
switch (highbit)
{
case 0:
n = 0.0001;
break;
case 1:
n = 0.01;
break;
case 2:
n = 1;
break;
case 3:
n = 100;
break;
}
dataDymX.DiagnosisPk = lowbit * n;
2024-07-09 09:49:42 +08:00
memset(buf, 0, sizeof(buf));
2021-09-18 13:45:24 +08:00
sprintf(buf, "%02x%02x", pRecvData->Data[11], pRecvData->Data[10]);
iTemp = (unsigned int)strtol(buf, NULL, 16);
highbit = iTemp >> 14 & 0x3;
lowbit = iTemp & 0x3fff;
switch (highbit)
{
case 0:
n = 0.0001;
break;
case 1:
n = 0.01;
break;
case 2:
n = 1;
break;
case 3:
n = 100;
break;
}
dataDymX.RmsValues = lowbit * n;
2024-07-09 09:49:42 +08:00
memset(buf, 0, sizeof(buf));
2021-09-18 13:45:24 +08:00
sprintf(buf, "%02x%02x", pRecvData->Data[13], pRecvData->Data[12]);
iTemp = (unsigned int)strtol(buf, NULL, 16);
highbit = iTemp >> 14 & 0x3;
lowbit = iTemp & 0x3fff;
switch (highbit)
{
case 0:
n = 0.0001;
break;
case 1:
n = 0.01;
break;
case 2:
n = 1;
break;
case 3:
n = 100;
break;
}
dataDymX.IntegratPk = lowbit * n;
2024-07-09 09:49:42 +08:00
memset(buf, 0, sizeof(buf));
2021-09-18 13:45:24 +08:00
sprintf(buf, "%02x%02x", pRecvData->Data[15], pRecvData->Data[14]);
iTemp = (unsigned int)strtol(buf, NULL, 16);
highbit = iTemp >> 14 & 0x3;
lowbit = iTemp & 0x3fff;
switch (highbit)
{
case 0:
n = 0.0001;
break;
case 1:
n = 0.01;
break;
case 2:
n = 1;
break;
case 3:
n = 100;
break;
}
dataDymX.IntegratRMS = lowbit * n;
2024-07-09 09:49:42 +08:00
memset(buf, 0, sizeof(buf));
2021-09-18 13:45:24 +08:00
sprintf(buf, "%02x%02x", pRecvData->Data[17], pRecvData->Data[16]);
iTemp = (unsigned int)strtol(buf, NULL, 16);
highbit = iTemp >> 14 & 0x3;
lowbit = iTemp & 0x3fff;
switch (highbit)
{
case 0:
n = 0.0001;
break;
case 1:
n = 0.01;
break;
case 2:
n = 1;
break;
case 3:
n = 100;
break;
}
dataDymX.Amp1 = lowbit * n;
2024-07-09 09:49:42 +08:00
memset(buf, 0, sizeof(buf));
2021-09-18 13:45:24 +08:00
sprintf(buf, "%02x%02x", pRecvData->Data[19], pRecvData->Data[18]);
iTemp = (unsigned int)strtol(buf, NULL, 16);
highbit = iTemp >> 14 & 0x3;
lowbit = iTemp & 0x3fff;
switch (highbit)
{
case 0:
n = 0.0001;
break;
case 1:
n = 0.01;
break;
case 2:
n = 1;
break;
case 3:
n = 100;
break;
}
dataDymX.Amp2 = lowbit * n;
2024-07-09 09:49:42 +08:00
memset(buf, 0, sizeof(buf));
sprintf(buf, "%02x%02x", pRecvData->Data[21], pRecvData->Data[20]);
2021-09-18 13:45:24 +08:00
iTemp = (unsigned int)strtol(buf, NULL, 16);
highbit = iTemp >> 14 & 0x3;
lowbit = iTemp & 0x3fff;
switch (highbit)
{
case 0:
n = 0.0001;
break;
case 1:
n = 0.01;
break;
case 2:
n = 1;
break;
case 3:
n = 100;
break;
2024-07-09 09:49:42 +08:00
}
dataDymX.Amp3 = lowbit * n;
2021-09-18 13:45:24 +08:00
2024-07-09 09:49:42 +08:00
memset(buf, 0, sizeof(buf));
sprintf(buf, "%02x%02x", pRecvData->Data[23], pRecvData->Data[22]);
2021-09-18 13:45:24 +08:00
iTemp = (unsigned int)strtol(buf, NULL, 16);
highbit = iTemp >> 14 & 0x3;
lowbit = iTemp & 0x3fff;
switch (highbit)
{
case 0:
n = 0.0001;
break;
case 1:
n = 0.01;
break;
case 2:
n = 1;
break;
case 3:
n = 100;
break;
2024-07-09 09:49:42 +08:00
}
dataDymX.Amp4 = lowbit * n;
2021-09-18 13:45:24 +08:00
2024-07-09 09:49:42 +08:00
memset(buf, 0, sizeof(buf));
sprintf(buf, "%02x%02x", pRecvData->Data[25], pRecvData->Data[24]);
2021-09-18 13:45:24 +08:00
iTemp = (unsigned int)strtol(buf, NULL, 16);
highbit = iTemp >> 14 & 0x3;
lowbit = iTemp & 0x3fff;
switch (highbit)
{
case 0:
n = 0.0001;
break;
case 1:
n = 0.01;
break;
case 2:
n = 1;
break;
case 3:
n = 100;
break;
2024-07-09 09:49:42 +08:00
}
dataDymX.Amp5 = lowbit * n;
memset(buf, 0, sizeof(buf));
// sprintf(buf, "%02x%02x", pRecvData->Data[27], pRecvData->Data[26]);
// iTemp = (unsigned int)strtol(buf, NULL, 16);
// highbit = iTemp >> 14 & 0x3;
// lowbit = iTemp & 0x3fff;
// switch (highbit)
// {
// case 0:
// n = 0.0001;
// break;
// case 1:
// n = 0.01;
// break;
// case 2:
// n = 1;
// break;
// case 3:
// n = 100;
// break;
// }
// dataDymX.EnvelopEnergy = lowbit * n;
dataDymX.EnvelopEnergy = 0;
memset(buf, 0, sizeof(buf));
// sprintf(buf, "%02x%02x", pRecvData->Data[29], pRecvData->Data[28]);
// iTemp = (unsigned int)strtol(buf, NULL, 16);
// highbit = iTemp >> 14 & 0x3;
// lowbit = iTemp & 0x3fff;
// switch (highbit)
// {
// case 0:
// n = 0.0001;
// break;
// case 1:
// n = 0.01;
// break;
// case 2:
// n = 1;
// break;
// case 3:
// n = 100;
// break;
// }
// dataDymX.Phase1 = lowbit * n;
dataDymX.Phase1 = 0;
memset(buf, 0, sizeof(buf));
// sprintf(buf, "%02x%02x", pRecvData->Data[31], pRecvData->Data[30]);
// iTemp = (unsigned int)strtol(buf, NULL, 16);
// highbit = iTemp >> 14 & 0x3;
// lowbit = iTemp & 0x3fff;
// switch (highbit)
// {
// case 0:
// n = 0.0001;
// break;
// case 1:
// n = 0.01;
// break;
// case 2:
// n = 1;
// break;
// case 3:
// n = 100;
// break;
// }
dataDymX.Phase2 = 0;
memset(buf, 0, sizeof(buf));
2021-09-18 13:45:24 +08:00
memset(buf, 0, 8);
2024-07-09 09:49:42 +08:00
// sprintf(buf, "%02x%02x", pRecvData->Data[33], pRecvData->Data[32]);
// iTemp = (unsigned int)strtol(buf, NULL, 16);
// highbit = iTemp >> 14 & 0x3;
// lowbit = iTemp & 0x3fff;
// switch (highbit)
// {
// case 0:
// n = 0.0001;
// break;
// case 1:
// n = 0.01;
// break;
// case 2:
// n = 1;
// break;
// case 3:
// n = 100;
// break;
// }
dataDymX.Phase3 = 0;
memset(buf, 0, sizeof(buf));
sprintf(buf, "%02x%02x", pRecvData->Data[35], pRecvData->Data[34]);
// iTemp = (unsigned int)strtol(buf, NULL, 16);
// highbit = iTemp >> 14 & 0x3;
// lowbit = iTemp & 0x3fff;
// switch (highbit)
// {
// case 0:
// n = 0.0001;
// break;
// case 1:
// n = 0.01;
// break;
// case 2:
// n = 1;
// break;
// case 3:
// n = 100;
// break;
// }
dataDymX.Phase4 = 0;
memset(whereCon, 0, 1024);
2024-08-05 10:25:50 +08:00
sprintf(whereCon, "channelID='%s' and sendMsg = '1'", (strMeasurementID + "-X").c_str());
2024-07-09 09:49:42 +08:00
memset(updateSql, 0, 1024);
sprintf(updateSql, "diagnosisPk='%f',integratPk='%f',integratRMS='%f',rmsValues='%f',envelopEnergy='%f',\
Amp1='%f',Amp2='%f',Amp3='%f',Amp4='%f',Amp5='%f',Phase1='%f',Phase2='%f',Phase3='%f',Phase4='%f',timeStamp='%s',StaticIndex = %d,nodeResend = %d ",\
dataDymX.DiagnosisPk, dataDymX.IntegratPk, dataDymX.IntegratRMS, dataDymX.RmsValues, dataDymX.EnvelopEnergy,\
dataDymX.Amp1, dataDymX.Amp2, dataDymX.Amp3, dataDymX.Amp4, dataDymX.Amp5,dataDymX.Phase1, dataDymX.Phase2, dataDymX.Phase3, dataDymX.Phase4, nowTimetamp.c_str(),staticIndex,nodeResend);
if ( /*0 == sql_ctl->GetTableRows(T_DATA_INFO(TNAME), whereCon)*/(Count * 3 < SAVE_COUNT && lTime < OneWeek ) || strTime.size() == 0) {//1 week
char insertSql[1024] = { 0 };
memset(insertSql,0x00,sizeof(insertSql));
sprintf(insertSql, "'%s','%s','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f',%d,'%s','1',%d",
2024-08-05 10:25:50 +08:00
strLongAddr.c_str(), (strMeasurementID + "-X").c_str(), dataDymX.DiagnosisPk, dataDymX.IntegratPk, dataDymX.IntegratRMS, dataDymX.RmsValues, dataDymX.EnvelopEnergy,\
2024-07-09 09:49:42 +08:00
dataDymX.Amp1, dataDymX.Amp2, dataDymX.Amp3, dataDymX.Amp4, dataDymX.Amp5,dataDymX.Phase1, dataDymX.Phase2, dataDymX.Phase3, dataDymX.Phase4, staticIndex,nowTimetamp.c_str(),nodeResend);
sql_ctl->InsertData(szTableName, insertSql);
if(0 == sql_ctl->GetTableRows(T_DATA_INFO(TNAME), whereCon))
sql_ctl->InsertData(T_DATA_INFO(TNAME), insertSql);
else
sql_ctl->UpdateTableData(T_DATA_INFO(TNAME), updateSql, whereCon);
} else {
memset(whereCon,0x00,sizeof(whereCon));
2024-08-05 10:25:50 +08:00
sprintf(whereCon, "channelID='%s' and sendMsg = '1' and timeStamp = '%s'", (strMeasurementID + "-X").c_str(),strTime.c_str());
2024-07-09 09:49:42 +08:00
// sprintf(whereCon, "channelID='%s' and sendMsg = '1' ", (strLongAddr + "-X").c_str());
sql_ctl->UpdateTableData(szTableName, updateSql, whereCon);
memset(whereCon,0x00,sizeof(whereCon));
2024-08-05 10:25:50 +08:00
sprintf(whereCon, "channelID='%s' ", (strMeasurementID + "-X").c_str());
2024-07-09 09:49:42 +08:00
sql_ctl->UpdateTableData(T_DATA_INFO(TNAME), updateSql, whereCon);
}
print_info("x:%s,%s,diagnosisPk=%f,integratPk=%f,integratRMS=%f,rmsValues=%f,envelopEnergy=%f,Amp1=%f,Amp2=%f,Amp3=%f,Amp4=%f,Amp5=%f,Phase1=%f,Phase2=%f,Phase3=%f,Phase4=%f,timeStamp=%s\n",\
2024-08-05 10:25:50 +08:00
strLongAddr.c_str(), (strMeasurementID + "-X").c_str(), dataDymX.DiagnosisPk, dataDymX.IntegratPk, dataDymX.IntegratRMS, dataDymX.RmsValues, dataDymX.EnvelopEnergy,\
2024-07-09 09:49:42 +08:00
dataDymX.Amp1, dataDymX.Amp2, dataDymX.Amp3, dataDymX.Amp4, dataDymX.Amp5,dataDymX.Phase1, dataDymX.Phase2, dataDymX.Phase3, dataDymX.Phase4, nowTimetamp.c_str());
Json::Value valNodeData;
Json::Value valNodeFeature;
2024-08-05 10:25:50 +08:00
valNodeFeature["dataNodeNo"] = strMeasurementID;
valNodeFeature["ChannelId"] = strMeasurementID + "-X";
2024-07-09 09:49:42 +08:00
valNodeFeature["diagnosisPk"] = dataDymX.DiagnosisPk;
valNodeFeature["integratPk"] = dataDymX.IntegratPk;
valNodeFeature["integratRMS"] = dataDymX.IntegratRMS;
valNodeFeature["rmsValues"] = dataDymX.RmsValues;
valNodeFeature["envelopEnergy"] = dataDymX.EnvelopEnergy;
valNodeFeature["Amp1"] = dataDymX.Amp1;
valNodeFeature["Amp2"] = dataDymX.Amp2;
valNodeFeature["Amp3"] = dataDymX.Amp3;
valNodeFeature["Amp4"] = dataDymX.Amp4;
valNodeFeature["Amp5"] = dataDymX.Amp5;
valNodeFeature["Phase1"] = dataDymX.Phase1;
valNodeFeature["Phase2"] = dataDymX.Phase2;
valNodeFeature["Phase3"] = dataDymX.Phase3;
valNodeFeature["Phase4"] = dataDymX.Phase4;
valNodeFeature["timeStamp"] = nowTimetamp;
valNodeData.append(valNodeFeature);
DataRecvDym dataDymY;
memset(buf, 0, sizeof(buf));
sprintf(buf, "%02x%02x", pRecvData->Data[37], pRecvData->Data[36]);
2021-09-18 13:45:24 +08:00
iTemp = (unsigned int)strtol(buf, NULL, 16);
highbit = iTemp >> 14 & 0x3;
lowbit = iTemp & 0x3fff;
switch (highbit)
{
case 0:
n = 0.0001;
break;
case 1:
n = 0.01;
break;
case 2:
n = 1;
break;
case 3:
n = 100;
break;
2024-07-09 09:49:42 +08:00
}
dataDymY.DiagnosisPk = lowbit * n;
2021-09-18 13:45:24 +08:00
2024-07-09 09:49:42 +08:00
memset(buf, 0, sizeof(buf));
sprintf(buf, "%02x%02x", pRecvData->Data[39], pRecvData->Data[38]);
2021-09-18 13:45:24 +08:00
iTemp = (unsigned int)strtol(buf, NULL, 16);
highbit = iTemp >> 14 & 0x3;
lowbit = iTemp & 0x3fff;
switch (highbit)
{
case 0:
n = 0.0001;
break;
case 1:
n = 0.01;
break;
case 2:
n = 1;
break;
case 3:
n = 100;
break;
2024-07-09 09:49:42 +08:00
}
dataDymY.RmsValues = lowbit * n;
2021-09-18 13:45:24 +08:00
2024-07-09 09:49:42 +08:00
memset(buf, 0, sizeof(buf));
sprintf(buf, "%02x%02x", pRecvData->Data[41], pRecvData->Data[40]);
2021-09-18 13:45:24 +08:00
iTemp = (unsigned int)strtol(buf, NULL, 16);
highbit = iTemp >> 14 & 0x3;
lowbit = iTemp & 0x3fff;
switch (highbit)
{
case 0:
n = 0.0001;
break;
case 1:
n = 0.01;
break;
case 2:
n = 1;
break;
case 3:
n = 100;
break;
2024-07-09 09:49:42 +08:00
}
dataDymY.IntegratPk = lowbit * n;
2021-09-18 13:45:24 +08:00
2024-07-09 09:49:42 +08:00
memset(buf, 0, sizeof(buf));
sprintf(buf, "%02x%02x", pRecvData->Data[43], pRecvData->Data[42]);
2021-09-18 13:45:24 +08:00
iTemp = (unsigned int)strtol(buf, NULL, 16);
highbit = iTemp >> 14 & 0x3;
lowbit = iTemp & 0x3fff;
switch (highbit)
{
case 0:
n = 0.0001;
break;
case 1:
n = 0.01;
break;
case 2:
n = 1;
break;
case 3:
n = 100;
break;
2024-07-09 09:49:42 +08:00
}
dataDymY.IntegratRMS = lowbit * n;
2021-09-18 13:45:24 +08:00
2024-07-09 09:49:42 +08:00
memset(buf, 0, sizeof(buf));
sprintf(buf, "%02x%02x", pRecvData->Data[45], pRecvData->Data[44]);
2021-09-18 13:45:24 +08:00
iTemp = (unsigned int)strtol(buf, NULL, 16);
highbit = iTemp >> 14 & 0x3;
lowbit = iTemp & 0x3fff;
switch (highbit)
{
case 0:
n = 0.0001;
break;
case 1:
n = 0.01;
break;
case 2:
n = 1;
break;
case 3:
n = 100;
break;
}
2024-07-09 09:49:42 +08:00
dataDymY.Amp1 = lowbit * n;
2021-09-18 13:45:24 +08:00
2024-07-09 09:49:42 +08:00
memset(buf, 0, sizeof(buf));
sprintf(buf, "%02x%02x", pRecvData->Data[47], pRecvData->Data[46]);
2021-09-18 13:45:24 +08:00
iTemp = (unsigned int)strtol(buf, NULL, 16);
highbit = iTemp >> 14 & 0x3;
lowbit = iTemp & 0x3fff;
switch (highbit)
{
case 0:
n = 0.0001;
break;
case 1:
n = 0.01;
break;
case 2:
n = 1;
break;
case 3:
n = 100;
break;
2024-07-09 09:49:42 +08:00
}
dataDymY.Amp2 = lowbit * n;
2021-09-18 13:45:24 +08:00
2024-07-09 09:49:42 +08:00
memset(buf, 0, sizeof(buf));
sprintf(buf, "%02x%02x", pRecvData->Data[49], pRecvData->Data[48]);
2021-09-18 13:45:24 +08:00
iTemp = (unsigned int)strtol(buf, NULL, 16);
highbit = iTemp >> 14 & 0x3;
lowbit = iTemp & 0x3fff;
switch (highbit)
{
case 0:
n = 0.0001;
break;
case 1:
n = 0.01;
break;
case 2:
n = 1;
break;
case 3:
n = 100;
break;
2024-07-09 09:49:42 +08:00
}
dataDymY.Amp3 = lowbit * n;
2021-09-18 13:45:24 +08:00
2024-07-09 09:49:42 +08:00
memset(buf, 0, sizeof(buf));
sprintf(buf, "%02x%02x", pRecvData->Data[51], pRecvData->Data[50]);
2021-09-18 13:45:24 +08:00
iTemp = (unsigned int)strtol(buf, NULL, 16);
highbit = iTemp >> 14 & 0x3;
lowbit = iTemp & 0x3fff;
switch (highbit)
{
case 0:
n = 0.0001;
break;
case 1:
n = 0.01;
break;
case 2:
n = 1;
break;
case 3:
n = 100;
break;
2024-07-09 09:49:42 +08:00
}
dataDymY.Amp4 = lowbit * n;
2021-09-18 13:45:24 +08:00
2024-07-09 09:49:42 +08:00
memset(buf, 0, sizeof(buf));
sprintf(buf, "%02x%02x", pRecvData->Data[53], pRecvData->Data[52]);
2021-09-18 13:45:24 +08:00
iTemp = (unsigned int)strtol(buf, NULL, 16);
highbit = iTemp >> 14 & 0x3;
lowbit = iTemp & 0x3fff;
switch (highbit)
{
case 0:
n = 0.0001;
break;
case 1:
n = 0.01;
break;
case 2:
n = 1;
break;
case 3:
n = 100;
break;
2024-07-09 09:49:42 +08:00
}
dataDymY.Amp5 = lowbit * n;
memset(buf, 0, sizeof(buf));
// sprintf(buf, "%02x%02x", pRecvData->Data[55], pRecvData->Data[54]);
// iTemp = (unsigned int)strtol(buf, NULL, 16);
// highbit = iTemp >> 14 & 0x3;
// lowbit = iTemp & 0x3fff;
// switch (highbit)
// {
// case 0:
// n = 0.0001;
// break;
// case 1:
// n = 0.01;
// break;
// case 2:
// n = 1;
// break;
// case 3:
// n = 100;
// break;
// }
// dataDymY.EnvelopEnergy = lowbit * n;
dataDymY.EnvelopEnergy = 0;
memset(buf, 0, sizeof(buf));
// sprintf(buf, "%02x%02x", pRecvData->Data[57], pRecvData->Data[56]);
// iTemp = (unsigned int)strtol(buf, NULL, 16);
// highbit = iTemp >> 14 & 0x3;
// lowbit = iTemp & 0x3fff;
// switch (highbit)
// {
// case 0:
// n = 0.0001;
// break;
// case 1:
// n = 0.01;
// break;
// case 2:
// n = 1;
// break;
// case 3:
// n = 100;
// break;
// }
dataDymY.Phase1 = 0;
memset(buf, 0, sizeof(buf));
// sprintf(buf, "%02x%02x", pRecvData->Data[59], pRecvData->Data[58]);
// iTemp = (unsigned int)strtol(buf, NULL, 16);
// highbit = iTemp >> 14 & 0x3;
// lowbit = iTemp & 0x3fff;
// switch (highbit)
// {
// case 0:
// n = 0.0001;
// break;
// case 1:
// n = 0.01;
// break;
// case 2:
// n = 1;
// break;
// case 3:
// n = 100;
// break;
// }
dataDymY.Phase2 = 0;
memset(buf, 0, sizeof(buf));
// sprintf(buf, "%02x%02x", pRecvData->Data[61], pRecvData->Data[60]);
// iTemp = (unsigned int)strtol(buf, NULL, 16);
// highbit = iTemp >> 14 & 0x3;
// lowbit = iTemp & 0x3fff;
// switch (highbit)
// {
// case 0:
// n = 0.0001;
// break;
// case 1:
// n = 0.01;
// break;
// case 2:
// n = 1;
// break;
// case 3:
// n = 100;
// break;
// }
dataDymY.Phase3 = 0;
memset(buf, 0, sizeof(buf));
// sprintf(buf, "%02x%02x", pRecvData->Data[63], pRecvData->Data[62]);
// iTemp = (unsigned int)strtol(buf, NULL, 16);
// highbit = iTemp >> 14 & 0x3;
// lowbit = iTemp & 0x3fff;
// switch (highbit)
// {
// case 0:
// n = 0.0001;
// break;
// case 1:
// n = 0.01;
// break;
// case 2:
// n = 1;
// break;
// case 3:
// n = 100;
// break;
// }
dataDymY.Phase4 = 0;
memset(whereCon, 0, 1024);
2024-08-05 10:25:50 +08:00
sprintf(whereCon, "channelID='%s' and sendMsg = '1'", (strMeasurementID + "-Y").c_str());
2024-07-09 09:49:42 +08:00
memset(updateSql, 0, 1024);
sprintf(updateSql, "diagnosisPk='%f',integratPk='%f',integratRMS='%f',rmsValues='%f',envelopEnergy='%f',\
Amp1='%f',Amp2='%f',Amp3='%f',Amp4='%f',Amp5='%f',Phase1='%f',Phase2='%f',Phase3='%f',Phase4='%f',timeStamp='%s',StaticIndex = %d,nodeResend = %d ",\
dataDymY.DiagnosisPk, dataDymY.IntegratPk, dataDymY.IntegratRMS, dataDymY.RmsValues, dataDymY.EnvelopEnergy,\
dataDymY.Amp1, dataDymY.Amp2, dataDymY.Amp3, dataDymY.Amp4, dataDymY.Amp5,dataDymY.Phase1, dataDymY.Phase2, dataDymY.Phase3, dataDymY.Phase4, nowTimetamp.c_str(),staticIndex,nodeResend);
if ( /*0 == sql_ctl->GetTableRows(T_DATA_INFO(TNAME), whereCon)*/ (Count * 3 < SAVE_COUNT && lTime < OneWeek ) || strTime.size() == 0) {
char insertSql[1024] = { 0 };
memset(insertSql,0x00,sizeof(insertSql));
sprintf(insertSql, "'%s','%s','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f',%d,'%s','1',%d",
2024-08-05 10:25:50 +08:00
strLongAddr.c_str(), (strMeasurementID + "-Y").c_str(), dataDymY.DiagnosisPk, dataDymY.IntegratPk, dataDymY.IntegratRMS, dataDymY.RmsValues, dataDymY.EnvelopEnergy,\
2024-07-09 09:49:42 +08:00
dataDymY.Amp1, dataDymY.Amp2, dataDymY.Amp3, dataDymY.Amp4, dataDymY.Amp5,dataDymY.Phase1, dataDymY.Phase2, dataDymY.Phase3, dataDymY.Phase4,staticIndex, nowTimetamp.c_str(),nodeResend);
sql_ctl->InsertData(szTableName, insertSql);
if(0 == sql_ctl->GetTableRows(T_DATA_INFO(TNAME), whereCon))
sql_ctl->InsertData(T_DATA_INFO(TNAME), insertSql);
else
sql_ctl->UpdateTableData(T_DATA_INFO(TNAME), updateSql, whereCon);
} else {
memset(whereCon,0x00,sizeof(whereCon));
2024-08-05 10:25:50 +08:00
sprintf(whereCon, "channelID='%s' and sendMsg = '1' and timeStamp = '%s'", (strMeasurementID + "-Y").c_str(),strTime.c_str());
2024-07-09 09:49:42 +08:00
// sprintf(whereCon, "channelID='%s' and sendMsg = '1' ", (strLongAddr + "-Y").c_str());
sql_ctl->UpdateTableData(szTableName, updateSql, whereCon);
memset(whereCon,0x00,sizeof(whereCon));
2024-08-05 10:25:50 +08:00
sprintf(whereCon, "channelID='%s' ", (strMeasurementID + "-Y").c_str());
2021-09-18 13:45:24 +08:00
sql_ctl->UpdateTableData(T_DATA_INFO(TNAME), updateSql, whereCon);
2024-07-09 09:49:42 +08:00
}
2021-09-18 13:45:24 +08:00
print_info("y: %s,%s,diagnosisPk=%f,integratPk=%f,integratRMS=%f,rmsValues=%f,envelopEnergy=%f,Amp1=%f,Amp2=%f,Amp3=%f,Amp4=%f,Amp5=%f,Phase1=%f,Phase2=%f,Phase3=%f,Phase4=%f,timeStamp=%s\n",\
2024-08-05 10:25:50 +08:00
strLongAddr.c_str(), (strMeasurementID + "-Y").c_str(), dataDymY.DiagnosisPk, dataDymY.IntegratPk, dataDymY.IntegratRMS, dataDymY.RmsValues, dataDymY.EnvelopEnergy,\
2021-09-18 13:45:24 +08:00
dataDymY.Amp1, dataDymY.Amp2, dataDymY.Amp3, dataDymY.Amp4, dataDymY.Amp5,dataDymY.Phase1, dataDymY.Phase2, dataDymY.Phase3, dataDymY.Phase4, nowTimetamp.c_str());
2024-08-05 10:25:50 +08:00
valNodeFeature["dataNodeNo"] = strMeasurementID;
valNodeFeature["ChannelId"] = strMeasurementID + "-Y";
2021-09-18 13:45:24 +08:00
valNodeFeature["diagnosisPk"] = dataDymY.DiagnosisPk;
valNodeFeature["integratPk"] = dataDymY.IntegratPk;
valNodeFeature["integratRMS"] = dataDymY.IntegratRMS;
valNodeFeature["rmsValues"] = dataDymY.RmsValues;
valNodeFeature["envelopEnergy"] = dataDymY.EnvelopEnergy;
valNodeFeature["Amp1"] = dataDymY.Amp1;
valNodeFeature["Amp2"] = dataDymY.Amp2;
valNodeFeature["Amp3"] = dataDymY.Amp3;
valNodeFeature["Amp4"] = dataDymY.Amp4;
valNodeFeature["Amp5"] = dataDymY.Amp5;
valNodeFeature["Phase1"] = dataDymY.Phase1;
valNodeFeature["Phase2"] = dataDymY.Phase2;
valNodeFeature["Phase3"] = dataDymY.Phase3;
valNodeFeature["Phase4"] = dataDymY.Phase4;
valNodeFeature["timeStamp"] = nowTimetamp;
valNodeData.append(valNodeFeature);
DataRecvDym dataDymZ;
2024-07-09 09:49:42 +08:00
memset(buf, 0, sizeof(buf));
2021-09-18 13:45:24 +08:00
sprintf(buf, "%02x%02x", pRecvData->Data[65], pRecvData->Data[64]);
iTemp = (unsigned int)strtol(buf, NULL, 16);
highbit = iTemp >> 14 & 0x3;
lowbit = iTemp & 0x3fff;
switch (highbit)
{
case 0:
n = 0.0001;
break;
case 1:
n = 0.01;
break;
case 2:
n = 1;
break;
case 3:
n = 100;
break;
}
dataDymZ.DiagnosisPk = lowbit * n;
2024-07-09 09:49:42 +08:00
memset(buf, 0, sizeof(buf));
2021-09-18 13:45:24 +08:00
sprintf(buf, "%02x%02x", pRecvData->Data[67], pRecvData->Data[66]);
iTemp = (unsigned int)strtol(buf, NULL, 16);
highbit = iTemp >> 14 & 0x3;
lowbit = iTemp & 0x3fff;
switch (highbit)
{
case 0:
n = 0.0001;
break;
case 1:
n = 0.01;
break;
case 2:
n = 1;
break;
case 3:
n = 100;
break;
}
dataDymZ.RmsValues = lowbit * n;
2024-07-09 09:49:42 +08:00
memset(buf, 0, sizeof(buf));
2021-09-18 13:45:24 +08:00
sprintf(buf, "%02x%02x", pRecvData->Data[69], pRecvData->Data[68]);
iTemp = (unsigned int)strtol(buf, NULL, 16);
highbit = iTemp >> 14 & 0x3;
lowbit = iTemp & 0x3fff;
switch (highbit)
{
case 0:
n = 0.0001;
break;
case 1:
n = 0.01;
break;
case 2:
n = 1;
break;
case 3:
n = 100;
break;
}
dataDymZ.IntegratPk = lowbit * n;
2024-07-09 09:49:42 +08:00
memset(buf, 0, sizeof(buf));
2021-09-18 13:45:24 +08:00
sprintf(buf, "%02x%02x", pRecvData->Data[71], pRecvData->Data[70]);
iTemp = (unsigned int)strtol(buf, NULL, 16);
highbit = iTemp >> 14 & 0x3;
lowbit = iTemp & 0x3fff;
switch (highbit)
{
case 0:
n = 0.0001;
break;
case 1:
n = 0.01;
break;
case 2:
n = 1;
break;
case 3:
n = 100;
break;
}
dataDymZ.IntegratRMS = lowbit * n;
2024-07-09 09:49:42 +08:00
memset(buf, 0, sizeof(buf));
2021-09-18 13:45:24 +08:00
sprintf(buf, "%02x%02x", pRecvData->Data[73], pRecvData->Data[72]);
iTemp = (unsigned int)strtol(buf, NULL, 16);
highbit = iTemp >> 14 & 0x3;
lowbit = iTemp & 0x3fff;
switch (highbit)
{
case 0:
n = 0.0001;
break;
case 1:
n = 0.01;
break;
case 2:
n = 1;
break;
case 3:
n = 100;
break;
}
dataDymZ.Amp1 = lowbit * n;
2024-07-09 09:49:42 +08:00
memset(buf, 0, sizeof(buf));
2021-09-18 13:45:24 +08:00
sprintf(buf, "%02x%02x", pRecvData->Data[75], pRecvData->Data[74]);
iTemp = (unsigned int)strtol(buf, NULL, 16);
highbit = iTemp >> 14 & 0x3;
lowbit = iTemp & 0x3fff;
switch (highbit)
{
case 0:
n = 0.0001;
break;
case 1:
n = 0.01;
break;
case 2:
n = 1;
break;
case 3:
n = 100;
break;
}
dataDymZ.Amp2 = lowbit * n;
2024-07-09 09:49:42 +08:00
memset(buf, 0, sizeof(buf));
2021-09-18 13:45:24 +08:00
sprintf(buf, "%02x%02x", pRecvData->Data[77], pRecvData->Data[76]);
iTemp = (unsigned int)strtol(buf, NULL, 16);
highbit = iTemp >> 14 & 0x3;
lowbit = iTemp & 0x3fff;
switch (highbit)
{
case 0:
n = 0.0001;
break;
case 1:
n = 0.01;
break;
case 2:
n = 1;
break;
case 3:
n = 100;
break;
}
dataDymZ.Amp3 = lowbit * n;
2024-07-09 09:49:42 +08:00
memset(buf, 0, sizeof(buf));
2021-09-18 13:45:24 +08:00
sprintf(buf, "%02x%02x", pRecvData->Data[79], pRecvData->Data[78]);
iTemp = (unsigned int)strtol(buf, NULL, 16);
highbit = iTemp >> 14 & 0x3;
lowbit = iTemp & 0x3fff;
switch (highbit)
{
case 0:
n = 0.0001;
break;
case 1:
n = 0.01;
break;
case 2:
n = 1;
break;
case 3:
n = 100;
break;
}
dataDymZ.Amp4 = lowbit * n;
2024-07-09 09:49:42 +08:00
memset(buf, 0, sizeof(buf));
2021-09-18 13:45:24 +08:00
sprintf(buf, "%02x%02x", pRecvData->Data[81], pRecvData->Data[80]);
iTemp = (unsigned int)strtol(buf, NULL, 16);
highbit = iTemp >> 14 & 0x3;
lowbit = iTemp & 0x3fff;
switch (highbit)
{
case 0:
n = 0.0001;
break;
case 1:
n = 0.01;
break;
case 2:
n = 1;
break;
case 3:
n = 100;
break;
}
dataDymZ.Amp5 = lowbit * n;
2024-07-09 09:49:42 +08:00
memset(buf, 0, sizeof(buf));
2021-09-18 13:45:24 +08:00
sprintf(buf, "%02x%02x", pRecvData->Data[83], pRecvData->Data[82]);
iTemp = (unsigned int)strtol(buf, NULL, 16);
highbit = iTemp >> 14 & 0x3;
lowbit = iTemp & 0x3fff;
switch (highbit)
{
case 0:
n = 0.0001;
break;
case 1:
n = 0.01;
break;
case 2:
n = 1;
break;
case 3:
n = 100;
break;
}
dataDymZ.EnvelopEnergy = lowbit * n;
2024-07-09 09:49:42 +08:00
memset(buf, 0, sizeof(buf));
2021-09-18 13:45:24 +08:00
sprintf(buf, "%02x%02x", pRecvData->Data[85], pRecvData->Data[84]);
iTemp = (unsigned int)strtol(buf, NULL, 16);
highbit = iTemp >> 14 & 0x3;
lowbit = iTemp & 0x3fff;
switch (highbit)
{
case 0:
n = 0.0001;
break;
case 1:
n = 0.01;
break;
case 2:
n = 1;
break;
case 3:
n = 100;
break;
}
dataDymZ.Phase1 = lowbit * n;
2024-07-09 09:49:42 +08:00
memset(buf, 0, sizeof(buf));
2021-09-18 13:45:24 +08:00
sprintf(buf, "%02x%02x", pRecvData->Data[87], pRecvData->Data[86]);
iTemp = (unsigned int)strtol(buf, NULL, 16);
highbit = iTemp >> 14 & 0x3;
lowbit = iTemp & 0x3fff;
switch (highbit)
{
case 0:
n = 0.0001;
break;
case 1:
n = 0.01;
break;
case 2:
n = 1;
break;
case 3:
n = 100;
break;
}
dataDymZ.Phase2 = lowbit * n;
2024-07-09 09:49:42 +08:00
memset(buf, 0, sizeof(buf));
2021-09-18 13:45:24 +08:00
sprintf(buf, "%02x%02x", pRecvData->Data[89], pRecvData->Data[88]);
iTemp = (unsigned int)strtol(buf, NULL, 16);
highbit = iTemp >> 14 & 0x3;
lowbit = iTemp & 0x3fff;
switch (highbit)
{
case 0:
n = 0.0001;
break;
case 1:
n = 0.01;
break;
case 2:
n = 1;
break;
case 3:
n = 100;
break;
}
dataDymZ.Phase3 = lowbit * n;
sprintf(buf, "%02x%02x", pRecvData->Data[91], pRecvData->Data[90]);
iTemp = (unsigned int)strtol(buf, NULL, 16);
highbit = iTemp >> 14 & 0x3;
lowbit = iTemp & 0x3fff;
switch (highbit)
{
case 0:
n = 0.0001;
break;
case 1:
n = 0.01;
break;
case 2:
n = 1;
break;
case 3:
n = 100;
break;
}
dataDymZ.Phase4 = lowbit * n;
2024-07-09 09:49:42 +08:00
memset(whereCon, 0, 1024);
2024-08-05 10:25:50 +08:00
sprintf(whereCon, "channelID='%s' and sendMsg = '1'", (strMeasurementID + "-Z").c_str());
2024-07-09 09:49:42 +08:00
memset(updateSql, 0, 1024);
sprintf(updateSql, "diagnosisPk='%f',integratPk='%f',integratRMS='%f',rmsValues='%f',envelopEnergy='%f',\
Amp1='%f',Amp2='%f',Amp3='%f',Amp4='%f',Amp5='%f',Phase1='%f',Phase2='%f',Phase3='%f',Phase4='%f',timeStamp='%s',StaticIndex = %d,nodeResend = %d ",\
dataDymZ.DiagnosisPk, dataDymZ.IntegratPk, dataDymZ.IntegratRMS, dataDymZ.RmsValues, dataDymZ.EnvelopEnergy,\
dataDymZ.Amp1, dataDymZ.Amp2, dataDymZ.Amp3, dataDymZ.Amp4, dataDymZ.Amp5,dataDymZ.Phase1, dataDymZ.Phase2, dataDymZ.Phase3, dataDymZ.Phase4, nowTimetamp.c_str(),staticIndex,nodeResend);
if ( /*0 == sql_ctl->GetTableRows(T_DATA_INFO(TNAME), whereCon)*/ Count *3 < SAVE_COUNT && (lTime < OneWeek || strTime.size() == 0)) {
char insertSql[1024] = { 0 };
memset(insertSql,0x00,sizeof(insertSql));
sprintf(insertSql, "'%s','%s','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f',%d,'%s','1',%d",
2024-08-05 10:25:50 +08:00
strLongAddr.c_str(), (strMeasurementID + "-Z").c_str(), dataDymZ.DiagnosisPk, dataDymZ.IntegratPk, dataDymZ.IntegratRMS, dataDymZ.RmsValues, dataDymZ.EnvelopEnergy,\
2024-07-09 09:49:42 +08:00
dataDymZ.Amp1, dataDymZ.Amp2, dataDymZ.Amp3, dataDymZ.Amp4, dataDymZ.Amp5,dataDymZ.Phase1, dataDymZ.Phase2, dataDymZ.Phase3, dataDymZ.Phase4, staticIndex,nowTimetamp.c_str(),nodeResend);
sql_ctl->InsertData(szTableName, insertSql);
if(0 == sql_ctl->GetTableRows(T_DATA_INFO(TNAME), whereCon))
sql_ctl->InsertData(T_DATA_INFO(TNAME), insertSql);
else
sql_ctl->UpdateTableData(T_DATA_INFO(TNAME), updateSql, whereCon);
} else {
memset(whereCon,0x00,sizeof(whereCon));
2024-08-05 10:25:50 +08:00
sprintf(whereCon, "channelID='%s' and sendMsg = '1' and timeStamp = '%s'", (strMeasurementID + "-Z").c_str(),strTime.c_str());
2024-07-09 09:49:42 +08:00
// sprintf(whereCon, "channelID='%s' and sendMsg = '1'", (strLongAddr + "-Z").c_str());
sql_ctl->UpdateTableData(szTableName, updateSql, whereCon);
memset(whereCon,0x00,sizeof(whereCon));
2024-08-05 10:25:50 +08:00
sprintf(whereCon, "channelID='%s' ", (strMeasurementID + "-Z").c_str());
2021-09-18 13:45:24 +08:00
sql_ctl->UpdateTableData(T_DATA_INFO(TNAME), updateSql, whereCon);
2024-07-09 09:49:42 +08:00
}
2021-09-18 13:45:24 +08:00
print_info("Z: %s,%s,diagnosisPk=%f,integratPk=%f,integratRMS=%f,rmsValues=%f,envelopEnergy=%f,Amp1=%f,Amp2=%f,Amp3=%f,Amp4=%f,Amp5=%f,Phase1=%f,Phase2=%f,Phase3=%f,Phase4=%f,timeStamp=%s\n",\
2024-08-05 10:25:50 +08:00
strLongAddr.c_str(), (strMeasurementID + "-Z").c_str(), dataDymZ.DiagnosisPk, dataDymZ.IntegratPk, dataDymZ.IntegratRMS, dataDymZ.RmsValues, dataDymZ.EnvelopEnergy,\
2021-09-18 13:45:24 +08:00
dataDymZ.Amp1, dataDymZ.Amp2, dataDymZ.Amp3, dataDymZ.Amp4, dataDymZ.Amp5,dataDymZ.Phase1, dataDymZ.Phase2, dataDymZ.Phase3, dataDymZ.Phase4, nowTimetamp.c_str());
2024-07-09 09:49:42 +08:00
memset(whereCon,0x00,sizeof(whereCon));
sprintf(whereCon, "dataNodeNo='%s'", strLongAddr.c_str());
sql_ctl->UpdateTableData(T_SENSOR_INFO(TNAME), "status='1'", whereCon);
//string strData = sql_ctl->GetNodeConfigureInfor(whereCon);
//data_publish(strData.c_str(), GlobalConfig::Topic_G.mPubConfig.c_str());
2021-09-18 13:45:24 +08:00
//无线传感器Z信息
2024-08-05 10:25:50 +08:00
valNodeFeature["dataNodeNo"] = strMeasurementID;
valNodeFeature["ChannelId"] = strMeasurementID + "-Z";
2021-09-18 13:45:24 +08:00
valNodeFeature["diagnosisPk"] = dataDymZ.DiagnosisPk;
valNodeFeature["integratPk"] = dataDymZ.IntegratPk;
valNodeFeature["integratRMS"] = dataDymZ.IntegratRMS;
valNodeFeature["rmsValues"] = dataDymZ.RmsValues;
valNodeFeature["envelopEnergy"] = dataDymZ.EnvelopEnergy;
valNodeFeature["Amp1"] = dataDymZ.Amp1;
valNodeFeature["Amp2"] = dataDymZ.Amp2;
valNodeFeature["Amp3"] = dataDymZ.Amp3;
valNodeFeature["Amp4"] = dataDymZ.Amp4;
valNodeFeature["Amp5"] = dataDymZ.Amp5;
valNodeFeature["Phase1"] = dataDymZ.Phase1;
valNodeFeature["Phase2"] = dataDymZ.Phase2;
valNodeFeature["Phase3"] = dataDymZ.Phase3;
valNodeFeature["Phase4"] = dataDymZ.Phase4;
valNodeFeature["timeStamp"] = nowTimetamp;
valNodeData.append(valNodeFeature);
2024-07-09 09:49:42 +08:00
memset(whereCon, 0, 1024);
sprintf(whereCon, "dataNodeNo='%s'", strLongAddr.c_str());
string strBattery = sql_ctl->GetData(T_SENSOR_INFO(TNAME),"batteryPower",whereCon);
vector<string> vBattery;
vBattery.push_back("0");
vBattery.push_back("0");
if(strBattery.length() > 0){
boost::split( vBattery, strBattery, boost::is_any_of( "," ), boost::token_compress_on );
}
2021-09-18 13:45:24 +08:00
//无线传感器信息
Json::Value root;
Json::Value valdatastatic;
valdatastatic["TemperatureTop"] = dataStatic.TemTop;
valdatastatic["TemperatureBot"] = dataStatic.TemBot;
2024-08-05 10:25:50 +08:00
valdatastatic["WorkTime"] = dataStatic.nodeWorkTime;
valdatastatic["SendTime"] = dataStatic.nodeSendTime;
2021-09-18 13:45:24 +08:00
valdatastatic["Dip"] = dataStatic.Dip;
valdatastatic["Voltage"] = dataStatic.Voltage;
valdatastatic["ChannelType"] = "STATUS";
2024-08-05 10:25:50 +08:00
valdatastatic["ChannelId"] = strMeasurementID + "-S";
2021-09-18 13:45:24 +08:00
valdatastatic["TimeStamp"] = nowTimetamp;
2024-08-05 10:25:50 +08:00
valdatastatic["bateryProportion"] = atof(vBattery[1].c_str())/atof(vBattery[0].c_str());
2024-07-09 09:49:42 +08:00
valdatastatic["batteryRemainDay"] = atof(vBattery[1].c_str());
2024-08-05 10:25:50 +08:00
valdatastatic["dataNodeNo"] = strMeasurementID;
2021-09-18 13:45:24 +08:00
valNodeData.append(valdatastatic);
root["data"] = valNodeData;
root["TimeStamp"] = nowTimetamp;
2024-08-05 10:25:50 +08:00
root["dataNodeNo"] = strMeasurementID;
2021-09-18 13:45:24 +08:00
root["dataNodeGatewayNo"] = GlobalConfig::MacAddr_G;
Json::FastWriter featureValue;
std::string strstatisticData = featureValue.write(root);
//传感器发来的数据包中的表示设备信息的数据转化为json格式后通过调用data_publish将数据传给mqttclient Topicwireless/cmd/60294D203717
2024-07-09 09:49:42 +08:00
int iRet = data_publish(strstatisticData.c_str(), GlobalConfig::Topic_G.mPubData.c_str());
print_info("dataNodeNo = '%s' and TimeStamp = '%s',MQTT ret = %d\n",strLongAddr.c_str(),nowTimetamp.c_str(),iRet);
if(iRet != 0){
char updateSql[1024] = { 0 };
memset(whereCon, 0, 64);
sprintf(whereCon, "dataNodeNo = '%s' and TimeStamp = '%s'", strLongAddr.c_str(),nowTimetamp.c_str());
memcpy(updateSql, "sendMsg='0'",sizeof(updateSql));
sql_ctl->UpdateTableData(szTableNameStatic, updateSql, whereCon);
sql_ctl->UpdateTableData(szTableNameData, updateSql, whereCon);
}
2021-09-18 13:45:24 +08:00
//综上代码把静态数据x y z轴的特征值存放到sql数据库中如果数据原来不存在则插入新数据如果存在则更新数据
2024-07-09 09:49:42 +08:00
2021-09-18 13:45:24 +08:00
print_info("Dip : %d TemBot : %f TemBot : %f Voltage : %d\n", dataStatic.Dip, dataStatic.TemBot, dataStatic.TemTop, dataStatic.Voltage);
2024-07-09 09:49:42 +08:00
memset(selectCon,0x00,sizeof(selectCon));
sprintf(selectCon, "zigbeeSignal <> '' ORDER BY timeStamp desc LIMIT 0,1");
strTime = sql_ctl->GetData(szTableNameStatic, "timeStamp", selectCon);
if(/*(atol(nowTimetamp.c_str()) - atol(strTime.c_str()) > 3600)*/(1 == flag) &&
2024-07-09 09:49:42 +08:00
(unsigned short)GlobalConfig::Zigbee_G.MyAddr == 0x9999)// 24h = 86400s ; 8h = 36000 ; 16h = 72000
{
LOG_DEBUG("Zigbee Signal !\n");
int Times = 0;
bZigbeeSinal = true;
usleep(20000);
while(Times < 3 && bZigbeeSinal){
getZigbeeSignal(pRecvData->ShortAddr);
Times ++ ;
usleep(20000);;
}
bZigbeeSinal = false;
}
2024-08-05 10:25:50 +08:00
//LOG_INFO("DealDataNodeFeature end\n");
2021-09-18 13:45:24 +08:00
}
void Uart::DealDataNodeWave(const char *pData)
{
2024-07-09 09:49:42 +08:00
//print_info("recv wave\n");
2021-09-18 13:45:24 +08:00
RecvData * pRecvData = (RecvData *)pData;
m_VecWaveData.push_back(*pRecvData);
2024-07-09 09:49:42 +08:00
//print_blue("wave data size is(m_VecWaveData.size) : %d\n",m_VecWaveData.size());
2021-09-18 13:45:24 +08:00
char localtimestamp[32] = { 0 };
GetTimeNet(localtimestamp, 1);
// 接收到原始数据信息,则更新时间戳,如果三秒种未收到原始数据,则重新从短地址 9999 切换回 短地址 8888
m_TimeStamp = strtol(localtimestamp, NULL, 10);
}
void Uart::DealWaveThread() //连续三秒没有原始数据,则处理缓存的原始数据
{
unsigned long nowTimeStamp = 0;
unsigned long tmpTimeStamp = 0;
while (1)
{
// 接收到原始波形,则 m_TimeStamp 不为零
// 如果当前时间与记录时间超过3秒要求m_TimeStamp不变化而数据在传输则一定小于3秒
if (0 == m_TimeStamp) {
2024-07-09 09:49:42 +08:00
sleep(1);
2021-09-18 13:45:24 +08:00
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);
2024-07-09 09:49:42 +08:00
//DealWave();
2021-09-18 13:45:24 +08:00
m_TimeStamp = 0;
offSize = 0;
2021-09-18 13:45:24 +08:00
GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = NO_ENTER_TRANSMITTING_STATUS;
GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0;
// 准备重新恢复到 8888 PanID正常接收特征数据
2024-07-09 09:49:42 +08:00
mPackgeIndex = -1;
//WriteLocalAddr(0x8888);
//GlobalConfig::Zigbee_G.MyAddr = 0x8888;
// WriteShortAddr2Zigbee(0x8888);
// UpdateZigbeeInfoCtrl();
2021-09-18 13:45:24 +08:00
} else {
print_info("NO! The time difference is less than 3,nowTimeStamp : %ld m_TimeStamp : %ld\n", nowTimeStamp, m_TimeStamp);
2024-07-09 09:49:42 +08:00
}
2021-09-18 13:45:24 +08:00
2024-07-09 09:49:42 +08:00
sleep(1);
2021-09-18 13:45:24 +08:00
}
}
void Uart::DealWave()
{
2024-07-09 09:49:42 +08:00
LOG_DEBUG("begin deal Wave data !\n");
print_blue("wave data size is(m_VecWaveData.size) : %d\n",m_VecWaveData.size());
2021-09-18 13:45:24 +08:00
std::string strShortAddr = "";
std::string strShortAddrTemp;
std::string strLongAddr = "";
2024-08-05 10:25:50 +08:00
std::string strMeasurementID= "";
2021-09-18 13:45:24 +08:00
std::string strFileName = "";
int iChannel = 0;
int iChannelTemp = 0;
long iTemp = 0;
std::vector<float> vecData;
char buf[8];
RecvData recvTemp;
2024-07-09 09:49:42 +08:00
LOG_INFO("all wave data size is(m_VecWaveData.size) : %d \n",\
m_VecWaveData.size());
2021-09-18 13:45:24 +08:00
while (m_VecWaveData.size() > 0) { //对每个传感器的每个通道进行遍历然后处理数据例如传感器1x轴的数据处理完后再去处理y轴的。传感器1的所有数据处理完后再处理传感器2的
std::vector<RecvData>::iterator iter = m_VecWaveData.begin();
recvTemp = *iter;
memset(buf, 0, 8);
sprintf(buf, "%02x%02x", recvTemp.ShortAddr[0], recvTemp.ShortAddr[1]);
strShortAddr = std::string(buf);
memset(buf, 0, 8);
sprintf(buf, "%02d", recvTemp.Type&0xFF);
2024-07-09 09:49:42 +08:00
iChannel = atoi(buf);
2021-09-18 13:45:24 +08:00
char getLongAddr_sql[32] = { 0 };
sprintf(getLongAddr_sql, "zigbeeShortAddr='%s'", strShortAddr.c_str());
2024-07-09 09:49:42 +08:00
//strLongAddr = sql_ctl->GetData(T_SENSOR_INFO(TNAME), T_SENSOR_INFO(ZIGBEELONGADDR), getLongAddr_sql);
vec_t res = sql_ctl->GetDataSingleLine(T_SENSOR_INFO(TNAME)," * ",getLongAddr_sql);
2024-08-05 10:25:50 +08:00
strLongAddr = res[0];
strMeasurementID = res[44];
2021-09-18 13:45:24 +08:00
//print_info("3.1.2.3--->strLongAddr : %s\n", strLongAddr.c_str());
if ( 0 == strLongAddr.length() ) {
2024-07-09 09:49:42 +08:00
sleep(1);
2021-09-18 13:45:24 +08:00
continue;
}
std::string ran = "";
int n = 0;
int range = 0;
float coe = 0;
2024-07-09 09:49:42 +08:00
int sampleRate = 0,ACCSampleTime = 0;
2021-09-18 13:45:24 +08:00
char getrange[32] = {0};
std::string str = "range";
2024-07-09 09:49:42 +08:00
sprintf(getrange, "zigbeeShortAddr='%s'", strShortAddr.c_str());
//ran = sql_ctl->GetData(T_SENSOR_INFO(TNAME), T_SENSOR_INFO(RANGE), getrange);
ran = res[25];
sampleRate = atoi(res[23].c_str());
ACCSampleTime = atoi(res[36].c_str());
// print_info("@@@@@@@@@@@@@@@@@@@@@@ran=%s\n",ran.c_str());
2021-09-18 13:45:24 +08:00
memset(getrange, 0, 32);
sprintf(getrange, "%s", ran.c_str());
n = (int)strtol(getrange, NULL, 32);
2024-07-09 09:49:42 +08:00
// print_light_purple("!!!!!!!!!!!!!n=%d\n",n);
if(res[17] == "01"){
switch (n)
2021-09-18 13:45:24 +08:00
{
case 0:{
range = 8;
coe = 8*1.0/32767;
}
break;
case 1:{
range = 16;
coe = 16*1.0/32767;
}
break;
case 2:{
range = 32;
coe = 32*1.0/32767;
}
break;
case 3:{
range = 64;
coe = 64*1.0/32767;
}
break;
}
2024-07-09 09:49:42 +08:00
}else if(res[17] == "02"){
if(iChannel == 3 || iChannel == 4){
coe = 0.00048828125;
}
if(iChannel == 5){
if(res[8] == "0.1"){
coe = 0.0034521484375;//0.03265968810083316;
}else{
coe = 0.00172607421875;
}
}
}
2021-09-18 13:45:24 +08:00
//print_blue("##############range = %d,%f\n",range,coe);
for(; iter != m_VecWaveData.end();)
{
recvTemp = *iter;
memset(buf, 0, 8);
sprintf(buf, "%02x%02x", recvTemp.ShortAddr[0], recvTemp.ShortAddr[1]);
strShortAddrTemp = std::string(buf);
memset(buf, 0, 8);
sprintf(buf, "%02d", recvTemp.Type & 0xFF);
2024-07-09 09:49:42 +08:00
iChannelTemp = atoi(buf);
2021-09-18 13:45:24 +08:00
if( 0 == strShortAddr.compare(strShortAddrTemp) && (iChannel == iChannelTemp) ) {
float fTemp;
2024-07-09 09:49:42 +08:00
{
for (int i = 0; i < 46; i++) {
memset(buf, 0, 8);
sprintf(buf, "%02x%02x", recvTemp.Data[2*i+1],recvTemp.Data[i*2]);
iTemp = strtol(buf, NULL, 16);
if (iTemp < 0x8000) {
fTemp = iTemp * coe * 9.8; //convert to m/s2
} else {
fTemp = (((~iTemp)&0xffff) + 1) * - coe * 9.8; //convert to m/s2
}
// print_blue("wave data is %u,%f,%f\n",iTemp,fTemp, coe);
vecData.push_back(fTemp);
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 ){//过滤数据包结尾空数据
print_blue("%d vecData.size() == %d,sampleRate * ACCSampleTime = %d\n",iChannel,vecData.size(),sampleRate * ACCSampleTime);
break;
}
if(vecData.size() >= sampleRate * ACCSampleTime && iChannel == 4 ){//过滤数据包结尾空数据
print_blue("%d vecData.size() == %d,sampleRate * ACCSampleTime = %d\n",iChannel,vecData.size(),sampleRate * ACCSampleTime);
break;
}
if(vecData.size() >= sampleRate * ACCSampleTime && iChannel == 5 ){//过滤数据包结尾空数据
print_blue("%d vecData.size() == %d,sampleRate * ACCSampleTime = %d\n",iChannel,vecData.size(),sampleRate * ACCSampleTime);
break;
}
}else if(res[17] == "02"){
if(vecData.size() >= 8192 && iChannel == 3 ){//过滤数据包结尾空数据
break;
}
if(vecData.size() >= 8192 && iChannel == 4 ){//过滤数据包结尾空数据
break;
}
if(vecData.size() >= sampleRate * ACCSampleTime && iChannel == 5 ){//过滤数据包结尾空数据
break;
}
}
}
2021-09-18 13:45:24 +08:00
}
iter = m_VecWaveData.erase(iter);
2024-07-09 09:49:42 +08:00
//print_blue("m_VecWaveData.size() == %d,vecData = %d\n",m_VecWaveData.size(),vecData.size());
2021-09-18 13:45:24 +08:00
} else {
iter++;
}
}
/*char getLongAddr_sql[32] = { 0 };
sprintf(getLongAddr_sql, "zigbeeShortAddr='%s'", strShortAddr.c_str());
strLongAddr = sql_ctl->GetData(T_SENSOR_INFO(TNAME), T_SENSOR_INFO(ZIGBEELONGADDR), getLongAddr_sql);
print_info("strLongAddr : %s\n", strLongAddr.c_str());*/
if ( 0 == strLongAddr.length() ) {
2024-07-09 09:49:42 +08:00
sleep(1);
2021-09-18 13:45:24 +08:00
continue;
}
2024-07-09 09:49:42 +08:00
char localtimestamp[32] = { 0 };
GetTimeNet(localtimestamp, 1);
std::string nowTimetamp = std::string(localtimestamp);
2021-09-18 13:45:24 +08:00
std::string strChannelID = "";
switch (iChannel)
{
case 3:{
2024-08-05 10:25:50 +08:00
strFileName = "/opt/data/" + strMeasurementID + "-X.dat";
strChannelID = strMeasurementID + "-X";
2021-09-18 13:45:24 +08:00
}
break;
case 4:{
2024-08-05 10:25:50 +08:00
strFileName = "/opt/data/" + strMeasurementID + "-Y.dat";
strChannelID = strMeasurementID + "-Y";
2021-09-18 13:45:24 +08:00
}
break;
case 5:{
2024-08-05 10:25:50 +08:00
strFileName = "/opt/data/" + strMeasurementID + "-Z.dat";
strChannelID = strMeasurementID + "-Z";
2021-09-18 13:45:24 +08:00
}
break;
default:
break;
}
if (access(strFileName.c_str(), 0) > 0) { //如果存在原始数据删除原来的,只保留一份
std::string strCmd = "rm " + strFileName;
system(strCmd.c_str());
}
FILE *fp = fopen(strFileName.c_str(), "w");
2024-07-09 09:49:42 +08:00
fwrite(localtimestamp,sizeof(localtimestamp),1,fp);
2021-09-18 13:45:24 +08:00
print_info("fopen FIle vecData.size : %d\n", vecData.size());
2024-07-09 09:49:42 +08:00
float mean = pCalculation->mean(vecData);
2021-09-18 13:45:24 +08:00
float frTemp;
char buf[33]={0x00};
std::string strWaveData = "";
for (int i = 0; i < vecData.size(); i++) {
2024-07-09 09:49:42 +08:00
frTemp = vecData[i] - mean;
2021-09-18 13:45:24 +08:00
fwrite(&frTemp,sizeof(float),1,fp);
memset(buf,0x00,sizeof(buf));
2024-07-09 09:49:42 +08:00
sprintf(buf, "%.2f", frTemp);
2021-09-18 13:45:24 +08:00
std::string waveTemp(buf);
if(i == 0)
strWaveData = waveTemp;
2024-08-05 10:25:50 +08:00
else
strWaveData = strWaveData + "," + waveTemp;
2021-09-18 13:45:24 +08:00
}
2024-07-09 09:49:42 +08:00
2021-09-18 13:45:24 +08:00
fclose(fp);
//
Json::Value valWaveData;
2024-07-09 09:49:42 +08:00
int length = vecData.size();
valWaveData["number"] = sampleRate;
2021-09-18 13:45:24 +08:00
valWaveData["channelId"] = strChannelID;
2024-08-05 10:25:50 +08:00
valWaveData["dataNodeNo"] = strMeasurementID;
2021-09-18 13:45:24 +08:00
valWaveData["dataNodeGatewayNo"] = GlobalConfig::MacAddr_G;
valWaveData["SensorEngineeringUnit"] = "";
2024-07-09 09:49:42 +08:00
valWaveData["timeStamp"] = nowTimetamp;
2021-09-18 13:45:24 +08:00
valWaveData["waveData"] = strWaveData;
2024-07-09 09:49:42 +08:00
valWaveData["mean"] = mean;
2021-09-18 13:45:24 +08:00
Json::FastWriter WaveValue;
std::string WaveData = WaveValue.write(valWaveData);
2024-07-09 09:49:42 +08:00
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 ){//过滤数据包结尾空数据
print_blue("%d vecData.size() == %d,sampleRate * ACCSampleTime = %d\n",iChannel,vecData.size(),sampleRate * ACCSampleTime);
std::vector<float>().swap(vecData);
continue;
}
if(vecData.size() < sampleRate * ACCSampleTime && iChannel == 4 ){//过滤数据包结尾空数据
print_blue("%d vecData.size() == %d,sampleRate * ACCSampleTime = %d\n",iChannel,vecData.size(),sampleRate * ACCSampleTime);
std::vector<float>().swap(vecData);
continue;
}
if(vecData.size() < sampleRate * ACCSampleTime && iChannel == 5 ){//过滤数据包结尾空数据
print_blue("%d vecData.size() == %d,sampleRate * ACCSampleTime = %d\n",iChannel,vecData.size(),sampleRate * ACCSampleTime);
std::vector<float>().swap(vecData);
continue;
}
}else if(res[17] == "02"){
if(vecData.size() < 8192 && iChannel == 3 ){//过滤数据包结尾空数据
continue;
}
if(vecData.size() < 8192 && iChannel == 4 ){//过滤数据包结尾空数据
continue;
}
if(vecData.size() < sampleRate * ACCSampleTime && iChannel == 5 ){//过滤数据包结尾空数据
continue;
}
}
char selectCon[128] = { 0 };
sprintf(selectCon, "channelID='%s' and sendMsg = '1' ORDER BY timeStamp ASC LIMIT 0,1",strChannelID.c_str());
std::string strTime = sql_ctl->GetData("t_data_waveSend", "timeStamp", selectCon);
long lTime = atol(nowTimetamp.c_str())-atol(strTime.c_str());
int Count = sql_ctl->GetTableRows("t_data_waveSend", NULL);
std::string strFileName_Record = strFileName + "_" + nowTimetamp;
if ((Count * 3 < SAVE_COUNT && lTime < OneWeek ) || strTime.size() == 0 ) {
char insertSql[128]={0x00};
sprintf(insertSql,"'%s','%s','%s',1,0",strChannelID.c_str(),strFileName_Record.c_str(),nowTimetamp.c_str());
sql_ctl->InsertData("t_data_waveSend", insertSql);
}else{
char updateSql[128] = { 0 },whereCon[128] = {0};
sprintf(updateSql, "waveName='%s',timeStamp='%s'",strFileName_Record.c_str(),nowTimetamp.c_str());
memset(whereCon,0x00,sizeof(whereCon));
sprintf(whereCon, "channelID='%s' and sendMsg = '1' and timeStamp = '%s'", strChannelID.c_str(),strTime.c_str());
print_info("update static data to sql\n");
sql_ctl->UpdateTableData("t_data_waveSend", updateSql, whereCon);
}
2021-09-18 13:45:24 +08:00
//传感器发来的数据包中的表示设备信息的数据转化为json格式后通过调用data_publish将数据传给mqttclient Topicwireless/cmd/60294D203717
2024-07-09 09:49:42 +08:00
int iRet = data_publish(WaveData.c_str(), GlobalConfig::Topic_G.mPubWaveData.c_str());
if(iRet != 0){
char whereCon[1024] = {0x00};
char updateSql[1024] = {0x00};
char tmpWhere[128]={0x00};
sprintf(tmpWhere,"channelID = '%s' and sendMsg = 0 ",strChannelID.c_str());
int count = sql_ctl->GetTableRows("t_data_waveSend", tmpWhere);
LOG_ERROR("save channlID %s dat count = %d\n",strChannelID.c_str(),count);
if(count <= 12)
{
sprintf(whereCon, "channelID='%s' and timeStamp = '%s' ", strChannelID.c_str(),nowTimetamp.c_str());
sprintf(updateSql, "SendMsg = 0 ");
sql_ctl->UpdateTableData("t_data_waveSend", updateSql, whereCon);
LOG_ERROR("send failed,filename %s,iRet = %d\n",strFileName.c_str(),iRet);
string strFileName_failed = strFileName + "_" + nowTimetamp;
char tmpCmd[128]={0x00};
sprintf(tmpCmd,"cp %s %s",strFileName.c_str(),strFileName_failed.c_str());
system(tmpCmd);
}else {
memset(tmpWhere,0x00,sizeof(tmpWhere));
memset(updateSql,0x00,sizeof(updateSql));
sprintf(tmpWhere, " sendMsg = 0 and channelID='%s' ORDER BY timeStamp ASC LIMIT 0,1",strChannelID.c_str());
vec_t vecRet = sql_ctl->GetDataSingleLine("t_data_waveSend","*",tmpWhere);
memset(tmpWhere,0x00,sizeof(tmpWhere));
sprintf(tmpWhere, " sendMsg = 0 and timeStamp = '%s' and channelID = '%s' ",vecRet[2].c_str(),vecRet[0].c_str());
sprintf(updateSql, "sendMsg = 3 ");
int iRet = sql_ctl->UpdateTableData("t_data_waveSend", updateSql, tmpWhere);
memset(tmpWhere,0x00,sizeof(tmpWhere));
memset(updateSql,0x00,sizeof(updateSql));
sprintf(whereCon, "channelID='%s' and timeStamp = '%s' ", strChannelID.c_str(),nowTimetamp.c_str());
sprintf(updateSql, "sendMsg = 0");
int iRet2 = sql_ctl->UpdateTableData("t_data_waveSend", updateSql, whereCon);
string strFileName_failed = strFileName + "_" + nowTimetamp;
char tmpCmd[128]={0x00};
sprintf(tmpCmd,"cp %s %s",strFileName.c_str(),strFileName_failed.c_str());
system(tmpCmd);
LOG_ERROR("cp dat file %s \n",tmpCmd);
memset(tmpWhere,0x00,sizeof(tmpWhere));
sprintf(tmpWhere," channelID = '%s' and sendMsg = 0 ",strChannelID.c_str());
int count = sql_ctl->GetTableRows("t_data_waveSend", tmpWhere);
memset(tmpCmd,0x00,sizeof(tmpCmd));
sprintf(tmpCmd,"rm %s ",vecRet[1].c_str());
system(tmpCmd);
LOG_ERROR("rm dat file %s \n",tmpCmd);
}
}else{
LOG_DEBUG("send data , filename %s\n", strFileName.c_str());
}
#ifdef G2UL_GATEWAY//存储6条波形数据
char whereCon[1024] = {0x00};
char updateSql[1024] = {0x00};
char tmpWhere[128]={0x00};
sprintf(tmpWhere,"channelID = '%s' and save = 1 ",strChannelID.c_str());
int count = sql_ctl->GetTableRows("t_data_waveSend", tmpWhere);
LOG_INFO("save channlID %s dat count = %d\n",strChannelID.c_str(),count);
if(count <= 5)
{
sprintf(whereCon, "channelID='%s' and timeStamp = '%s' ", strChannelID.c_str(),nowTimetamp.c_str());
sprintf(updateSql, "save = 1 ");
sql_ctl->UpdateTableData("t_data_waveSend", updateSql, whereCon);
string strFileName_save = strFileName + "_" + nowTimetamp + "_save";
char tmpCmd[128]={0x00};
sprintf(tmpCmd,"cp %s %s",strFileName.c_str(),strFileName_save.c_str());
system(tmpCmd);
}else {
memset(tmpWhere,0x00,sizeof(tmpWhere));
memset(updateSql,0x00,sizeof(updateSql));
sprintf(tmpWhere, " save = 1 and channelID='%s' ORDER BY timeStamp ASC LIMIT 0,1",strChannelID.c_str());
vec_t vecRet = sql_ctl->GetDataSingleLine("t_data_waveSend","*",tmpWhere);
memset(tmpWhere,0x00,sizeof(tmpWhere));
sprintf(tmpWhere, " save = 1 and timeStamp = '%s' and channelID = '%s' ",vecRet[2].c_str(),vecRet[0].c_str());
sprintf(updateSql, "save = 0 ");
int iRet = sql_ctl->UpdateTableData("t_data_waveSend", updateSql, tmpWhere);
memset(tmpWhere,0x00,sizeof(tmpWhere));
memset(updateSql,0x00,sizeof(updateSql));
sprintf(whereCon, "channelID='%s' and timeStamp = '%s' ", strChannelID.c_str(),nowTimetamp.c_str());
sprintf(updateSql, "save = 1");
int iRet2 = sql_ctl->UpdateTableData("t_data_waveSend", updateSql, whereCon);
string strFileName_save = strFileName + "_" + nowTimetamp + "_save";
char tmpCmd[128]={0x00};
sprintf(tmpCmd,"cp %s %s",strFileName.c_str(),strFileName_save.c_str());
system(tmpCmd);
memset(tmpWhere,0x00,sizeof(tmpWhere));
sprintf(tmpWhere," channelID = '%s' and save = 1 ",strChannelID.c_str());
int count = sql_ctl->GetTableRows("t_data_waveSend", tmpWhere);
memset(tmpCmd,0x00,sizeof(tmpCmd));
sprintf(tmpCmd,"rm %s ",(vecRet[1]+"_save").c_str());
system(tmpCmd);
LOG_INFO("rm dat file %s \n",tmpCmd);
}
#endif
2021-09-18 13:45:24 +08:00
print_info("write data to filename %s\n", strFileName.c_str());
2024-07-09 09:49:42 +08:00
std::vector<float>().swap(vecData);
sleep(1);
2021-09-18 13:45:24 +08:00
}
2024-07-09 09:49:42 +08:00
std::vector<RecvData>().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());
2021-09-18 13:45:24 +08:00
// memset(buf, 0, 8);
// sprintf(buf, "%02x%02x", recvTemp.ShortAddr[0], recvTemp.ShortAddr[1]);
// std::string strShortAddr = std::string(buf);
2024-07-09 09:49:42 +08:00
}
void Uart::DealNodeSendTime(unsigned char* shortaddr)
{
/*char updateSql[1024]={0x00},whereCon[1024]={0x00},insertSql[1024]={0x00};
memset(whereCon,0x00,sizeof(whereCon));
memset(insertSql,0x00,sizeof(insertSql));
sprintf(whereCon,"dataNodeNo = '%s'",dataNodeInfo.ZigbeeLongAddr.c_str());
int nodegroup = 0;
int staticStartTime = 0;
int nodeindex = 0;
int statictime = 0;
int nodewaveindex = 1;
if(0 == sql_ctl->GetTableRows(T_DATANODE_TIME(TNAME), whereCon)){
sprintf(whereCon,"staticcycle = '%d' and wavecycle = '%d' order by nodeindex desc LIMIT 0 , 1",dataNodeInfo.FeatureInterVal,dataNodeInfo.WaveInterVal);
vec_t vecResult = sql_ctl->GetDataSingleLine(T_DATANODE_TIME(TNAME),"*",whereCon);
if(vecResult.size() == 0){
string strResult = sql_ctl->GetData(T_DATANODE_TIME(TNAME),"nodegroup"," nodegroup > 0 order by nodegroup desc LIMIT 0 , 1");
if(atoi(strResult.c_str()) > 0){
print_info("strResult = %s\n",strResult.c_str());
nodegroup = atoi(strResult.c_str()) + 1;
}else{
nodegroup = 1;
}
staticStartTime = 0;
}else{
nodegroup = atoi(vecResult[4].c_str());
nodewaveindex = atoi(vecResult[6].c_str()) + 1;
statictime = atoi(vecResult[7].c_str()) + atoi(vecResult[2].c_str());
if(statictime > atoi(vecResult[3].c_str())){
staticStartTime = ceil(atof(vecResult[2].c_str()) / 2 );
statictime = staticStartTime;
nodewaveindex = 1;
}else{
staticStartTime = atoi(vecResult[8].c_str());
}
}
std::string strnodeinex = sql_ctl->GetData(T_DATANODE_TIME(TNAME), "nodeindex", "nodeindex > 0 order by nodeindex desc LIMIT 0 , 1");
LOG_INFO("DealNodeSendTime strnodeinex = %s\n",strnodeinex.c_str());
nodeindex = atoi(strnodeinex.c_str())+1;
sprintf(insertSql,"'%s','%s','%d','%d',%d,%d,%d,%d,%d",dataNodeInfo.ZigbeeLongAddr.c_str(),dataNodeInfo.ZigbeeShortAddr.c_str(),\
dataNodeInfo.FeatureInterVal,dataNodeInfo.WaveInterVal,nodegroup,nodeindex,nodewaveindex,statictime,staticStartTime);
sql_ctl->InsertData(T_DATANODE_TIME(TNAME), insertSql);
LOG_INFO("DealNodeSendTime InsertData = %s\n",insertSql);
UpdateWirelessNodeTime(shortaddr,nodewaveindex,staticStartTime,nodeindex);//更新时间戳
}else{
sprintf(whereCon,"staticcycle = '%d' and wavecycle = '%d' order by nodeindex desc LIMIT 0 , 1",
dataNodeInfo.FeatureInterVal,dataNodeInfo.WaveInterVal);
vec_t vecResult = sql_ctl->GetDataSingleLine(T_DATANODE_TIME(TNAME),"*",whereCon);
if(vecResult.size() == 0){
string strResult = sql_ctl->GetData(T_DATANODE_TIME(TNAME),"nodegroup"," nodegroup > 0 order by nodeindex desc LIMIT 0 , 1");
if(atoi(strResult.c_str()) > 0){
nodegroup = atoi(strResult.c_str()) + 1;
}else{
nodegroup = 1;
}
staticStartTime = 0;
statictime = 0;
memset(whereCon,0x00,sizeof(whereCon));
sprintf(whereCon,"dataNodeNo = '%s'",dataNodeInfo.ZigbeeLongAddr.c_str());
sprintf(updateSql," staticcycle = '%d',wavecycle = '%d',nodegroup = %d,nodewaveindex = %d,statictime = %d, staticstarttime = %d",dataNodeInfo.FeatureInterVal,dataNodeInfo.WaveInterVal,\
nodegroup,nodewaveindex,statictime,staticStartTime);
sql_ctl->UpdateTableData(T_DATANODE_TIME(TNAME), updateSql, whereCon);
LOG_INFO("DealNodeSendTime updateSql = %s\n",updateSql);
LOG_DEBUG("DealNodeSendTime1");
UpdateWirelessNodeTime(shortaddr,nodewaveindex,staticStartTime,nodeindex);//更新时间戳
}else{
print_info("=======Send Time======\n");
LOG_DEBUG("DealNodeSendTime2");
UpdateWirelessNodeTime(shortaddr,atoi(vecResult[6].c_str()),atoi(vecResult[8].c_str()),atoi(vecResult[5].c_str()));//更新时间戳
}
}
memset(whereCon,0x00,sizeof(whereCon));
sprintf(whereCon,"dataNodeNo = '%s' and staticcycle <> '%d' and wavecycle <> '%d'",dataNodeInfo.ZigbeeLongAddr.c_str(),\
dataNodeInfo.FeatureInterVal,dataNodeInfo.WaveInterVal);
sql_ctl->DeleteTableData(T_DATANODE_TIME(TNAME),whereCon);*/
}
bool Uart::CheckCrc(char* pCheckBuff,int No)
{
unsigned char tmp = 0x00;
for(int i = 0 ; i < No;i++){
tmp += (unsigned char)pCheckBuff[i];
// printf("%02x ",pCheckBuff[i]);
}
if((unsigned char)pCheckBuff[No] != (unsigned char)tmp)
return false;
return true;
2021-09-18 13:45:24 +08:00
}
void Uart::modify_distaddr_info(unsigned short id, char * zigbee,unsigned char* distAddr)
{
2024-07-09 09:49:42 +08:00
char command[6] = {0x00};
command[0] = 0xDE;
command[1] = 0xDF;
command[2] = 0xEF;
command[3] = 0xDC;
command[4] = ((char *)&id)[1];
command[5] = ((char *)&id)[0];
WriteToUart(command, 6);
usleep(10000);
memset(command,0x00,sizeof(command));
command[0] = 0xDE;
command[1] = 0xDF;
command[2] = 0xEF;
command[3] = 0xD2;
command[4] = distAddr[0];
command[5] = distAddr[1];
WriteToUart(command, 6);
}
void Uart::getZigbeeSignal(unsigned char* distAddr)
{
usleep(10000);
char command[6] = {0x00};
command[0] = 0xDE;
command[1] = 0xDF;
command[2] = 0xEF;
command[3] = 0xDA;
command[4] = distAddr[0];
command[5] = distAddr[1];
WriteToUart(command, 6);
}
void Uart::modify_DistAddr(unsigned char* distAddr)
{
char command[6] = {0x00};
memset(command,0x00,sizeof(command));
command[0] = 0xDE;
command[1] = 0xDF;
command[2] = 0xEF;
command[3] = 0xD2;
command[4] = distAddr[0];
command[5] = distAddr[1];
WriteToUart(command, 6);
2024-07-09 09:49:42 +08:00
}
void Uart::modify_LocalAddr(unsigned short id)
{
char command[6] = {0x00};
command[0] = 0xDE;
command[1] = 0xDF;
command[2] = 0xEF;
command[3] = 0xDC;
command[4] = ((char *)&id)[1];
command[5] = ((char *)&id)[0];
WriteToUart(command, 6);
// ReadFromUart();
}
void Uart::modify_Localchannel(unsigned char pad)
{
char command[6] = {0x00};
command[0] = 0xDE;
command[1] = 0xDF;
command[2] = 0xEF;
command[3] = 0xD1;
command[4] = pad & 0xff;
WriteToUart(command, 5);
// ReadFromUart();
}
void Uart::modify_LocalPanID(unsigned short padID)
{
char command[6] = {0x00};
command[0] = 0xDE;
command[1] = 0xDF;
command[2] = 0xEF;
command[3] = 0xDB;
command[4] = ((char *)&padID)[1];
command[5] = ((char *)&padID)[0];
WriteToUart(command, 6);
// ReadFromUart();
2021-09-18 13:45:24 +08:00
}
2021-09-18 13:45:24 +08:00
void Uart::modify_info(unsigned short id, char * zigbee)
{
int i, j, ret,con;
char command[100];
char command1[20];
char tmp = 0;
command[0] = 0xab;
command[1] = 0xbc;
command[2] = 0xcd;
command[3] = 0xd6;
2024-07-09 09:49:42 +08:00
command[4] = ((char *)&id)[0];
command[5] = ((char *)&id)[1];
2021-09-18 13:45:24 +08:00
command[6] = 0;
if (zigbee != NULL) {
con = 71;
memcpy(&(command[6]), zigbee + 4, 65);
} else {
con = 6;
}
for(i = 0; i < con; i++)
{
tmp += command[i];
}
2024-07-09 09:49:42 +08:00
command[i] = 0xaa;
2021-09-18 13:45:24 +08:00
WriteToUart(command, i+1);
2024-07-09 09:49:42 +08:00
sleep(1);
2021-09-18 13:45:24 +08:00
}
void Uart::zigbee_reset(unsigned short pad, unsigned short type)
{
char command[10],tmp = 0,i;
command[0] = 0xab;
command[1] = 0xbc;
command[2] = 0xcd;
command[3] = 0xd9;
command[4] = ((char *)&pad)[1];
command[5] = ((char *)&pad)[0];
command[6] = ((char *)&type)[1];
command[7] = ((char *)&type)[0];
for(i = 0; i<8; i++)
{
tmp += command[i];
}
command[8] = tmp;
WriteToUart(command, 9);
}
void Uart::WriteChanl2Zigbee(unsigned char pad)
{
print_info("WriteChanl2Zigbee : %d\n", pad);
unsigned char pad1 = pad;
unsigned short tmp;
tmp = GlobalConfig::Zigbee_G.MyAddr;
//swap((char *)&pad1);
2024-07-09 09:49:42 +08:00
//swap((char *)&tmp);
2021-09-18 13:45:24 +08:00
GlobalConfig::Zigbee_G.Chan = pad1;
modify_info(tmp, (char *)& GlobalConfig::Zigbee_G);
2024-07-09 09:49:42 +08:00
gpio_set(GlobalConfig::GPIO_G.zigReset,0);
usleep(10000);
gpio_set(GlobalConfig::GPIO_G.zigReset,1);
2021-09-18 13:45:24 +08:00
}
void Uart::WritePanId2Zigbee(unsigned short pad)
{
print_info("WritePanId2Zigbee : %d\n", pad);
unsigned short pad1 = pad,tmp;
tmp = GlobalConfig::Zigbee_G.MyAddr;
2024-07-09 09:49:42 +08:00
print_info("MyAddr : %d\n", GlobalConfig::Zigbee_G.MyAddr);
2021-09-18 13:45:24 +08:00
swap((char *)&pad1);
2024-07-09 09:49:42 +08:00
//swap((char *)&tmp);
2021-09-18 13:45:24 +08:00
GlobalConfig::Zigbee_G.PanID = pad1;
2024-07-09 09:49:42 +08:00
modify_info(tmp, (char *)&GlobalConfig::Zigbee_G);
2021-09-18 13:45:24 +08:00
2024-07-09 09:49:42 +08:00
gpio_set(GlobalConfig::GPIO_G.zigReset,0);
usleep(10000);
gpio_set(GlobalConfig::GPIO_G.zigReset,1);
}
void Uart::WriteSpeed2Zigbee()
{
GlobalConfig::Zigbee_G.Serial_Rate = 0x07;
GlobalConfig::Zigbee_G.Serial_DataB = 0x08;
GlobalConfig::Zigbee_G.Serial_StopB = 0x01;
unsigned short tmp;
tmp = GlobalConfig::Zigbee_G.MyAddr;
modify_info(tmp, (char *)&GlobalConfig::Zigbee_G);
gpio_set(GlobalConfig::GPIO_G.zigReset,0);
usleep(10000);
gpio_set(GlobalConfig::GPIO_G.zigReset,1);
}
/*void Uart::WriteLocalAddr(unsigned short id)
{
gpio_set(116,0);
usleep(10000);
gpio_set(116,1);
sleep(1);
char command[8]={0x00};
command[0] = 0xab;
command[1] = 0xbc;
command[2] = 0xcd;
command[3] = 0xdc;
command[4] = ((char *)&id)[1];
command[5] = ((char *)&id)[0];
command[6] = 0x00;
command[7] = 0xaa;
WriteToUart(command, 8);
usleep(600000);
gpio_set(116,0);
usleep(10000);
gpio_set(116,1);
}*/
void Uart::WriteTranTimeout2Zigbee(unsigned char Time)
{
print_info("WriteTranTimeout2Zigbee : %d\n", Time);
unsigned short tmp = GlobalConfig::Zigbee_G.MyAddr;
//print_info("MyAddr : %d\n", GlobalConfig::Zigbee_G.MyAddr);
GlobalConfig::Zigbee_G.PowerLevel = 0x03;
GlobalConfig::Zigbee_G.RetryNum = 0x64;
GlobalConfig::Zigbee_G.TranTimeout = Time;
modify_info(tmp, (char *)&GlobalConfig::Zigbee_G);
gpio_set(GlobalConfig::GPIO_G.zigReset,0);
usleep(10000);
gpio_set(GlobalConfig::GPIO_G.zigReset,1);
}
2021-09-18 13:45:24 +08:00
void Uart::WriteShortAddr2Zigbee(unsigned short pad)
{
print_info("WriteShortAddr2Zigbee : %4x\n", (unsigned short)pad);
unsigned short pad1 = pad,tmp;
tmp = GlobalConfig::Zigbee_G.MyAddr;
2024-07-09 09:49:42 +08:00
//swap((char *)&pad1);
//swap((char *)&tmp);
2021-09-18 13:45:24 +08:00
GlobalConfig::Zigbee_G.MyAddr = pad1;
modify_info(tmp, (char *)& GlobalConfig::Zigbee_G);
2024-07-09 09:49:42 +08:00
gpio_set(GlobalConfig::GPIO_G.zigReset,0);
usleep(10000);
gpio_set(GlobalConfig::GPIO_G.zigReset,1);
sleep(1);
2021-09-18 13:45:24 +08:00
}
void Uart::WriteShortAddr_DistAddr2Zigbee(unsigned short pad,unsigned char* pDestShortAddr)
2021-09-18 13:45:24 +08:00
{
print_info("WriteShortAddr2Zigbee : %4x\n", (unsigned short)pad);
unsigned short pad1 = pad,tmp;
tmp = GlobalConfig::Zigbee_G.MyAddr;
swap((char *)&pad1);
swap((char *)&tmp);
char tmpDest[8]={0x00};
2024-07-09 09:49:42 +08:00
sprintf(tmpDest,"%02x%02x",pDestShortAddr[0],pDestShortAddr[1]);
memcpy(&GlobalConfig::Zigbee_G.DstAddr,pDestShortAddr,2);
GlobalConfig::Zigbee_G.MyAddr = pad1;
// GlobalConfig::Zigbee_G.DstAddr = (short)atoi((char*)tmpDest);
2024-07-09 09:49:42 +08:00
print_info("DstAddr = %x\n",GlobalConfig::Zigbee_G.DstAddr);
GlobalConfig::ZigbeeInfo_G.MyAddr = "9999";
modify_info(tmp, (char *)& GlobalConfig::Zigbee_G);
2024-07-09 09:49:42 +08:00
gpio_set(GlobalConfig::GPIO_G.zigReset,0);
usleep(10000);
gpio_set(GlobalConfig::GPIO_G.zigReset,1);
}
2021-09-18 13:45:24 +08:00
void Uart::ZigbeeInit()
{
2024-07-09 09:49:42 +08:00
std::string strPanId = sql_ctl->GetData("t_gateway_info","zigbeePanID",NULL);
{
unsigned short shortAddr = 0x8888;
//GlobalConfig::Zigbee_G.MyAddr = shortAddr;
//WriteLocalAddr(shortAddr);
WriteShortAddr2Zigbee(shortAddr);
usleep(100000);
// 更新GlobalConfig::ZigbeeInfo_G.MyAddr用于外部显示
}
//std::string strPanId = GlobalConfig::MacAddr_G.substr(8);
2021-09-18 13:45:24 +08:00
print_info("strPanId : %s\n", strPanId.c_str());
2024-07-09 09:49:42 +08:00
print_info("MacAddr_G : %s\n", GlobalConfig::MacAddr_G.c_str());
2021-09-18 13:45:24 +08:00
// 新增管理ZigBee代码
std::string strchan = ReadStrByOpt(ZIGBEECONFIG, "Zigbee", "channel");
2024-07-09 09:49:42 +08:00
unsigned short Chan = (unsigned short)strtol(strchan.c_str(), NULL, 10);
print_info("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%the chan = %u\n",Chan);
2021-09-18 13:45:24 +08:00
print_info("ZigbeeInfo_G.Channel=%d\n",GlobalConfig::ZigbeeInfo_G.Channel);
if(Chan > 10 && Chan < 27)
{
2024-07-09 09:49:42 +08:00
//if (Chan != GlobalConfig::ZigbeeInfo_G.Channel)
{
WriteChanl2Zigbee(Chan);
//modify_Localchannel(Chan);
usleep(100000);
}
2021-09-18 13:45:24 +08:00
}
2024-07-09 09:49:42 +08:00
print_info("PanID1 = %s,strPanId = %s\n",GlobalConfig::ZigbeeInfo_G.PanID.c_str(),strPanId.c_str());
//if (0 != GlobalConfig::ZigbeeInfo_G.PanID.compare(strPanId.c_str()))
{
2021-09-18 13:45:24 +08:00
long lShortAddr = strtol(strPanId.c_str(), NULL, 16);
unsigned short panid = lShortAddr & 0xffff;
2024-07-09 09:49:42 +08:00
//modify_LocalPanID(panid);
2021-09-18 13:45:24 +08:00
2024-07-09 09:49:42 +08:00
WritePanId2Zigbee(panid);
usleep(100000);
2021-09-18 13:45:24 +08:00
}
2024-07-09 09:49:42 +08:00
//if (0 != GlobalConfig::ZigbeeInfo_G.MyAddr.compare("8888"))
2021-09-18 13:45:24 +08:00
print_info("ZigbeeInfo_G.MyAddr=%s\n",GlobalConfig::ZigbeeInfo_G.MyAddr.c_str());
}