diff --git a/.cproject b/.cproject index cc56c8b..55e6828 100644 --- a/.cproject +++ b/.cproject @@ -91,19 +91,19 @@ - + - + diff --git a/common/SH_CommonFunc.cpp b/common/SH_CommonFunc.cpp index 0d4f291..016dd81 100644 --- a/common/SH_CommonFunc.cpp +++ b/common/SH_CommonFunc.cpp @@ -142,7 +142,7 @@ std::string ReadStrByOpt(std::string filename, std::string config, std::string o is.close(); return value; } -std::vector ReadStrByOpt(std::string filename,std::string strUpdataFileName) +std::vector ReadStrByOpt(std::string filename,std::string& strUpdataFileName) { boost::mutex::scoped_lock lock(s_config_mu); Json::Value root,hwVersion; diff --git a/common/SH_CommonFunc.hpp b/common/SH_CommonFunc.hpp index 80595dd..6da3656 100644 --- a/common/SH_CommonFunc.hpp +++ b/common/SH_CommonFunc.hpp @@ -45,8 +45,17 @@ #define BUILD_UINT16(x,y) (((x & 0x00FFu) << 8u) | (y & 0x00FFu)) // 生成UINT32 数据 #define BUILD_UINT32(u,v,x,y) (((u & 0x00FFu) << 24u) | (v & 0x00FFu) << 16u) | (((x & 0x00FFu) << 8u) | (y & 0x00FFu)) -#define GENERAL_BUF_SIZE 128*1024 +// 获取UINT32的高低字节 +#define UINT32_HIGH_1(x) ((x & 0xFF000000u) >> 24u) +#define UINT32_HIGH_2(x) ((x & 0x00FF0000u) >> 16u) +#define UINT32_LOW_1(x) ((x & 0x0000FF00u) >> 8u) +#define UINT32_LOW_2(x) ((x & 0x000000FFu)) +// 获取UINT32的高低字节 +#define UINT16_HIGH(x) ((x & 0xFF00u) >> 8u) +#define UINT16_LOW(x) ((x & 0x00FFu)) + +#define GENERAL_BUF_SIZE 128*1024*10 using namespace std; enum TIME_SIZE{ @@ -591,7 +600,7 @@ extern bool CheckIP(const char *ip); bool IsValidMask(std::string mask); //read update config file -extern std::vector ReadStrByOpt(std::string filename,std::string strUpdataFileName); +extern std::vector ReadStrByOpt(std::string filename,std::string& strUpdataFileName); extern void swap(char *data); diff --git a/common/SH_global.h b/common/SH_global.h index c416dec..c89673c 100644 --- a/common/SH_global.h +++ b/common/SH_global.h @@ -16,6 +16,7 @@ enum enumZigBeeTransmitStatus { ENTER_TRANSMITTING_STATUS }; + class GlobalConfig { public : diff --git a/jsonparse/SH_JsonCmd.cpp b/jsonparse/SH_JsonCmd.cpp index 2c77fd2..526cd86 100644 --- a/jsonparse/SH_JsonCmd.cpp +++ b/jsonparse/SH_JsonCmd.cpp @@ -555,7 +555,7 @@ std::string JsonData::JsonCmd_Cgi_09() jsonVal[JSON_FIELD_CMD] = "09"; jsonVal["success"] = true; jsonVal["message"] = " "; - + vec_t vetRes = sql_ctl->GetDataMultiLineOfOneColumn(T_SENSOR_INFO(TNAME), T_SENSOR_INFO(DATANODENO), NULL); int nSize = vetRes.size(); @@ -565,7 +565,7 @@ std::string JsonData::JsonCmd_Cgi_09() Json::Value jsSensor; std::string strDataNodeNo = vetRes[i]; char whereCon[64] = {}; - sprintf(whereCon, "dataNodeNo='%s'", strDataNodeNo.c_str()); + sprintf(whereCon, "dataNodeNo='%s' ORDER BY timeStamp DESC LIMIT 0,3", strDataNodeNo.c_str()); // 自数据库获取传感器特征数据 array_t arrRes; arrRes = sql_ctl->GetDataMultiLine(T_DATA_INFO(TNAME), "*", whereCon); @@ -595,7 +595,7 @@ std::string JsonData::JsonCmd_Cgi_09() } char selectCon[128] = { 0 }; - sprintf(selectCon, "dataNodeNo='%s'", strDataNodeNo.c_str()); + sprintf(selectCon, "dataNodeNo='%s' ORDER BY timeStamp DESC LIMIT 0,1", strDataNodeNo.c_str()); vec_t vecRes = sql_ctl->GetDataSingleLine(T_DATASTATIC_INFO(TNAME), "*", selectCon); Json::Value jsStaticData; if(vecRes.size()>0){ @@ -782,6 +782,7 @@ std::string JsonData::JsonCmd_Cgi_26() array_t arrRes; arrRes = sql_ctl->GetDataMultiLine(T_SENSOR_INFO(TNAME), "*", NULL); int iResult = arrRes.size(); + printf("iResult = %d\n",iResult); if (iResult > 0) { for (int j = 0; j < iResult; j++) { Json::Value jsSensorData; @@ -792,31 +793,39 @@ std::string JsonData::JsonCmd_Cgi_26() jsSensorData["zigbeeFlag"] = boost::lexical_cast(arrRes[j][4]); jsSensorData["temTopFlag"] = boost::lexical_cast(arrRes[j][5]); jsSensorData["temBotFlag"] = boost::lexical_cast(arrRes[j][6]); - jsSensorData["equipsta"] = boost::lexical_cast(arrRes[j][7]); jsSensorData["hardVersion"] = arrRes[j][8]; jsSensorData["softVersion"] = arrRes[j][9]; jsSensorData["bpNo"] = arrRes[j][10]; jsSensorData["serialNo"] = arrRes[j][11]; jsSensorData["firstPowerTime"] = arrRes[j][12]; - jsSensorData["configFlag"] = boost::lexical_cast(arrRes[j][13]); - jsSensorData["startBrands"] = arrRes[j][14]; - jsSensorData["stopBrands"] = arrRes[j][15]; - jsSensorData["featureInterVal"] = boost::lexical_cast(arrRes[j][16]); - jsSensorData["waveInterVal"] = boost::lexical_cast(arrRes[j][17]); - jsSensorData["samplingRate"] = boost::lexical_cast(arrRes[j][18]); - jsSensorData["scope"] = arrRes[j][19]; - jsSensorData["range"] = boost::lexical_cast(arrRes[j][20]);; - jsSensorData["envelopeBandPass"] = arrRes[j][21]; - jsSensorData["faultFrequency"] = arrRes[j][12]; - jsSensorData["zigbeePanId"] = arrRes[j][23]; - jsSensorData["zigbeeChannel"] = boost::lexical_cast(arrRes[j][24]); - jsSensorData["zigbeeAddr"] = arrRes[j][25]; - jsSensorData["zigbeeLongAddr"] = arrRes[j][26]; - jsSensorData["zigbeeDesAddr"] = arrRes[j][27]; - jsSensorData["status"] = boost::lexical_cast(arrRes[j][28]); - jsSensorData["timeStamp"] = arrRes[j][29]; - jsSensorData["viff"] = boost::lexical_cast(arrRes[j][30]); - jsSensorData["RSSI"] = boost::lexical_cast(arrRes[j][31]); + jsSensorData["WakeupTime"] = boost::lexical_cast(arrRes[j][13]); + jsSensorData["StaticTime"] = boost::lexical_cast(arrRes[j][14]); + jsSensorData["WaveTime"] = boost::lexical_cast(arrRes[j][15]); + jsSensorData["BateryV"] = arrRes[j][16]; + jsSensorData["ProductNo"] = arrRes[j][17]; + jsSensorData["configFlag"] = boost::lexical_cast(arrRes[j][18]); + jsSensorData["startBrands"] = arrRes[j][19]; + jsSensorData["stopBrands"] = arrRes[j][20]; + jsSensorData["featureInterVal"] = boost::lexical_cast(arrRes[j][21]); + jsSensorData["waveInterVal"] = boost::lexical_cast(arrRes[j][22]); + jsSensorData["samplingRate"] = boost::lexical_cast(arrRes[j][23]); + // jsSensorData["scope"] = boost::lexical_cast(arrRes[j][24]); + jsSensorData["range"] = boost::lexical_cast(arrRes[j][25]); + jsSensorData["envelopeBandPass"] = arrRes[j][26]; + jsSensorData["faultFrequency"] = arrRes[j][27]; + jsSensorData["zigbeePanId"] = arrRes[j][28]; + jsSensorData["zigbeeChannel"] = boost::lexical_cast(arrRes[j][29]); + jsSensorData["zigbeeAddr"] = arrRes[j][30]; + jsSensorData["zigbeeLongAddr"] = arrRes[j][31]; + jsSensorData["zigbeeDesAddr"] = arrRes[j][32]; + jsSensorData["ZigbeePower"] = boost::lexical_cast(arrRes[j][33]); + jsSensorData["ZigbeeRetry"] = boost::lexical_cast(arrRes[j][34]); + jsSensorData["ZigbeeRetryGap"] = boost::lexical_cast(arrRes[j][35]); + jsSensorData["ACCSampleTime"] = boost::lexical_cast(arrRes[j][36]); + jsSensorData["status"] = boost::lexical_cast(arrRes[j][37]); + jsSensorData["timeStamp"] = arrRes[j][38]; + jsSensorData["viff"] = boost::lexical_cast(arrRes[j][39]); + jsSensorData["RSSI"] = boost::lexical_cast(arrRes[j][40]); jsArray.append(jsSensorData); } } else { @@ -1046,6 +1055,7 @@ std::string JsonData::JsonCmd_Cgi_30(Param_30 ¶m) //进行傅立叶变换 pCalculation->FFTSpec(vecWave, fftWave); printf("2---------------------------------------------->%d\n",fftWave.size()); + printf("2---------------------------------------------->%f\n",fftWave[10]); /*std::string strWaveData2 = ""; char bufff[32] = {0}; for(int k=0;kfftWave.size()=%d\n", fftWave.size()); //print_info("after fft---------------------------------------------------> %s,fftWave.size()=%d\n", strWaveData2.c_str(),fftWave.size()); - int flag = param.mPackageFlag; + /* int flag = param.mPackageFlag; //print_info("*********************************param.mPackageFlag=%d\n",param.mPackageFlag); flag = (flag + 1) * 1024; int number = fftWave.size(); @@ -1104,7 +1114,39 @@ std::string JsonData::JsonCmd_Cgi_30(Param_30 ¶m) { max = 1; } - jsBody["packageMax"] = max; + jsBody["packageMax"] = max; */ + int flag = param.mPackageFlag; + flag = (flag + 1) * 1024; + int number = fftWave.size(); + int start = param.mPackageFlag * 1024; + printf("param.mPackageFlag = %d\n",param.mPackageFlag); + printf("param.start = %d\n",start); + printf("param.flag = %d\n",flag); + if (number < 1024) { + flag = number; + start = 0; + } + char buf[32]; + for (int i = start; i < flag; i++) { + if ( i == start ) { + memset(buf, 0, 32); + sprintf(buf, "%.6f", fftWave[i]); + std::string waveTemp(buf); + strWaveData = waveTemp; + + } else { + memset(buf, 0, 32); + sprintf(buf, "%.6f", fftWave[i]); + std::string waveTemp(buf); + strWaveData = strWaveData + "," + waveTemp; + } + } + + int max = number / 1024; + if (max == 0 && number > 0) { + max = 1; + } + jsBody["packageMax"] = max; } } else { jsonVal["success"] = false; diff --git a/localserver/SH_LocalServer.cpp b/localserver/SH_LocalServer.cpp index 44f658b..23c608e 100644 --- a/localserver/SH_LocalServer.cpp +++ b/localserver/SH_LocalServer.cpp @@ -352,6 +352,7 @@ try{ case 26:{ JsonData jd; std::string data = jd.JsonCmd_Cgi_26(); + printf("data = %s\n",data.c_str()); return data; } break; diff --git a/threadfunc/SH_ThreadFunc.cpp b/threadfunc/SH_ThreadFunc.cpp index 77eeff3..140f075 100644 --- a/threadfunc/SH_ThreadFunc.cpp +++ b/threadfunc/SH_ThreadFunc.cpp @@ -136,13 +136,15 @@ void UartStart() { // onReceiveUart cb = (onReceiveUart)&ProtoConvert::HandleFromUart; // pUart->setCallBack(cb); + pUart->InitUart(); + while (GlobalConfig::QuitFlag_G) { - pUart->InitUart(); + pUart->UpdateZigbeeInfoCtrl(); pUart->ReadFromUart(); pUart->Run(); - pUart->Stop(); - boost::this_thread::sleep(boost::posix_time::seconds(1)); +// pUart->Stop(); + boost::this_thread::sleep(boost::posix_time::milliseconds(10)); } } diff --git a/uart/SH_Uart.cpp b/uart/SH_Uart.cpp index ade4c9c..0ae8da5 100644 --- a/uart/SH_Uart.cpp +++ b/uart/SH_Uart.cpp @@ -10,8 +10,9 @@ namespace{ Uart *pUart = Uart::instance(); } - - +char g_UartRecvBuf[GENERAL_BUF_SIZE]; +int offSize = 0; +int Size = 0; // namespace{ // PlatformInit *platform = PlatformInit::instance(); // LocalServer *wlServer = LocalServer::instance(); @@ -25,7 +26,6 @@ Uart::Uart():mUart(mIoSev),mStrand(mIoSev) { mRdLength = 0; memset(mUartRecvBuf,0,BUF_LENGTH); - update = false; } void Uart::InitUart() @@ -89,14 +89,15 @@ void Uart::WriteToUart(const char *strSend,int pLen) void Uart::ReadFromUart() { if (mUart.is_open()) { - mUart.async_read_some(buffer(&mUartRecvBuf[mRdLength], BUF_LENGTH-mRdLength-4), + print_info("async_read_some,wait......................................\n"); + mUart.async_read_some(buffer(&mUartRecvBuf[mRdLength], BUF_LENGTH-mRdLength-Size), mStrand.wrap( boost::bind(&Uart::ReadHandle,this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)) ); } - print_info("async_read_some,wait......................................\n"); + } void Uart::Run() @@ -116,16 +117,16 @@ void Uart::ReadHandle(const boost::system::error_code &ec,size_t bytesRead) try{ if (ec) { mRdLength = 0; - ReadFromUart(); +// ReadFromUart(); return ; } else { //长度检测 - /* printf("recv uart len in ZigBee short address %4x : %d\n recv uart data:", + printf("recv uart len in ZigBee short address %4x : %d\n recv uart data:", (unsigned short)GlobalConfig::Zigbee_G.MyAddr, bytesRead); - for (unsigned int i = 0; i < bytesRead; i++) - print_debug("%02X ",mUartRecvBuf[i]&0xFF); - */ + for ( int i = 0; i < bytesRead; i++) + print_debug("%02X ",mUartRecvBuf[i]&0xFF); + int iPackageSize = bytesRead / 100; if (0 == iPackageSize) { iPackageSize = 1; @@ -136,9 +137,28 @@ try{ 通信,数据量处理不好就会产生很大的影响。我的逻辑:利用while循环,条件是判断bytesRead是否大于0,如果条件满足,再取数据包的前3字节, 判断命令码是ABBCCD还是AA55AA,如果是ABBCCD就处理74字节数据,如果是AA55AA就处理100自己数据,此时存放数据的buf减去处理的大小数据, 然后进入下一次while循环。*/ + + if((unsigned short)GlobalConfig::Zigbee_G.MyAddr == (unsigned short)0x9999){ + + for (int j = 0; j < iPackageSize; j++) { + char buf[6]; + char mUartRecvPackage[100] = {0}; + + memcpy(mUartRecvPackage, mUartRecvBuf+7-Size, 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); + std::string strHeadFlag(buf); + print_info("data package head command type:%s,Size = %d\n", strHeadFlag.c_str(),Size); + if ( 0 == strHeadFlag.compare("AA55AA") ) { + DealRecvData(mUartRecvPackage); + } + } + Size = 7; + }else{ for (int j = 0; j < iPackageSize; j++) { char buf[6]; - char buf1[2]; char mUartRecvPackage[100] = {0}; // 多包分包,解包, if (Count < 100) { @@ -147,6 +167,7 @@ try{ memcpy(mUartRecvPackage, &mUartRecvBuf[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); @@ -162,14 +183,13 @@ try{ } if ( 0 == strHeadFlag.compare("DEDFEF") ) { - update = true; LOG_INFO("ReadHandle flag = %s\n",strHeadFlag.c_str()); } if ( 0 == strHeadFlag.compare("AA55AA") ) { DealRecvData(mUartRecvPackage); } - } + }} } //读串口循环 @@ -261,16 +281,14 @@ void int2bytes(int i, unsigned char* bytes, int size) { bytes[2] = (unsigned char) ((0xff0000 & i) >> 16); bytes[3] = (unsigned char) ((0xff000000 & i) >> 24); } -void Uart::UpdateWirelessNode(unsigned char* id) +bool Uart::ReadUpdatePackge(unsigned char* pDestShortAddr) { - LOG_INFO("UpdateWirelessNode id = %02x %02x\n",id[0],id[1]); - //compare hardversion in update list std::vector strHWversion; std::string strFileName = ""; strHWversion = ReadStrByOpt("/opt/update/DataNode/config.json",strFileName); char gethardVersion_sql[32] = { 0 }; - sprintf(gethardVersion_sql, "zigbeeShortAddr='%s'", id); + sprintf(gethardVersion_sql, "zigbeeShortAddr='%02x%02x'", pDestShortAddr[0],pDestShortAddr[1]); std::string hardVersion = sql_ctl->GetData(T_SENSOR_INFO(TNAME), T_SENSOR_INFO(HARDVERSION), gethardVersion_sql); int thisindex = -1; for(int i = 0; i < strHWversion.size();i++){ @@ -281,8 +299,57 @@ void Uart::UpdateWirelessNode(unsigned char* id) } printf("thisindex = %d\n",thisindex); if(thisindex < 0) - return; + return false; + FILE * pFile=NULL; + int thisSize = 0; + strFileName = "/opt/update/DataNode/" + strFileName; + printf("strFileName = %s\n",strFileName.c_str()); + pFile = fopen (strFileName.c_str(),"rb"); + if (pFile==NULL){ + return false; + } + else + { + while (fgetc(pFile) != EOF) { + ++thisSize; + } + fclose (pFile); + } + unsigned short localAddr = 0x9999; + WriteShortAddr_DistAddr2Zigbee(localAddr,pDestShortAddr); + unsigned char Data[12]={0x00}; + unsigned char size[4]={0x00}; + printf("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]=0x07; + 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; + WriteToUart((const char*)Data,12); + boost::this_thread::sleep(boost::posix_time::milliseconds(1000)); + ReadFromUart(); + return true; +} +void Uart::UpdateWirelessNode(unsigned short shortAdd) +{ + LOG_INFO("UpdateWirelessNode id = %02x %02x\n",UINT16_HIGH(shortAdd),UINT16_LOW(shortAdd)); + + std::string strFileName = ""; + ReadStrByOpt("/opt/update/DataNode/config.json",strFileName); FILE * pFile=NULL; int thisSize = 0; char *buffer=NULL; @@ -298,40 +365,19 @@ void Uart::UpdateWirelessNode(unsigned char* id) buffer = (char*)malloc(thisSize);// fread (buffer, sizeof (char), thisSize, pFile); fclose (pFile); - unsigned char Data[12]={0x00}; - unsigned char size[4]={0x00}; - //帧头[3byte] 节点地址[2byte] 数据类型[1byte] 序号[1byte] 升级包大小[4byte] CRC校验[1byte] - Data[0]=0xAA; - Data[1]=0x55; - Data[2]=0xAA; - Data[3]=id[0] & 0xFF; - Data[4]=id[1] & 0xFF; - Data[5]=0x07; - 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; - WriteToUart((const char*)Data,12); - boost::this_thread::sleep(boost::posix_time::milliseconds(100)); 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; for(int j = 0; j < Count;j++){ UpdateData[0]=0xAA; UpdateData[1]=0x55; UpdateData[2]=0xAA; - UpdateData[3]=id[0]; - UpdateData[4]=id[1]; + UpdateData[3]=UINT16_HIGH(shortAdd); + UpdateData[4]=UINT16_LOW(shortAdd); UpdateData[5]=0x08; UpdateData[6]=0xff & j; memcpy(&UpdateData[7],buffer+92*j,92); @@ -341,7 +387,7 @@ void Uart::UpdateWirelessNode(unsigned char* id) } UpdateData[99] = tmp; WriteToUart((const char*)UpdateData,100); - boost::this_thread::sleep(boost::posix_time::milliseconds(100)); + boost::this_thread::sleep(boost::posix_time::milliseconds(200)); memset(UpdateData,0x00,sizeof(UpdateData)); } printf("Count =%d,lastSize = %d\n",Count,lastSize); @@ -349,8 +395,8 @@ void Uart::UpdateWirelessNode(unsigned char* id) UpdateData[0]=0xAA; UpdateData[1]=0x55; UpdateData[2]=0xAA; - UpdateData[3]=id[0]; - UpdateData[4]=id[1]; + UpdateData[3]=UINT16_HIGH(shortAdd); + UpdateData[4]=UINT16_LOW(shortAdd); UpdateData[5]=0x08; UpdateData[6]=0xff & Count; memcpy(&UpdateData[7],buffer+92*Count,lastSize); @@ -366,13 +412,16 @@ void Uart::UpdateWirelessNode(unsigned char* id) } free(buffer); ZigbeeInit(); - update = false; + Size = 0; LOG_INFO("UpdateWirelessNode success"); } void Uart::DealRecvData(const char *pData) { char buf[8]; - sprintf(buf, "%02d", pData[5]&0xFF); + 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 = boost::lexical_cast(buf); print_info("the data package type(1,2,3,4,5,6) is %s,%x\n",buf,pData[5]&0xFF); @@ -404,7 +453,7 @@ void Uart::DealRecvData(const char *pData) break; case 7:{//0x07:升级 - //DealDataNodeFeature(pData, 1); + UpdateWirelessNode(ushortAdd); } break; default: @@ -687,15 +736,24 @@ void Uart::DealDataNodeFeature(const char *pData, int flag) LOG_INFO("DealDataNodeFeature %02x%02x\n",pRecvData->ShortAddr[0], pRecvData->ShortAddr[1]); std::string strShortAddr = std::string(buf); - print_info("zigbeeShortAddr='%s'", strShortAddr.c_str()); + print_info("zigbeeShortAddr='%s'\n", strShortAddr.c_str()); if (1 == flag) { print_info("recv wave minute\n"); - unsigned short shortAddr = 0x9999; - - WriteShortAddr_DistAddr2Zigbee(shortAddr,pRecvData->ShortAddr); + unsigned short localAddr = 0x9999; + bool bUpdate = ReadUpdatePackge(pRecvData->ShortAddr); + if(!bUpdate){ + GlobalConfig::ZigbeeInfo_G.MyAddr = "9999"; + WriteShortAddr2Zigbee(localAddr); + } +/* bool bUpdate = ReadUpdatePackge(pRecvData->ShortAddr,buf); + if(bUpdate){ + WriteShortAddr_DistAddr2Zigbee(shortAddr,buf); + }else{ + WriteShortAddr2Zigbee(shortAddr); + }*/ // LOG_INFO("[DealDataNodeFeature] ZigBee PanID: %s ; Channel: %d ; MyAddr : %4x ",GlobalConfig::ZigbeeInfo_G.PanID.c_str(),GlobalConfig::ZigbeeInfo_G.Channel, // (unsigned short)GlobalConfig::Zigbee_G.MyAddr); - UpdateWirelessNode(pRecvData->ShortAddr); + // 进入传输原始数据状态,启动计数 60秒 GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = ENTER_TRANSMITTING_STATUS; GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0; @@ -750,19 +808,19 @@ void Uart::DealDataNodeFeature(const char *pData, int flag) , dataStatic.TemBot, dataStatic.Dip, dataStatic.Voltage); char whereCon[64] = {0}; sprintf(whereCon, "channelID='%s'", (strLongAddr + "-S").c_str()); - if ( 0 == sql_ctl->GetTableRows(T_DATASTATIC_INFO(TNAME), whereCon) ) { +// if ( 0 == sql_ctl->GetTableRows(T_DATASTATIC_INFO(TNAME), whereCon) ) { print_info("insert static data to sql\n"); char insertSql[1024] = { 0 }; sprintf(insertSql, "'%s','%s','%f','%f','%d','%d','%s'", strLongAddr.c_str(), (strLongAddr + "-S").c_str(), dataStatic.TemTop, dataStatic.TemBot, dataStatic.Dip, dataStatic.Voltage, nowTimetamp.c_str()); sql_ctl->InsertData(T_DATASTATIC_INFO(TNAME), insertSql); - } else { - print_info("update static data to sql\n"); + // } else { +/* print_info("update static data to sql\n"); char updateSql[1024] = { 0 }; sprintf(updateSql, "temTop='%f',temBot='%f',dip='%d',voltage='%d',timeStamp='%s'",\ dataStatic.TemTop, dataStatic.TemBot, dataStatic.Dip, dataStatic.Voltage, nowTimetamp.c_str()); sql_ctl->UpdateTableData(T_DATASTATIC_INFO(TNAME), updateSql, whereCon); - } + }*/ DataRecvDym dataDymX; memset(buf, 0, 8); @@ -1075,20 +1133,21 @@ void Uart::DealDataNodeFeature(const char *pData, int flag) memset(whereCon, 0, 64); sprintf(whereCon, "channelID='%s'", (strLongAddr + "-X").c_str()); - if ( 0 == sql_ctl->GetTableRows(T_DATA_INFO(TNAME), whereCon) ) { - char insertSql[1024] = { 0 }; + // if ( 0 == sql_ctl->GetTableRows(T_DATA_INFO(TNAME), whereCon) ) { +// 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','%s'", strLongAddr.c_str(), (strLongAddr + "-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()); sql_ctl->InsertData(T_DATA_INFO(TNAME), insertSql); - } else { - char updateSql[1024] = { 0 }; +// } else { +/* char updateSql[1024] = { 0 }; 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'",\ 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()); 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(), (strLongAddr + "-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()); @@ -1424,20 +1483,21 @@ void Uart::DealDataNodeFeature(const char *pData, int flag) memset(whereCon, 0, 64); sprintf(whereCon, "channelID='%s'", (strLongAddr + "-Y").c_str()); - if ( 0 == sql_ctl->GetTableRows(T_DATA_INFO(TNAME), whereCon) ) { - char insertSql[1024] = { 0 }; +// if ( 0 == sql_ctl->GetTableRows(T_DATA_INFO(TNAME), whereCon) ) { + // 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','%s'", strLongAddr.c_str(), (strLongAddr + "-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()); sql_ctl->InsertData(T_DATA_INFO(TNAME), insertSql); - } else { - char updateSql[1024] = { 0 }; + // } else { +/* char updateSql[1024] = { 0 }; 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'",\ 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()); 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(), (strLongAddr + "-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()); @@ -1773,20 +1833,21 @@ void Uart::DealDataNodeFeature(const char *pData, int flag) memset(whereCon, 0, 64); sprintf(whereCon, "channelID='%s'", (strLongAddr + "-Z").c_str()); - if ( 0 == sql_ctl->GetTableRows(T_DATA_INFO(TNAME), whereCon) ) { - char insertSql[1024] = { 0 }; +// if ( 0 == sql_ctl->GetTableRows(T_DATA_INFO(TNAME), whereCon) ) { +// 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','%s'", strLongAddr.c_str(), (strLongAddr + "-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()); sql_ctl->InsertData(T_DATA_INFO(TNAME), insertSql); - } else { + /* } else { char updateSql[1024] = { 0 }; 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'",\ 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()); 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(), (strLongAddr + "-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()); @@ -1877,11 +1938,12 @@ void Uart::DealWaveThread() //连续三秒没有原始数据,则处理缓存 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; - pUart->UpdateZigbeeInfoCtrl(); + Size = 0; GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = NO_ENTER_TRANSMITTING_STATUS; GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0; // 准备重新恢复到 8888 PanID,正常接收特征数据 ZigbeeInit(); + pUart->UpdateZigbeeInfoCtrl(); } else { print_info("NO! The time difference is less than 3,nowTimeStamp : %ld m_TimeStamp : %ld\n", nowTimeStamp, m_TimeStamp); } @@ -2064,7 +2126,7 @@ void Uart::DealWave() boost::this_thread::sleep(boost::posix_time::seconds(1)); } - + Size = 0; // memset(buf, 0, 8); // sprintf(buf, "%02x%02x", recvTemp.ShortAddr[0], recvTemp.ShortAddr[1]); // std::string strShortAddr = std::string(buf); @@ -2072,14 +2134,14 @@ void Uart::DealWave() } void Uart::modify_distaddr_info(unsigned short id, char * zigbee,unsigned char* distAddr) { - /*char command[6]; + char command[6]; command[0] = 0xDE; command[1] = 0xDF; command[2] = 0xEF; command[3] = 0xD2; - command[4] = destaddr[0]; - command[5] = destaddr[1]; - WriteToUart(command, 6);*/ + command[4] = distAddr[0]; + command[5] = distAddr[1]; + WriteToUart(command, 6); } void Uart::modify_info(unsigned short id, char * zigbee) @@ -2167,14 +2229,19 @@ void Uart::WriteShortAddr2Zigbee(unsigned short pad) zigbee_reset(tmp, 1); } -void Uart::WriteShortAddr_DistAddr2Zigbee(unsigned short pad,unsigned char* id) +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); - GlobalConfig::Zigbee_G.MyAddr = BUILD_UINT16(id[0],id[1]); + char tmpDest[8]={0x00}; + sprintf(tmpDest,"%02d%02d",pDestShortAddr[0],pDestShortAddr[1]); + memcpy(&GlobalConfig::Zigbee_G.DstAddr,pDestShortAddr,2); + GlobalConfig::Zigbee_G.MyAddr = pad1; +// GlobalConfig::Zigbee_G.DstAddr = (short)atoi((char*)tmpDest); + printf("DstAddr = %d\n",GlobalConfig::Zigbee_G.DstAddr); GlobalConfig::ZigbeeInfo_G.MyAddr = "9999"; modify_info(tmp, (char *)& GlobalConfig::Zigbee_G); zigbee_reset(tmp, 1); diff --git a/uart/SH_Uart.hpp b/uart/SH_Uart.hpp index b305468..1868e77 100644 --- a/uart/SH_Uart.hpp +++ b/uart/SH_Uart.hpp @@ -28,7 +28,7 @@ private : boost::asio::io_service mIoSev; boost::asio::serial_port mUart; int mRdLength; - enum{BUF_LENGTH = 1024}; + enum{BUF_LENGTH = 1107}; char mUartRecvBuf[BUF_LENGTH]; boost::mutex mLock; boost::asio::io_service::strand mStrand; @@ -58,8 +58,9 @@ public : void WriteChanl2Zigbee(unsigned char pad); void WritePanId2Zigbee(unsigned short pad); void WriteShortAddr2Zigbee(unsigned short pad); - void WriteShortAddr_DistAddr2Zigbee(unsigned short pad,unsigned char* id); - void UpdateWirelessNode(unsigned char* id); + void WriteShortAddr_DistAddr2Zigbee(unsigned short pad,unsigned char* pDestShortAddr); + void UpdateWirelessNode(unsigned short shortAdd); + bool ReadUpdatePackge(unsigned char* shortAddr); virtual void DataAnalysis_R(DevDataOfGwid &pData); virtual void DataAnalysis_W(DevData &pData,bool pFlag); virtual void ThreadInit(); @@ -69,7 +70,6 @@ private : onReceiveUart m_callback; std::vector m_VecWaveData; unsigned long m_TimeStamp; - bool update; };