2024-10-22 19:04:25 +08:00
# include "uart.hpp"
# include <signal.h>
# include <stdio.h>
# include <fcntl.h>
# include <unistd.h>
# include <termios.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <boost/thread/thread.hpp>
# include <boost/lexical_cast.hpp>
# include <boost/algorithm/string.hpp>
# include <boost/bind.hpp>
2024-10-24 16:01:21 +08:00
# include <boost/timer/timer.hpp>
2024-10-22 19:04:25 +08:00
# include <boost/date_time/posix_time/posix_time.hpp>
2024-10-23 19:51:01 +08:00
# include <zlog.h>
2024-10-23 22:25:03 +08:00
# include <json/json.h>
# include "mqttclient/mqtt_client.h"
2024-10-23 09:22:06 +08:00
# include "utility/serial.h"
2024-10-31 19:30:10 +08:00
# include "scheduler/schedule.hpp"
2024-11-20 16:45:04 +08:00
# include "jsonparse/communication_cmd.hpp"
2024-10-22 19:04:25 +08:00
2024-10-23 20:33:05 +08:00
extern zlog_category_t * zct ;
extern zlog_category_t * zbt ;
2024-10-23 22:25:03 +08:00
extern std : : map < std : : string , compressWaveChannel > g_mapCompress ;
2024-10-23 20:33:05 +08:00
2024-10-22 19:04:25 +08:00
int offSize = 0 ;
pTestRecvCallBack pTestRecv ;
2024-10-23 22:25:03 +08:00
extern std : : vector < RecvData > g_VecWaveDataX ;
extern std : : vector < RecvData > g_VecWaveDataY ;
extern std : : vector < RecvData > g_VecWaveDataZ ;
2026-03-02 19:02:28 +08:00
extern std : : vector < RecvData > g_VecWaveDataVolX ;
extern std : : vector < RecvData > g_VecWaveDataVolY ;
extern std : : vector < RecvData > g_VecWaveDataVolZ ;
2024-10-23 22:25:03 +08:00
2024-10-22 19:04:25 +08:00
int Uart : : UartRecv ( int fd , char srcshow , char * buffer ) {
char buff [ BUF_LENGTH ] ;
int ret = 0 ;
int maxSize = 0 ;
int offSize = 0 ;
int timeoutflag = 0 ;
char head [ ] = { 0xAA , 0x55 , 0xAA } ;
2024-11-04 21:35:50 +08:00
# ifdef G2UL_GATEWAY
2024-10-22 19:04:25 +08:00
char szbuffer [ BUF_LENGTH ] = { 0x00 } ;
2024-11-04 21:35:50 +08:00
# endif
2024-10-22 19:04:25 +08:00
while ( 1 ) {
2024-10-31 19:30:10 +08:00
if ( now_task = = WAVE_CMD ) {
2024-10-22 19:04:25 +08:00
memset ( buff , 0 , sizeof ( buff ) ) ;
ret = read_data ( fd , buff , BUF_LENGTH , 10 ) ;
if ( ret < = 0 ) {
2024-10-31 19:30:10 +08:00
timeoutflag + + ;
2025-04-30 15:17:01 +08:00
if ( timeoutflag > 200 ) {
2024-10-31 19:30:10 +08:00
DealReviveDuration ( wave_shortAddr ) ;
2024-11-13 17:50:54 +08:00
zlog_warn ( zct , " ===============0x9999 timeout= %d offSize = %d===============shortAddr = %02x%02x " , timeoutflag , offSize , UINT16_HIGH ( wave_shortAddr ) , UINT16_LOW ( wave_shortAddr ) ) ;
zlog_warn ( zct , " 0x9999 timeout %d===============Size = %d " , timeoutflag , offSize ) ;
2024-10-31 19:30:10 +08:00
FindRecvPackage ( offSize , mUartRecvTmpBuf , head ) ;
now_task = - 1 ;
timeoutflag = 0 ;
offSize = 0 ;
maxSize = 0 ;
tcflush ( fd , TCIFLUSH ) ;
2024-10-22 19:04:25 +08:00
mssleep ( 10000 ) ;
2024-10-31 19:30:10 +08:00
wave_trans_ = true ;
memset ( mUartRecvTmpBuf , 0 , BUF_LENGTH ) ;
2025-04-03 10:00:57 +08:00
zlog_warn ( zct , " wave end,now_task = %d " , now_task ) ;
2024-10-22 19:04:25 +08:00
}
2024-10-31 19:30:10 +08:00
mssleep ( 10000 ) ;
2024-10-22 19:04:25 +08:00
} else if ( ret > 0 ) {
maxSize + = ret ;
2024-11-10 16:43:15 +08:00
timeoutflag = 0 ;
memcpy ( mUartRecvTmpBuf + offSize , buff , ret ) ;
offSize = offSize + ret ;
if ( offSize > BUF_LENGTH * 15 ) {
zlog_info ( zct , " maxSize = %d " , offSize ) ;
memset ( mUartRecvTmpBuf , 0 , BUF_LENGTH ) ;
timeoutflag = 0 ;
offSize = 0 ;
maxSize = 0 ;
tcflush ( fd , TCIOFLUSH ) ;
2024-10-22 19:04:25 +08:00
}
2025-05-06 10:07:30 +08:00
//zlog_info(zct, "0x9999 ===str_recv===,offSize = %d,ret = %d", offSize,ret);
2024-10-22 19:04:25 +08:00
}
2024-10-31 19:30:10 +08:00
} else {
2024-10-22 19:04:25 +08:00
memset ( buff , 0 , sizeof ( buff ) ) ;
2025-05-08 22:06:26 +08:00
ret = read_data ( fd , buff , BUF_LENGTH , 10 ) ;
2024-10-22 19:04:25 +08:00
if ( ret < = 0 ) {
continue ;
}
if ( srcshow ) {
2024-10-23 20:33:05 +08:00
zlog_info ( zct , " 0x8888 ===str_recv===,ret = %d " , ret ) ;
2025-05-08 15:21:36 +08:00
printf ( " \n current time = %s \n " , GetCurrentTime ( ) . c_str ( ) ) ;
2024-10-22 19:04:25 +08:00
for ( int i = 0 ; i < ret ; i + + ) {
2024-11-04 21:35:50 +08:00
printf ( " [%02x] " , buff [ i ] & 0xff ) ;
2024-10-22 19:04:25 +08:00
}
printf ( " \n " ) ;
FindRecvPackage ( ret , buff , head ) ;
}
}
}
}
Uart : : Uart ( ) : mUart ( mIoSev ) , mStrand ( mIoSev ) {
mRdLength = 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 ;
2024-10-31 19:30:10 +08:00
wave_shortAddr = 0 ;
2024-10-22 19:04:25 +08:00
DataNodeUpdateFile = " " ;
strTimetamp = " " ;
bZigbeeSinal = false ;
m_waveCountX = 0 ;
m_waveCountY = 0 ;
m_waveCountZ = 0 ;
VecWaveDataX . reserve ( 1000 ) ;
VecWaveDataY . reserve ( 1000 ) ;
VecWaveDataZ . reserve ( 1500 ) ;
2026-03-02 19:02:28 +08:00
m_waveCountLowFreX = 0 ;
m_waveCountLowFreY = 0 ;
m_waveCountLowFreZ = 0 ;
VecWaveDataLowFreX . reserve ( 1000 ) ;
VecWaveDataLowFreY . reserve ( 1000 ) ;
VecWaveDataLowFreZ . reserve ( 1500 ) ;
2025-05-08 15:21:36 +08:00
memset ( send_data , 0 , sizeof ( send_data ) ) ;
2025-05-08 22:06:26 +08:00
last_short_addr = 0 ;
last_time = 0 ;
2026-03-02 19:02:28 +08:00
current_z = false ;
2024-10-22 19:04:25 +08:00
}
Uart : : ~ Uart ( ) {
close ( fd ) ;
close ( TestFd ) ;
if ( mUart . is_open ( ) ) mUart . close ( ) ;
}
void Uart : : InitUart ( speed_t speed ) {
fd = config_uart ( " /dev/ttymxc4 " , speed ) ;
2024-10-23 20:33:05 +08:00
zlog_info ( zct , " InitUart fd = %d " , fd ) ;
2024-10-22 19:04:25 +08:00
if ( fd < 0 ) {
2024-10-23 20:33:05 +08:00
zlog_error ( zct , " config_uart error " ) ;
2024-10-22 19:04:25 +08:00
}
}
void Uart : : Close ( ) {
close ( fd ) ;
fd = 0 ;
}
void Uart : : InitZigbeeHW ( ) {
InitUart ( B115200 ) ;
UpdateZigbeeInfoCtrl ( ) ;
sleep ( 2 ) ;
int iRet = ReadFromUart ( ) ;
if ( iRet < = 0 ) {
Close ( ) ;
sleep ( 2 ) ;
2024-10-23 20:33:05 +08:00
zlog_info ( zct , " Speed error " ) ;
2024-10-22 19:04:25 +08:00
InitUart ( B57600 ) ;
UpdateZigbeeInfoCtrl ( ) ;
sleep ( 2 ) ;
ReadFromUart ( ) ;
2024-10-23 20:33:05 +08:00
zlog_info ( zct , " modify speed " ) ;
2024-10-22 19:04:25 +08:00
WriteSpeed2Zigbee ( ) ;
ReadFromUart ( ) ;
}
Close ( ) ;
sleep ( 1 ) ;
}
void TestRecv ( int status ) {
char szStatus [ 10 ] = { 0x00 } ;
sprintf ( szStatus , " %d " , status ) ;
write_data ( uart_inst : : instance ( ) . TestFd , ( char * ) szStatus , strlen ( szStatus ) ) ;
}
void Regist ( pTestRecvCallBack pTestRecv1 ) { pTestRecv = pTestRecv1 ; }
void Uart : : InitTestUart ( speed_t speed ) {
2024-10-23 20:33:05 +08:00
zlog_info ( zct , " InitTestUart " ) ;
2024-10-22 19:04:25 +08:00
TestFd = config_uart ( " /dev/ttymxc2 " , speed ) ; // Test
if ( TestFd < 0 ) {
2024-10-23 20:33:05 +08:00
zlog_error ( zct , " Test config_uart error " ) ;
2024-10-22 19:04:25 +08:00
}
Regist ( TestRecv ) ;
}
void Uart : : ReadTestUart ( ) {
char buff [ BUF_LENGTH ] = { 0x00 } ;
2024-10-23 20:33:05 +08:00
zlog_info ( zct , " ReadTestUart " ) ;
2024-10-22 19:04:25 +08:00
while ( 1 ) {
int ret = read_data ( TestFd , buff , BUF_LENGTH , 10 ) ;
if ( ret > 0 ) {
2024-10-23 20:33:05 +08:00
zlog_info ( zct , " buff = %s " , buff ) ;
2024-10-22 19:04:25 +08:00
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 " ) ) {
2024-10-23 22:25:03 +08:00
std : : string IP = GetGwIp_ ( " eth0 " ) ;
2024-10-22 19:04:25 +08:00
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 ( ) {
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 ) {
2024-11-18 19:28:20 +08:00
if ( ! bUpdate )
2024-11-09 16:15:39 +08:00
{
2025-05-08 15:21:36 +08:00
printf ( " \n Write To Uart Start: %s \n " , GetCurrentTime ( ) . c_str ( ) ) ;
2024-10-22 19:04:25 +08:00
for ( int i = 0 ; i < pLen ; i + + ) {
2024-11-04 21:35:50 +08:00
printf ( " %02X " , * ( strSend + i ) & 0xFF ) ;
2024-10-22 19:04:25 +08:00
}
2024-11-08 09:17:35 +08:00
printf ( " \n Write To Uart End. \n " ) ;
2024-10-22 19:04:25 +08:00
}
2024-10-23 22:25:03 +08:00
write_data ( fd , ( char * ) strSend , pLen ) ;
2024-10-22 19:04:25 +08:00
}
int Uart : : ReadFromUart ( ) {
char buffer [ 100 ] = { 0x00 } ;
int len = read_data ( fd , ( char * ) buffer , 100 , 10 ) ;
2024-11-04 21:35:50 +08:00
for ( int i = 0 ; i < len ; i + + ) printf ( " %02X " , buffer [ i ] & 0xFF ) ;
2024-10-23 20:33:05 +08:00
zlog_info ( zct , " ==========ReadFromUart========len = %d " , len ) ;
2024-10-22 19:04:25 +08:00
if ( len ) {
char head [ ] = { 0xAA , 0x55 , 0xAA } ;
FindRecvPackage ( len , buffer , head ) ;
}
return len ;
}
void Uart : : Run ( ) { mIoSev . run ( ) ; }
void Uart : : Stop ( ) {
if ( mUart . is_open ( ) ) mUart . close ( ) ;
mIoSev . stop ( ) ;
}
void Uart : : setCallBack ( onReceiveUart _callback ) { m_callback = _callback ; }
void Uart : : UpdateZigbeeInfo ( const char * pData ) {
2024-10-23 20:33:05 +08:00
zlog_info ( zct , " update gateway zigbee info " ) ;
2024-10-22 19:04:25 +08:00
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 ) ;
2024-11-13 17:50:54 +08:00
zlog_info ( zbt , " local zigbee module info Mode : %d Chan : %d PanID : %s MyAddr : %s DstAddr : %s,RetryNum:%s,TranTimeout:%s " , GlobalConfig : : ZigbeeInfo_G . DevMode , GlobalConfig : : ZigbeeInfo_G . Channel , GlobalConfig : : ZigbeeInfo_G . PanID . c_str ( ) ,
2024-10-23 20:33:05 +08:00
GlobalConfig : : ZigbeeInfo_G . MyAddr . c_str ( ) , GlobalConfig : : ZigbeeInfo_G . DstAddr . c_str ( ) , GlobalConfig : : ZigbeeInfo_G . RetryNum . c_str ( ) , GlobalConfig : : ZigbeeInfo_G . TranTimeout . c_str ( ) ) ;
2024-10-22 19:04:25 +08:00
2024-11-13 17:50:54 +08:00
zlog_info ( zbt , " PanID = %s,MacAddr_G= %s " , GlobalConfig : : ZigbeeInfo_G . PanID . c_str ( ) , GlobalConfig : : MacAddr_G . c_str ( ) ) ;
2024-10-22 19:04:25 +08:00
std : : string strchan = ReadStrByOpt ( ZIGBEECONFIG , " Zigbee " , " channel " ) ;
2024-11-13 17:50:54 +08:00
zlog_info ( zbt , " Channel = %d,strchan = %s " , GlobalConfig : : ZigbeeInfo_G . Channel , strchan . c_str ( ) ) ;
2024-10-22 19:04:25 +08:00
std : : string strType = ReadStrByOpt ( SYSTEMINFOFILE , " ZigbeeType " , " type " ) ;
2024-11-13 17:50:54 +08:00
zlog_info ( zbt , " TranTimeout = %02x,strType = %s " , GlobalConfig : : Zigbee_G . TranTimeout , strType . c_str ( ) ) ;
2024-10-22 19:04:25 +08:00
if ( strType = = " aw21 " ) {
unsigned short TranTimeout = 0x0A ;
WriteTranTimeout2Zigbee ( TranTimeout ) ;
}
if ( strType = = " zm5161 " ) {
unsigned short TranTimeout = 0x03 ;
WriteTranTimeout2Zigbee ( TranTimeout ) ;
}
}
2024-10-23 20:33:05 +08:00
2024-10-22 19:04:25 +08:00
void int2bytes ( int i , unsigned char * bytes , int size ) {
memset ( bytes , 0 , sizeof ( unsigned char ) * size ) ;
bytes [ 0 ] = ( unsigned char ) ( 0xff & i ) ;
bytes [ 1 ] = ( unsigned char ) ( ( 0xff00 & i ) > > 8 ) ;
bytes [ 2 ] = ( unsigned char ) ( ( 0xff0000 & i ) > > 16 ) ;
bytes [ 3 ] = ( unsigned char ) ( ( 0xff000000 & i ) > > 24 ) ;
}
2024-11-12 20:04:43 +08:00
void Uart : : GetLocalZigbeeRSSI ( uint16_t ushortAdd ) {
2024-11-11 20:51:40 +08:00
zlog_info ( zct , " Local Zigbee Signal ! \n " ) ;
mssleep ( 20000 ) ;
2024-11-12 20:04:43 +08:00
getZigbeeSignal ( ushortAdd ) ;
2025-04-29 19:35:34 +08:00
mssleep ( 20000 ) ;
getZigbeeSignal ( ushortAdd ) ;
mssleep ( 20000 ) ;
2024-11-11 20:51:40 +08:00
}
2024-10-31 19:30:10 +08:00
int Uart : : DealAskTask ( uint16_t ushortAdd ) {
2024-11-14 18:48:45 +08:00
char wherecon [ 50 ] = { 0 } ;
sprintf ( wherecon , " zigbeeShortAddr='%02x%02x' " , UINT16_HIGH ( ushortAdd ) , UINT16_LOW ( ushortAdd ) ) ;
int sensor_count = sqlite_db_ctrl : : instance ( ) . GetTableRows ( T_SENSOR_INFO ( TNAME ) , wherecon ) ;
2024-10-29 19:26:36 +08:00
2024-11-14 18:48:45 +08:00
if ( sensor_count < 1 ) {
zlog_warn ( zct , " device info not found %02x%02x " , UINT16_HIGH ( ushortAdd ) , UINT16_LOW ( ushortAdd ) ) ;
return 1 ;
}
2025-04-29 19:35:34 +08:00
int next_duration = 0 , next_task_id = 0 ;
2025-04-29 11:13:09 +08:00
int taskID ;
2024-10-31 19:30:10 +08:00
ScheduleTask scheduleTask ;
2026-03-02 19:02:28 +08:00
bool z = false ;
taskID = scheduler : : instance ( ) . StartSchedule ( ushortAdd , next_duration , z , next_task_id ) ;
current_z = z ;
zlog_info ( zct , " taskID = %d next_duration = %d next_task_id = %d,current_z = %d " , taskID , next_duration , next_task_id , current_z ) ;
2025-04-29 19:35:34 +08:00
if ( taskID = = kScheduleConfigSensor )
2024-10-31 19:30:10 +08:00
{
scheduleTask . cmd = CONFIG ;
2025-04-29 19:35:34 +08:00
scheduleTask . duration = next_duration ;
scheduleTask . next_taskID = next_task_id ;
2024-10-31 19:30:10 +08:00
scheduleTask . shortAddr = ushortAdd ;
TaskResp ( scheduleTask ) ;
2024-11-10 16:43:15 +08:00
mssleep ( 50000 ) ;
UpdateConfig ( ushortAdd ) ;
2025-04-29 19:35:34 +08:00
}
if ( next_task_id = = kScheduleWaveForm ) {
2024-10-31 19:30:10 +08:00
scheduleTask . cmd = REVIVE_DURATION ;
scheduleTask . shortAddr = ushortAdd ;
scheduleTask . duration = next_duration ;
2026-03-02 19:02:28 +08:00
scheduleTask . z = z ;
2025-04-29 19:35:34 +08:00
scheduleTask . next_taskID = WAVE_CMD ;
2024-10-31 19:30:10 +08:00
TaskResp ( scheduleTask ) ;
2025-04-29 19:35:34 +08:00
} else if ( next_task_id = = kScheduleEigenValue ) {
scheduleTask . cmd = REVIVE_DURATION ;
2024-11-10 19:22:16 +08:00
scheduleTask . shortAddr = ushortAdd ;
scheduleTask . duration = next_duration ;
2025-05-06 10:07:30 +08:00
scheduleTask . next_taskID = MEAS_EVAL ;
2024-11-10 19:22:16 +08:00
TaskResp ( scheduleTask ) ;
2025-05-06 10:07:30 +08:00
} else if ( next_task_id = = kScheduleUpgrade ) {
2025-04-30 15:17:01 +08:00
scheduleTask . cmd = REVIVE_DURATION ;
scheduleTask . shortAddr = ushortAdd ;
scheduleTask . duration = next_duration ;
2025-05-06 10:07:30 +08:00
scheduleTask . next_taskID = UPGRADE ;
2025-04-30 15:17:01 +08:00
TaskResp ( scheduleTask ) ;
2024-10-31 19:30:10 +08:00
}
2025-04-29 19:35:34 +08:00
// if (next_task_id == kScheduleEigenValue) //1.特征值
// {
// }
// else if (next_task_id == kScheduleWaveForm) //2.波形
// {
// DealReviveDuration(ushortAdd);
// }
// else if (next_task_id == kScheduleUpgrade) //3.升级
// {
// UpdateWirelessNode(ushortAdd);
// }else if (taskID == kScheduleConfigSensor) //4.更新配置
// {
// scheduleTask.cmd = CONFIG;
// scheduleTask.shortAddr = ushortAdd;
// mssleep(50000);
// TaskResp(scheduleTask);
// mssleep(50000);
// UpdateConfig(ushortAdd);
// }else if (taskID == kScheduleWrongTime) //5.异常连接
// {
// scheduleTask.cmd = REVIVE_DURATION;
// scheduleTask.shortAddr = ushortAdd;
// scheduleTask.duration = next_duration;
// zlog_info(zct, "next_duration = %d ", next_duration);
// TaskResp(scheduleTask);
// }
2024-11-19 15:44:18 +08:00
zlog_info ( zct , " DealAskTask end " ) ;
2024-10-29 19:26:36 +08:00
return 0 ;
}
2025-05-29 21:34:17 +08:00
int Uart : : DealAskTaskOld ( uint16_t ushortAdd ) {
char wherecon [ 50 ] = { 0 } ;
sprintf ( wherecon , " zigbeeShortAddr='%02x%02x' " , UINT16_HIGH ( ushortAdd ) , UINT16_LOW ( ushortAdd ) ) ;
int sensor_count = sqlite_db_ctrl : : instance ( ) . GetTableRows ( T_SENSOR_INFO ( TNAME ) , wherecon ) ;
2024-10-29 19:26:36 +08:00
2025-05-29 21:34:17 +08:00
if ( sensor_count < 1 ) {
zlog_warn ( zct , " device info not found %02x%02x " , UINT16_HIGH ( ushortAdd ) , UINT16_LOW ( ushortAdd ) ) ;
return 1 ;
}
int next_duration = 0 , next_task_id = 0 ;
int taskID ;
ScheduleTask scheduleTask ;
taskID = scheduler : : instance ( ) . StartSchedule ( ushortAdd , next_duration , next_task_id ) ;
zlog_info ( zct , " DealAskTaskOld taskID = %d next_duration = %d next_task_id = %d " , taskID , next_duration , next_task_id ) ;
if ( next_task_id = = kScheduleUpgrade ) {
scheduleTask . cmd = REVIVE_DURATION ;
scheduleTask . shortAddr = ushortAdd ;
scheduleTask . duration = next_duration ;
scheduleTask . next_taskID = UPGRADE ;
TaskResp ( scheduleTask ) ;
}
if ( taskID = = kScheduleUpgrade ) {
UpdateWirelessNode ( ushortAdd ) ;
}
return 0 ;
}
2024-10-29 19:26:36 +08:00
int Uart : : DealException ( const char * pData ) {
return 0 ;
}
int Uart : : DealReviveDuration ( uint16_t ushortAdd ) {
2024-10-31 19:30:10 +08:00
ScheduleTask scheduleTask ;
2025-04-28 15:42:53 +08:00
int next_taskID = 0 ;
2026-03-02 19:02:28 +08:00
bool z = false ;
uint16_t next_duration = scheduler : : instance ( ) . GetNextDuration ( ushortAdd , z , next_taskID ) ;
zlog_info ( zct , " next_duration = %d next_taskID = %d,z = %d " , next_duration , next_taskID , z ) ;
2024-10-31 19:30:10 +08:00
scheduleTask . cmd = REVIVE_DURATION ;
scheduleTask . shortAddr = ushortAdd ;
scheduleTask . duration = next_duration ;
2025-04-28 15:42:53 +08:00
scheduleTask . next_taskID = next_taskID & 0xFF ;
2024-10-31 19:30:10 +08:00
TaskResp ( scheduleTask ) ;
2024-10-29 19:26:36 +08:00
return 0 ;
}
int Uart : : DealConfig ( uint16_t ushortAdd ) {
char whereCon [ 1024 ] = { 0 } ;
char updateSql [ 1024 ] = { 0 } ;
2024-11-09 16:15:39 +08:00
sprintf ( updateSql , " UpdateFlag = 1 " ) ;
2024-11-14 18:48:45 +08:00
sprintf ( whereCon , " zigbeeShortAddr='%02x%02x' " , UINT16_HIGH ( ushortAdd ) , UINT16_LOW ( ushortAdd ) ) ;
2024-10-29 19:26:36 +08:00
sqlite_db_ctrl : : instance ( ) . UpdateTableData ( T_SENSOR_INFO ( TNAME ) , updateSql , whereCon ) ;
2024-11-09 16:15:39 +08:00
scheduler : : instance ( ) . UpdateConfigResult ( ushortAdd , 0 ) ;
2024-10-29 19:26:36 +08:00
return 0 ;
}
2026-03-02 19:02:28 +08:00
// 判断综合信号强度,低电量,停机状态,任一条件满足都不调度波形
int Uart : : WaveSendCondition ( char * shortAddr ) {
int lowSignal = - 1 , signalThreshold = - 1 , lowBatteryLevel = - 1 , batteryLevelThreshold = - 1 ;
char whereCon [ 100 ] = { 0 } ;
std : : string effect = " " , rssi = " " , batteryPower = " " ;
sprintf ( whereCon , " zigbeeShortAddr = '%s' " , shortAddr ) ;
lowSignal = readIntValue ( " config " , " lowSignal " , ( char * ) GlobalConfig : : Config_G . c_str ( ) ) ;
signalThreshold = readIntValue ( " config " , " signalThreshold " , ( char * ) GlobalConfig : : Config_G . c_str ( ) ) ;
lowBatteryLevel = readIntValue ( " config " , " lowBatteryLevel " , ( char * ) GlobalConfig : : Config_G . c_str ( ) ) ;
batteryLevelThreshold = readIntValue ( " config " , " batteryLevelThreshold " , ( char * ) GlobalConfig : : Config_G . c_str ( ) ) ;
vec_t sensorInfo = sqlite_db_ctrl : : instance ( ) . GetDataSingleLine ( T_SENSOR_INFO ( TNAME ) , " RSSI,batteryPower " , whereCon ) ;
rssi = sensorInfo [ 0 ] ;
batteryPower = sensorInfo [ 1 ] ;
std : : vector < std : : string > vParamBatteryPower ;
float fBatteryPower = 100.0 ;
boost : : split ( vParamBatteryPower , batteryPower , boost : : is_any_of ( " , " ) , boost : : token_compress_on ) ;
if ( vParamBatteryPower . size ( ) > 0 ) {
fBatteryPower = atof ( vParamBatteryPower [ 1 ] . c_str ( ) ) / atof ( vParamBatteryPower [ 0 ] . c_str ( ) ) ;
}
memset ( whereCon , 0 , sizeof ( whereCon ) ) ;
sprintf ( whereCon , " shortAddr = '%s' " , shortAddr ) ;
vec_t vecResult = sqlite_db_ctrl : : instance ( ) . GetDataSingleLine ( " t_shutdown_info " , " * " , whereCon ) ;
effect = vecResult [ 5 ] ;
if ( ( lowSignal = = 1 & & atof ( rssi . c_str ( ) ) < signalThreshold ) | | ( lowBatteryLevel = = 1 & & fBatteryPower < batteryLevelThreshold ) | | effect = = " 1 " ) {
zlog_warn ( zct , " WaveSendCondition not meet condition shortAddr = %s,rssi = %s,batteryPower = %s,effect = %s " , shortAddr , rssi . c_str ( ) , batteryPower . c_str ( ) , effect . c_str ( ) ) ;
return 1 ;
}
return 0 ;
}
2024-11-04 21:35:50 +08:00
int Uart : : DealWaveCompress ( const char * pData , uint16_t ushortAdd ) {
2024-11-06 21:16:18 +08:00
zlog_info ( zct , " DealWaveCompress " ) ;
2026-03-02 19:02:28 +08:00
wave_shortAddr = ushortAdd ;
char shortAdd [ 20 ] = { 0x00 } , whereCon [ 100 ] = { 0 } ;
char sensor_rssi [ 10 ] = { 0x00 } ;
sprintf ( shortAdd , " %02x%02x " , ( ushortAdd > > 8 ) & 0xFF , ushortAdd & 0xFF ) ;
2025-04-28 15:42:53 +08:00
now_task = WAVE_CMD ;
2026-03-02 19:02:28 +08:00
std : : string strShortAddr = std : : string ( shortAdd ) ;
sprintf ( whereCon , " zigbeeShortAddr = '%s' " , shortAdd ) ;
vec_t res = sqlite_db_ctrl : : instance ( ) . GetDataSingleLine ( T_SENSOR_INFO ( TNAME ) , " * " , whereCon ) ;
std : : string softVersion = res [ 9 ] ;
std : : string productNo = res [ 17 ] ;
2024-11-04 21:35:50 +08:00
compressWaveChannel tempchannel ;
2026-03-02 19:02:28 +08:00
if ( ( compareVersions ( softVersion , " 2.6 " ) = = - 1 & & productNo = = " 02 " ) | | productNo = = " 01 " ) { // DN101所有版本和DN102 2.6以前版本
ScheduleStatus schedule_status = scheduler : : instance ( ) . GetScheduleStatus ( ) ;
if ( schedule_status = = kScheduleStatusDebug | | schedule_status = = kScheduleStatusUpgrade ) {
zlog_warn ( zct , " ScheduleStatus not meet condition ,shortAddr = %s,schedule_status = %d " , shortAdd , schedule_status ) ;
scheduler : : instance ( ) . WaveSuccess ( ushortAdd , true ) ;
return 1 ;
}
WaveResp ( ushortAdd ) ; // 响应超时和传感器配值的重试次数有关, 重试10次是1s
tempchannel . compressChannelX = pData [ 7 ] ;
tempchannel . compressChannelY = pData [ 8 ] ;
tempchannel . compressChannelZ = pData [ 9 ] ;
tempchannel . CountX = BUILD_UINT32 ( pData [ 13 ] , pData [ 12 ] , pData [ 11 ] , pData [ 10 ] ) ;
tempchannel . CountY = BUILD_UINT32 ( pData [ 17 ] , pData [ 16 ] , pData [ 15 ] , pData [ 14 ] ) ;
tempchannel . CountZ = BUILD_UINT32 ( pData [ 21 ] , pData [ 20 ] , pData [ 19 ] , pData [ 18 ] ) ;
sprintf ( sensor_rssi , " %02d " , pData [ 22 ] & 0xFF ) ;
} else {
int ret = 0 ; //= WaveSendCondition(shortAdd);
if ( ret = = 1 ) {
zlog_warn ( zct , " WaveSendCondition not meet condition ,shortAddr = %s " , shortAdd ) ;
scheduler : : instance ( ) . WaveSuccess ( ushortAdd , true ) ;
return 1 ;
} else {
ScheduleStatus schedule_status = scheduler : : instance ( ) . GetScheduleStatus ( ) ;
if ( schedule_status = = kScheduleStatusUpgrade ) {
zlog_warn ( zct , " ScheduleStatus not meet condition ,shortAddr = %s,schedule_status = %d " , shortAdd , schedule_status ) ;
scheduler : : instance ( ) . WaveSuccess ( ushortAdd , true ) ;
return 1 ;
}
WaveResp ( ushortAdd ) ;
}
sprintf ( sensor_rssi , " %02d " , pData [ 7 ] & 0xFF ) ;
tempchannel . compressChannelX = pData [ 8 ] ;
tempchannel . compressChannelY = pData [ 9 ] ;
tempchannel . compressChannelZ = pData [ 10 ] ;
tempchannel . compressChannelVolX = pData [ 23 ] ;
tempchannel . compressChannelVolY = pData [ 24 ] ;
tempchannel . compressChannelVolZ = pData [ 25 ] ;
tempchannel . CountX = BUILD_UINT32 ( pData [ 14 ] , pData [ 13 ] , pData [ 12 ] , pData [ 11 ] ) ;
tempchannel . CountY = BUILD_UINT32 ( pData [ 18 ] , pData [ 17 ] , pData [ 16 ] , pData [ 15 ] ) ;
tempchannel . CountZ = BUILD_UINT32 ( pData [ 22 ] , pData [ 21 ] , pData [ 20 ] , pData [ 19 ] ) ;
tempchannel . CountVolX = BUILD_UINT32 ( pData [ 29 ] , pData [ 28 ] , pData [ 27 ] , pData [ 26 ] ) ;
tempchannel . CountVolY = BUILD_UINT32 ( pData [ 33 ] , pData [ 32 ] , pData [ 31 ] , pData [ 30 ] ) ;
tempchannel . CountVolZ = BUILD_UINT32 ( pData [ 37 ] , pData [ 36 ] , pData [ 35 ] , pData [ 34 ] ) ;
tempchannel . samplerateX = BUILD_UINT32 ( pData [ 41 ] , pData [ 40 ] , pData [ 39 ] , pData [ 38 ] ) ;
tempchannel . samplerateY = BUILD_UINT32 ( pData [ 45 ] , pData [ 44 ] , pData [ 43 ] , pData [ 42 ] ) ;
tempchannel . samplerateZ = BUILD_UINT32 ( pData [ 49 ] , pData [ 48 ] , pData [ 47 ] , pData [ 46 ] ) ;
tempchannel . samplerateVolX = BUILD_UINT32 ( pData [ 53 ] , pData [ 52 ] , pData [ 51 ] , pData [ 50 ] ) ;
tempchannel . samplerateVolY = BUILD_UINT32 ( pData [ 57 ] , pData [ 56 ] , pData [ 55 ] , pData [ 54 ] ) ;
tempchannel . samplerateVolZ = BUILD_UINT32 ( pData [ 61 ] , pData [ 60 ] , pData [ 59 ] , pData [ 58 ] ) ;
}
2024-11-04 21:35:50 +08:00
g_mapCompress [ strShortAddr ] = tempchannel ;
2026-03-02 19:02:28 +08:00
2025-04-28 15:42:53 +08:00
if ( ! strcmp ( sensor_rssi , " 00 " ) | | ! strcmp ( sensor_rssi , " 0 " ) ) {
char errorInfo [ 100 ] = { 0x00 } ;
sprintf ( errorInfo , " No RSSI %s " , sensor_rssi ) ;
zlog_error ( zct , errorInfo ) ;
} else {
2025-04-29 21:54:18 +08:00
char tableName [ 100 ] = { 0x00 } ;
2025-04-28 15:42:53 +08:00
char whereCon [ 100 ] = { 0 } ;
char updateSql [ 100 ] = { 0 } ;
sprintf ( whereCon , " zigbeeShortAddr='%02x%02x' " , UINT16_HIGH ( ushortAdd ) , UINT16_LOW ( ushortAdd ) ) ;
vec_t vecDataNodeNo = sqlite_db_ctrl : : instance ( ) . GetDataSingleLine ( T_SENSOR_INFO ( TNAME ) , " MeasurementID,RSSI " , whereCon ) ;
sprintf ( tableName , " t_dataStatic_%s " , ( char * ) vecDataNodeNo [ 0 ] . c_str ( ) ) ;
memset ( whereCon , 0 , sizeof ( whereCon ) ) ;
sprintf ( whereCon , " dataNodeNo='%s' order by timeStamp desc limit 1 " , vecDataNodeNo [ 0 ] . c_str ( ) ) ;
std : : string timestamp_last = sqlite_db_ctrl : : instance ( ) . GetData ( tableName , " timeStamp " , whereCon ) ;
sprintf ( updateSql , " zigbeeSignalNode = '%02d' " , atoi ( sensor_rssi ) ) ;
memset ( whereCon , 0 , sizeof ( whereCon ) ) ;
sprintf ( whereCon , " dataNodeNo='%s' and timeStamp = '%s' " , ( char * ) vecDataNodeNo [ 0 ] . c_str ( ) , timestamp_last . c_str ( ) ) ;
sqlite_db_ctrl : : instance ( ) . UpdateTableData ( tableName , updateSql , whereCon ) ;
}
2026-03-02 19:02:28 +08:00
zlog_warn ( zct , " count X = %d,Y = %d,Z = %d,vol X = %d,vol Y = %d,vol Z = %d short_add = %s " , tempchannel . CountX , tempchannel . CountY , tempchannel . CountZ , tempchannel . CountVolX , tempchannel . CountVolY , tempchannel . CountVolZ , strShortAddr . c_str ( ) ) ;
2024-11-04 21:35:50 +08:00
zlog_info ( zct , " compress X = %d,Y = %d,Z = %d " , tempchannel . compressChannelX , tempchannel . compressChannelY , tempchannel . compressChannelZ ) ;
2026-03-02 19:02:28 +08:00
zlog_info ( zct , " compress vol X = %d,vol Y = %d,vol Z = %d " , tempchannel . compressChannelVolX , tempchannel . compressChannelVolY , tempchannel . compressChannelVolZ ) ;
zlog_info ( zct , " samplerate X = %d,Y = %d,Z = %d " , tempchannel . samplerateX , tempchannel . samplerateY , tempchannel . samplerateZ ) ;
zlog_info ( zct , " samplerate vol X = %d,vol Y = %d,vol Z = %d " , tempchannel . samplerateVolX , tempchannel . samplerateVolY , tempchannel . samplerateVolZ ) ;
2024-11-04 21:35:50 +08:00
return 0 ;
}
2024-11-10 16:43:15 +08:00
int Uart : : DealSensorRSSI ( const char * pData , uint16_t ushortAdd ) {
2024-11-13 17:50:54 +08:00
//传感器获取网关信号强度
zlog_info ( zct , " DealSensorRSSI ushortAdd = %02x%02x " , UINT16_HIGH ( ushortAdd ) , UINT16_LOW ( ushortAdd ) ) ;
mssleep ( 200000 ) ;
ScheduleTask scheduleTask ;
now_task = WAVE_CMD ;
scheduleTask . cmd = WAVE_CMD ;
scheduleTask . shortAddr = ushortAdd ;
TaskResp ( scheduleTask ) ;
2024-11-10 16:43:15 +08:00
char localtimestamp [ 32 ] = { 0 } ;
GetTimeNet ( localtimestamp , 1 ) ;
char sensor_rssi [ 10 ] = { 0x00 } ;
sprintf ( sensor_rssi , " %02d " , pData [ 7 ] & 0xFF ) ;
if ( ! strcmp ( sensor_rssi , " 00 " ) | | ! strcmp ( sensor_rssi , " 0 " ) ) {
char errorInfo [ 100 ] = { 0x00 } ;
2024-11-13 17:50:54 +08:00
sprintf ( errorInfo , " No RSSI %s " , sensor_rssi ) ;
2024-11-10 16:43:15 +08:00
zlog_error ( zct , errorInfo ) ;
} else {
char tableName [ 100 ] = { 0x00 } ;
char whereCon [ 100 ] = { 0 } ;
char updateSql [ 100 ] = { 0 } ;
sprintf ( whereCon , " zigbeeShortAddr='%02x%02x' " , UINT16_HIGH ( ushortAdd ) , UINT16_LOW ( ushortAdd ) ) ;
vec_t vecDataNodeNo = sqlite_db_ctrl : : instance ( ) . GetDataSingleLine ( T_SENSOR_INFO ( TNAME ) , " MeasurementID,RSSI " , whereCon ) ;
sprintf ( tableName , " t_dataStatic_%s " , ( char * ) vecDataNodeNo [ 0 ] . c_str ( ) ) ;
2024-11-13 17:50:54 +08:00
memset ( whereCon , 0 , sizeof ( whereCon ) ) ;
2024-11-14 18:48:45 +08:00
sprintf ( whereCon , " dataNodeNo='%s' order by timeStamp desc limit 1 " , vecDataNodeNo [ 0 ] . c_str ( ) ) ;
2024-11-13 17:50:54 +08:00
std : : string timestamp_last = sqlite_db_ctrl : : instance ( ) . GetData ( tableName , " timeStamp " , whereCon ) ;
sprintf ( updateSql , " zigbeeSignalNode = '%02d' " , atoi ( sensor_rssi ) ) ;
memset ( whereCon , 0 , sizeof ( whereCon ) ) ;
sprintf ( whereCon , " dataNodeNo='%s' and timeStamp = '%s' " , ( char * ) vecDataNodeNo [ 0 ] . c_str ( ) , timestamp_last . c_str ( ) ) ;
sqlite_db_ctrl : : instance ( ) . UpdateTableData ( tableName , updateSql , whereCon ) ;
2024-11-10 16:43:15 +08:00
}
return 0 ;
}
2024-11-12 20:04:43 +08:00
int Uart : : DealUpgrade ( uint16_t ushortAdd , int status ) {
char updateSql [ 100 ] = { 0 } ;
char wherecon [ 100 ] = { 0 } ;
memset ( wherecon , 0 , sizeof ( wherecon ) ) ;
memset ( updateSql , 0 , sizeof ( updateSql ) ) ;
sprintf ( wherecon , " zigbeeShortAddr = '%02x%02x' " , UINT16_HIGH ( ushortAdd ) , UINT16_LOW ( ushortAdd ) ) ;
sprintf ( updateSql , " upgradeStatus = %d " , status ) ;
sqlite_db_ctrl : : instance ( ) . UpdateTableData ( T_SENSOR_INFO ( TNAME ) , updateSql , wherecon ) ;
2024-11-14 18:48:45 +08:00
memset ( wherecon , 0 , sizeof ( wherecon ) ) ;
sprintf ( wherecon , " short_addr = '%02x%02x' ORDER BY start_timestamp DESC LIMIT 1 " , UINT16_HIGH ( ushortAdd ) , UINT16_LOW ( ushortAdd ) ) ;
std : : string start_timestamp = sqlite_db_ctrl : : instance ( ) . GetData ( " firmware_upgrade " , " start_timestamp " , wherecon ) ;
2024-11-12 20:04:43 +08:00
memset ( updateSql , 0 , sizeof ( updateSql ) ) ;
memset ( wherecon , 0 , sizeof ( wherecon ) ) ;
2024-11-14 18:48:45 +08:00
sprintf ( wherecon , " short_addr = '%02x%02x' AND start_timestamp = '%s' " , UINT16_HIGH ( ushortAdd ) , UINT16_LOW ( ushortAdd ) , start_timestamp . c_str ( ) ) ;
2024-11-12 20:04:43 +08:00
sprintf ( updateSql , " status = %d " , status ) ;
sqlite_db_ctrl : : instance ( ) . UpdateTableData ( " firmware_upgrade " , updateSql , wherecon ) ;
2024-11-14 18:48:45 +08:00
2024-11-12 20:04:43 +08:00
return 0 ;
}
2025-05-08 22:06:26 +08:00
int Uart : : DealFeatureValue ( const char * pData , uint16_t ushortAdd ) {
zlog_info ( zct , " DealFeatureValue " ) ;
char localtimestamp [ 32 ] = { 0 } ;
GetTimeNet ( localtimestamp , 1 ) ;
std : : string nowTimetamp = std : : string ( localtimestamp ) ;
long now_time = atol ( nowTimetamp . c_str ( ) ) ;
if ( ushortAdd = = last_short_addr & & ( now_time - last_time ) < 5 ) {
2025-05-12 17:29:14 +08:00
zlog_warn ( zct , " DealFeatureValue short_addr_last = %02x%02x,timestamp_last = %ld,nowTime = %ld " , UINT16_HIGH ( ushortAdd ) , UINT16_LOW ( ushortAdd ) , last_time , now_time ) ;
2025-05-08 22:06:26 +08:00
int iRet = DealDataNodeFeature ( pData , 0 ) ;
if ( iRet ! = 0 )
{
return - 1 ;
}
2026-03-02 19:02:28 +08:00
// char logInfo[100] = {0x00};
// std::vector<uint8_t>& data_vec = map_send_data[ushortAdd];
// const uint8_t* send_data = data_vec.data();
// WriteToUart((const char*)send_data, 100);
// mssleep(50000);
// WriteToUart((const char*)send_data, 100);
// mssleep(50000);
// WriteToUart((const char*)send_data, 100);
2025-05-08 22:06:26 +08:00
} else {
DealAskTask ( ushortAdd ) ;
DealDataNodeFeature ( pData , 0 ) ;
}
GetTimeNet ( localtimestamp , 1 ) ;
nowTimetamp = std : : string ( localtimestamp ) ;
last_time = atol ( nowTimetamp . c_str ( ) ) ;
return 0 ;
}
2026-03-02 19:02:28 +08:00
2024-10-22 19:04:25 +08:00
void Uart : : DealRecvData ( const char * pData ) {
2024-10-29 19:26:36 +08:00
uint16_t ushortAdd = BUILD_UINT16 ( pData [ 3 ] & 0xFF , pData [ 4 ] & 0xFF ) ;
uint8_t command = pData [ 5 ] & 0xFF ;
uint8_t recvcode = pData [ 7 ] & 0xFF ;
2025-04-29 19:35:34 +08:00
int next_duration = 0 , next_task_id = 0 ;
int taskID = 0 ;
ScheduleTask scheduleTask ;
2026-03-02 19:02:28 +08:00
bool z = false ;
2024-11-04 21:35:50 +08:00
zlog_info ( zct , " shortAdd = %02x%02x,command = %d " , UINT16_HIGH ( ushortAdd ) , UINT16_LOW ( ushortAdd ) , command ) ;
2025-05-12 10:34:06 +08:00
if ( command = = WAVE_COMPRESS | | command = = UPGRADE_ASK )
{
char localtimestamp [ 32 ] = { 0 } ;
GetTimeNet ( localtimestamp , 1 ) ;
std : : string nowTimetamp = std : : string ( localtimestamp ) ;
long now_time = atol ( nowTimetamp . c_str ( ) ) ;
if ( ushortAdd = = last_short_addr & & ( now_time - last_time ) < 5 ) {
2025-05-12 17:29:14 +08:00
zlog_warn ( zct , " WAVE_COMPRESS | UPGRADE_ASK short_addr_last = %02x%02x,timestamp_last = %ld,nowTime = %ld " , UINT16_HIGH ( ushortAdd ) , UINT16_LOW ( ushortAdd ) , last_time , now_time ) ;
2025-05-12 10:34:06 +08:00
return ;
}
GetTimeNet ( localtimestamp , 1 ) ;
nowTimetamp = std : : string ( localtimestamp ) ;
last_time = atol ( nowTimetamp . c_str ( ) ) ;
}
2025-05-09 23:42:53 +08:00
if ( ushortAdd ! = last_short_addr ) {
mssleep ( 50000 ) ;
ModifyDistAddr ( ushortAdd ) ;
mssleep ( 50000 ) ;
}
2024-10-29 19:26:36 +08:00
switch ( command ) {
2024-11-04 21:35:50 +08:00
case DEVICE_INF :
2024-10-31 19:30:10 +08:00
DealDataNodeInfo ( pData ) ;
2024-11-04 21:35:50 +08:00
break ;
case DEVICE_INF2 :
2024-10-31 19:30:10 +08:00
DealDataNodeName ( pData ) ;
2024-11-04 21:35:50 +08:00
break ;
2025-05-29 21:34:17 +08:00
case ASK_TASK :
DealAskTaskOld ( ushortAdd ) ;
break ;
2024-10-31 19:30:10 +08:00
case DEVICE_EXCEPTION :
2024-10-29 19:26:36 +08:00
DealException ( pData ) ;
break ;
2024-10-31 19:30:10 +08:00
case MEAS_EVAL :
2025-05-08 22:06:26 +08:00
DealFeatureValue ( pData , ushortAdd ) ;
2024-10-29 19:26:36 +08:00
break ;
2024-11-10 16:43:15 +08:00
case UPGRADE :
2024-11-11 20:51:40 +08:00
if ( recvcode = = 0 ) {
2025-04-28 15:42:53 +08:00
zlog_info ( zbt , " [UPLOAD SUCCESS] shortAdd = %02x%02x,command = %d,recvcode = %d " , UINT16_HIGH ( ushortAdd ) , UINT16_LOW ( ushortAdd ) , command , recvcode ) ;
2025-01-23 20:07:10 +08:00
} else if ( recvcode = = 5 )
{
DealUpgrade ( ushortAdd , 2 ) ;
scheduler : : instance ( ) . UpgradeResult ( ushortAdd , 0 ) ;
2025-04-28 15:42:53 +08:00
zlog_warn ( zbt , " [UPLOAD ALREADY] shortAdd = %02x%02x,command = %d,recvcode = %d " , UINT16_HIGH ( ushortAdd ) , UINT16_LOW ( ushortAdd ) , command , recvcode ) ;
2025-01-23 20:07:10 +08:00
} else
{
2024-11-12 20:04:43 +08:00
DealUpgrade ( ushortAdd , 3 ) ;
2024-11-11 20:51:40 +08:00
scheduler : : instance ( ) . UpgradeResult ( ushortAdd , recvcode ) ;
2025-04-28 15:42:53 +08:00
zlog_warn ( zbt , " [UPLOAD FAILED] shortAdd = %02x%02x,command = %d,recvcode = %d " , UINT16_HIGH ( ushortAdd ) , UINT16_LOW ( ushortAdd ) , command , recvcode ) ;
2024-11-04 21:35:50 +08:00
}
2024-10-29 19:26:36 +08:00
break ;
2024-11-10 16:43:15 +08:00
case CONFIG :
2024-11-11 20:51:40 +08:00
if ( recvcode = = 0 ) {
2024-11-09 16:15:39 +08:00
DealReviveDuration ( ushortAdd ) ;
2024-11-11 20:51:40 +08:00
DealConfig ( ushortAdd ) ;
2024-11-04 21:35:50 +08:00
} else {
2024-11-14 18:48:45 +08:00
scheduler : : instance ( ) . UpdateConfigResult ( ushortAdd , recvcode ) ;
2024-11-13 17:50:54 +08:00
zlog_warn ( zbt , " [CONFIG] shortAdd = %02x%02x,command = %d,recvcode = %d " , UINT16_HIGH ( ushortAdd ) , UINT16_LOW ( ushortAdd ) , command , recvcode ) ;
2024-11-04 21:35:50 +08:00
}
break ;
case WAVE_COMPRESS :
2025-05-12 10:34:06 +08:00
GetLocalZigbeeRSSI ( ushortAdd ) ;
DealWaveCompress ( pData , ushortAdd ) ;
2024-10-29 19:26:36 +08:00
break ;
2025-04-29 19:35:34 +08:00
case UPGRADE_ASK :
2025-05-08 10:00:32 +08:00
mssleep ( 50000 ) ;
2026-03-02 19:02:28 +08:00
taskID = scheduler : : instance ( ) . StartSchedule ( ushortAdd , next_duration , z , next_task_id ) ;
2025-04-30 15:17:01 +08:00
taskID = kScheduleUpgrade ;
2025-04-29 19:35:34 +08:00
if ( taskID = = kScheduleUpgrade ) {
UpdateWirelessNode ( ushortAdd ) ;
} else if ( taskID = = kScheduleResultNone ) {
scheduleTask . cmd = REVIVE_DURATION ;
scheduleTask . shortAddr = ushortAdd ;
scheduleTask . duration = next_duration ;
scheduleTask . next_taskID = next_task_id ;
TaskResp ( scheduleTask ) ;
}
2024-11-10 16:43:15 +08:00
break ;
2024-10-22 19:04:25 +08:00
default : break ;
}
2025-05-09 23:42:53 +08:00
last_short_addr = ushortAdd ;
2024-10-22 19:04:25 +08:00
}
2026-03-02 19:02:28 +08:00
void Uart : : DelRepeatData ( char * pShortAdd , const char * dataNodeNo , const char * measurementID ) {
char whereCon [ 64 ] = { 0 } ;
memset ( whereCon , 0 , sizeof ( whereCon ) ) ;
sprintf ( whereCon , " %s= '%s' " , T_SENSOR_INFO ( DATANODENO ) , dataNodeNo ) ;
sqlite_db_ctrl : : instance ( ) . DeleteTableData ( T_SENSOR_INFO ( TNAME ) , whereCon ) ;
memset ( whereCon , 0 , sizeof ( whereCon ) ) ;
sprintf ( whereCon , " %s= '%s' " , T_SENSOR_INFO ( DATANODENO ) , measurementID ) ;
sqlite_db_ctrl : : instance ( ) . DeleteTableData ( T_DATA_INFO ( TNAME ) , whereCon ) ;
sqlite_db_ctrl : : instance ( ) . DeleteTableData ( T_DATASTATIC_INFO ( TNAME ) , whereCon ) ;
sqlite_db_ctrl : : instance ( ) . DeleteTableData ( T_BATTERY_INFO ( TNAME ) , whereCon ) ;
sqlite_db_ctrl : : instance ( ) . DeleteTableData ( " t_battery_history " , whereCon ) ;
memset ( whereCon , 0 , sizeof ( whereCon ) ) ;
sprintf ( whereCon , " MeasurementID = '%s' " , measurementID ) ;
sqlite_db_ctrl : : instance ( ) . DeleteTableData ( " t_debug_info " , whereCon ) ;
sqlite_db_ctrl : : instance ( ) . DeleteTableData ( " t_shutdown_info " , whereCon ) ;
sqlite_db_ctrl : : instance ( ) . DeleteTableData ( " t_wave_triger_info " , whereCon ) ;
memset ( whereCon , 0 , sizeof ( whereCon ) ) ;
sprintf ( whereCon , " channelID like'%s' " , measurementID ) ;
sqlite_db_ctrl : : instance ( ) . DeleteTableData ( " t_data_waveSend " , whereCon ) ;
char szTableName [ 50 ] = { 0x00 } ;
sprintf ( szTableName , " DROP TABLE t_data_%s " , measurementID ) ;
sqlite_db_ctrl : : instance ( ) . CreateTable ( szTableName ) ;
memset ( szTableName , 0x00 , sizeof ( szTableName ) ) ;
sprintf ( szTableName , " DROP TABLE t_dataStatic_%s " , measurementID ) ;
sqlite_db_ctrl : : instance ( ) . CreateTable ( szTableName ) ;
uint16_t short_addr ;
char * end_ptr = NULL ;
short_addr = strtol ( pShortAdd , & end_ptr , 16 ) ;
scheduler : : instance ( ) . ClearScheduleCfg ( short_addr ) ;
}
2024-10-22 19:04:25 +08:00
void Uart : : DealDataNodeName ( const char * pData ) {
2024-10-23 20:33:05 +08:00
zlog_info ( zct , " DealDataNodeName " ) ;
std : : string strTime = GetLocalTimeWithMs ( ) ;
2024-10-22 19:04:25 +08:00
char szShortAdd [ 8 ] = { 0x00 } ;
char shortAdd [ 8 ] = { 0x00 } ;
2024-11-05 11:20:22 +08:00
char NodeName [ 128 ] = { 0x00 } ;
2024-10-22 19:04:25 +08:00
sprintf ( szShortAdd , " %02x%02x " , pData [ 3 ] & 0xFF , pData [ 4 ] & 0xFF ) ;
memcpy ( NodeName , & pData [ 7 ] , 64 ) ;
memcpy ( shortAdd , & pData [ 3 ] , 2 ) ;
2026-03-02 19:02:28 +08:00
char whereCon [ 512 ] = { 0 } ;
char uplCon [ 512 ] = { 0x00 } ;
2024-10-22 19:04:25 +08:00
char nodeWaveSend [ 10 ] = { 0x00 } ;
for ( int i = 0 ; i < 64 ; i + + ) {
sprintf ( & NodeName [ i * 2 ] , " %02X " , pData [ 7 + i ] & 0xFF ) ;
}
2026-03-02 19:02:28 +08:00
char MeasurementID [ 50 ] = { 0x00 } ;
2024-10-22 19:04:25 +08:00
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 ] ) ;
2025-04-28 19:36:13 +08:00
2025-04-28 15:42:53 +08:00
2024-10-22 19:04:25 +08:00
char gbkNodeName [ 128 ] = { 0x00 } ;
sprintf ( whereCon , " zigbeeShortAddr='%s' " , szShortAdd ) ;
2024-10-23 20:33:05 +08:00
zlog_info ( zct , " whereCon = %s " , whereCon ) ;
2024-10-22 20:56:21 +08:00
array_t vecRes = sqlite_db_ctrl : : instance ( ) . GetDataMultiLine ( T_SENSOR_INFO ( TNAME ) , " dataNodeNo, MeasurementID,hardVersion,softVersion " , whereCon ) ;
2024-11-16 17:57:38 +08:00
zlog_info ( zct , " vecRes = %d " , vecRes . size ( ) ) ;
2024-10-22 19:04:25 +08:00
if ( vecRes . size ( ) > 1 ) {
2024-10-23 22:25:03 +08:00
for ( size_t i = 0 ; i < vecRes . size ( ) ; i + + ) {
2026-03-02 19:02:28 +08:00
if ( vecRes [ i ] [ 1 ] ! = " " ) { //删除短地址重复的传感器,新增的传感器,通道编码此时为空,将不会删除。
DelRepeatData ( szShortAdd , vecRes [ i ] [ 0 ] . c_str ( ) , vecRes [ i ] [ 1 ] . c_str ( ) ) ;
2024-10-22 19:04:25 +08:00
}
}
}
2024-11-16 17:57:38 +08:00
zlog_info ( zct , " vecRes22 = %d " , vecRes . size ( ) ) ;
zlog_info ( zct , " hardVersion = %s,softVersion = %s " , vecRes [ 0 ] [ 2 ] . c_str ( ) , vecRes [ 0 ] [ 3 ] . c_str ( ) ) ;
2024-10-23 20:33:05 +08:00
std : : string hardVersion = vecRes [ 0 ] [ 2 ] ;
std : : string softVersion = vecRes [ 0 ] [ 3 ] ;
2024-10-25 18:45:26 +08:00
if ( ( hardVersion = = " 3.0 " & & compareVersions ( softVersion , " 3.6 " ) = = - 1 ) | | ( hardVersion = = " 4.0 " & & compareVersions ( softVersion , " 4.6 " ) = = - 1 ) | | ( hardVersion = = " 1.0 " & & compareVersions ( softVersion , " 1.2 " ) = = - 1 ) ) {
2024-10-22 19:04:25 +08:00
memcpy ( MeasurementID , vecRes [ 0 ] [ 0 ] . c_str ( ) , sizeof ( MeasurementID ) ) ;
}
2024-11-16 17:57:38 +08:00
memset ( whereCon , 0 , sizeof ( whereCon ) ) ;
2026-03-02 19:02:28 +08:00
sprintf ( whereCon , " MeasurementID='%s' " , MeasurementID ) ;
array_t vecRes2 = sqlite_db_ctrl : : instance ( ) . GetDataMultiLine ( T_SENSOR_INFO ( TNAME ) , " dataNodeNo, MeasurementID,hardVersion,softVersion " , whereCon ) ;
zlog_info ( zct , " vecRes2 = %zu " , vecRes2 . size ( ) ) ;
if ( vecRes2 . size ( ) > 1 ) {
for ( size_t i = 0 ; i < vecRes2 . size ( ) ; i + + ) {
if ( vecRes2 [ i ] [ 1 ] ! = " " ) { //删除通道ID重复的传感器, 新增的传感器, 通道编码此时为空, 将不会删除。
DelRepeatData ( szShortAdd , vecRes2 [ i ] [ 0 ] . c_str ( ) , vecRes2 [ i ] [ 1 ] . c_str ( ) ) ;
}
}
}
memset ( whereCon , 0 , sizeof ( whereCon ) ) ;
2024-11-16 17:57:38 +08:00
sprintf ( whereCon , " zigbeeShortAddr='%s' " , szShortAdd ) ;
2024-10-22 19:04:25 +08:00
std : : string strNodeName ( NodeName ) ;
2024-10-23 20:33:05 +08:00
zlog_info ( zct , " strNodeName = %s " , strNodeName . c_str ( ) ) ;
2024-11-16 22:03:51 +08:00
//solve(gbkNodeName, NodeName);
hexToAscii ( strNodeName . c_str ( ) , gbkNodeName ) ;
2024-10-23 20:33:05 +08:00
zlog_info ( zct , " gbkNodeName = %s " , gbkNodeName ) ;
zlog_info ( zct , " NodeName = %s " , NodeName ) ;
zlog_info ( zct , " whereCon = %s " , whereCon ) ;
2024-10-22 19:04:25 +08:00
sprintf ( uplCon , " dataNodeName = '%s' , MeasurementID = '%s',NodeWaveSend = '%s' " , gbkNodeName , MeasurementID , nodeWaveSend ) ;
2024-10-23 22:25:03 +08:00
sqlite_db_ctrl : : instance ( ) . UpdateTableData ( T_SENSOR_INFO ( TNAME ) , uplCon , whereCon ) ;
2024-10-23 20:33:05 +08:00
std : : string strData = sqlite_db_ctrl : : instance ( ) . GetNodeConfigureInfor ( whereCon ) ;
2024-10-23 22:25:03 +08:00
data_publish ( strData . c_str ( ) , GlobalConfig : : Topic_G . mPubConfig . c_str ( ) ) ;
2024-11-21 11:06:07 +08:00
2026-03-02 19:02:28 +08:00
char szTableName [ 100 ] = { 0x00 } ;
2024-11-21 11:06:07 +08:00
sprintf ( szTableName , " t_data_%s " , MeasurementID ) ;
2024-11-21 15:11:03 +08:00
memset ( whereCon , 0 , sizeof ( whereCon ) ) ;
sprintf ( whereCon , " type='table' and name = '%s' " , szTableName ) ;
int ret = sqlite_db_ctrl : : instance ( ) . GetTableRows ( " sqlite_master " , whereCon ) ;
zlog_info ( zct , " ret = %d " , ret ) ;
2024-11-21 11:06:07 +08:00
if ( ret = = 0 ) {
sqlite_db_ctrl : : instance ( ) . Createtable ( szTableName ) ;
}
memset ( szTableName , 0x00 , sizeof ( szTableName ) ) ;
sprintf ( szTableName , " t_dataStatic_%s " , MeasurementID ) ;
2024-11-21 15:11:03 +08:00
memset ( whereCon , 0 , sizeof ( whereCon ) ) ;
sprintf ( whereCon , " type='table' and name = '%s' " , szTableName ) ;
ret = sqlite_db_ctrl : : instance ( ) . GetTableRows ( " sqlite_master " , whereCon ) ;
zlog_info ( zct , " ret = %d " , ret ) ;
2024-11-21 11:06:07 +08:00
if ( ret = = 0 ) {
sqlite_db_ctrl : : instance ( ) . CreatedataStatictable ( szTableName ) ;
}
2026-03-02 19:02:28 +08:00
char insertSql [ 1024 ] = { 0 } ;
sprintf ( insertSql , " '%s','%s','','','','','','0','' " ,
MeasurementID , szShortAdd ) ;
sqlite_db_ctrl : : instance ( ) . InsertData ( " t_debug_info " , insertSql ) ;
memset ( insertSql , 0 , sizeof ( insertSql ) ) ;
sprintf ( insertSql , " '%s','%s','0','0','0','0' " ,
MeasurementID , szShortAdd ) ;
sqlite_db_ctrl : : instance ( ) . InsertData ( " t_shutdown_info " , insertSql ) ;
memset ( insertSql , 0 , sizeof ( insertSql ) ) ;
sprintf ( insertSql , " '%s','%s','0','0','0','0','0' " ,
MeasurementID , szShortAdd ) ;
sqlite_db_ctrl : : instance ( ) . InsertData ( " t_wave_triger_info " , insertSql ) ;
2024-10-22 19:04:25 +08:00
}
void Uart : : DealDataNodeInfo ( const char * pData ) {
2024-11-13 17:50:54 +08:00
2024-10-25 18:45:26 +08:00
int res = 0 ;
2024-10-22 19:04:25 +08:00
RecvData * pRecvData = ( RecvData * ) pData ;
2024-11-13 17:50:54 +08:00
zlog_warn ( zct , " recv remote zigbee module info shortAddr %02x%02x " , pRecvData - > Data [ 46 ] , pRecvData - > Data [ 47 ] ) ;
2024-10-22 19:04:25 +08:00
char buf [ 32 ] = { 0 } ;
2025-04-28 19:36:13 +08:00
char whereCon [ 64 ] = { 0 } ;
2025-04-29 11:25:46 +08:00
uint16_t ushortAdd = BUILD_UINT16 ( pData [ 3 ] & 0xFF , pData [ 4 ] & 0xFF ) ;
2024-10-22 19:04:25 +08:00
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 , " %.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
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
memset ( buf , 0 , 32 ) ;
dataNodeInfo . WaveTime = BUILD_UINT32 ( pRecvData - > Data [ 31 ] , pRecvData - > Data [ 32 ] , pRecvData - > Data [ 33 ] , pRecvData - > Data [ 34 ] ) ; //原始波形发送次数 4 byte
memset ( buf , 0 , 32 ) ;
dataNodeInfo . BateryV = BUILD_UINT16 ( pRecvData - > Data [ 35 ] , pRecvData - > Data [ 36 ] ) ; //电池电压 2 byte
memset ( buf , 0 , 32 ) ;
sprintf ( buf , " %02x " , pRecvData - > Data [ 37 ] ) ;
dataNodeInfo . ProductNo = std : : string ( buf ) ; //产品型号 1 byte
// 获取 RSSI
chTemp = pRecvData - > Data [ 38 ] ;
memset ( buf , 0 , 32 ) ;
sprintf ( buf , " %d " , chTemp ) ;
dataNodeInfo . RSSI = atoi ( buf ) ; //无线信号强度 1 byte
chTemp = pRecvData - > Data [ 39 ] ;
memset ( buf , 0 , 32 ) ;
sprintf ( buf , " %02x " , chTemp ) ;
dataNodeInfo . ConfigFlag = ( ( 0 = = std : : string ( buf ) . compare ( " aa " ) ) ? 1 : 0 ) ; //配置标志 1 byte
chTemp = pRecvData - > Data [ 40 ] ;
memset ( buf , 0 , 32 ) ;
sprintf ( buf , " %d " , chTemp ) ;
dataNodeInfo . FeatureInterVal = atoi ( buf ) ; //唤醒周期 1 byte
memset ( buf , 0 , 32 ) ;
// yxq
sprintf ( buf , " %u%u " , pRecvData - > Data [ 41 ] , pRecvData - > Data [ 42 ] ) ;
dataNodeInfo . WaveInterVal = atoi ( buf ) ; //原始波形发送周期 2 byte
memset ( buf , 0 , 32 ) ;
sprintf ( buf , " %02x%02x " , pRecvData - > Data [ 43 ] , pRecvData - > Data [ 44 ] ) ; // Zigbee PID 2 byte
dataNodeInfo . ZigbeePanId = std : : string ( buf ) ;
chTemp = pRecvData - > Data [ 45 ] ;
memset ( buf , 0 , 32 ) ;
sprintf ( buf , " %d " , chTemp ) ;
dataNodeInfo . ZigbeeChannel = atoi ( buf ) ; // Zigbee 信道 1 byte
memset ( buf , 0 , 32 ) ;
sprintf ( buf , " %02x%02x " , pRecvData - > Data [ 46 ] , pRecvData - > Data [ 47 ] ) ; // Zigbee 本地地址 2 byte
dataNodeInfo . ZigbeeShortAddr = std : : string ( buf ) ;
unsigned char shortAddr [ 2 ] = { 0x00 } ;
memcpy ( shortAddr , ( unsigned char * ) & pRecvData - > Data [ 46 ] , 2 ) ;
modify_DistAddr ( shortAddr ) ; //修改目标地址
2024-10-23 20:33:05 +08:00
std : : string strTime = GetLocalTimeWithMs ( ) ;
2024-10-22 19:04:25 +08:00
memset ( buf , 0 , 32 ) ;
sprintf ( buf , " %02x%02x " , pRecvData - > Data [ 48 ] , pRecvData - > Data [ 49 ] ) ; // Zigbee 目标地址 2 byte
dataNodeInfo . ZigbeeDesAddr = std : : string ( buf ) ;
// 50 51 52=》序号23 zigbee重试间隔
memset ( buf , 0 , 32 ) ;
sprintf ( buf , " %02x " , pRecvData - > Data [ 50 ] ) ; // Zigbee 发射功率 1 byte
dataNodeInfo . ZigbeePower = atoi ( buf ) ;
memset ( buf , 0 , 32 ) ;
sprintf ( buf , " %d " , BUILD_UINT16 ( 00 , pRecvData - > Data [ 51 ] ) ) ; // Zigbee 重试次数 1 byte
dataNodeInfo . ZigbeeRetry = atoi ( buf ) ;
memset ( buf , 0 , 32 ) ;
sprintf ( buf , " %d " , BUILD_UINT16 ( 00 , pRecvData - > Data [ 52 ] ) ) ; // Zigbee 重试间隔 1 byte
dataNodeInfo . ZigbeeRetryGap = atoi ( buf ) ;
if ( dataNodeInfo . ProductNo = = " 01 " ) {
chTemp = pRecvData - > Data [ 53 ] ;
unsigned char range = ( chTemp > > 2 ) & 0x3 ;
dataNodeInfo . Range = range ;
int SamplingRate = ( chTemp & 0x3 ) ;
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 ) ;
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 ;
}
}
2025-04-28 19:36:13 +08:00
int upgrade = GET_BIT ( chTemp , 7 ) ;
2025-05-06 10:07:30 +08:00
2025-04-28 19:36:13 +08:00
2024-10-22 19:04:25 +08:00
// 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 ) {
2024-10-23 22:25:03 +08:00
dataNodeInfo . StartBrands = std : : to_string ( iTemp ) ;
2024-10-22 19:04:25 +08:00
} else {
2024-10-23 22:25:03 +08:00
dataNodeInfo . StartBrands + = ( " , " + std : : to_string ( iTemp ) ) ;
2024-10-22 19:04:25 +08:00
}
}
//使用了57 58 61 62 65 66 69 70 73 74
for ( int j = 0 ; j < 5 ; j + + ) {
memset ( buf , 0 , 32 ) ;
sprintf ( buf , " %02x%02x " , pRecvData - > Data [ 57 + j * 4 ] , pRecvData - > Data [ 57 + j * 4 + 1 ] ) ;
iTemp = ( int ) strtol ( buf , NULL , 16 ) ;
if ( 0 = = j ) {
2024-10-23 22:25:03 +08:00
dataNodeInfo . StopBrands = std : : to_string ( iTemp ) ;
2024-10-22 19:04:25 +08:00
} else {
2024-10-23 22:25:03 +08:00
dataNodeInfo . StopBrands + = ( " , " + std : : to_string ( iTemp ) ) ;
2024-10-22 19:04:25 +08:00
}
}
memset ( buf , 0 , 32 ) ;
sprintf ( buf , " %02x%02x " , pRecvData - > Data [ 75 ] , pRecvData - > Data [ 76 ] ) ;
iTemp = ( int ) strtol ( buf , NULL , 16 ) ;
2024-10-23 22:25:03 +08:00
dataNodeInfo . EnvelopeBandPass = std : : to_string ( iTemp ) ;
2024-10-22 19:04:25 +08:00
memset ( buf , 0 , 32 ) ;
sprintf ( buf , " %02x%02x " , pRecvData - > Data [ 77 ] , pRecvData - > Data [ 78 ] ) ;
iTemp = ( int ) strtol ( buf , NULL , 16 ) ;
2024-10-23 22:25:03 +08:00
dataNodeInfo . EnvelopeBandPass + = ( " , " + std : : to_string ( iTemp ) ) ;
2024-10-22 19:04:25 +08:00
//使用了79 80 81 82 83 84 85 86
for ( int j = 0 ; j < 4 ; j + + ) {
memset ( buf , 0 , 32 ) ;
sprintf ( buf , " %02x%02x " , pRecvData - > Data [ 79 + j * 2 ] , pRecvData - > Data [ 79 + j * 2 + 1 ] ) ;
iTemp = ( int ) strtol ( buf , NULL , 16 ) ;
if ( 0 = = j ) {
2024-10-23 22:25:03 +08:00
dataNodeInfo . FaultFrequency = std : : to_string ( iTemp ) ;
2024-10-22 19:04:25 +08:00
} else {
2024-10-23 22:25:03 +08:00
dataNodeInfo . FaultFrequency + = ( " , " + std : : to_string ( iTemp ) ) ;
2024-10-22 19:04:25 +08:00
}
}
memset ( buf , 0 , 32 ) ;
sprintf ( buf , " %02x%02x%02x%02x " , pRecvData - > Data [ 87 ] , pRecvData - > Data [ 88 ] , pRecvData - > Data [ 89 ] , pRecvData - > Data [ 90 ] ) ;
long lTimeStamp = strtol ( buf , NULL , 16 ) ;
2024-10-23 22:25:03 +08:00
dataNodeInfo . ConfigDate = std : : to_string ( lTimeStamp ) ;
2024-10-22 19:04:25 +08:00
chTemp = pRecvData - > Data [ 91 ] ;
memset ( buf , 0 , 32 ) ;
sprintf ( buf , " %d " , chTemp ) ;
dataNodeInfo . VIntegralFilterFrequency = atoi ( buf ) ;
2025-05-06 10:07:30 +08:00
sprintf ( whereCon , " dataNodeNo='%s' " , dataNodeInfo . ZigbeeLongAddr . c_str ( ) ) ;
std : : string soft_version = sqlite_db_ctrl : : instance ( ) . GetData ( T_SENSOR_INFO ( TNAME ) , " softVersion " , whereCon ) ;
2025-04-28 19:36:13 +08:00
2024-10-22 19:04:25 +08:00
sprintf ( whereCon , " dataNodeNo='%s' " , dataNodeInfo . ZigbeeLongAddr . c_str ( ) ) ;
2024-10-22 20:56:21 +08:00
if ( sqlite_db_ctrl : : instance ( ) . GetTableRows ( T_SENSOR_INFO ( TNAME ) , whereCon ) > 0 ) {
2024-10-22 19:04:25 +08:00
char updateSql [ 1024 ] = { 0 } ;
sprintf ( updateSql , " initFlag = '%d',accFlag = '%d',zigbeeFlag = '%d',temTopFlag = '%d',temBotFlag = '%d',equipSta = '%d', \
hardVersion = ' % s ' , softVersion = ' % s ' , bpNo = ' % s ' , serialNo = ' % s ' , firstPowerTime = ' % s ' , WakeupTime = ' % d ' , \
StaticTime = ' % d ' , WaveTime = ' % d ' , BateryV = ' % d ' , ProductNo = ' % s ' , configFlag = ' % d ' , startBrands = ' % s ' , \
stopBrands = ' % s ' , featureInterVal = ' % u ' , waveInterVal = ' % d ' , samplingRate = ' % d ' , scope = ' % s ' , range = ' % d ' , envelopeBandPass = ' % s ' , faultFrequency = ' % s ' , \
zigbeePanId = ' % s ' , zigbeeChannel = ' % d ' , zigbeeShortAddr = ' % s ' , zigbeeLongAddr = ' % s ' , zigbeeDesAddr = ' % s ' , \
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 ( ) ) ;
2024-10-25 18:45:26 +08:00
res = sqlite_db_ctrl : : instance ( ) . UpdateTableData ( T_SENSOR_INFO ( TNAME ) , updateSql , whereCon ) ;
if ( res ! = 0 ) {
zlog_error ( zct , " res = %d " , res ) ;
}
2024-10-22 19:04:25 +08:00
} else {
char insertSql [ 1024 ] = { 0 } ;
sprintf ( insertSql , " '%s','%s','%d','%d','%d','%d','%d','%d', \
' % s ' , ' % s ' , ' % s ' , ' % s ' , ' % s ' , ' % d ' , \
' % d ' , ' % d ' , ' % d ' , ' % s ' , ' % d ' , ' % s ' , \
' % s ' , ' % u ' , ' % d ' , ' % d ' , ' % s ' , ' % d ' , ' % s ' , ' % s ' , \
' % s ' , ' % d ' , ' % s ' , ' % s ' , ' % s ' , \
2024-11-09 16:15:39 +08:00
' % d ' , ' % d ' , ' % d ' , ' % d ' , ' % s ' , ' % s ' , ' % d ' , ' 0 , % d ' , ' 1 ' , ' 0 , 0 ' , ' ' , ' ' , ' ' , 0 " ,
2024-10-22 19:04:25 +08:00
dataNodeInfo . ZigbeeLongAddr . c_str ( ) , " " , dataNodeInfo . InitFlag , dataNodeInfo . AccFlag , dataNodeInfo . ZigbeeFlag , dataNodeInfo . TemTopFlag , dataNodeInfo . TemBotFlag , dataNodeInfo . EquipSta , dataNodeInfo . HardVersion . c_str ( ) , dataNodeInfo . SoftVersion . c_str ( ) ,
dataNodeInfo . BpNo . c_str ( ) , dataNodeInfo . SerialNo . c_str ( ) , dataNodeInfo . FirstPowerTime . c_str ( ) , dataNodeInfo . WakeupTime , dataNodeInfo . StaticTime , dataNodeInfo . WaveTime , dataNodeInfo . BateryV , dataNodeInfo . ProductNo . c_str ( ) , dataNodeInfo . ConfigFlag ,
dataNodeInfo . StartBrands . c_str ( ) , dataNodeInfo . StopBrands . c_str ( ) , dataNodeInfo . FeatureInterVal , dataNodeInfo . WaveInterVal , dataNodeInfo . SamplingRate , " " , dataNodeInfo . Range , dataNodeInfo . EnvelopeBandPass . c_str ( ) , dataNodeInfo . FaultFrequency . c_str ( ) ,
dataNodeInfo . ZigbeePanId . c_str ( ) , dataNodeInfo . ZigbeeChannel , dataNodeInfo . ZigbeeShortAddr . c_str ( ) , dataNodeInfo . ZigbeeLongAddr . c_str ( ) , dataNodeInfo . ZigbeeDesAddr . c_str ( ) , dataNodeInfo . ZigbeePower , dataNodeInfo . ZigbeeRetry , dataNodeInfo . ZigbeeRetryGap ,
dataNodeInfo . ACCSampleTime , " 1 " , dataNodeInfo . ConfigDate . c_str ( ) , dataNodeInfo . VIntegralFilterFrequency , dataNodeInfo . RSSI ) ;
2024-10-25 18:45:26 +08:00
res = sqlite_db_ctrl : : instance ( ) . InsertData ( T_SENSOR_INFO ( TNAME ) , insertSql ) ;
if ( res ! = 0 ) {
zlog_error ( zct , " res = %d " , res ) ;
}
2024-10-22 19:04:25 +08:00
}
2025-05-06 10:07:30 +08:00
zlog_info ( zct , " upgrade = %d " , upgrade ) ;
if ( upgrade = = 1 )
{
zlog_info ( zct , " soft_version = %s,dataNodeInfo.SoftVersion = %s " , soft_version . c_str ( ) , dataNodeInfo . SoftVersion . c_str ( ) ) ;
if ( dataNodeInfo . SoftVersion ! = soft_version ) {
DealUpgrade ( ushortAdd , 2 ) ;
scheduler : : instance ( ) . UpgradeResult ( ushortAdd , 0 ) ;
} else {
DealUpgrade ( ushortAdd , 3 ) ;
scheduler : : instance ( ) . UpgradeResult ( ushortAdd , 3 ) ;
}
}
2024-10-22 19:04:25 +08:00
Json : : Value jsonVal ;
jsonVal . clear ( ) ;
jsonVal [ " cmd " ] = " 26 " ;
Json : : Value jsBody ;
jsBody [ " dataNodeGatewayNo " ] = GlobalConfig : : MacAddr_G ;
jsBody [ " dataNodeNo " ] = dataNodeInfo . ZigbeeLongAddr ;
jsBody [ " type " ] = " insert " ;
Json : : Value jsSensorData ;
jsSensorData [ " dataNodeNo " ] = dataNodeInfo . ZigbeeLongAddr ;
jsSensorData [ " dataNodeName " ] = " " ;
jsSensorData [ " initFlag " ] = dataNodeInfo . InitFlag ;
jsSensorData [ " accFlag " ] = dataNodeInfo . AccFlag ;
jsSensorData [ " zigbeeFlag " ] = dataNodeInfo . ZigbeeFlag ;
jsSensorData [ " temTopFlag " ] = dataNodeInfo . TemTopFlag ;
jsSensorData [ " temBotFlag " ] = dataNodeInfo . TemBotFlag ;
jsSensorData [ " equipsta " ] = dataNodeInfo . EquipSta ;
jsSensorData [ " hardVersion " ] = dataNodeInfo . HardVersion ;
jsSensorData [ " softVersion " ] = dataNodeInfo . SoftVersion ;
jsSensorData [ " bpNo " ] = dataNodeInfo . BpNo ;
jsSensorData [ " serialNo " ] = dataNodeInfo . SerialNo ;
jsSensorData [ " firstPowerTime " ] = dataNodeInfo . FirstPowerTime ;
jsSensorData [ " configFlag " ] = dataNodeInfo . ConfigFlag ;
jsSensorData [ " startBrands " ] = dataNodeInfo . StartBrands ;
jsSensorData [ " stopBrands " ] = dataNodeInfo . StopBrands ;
jsSensorData [ " featureInterVal " ] = dataNodeInfo . FeatureInterVal ;
jsSensorData [ " waveInterVal " ] = dataNodeInfo . WaveInterVal ;
jsSensorData [ " samplingRate " ] = dataNodeInfo . SamplingRate ;
jsSensorData [ " range " ] = dataNodeInfo . Range ;
jsSensorData [ " envelopeBandPass " ] = dataNodeInfo . EnvelopeBandPass ;
jsSensorData [ " faultFrequency " ] = dataNodeInfo . FaultFrequency ;
jsSensorData [ " zigbeePanId " ] = dataNodeInfo . ZigbeePanId ;
jsSensorData [ " zigbeeChannel " ] = dataNodeInfo . ZigbeeChannel ;
jsSensorData [ " zigbeeAddr " ] = dataNodeInfo . ZigbeeLongAddr ;
jsSensorData [ " zigbeeDesAddr " ] = dataNodeInfo . ZigbeeDesAddr ;
jsSensorData [ " status " ] = 1 ;
jsSensorData [ " timeStamp " ] = dataNodeInfo . ConfigDate ;
jsSensorData [ " VIntegralFilterFrequency " ] = dataNodeInfo . VIntegralFilterFrequency ;
jsSensorData [ " RSSI " ] = dataNodeInfo . RSSI ;
jsBody [ " dataNodeInfo " ] = jsSensorData ;
Json : : FastWriter showValue ;
std : : string dataBody = showValue . write ( jsBody ) ;
jsonVal [ " cmdBody " ] = dataBody ;
std : : string strCmd26 = showValue . write ( jsonVal ) ;
// 传感器发来的数据包中的表示设备信息的数据转化为json格式后, 通过调用data_publish将数据传给mqttclient : Topic: wireless/cmd/60294D203717
2024-10-25 18:45:26 +08:00
res = data_publish ( strCmd26 . c_str ( ) , GlobalConfig : : Topic_G . mPubCmd . c_str ( ) ) ;
if ( res ! = 0 ) {
zlog_error ( zct , " res = %d " , res ) ;
}
2024-10-23 20:33:05 +08:00
zlog_info ( zct , " 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 \
2024-10-23 19:51:01 +08:00
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 " ,
2024-10-23 20:33:05 +08:00
dataNodeInfo . AccFlag , dataNodeInfo . EquipSta , dataNodeInfo . BpNo . c_str ( ) , dataNodeInfo . ConfigFlag , dataNodeInfo . EnvelopeBandPass . c_str ( ) , dataNodeInfo . FaultFrequency . c_str ( ) , dataNodeInfo . FeatureInterVal , dataNodeInfo . FirstPowerTime . c_str ( ) ,
dataNodeInfo . HardVersion . c_str ( ) , dataNodeInfo . InitFlag , dataNodeInfo . SamplingRate , dataNodeInfo . Range , dataNodeInfo . SerialNo . c_str ( ) , dataNodeInfo . SoftVersion . c_str ( ) , dataNodeInfo . StartBrands . c_str ( ) , dataNodeInfo . StopBrands . c_str ( ) , dataNodeInfo . TemBotFlag ,
dataNodeInfo . TemTopFlag , dataNodeInfo . WaveInterVal , dataNodeInfo . ZigbeeChannel , dataNodeInfo . ZigbeeDesAddr . c_str ( ) , dataNodeInfo . ZigbeeFlag , dataNodeInfo . ZigbeeLongAddr . c_str ( ) , dataNodeInfo . ZigbeePanId . c_str ( ) , dataNodeInfo . ZigbeeShortAddr . c_str ( ) ,
dataNodeInfo . ConfigDate . c_str ( ) , dataNodeInfo . VIntegralFilterFrequency , dataNodeInfo . RSSI ) ;
2024-10-22 19:04:25 +08:00
if ( g_mapCompress . find ( dataNodeInfo . ZigbeeShortAddr ) = = g_mapCompress . end ( ) ) {
compressWaveChannel tempchannel ;
g_mapCompress . insert ( std : : make_pair ( dataNodeInfo . ZigbeeShortAddr , tempchannel ) ) ;
2024-10-23 20:33:05 +08:00
zlog_info ( zct , " new Node,size = %d " , g_mapCompress . size ( ) ) ;
2024-10-22 19:04:25 +08:00
}
2024-10-23 20:33:05 +08:00
zlog_info ( zct , " DealDataNodeInfo %s " , dataNodeInfo . ZigbeeShortAddr . c_str ( ) ) ;
2024-10-22 19:04:25 +08:00
}
void Uart : : ZigbeeParameterConfig ( ) {
2024-10-22 20:56:21 +08:00
std : : string strPanId = sqlite_db_ctrl : : instance ( ) . GetData ( " t_gateway_info " , " zigbeePanID " , NULL ) ;
2024-10-22 19:04:25 +08:00
{
unsigned short shortAddr = 0x8888 ;
WriteShortAddr2Zigbee ( shortAddr ) ;
mssleep ( 100000 ) ;
}
2024-10-23 20:33:05 +08:00
zlog_info ( zct , " strPanId : %s " , strPanId . c_str ( ) ) ;
zlog_info ( zct , " MacAddr_G : %s " , GlobalConfig : : MacAddr_G . c_str ( ) ) ;
2024-10-22 19:04:25 +08:00
std : : string strchan = ReadStrByOpt ( ZIGBEECONFIG , " Zigbee " , " channel " ) ;
unsigned short Chan = ( unsigned short ) strtol ( strchan . c_str ( ) , NULL , 10 ) ;
2024-10-23 22:25:03 +08:00
zlog_info ( zct , " the chan = %d " , Chan ) ;
2024-10-23 20:33:05 +08:00
zlog_info ( zct , " ZigbeeInfo_G.Channel=%d " , GlobalConfig : : ZigbeeInfo_G . Channel ) ;
2024-10-22 19:04:25 +08:00
if ( Chan > 10 & & Chan < 27 ) {
WriteChanl2Zigbee ( Chan ) ;
mssleep ( 100000 ) ;
}
2024-10-23 20:33:05 +08:00
zlog_info ( zct , " PanID1 = %s,strPanId = %s " , GlobalConfig : : ZigbeeInfo_G . PanID . c_str ( ) , strPanId . c_str ( ) ) ;
2024-10-22 19:04:25 +08:00
long lShortAddr = strtol ( strPanId . c_str ( ) , NULL , 16 ) ;
unsigned short panid = lShortAddr & 0xffff ;
WritePanId2Zigbee ( panid ) ;
mssleep ( 100000 ) ;
2024-10-23 20:33:05 +08:00
zlog_info ( zct , " ZigbeeInfo_G.MyAddr=%s " , GlobalConfig : : ZigbeeInfo_G . MyAddr . c_str ( ) ) ;
2024-10-22 19:04:25 +08:00
}
int Uart : : FindRecvPackage ( int bytesRead , char * mUartRecvBuf , char * head ) {
2024-11-12 20:04:43 +08:00
char localtimestamp [ 32 ] = { 0 } ;
GetTimeNet ( localtimestamp , 1 ) ;
2024-10-22 19:04:25 +08:00
char head1 [ ] = { 0xAB , 0xBC , 0xCD } ;
char head2 [ ] = { 0xDE , 0xDF , 0xEF } ;
int lastSize = 0 ;
char UartRecvBuf [ BUF_LENGTH * 15 ] = { 0x00 } ;
char RecvBuf [ 200 ] = { 0x00 } ;
if ( mlastSize > 0 ) {
2024-10-23 20:33:05 +08:00
zlog_info ( zct , " mlastSize = %d " , mlastSize ) ;
2024-10-22 19:04:25 +08:00
memcpy ( UartRecvBuf , mUartRecvTmpBuf , mlastSize ) ;
for ( int i = 0 ; i < mlastSize ; i + + ) {
2024-10-23 20:33:05 +08:00
zlog_info ( zct , " %02x " , UartRecvBuf [ i ] ) ;
2024-10-22 19:04:25 +08:00
}
memset ( mUartRecvTmpBuf , 0x00 , sizeof ( mUartRecvTmpBuf ) ) ;
}
memcpy ( UartRecvBuf + mlastSize , mUartRecvBuf , bytesRead ) ;
2025-04-03 10:00:57 +08:00
bool wave_error_x = false ;
bool wave_error_y = false ;
bool wave_error_z = false ;
2024-10-22 19:04:25 +08:00
bytesRead = bytesRead + mlastSize ;
2025-04-07 15:51:25 +08:00
2024-10-22 19:04:25 +08:00
for ( int i = 0 ; i < bytesRead ; i + + ) {
if ( UartRecvBuf [ i ] = = head [ 0 ] ) {
2024-11-05 11:10:50 +08:00
char buf [ 12 ] = { 0x00 } ;
2024-10-22 19:04:25 +08:00
char ShortAddr [ 8 ] = { 0x00 } ;
2024-11-05 11:10:50 +08:00
sprintf ( buf , " %02X%02X%02X " , UartRecvBuf [ i ] & 0xFF , UartRecvBuf [ i + 1 ] & 0xFF , UartRecvBuf [ i + 2 ] & 0xFF ) ;
2024-10-22 19:04:25 +08:00
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 } ;
2024-10-29 19:26:36 +08:00
sprintf ( buf , " %02d " , UartRecvBuf [ i + 5 ] ) ;
2024-10-22 19:04:25 +08:00
int command = atoi ( buf ) ;
2025-04-03 10:00:57 +08:00
if ( wave_error_x & & ! wave_error_y & & ! wave_error_z & & command = = WAVE_X )
{
continue ;
}
2025-04-07 15:51:25 +08:00
else if ( ( wave_error_x | | wave_error_y ) & & ! wave_error_z & & command = = WAVE_Y )
2025-04-03 10:00:57 +08:00
{
continue ;
}
if ( wave_error_z & & command = = WAVE_Z )
{
m_waveCountX = 0 ;
m_waveCountY = 0 ;
m_waveCountZ = 0 ;
g_VecWaveDataX . clear ( ) ;
g_VecWaveDataY . clear ( ) ;
g_VecWaveDataZ . clear ( ) ;
break ;
}
2026-03-02 19:02:28 +08:00
if ( ( mPackgeIndex = = - 1 | | ( unsigned int ) UartRecvBuf [ i + 6 ] = = 0 ) & & \
( command = = WAVE_X | | command = = WAVE_Y | | command = = WAVE_Z | | command = = WAVE_LF_X | | command = = WAVE_LF_Y | | command = = WAVE_LF_Z ) & & \
now_task = = WAVE_CMD ) {
2024-10-22 19:04:25 +08:00
mPackgeIndex = UartRecvBuf [ i + 6 ] & 0xFF ;
2026-03-02 19:02:28 +08:00
} else if ( ( unsigned int ) mPackgeIndex = = ( unsigned int ) UartRecvBuf [ i + 6 ] & & mPackgeIndex ! = - 1 & & command ! = 2 & & \
( command = = WAVE_X | | command = = WAVE_Y | | command = = WAVE_Z | | command = = WAVE_LF_X | | command = = WAVE_LF_Y | | command = = WAVE_LF_Z ) & & \
now_task = = WAVE_CMD ) {
2024-11-08 09:17:35 +08:00
zlog_warn ( zct , " mPackgeIndex same index1:%d,index2:%02d ShortAddr :%s " , mPackgeIndex , UartRecvBuf [ i + 6 ] & 0xff , strShortAddr . c_str ( ) ) ;
2024-10-22 19:04:25 +08:00
continue ;
2026-03-02 19:02:28 +08:00
} else if ( ( unsigned int ) mPackgeIndex + 1 ! = ( unsigned int ) UartRecvBuf [ i + 6 ] & & mPackgeIndex ! = - 1 & & command ! = 2 & & ( command = = WAVE_X | | command = = WAVE_Y | | command = = WAVE_Z | | command = = WAVE_LF_X | | command = = WAVE_LF_Y | | command = = WAVE_LF_Z ) & & now_task = = WAVE_CMD ) {
2024-10-22 19:04:25 +08:00
2026-03-02 19:02:28 +08:00
zlog_error ( zct , " mPackgeIndex error index1:%d,index2:%02d ShortAddr :%s ,now_task = %d " , mPackgeIndex , UartRecvBuf [ i + 6 ] & 0xff , strShortAddr . c_str ( ) , now_task ) ;
zlog_error ( zct , " mPackgeIndex error ShortAddr :%s " , strShortAddr . c_str ( ) ) ;
2024-10-22 19:04:25 +08:00
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 ) ;
}
2026-03-02 19:02:28 +08:00
zlog_error ( zct , " error str = %s " , tmp2 ) ;
2024-11-12 20:04:43 +08:00
char insertSql [ 100 ] = { 0x00 } ;
char whereCon [ 50 ] = { 0x00 } ;
2024-11-20 16:45:04 +08:00
std : : string error_msg = " " ;
std : : string strChannelID = " " ;
2024-11-12 20:04:43 +08:00
sprintf ( whereCon , " zigbeeShortAddr='%s' " , strShortAddr . c_str ( ) ) ;
2024-11-20 16:45:04 +08:00
std : : string strMeasurementID = sqlite_db_ctrl : : instance ( ) . GetData ( T_SENSOR_INFO ( TNAME ) , " MeasurementID " , whereCon ) ;
2024-11-12 20:04:43 +08:00
memset ( whereCon , 0x00 , sizeof ( whereCon ) ) ;
2024-11-15 19:47:21 +08:00
if ( command = = WAVE_X ) {
2024-11-20 16:45:04 +08:00
error_msg = " Index error,wave X " ;
strChannelID = strMeasurementID + " -X " ;
2025-04-03 10:00:57 +08:00
wave_error_x = true ;
2024-11-15 19:47:21 +08:00
} else if ( command = = WAVE_Y ) {
2024-11-20 16:45:04 +08:00
error_msg = " Index error,wave Y " ;
strChannelID = strMeasurementID + " -Y " ;
2025-04-03 10:00:57 +08:00
wave_error_y = true ;
2024-11-15 19:47:21 +08:00
} else if ( command = = WAVE_Z ) {
2024-11-20 16:45:04 +08:00
error_msg = " Index error,wave Z " ;
strChannelID = strMeasurementID + " -Z " ;
2025-04-03 10:00:57 +08:00
wave_error_z = true ;
2024-11-15 19:47:21 +08:00
}
2025-04-10 15:51:47 +08:00
zlog_error ( zct , " WaveError error ShortAddr :%s " , strShortAddr . c_str ( ) ) ;
2025-05-30 18:35:58 +08:00
sprintf ( insertSql , " '%s','%s','%s',%d,'%d','%s','' " , strChannelID . c_str ( ) , strShortAddr . c_str ( ) , localtimestamp , bytesRead , 0 , error_msg . c_str ( ) ) ;
2025-04-10 15:51:47 +08:00
sqlite_db_ctrl : : instance ( ) . InsertData ( " receive_wave_status " , insertSql ) ;
2025-04-03 10:00:57 +08:00
2024-11-20 16:45:04 +08:00
JsonData jd ;
jd . JsonCmd_32 ( strMeasurementID , 1 , 1 , strChannelID , error_msg ) ;
2025-04-03 10:00:57 +08:00
continue ;
2024-10-22 19:04:25 +08:00
}
2026-03-02 19:02:28 +08:00
if ( now_task = = WAVE_CMD & & ( command = = WAVE_X | | command = = WAVE_Y | | command = = WAVE_Z | | command = = WAVE_LF_X | | command = = WAVE_LF_Y | | command = = WAVE_LF_Z ) ) {
2024-10-22 19:04:25 +08:00
if ( ! CheckCrc ( & UartRecvBuf [ i ] , 99 ) ) {
2026-03-02 19:02:28 +08:00
zlog_error ( zct , " CheckCrc error ShortAddr :%s command = %d " , strShortAddr . c_str ( ) , command ) ;
2024-10-22 19:04:25 +08:00
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 ) ;
}
2024-10-23 20:33:05 +08:00
zlog_error ( zct , " error str = %s " , tmp2 ) ;
2025-04-03 10:00:57 +08:00
2024-11-12 20:04:43 +08:00
char insertSql [ 100 ] = { 0x00 } ;
char whereCon [ 50 ] = { 0x00 } ;
2024-11-20 16:45:04 +08:00
std : : string error_msg = " " ;
std : : string strChannelID = " " ;
2024-11-12 20:04:43 +08:00
sprintf ( whereCon , " zigbeeShortAddr='%s' " , strShortAddr . c_str ( ) ) ;
2024-11-20 16:45:04 +08:00
std : : string strMeasurementID = sqlite_db_ctrl : : instance ( ) . GetData ( T_SENSOR_INFO ( TNAME ) , " MeasurementID " , whereCon ) ;
2024-11-12 20:04:43 +08:00
memset ( whereCon , 0x00 , sizeof ( whereCon ) ) ;
2024-11-15 19:47:21 +08:00
if ( command = = WAVE_X ) {
2024-11-20 16:45:04 +08:00
error_msg = " Crc error,wave X " ;
2025-01-23 20:07:10 +08:00
strChannelID = strMeasurementID + " -X " ;
2025-04-03 10:00:57 +08:00
wave_error_x = true ;
2024-11-15 19:47:21 +08:00
} else if ( command = = WAVE_Y ) {
2024-11-20 16:45:04 +08:00
error_msg = " Crc error,wave Y " ;
2025-01-23 20:07:10 +08:00
strChannelID = strMeasurementID + " -Y " ;
2025-04-03 10:00:57 +08:00
wave_error_y = true ;
2024-11-15 19:47:21 +08:00
} else if ( command = = WAVE_Z ) {
2024-11-20 16:45:04 +08:00
error_msg = " Crc error,wave Z " ;
2025-01-23 20:07:10 +08:00
strChannelID = strMeasurementID + " -Z " ;
2025-04-03 10:00:57 +08:00
wave_error_z = true ;
}
2025-04-10 15:51:47 +08:00
zlog_error ( zct , " WaveError error ShortAddr :%s " , strShortAddr . c_str ( ) ) ;
2025-05-30 18:35:58 +08:00
sprintf ( insertSql , " '%s','%s','%s',%d,'%d','%s','' " , strChannelID . c_str ( ) , strShortAddr . c_str ( ) , localtimestamp , bytesRead , 0 , error_msg . c_str ( ) ) ;
2025-04-10 15:51:47 +08:00
sqlite_db_ctrl : : instance ( ) . InsertData ( " receive_wave_status " , insertSql ) ;
2024-11-20 16:45:04 +08:00
JsonData jd ;
jd . JsonCmd_32 ( strMeasurementID , 1 , 1 , strChannelID , error_msg ) ;
2025-04-03 10:00:57 +08:00
continue ;
2024-10-22 19:04:25 +08:00
}
mlastSize = 0 ;
lastSize = bytesRead - i ;
if ( lastSize < 100 & & lastSize > 0 ) {
memcpy ( mUartRecvTmpBuf , ( char * ) & UartRecvBuf [ bytesRead - lastSize ] , lastSize ) ;
mlastSize = lastSize ;
break ;
}
memcpy ( RecvBuf , ( char * ) & UartRecvBuf [ i ] , 100 ) ;
DealDataNodeWave ( RecvBuf , command ) ;
2025-01-06 16:44:15 +08:00
mPackgeIndex = ( unsigned int ) UartRecvBuf [ i + 6 ] ;
2025-05-29 21:34:17 +08:00
} else if ( now_task ! = WAVE_CMD & & ( command = = ASK_TASK | | command = = DEVICE_INF | | command = = MEAS_EVAL | | command = = CONFIG | | command = = UPGRADE | | command = = DEVICE_INF2 | | command = = SIGNAL_STRENGTH | | command = = DEVICE_EXCEPTION | | command = = UPGRADE_ASK ) ) {
2024-10-22 19:04:25 +08:00
char RecvBuf [ 100 ] = { 0x00 } ;
2025-05-29 21:34:17 +08:00
if ( command = = UPGRADE_ASK | | command = = ASK_TASK )
2024-11-05 17:22:55 +08:00
{
memcpy ( RecvBuf , & UartRecvBuf [ i ] , 8 ) ;
if ( ! CheckCrc ( RecvBuf , 7 ) ) {
2026-03-02 19:02:28 +08:00
zlog_error ( zct , " CheckCrc error ShortAddr :%s command = %d " , strShortAddr . c_str ( ) , command ) ;
2024-11-05 17:22:55 +08:00
break ;
}
}
2025-04-29 19:35:34 +08:00
if ( command = = CONFIG | | command = = CONFIG_INF2 | | command = = UPGRADE | | command = = SIGNAL_STRENGTH | | command = = DEVICE_EXCEPTION )
2024-11-05 17:22:55 +08:00
{
memcpy ( RecvBuf , & UartRecvBuf [ i ] , 9 ) ;
if ( ! CheckCrc ( RecvBuf , 8 ) ) {
2026-03-02 19:02:28 +08:00
zlog_error ( zct , " CheckCrc error ShortAddr :%s command = %d " , strShortAddr . c_str ( ) , command ) ;
2024-11-05 17:22:55 +08:00
break ;
}
2024-10-22 19:04:25 +08:00
}
2024-11-05 17:22:55 +08:00
if ( command = = DEVICE_INF | | command = = MEAS_EVAL | | command = = DEVICE_INF2 )
{
memcpy ( RecvBuf , & UartRecvBuf [ i ] , 100 ) ;
if ( ! CheckCrc ( RecvBuf , 99 ) ) {
2026-03-02 19:02:28 +08:00
zlog_error ( zct , " CheckCrc error ShortAddr :%s command = %d " , strShortAddr . c_str ( ) , command ) ;
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 ) ;
}
zlog_error ( zct , " crc error str = %s " , tmp2 ) ;
2024-11-05 17:22:55 +08:00
break ;
}
}
2024-10-22 19:04:25 +08:00
DealRecvData ( RecvBuf ) ;
2025-04-29 19:35:34 +08:00
} else if ( command = = WAVE_COMPRESS & & now_task ! = WAVE_CMD ) {
2026-03-02 19:02:28 +08:00
if ( bytesRead > 30 )
{
memcpy ( RecvBuf , & UartRecvBuf [ i ] , 100 ) ;
if ( ! CheckCrc ( RecvBuf , 99 ) ) {
zlog_error ( zct , " CheckCrc error ShortAddr :%s command = %d " , strShortAddr . c_str ( ) , command ) ;
break ;
}
} else {
memcpy ( RecvBuf , & UartRecvBuf [ i ] , 24 ) ;
if ( ! CheckCrc ( RecvBuf , 23 ) ) {
zlog_error ( zct , " CheckCrc error ShortAddr :%s command = %d " , strShortAddr . c_str ( ) , command ) ;
break ;
}
2024-11-06 21:16:18 +08:00
}
2024-11-18 19:28:20 +08:00
// char tmp[23] = {0x00};
// char tmp2[23] = {0x00};
// for (int j = 0; j < 23; j++) {
// sprintf(tmp, "%02x ", UartRecvBuf[i + j] & 0xff);
// strcat(tmp2, tmp);
// }
// zlog_info(zct, "str = %s", tmp2);
2024-11-06 21:16:18 +08:00
DealRecvData ( RecvBuf ) ;
2024-10-29 19:26:36 +08:00
} else if ( command = = 35 ) {
2024-10-22 19:04:25 +08:00
char signalNode [ 10 ] = { 0x00 } ;
sprintf ( signalNode , " %02d " , UartRecvBuf [ i + 14 ] & 0xFF ) ;
if ( ! strcmp ( signalNode , " 00 " ) | | ! strcmp ( signalNode , " 0 " ) ) {
char errorInfo [ 100 ] = { 0x00 } ;
2024-11-13 17:50:54 +08:00
sprintf ( errorInfo , " No RSSI %s " , signalNode ) ;
zlog_warn ( zct , errorInfo ) ;
2024-10-22 19:04:25 +08:00
} else {
char whereCon [ 1024 ] = { 0 } ;
char updateSql [ 1024 ] = { 0 } ;
char tableName [ 100 ] = { 0x00 } ;
sprintf ( whereCon , " zigbeeShortAddr='%s' " , strShortAddr . c_str ( ) ) ;
2024-10-22 20:56:21 +08:00
vec_t vecDataNodeNo = sqlite_db_ctrl : : instance ( ) . GetDataSingleLine ( T_SENSOR_INFO ( TNAME ) , " dataNodeNo,RSSI " , whereCon ) ;
2024-10-22 19:04:25 +08:00
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 ( ) ) ;
2024-10-22 20:56:21 +08:00
sqlite_db_ctrl : : instance ( ) . UpdateTableData ( tableName , updateSql , whereCon ) ;
2024-10-22 19:04:25 +08:00
}
}
2025-01-06 16:44:15 +08:00
2024-10-22 19:04:25 +08:00
} else {
continue ;
}
} else if ( UartRecvBuf [ i ] = = head1 [ 0 ] ) {
2024-11-05 11:10:50 +08:00
char buf [ 12 ] = { 0x00 } ;
sprintf ( buf , " %02X%02X%02X " , UartRecvBuf [ i ] & 0xFF , UartRecvBuf [ i + 1 ] & 0xFF , UartRecvBuf [ i + 2 ] & 0xFF ) ;
2024-10-22 19:04:25 +08:00
std : : string strHeadFlag ( buf ) ;
if ( 0 = = strHeadFlag . compare ( " ABBCCD " ) ) {
char buf [ 8 ] = { 0x00 } ;
sprintf ( buf , " %02d " , UartRecvBuf [ i + 3 ] & 0xFF ) ;
int command = atoi ( buf ) ;
2024-10-23 20:33:05 +08:00
zlog_info ( zct , " command = %d " , command ) ;
2024-10-22 19:04:25 +08:00
if ( command = = 209 ) {
UpdateZigbeeInfo ( & UartRecvBuf [ i ] ) ;
} else if ( command = = 220 ) { // DC
2024-10-23 20:33:05 +08:00
zlog_info ( zct , " zigbeeShortAddr = %s , ret = %02d " , m_strDestShortAddr . c_str ( ) , UartRecvBuf [ i + 6 ] & 0xFF ) ;
2024-10-23 22:25:03 +08:00
if ( ( UartRecvBuf [ i + 6 ] & 0xFF ) ! = 0 ) {
2024-10-22 19:04:25 +08:00
modify_LocalAddr ( 0x8888 ) ;
}
}
}
} else if ( UartRecvBuf [ i ] = = head2 [ 0 ] ) {
2024-11-05 11:10:50 +08:00
char buf [ 12 ] = { 0x00 } ;
sprintf ( buf , " %02X%02X%02X " , UartRecvBuf [ i ] & 0xFF , UartRecvBuf [ i + 1 ] & 0xFF , UartRecvBuf [ i + 2 ] & 0xFF ) ;
2024-10-22 19:04:25 +08:00
std : : string strHeadFlag ( buf ) ;
if ( 0 = = strHeadFlag . compare ( " DEDFEF " ) ) {
char buf [ 8 ] = { 0x00 } ;
sprintf ( buf , " %02d " , UartRecvBuf [ i + 3 ] & 0xFF ) ;
int command = atoi ( buf ) ;
2024-10-23 20:33:05 +08:00
zlog_info ( zct , " command = %d " , command ) ;
2024-10-22 19:04:25 +08:00
if ( command = = 209 ) { // D1
pTestRecv ( command ) ;
} else if ( command = = 219 ) { // DB
pTestRecv ( command ) ;
} else if ( command = = 220 ) { // DC
2024-10-23 20:33:05 +08:00
zlog_info ( zct , " %02x,%02x,%02x,%02x,%02x " , UartRecvBuf [ i ] , UartRecvBuf [ i + 1 ] , UartRecvBuf [ i + 2 ] , UartRecvBuf [ i + 3 ] , UartRecvBuf [ i + 4 ] ) ;
zlog_info ( zct , " zigbeeShortAddr = %s , ret = %02d " , m_strDestShortAddr . c_str ( ) , UartRecvBuf [ i + 4 ] & 0xFF ) ;
2024-10-22 19:04:25 +08:00
2024-11-13 17:50:54 +08:00
} else if ( command = = 218 ) { // DA 网关获取传感器信号强度
2024-11-10 19:22:16 +08:00
char localtimestamp [ 32 ] = { 0 } ;
GetTimeNet ( localtimestamp , 1 ) ;
2024-10-22 19:04:25 +08:00
char whereCon [ 1024 ] = { 0 } ;
char updateSql [ 1024 ] = { 0 } ;
char tableName [ 100 ] = { 0x00 } ;
bZigbeeSinal = false ;
2024-11-12 20:04:43 +08:00
sprintf ( whereCon , " zigbeeShortAddr='%02x%02x' " , UINT16_HIGH ( wave_shortAddr ) , UINT16_LOW ( wave_shortAddr ) ) ;
2024-11-13 17:50:54 +08:00
vec_t vecDataNodeNo = sqlite_db_ctrl : : instance ( ) . GetDataSingleLine ( T_SENSOR_INFO ( TNAME ) , " MeasurementID,LooseValue,RSSI " , whereCon ) ;
2024-10-22 19:04:25 +08:00
memset ( whereCon , 0x00 , sizeof ( whereCon ) ) ;
2024-11-13 17:50:54 +08:00
sprintf ( tableName , " t_dataStatic_%s " , ( char * ) vecDataNodeNo [ 0 ] . c_str ( ) ) ;
memset ( whereCon , 0 , sizeof ( whereCon ) ) ;
2024-11-14 18:48:45 +08:00
sprintf ( whereCon , " dataNodeNo='%s' order by timeStamp desc limit 1 " , vecDataNodeNo [ 0 ] . c_str ( ) ) ;
2024-11-13 17:50:54 +08:00
std : : string timestamp_last = sqlite_db_ctrl : : instance ( ) . GetData ( tableName , " timeStamp " , whereCon ) ;
sprintf ( updateSql , " zigbeeSignal = '%02d' " , UartRecvBuf [ i + 6 ] & 0xFF ) ;
sprintf ( whereCon , " dataNodeNo='%s' and timeStamp = '%s' " , ( char * ) vecDataNodeNo [ 0 ] . c_str ( ) , timestamp_last . c_str ( ) ) ;
sqlite_db_ctrl : : instance ( ) . UpdateTableData ( tableName , updateSql , whereCon ) ;
2024-11-10 19:22:16 +08:00
// zigbeeRSSIType = 1 网关获取传感器信号强度
2024-10-22 19:04:25 +08:00
Json : : Value jsBody , jsonVal ;
Json : : FastWriter showValue ;
char looseValue [ 10 ] = { 0x00 } ;
2024-10-24 16:01:21 +08:00
readStringValue ( " config " , ( char * ) " loose " , looseValue , GlobalConfig : : Config_G . c_str ( ) ) ;
2024-10-22 19:04:25 +08:00
if ( atof ( looseValue ) < atof ( vecDataNodeNo [ 1 ] . c_str ( ) ) ) {
jsBody [ " looseStatus " ] = " 1 " ;
} else {
jsBody [ " looseStatus " ] = " 0 " ;
}
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 ( ) ) ;
}
}
}
}
2024-10-23 22:25:03 +08:00
return 0 ;
}
2024-10-24 16:01:21 +08:00
bool Uart : : CheckCrc ( char * pCheckBuff , int No ) {
unsigned char tmp = 0x00 ;
for ( int i = 0 ; i < No ; i + + ) {
tmp + = ( unsigned char ) pCheckBuff [ i ] ;
}
if ( ( unsigned char ) pCheckBuff [ No ] ! = ( unsigned char ) tmp ) return false ;
return true ;
}