4501 lines
156 KiB
C++
4501 lines
156 KiB
C++
#include <signal.h>
|
||
#include <stdio.h>
|
||
#include <fcntl.h>
|
||
#include <unistd.h>
|
||
#include <termios.h>
|
||
#include <sys/types.h>
|
||
#include <sys/stat.h>
|
||
#include <boost/thread/thread.hpp>
|
||
#include <boost/lexical_cast.hpp>
|
||
#include <boost/algorithm/string.hpp>
|
||
#include <boost/bind.hpp>
|
||
#include <boost/timer.hpp>
|
||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||
#include "../uart/SH_Uart.hpp"
|
||
#include "../serial/serial.h"
|
||
|
||
|
||
namespace{
|
||
Uart *pUart = Uart::instance();
|
||
Calculation *pCalculation = Calculation::instance();
|
||
}
|
||
char g_UartRecvBuf[GENERAL_BUF_SIZE];
|
||
int offSize = 0;
|
||
pTestRecvCallBack pTestRecv ;
|
||
std::vector<RecvData> g_VecWaveDataX;
|
||
std::vector<RecvData> g_VecWaveDataY;
|
||
std::vector<RecvData> g_VecWaveDataZ;
|
||
map<string,compressWaveChannel> g_mapCompress;
|
||
// 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;
|
||
|
||
int Uart::UartRecv(int fd, char srcshow,char* buffer)
|
||
{
|
||
char buff[BUF_LENGTH];
|
||
int ret = 0;
|
||
int maxSize = 0;
|
||
int offSize = 0;
|
||
int timeoutflag = 0;
|
||
char head[] = {0xAA,0x55,0xAA};
|
||
char szbuffer[BUF_LENGTH]={0x00};
|
||
while(1)
|
||
{
|
||
|
||
if((unsigned short)GlobalConfig::Zigbee_G.MyAddr == 0x9999){
|
||
|
||
memset(buff, 0, sizeof(buff));
|
||
ret = read_data(fd, buff, BUF_LENGTH, 10);
|
||
if (ret <= 0 ){
|
||
if(!bUpdate && !bUpdateconfig && GlobalConfig::EnterZigBeeWaveTransmittingCnt_G > 15){
|
||
timeoutflag ++;
|
||
if(timeoutflag > 300){
|
||
LOG_DEBUG("===============0x9999 timeout= %d offSize = %d===============\n",timeoutflag,offSize);
|
||
print_info("0x9999 timeout %d===============Size = %d\n",timeoutflag,offSize);
|
||
FindRecvPackage(offSize, mUartRecvTmpBuf,head);
|
||
GlobalConfig::Zigbee_G.MyAddr = 0x8888;
|
||
timeoutflag = 0;
|
||
offSize = 0;
|
||
maxSize = 0;
|
||
tcflush(fd,TCIFLUSH);
|
||
bModifyAddr = true;
|
||
modify_LocalAddr(0x8888);
|
||
bSendTimeStamp = false;
|
||
mssleep(10000);
|
||
m_waveTrans = true;
|
||
memset(mUartRecvTmpBuf,0,BUF_LENGTH);
|
||
GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = NO_ENTER_TRANSMITTING_STATUS;
|
||
GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0;
|
||
|
||
LOG_DEBUG("wave end\n");
|
||
}
|
||
mssleep(10000);
|
||
}else if(bUpdatePre || (bUpdateconfig && GlobalConfig::EnterZigBeeWaveTransmittingCnt_G > 15)){
|
||
timeoutflag ++;
|
||
if(timeoutflag > 300){
|
||
print_info("bUpdateconfig %d===============\n",timeoutflag);
|
||
GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = NO_ENTER_TRANSMITTING_STATUS;
|
||
GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0;
|
||
timeoutflag = 0;
|
||
offSize = 0;
|
||
maxSize = 0;
|
||
bUpdate = false;
|
||
bUpdatePre = false;
|
||
bUpdateconfig = false;
|
||
bModifyAddr = true;
|
||
bSendTimeStamp = false;
|
||
modify_LocalAddr(0x8888);
|
||
mssleep(10000);
|
||
GlobalConfig::Zigbee_G.MyAddr = 0x8888;
|
||
}
|
||
mssleep(10000);
|
||
}
|
||
}
|
||
else if(ret > 0){
|
||
maxSize += ret;
|
||
// 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;
|
||
}
|
||
//print_debug("offSize = %d\n",offSize);
|
||
}
|
||
}
|
||
|
||
}else if((unsigned short)GlobalConfig::Zigbee_G.MyAddr == 0x8888){
|
||
#ifdef IMX6UL_GATEWAY
|
||
memset(buff, 0, sizeof(buff));
|
||
ret = read_data(fd, buff, BUF_LENGTH, 50);
|
||
if (ret <= 0){
|
||
continue;
|
||
}
|
||
if (srcshow) {
|
||
print_info("0x8888 ===str_recv===,ret = %d\n",ret);
|
||
for(int i = 0; i < ret;i++){
|
||
print_debug("[%02x]", buff[i]&0xff);
|
||
}
|
||
printf("\n");
|
||
FindRecvPackage(ret, buff,head);
|
||
|
||
}
|
||
#else if G2UL_GATEWAY
|
||
memset(buff, 0x00, sizeof(buff));
|
||
ret = read_data(fd, buff, BUF_LENGTH, 50);
|
||
if (ret <= 0 ){
|
||
timeoutflag ++;
|
||
if(timeoutflag > 5){
|
||
|
||
FindRecvPackage(offSize, szbuffer,(char*)head);
|
||
memset(szbuffer,0x00,sizeof(szbuffer));
|
||
timeoutflag = 0;
|
||
offSize = 0;
|
||
maxSize = 0;
|
||
mssleep(10000);
|
||
}
|
||
}else if(ret > 0){
|
||
maxSize += ret;
|
||
print_debug("0x8888==str_recv===,ret = %d offSize = %d\n",ret,maxSize);
|
||
for(int i = 0; i < ret;i++){
|
||
print_debug("%02x ", buff[i]&0xff);
|
||
}
|
||
print_debug("\n");
|
||
timeoutflag = 0;
|
||
m_TimeStamp = 0;
|
||
memcpy(szbuffer + offSize,buff,ret);
|
||
offSize = offSize + ret;
|
||
}
|
||
#endif
|
||
}
|
||
//mssleep(50);
|
||
}
|
||
}
|
||
|
||
Uart::Uart():mUart(mIoSev),mStrand(mIoSev)
|
||
{
|
||
mRdLength = 0;
|
||
m_TimeStamp = 0;
|
||
mlastSize = 0;
|
||
fd = 0;
|
||
waittime = 0;
|
||
mPackgeIndex = -1;
|
||
bUpdate = false;
|
||
m_strDestShortAddr = "";
|
||
memset(mUartRecvBuf,0,BUF_LENGTH);
|
||
memset(mUartRecvTmpBuf,0,BUF_LENGTH);
|
||
bTest = false;
|
||
TestFd = 0;
|
||
DataNodeUpdateFile = "";
|
||
strTimetamp = "";
|
||
bZigbeeSinal = false;
|
||
bModifyAddr = false;
|
||
bUpdatePre = false;
|
||
bSendTimeStamp = false;
|
||
m_waveCountX = 0;
|
||
m_waveCountY = 0;
|
||
m_waveCountZ = 0;
|
||
VecWaveDataX.reserve(300000);
|
||
VecWaveDataY.reserve(300000);
|
||
VecWaveDataZ.reserve (500000);
|
||
}
|
||
Uart::~Uart()
|
||
{
|
||
close(fd);
|
||
close(TestFd);
|
||
if (mUart.is_open())
|
||
mUart.close();
|
||
}
|
||
|
||
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)
|
||
{
|
||
|
||
#ifdef G2UL_GATEWAY
|
||
fd = config_uart("/dev/ttySC2",speed);
|
||
#endif
|
||
#ifdef IMX6UL_GATEWAY
|
||
fd = config_uart("/dev/ttymxc4",speed);
|
||
#endif
|
||
print_info("InitUart fd = %d\n",fd);
|
||
if(fd < 0){
|
||
printf("config_uart error\n");
|
||
}
|
||
|
||
}
|
||
void Uart::Close()
|
||
{
|
||
close(fd);
|
||
fd = 0;
|
||
}
|
||
void Uart::InitZigbee()
|
||
{
|
||
|
||
InitUart(B115200);
|
||
UpdateZigbeeInfoCtrl();
|
||
sleep(2);
|
||
int iRet = ReadFromUart();
|
||
if(iRet <= 0)
|
||
{
|
||
Close();
|
||
sleep(2);
|
||
print_info("Speed error\n");
|
||
InitUart(B57600);
|
||
UpdateZigbeeInfoCtrl();
|
||
sleep(2);
|
||
ReadFromUart();
|
||
print_info("modify speed\n");
|
||
WriteSpeed2Zigbee();
|
||
ReadFromUart();
|
||
}
|
||
Close();
|
||
sleep(1);
|
||
}
|
||
void TestRecv(int status)
|
||
{
|
||
char szStatus[10]={0x00};
|
||
sprintf(szStatus,"%d",status);
|
||
write_data(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);
|
||
}
|
||
}
|
||
mssleep(20000);
|
||
}
|
||
}
|
||
|
||
int Uart::ZigbeeTest()
|
||
{
|
||
char buff[BUF_LENGTH] = {0x00};
|
||
modify_Localchannel(22);
|
||
mssleep(100000);
|
||
|
||
modify_LocalPanID(2222);
|
||
|
||
mssleep(100000);
|
||
modify_LocalAddr(6666);
|
||
|
||
mssleep(100000);
|
||
return 0;
|
||
}
|
||
|
||
void Uart::WriteToUart(const char *strSend,int pLen)
|
||
{
|
||
//#ifdef ZIGBEE_TEST
|
||
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");
|
||
}
|
||
//#endif
|
||
/* if (mUart.is_open()) {
|
||
mUart.async_write_some(buffer(strSend,pLen),
|
||
mStrand.wrap(
|
||
boost::bind(&Uart::WriteHandle,this,strSend,
|
||
boost::asio::placeholders::error,
|
||
boost::asio::placeholders::bytes_transferred))
|
||
);
|
||
}*/
|
||
int len = write_data(fd,(char*)strSend,pLen);
|
||
// printf("len = %d\n",len);
|
||
// ReadFromUart();
|
||
}
|
||
|
||
int Uart::ReadFromUart()
|
||
{
|
||
|
||
/* if (mUart.is_open()) {
|
||
print_info("async_read_some,wait......................................\n");
|
||
mUart.async_read_some(buffer(&mUartRecvBuf[mRdLength], BUF_LENGTH-mRdLength-offSize),
|
||
mStrand.wrap(
|
||
boost::bind(&Uart::ReadHandle,this,
|
||
boost::asio::placeholders::error,
|
||
boost::asio::placeholders::bytes_transferred))
|
||
);
|
||
}*/
|
||
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);
|
||
}*/
|
||
}
|
||
|
||
void Uart::Run()
|
||
{
|
||
mIoSev.run();
|
||
}
|
||
|
||
void Uart::Stop()
|
||
{
|
||
if (mUart.is_open())
|
||
mUart.close();
|
||
mIoSev.stop();
|
||
}
|
||
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);
|
||
GlobalConfig::Zigbee_G.MyAddr = 0x8888;
|
||
tcflush(fd,TCIOFLUSH);
|
||
sleep(1);
|
||
modify_LocalAddr(0x8888);
|
||
sleep(1);
|
||
bModifyAddr = true;
|
||
bSendTimeStamp = false;
|
||
GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = NO_ENTER_TRANSMITTING_STATUS;
|
||
GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0;
|
||
//std::vector<RecvData>().swap(m_VecWaveData);
|
||
break;
|
||
}
|
||
|
||
if(command == 32){
|
||
LOG_INFO("bUpdatepackge 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;
|
||
GlobalConfig::Zigbee_G.MyAddr = 0x8888;
|
||
tcflush(fd,TCIOFLUSH);
|
||
sleep(1);
|
||
modify_LocalAddr(0x8888);
|
||
sleep(1);
|
||
bModifyAddr = true;
|
||
bSendTimeStamp = false;
|
||
GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = NO_ENTER_TRANSMITTING_STATUS;
|
||
GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0;
|
||
break;
|
||
}
|
||
bUpdate = true;
|
||
m_TimeStamp = 0;
|
||
DealRecvData(RecvBuf);
|
||
break;
|
||
}else if(!bUpdatePre && !bUpdateconfig && (command == 3 || command == 4 || command == 5)){
|
||
if(!CheckCrc(&UartRecvBuf[i],99)){
|
||
m_TimeStamp = 0;
|
||
mPackgeIndex = -1;
|
||
LOG_INFO("CheckCrc error ShortAddr :%s command = %d\n",strShortAddr.c_str(),command);
|
||
print_error("CheckCrc error ShortAddr :%s \n",strShortAddr.c_str());
|
||
char tmp[10]={0x00};
|
||
char tmp2[10]={0x00};
|
||
for(int j = 0; j < 100;j++){
|
||
sprintf(tmp,"%02x ",UartRecvBuf[i + j] & 0xff);
|
||
strcat(tmp2,tmp);
|
||
}
|
||
LOG_ERROR("error str = %s\n",tmp2);
|
||
print_info("\n");
|
||
GlobalConfig::Zigbee_G.MyAddr = 0x8888;
|
||
tcflush(fd,TCIOFLUSH);
|
||
sleep(1);
|
||
modify_LocalAddr(0x8888);
|
||
bModifyAddr = true;
|
||
sleep(1);
|
||
bSendTimeStamp = false;
|
||
GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = NO_ENTER_TRANSMITTING_STATUS;
|
||
GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0;
|
||
m_waveCountX = 0;
|
||
m_waveCountY = 0;
|
||
m_waveCountZ = 0;
|
||
g_VecWaveDataX.clear();
|
||
g_VecWaveDataY.clear();
|
||
g_VecWaveDataZ.clear();
|
||
//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,command);
|
||
}else if(!bUpdate && !bUpdateconfig && (command == 1 || command == 2 || command == 6 || command == 7)){
|
||
char RecvBuf[100] = {0x00};
|
||
memcpy(RecvBuf,&UartRecvBuf[i],100);
|
||
if(!CheckCrc(RecvBuf,99)){
|
||
LOG_INFO("CheckCrc error ShortAddr :%s command = %d \n",strShortAddr.c_str(),command);
|
||
break;
|
||
}
|
||
DealRecvData(RecvBuf);
|
||
|
||
}else if(bUpdate && !bUpdateconfig && (command == 1 || command == 2 || command == 6 || command == 7)){
|
||
|
||
print_info("m_strDestShortAddr = %s,strShortAddr = %s,waittime = %d\n",\
|
||
m_strDestShortAddr.c_str(),strShortAddr.c_str(),waittime);
|
||
//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){
|
||
mssleep(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
|
||
sprintf(updateSql, "UpdateFlag = UpdateFlag + 1");
|
||
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());
|
||
GlobalConfig::Zigbee_G.MyAddr = 0x8888;
|
||
bUpdateconfig = false;
|
||
mPackgeIndex = -1;
|
||
tcflush(fd,TCIOFLUSH);
|
||
sleep(1);
|
||
modify_LocalAddr(0x8888);
|
||
bModifyAddr = true;
|
||
bSendTimeStamp = false;
|
||
sleep(1);
|
||
|
||
GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = NO_ENTER_TRANSMITTING_STATUS;
|
||
GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0;
|
||
break;
|
||
}
|
||
break;
|
||
}else if(command == 35){
|
||
//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);
|
||
}
|
||
}
|
||
|
||
}
|
||
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
|
||
//LOG_INFO("command = %d,zigbeeShortAddr = %s , signal = %02d,strTimetamp = %s\n",command,m_strDestShortAddr.c_str(),UartRecvBuf[i+6]&0xFF,strTimetamp.c_str());
|
||
char whereCon[1024] = {0};
|
||
char updateSql[1024] = { 0 };
|
||
char tableName[100]={0x00};
|
||
bZigbeeSinal = false;
|
||
sprintf(whereCon, "zigbeeShortAddr='%s'", m_strDestShortAddr.c_str());
|
||
vec_t vecDataNodeNo = sql_ctl->GetDataSingleLine(T_SENSOR_INFO(TNAME), " dataNodeNo,LooseValue,RSSI ", whereCon);
|
||
memset(whereCon,0x00,sizeof(whereCon));
|
||
sprintf(updateSql, "zigbeeSignal = '%02d' ",UartRecvBuf[i+6]&0xFF);//zigbeeRSSIType = 1 网关获取传感器信号强度
|
||
sprintf(whereCon, "dataNodeNo='%s' and timeStamp = '%s'", (char*)vecDataNodeNo[0].c_str(),strTimetamp.c_str());
|
||
sprintf(tableName,"t_dataStatic_%s",(char*)vecDataNodeNo[0].c_str());
|
||
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";
|
||
}
|
||
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);
|
||
|
||
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)
|
||
{
|
||
/*try{
|
||
if (ec) {
|
||
mRdLength = 0;
|
||
ReadFromUart();
|
||
return ;
|
||
}
|
||
else*/
|
||
{ //长度检测
|
||
char UartRecvBuf[BUF_LENGTH]={0x00};
|
||
memcpy(UartRecvBuf,pUartRecvBuf,bytesRead);
|
||
print_info("recv uart len in ZigBee short address %4x : %d\n recv uart data:",
|
||
(unsigned short)GlobalConfig::Zigbee_G.MyAddr, bytesRead);
|
||
// for ( int i = 0; i < bytesRead; i++)
|
||
// print_debug("%02X ",mUartRecvBuf[i]&0xFF);
|
||
|
||
int iPackageSize = bytesRead / 100;
|
||
if (0 == iPackageSize) {
|
||
iPackageSize = 1;
|
||
}
|
||
int Count = bytesRead;
|
||
/*每次看到这块代码时总感觉不妥,因为bytesRead是100的整数倍时对结果不影响;如果不是,则会造成数据丢失(例如1024时,iPackageSize的值
|
||
为10,这样就丢失了24字节的数据)。况且一个传感器与无线网关通信数据量对 无线网关的处理影响不大,但是如果是多个传感器都在和无线网关
|
||
通信,数据量处理不好就会产生很大的影响。我的逻辑:利用while循环,条件是判断bytesRead是否大于0,如果条件满足,再取数据包的前3字节,
|
||
判断命令码是ABBCCD还是AA55AA,如果是ABBCCD就处理74字节数据,如果是AA55AA就处理100自己数据,此时存放数据的buf减去处理的大小数据,
|
||
然后进入下一次while循环。*/
|
||
|
||
if((unsigned short)GlobalConfig::Zigbee_G.MyAddr == (unsigned short)0x9999){
|
||
|
||
}else{
|
||
for (int j = 0; j < iPackageSize; j++) {
|
||
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;
|
||
}
|
||
|
||
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){
|
||
mssleep(100000);
|
||
waittime ++;
|
||
return ;
|
||
}
|
||
}
|
||
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());
|
||
}
|
||
|
||
if ( 0 == strHeadFlag.compare("AA55AA") ) {
|
||
DealRecvData(mUartRecvPackage);
|
||
}
|
||
}}
|
||
|
||
}
|
||
/* //读串口循环
|
||
ReadFromUart();
|
||
} catch(...) {
|
||
print_error("PlatFormInit exception happend.\n");
|
||
// LOG_ERROR("%s,%d\n", __FUNCTION__, __LINE__);
|
||
}*/
|
||
}
|
||
|
||
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);
|
||
}
|
||
}
|
||
|
||
|
||
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()
|
||
{
|
||
/* char buf[5] = {0xAB, 0xBC, 0xCD, 0xD1, 0x05};
|
||
WriteToUart(buf, 5);
|
||
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);*/
|
||
}
|
||
|
||
void Uart::UpdateZigbeeInfo(const char *pData)
|
||
{
|
||
|
||
print_info("update gateway zigbee info\n");
|
||
GlobalConfig::Zigbee_G = *((ZIGBEE *)pData);
|
||
char buff[8];
|
||
sprintf(buff, "%02d", pData[36]&0xFF);
|
||
GlobalConfig::ZigbeeInfo_G.DevMode = atoi(buff);
|
||
|
||
memset(buff, 0, 8);
|
||
sprintf(buff, "%02d", pData[37]&0xFF);
|
||
GlobalConfig::ZigbeeInfo_G.Channel = atoi(buff);
|
||
|
||
memset(buff, 0, 8);
|
||
sprintf(buff, "%02x%02x", pData[38]&0xFF, pData[39]&0xFF);
|
||
GlobalConfig::ZigbeeInfo_G.PanID = std::string(buff);
|
||
|
||
memset(buff, 0, 8);
|
||
sprintf(buff, "%02x%02x", pData[40]&0xFF, pData[41]&0xFF);
|
||
GlobalConfig::ZigbeeInfo_G.MyAddr = std::string(buff);
|
||
|
||
|
||
memset(buff, 0, 8);
|
||
sprintf(buff, "%02x%02x", pData[50]&0xFF, pData[51]&0xFF);
|
||
GlobalConfig::ZigbeeInfo_G.DstAddr = std::string(buff);
|
||
|
||
memset(buff, 0, 8);
|
||
sprintf(buff, "%d", pData[62]);
|
||
GlobalConfig::ZigbeeInfo_G.RetryNum = std::string(buff);
|
||
|
||
memset(buff, 0, 8);
|
||
sprintf(buff, "%d", pData[63]);
|
||
GlobalConfig::ZigbeeInfo_G.TranTimeout = std::string(buff);
|
||
|
||
LOG_INFO("local zigbee module info Mode : %d Chan : %d PanID : %s MyAddr : %s DstAddr : %s,RetryNum:%s,TranTimeout:%s\n", GlobalConfig::ZigbeeInfo_G.DevMode, GlobalConfig::ZigbeeInfo_G.Channel,
|
||
GlobalConfig::ZigbeeInfo_G.PanID.c_str(),GlobalConfig::ZigbeeInfo_G.MyAddr.c_str(), GlobalConfig::ZigbeeInfo_G.DstAddr.c_str(),GlobalConfig::ZigbeeInfo_G.RetryNum.c_str(),GlobalConfig::ZigbeeInfo_G.TranTimeout.c_str());
|
||
// 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);
|
||
// mssleep(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);
|
||
// mssleep(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());
|
||
|
||
}
|
||
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;
|
||
std::string strFileName = "",strSoftVersion = "";
|
||
std::vector <DataNodeUpdate> vecDataNodeUpdate;
|
||
vecDataNodeUpdate = ReadStrUpdate("/opt/DataNode/config.json");
|
||
char gethardVersion_sql[32] = { 0 };
|
||
char selectsql[1024]={ 0 };
|
||
sprintf(gethardVersion_sql, "zigbeeShortAddr='%02x%02x'", pDestShortAddr[0],pDestShortAddr[1]);
|
||
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;
|
||
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;
|
||
}
|
||
}
|
||
}
|
||
if(thisindex >= 0 || flag == 0)
|
||
break;
|
||
}
|
||
|
||
if(thisindex < 0)
|
||
return false;
|
||
|
||
// 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;
|
||
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);
|
||
}
|
||
|
||
// 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};
|
||
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];
|
||
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;
|
||
sleep(1);
|
||
WriteToUart((const char*)Data,12);
|
||
int iRet = CheckZigbeeACK();
|
||
if(iRet == 0){
|
||
LOG_DEBUG("Packge ACK send success,shortAddr = %02x%02x\n",pDestShortAddr[0],pDestShortAddr[1]);
|
||
}else{
|
||
LOG_ERROR("Packge ACK send failed,shortAddr = %02x%02x\n",pDestShortAddr[0],pDestShortAddr[1]);
|
||
}
|
||
string strTime = GetLocalTimeWithMs();
|
||
LOG_INFO("ReadUpdatePackge strTime = %s\n",strTime.c_str());
|
||
return true;
|
||
}
|
||
void Uart::UpdateWirelessNode(unsigned short shortAdd)
|
||
{
|
||
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));
|
||
mssleep(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;
|
||
mssleep(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;
|
||
}
|
||
}
|
||
gpio_set(GlobalConfig::GPIO_G.zigAckreset,0);
|
||
mssleep(2000);
|
||
// if(gpio_read(GlobalConfig::GPIO_G.zigAckrep) == 48)
|
||
// gpio_set(GlobalConfig::GPIO_G.zigAckreset,1);
|
||
memset(UpdateData,0x00,sizeof(UpdateData));
|
||
//boost::this_thread::sleep(boost::posix_time::milliseconds(5));
|
||
mssleep(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);
|
||
mssleep(10000);
|
||
if(gpio_read(GlobalConfig::GPIO_G.zigAckrep) == 48)
|
||
gpio_set(GlobalConfig::GPIO_G.zigAckreset,1);
|
||
break;
|
||
}
|
||
mssleep(10000);
|
||
time += 1;
|
||
}while(time < 150);
|
||
if(time >= 150){
|
||
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;
|
||
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");
|
||
}
|
||
|
||
|
||
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")
|
||
{
|
||
// 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");
|
||
LOG_INFO("UpdateConfig\n");
|
||
bUpdateconfig = true;
|
||
unsigned short localAddr = 0x9999;
|
||
GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = ENTER_TRANSMITTING_STATUS;
|
||
GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0;
|
||
// WriteShortAddr_DistAddr2Zigbee(localAddr,pDestShortAddr);//永久参数配置
|
||
char tmpbuf[8] = {0x00};
|
||
sprintf(tmpbuf,"%02x%02x",pDestShortAddr[0],pDestShortAddr[1]);
|
||
m_strDestShortAddr = std::string(tmpbuf);
|
||
//modify_distaddr_info(localAddr,"",pDestShortAddr);//临时参数配置
|
||
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,\
|
||
envelopeBandPass,faultFrequency,timeStamp,viff,ZigbeePower,ZigbeeRetry,MeasurementID,NodeWaveSend");
|
||
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]
|
||
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]=0x01;
|
||
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;
|
||
}
|
||
}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;
|
||
}
|
||
}
|
||
UpdateData[18] = atoi(vecResult[11].c_str()) & 0xFF;;
|
||
UpdateData[19] = atoi(vecResult[12].c_str()) & 0xFF;;
|
||
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=%s\n",vecResult[5].c_str(),vecResult[6].c_str(),vecResult[7].c_str(),vecResult[8].c_str(),vecResult[14].c_str());
|
||
vector<string> vStart,vStop,vEnvelopeBandPass,vfaultFrequency,vNodeWaveSend;
|
||
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 );
|
||
if(vecResult[14] != "")
|
||
boost::split( vNodeWaveSend, vecResult[14], 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;
|
||
|
||
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];
|
||
}
|
||
unsigned char byte = 0;
|
||
if(vNodeWaveSend.size() > 0){
|
||
if (vNodeWaveSend[0] == "0")
|
||
{
|
||
Binary_Bit(&byte,0,0);
|
||
}
|
||
if (vNodeWaveSend[0] == "1")
|
||
{
|
||
Binary_Bit(&byte,0,1);
|
||
}
|
||
if (vNodeWaveSend[1] == "0")
|
||
{
|
||
Binary_Bit(&byte,1,0);
|
||
}
|
||
if (vNodeWaveSend[1] == "1")
|
||
{
|
||
Binary_Bit(&byte,1,1);
|
||
}
|
||
if (vNodeWaveSend[2] == "0")
|
||
{
|
||
Binary_Bit(&byte,2,0);
|
||
}
|
||
if (vNodeWaveSend[2] == "1")
|
||
{
|
||
Binary_Bit(&byte,2,1);
|
||
}
|
||
}
|
||
UpdateData[68] = byte;
|
||
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);
|
||
int iRet = CheckZigbeeACK();
|
||
if(iRet == 0){
|
||
LOG_DEBUG("updataconfig ACK send success,shortAddr = %02x%02x\n",pDestShortAddr[0],pDestShortAddr[1]);
|
||
}else{
|
||
LOG_ERROR("updataconfig ACK send failed,shortAddr = %02x%02x\n",pDestShortAddr[0],pDestShortAddr[1]);
|
||
}
|
||
return 0;
|
||
}else if(vecResultNode[41] == "-1"){
|
||
|
||
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);
|
||
int iRet = CheckZigbeeACK();
|
||
if(iRet == 0){
|
||
LOG_DEBUG("updataname ACK send success,shortAddr = %02x%02x\n",pDestShortAddr[0],pDestShortAddr[1]);
|
||
}else{
|
||
LOG_ERROR("updataname ACK send failed,shortAddr = %02x%02x\n",pDestShortAddr[0],pDestShortAddr[1]);
|
||
}
|
||
return 0;
|
||
|
||
}
|
||
|
||
|
||
|
||
}else{
|
||
return -1;
|
||
}
|
||
|
||
}
|
||
int Uart::UpdateWirelessNodeTime(unsigned char* pDestShortAddr,int modifyaddr/*,int nodewaveindex,int nodetime,int nodeindex*/)
|
||
{
|
||
if(modifyaddr)
|
||
modify_DistAddr(pDestShortAddr);
|
||
mssleep(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);
|
||
int iRet = CheckZigbeeACK();
|
||
if(iRet == 0){
|
||
LOG_DEBUG("NodeTime ACK send success,shortAddr = %02x%02x\n",pDestShortAddr[0],pDestShortAddr[1]);
|
||
}else{
|
||
LOG_ERROR("NodeTime ACK send failed,shortAddr = %02x%02x\n",pDestShortAddr[0],pDestShortAddr[1]);
|
||
}
|
||
return iRet;
|
||
}
|
||
void Uart::DealRecvData(const char *pData)
|
||
{
|
||
char buf[8];
|
||
char shortAdd[8]={0x00};
|
||
sprintf(buf, "%02d", pData[5]&0xFF);
|
||
sprintf(shortAdd, "%02x%02x", pData[3]&0xFF,pData[4]&0xFF);
|
||
unsigned short ushortAdd = BUILD_UINT16(pData[3]&0xFF,pData[4]&0xFF);
|
||
int flag = atoi(buf);
|
||
print_info("the data package type(1,2,3,4,5,6) is %s,%x\n",buf,pData[5]&0xFF);
|
||
|
||
switch (flag)
|
||
{
|
||
case 1:{//0x01:设备信息
|
||
DealDataNodeInfo(pData);
|
||
}
|
||
break;
|
||
case 2:{//0x02:特征值
|
||
DealDataNodeFeature(pData, 0);
|
||
}
|
||
break;
|
||
/* case 3:{//0x03:长波形X轴
|
||
DealDataNodeWave(pData);
|
||
}
|
||
break;
|
||
case 4:{//0x04:长波形Y轴
|
||
DealDataNodeWave(pData);
|
||
}
|
||
break;
|
||
case 5:{//0x05:长波形Z轴
|
||
DealDataNodeWave(pData);
|
||
}
|
||
break;*/
|
||
case 6:{//0x06:特征值+长波形
|
||
//LOG_INFO("DealDataNodeFeature 06");
|
||
DealDataNodeFeature(pData, 1);
|
||
}
|
||
break;
|
||
case 32:{//升级
|
||
|
||
UpdateWirelessNode(ushortAdd);
|
||
}
|
||
break;
|
||
case 7:{
|
||
DealDataNodeName(pData);
|
||
}
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
|
||
void Uart::DealDataNodeName(const char* pData)
|
||
{
|
||
bSendTimeStamp = false;
|
||
print_info("DealDataNodeName \n");
|
||
string strTime = GetLocalTimeWithMs();
|
||
//LOG_INFO("DealDataNodeName Time = %s\n",strTime.c_str());
|
||
char szShortAdd[8]={0x00};
|
||
char shortAdd[8]={0x00};
|
||
char NodeName[64]={0x00};
|
||
sprintf(szShortAdd, "%02x%02x", pData[3]&0xFF,pData[4]&0xFF);
|
||
memcpy(NodeName,&pData[7],64);
|
||
memcpy(shortAdd,&pData[3],2);
|
||
char whereCon[64] = { 0 };
|
||
char uplCon[200]={0x00};
|
||
int iRet = -1;
|
||
char nodeWaveSend[10]={0x00};
|
||
|
||
UpdateWirelessNodeTime((unsigned char*)shortAdd,1);
|
||
|
||
for(int i = 0; i < 64;i++){
|
||
sprintf(&NodeName[i * 2], "%02X", pData[7 + i]&0xFF);
|
||
}
|
||
char MeasurementID[100]={0x00};
|
||
sprintf(MeasurementID, "%02x%02x%02x%02x%02x%02x%02x%02x", pData[71], pData[72], pData[73], pData[74],
|
||
pData[75], pData[76], pData[77],pData[78]);
|
||
sprintf(nodeWaveSend,"%d,%d,%d",GET_BIT(pData[79], 0 ),GET_BIT(pData[79], 1 ),GET_BIT(pData[79], 2 ));
|
||
char gbkNodeName[128]={0x00};
|
||
sprintf(whereCon, "zigbeeShortAddr='%s'", szShortAdd);
|
||
print_info("whereCon = %s\n",whereCon);
|
||
array_t vecRes = 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));
|
||
}
|
||
std::string strNodeName(NodeName);
|
||
print_info("strNodeName = %s\n",strNodeName.c_str());
|
||
solve(gbkNodeName,NodeName);
|
||
print_info("gbkNodeName = %s\n",gbkNodeName);
|
||
string utfNodeName = GBKToUTF8(gbkNodeName);
|
||
print_info("NodeName = %s\n",NodeName);
|
||
print_info("whereCon = %s\n",whereCon);
|
||
sprintf(uplCon,"dataNodeName = '%s' , MeasurementID = '%s',NodeWaveSend = '%s'",gbkNodeName,MeasurementID,nodeWaveSend);
|
||
iRet = sql_ctl->UpdateTableData(T_SENSOR_INFO(TNAME),uplCon,whereCon,0);
|
||
string strData = sql_ctl->GetNodeConfigureInfor(whereCon);
|
||
iRet = data_publish(strData.c_str(), GlobalConfig::Topic_G.mPubConfig.c_str());
|
||
|
||
}
|
||
|
||
void Uart::DealDataNodeInfo(const char *pData)
|
||
{
|
||
print_info("recv remote zigbee module info\n");
|
||
RecvData * pRecvData = (RecvData *)pData;
|
||
char buf[32] = {0};
|
||
char chTemp = pRecvData->Data[0];//设备状态标志 1 byte
|
||
DataNodeInfo dataNodeInfo;
|
||
dataNodeInfo.EquipSta = GET_BIT(chTemp, 2 );
|
||
dataNodeInfo.TemTopFlag = GET_BIT(chTemp, 3 );
|
||
dataNodeInfo.TemBotFlag = GET_BIT(chTemp , 4);
|
||
dataNodeInfo.ZigbeeFlag = GET_BIT(chTemp , 5);
|
||
dataNodeInfo.AccFlag = GET_BIT(chTemp ,6);
|
||
dataNodeInfo.InitFlag = GET_BIT(chTemp ,7);
|
||
|
||
sprintf(buf, "%02x%02x%02x%02x%02x%02x%02x%02x", pRecvData->Data[1], pRecvData->Data[2], pRecvData->Data[3], pRecvData->Data[4],
|
||
pRecvData->Data[5], pRecvData->Data[6], pRecvData->Data[7],pRecvData->Data[8]);
|
||
dataNodeInfo.ZigbeeLongAddr = std::string(buf);//Zigbee MAC 8 byte
|
||
|
||
chTemp = pRecvData->Data[9];//硬件版本 1 byte
|
||
memset(buf, 0, 32);
|
||
sprintf(buf, "%.1f", 0.1*chTemp);
|
||
dataNodeInfo.HardVersion = std::string(buf);
|
||
|
||
chTemp = pRecvData->Data[10];//软件版本 1 byte
|
||
memset(buf, 0, 32);
|
||
//sprintf(buf, "%02x", chTemp);
|
||
sprintf(buf, "%.1f", 0.1*chTemp);
|
||
dataNodeInfo.SoftVersion = std::string(buf);
|
||
|
||
memset(buf, 0, 32);
|
||
sprintf(buf, "%d", BUILD_UINT32(pRecvData->Data[11], pRecvData->Data[12], pRecvData->Data[13], pRecvData->Data[14]));
|
||
dataNodeInfo.BpNo = std::string(buf);//生产批号 4 byte
|
||
|
||
memset(buf, 0, 32);
|
||
sprintf(buf, "%d", BUILD_UINT32(pRecvData->Data[15], pRecvData->Data[16], pRecvData->Data[17], pRecvData->Data[18]));
|
||
dataNodeInfo.SerialNo = std::string(buf);//生产序列号 4 byte
|
||
|
||
memset(buf, 0, 32);
|
||
sprintf(buf, "%d", BUILD_UINT32(pRecvData->Data[19], pRecvData->Data[20], pRecvData->Data[21], pRecvData->Data[22]));
|
||
dataNodeInfo.FirstPowerTime = std::string(buf);//首次上电日期 4 byte
|
||
|
||
//23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38=>序号13 无线信号强度
|
||
|
||
memset(buf, 0, 32);
|
||
sprintf(buf, "%d", BUILD_UINT32(pRecvData->Data[23], pRecvData->Data[24], pRecvData->Data[25], pRecvData->Data[26]));
|
||
dataNodeInfo.WakeupTime = atoi(buf);//唤醒次数 4 byte
|
||
printf("WakeupTime = %d\n",dataNodeInfo.WakeupTime);
|
||
memset(buf, 0, 32);
|
||
sprintf(buf, "%d", BUILD_UINT32(pRecvData->Data[27], pRecvData->Data[28], pRecvData->Data[29], pRecvData->Data[30]));
|
||
dataNodeInfo.StaticTime = atoi(buf);//特征值发送次数 4 byte
|
||
printf("StaticTime = %d\n",dataNodeInfo.StaticTime);
|
||
memset(buf, 0, 32);
|
||
// sprintf(buf, "%d", BUILD_UINT32(pRecvData->Data[31], pRecvData->Data[32], pRecvData->Data[33], pRecvData->Data[34]));
|
||
|
||
dataNodeInfo.WaveTime = BUILD_UINT32(pRecvData->Data[31], pRecvData->Data[32], pRecvData->Data[33], pRecvData->Data[34]);//原始波形发送次数 4 byte
|
||
|
||
memset(buf, 0, 32);
|
||
// 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
|
||
|
||
// 获取 RSSI
|
||
chTemp = pRecvData->Data[38];
|
||
memset(buf, 0, 32);
|
||
sprintf(buf, "%d", chTemp);
|
||
dataNodeInfo.RSSI = atoi(buf); //无线信号强度 1 byte
|
||
|
||
chTemp = pRecvData->Data[39];
|
||
memset(buf, 0, 32);
|
||
sprintf(buf, "%02x", chTemp);
|
||
dataNodeInfo.ConfigFlag = ((0 == std::string(buf).compare("aa")) ? 1 : 0); //配置标志 1 byte
|
||
|
||
chTemp = pRecvData->Data[40];
|
||
memset(buf, 0, 32);
|
||
sprintf(buf, "%d", chTemp);
|
||
dataNodeInfo.FeatureInterVal = atoi(buf); //唤醒周期 1 byte
|
||
memset(buf, 0, 32);
|
||
// yxq
|
||
sprintf(buf, "%u%u", pRecvData->Data[41],pRecvData->Data[42]);
|
||
dataNodeInfo.WaveInterVal = atoi(buf);//原始波形发送周期 2 byte
|
||
memset(buf, 0, 32);
|
||
sprintf(buf, "%02x%02x", pRecvData->Data[43], pRecvData->Data[44]); //Zigbee PID 2 byte
|
||
dataNodeInfo.ZigbeePanId = std::string(buf);
|
||
|
||
chTemp = pRecvData->Data[45];
|
||
memset(buf, 0, 32);
|
||
sprintf(buf, "%d", chTemp);
|
||
dataNodeInfo.ZigbeeChannel = atoi(buf);//Zigbee 信道 1 byte
|
||
|
||
memset(buf, 0, 32);
|
||
sprintf(buf, "%02x%02x", pRecvData->Data[46], pRecvData->Data[47]);//Zigbee 本地地址 2 byte
|
||
|
||
dataNodeInfo.ZigbeeShortAddr = std::string(buf);
|
||
unsigned char shortAddr[2] = {0x00};
|
||
memcpy(shortAddr,(unsigned char*)&pRecvData->Data[46],2);
|
||
modify_DistAddr(shortAddr);//修改目标地址
|
||
string strTime = GetLocalTimeWithMs();
|
||
//LOG_INFO("DealDataNodeInfo modify_DistAddr Time = %s\n",strTime.c_str());
|
||
|
||
//LOG_INFO("ZigbeeShortAddr = %s,SoftVersion = %s\n",dataNodeInfo.ZigbeeShortAddr.c_str(),dataNodeInfo.SoftVersion.c_str());
|
||
memset(buf, 0, 32);
|
||
sprintf(buf, "%02x%02x", pRecvData->Data[48], pRecvData->Data[49]);//Zigbee 目标地址 2 byte
|
||
dataNodeInfo.ZigbeeDesAddr = std::string(buf);
|
||
//print_info("ZigbeeDesAddr = %s\n",buf);
|
||
//50 51 52=》序号23 zigbee重试间隔
|
||
|
||
memset(buf, 0, 32);
|
||
sprintf(buf, "%02x", pRecvData->Data[50]);//Zigbee 发射功率 1 byte
|
||
dataNodeInfo.ZigbeePower = atoi(buf);
|
||
//print_info("ZigbeePower = %s\n",buf);
|
||
|
||
memset(buf, 0, 32);
|
||
sprintf(buf, "%d", BUILD_UINT16(00,pRecvData->Data[51]));//Zigbee 重试次数 1 byte
|
||
dataNodeInfo.ZigbeeRetry = atoi(buf);
|
||
//print_info("ZigbeeRetry = %s\n",buf);
|
||
|
||
memset(buf, 0, 32);
|
||
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;
|
||
}
|
||
}
|
||
// 54=》序号25 ACC采样时间
|
||
memset(buf, 0, 32);
|
||
sprintf(buf, "%u", pRecvData->Data[54]);//ACC 采样时间 1 byte
|
||
dataNodeInfo.ACCSampleTime = atoi(buf);
|
||
|
||
int iTemp = 0;
|
||
//使用了55 56 59 60 63 64 67 68 71 72
|
||
for (int i = 0; i < 5; i++) {
|
||
memset(buf, 0, 32);
|
||
sprintf(buf, "%02x%02x", pRecvData->Data[55 + i * 4], pRecvData->Data[55 + i * 4 + 1]);
|
||
iTemp = (int)strtol(buf, NULL, 16);
|
||
|
||
if (0 == i) {
|
||
dataNodeInfo.StartBrands = to_string(iTemp);
|
||
} else {
|
||
dataNodeInfo.StartBrands += ("," + to_string(iTemp));
|
||
}
|
||
}
|
||
//使用了57 58 61 62 65 66 69 70 73 74
|
||
for (int j = 0; j < 5; j++) {
|
||
memset(buf, 0, 32);
|
||
sprintf(buf, "%02x%02x", pRecvData->Data[57 + j * 4], pRecvData->Data[57 + j * 4 + 1]);
|
||
iTemp = (int)strtol(buf, NULL, 16);
|
||
if (0 == j) {
|
||
dataNodeInfo.StopBrands = to_string(iTemp);
|
||
} else {
|
||
dataNodeInfo.StopBrands += ("," + to_string(iTemp));
|
||
}
|
||
}
|
||
|
||
|
||
memset(buf, 0, 32);
|
||
sprintf(buf, "%02x%02x", pRecvData->Data[75], pRecvData->Data[76]);
|
||
iTemp = (int)strtol(buf, NULL, 16);
|
||
dataNodeInfo.EnvelopeBandPass = to_string(iTemp);
|
||
|
||
|
||
memset(buf, 0, 32);
|
||
sprintf(buf, "%02x%02x", pRecvData->Data[77], pRecvData->Data[78]);
|
||
iTemp = (int)strtol(buf, NULL, 16);
|
||
dataNodeInfo.EnvelopeBandPass += ("," + to_string(iTemp));
|
||
//使用了79 80 81 82 83 84 85 86
|
||
for (int j = 0; j < 4; j++) {
|
||
memset(buf, 0, 32);
|
||
sprintf(buf, "%02x%02x", pRecvData->Data[79 + j * 2], pRecvData->Data[79 + j * 2 + 1]);
|
||
iTemp = (int)strtol(buf, NULL, 16);
|
||
|
||
if (0 == j) {
|
||
dataNodeInfo.FaultFrequency = to_string(iTemp);
|
||
} else {
|
||
dataNodeInfo.FaultFrequency += ("," + to_string(iTemp));
|
||
}
|
||
}
|
||
|
||
memset(buf, 0, 32);
|
||
sprintf(buf, "%02x%02x%02x%02x", pRecvData->Data[87], pRecvData->Data[88], pRecvData->Data[89],pRecvData->Data[90]);
|
||
long lTimeStamp = strtol(buf, NULL, 16);
|
||
dataNodeInfo.ConfigDate = to_string(lTimeStamp);
|
||
|
||
chTemp = pRecvData->Data[91];
|
||
memset(buf, 0, 32);
|
||
sprintf(buf, "%d", chTemp);
|
||
dataNodeInfo.VIntegralFilterFrequency = atoi(buf);
|
||
|
||
char whereCon[64] = {0};
|
||
sprintf(whereCon, "dataNodeNo='%s'", dataNodeInfo.ZigbeeLongAddr.c_str());
|
||
if (sql_ctl->GetTableRows(T_SENSOR_INFO(TNAME), whereCon) > 0) {
|
||
|
||
char updateSql[1024] = { 0 };
|
||
sprintf(updateSql, " initFlag = '%d',accFlag = '%d',zigbeeFlag = '%d',temTopFlag = '%d',temBotFlag = '%d',equipSta = '%d',\
|
||
hardVersion = '%s',softVersion = '%s',bpNo = '%s',serialNo = '%s',firstPowerTime = '%s',WakeupTime = '%d',\
|
||
StaticTime = '%d',WaveTime = '%d',BateryV = '%d',ProductNo = '%s',configFlag = '%d',startBrands = '%s',\
|
||
stopBrands = '%s',featureInterVal = '%u',waveInterVal = '%d',samplingRate = '%d',scope = '%s',range = '%d',envelopeBandPass = '%s',faultFrequency = '%s',\
|
||
zigbeePanId = '%s',zigbeeChannel = '%d',zigbeeShortAddr = '%s',zigbeeLongAddr = '%s',zigbeeDesAddr = '%s',\
|
||
ZigbeePower = '%d',ZigbeeRetry = '%d',ZigbeeRetryGap = '%d',ACCSampleTime = '%d',status = '%s',timeStamp = '%s',viff = '%d',RSSI = '0,%d',UpdateFlag = 1",
|
||
dataNodeInfo.InitFlag, dataNodeInfo.AccFlag, dataNodeInfo.ZigbeeFlag, dataNodeInfo.TemTopFlag, dataNodeInfo.TemBotFlag,dataNodeInfo.EquipSta,\
|
||
dataNodeInfo.HardVersion.c_str(), dataNodeInfo.SoftVersion.c_str(), dataNodeInfo.BpNo.c_str(), dataNodeInfo.SerialNo.c_str(), dataNodeInfo.FirstPowerTime.c_str(), dataNodeInfo.WakeupTime,\
|
||
dataNodeInfo.StaticTime,dataNodeInfo.WaveTime,dataNodeInfo.BateryV,dataNodeInfo.ProductNo.c_str(),dataNodeInfo.ConfigFlag, dataNodeInfo.StartBrands.c_str(), \
|
||
dataNodeInfo.StopBrands.c_str(), dataNodeInfo.FeatureInterVal, dataNodeInfo.WaveInterVal, dataNodeInfo.SamplingRate,"",dataNodeInfo.Range, dataNodeInfo.EnvelopeBandPass.c_str(), dataNodeInfo.FaultFrequency.c_str(),\
|
||
dataNodeInfo.ZigbeePanId.c_str(), dataNodeInfo.ZigbeeChannel, dataNodeInfo.ZigbeeShortAddr.c_str(), dataNodeInfo.ZigbeeLongAddr.c_str(), dataNodeInfo.ZigbeeDesAddr.c_str(), \
|
||
dataNodeInfo.ZigbeePower,dataNodeInfo.ZigbeeRetry,dataNodeInfo.ZigbeeRetryGap,dataNodeInfo.ACCSampleTime,"1", dataNodeInfo.ConfigDate.c_str(),dataNodeInfo.VIntegralFilterFrequency,dataNodeInfo.RSSI);
|
||
sprintf(whereCon,"dataNodeNo = '%s'",dataNodeInfo.ZigbeeLongAddr.c_str());
|
||
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',\
|
||
'%d','%d','%d','%d','%s','%s', '%d', '0,%d','1','0,0','','',''",
|
||
dataNodeInfo.ZigbeeLongAddr.c_str(), " ", dataNodeInfo.InitFlag, dataNodeInfo.AccFlag, dataNodeInfo.ZigbeeFlag, dataNodeInfo.TemTopFlag, dataNodeInfo.TemBotFlag,dataNodeInfo.EquipSta,\
|
||
dataNodeInfo.HardVersion.c_str(), dataNodeInfo.SoftVersion.c_str(), dataNodeInfo.BpNo.c_str(), dataNodeInfo.SerialNo.c_str(), dataNodeInfo.FirstPowerTime.c_str(), dataNodeInfo.WakeupTime,\
|
||
dataNodeInfo.StaticTime,dataNodeInfo.WaveTime,dataNodeInfo.BateryV,dataNodeInfo.ProductNo.c_str(),dataNodeInfo.ConfigFlag, dataNodeInfo.StartBrands.c_str(), \
|
||
dataNodeInfo.StopBrands.c_str(), dataNodeInfo.FeatureInterVal, dataNodeInfo.WaveInterVal, dataNodeInfo.SamplingRate,"",dataNodeInfo.Range, dataNodeInfo.EnvelopeBandPass.c_str(), dataNodeInfo.FaultFrequency.c_str(),\
|
||
dataNodeInfo.ZigbeePanId.c_str(), dataNodeInfo.ZigbeeChannel, dataNodeInfo.ZigbeeShortAddr.c_str(), dataNodeInfo.ZigbeeLongAddr.c_str(), dataNodeInfo.ZigbeeDesAddr.c_str(), \
|
||
dataNodeInfo.ZigbeePower,dataNodeInfo.ZigbeeRetry,dataNodeInfo.ZigbeeRetryGap,dataNodeInfo.ACCSampleTime,"1", dataNodeInfo.ConfigDate.c_str(),dataNodeInfo.VIntegralFilterFrequency,dataNodeInfo.RSSI);
|
||
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);
|
||
Json::Value jsonVal;
|
||
jsonVal.clear();
|
||
jsonVal["cmd"] = "26";
|
||
|
||
Json::Value jsBody;
|
||
jsBody["dataNodeGatewayNo"] = GlobalConfig::MacAddr_G;
|
||
jsBody["dataNodeNo"] = dataNodeInfo.ZigbeeLongAddr;
|
||
jsBody["type"] = "insert";
|
||
|
||
Json::Value jsSensorData;
|
||
jsSensorData["dataNodeNo"] = dataNodeInfo.ZigbeeLongAddr;
|
||
jsSensorData["dataNodeName"] = "";
|
||
jsSensorData["initFlag"] = dataNodeInfo.InitFlag;
|
||
jsSensorData["accFlag"] = dataNodeInfo.AccFlag;
|
||
jsSensorData["zigbeeFlag"] = dataNodeInfo.ZigbeeFlag;
|
||
jsSensorData["temTopFlag"] = dataNodeInfo.TemTopFlag;
|
||
jsSensorData["temBotFlag"] = dataNodeInfo.TemBotFlag;
|
||
jsSensorData["equipsta"] = dataNodeInfo.EquipSta;
|
||
jsSensorData["hardVersion"] = dataNodeInfo.HardVersion;
|
||
jsSensorData["softVersion"] = dataNodeInfo.SoftVersion;
|
||
jsSensorData["bpNo"] = dataNodeInfo.BpNo;
|
||
jsSensorData["serialNo"] = dataNodeInfo.SerialNo;
|
||
jsSensorData["firstPowerTime"] = dataNodeInfo.FirstPowerTime;
|
||
jsSensorData["configFlag"] = dataNodeInfo.ConfigFlag;
|
||
jsSensorData["startBrands"] = dataNodeInfo.StartBrands;
|
||
jsSensorData["stopBrands"] = dataNodeInfo.StopBrands;
|
||
jsSensorData["featureInterVal"] = dataNodeInfo.FeatureInterVal;
|
||
jsSensorData["waveInterVal"] = dataNodeInfo.WaveInterVal;
|
||
jsSensorData["samplingRate"] = dataNodeInfo.SamplingRate;
|
||
jsSensorData["range"] = dataNodeInfo.Range;
|
||
jsSensorData["envelopeBandPass"] = dataNodeInfo.EnvelopeBandPass;
|
||
jsSensorData["faultFrequency"] = dataNodeInfo.FaultFrequency;
|
||
jsSensorData["zigbeePanId"] = dataNodeInfo.ZigbeePanId;
|
||
jsSensorData["zigbeeChannel"] = dataNodeInfo.ZigbeeChannel;
|
||
jsSensorData["zigbeeAddr"] = dataNodeInfo.ZigbeeLongAddr;
|
||
jsSensorData["zigbeeDesAddr"] = dataNodeInfo.ZigbeeDesAddr;
|
||
jsSensorData["status"] = 1;
|
||
jsSensorData["timeStamp"] = dataNodeInfo.ConfigDate;
|
||
jsSensorData["VIntegralFilterFrequency"] = dataNodeInfo.VIntegralFilterFrequency;
|
||
jsSensorData["RSSI"] = dataNodeInfo.RSSI;
|
||
|
||
jsBody["dataNodeInfo"] = jsSensorData;
|
||
|
||
Json::FastWriter showValue;
|
||
std::string dataBody = showValue.write(jsBody);
|
||
jsonVal["cmdBody"] = dataBody;
|
||
std::string strCmd26 = showValue.write(jsonVal);
|
||
//传感器发来的数据包中的表示设备信息的数据转化为json格式后,通过调用data_publish将数据传给mqttclient : Topic:wireless/cmd/60294D203717
|
||
data_publish(strCmd26.c_str(), GlobalConfig::Topic_G.mPubCmd.c_str());
|
||
|
||
print_info("remote wireless sensor device info AccFlag : %d EquipSta : %d BpNo : %s ConfigFlag : %d EnvelopeBandPass : %s FaultFrequency : %s FeatureInterVal : %u FirstPowerTime : %s HardVersion : %s InitFlag : %d SamplingRate : %d range : %d SerialNo : %s\
|
||
SoftVersion : %s StartBrands : %s StopBrands : %s TemBotFlag : %d TemTopFlag : %d WaveInterVal : %d ZigbeeChannel : %d ZigbeeDesAddr : %s ZigbeeFlag : %d ZigbeeLongAddr : %s panid : %s ZigbeeShortAddr : %s Configdate : %s vintegralfilterfrequency : %d RSSI : %d \n", \
|
||
dataNodeInfo.AccFlag, dataNodeInfo.EquipSta, dataNodeInfo.BpNo.c_str(), dataNodeInfo.ConfigFlag, dataNodeInfo.EnvelopeBandPass.c_str(), dataNodeInfo.FaultFrequency.c_str(),\
|
||
dataNodeInfo.FeatureInterVal, dataNodeInfo.FirstPowerTime.c_str(), dataNodeInfo.HardVersion.c_str(), dataNodeInfo.InitFlag, dataNodeInfo.SamplingRate, dataNodeInfo.Range, dataNodeInfo.SerialNo.c_str(),\
|
||
dataNodeInfo.SoftVersion.c_str(), dataNodeInfo.StartBrands.c_str(), dataNodeInfo.StopBrands.c_str(), dataNodeInfo.TemBotFlag, dataNodeInfo.TemTopFlag, dataNodeInfo.WaveInterVal,\
|
||
dataNodeInfo.ZigbeeChannel, dataNodeInfo.ZigbeeDesAddr.c_str(), dataNodeInfo.ZigbeeFlag, dataNodeInfo.ZigbeeLongAddr.c_str(), dataNodeInfo.ZigbeePanId.c_str(), dataNodeInfo.ZigbeeShortAddr.c_str(), dataNodeInfo.ConfigDate.c_str(), dataNodeInfo.VIntegralFilterFrequency, dataNodeInfo.RSSI);
|
||
|
||
if (g_mapCompress.find(dataNodeInfo.ZigbeeShortAddr) == g_mapCompress.end()){
|
||
|
||
compressWaveChannel tempchannel;
|
||
g_mapCompress.insert(std::make_pair(dataNodeInfo.ZigbeeShortAddr,tempchannel));
|
||
print_info("new Node,size = %d\n",g_mapCompress.size());
|
||
}
|
||
LOG_INFO("DealDataNodeInfo %s \n",dataNodeInfo.ZigbeeShortAddr.c_str());
|
||
}
|
||
|
||
void Uart::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);
|
||
}
|
||
void Uart::DealDataNodeFeature(const char *pData, int flag)
|
||
{
|
||
print_info("recv feature\n");
|
||
RecvData * pRecvData = (RecvData *)pData;
|
||
char whereCon[1024] = {0};
|
||
char updateSql[1024] = { 0 };
|
||
char buf[20] = {0x00};
|
||
int nodeResend = 0,timing = 0;
|
||
sprintf(buf, "%02x%02x", pRecvData->ShortAddr[0], pRecvData->ShortAddr[1]);
|
||
if (flag == 1)
|
||
{
|
||
LOG_INFO("DealDataNodeFeature %02x%02x, %d\n",pRecvData->ShortAddr[0], pRecvData->ShortAddr[1],flag);
|
||
}else{
|
||
LOG_INFO("DealDataNodeFeature %02x%02x\n",pRecvData->ShortAddr[0], pRecvData->ShortAddr[1]);
|
||
}
|
||
|
||
|
||
if (bSendTimeStamp)//波形处理中
|
||
return;
|
||
std::string strShortAddr = std::string(buf);
|
||
print_info("zigbeeShortAddr='%s'\n", strShortAddr.c_str());
|
||
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);
|
||
vec_t vecResult = sql_ctl->GetDataSingleLine(T_SENSOR_INFO(TNAME)," softVersion,dataNodeNo,MeasurementID ",getLongAddr_sql);
|
||
|
||
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];
|
||
std::string strMeasurementID = vecResult[2];
|
||
if (1 == flag) {
|
||
|
||
tcflush(fd,TCIOFLUSH);
|
||
|
||
if (!bSendTimeStamp)
|
||
{
|
||
bSendTimeStamp = true;
|
||
modify_distaddr_info(0x9999,"",pRecvData->ShortAddr);//临时参数配置
|
||
mssleep(10000);
|
||
|
||
LOG_DEBUG("Zigbee Signal !\n");
|
||
int Times = 0;
|
||
mssleep(20000);
|
||
while(Times < 3 ){
|
||
getZigbeeSignal(pRecvData->ShortAddr);
|
||
Times ++ ;
|
||
mssleep(20000);
|
||
}
|
||
mssleep(10000);
|
||
timing = 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);
|
||
}
|
||
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;
|
||
}
|
||
unsigned char compressChannel = pRecvData->Data[34];
|
||
compressWaveChannel tempchannel;
|
||
tempchannel.compressChannelX = GET_BIT(compressChannel ,0);
|
||
tempchannel.compressChannelY = GET_BIT(compressChannel ,2);
|
||
tempchannel.compressChannelZ = GET_BIT(compressChannel ,4);
|
||
tempchannel.CountX = BUILD_UINT16(pRecvData->Data[55],pRecvData->Data[54]);
|
||
tempchannel.CountY = BUILD_UINT16(pRecvData->Data[57],pRecvData->Data[56]);
|
||
tempchannel.CountZ = BUILD_UINT16(pRecvData->Data[59],pRecvData->Data[58]);
|
||
g_mapCompress[strShortAddr] = tempchannel;
|
||
|
||
print_info("count X = %d,Y = %d,Z = %d\n",tempchannel.CountX,tempchannel.CountY,tempchannel.CountZ);
|
||
print_info("compress X = %d,Y = %d,Z = %d \n",tempchannel.compressChannelX,tempchannel.compressChannelY,tempchannel.compressChannelZ);
|
||
}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());
|
||
}
|
||
|
||
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]);
|
||
|
||
print_info("staticIndex = %d\n",staticIndex);
|
||
|
||
|
||
//LOG_INFO("staticIndex = %d\n",staticIndex);
|
||
|
||
char localtimestamp[32] = { 0 };
|
||
GetTimeNet(localtimestamp, 1);
|
||
std::string nowTimetamp = std::string(localtimestamp);
|
||
strTimetamp = nowTimetamp;
|
||
|
||
int iTemp = 0;
|
||
unsigned char highbit = 0;
|
||
unsigned int lowbit = 0;
|
||
float n = 0;
|
||
|
||
DataRecvStatic dataStatic;
|
||
memset(buf, 0, sizeof(buf));
|
||
sprintf(buf, "%02x%02x", pRecvData->Data[5], pRecvData->Data[4]);
|
||
print_blue("!!!!!!!!!!!!!!!!!!!!!!%s\n",buf);
|
||
iTemp = (int)strtol(buf, NULL, 16);
|
||
dataStatic.Dip = iTemp;
|
||
|
||
int fTemp = 0;
|
||
memset(buf, 0, sizeof(buf));
|
||
sprintf(buf, "%02x%02x", pRecvData->Data[1], pRecvData->Data[0]);
|
||
print_blue("@@@@@@@@@@@@@@@@@%s\n",buf);
|
||
iTemp = (int)strtol(buf, NULL, 16);
|
||
if (iTemp < 0x8000) {
|
||
fTemp = iTemp ;
|
||
} else {
|
||
fTemp = (((~iTemp)&0xffff) + 1)* (-1);
|
||
}
|
||
dataStatic.TemBot = fTemp * 0.0625;//设备温度
|
||
|
||
fTemp = 0;
|
||
memset(buf, 0, sizeof(buf));
|
||
sprintf(buf, "%02x%02x", pRecvData->Data[3], pRecvData->Data[2]);
|
||
iTemp = (int)strtol(buf, NULL, 16);
|
||
if (iTemp < 0x8000) {
|
||
fTemp = iTemp ;
|
||
} else {
|
||
fTemp = (((~iTemp)&0xffff) + 1) * (-1);
|
||
}
|
||
dataStatic.TemTop = fTemp * 0.0625;//环境温度
|
||
|
||
memset(buf, 0, sizeof(buf));
|
||
sprintf(buf, "%02x%02x", pRecvData->Data[7], pRecvData->Data[6]);
|
||
iTemp = (int)strtol(buf, NULL, 16);
|
||
dataStatic.Voltage = iTemp;
|
||
|
||
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);
|
||
|
||
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);
|
||
}
|
||
|
||
////////////////////////////////////////////////////////////更换电池判断
|
||
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()));
|
||
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);
|
||
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;
|
||
}
|
||
}
|
||
print_info("nowTimetamp = %s",nowTimetamp.c_str());
|
||
// save dataStatic of 7 days
|
||
char selectCon[128] = { 0 };
|
||
sprintf(selectCon, "channelID='%s' ORDER BY timeStamp ASC LIMIT 0,1",(strMeasurementID + "-S").c_str());
|
||
std::string strTime = sql_ctl->GetData(szTableName, "timeStamp", selectCon);
|
||
sprintf(whereCon,"channelID='%s' ",(strMeasurementID + "-S").c_str());
|
||
int Count = sql_ctl->GetTableRows(szTableName, whereCon);
|
||
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);
|
||
|
||
print_info("dataStatic.TemTop : %f dataStatic.TemBot : %f dataStatic.Dip :%d dataStatic.Voltage : %d\n", dataStatic.TemTop\
|
||
, dataStatic.TemBot, dataStatic.Dip, dataStatic.Voltage);
|
||
|
||
sprintf(updateSql, "temTop='%f',temBot='%f',dip='%d',voltage='%d',timeStamp='%s',StaticIndex = %d, nodeResend = %d,zigbeeSignal = '',zigbeeSignalNode = '',statisticType = '%d',timing = '%d' ",\
|
||
dataStatic.TemTop, dataStatic.TemBot, dataStatic.Dip, dataStatic.Voltage, nowTimetamp.c_str(),staticIndex,nodeResend,flag,timing);
|
||
sprintf(whereCon, "channelID='%s' ", (strMeasurementID + "-S").c_str());
|
||
if ( /*0 == sql_ctl->GetTableRows(T_DATASTATIC_INFO(TNAME), whereCon)*/ (Count * 3 < SAVE_COUNT && lTime < OneWeek ) || strTime.size() == 0 ) {
|
||
print_info("insert static data to sql\n");
|
||
char insertSql[1024] = { 0 };
|
||
sprintf(insertSql, "'%s','%s','%f','%f','%d','%d','',%d,'%s','1',%d,'','%d','%d'",
|
||
strLongAddr.c_str(), (strMeasurementID + "-S").c_str(), dataStatic.TemTop, dataStatic.TemBot, dataStatic.Dip, dataStatic.Voltage, staticIndex,nowTimetamp.c_str(),nodeResend,flag,timing);
|
||
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",
|
||
strLongAddr.c_str(), (strMeasurementID + "-S").c_str(), dataStatic.TemTop, dataStatic.TemBot, dataStatic.Dip, dataStatic.Voltage, staticIndex,nowTimetamp.c_str(),nodeResend);
|
||
sql_ctl->InsertData(T_DATASTATIC_INFO(TNAME), insertSql);
|
||
sql_ctl->CalculateBattery();
|
||
}
|
||
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);
|
||
sql_ctl->UpdateTableData(T_DATASTATIC_INFO(TNAME), updateSql, whereCon);
|
||
}
|
||
} else {
|
||
memset(whereCon,0x00,sizeof(whereCon));
|
||
sprintf(whereCon, "channelID='%s' and timeStamp = '%s'", (strMeasurementID + "-S").c_str(),strTime.c_str());
|
||
print_info("update static data to sql\n");
|
||
sql_ctl->UpdateTableData(szTableName, updateSql, whereCon);
|
||
memset(whereCon,0x00,sizeof(whereCon));
|
||
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);
|
||
sql_ctl->UpdateTableData(T_DATASTATIC_INFO(TNAME), updateSql, whereCon);
|
||
}
|
||
memset(szTableName,0x00,sizeof(szTableName));
|
||
sprintf(szTableName,"t_data_%s",strLongAddr.c_str());
|
||
if(Count == -1){
|
||
sql_ctl->Createtable(szTableName);
|
||
}
|
||
DataRecvDym dataDymX;
|
||
memset(buf, 0, sizeof(buf));
|
||
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;
|
||
|
||
memset(buf, 0, sizeof(buf));
|
||
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;
|
||
|
||
memset(buf, 0, sizeof(buf));
|
||
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;
|
||
|
||
memset(buf, 0, sizeof(buf));
|
||
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;
|
||
|
||
memset(buf, 0, sizeof(buf));
|
||
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;
|
||
|
||
memset(buf, 0, sizeof(buf));
|
||
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;
|
||
|
||
memset(buf, 0, sizeof(buf));
|
||
sprintf(buf, "%02x%02x", pRecvData->Data[21], pRecvData->Data[20]);
|
||
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.Amp3 = lowbit * n;
|
||
|
||
memset(buf, 0, sizeof(buf));
|
||
sprintf(buf, "%02x%02x", pRecvData->Data[23], pRecvData->Data[22]);
|
||
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.Amp4 = lowbit * n;
|
||
|
||
|
||
memset(buf, 0, sizeof(buf));
|
||
sprintf(buf, "%02x%02x", pRecvData->Data[25], pRecvData->Data[24]);
|
||
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.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));
|
||
|
||
memset(buf, 0, 8);
|
||
// 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);
|
||
sprintf(whereCon, "channelID='%s' ", (strMeasurementID + "-X").c_str());
|
||
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",
|
||
strLongAddr.c_str(), (strMeasurementID + "-X").c_str(), 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, 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));
|
||
sprintf(whereCon, "channelID='%s' and timeStamp = '%s'", (strMeasurementID + "-X").c_str(),strTime.c_str());
|
||
// sprintf(whereCon, "channelID='%s' and sendMsg = '1' ", (strLongAddr + "-X").c_str());
|
||
sql_ctl->UpdateTableData(szTableName, updateSql, whereCon);
|
||
memset(whereCon,0x00,sizeof(whereCon));
|
||
sprintf(whereCon, "channelID='%s' ", (strMeasurementID + "-X").c_str());
|
||
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",\
|
||
strLongAddr.c_str(), (strMeasurementID + "-X").c_str(), 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());
|
||
|
||
Json::Value valNodeData;
|
||
Json::Value valNodeFeature;
|
||
valNodeFeature["dataNodeNo"] = strMeasurementID;
|
||
valNodeFeature["ChannelId"] = strMeasurementID + "-X";
|
||
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]);
|
||
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.DiagnosisPk = lowbit * n;
|
||
|
||
memset(buf, 0, sizeof(buf));
|
||
sprintf(buf, "%02x%02x", pRecvData->Data[39], pRecvData->Data[38]);
|
||
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.RmsValues = lowbit * n;
|
||
|
||
memset(buf, 0, sizeof(buf));
|
||
sprintf(buf, "%02x%02x", pRecvData->Data[41], pRecvData->Data[40]);
|
||
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.IntegratPk = lowbit * n;
|
||
|
||
memset(buf, 0, sizeof(buf));
|
||
sprintf(buf, "%02x%02x", pRecvData->Data[43], pRecvData->Data[42]);
|
||
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.IntegratRMS = lowbit * n;
|
||
|
||
memset(buf, 0, sizeof(buf));
|
||
sprintf(buf, "%02x%02x", pRecvData->Data[45], pRecvData->Data[44]);
|
||
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.Amp1 = lowbit * n;
|
||
|
||
memset(buf, 0, sizeof(buf));
|
||
sprintf(buf, "%02x%02x", pRecvData->Data[47], pRecvData->Data[46]);
|
||
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.Amp2 = lowbit * n;
|
||
|
||
memset(buf, 0, sizeof(buf));
|
||
sprintf(buf, "%02x%02x", pRecvData->Data[49], pRecvData->Data[48]);
|
||
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.Amp3 = lowbit * n;
|
||
|
||
memset(buf, 0, sizeof(buf));
|
||
sprintf(buf, "%02x%02x", pRecvData->Data[51], pRecvData->Data[50]);
|
||
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.Amp4 = lowbit * n;
|
||
|
||
memset(buf, 0, sizeof(buf));
|
||
sprintf(buf, "%02x%02x", pRecvData->Data[53], pRecvData->Data[52]);
|
||
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.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);
|
||
sprintf(whereCon, "channelID='%s' ", (strMeasurementID + "-Y").c_str());
|
||
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",
|
||
strLongAddr.c_str(), (strMeasurementID + "-Y").c_str(), 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,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));
|
||
sprintf(whereCon, "channelID='%s' and timeStamp = '%s'", (strMeasurementID + "-Y").c_str(),strTime.c_str());
|
||
// sprintf(whereCon, "channelID='%s' and sendMsg = '1' ", (strLongAddr + "-Y").c_str());
|
||
sql_ctl->UpdateTableData(szTableName, updateSql, whereCon);
|
||
memset(whereCon,0x00,sizeof(whereCon));
|
||
sprintf(whereCon, "channelID='%s' ", (strMeasurementID + "-Y").c_str());
|
||
sql_ctl->UpdateTableData(T_DATA_INFO(TNAME), updateSql, whereCon);
|
||
}
|
||
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",\
|
||
strLongAddr.c_str(), (strMeasurementID + "-Y").c_str(), 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());
|
||
|
||
|
||
valNodeFeature["dataNodeNo"] = strMeasurementID;
|
||
valNodeFeature["ChannelId"] = strMeasurementID + "-Y";
|
||
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;
|
||
memset(buf, 0, sizeof(buf));
|
||
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;
|
||
|
||
memset(buf, 0, sizeof(buf));
|
||
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;
|
||
|
||
memset(buf, 0, sizeof(buf));
|
||
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;
|
||
|
||
memset(buf, 0, sizeof(buf));
|
||
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;
|
||
|
||
memset(buf, 0, sizeof(buf));
|
||
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;
|
||
|
||
memset(buf, 0, sizeof(buf));
|
||
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;
|
||
|
||
memset(buf, 0, sizeof(buf));
|
||
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;
|
||
|
||
memset(buf, 0, sizeof(buf));
|
||
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;
|
||
|
||
memset(buf, 0, sizeof(buf));
|
||
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;
|
||
|
||
memset(buf, 0, sizeof(buf));
|
||
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;
|
||
|
||
memset(buf, 0, sizeof(buf));
|
||
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;
|
||
|
||
memset(buf, 0, sizeof(buf));
|
||
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;
|
||
|
||
memset(buf, 0, sizeof(buf));
|
||
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;
|
||
|
||
memset(whereCon, 0, 1024);
|
||
sprintf(whereCon, "channelID='%s' ", (strMeasurementID + "-Z").c_str());
|
||
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",
|
||
strLongAddr.c_str(), (strMeasurementID + "-Z").c_str(), 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, 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));
|
||
sprintf(whereCon, "channelID='%s' and timeStamp = '%s'", (strMeasurementID + "-Z").c_str(),strTime.c_str());
|
||
// sprintf(whereCon, "channelID='%s' and sendMsg = '1'", (strLongAddr + "-Z").c_str());
|
||
sql_ctl->UpdateTableData(szTableName, updateSql, whereCon);
|
||
memset(whereCon,0x00,sizeof(whereCon));
|
||
sprintf(whereCon, "channelID='%s' ", (strMeasurementID + "-Z").c_str());
|
||
sql_ctl->UpdateTableData(T_DATA_INFO(TNAME), updateSql, whereCon);
|
||
}
|
||
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",\
|
||
strLongAddr.c_str(), (strMeasurementID + "-Z").c_str(), 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());
|
||
|
||
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());
|
||
//无线传感器Z信息
|
||
valNodeFeature["dataNodeNo"] = strMeasurementID;
|
||
valNodeFeature["ChannelId"] = strMeasurementID + "-Z";
|
||
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);
|
||
|
||
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 );
|
||
}
|
||
|
||
//无线传感器信息
|
||
Json::Value root;
|
||
Json::Value valdatastatic;
|
||
valdatastatic["TemperatureTop"] = dataStatic.TemTop;
|
||
valdatastatic["TemperatureBot"] = dataStatic.TemBot;
|
||
valdatastatic["WorkTime"] = dataStatic.nodeWorkTime;
|
||
valdatastatic["SendTime"] = dataStatic.nodeSendTime;
|
||
valdatastatic["Dip"] = dataStatic.Dip;
|
||
valdatastatic["Voltage"] = dataStatic.Voltage;
|
||
valdatastatic["ChannelType"] = "STATUS";
|
||
valdatastatic["ChannelId"] = strMeasurementID + "-S";
|
||
valdatastatic["TimeStamp"] = nowTimetamp;
|
||
valdatastatic["bateryProportion"] = atof(vBattery[1].c_str())/atof(vBattery[0].c_str());
|
||
valdatastatic["batteryRemainDay"] = atof(vBattery[1].c_str());
|
||
valdatastatic["dataNodeNo"] = strMeasurementID;
|
||
valNodeData.append(valdatastatic);
|
||
|
||
root["data"] = valNodeData;
|
||
root["TimeStamp"] = nowTimetamp;
|
||
root["dataNodeNo"] = strMeasurementID;
|
||
root["dataNodeGatewayNo"] = GlobalConfig::MacAddr_G;
|
||
Json::FastWriter featureValue;
|
||
std::string strstatisticData = featureValue.write(root);
|
||
//传感器发来的数据包中的表示设备信息的数据转化为json格式后,通过调用data_publish将数据传给mqttclient : Topic:wireless/cmd/60294D203717
|
||
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);
|
||
}
|
||
//综上代码,把静态数据,x y z轴的特征值存放到sql数据库中(如果数据原来不存在,则插入新数据;如果存在,则更新数据)
|
||
|
||
print_info("Dip : %d TemBot : %f TemBot : %f Voltage : %d\n", dataStatic.Dip, dataStatic.TemBot, dataStatic.TemTop, dataStatic.Voltage);
|
||
|
||
memset(selectCon,0x00,sizeof(selectCon));
|
||
sprintf(selectCon, "zigbeeSignal <> '' ORDER BY timeStamp desc LIMIT 0,1");
|
||
strTime = sql_ctl->GetData(szTableNameStatic, "timeStamp", selectCon);
|
||
|
||
|
||
LOG_INFO("DealDataNodeFeature end %02x%02x\n",pRecvData->ShortAddr[0], pRecvData->ShortAddr[1]);
|
||
}
|
||
|
||
void Uart::DealDataNodeWave(const char *pData,int comand)
|
||
{
|
||
//print_info("recv wave\n");
|
||
RecvData * pRecvData = (RecvData *)pData;
|
||
if (m_waveTrans)
|
||
{
|
||
if (comand == 3)
|
||
{
|
||
VecWaveDataX.push_back(*pRecvData);
|
||
}else if(comand == 4){
|
||
VecWaveDataY.push_back(*pRecvData);
|
||
}else if(comand == 5){
|
||
VecWaveDataZ.push_back(*pRecvData);
|
||
}
|
||
}else{
|
||
if (comand == 3)
|
||
{
|
||
g_VecWaveDataX[m_waveCountX] = *pRecvData;
|
||
m_waveCountX++;
|
||
}else if(comand == 4){
|
||
g_VecWaveDataY[m_waveCountY] = *pRecvData;
|
||
m_waveCountY++;
|
||
}else if(comand == 5){
|
||
g_VecWaveDataZ[m_waveCountZ] = *pRecvData;
|
||
m_waveCountZ++;
|
||
}
|
||
}
|
||
//print_blue("wave data size is(m_VecWaveData.size) : %d\n",m_VecWaveData.size());
|
||
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) {
|
||
// sleep(1);
|
||
// continue;
|
||
// }
|
||
// char localtimestamp[32] = { 0 };
|
||
// GetTimeNet(localtimestamp, 1);
|
||
// nowTimeStamp = strtol(localtimestamp, NULL, 10);
|
||
// // 避免在未同步时钟导致数据错误
|
||
// if(nowTimeStamp >= m_TimeStamp) {
|
||
// tmpTimeStamp = nowTimeStamp - m_TimeStamp;
|
||
// }
|
||
// else {
|
||
// tmpTimeStamp = m_TimeStamp - nowTimeStamp;
|
||
// }
|
||
// //if ((nowTimeStamp - m_TimeStamp) > 3) { 时间戳需要修改为绝对值,可能丢失时钟,或工作一会儿,才同步时钟,可能减出异常值
|
||
// if (tmpTimeStamp > 3 ) { // TODO: 时间戳需要修改为绝对值,可能丢失时钟,或工作一会儿,才同步时钟,可能减出异常值 print_info("yes!The time difference is more than 3,nowTimeStamp : %ld m_TimeStamp : %ld\n", nowTimeStamp, m_TimeStamp);
|
||
// //DealWave();
|
||
// m_TimeStamp = 0;
|
||
// offSize = 0;
|
||
// GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = NO_ENTER_TRANSMITTING_STATUS;
|
||
// GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0;
|
||
// // 准备重新恢复到 8888 PanID,正常接收特征数据
|
||
// mPackgeIndex = -1;
|
||
// //WriteLocalAddr(0x8888);
|
||
// //GlobalConfig::Zigbee_G.MyAddr = 0x8888;
|
||
|
||
// // WriteShortAddr2Zigbee(0x8888);
|
||
// // UpdateZigbeeInfoCtrl();
|
||
|
||
// } else {
|
||
// print_info("NO! The time difference is less than 3,nowTimeStamp : %ld m_TimeStamp : %ld\n", nowTimeStamp, m_TimeStamp);
|
||
// }
|
||
DealWave();
|
||
sleep(1);
|
||
}
|
||
|
||
}
|
||
std::vector<float> Uart::DealData(int iChannel,float coe,int sampleRate,int ACCSampleTime,string strProduct)
|
||
{
|
||
|
||
int waveCount = 0 ;
|
||
unsigned char data[1024*100]={0x00};
|
||
unsigned char outdata[1024*100]={0x00};
|
||
unsigned char dealdata[1024*100]={0x00};
|
||
long unsigned int new_len = 0,deallen = 0;
|
||
int compress = 0,count = 0;
|
||
long iTemp = 0;
|
||
char buf[8] = {0x00};
|
||
std::vector<float> vecData;
|
||
print_info("data1 = %02x\n",g_VecWaveDataX[0].Data[0]);
|
||
print_info("data2 = %02x\n",g_VecWaveDataY[0].Data[0]);
|
||
print_info("data3 = %02x\n",g_VecWaveDataZ[0].Data[0]);
|
||
size_t j = 0;
|
||
std::string strShortAddr = "";
|
||
if (iChannel == 3)
|
||
{
|
||
if (VecWaveDataX.size() > 0 )
|
||
{
|
||
g_VecWaveDataX.assign(VecWaveDataX.begin(), VecWaveDataX.end());
|
||
waveCount = VecWaveDataX.size();
|
||
}
|
||
waveCount = m_waveCountX;
|
||
for (; j < waveCount; j++)
|
||
{
|
||
RecvData recvData = g_VecWaveDataX[j];
|
||
memcpy(data + j * 92 ,recvData.Data,92);
|
||
}
|
||
memset(buf,0x00,sizeof(buf));
|
||
sprintf(buf, "%02x%02x", g_VecWaveDataX[0].ShortAddr[0], g_VecWaveDataX[0].ShortAddr[1]);
|
||
strShortAddr = std::string(buf);
|
||
compress = g_mapCompress[strShortAddr].compressChannelX;
|
||
count = g_mapCompress[strShortAddr].CountX;
|
||
}
|
||
if (iChannel == 4)
|
||
{
|
||
if (VecWaveDataY.size() > 0 )
|
||
{
|
||
g_VecWaveDataY.assign(VecWaveDataY.begin(), VecWaveDataY.end());
|
||
waveCount = VecWaveDataY.size();
|
||
}
|
||
waveCount = m_waveCountY;
|
||
for (; j < waveCount; j++)
|
||
{
|
||
RecvData recvData = g_VecWaveDataY[j];
|
||
memcpy(data + j * 92 ,recvData.Data,92);
|
||
}
|
||
memset(buf,0x00,sizeof(buf));
|
||
sprintf(buf, "%02x%02x", g_VecWaveDataY[0].ShortAddr[0], g_VecWaveDataY[0].ShortAddr[1]);
|
||
strShortAddr = std::string(buf);
|
||
compress = g_mapCompress[strShortAddr].compressChannelY;
|
||
count = g_mapCompress[strShortAddr].CountY;
|
||
}
|
||
if (iChannel == 5)
|
||
{
|
||
if (VecWaveDataZ.size() > 0 )
|
||
{
|
||
g_VecWaveDataZ.assign(VecWaveDataZ.begin(), VecWaveDataZ.end());
|
||
waveCount = VecWaveDataZ.size();
|
||
}
|
||
waveCount = m_waveCountZ;
|
||
for (; j < waveCount; j++)
|
||
{
|
||
RecvData recvData = g_VecWaveDataZ[j];
|
||
memcpy(data + j * 92 ,recvData.Data,92);
|
||
}
|
||
memset(buf,0x00,sizeof(buf));
|
||
sprintf(buf, "%02x%02x", g_VecWaveDataZ[0].ShortAddr[0], g_VecWaveDataZ[0].ShortAddr[1]);
|
||
strShortAddr = std::string(buf);
|
||
compress = g_mapCompress[strShortAddr].compressChannelZ;
|
||
count = g_mapCompress[strShortAddr].CountZ;
|
||
}
|
||
if(j * 92 < count)
|
||
return vecData;
|
||
print_info("len = %d,data = %02x,iChannel = %d,compress = %d,count = %d\n",j,data[0],iChannel,compress,count);
|
||
if(compress){
|
||
print_info("iChannel = %d,compress = %d\n",iChannel,compress);
|
||
int r = lzo1x_decompress(data,count,outdata,&new_len,NULL);
|
||
print_info("lzo1x_decompress end\n");
|
||
if (r == LZO_E_OK ){
|
||
printf("decompressed %lu bytes back into %lu bytes\n",
|
||
(unsigned long) j * 92, (unsigned long) new_len);
|
||
LOG_INFO("iChannel = %d ,ShortAddr = %s decompressed %lu bytes back into %lu bytes\n",iChannel,strShortAddr.c_str(),
|
||
(unsigned long) j * 92, (unsigned long) new_len);
|
||
}
|
||
else
|
||
{
|
||
/* this should NEVER happen */
|
||
printf("internal error - decompression failed: %d\n", r);
|
||
LOG_ERROR("internal error - decompression failed: %d,channel = %d,ShortAddr = %s\n", r,iChannel,strShortAddr.c_str());
|
||
return vecData;
|
||
}
|
||
memcpy(dealdata,outdata,new_len);
|
||
deallen = new_len;
|
||
}else{
|
||
memcpy(dealdata,data,j * 92);
|
||
deallen = j * 92;
|
||
}
|
||
print_info("len = %d,dealdata = %02x\n",deallen,dealdata[0]);
|
||
for (int i = 0; i < deallen; i++) {
|
||
float fTemp = 0.0;
|
||
memset(buf, 0, 8);
|
||
sprintf(buf, "%02x%02x", dealdata[2*i+1],dealdata[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
|
||
}
|
||
vecData.push_back(fTemp);
|
||
if(strProduct == "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(strProduct == "02"){
|
||
if(vecData.size() == 8192 && iChannel == 3 ){//过滤数据包结尾空数据
|
||
break;
|
||
}
|
||
if(vecData.size() == 8192 && iChannel == 4 ){//过滤数据包结尾空数据
|
||
break;
|
||
}
|
||
if(vecData.size() == sampleRate * ACCSampleTime && iChannel == 5 ){//过滤数据包结尾空数据
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
return vecData;
|
||
}
|
||
void Uart::DealWave()
|
||
{
|
||
//LOG_DEBUG("begin deal Wave data !\n");
|
||
//print_blue("wave data size is(m_VecWaveData.size) : %d\n",m_VecWaveData.size());
|
||
std::string strShortAddr = "";
|
||
std::string strShortAddrTemp;
|
||
std::string strLongAddr = "";
|
||
std::string strMeasurementID= "";
|
||
std::string strFileName = "";
|
||
std::string strProduct = "";
|
||
int iChannel = 0;
|
||
int iChannelTemp = 0;
|
||
|
||
std::vector<float> vecData;
|
||
|
||
RecvData recvTemp;
|
||
if (m_waveTrans) { //对每个传感器的每个通道进行遍历然后处理数据,例如:传感器1x轴的数据处理完后,再去处理y轴的。传感器1的所有数据处理完后,再处理传感器2的
|
||
char getLongAddr_sql[32] = { 0 };
|
||
sprintf(getLongAddr_sql, "zigbeeShortAddr='%s'", m_strDestShortAddr.c_str());
|
||
vec_t res = sql_ctl->GetDataSingleLine(T_SENSOR_INFO(TNAME)," * ",getLongAddr_sql);
|
||
strLongAddr = res[0];
|
||
strMeasurementID = res[44];
|
||
if ( 0 == strLongAddr.length() ) {
|
||
sleep(1);
|
||
return;
|
||
}
|
||
std::string ran = "";
|
||
int n = 0;
|
||
int range = 0;
|
||
float coe = 0;
|
||
int sampleRate = 0,ACCSampleTime = 0;
|
||
char getrange[32] = {0};
|
||
std::string str = "range";
|
||
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());
|
||
strProduct = res[17];
|
||
memset(getrange, 0, 32);
|
||
sprintf(getrange, "%s", ran.c_str());
|
||
n = (int)strtol(getrange, NULL, 32);
|
||
if (m_waveCountX > 0 || VecWaveDataX.size() > 0)
|
||
{
|
||
print_info("m_waveCountX = %d,VecWaveData = %d\n",m_waveCountX,VecWaveDataX.size());
|
||
coe = Calcoe(n,3,strProduct,range);
|
||
vecData = DealData(3,coe,sampleRate,ACCSampleTime,strProduct);
|
||
WriteDatFile(sampleRate, strMeasurementID, 3,vecData);
|
||
m_waveCountX = 0;
|
||
g_VecWaveDataX.clear();
|
||
VecWaveDataX.clear();
|
||
}
|
||
if (m_waveCountY > 0 || VecWaveDataY.size() > 0)
|
||
{
|
||
print_info("m_waveCountX = %d,VecWaveData = %d\n",m_waveCountY,VecWaveDataY.size());
|
||
coe = Calcoe(n,4,strProduct,range);
|
||
vecData = DealData(4,coe,sampleRate,ACCSampleTime,strProduct);
|
||
WriteDatFile(sampleRate, strMeasurementID, 4,vecData);
|
||
m_waveCountY = 0;
|
||
g_VecWaveDataY.clear();
|
||
VecWaveDataY.clear();
|
||
}
|
||
if (m_waveCountZ > 0 || VecWaveDataZ.size() > 0)
|
||
{
|
||
print_info("m_waveCountZ = %d,VecWaveDataZ = %d\n",m_waveCountZ,VecWaveDataZ.size());
|
||
coe = Calcoe(n,5,strProduct,range);
|
||
vecData = DealData(5,coe,sampleRate,ACCSampleTime,strProduct);
|
||
WriteDatFile(sampleRate, strMeasurementID, 5,vecData);
|
||
m_waveCountZ = 0;
|
||
g_VecWaveDataZ.clear();
|
||
VecWaveDataZ.clear();
|
||
|
||
}
|
||
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);
|
||
m_waveTrans = false;
|
||
}
|
||
|
||
|
||
|
||
|
||
//string strData = sql_ctl->GetNodeConfigureInfor(whereCon);
|
||
//data_publish(strData.c_str(), GlobalConfig::Topic_G.mPubConfig.c_str());
|
||
// memset(buf, 0, 8);
|
||
// sprintf(buf, "%02x%02x", recvTemp.ShortAddr[0], recvTemp.ShortAddr[1]);
|
||
// std::string strShortAddr = std::string(buf);
|
||
|
||
}
|
||
float Uart::Calcoe(int ran,int iChannel,string& product,int range)
|
||
{
|
||
float coe = 0.0;
|
||
if(product== "01"){
|
||
switch (ran)
|
||
{
|
||
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;
|
||
}
|
||
}else if(product == "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;
|
||
}
|
||
}
|
||
}
|
||
return coe;
|
||
}
|
||
void Uart::WriteDatFile(int sampleRate,string& strMeasurementID,int iChannel,std::vector<float>& vecData)
|
||
{
|
||
if(vecData.size() <= 0)
|
||
return;
|
||
std::string strFileName = "";
|
||
char localtimestamp[32] = { 0 };
|
||
GetTimeNet(localtimestamp, 1);
|
||
std::string nowTimetamp = std::string(localtimestamp);
|
||
std::string strChannelID = "";
|
||
switch (iChannel)
|
||
{
|
||
case 3:{
|
||
strFileName = "/opt/data/" + strMeasurementID + "-X.dat";
|
||
strChannelID = strMeasurementID + "-X";
|
||
}
|
||
break;
|
||
case 4:{
|
||
strFileName = "/opt/data/" + strMeasurementID + "-Y.dat";
|
||
strChannelID = strMeasurementID + "-Y";
|
||
}
|
||
break;
|
||
case 5:{
|
||
strFileName = "/opt/data/" + strMeasurementID + "-Z.dat";
|
||
strChannelID = strMeasurementID + "-Z";
|
||
}
|
||
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");
|
||
fwrite(localtimestamp,sizeof(localtimestamp),1,fp);
|
||
print_info("fopen FIle vecData.size : %d\n", vecData.size());
|
||
float mean = pCalculation->mean(vecData);
|
||
float frTemp;
|
||
char buf[33]={0x00};
|
||
std::string strWaveData = "";
|
||
for (int i = 0; i < vecData.size(); i++) {
|
||
frTemp = vecData[i] - mean;
|
||
fwrite(&frTemp,sizeof(float),1,fp);
|
||
memset(buf,0x00,sizeof(buf));
|
||
sprintf(buf, "%.2f", frTemp);
|
||
std::string waveTemp(buf);
|
||
if(i == 0)
|
||
strWaveData = waveTemp;
|
||
else
|
||
strWaveData = strWaveData + "," + waveTemp;
|
||
|
||
if (i % 100 == 0)
|
||
{
|
||
mssleep(5000);
|
||
}
|
||
}
|
||
|
||
fclose(fp);
|
||
//
|
||
Json::Value valWaveData;
|
||
int length = vecData.size();
|
||
valWaveData["number"] = sampleRate;
|
||
valWaveData["channelId"] = strChannelID;
|
||
valWaveData["dataNodeNo"] = strMeasurementID;
|
||
valWaveData["dataNodeGatewayNo"] = GlobalConfig::MacAddr_G;
|
||
valWaveData["SensorEngineeringUnit"] = "";
|
||
valWaveData["timeStamp"] = nowTimetamp;
|
||
valWaveData["waveData"] = strWaveData;
|
||
valWaveData["mean"] = mean;
|
||
Json::FastWriter WaveValue;
|
||
std::string WaveData = WaveValue.write(valWaveData);
|
||
|
||
char selectCon[128] = { 0 };
|
||
sprintf(selectCon, "channelID='%s' 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 timeStamp = '%s'", strChannelID.c_str(),strTime.c_str());
|
||
print_info("update static data to sql\n");
|
||
sql_ctl->UpdateTableData("t_data_waveSend", updateSql, whereCon);
|
||
}
|
||
//传感器发来的数据包中的表示设备信息的数据转化为json格式后,通过调用data_publish将数据传给mqttclient : Topic:wireless/cmd/60294D203717
|
||
|
||
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,size = %d\n", strFileName.c_str(),vecData.size());
|
||
}
|
||
#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
|
||
print_info("write data to filename %s\n", strFileName.c_str());
|
||
std::vector<float>().swap(vecData);
|
||
sleep(1);
|
||
|
||
}
|
||
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;
|
||
|
||
}
|
||
void Uart::modify_distaddr_info(unsigned short id, char * zigbee,unsigned char* distAddr)
|
||
{
|
||
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);
|
||
|
||
mssleep(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)
|
||
{
|
||
mssleep(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);
|
||
}
|
||
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();
|
||
|
||
}
|
||
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;
|
||
command[4] = ((char *)&id)[0];
|
||
command[5] = ((char *)&id)[1];
|
||
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];
|
||
}
|
||
command[i] = 0xaa;
|
||
WriteToUart(command, i+1);
|
||
sleep(1);
|
||
}
|
||
|
||
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);
|
||
//swap((char *)&tmp);
|
||
GlobalConfig::Zigbee_G.Chan = pad1;
|
||
modify_info(tmp, (char *)& GlobalConfig::Zigbee_G);
|
||
|
||
gpio_set(GlobalConfig::GPIO_G.zigReset,0);
|
||
mssleep(10000);
|
||
gpio_set(GlobalConfig::GPIO_G.zigReset,1);
|
||
}
|
||
|
||
void Uart::WritePanId2Zigbee(unsigned short pad)
|
||
{
|
||
print_info("WritePanId2Zigbee : %d\n", pad);
|
||
unsigned short pad1 = pad,tmp;
|
||
tmp = GlobalConfig::Zigbee_G.MyAddr;
|
||
print_info("MyAddr : %d\n", GlobalConfig::Zigbee_G.MyAddr);
|
||
swap((char *)&pad1);
|
||
//swap((char *)&tmp);
|
||
GlobalConfig::Zigbee_G.PanID = pad1;
|
||
modify_info(tmp, (char *)&GlobalConfig::Zigbee_G);
|
||
|
||
gpio_set(GlobalConfig::GPIO_G.zigReset,0);
|
||
mssleep(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);
|
||
mssleep(10000);
|
||
gpio_set(GlobalConfig::GPIO_G.zigReset,1);
|
||
}
|
||
/*void Uart::WriteLocalAddr(unsigned short id)
|
||
{
|
||
gpio_set(116,0);
|
||
mssleep(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);
|
||
mssleep(600000);
|
||
gpio_set(116,0);
|
||
mssleep(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);
|
||
mssleep(10000);
|
||
gpio_set(GlobalConfig::GPIO_G.zigReset,1);
|
||
}
|
||
|
||
void Uart::WriteShortAddr2Zigbee(unsigned short pad)
|
||
{
|
||
print_info("WriteShortAddr2Zigbee : %4x\n", (unsigned short)pad);
|
||
unsigned short pad1 = pad,tmp;
|
||
tmp = GlobalConfig::Zigbee_G.MyAddr;
|
||
//swap((char *)&pad1);
|
||
//swap((char *)&tmp);
|
||
GlobalConfig::Zigbee_G.MyAddr = pad1;
|
||
modify_info(tmp, (char *)& GlobalConfig::Zigbee_G);
|
||
|
||
gpio_set(GlobalConfig::GPIO_G.zigReset,0);
|
||
mssleep(10000);
|
||
gpio_set(GlobalConfig::GPIO_G.zigReset,1);
|
||
sleep(1);
|
||
}
|
||
|
||
void Uart::WriteShortAddr_DistAddr2Zigbee(unsigned short pad,unsigned char* pDestShortAddr)
|
||
{
|
||
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};
|
||
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);
|
||
print_info("DstAddr = %x\n",GlobalConfig::Zigbee_G.DstAddr);
|
||
GlobalConfig::ZigbeeInfo_G.MyAddr = "9999";
|
||
modify_info(tmp, (char *)& GlobalConfig::Zigbee_G);
|
||
|
||
gpio_set(GlobalConfig::GPIO_G.zigReset,0);
|
||
mssleep(10000);
|
||
gpio_set(GlobalConfig::GPIO_G.zigReset,1);
|
||
}
|
||
int Uart::CheckZigbeeACK()
|
||
{
|
||
if(gpio_read(GlobalConfig::GPIO_G.zigAckrep) == 48)
|
||
gpio_set(GlobalConfig::GPIO_G.zigAckreset,1);
|
||
int time = 0,value = 0,iRet = -1;
|
||
do{
|
||
value = gpio_read(GlobalConfig::GPIO_G.zigAckrep);
|
||
if(value == 49)
|
||
{
|
||
iRet = 0;
|
||
break;
|
||
}
|
||
mssleep(10000);
|
||
}while(time < 150);
|
||
return iRet;
|
||
}
|
||
void Uart::ZigbeeInit()
|
||
{
|
||
std::string strPanId = sql_ctl->GetData("t_gateway_info","zigbeePanID",NULL);
|
||
|
||
{
|
||
unsigned short shortAddr = 0x8888;
|
||
//GlobalConfig::Zigbee_G.MyAddr = shortAddr;
|
||
//WriteLocalAddr(shortAddr);
|
||
WriteShortAddr2Zigbee(shortAddr);
|
||
mssleep(100000);
|
||
// 更新GlobalConfig::ZigbeeInfo_G.MyAddr,用于外部显示
|
||
|
||
}
|
||
|
||
//std::string strPanId = GlobalConfig::MacAddr_G.substr(8);
|
||
print_info("strPanId : %s\n", strPanId.c_str());
|
||
print_info("MacAddr_G : %s\n", GlobalConfig::MacAddr_G.c_str());
|
||
// 新增管理ZigBee代码
|
||
std::string strchan = ReadStrByOpt(ZIGBEECONFIG, "Zigbee", "channel");
|
||
|
||
unsigned short Chan = (unsigned short)strtol(strchan.c_str(), NULL, 10);
|
||
print_info("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%the chan = %u\n",Chan);
|
||
print_info("ZigbeeInfo_G.Channel=%d\n",GlobalConfig::ZigbeeInfo_G.Channel);
|
||
if(Chan > 10 && Chan < 27)
|
||
{
|
||
//if (Chan != GlobalConfig::ZigbeeInfo_G.Channel)
|
||
{
|
||
WriteChanl2Zigbee(Chan);
|
||
//modify_Localchannel(Chan);
|
||
mssleep(100000);
|
||
}
|
||
}
|
||
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()))
|
||
{
|
||
long lShortAddr = strtol(strPanId.c_str(), NULL, 16);
|
||
unsigned short panid = lShortAddr & 0xffff;
|
||
//modify_LocalPanID(panid);
|
||
|
||
WritePanId2Zigbee(panid);
|
||
mssleep(100000);
|
||
}
|
||
|
||
//if (0 != GlobalConfig::ZigbeeInfo_G.MyAddr.compare("8888"))
|
||
|
||
print_info("ZigbeeInfo_G.MyAddr=%s\n",GlobalConfig::ZigbeeInfo_G.MyAddr.c_str());
|
||
|
||
}
|
||
|
||
|