From b950ea6aaffa088b76528068514f0dfa49b16223 Mon Sep 17 00:00:00 2001 From: BlueMatthew Date: Mon, 20 Nov 2023 09:35:04 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=B0=E5=BD=95=E9=81=A5=E6=B5=8B=E6=95=B0?= =?UTF-8?q?=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 +- etc/ht_iec104.conf | 1 + include/HTHP104Table.h | 3 ++- include/HTMemCacheData.h | 3 ++- src/HTIEC104.cpp | 57 ++++++++++++++++++++++++++++------------ src/HTMemCacheData.cpp | 32 +++++++++++++++------- src/HTTcpSocket.cpp | 13 ++++++--- 7 files changed, 77 insertions(+), 34 deletions(-) diff --git a/.gitignore b/.gitignore index a49b133..6df7d56 100644 --- a/.gitignore +++ b/.gitignore @@ -93,6 +93,6 @@ vsproj/Debug vsproj/Release vsproj/x64 -log/ +log/* run/ bin/ diff --git a/etc/ht_iec104.conf b/etc/ht_iec104.conf index fbdf339..bc014c3 100644 --- a/etc/ht_iec104.conf +++ b/etc/ht_iec104.conf @@ -11,6 +11,7 @@ iec_site_id = 1 [COMM_SAVE_INTERNAL] DATA_SAVE_INTERNAL = 30 +ORIGIN_DATA_SAVE_INTERNAL = 3600 [IEC_OBJS_ADDR] iec_global_addr = 1 diff --git a/include/HTHP104Table.h b/include/HTHP104Table.h index 3082b17..e6bbe98 100644 --- a/include/HTHP104Table.h +++ b/include/HTHP104Table.h @@ -136,7 +136,8 @@ typedef struct _BREAK_EQM_CODE { // IEC104协议基础配置信息 typedef struct _IEC104_CONF { - unsigned int save_internal; // 104时间保存间隔时间(s) + unsigned int save_internal; // 104时间保存间隔时间(s) + unsigned int orgin_data_save_internal; // 104原始数据保存时间(s) unsigned int iec_byq_count; // 变压器数量 unsigned int iec_break_count; // 断路器数量 unsigned char site_id[DEF_EQM_CODE_SIZE]; // 站点ID diff --git a/include/HTMemCacheData.h b/include/HTMemCacheData.h index 734408c..3f1b6de 100644 --- a/include/HTMemCacheData.h +++ b/include/HTMemCacheData.h @@ -105,7 +105,8 @@ unsigned char cGetByqDeviceFaultState(unsigned char *pszEqm_code); void SethDevTimeStat(unsigned char *sys_code, unsigned char ws); time_t GetDevTimeStat(unsigned char *sys_code, unsigned char *ws); -void ResetCachedDeviceData(); +void ResetAllCachedDeviceData(); +void ResetCachedDeviceData(CACHED_DEV_DATA& cachedDev); std::string BuildSqlForDeviceData(CACHED_DEV_DATA& cachedDev); bool AssignValueToDeviceData(CACHED_DEV_DATA& cachedDev, const IEC_OBJVAL_NEW& val); diff --git a/src/HTIEC104.cpp b/src/HTIEC104.cpp index 519f4c3..c3f7216 100644 --- a/src/HTIEC104.cpp +++ b/src/HTIEC104.cpp @@ -184,6 +184,8 @@ static bool iGetIEC104Conf() char szTmp[512], szSection[128]; memset(&g_iec_conf, 0x00, sizeof(ST_IEC104_CONF)); // 初始化IEC104配置. + g_iec_conf.orgin_data_save_internal = 3600; + // 打开配置文件,准备加载配置参数 if (fpIec == 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); + // 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)); 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)); 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.tcpCloseSocket(g_IecCtrl.sockid); 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) { - 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.tcpCloseSocket(g_IecCtrl.sockid); g_IecCtrl.isConnect = false; @@ -972,7 +983,7 @@ int SendMsgFormatI(unsigned char *msgbuf, unsigned int len) memcpy(buf + sizeof(ST_APCI), msgbuf, len); 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.tcpCloseSocket(g_IecCtrl.sockid); g_IecCtrl.isConnect = false; @@ -1029,6 +1040,7 @@ void* thread_listen_proc(void * arg) accept_fd = g_Tcp.tcpAcceptSocket(server_fd); if (accept_fd < 0) { + /* mutex_lock(g_IecCtrl_mutex); g_Tcp.tcpCloseSocket(g_IecCtrl.sockid); g_IecCtrl.last_time = 0; @@ -1036,10 +1048,18 @@ void* thread_listen_proc(void * arg) g_IecCtrl.isConnect = false; mutex_unlock(g_IecCtrl_mutex); _SLEEP(1000); + */ // vPrtLogMsg(LOG_DEBUG, errno,"slave link waitting connect...accept_fd=%d", accept_fd); continue; } 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_yx_time = time(NULL); g_IecCtrl.last_yc_time = time(NULL); @@ -1236,7 +1256,7 @@ void* thread_recv_proc(void * arg) pkgLen = 0; recv_buflen = g_Tcp.tcpRecvBuffer(g_IecCtrl.sockid, (char*)szBuf, MAX_SBUFF_TCP, 5); // t1 = 15s if (recv_buflen == ErrException || (recv_buflen == 0 && errno == 2)) { - vPrtLogMsg(LOG_WARNG, errno, "socket link exceptiond, do close sockid=%d,thread_recv_proc pthread_exit.", g_IecCtrl.sockid); + vPrtLogMsg(LOG_WARNG, errno, "socket link exceptiond, do close sockid=%d, thread_recv_proc pthread_exit.", g_IecCtrl.sockid); g_Tcp.clear_tcp_buffer(g_IecCtrl.sockid, MAX_SBUFF_TCP); mutex_lock(g_IecCtrl_mutex); @@ -1246,8 +1266,8 @@ void* thread_recv_proc(void * arg) break; } 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); - //_SLEEP(g_TConfig.getTimeout1()); + 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(1000); continue; } @@ -1457,13 +1477,15 @@ bool bSetPointTableValueYC(const std::vector& values) if (itCachedDev->second.assignedFields == itCachedDev->second.fields.size()) { 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); itBusiData = g_list_busi_data.insert(g_list_busi_data.end(), std::string()); itBusiData->swap(sql); 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) { - ResetCachedDeviceData(); + ResetAllCachedDeviceData(); } vPrtLogMsg(LOG_DEBUG, RET_OK, "Recv Activation Confirmation, %d.", msgbuf[9] - 20); } else if (cause == 10) // 激活结束 { + // SendMsgFormatS(sendno); // 总召唤结束发送S帧确认 if (g_TConfig.shouldParseBusiData() != 0) { - ResetCachedDeviceData(); + ResetAllCachedDeviceData(); } - //SendMsgFormatS(sendno); // 总召唤结束发送S帧确认 vPrtLogMsg(LOG_DEBUG, RET_OK, "Recv Activation Termination, %d", msgbuf[9] - 20); } else @@ -1828,7 +1850,7 @@ int SendMsgFormatIAction(unsigned char cmd) iRet = g_Tcp.tcpSendBuffer(g_IecCtrl.sockid, (const char*)&stPack, stPack.apci.len + 2); 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.tcpCloseSocket(g_IecCtrl.sockid); g_IecCtrl.isConnect = false; @@ -1852,6 +1874,7 @@ void DecodeMsgFormatU(ST_APCI *p) SendMsgFormatU(CMD_TESTC); break; case 0x08: // 启动确认位:=1 + vPrtLogMsg(LOG_DEBUG, RET_OK, "Send Activation"); SendMsgFormatIAction(CMD_CTL_64H); // 发送总召激活 break; default: @@ -2111,13 +2134,12 @@ void * thread_origin_proc_new(void * arg) pdbHandle->dbAutoCommit(pMySql, true); 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; memset(szSql, 0x00, sizeof(szSql)); - vTranHostTimeFmt(now, szWaringTime); - snprintf(szSql, sizeof(szSql), "DELETE FROM iec_origin_data_new WHERE d_time<'%s' ", - szWaringTime); + vTranHostTimeFmt((now - g_iec_conf.orgin_data_save_internal), szWaringTime); + snprintf(szSql, sizeof(szSql), "DELETE FROM niec_origin_data_new WHERE d_time<'%s' ", szWaringTime); pdbHandle->AddInsertRecord(pMySql, szSql); } @@ -3633,10 +3655,11 @@ void *thread_Timer_proc(void *arg) { 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_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; } diff --git a/src/HTMemCacheData.cpp b/src/HTMemCacheData.cpp index c27a4d5..8f64233 100644 --- a/src/HTMemCacheData.cpp +++ b/src/HTMemCacheData.cpp @@ -482,7 +482,7 @@ static void vLoadIECPointTableNew() pdbHandle->FreeRecord(res); } -void ResetCachedDeviceData() +void ResetAllCachedDeviceData() { map::iterator it; std::map >::iterator itField; @@ -490,19 +490,31 @@ void ResetCachedDeviceData() mutex_lock(g_map_iec_mutex_new); for (it = g_map_dev_data.begin(); it != g_map_dev_data.end(); ++it) { - it->second.firstTs = 0; - it->second.assignedFields = 0; - - for (itField = it->second.fields.begin(); itField != it->second.fields.end(); ++itField) - { - itField->second.first.ts = 0; - itField->second.first.fval = 0.0; - itField->second.second = false; - } + ResetCachedDeviceData(it->second); } mutex_unlock(g_map_iec_mutex_new); } +void ResetCachedDeviceData(CACHED_DEV_DATA& cachedDev) +{ + if (cachedDev.assignedFields == 0) + { + return; + } + + cachedDev.firstTs = 0; + + std::map >::iterator itField; + for (itField = cachedDev.fields.begin(); itField != cachedDev.fields.end(); ++itField) + { + itField->second.first.ts = 0; + itField->second.first.fval = 0.0; + itField->second.second = false; + } + + cachedDev.assignedFields = 0; +} + std::string BuildSqlForDeviceData(CACHED_DEV_DATA& cachedDev) { std::string fields, fieldValues, sql; diff --git a/src/HTTcpSocket.cpp b/src/HTTcpSocket.cpp index 6a455f7..8bdf5a0 100644 --- a/src/HTTcpSocket.cpp +++ b/src/HTTcpSocket.cpp @@ -56,13 +56,14 @@ int TTcpSocket::tcpGetSockID() int TTcpSocket::tcpCloseSocket(int sockfd) { if(sockfd > 0) { + vPrtLogMsg(LOG_WARNG, RET_FAIL, "_close sockid=%d ", sockfd); #ifdef _WIN32 //if(_close(sockfd) == -1) if(shutdown(sockfd, 2) == -1) #else if(close(sockfd) == -1) #endif -// vPrtLogMsg(LOG_WARNG, RET_FAIL, "_close sockid=%d return:%d:%s",sockfd, errno, strerror(errno)); + vPrtLogMsg(LOG_WARNG, RET_FAIL, "_close sockid=%d return:%d:%s",sockfd, errno, strerror(errno)); return 1; } return 0 ; @@ -378,10 +379,14 @@ int TTcpSocket::tcpRecvBuffer(int sockid, char* pBuffer,int nMaxLength, int time FD_SET((unsigned int)sockid, &fd_Read) ; FD_SET((unsigned int)sockid, &fd_Except) ; isel= select (sockid + 1 , &fd_Read, NULL, &fd_Except, &st_TimeOut) ; - - if(isel <= 0 || isel == EINTR) + if (isel == 0) + { + vPrtLogMsg(LOG_DEBUG, errno, "select socket timeout,isel=%d,timeout=%d continue", isel, timeout); + continue; + } + if(isel < 0 || isel == EINTR) { - // vPrtLogMsg(LOG_DEBUG,errno, "select socket timeout,isel=%d",isel); + vPrtLogMsg(LOG_DEBUG,errno, "select socket timeout,isel=%d,timeout=%d",isel, timeout); break ; } else if( FD_ISSET(sockid, &fd_Except) )