@ -184,6 +184,8 @@ static bool iGetIEC104Conf()
char szTmp [ 512 ] , szSection [ 128 ] ;
char szTmp [ 512 ] , szSection [ 128 ] ;
memset ( & g_iec_conf , 0x00 , sizeof ( ST_IEC104_CONF ) ) ; // 初始化IEC104配置.
memset ( & g_iec_conf , 0x00 , sizeof ( ST_IEC104_CONF ) ) ; // 初始化IEC104配置.
g_iec_conf . orgin_data_save_internal = 3600 ;
// 打开配置文件,准备加载配置参数
// 打开配置文件,准备加载配置参数
if ( fpIec = = NULL ) {
if ( fpIec = = NULL ) {
if ( ( fpIec = fopen ( IEC_CONFIMG_FILE , " r " ) ) = = NULL ) {
if ( ( fpIec = fopen ( IEC_CONFIMG_FILE , " r " ) ) = = NULL ) {
@ -244,6 +246,15 @@ static bool iGetIEC104Conf()
}
}
g_iec_conf . save_internal = ( unsigned int ) atoi ( szTmp ) ;
g_iec_conf . save_internal = ( unsigned int ) atoi ( szTmp ) ;
// 104原始数据保存时间(s)
memset ( szTmp , 0x00 , sizeof ( szTmp ) ) ;
if ( ( iRet = iGetString ( " COMM_SAVE_INTERNAL " , " ORIGIN_DATA_SAVE_INTERNAL " , szTmp , sizeof ( szTmp ) ) ) < 0 ) {
vPrtLogMsg ( LOG_ERROR , iRet , " get DATA_SAVE_INTERNAL parameter failed! " ) ;
fclose ( fpIec ) ;
return false ;
}
g_iec_conf . orgin_data_save_internal = ( unsigned int ) atoi ( szTmp ) ;
// 获取公共地址配置
// 获取公共地址配置
memset ( szTmp , 0x00 , sizeof ( szTmp ) ) ;
memset ( szTmp , 0x00 , sizeof ( szTmp ) ) ;
if ( ( iRet = iGetString ( " IEC_OBJS_ADDR " , " iec_global_addr " , szTmp , sizeof ( szTmp ) ) ) < 0 ) {
if ( ( iRet = iGetString ( " IEC_OBJS_ADDR " , " iec_global_addr " , szTmp , sizeof ( szTmp ) ) ) < 0 ) {
@ -904,7 +915,7 @@ int SendMsgFormatU(int cmd)
iRet = g_Tcp . tcpSendBuffer ( g_IecCtrl . sockid , ( const char * ) & header , sizeof ( ST_APCI ) ) ;
iRet = g_Tcp . tcpSendBuffer ( g_IecCtrl . sockid , ( const char * ) & header , sizeof ( ST_APCI ) ) ;
if ( iRet = = ErrException )
if ( iRet = = ErrException )
{
{
vPrtLogMsg ( LOG_ERROR , iRet , " send mesg failed, sockid:%d msg:%s " , g_IecCtrl . sockid , strerror ( errno ) ) ;
vPrtLogMsg ( LOG_ERROR , iRet , " send mesg failed, sockid:%d msg:%s will close socket " , g_IecCtrl . sockid , strerror ( errno ) ) ;
g_Tcp . clear_tcp_buffer ( g_IecCtrl . sockid , MAX_SBUFF_TCP ) ;
g_Tcp . clear_tcp_buffer ( g_IecCtrl . sockid , MAX_SBUFF_TCP ) ;
g_Tcp . tcpCloseSocket ( g_IecCtrl . sockid ) ;
g_Tcp . tcpCloseSocket ( g_IecCtrl . sockid ) ;
g_IecCtrl . isConnect = false ;
g_IecCtrl . isConnect = false ;
@ -940,7 +951,7 @@ int SendMsgFormatS(unsigned short sendno)
if ( ( iRet = g_Tcp . tcpSendBuffer ( g_IecCtrl . sockid , ( const char * ) & header , sizeof ( ST_APCI ) ) ) < 0 )
if ( ( iRet = g_Tcp . tcpSendBuffer ( g_IecCtrl . sockid , ( const char * ) & header , sizeof ( ST_APCI ) ) ) < 0 )
{
{
vPrtLogMsg ( LOG_WARNG , iRet , " send Format S failed with error: %s, errno=%d " , strerror ( errno ) , errno ) ;
vPrtLogMsg ( LOG_WARNG , iRet , " send Format S failed with error: %s, errno=%d will close socket " , strerror ( errno ) , errno ) ;
g_Tcp . clear_tcp_buffer ( g_IecCtrl . sockid , MAX_SBUFF_TCP ) ;
g_Tcp . clear_tcp_buffer ( g_IecCtrl . sockid , MAX_SBUFF_TCP ) ;
g_Tcp . tcpCloseSocket ( g_IecCtrl . sockid ) ;
g_Tcp . tcpCloseSocket ( g_IecCtrl . sockid ) ;
g_IecCtrl . isConnect = false ;
g_IecCtrl . isConnect = false ;
@ -972,7 +983,7 @@ int SendMsgFormatI(unsigned char *msgbuf, unsigned int len)
memcpy ( buf + sizeof ( ST_APCI ) , msgbuf , len ) ;
memcpy ( buf + sizeof ( ST_APCI ) , msgbuf , len ) ;
if ( ( iRet = g_Tcp . tcpSendBuffer ( g_IecCtrl . sockid , ( const char * ) buf , len + sizeof ( ST_APCI ) ) ) < 0 )
if ( ( iRet = g_Tcp . tcpSendBuffer ( g_IecCtrl . sockid , ( const char * ) buf , len + sizeof ( ST_APCI ) ) ) < 0 )
{
{
vPrtLogMsg ( LOG_WARNG , iRet , " send Format I failed with error: %s, errno=%d " , strerror ( errno ) , errno ) ;
vPrtLogMsg ( LOG_WARNG , iRet , " send Format I failed with error: %s, errno=%d will close socket " , strerror ( errno ) , errno ) ;
g_Tcp . clear_tcp_buffer ( g_IecCtrl . sockid , MAX_SBUFF_TCP ) ;
g_Tcp . clear_tcp_buffer ( g_IecCtrl . sockid , MAX_SBUFF_TCP ) ;
g_Tcp . tcpCloseSocket ( g_IecCtrl . sockid ) ;
g_Tcp . tcpCloseSocket ( g_IecCtrl . sockid ) ;
g_IecCtrl . isConnect = false ;
g_IecCtrl . isConnect = false ;
@ -1029,6 +1040,7 @@ void* thread_listen_proc(void * arg)
accept_fd = g_Tcp . tcpAcceptSocket ( server_fd ) ;
accept_fd = g_Tcp . tcpAcceptSocket ( server_fd ) ;
if ( accept_fd < 0 )
if ( accept_fd < 0 )
{
{
/*
mutex_lock ( g_IecCtrl_mutex ) ;
mutex_lock ( g_IecCtrl_mutex ) ;
g_Tcp . tcpCloseSocket ( g_IecCtrl . sockid ) ;
g_Tcp . tcpCloseSocket ( g_IecCtrl . sockid ) ;
g_IecCtrl . last_time = 0 ;
g_IecCtrl . last_time = 0 ;
@ -1036,10 +1048,18 @@ void* thread_listen_proc(void * arg)
g_IecCtrl . isConnect = false ;
g_IecCtrl . isConnect = false ;
mutex_unlock ( g_IecCtrl_mutex ) ;
mutex_unlock ( g_IecCtrl_mutex ) ;
_SLEEP ( 1000 ) ;
_SLEEP ( 1000 ) ;
*/
// vPrtLogMsg(LOG_DEBUG, errno,"slave link waitting connect...accept_fd=%d", accept_fd);
// vPrtLogMsg(LOG_DEBUG, errno,"slave link waitting connect...accept_fd=%d", accept_fd);
continue ;
continue ;
}
}
mutex_lock ( g_IecCtrl_mutex ) ;
mutex_lock ( g_IecCtrl_mutex ) ;
if ( g_Tcp . tcpIsConnected ( g_IecCtrl . sockid ) )
{
mutex_unlock ( g_IecCtrl_mutex ) ;
vPrtLogMsg ( LOG_DEBUG , errno , " Previous link is connected, won't accept accept_fd=%d " , accept_fd ) ;
_SLEEP ( 1000 ) ;
continue ;
}
g_IecCtrl . last_time = time ( NULL ) ;
g_IecCtrl . last_time = time ( NULL ) ;
g_IecCtrl . last_yx_time = time ( NULL ) ;
g_IecCtrl . last_yx_time = time ( NULL ) ;
g_IecCtrl . last_yc_time = time ( NULL ) ;
g_IecCtrl . last_yc_time = time ( NULL ) ;
@ -1246,8 +1266,8 @@ void* thread_recv_proc(void * arg)
break ;
break ;
}
}
else if ( recv_buflen < = 0 ) {
else if ( recv_buflen < = 0 ) {
vPrtLogMsg ( LOG_WARNG , RET_OK , " socket link recv data length: %d,sockid=%d,errno=%d " , recv_buflen , g_IecCtrl . sockid , errno ) ;
vPrtLogMsg ( LOG_WARNG , RET_OK , " socket link recv data length: %d,sockid=%d,errno=%d , sleep and continue " , recv_buflen , g_IecCtrl . sockid , errno ) ;
//_SLEEP(g_TConfig.getTimeout1());
_SLEEP ( 1000 ) ;
continue ;
continue ;
}
}
@ -1458,12 +1478,14 @@ bool bSetPointTableValueYC(const std::vector<IEC_OBJVAL_NEW>& values)
{
{
sql = BuildSqlForDeviceData ( itCachedDev - > second ) ;
sql = BuildSqlForDeviceData ( itCachedDev - > second ) ;
vPrtLogMsg( LOG_DEBUG , RET_OK , " Insert busi data %s " , sql . c_str ( ) ) ;
ResetCachedDeviceData( itCachedDev - > second ) ;
mutex_lock ( g_list_busi_data_mutex ) ;
mutex_lock ( g_list_busi_data_mutex ) ;
itBusiData = g_list_busi_data . insert ( g_list_busi_data . end ( ) , std : : string ( ) ) ;
itBusiData = g_list_busi_data . insert ( g_list_busi_data . end ( ) , std : : string ( ) ) ;
itBusiData - > swap ( sql ) ;
itBusiData - > swap ( sql ) ;
mutex_unlock ( g_list_busi_data_mutex ) ;
mutex_unlock ( g_list_busi_data_mutex ) ;
vPrtLogMsg ( LOG_DEBUG , 0 , " Insert Busi Data into Table %s, sensor_id=%u " , ( const char * ) itCachedDev - > second . device - > tableName , itCachedDev - > second . device - > sensor_id ) ;
}
}
}
}
}
}
@ -1775,17 +1797,17 @@ int DecodeMsgFormatI(unsigned char *msgbuf, unsigned int len, unsigned short sen
{
{
if ( g_TConfig . shouldParseBusiData ( ) ! = 0 )
if ( g_TConfig . shouldParseBusiData ( ) ! = 0 )
{
{
Reset CachedDeviceData( ) ;
Reset All CachedDeviceData( ) ;
}
}
vPrtLogMsg ( LOG_DEBUG , RET_OK , " Recv Activation Confirmation, %d. " , msgbuf [ 9 ] - 20 ) ;
vPrtLogMsg ( LOG_DEBUG , RET_OK , " Recv Activation Confirmation, %d. " , msgbuf [ 9 ] - 20 ) ;
}
}
else if ( cause = = 10 ) // 激活结束
else if ( cause = = 10 ) // 激活结束
{
{
// SendMsgFormatS(sendno); // 总召唤结束发送S帧确认
if ( g_TConfig . shouldParseBusiData ( ) ! = 0 )
if ( g_TConfig . shouldParseBusiData ( ) ! = 0 )
{
{
Reset CachedDeviceData( ) ;
Reset All CachedDeviceData( ) ;
}
}
//SendMsgFormatS(sendno); // 总召唤结束发送S帧确认
vPrtLogMsg ( LOG_DEBUG , RET_OK , " Recv Activation Termination, %d " , msgbuf [ 9 ] - 20 ) ;
vPrtLogMsg ( LOG_DEBUG , RET_OK , " Recv Activation Termination, %d " , msgbuf [ 9 ] - 20 ) ;
}
}
else
else
@ -1828,7 +1850,7 @@ int SendMsgFormatIAction(unsigned char cmd)
iRet = g_Tcp . tcpSendBuffer ( g_IecCtrl . sockid , ( const char * ) & stPack , stPack . apci . len + 2 ) ;
iRet = g_Tcp . tcpSendBuffer ( g_IecCtrl . sockid , ( const char * ) & stPack , stPack . apci . len + 2 ) ;
if ( iRet = = ErrException )
if ( iRet = = ErrException )
{
{
vPrtLogMsg ( LOG_ERROR , iRet , " send mesg failed, sockid:%d msg:%s " , g_IecCtrl . sockid , strerror ( errno ) ) ;
vPrtLogMsg ( LOG_ERROR , iRet , " send mesg failed, sockid:%d msg:%s will close socket " , g_IecCtrl . sockid , strerror ( errno ) ) ;
g_Tcp . clear_tcp_buffer ( g_IecCtrl . sockid , MAX_SBUFF_TCP ) ;
g_Tcp . clear_tcp_buffer ( g_IecCtrl . sockid , MAX_SBUFF_TCP ) ;
g_Tcp . tcpCloseSocket ( g_IecCtrl . sockid ) ;
g_Tcp . tcpCloseSocket ( g_IecCtrl . sockid ) ;
g_IecCtrl . isConnect = false ;
g_IecCtrl . isConnect = false ;
@ -1852,6 +1874,7 @@ void DecodeMsgFormatU(ST_APCI *p)
SendMsgFormatU ( CMD_TESTC ) ;
SendMsgFormatU ( CMD_TESTC ) ;
break ;
break ;
case 0x08 : // 启动确认位:=1
case 0x08 : // 启动确认位:=1
vPrtLogMsg ( LOG_DEBUG , RET_OK , " Send Activation " ) ;
SendMsgFormatIAction ( CMD_CTL_64H ) ; // 发送总召激活
SendMsgFormatIAction ( CMD_CTL_64H ) ; // 发送总召激活
break ;
break ;
default :
default :
@ -2111,13 +2134,12 @@ void * thread_origin_proc_new(void * arg)
pdbHandle - > dbAutoCommit ( pMySql , true ) ;
pdbHandle - > dbAutoCommit ( pMySql , true ) ;
time_t now = time ( NULL ) ;
time_t now = time ( NULL ) ;
if ( now - g_IecCtrl . last_origin_time > = g_iec_conf . save_internal) // 超过设定保存时间间隔时
if ( now - g_IecCtrl . last_origin_time > = g_iec_conf . orgin_data_ save_internal) // 超过设定保存时间间隔时
{
{
g_IecCtrl . last_origin_time = now ;
g_IecCtrl . last_origin_time = now ;
memset ( szSql , 0x00 , sizeof ( szSql ) ) ;
memset ( szSql , 0x00 , sizeof ( szSql ) ) ;
vTranHostTimeFmt ( now , szWaringTime ) ;
vTranHostTimeFmt ( ( now - g_iec_conf . orgin_data_save_internal ) , szWaringTime ) ;
snprintf ( szSql , sizeof ( szSql ) , " DELETE FROM iec_origin_data_new WHERE d_time<'%s' " ,
snprintf ( szSql , sizeof ( szSql ) , " DELETE FROM niec_origin_data_new WHERE d_time<'%s' " , szWaringTime ) ;
szWaringTime ) ;
pdbHandle - > AddInsertRecord ( pMySql , szSql ) ;
pdbHandle - > AddInsertRecord ( pMySql , szSql ) ;
}
}
@ -3633,10 +3655,11 @@ void *thread_Timer_proc(void *arg)
{
{
if ( g_IecCtrl . TxCounter - g_IecCtrl . LastAckTx > 6 )
if ( g_IecCtrl . TxCounter - g_IecCtrl . LastAckTx > 6 )
{
{
vPrtLogMsg ( LOG_WARNG , 0 , " Wait for U RESP timeout: TxCounter=%d LastAckTx=%d > 6, will close socket " , g_IecCtrl . TxCounter , g_IecCtrl . LastAckTx ) ;
g_Tcp . tcpCloseSocket ( g_IecCtrl . sockid ) ;
g_Tcp . tcpCloseSocket ( g_IecCtrl . sockid ) ;
g_IecCtrl . isConnect = false ;
g_IecCtrl . isConnect = false ;
vPrtLogMsg ( LOG_DEBUG , 0 , " Wait for U RESP timeout: TxCounter=%d LastAckTx=%d > 6, close the socket " , g_IecCtrl . TxCounter , g_IecCtrl . LastAckTx ) ;
}
}
// g_IecCtrl.timer_Confirmflag = true;
// g_IecCtrl.timer_Confirmflag = true;
}
}