From cc52b06ef2ea9de053e85d33a7b31e71323ec37a Mon Sep 17 00:00:00 2001 From: zhouhuang Date: Wed, 11 Dec 2024 10:30:30 +0800 Subject: [PATCH] update --- das-dn/cmg/main.cpp | 1 + das-dn/cmg/ry.cpp | 79 +++++++++++-------- das-dn/cmg/ry.h | 5 +- das-dn/hostadsbf/hostadsbf.cpp | 13 ++- das-dn/hostadsbf/hostadsbf.h | 1 + das-dn/third_party/AdsLib/AdsDevice.cpp | 14 +++- das-dn/third_party/AdsLib/AdsDevice.h | 1 + das-dn/third_party/AdsLib/AdsVariable.h | 2 + .../AdsLib/Standalone/AmsConnection.cpp | 3 +- 9 files changed, 76 insertions(+), 43 deletions(-) diff --git a/das-dn/cmg/main.cpp b/das-dn/cmg/main.cpp index ae326bfe..b93320dc 100644 --- a/das-dn/cmg/main.cpp +++ b/das-dn/cmg/main.cpp @@ -250,6 +250,7 @@ int main(int argc, char** argv) config.processes[i].option.network.target_addr = INADDR_ANY; config.units[uid].state = TRUE; + #endif unsigned int m_runCount = 0; unsigned int count = 0; diff --git a/das-dn/cmg/ry.cpp b/das-dn/cmg/ry.cpp index 5efbec3f..118df2d0 100644 --- a/das-dn/cmg/ry.cpp +++ b/das-dn/cmg/ry.cpp @@ -2927,6 +2927,7 @@ bool CRYDevice::publishAnalogData(int uid) if (archiveValues.size()) { root["archiveValues"] = archiveValues; } + vLog(LOG_DEBUG, "publish unit<%d> data analogData, and deviceId is: %s\n", uid, static_units[uid].deviceId); return publish_sensor_data("", "analogData", root); } @@ -3027,6 +3028,28 @@ void CRYDevice::releaseAllUnits(void) } } +int CRYDevice::sendMsg(std::string sMessage) +{ + if (NULL != m_ws && m_ws->getReadyState() == easywsclient::WebSocket::OPEN) + { + pthread_mutex_lock(&m_SendQueueMutex); + m_SendQueue.push(sMessage); + pthread_mutex_unlock(&m_SendQueueMutex); + + return (int)sMessage.length(); + } + return 0; +} + +int CRYDevice::recvMsg(std::string sMessage) +{ + pthread_mutex_lock(&m_RecvQueueMutex); + m_recvMsgQueue.push(sMessage); + pthread_mutex_unlock(&m_RecvQueueMutex); + + return (int)sMessage.length(); +} + BOOLEAN CRYDevice::ry_init(const char *host, const int port, const char *nodeId, const char *version) { snprintf(m_host, sizeof(m_host), "%s", host); @@ -3034,7 +3057,8 @@ BOOLEAN CRYDevice::ry_init(const char *host, const int port, const char *nodeId, snprintf(m_version, sizeof(m_version), "%s", version); m_port = port; - status = 2; //0 - 离线, 1 - 在线, 2 - 未配置 + m_nCurUnit = 0; + m_status = 2; //0 - 离线, 1 - 在线, 2 - 未配置 m_dataAcquisitionReload = false; unitname2service_map.clear(); @@ -3075,7 +3099,7 @@ BOOLEAN CRYDevice::ry_init(const char *host, const int port, const char *nodeId, vLog(LOG_WARN, "系统配置了两个相同的设备ID<%s>,请检查。\n", unit_id.c_str()); } } - status = 1; + m_status = 1; #ifndef USE_NOPOLL_WEBSOCKET char url[560]; @@ -3101,28 +3125,6 @@ BOOLEAN CRYDevice::ry_init(const char *host, const int port, const char *nodeId, return TRUE; } -int CRYDevice::sendMsg(std::string sMessage) -{ - if (NULL != m_ws && m_ws->getReadyState() == easywsclient::WebSocket::OPEN) - { - pthread_mutex_lock(&m_SendQueueMutex); - m_SendQueue.push(sMessage); - pthread_mutex_unlock(&m_SendQueueMutex); - - return (int)sMessage.length(); - } - return 0; -} - -int CRYDevice::recvMsg(std::string sMessage) -{ - pthread_mutex_lock(&m_RecvQueueMutex); - m_recvMsgQueue.push(sMessage); - pthread_mutex_unlock(&m_RecvQueueMutex); - - return (int)sMessage.length(); -} - bool CRYDevice::ry_run(void) { #ifndef USE_NOPOLL_WEBSOCKET @@ -3234,19 +3236,32 @@ bool CRYDevice::ry_run(void) } if (sec_changed) { if ((last_sec % 20) == 0) { - heart_beat(status); + heart_beat(m_status); } } publishdeviceEventData(); - for (int i = 0; i < UNIT_NUM; i++) { - if ((config.units[i].state & 0x01) != TRUE) continue; - MakeYKFrame(i); - MakeYTFrame(i); - if (sec_changed) { - publishAnalogData(i); - publishStateData(i); + int i = 0; +#if 0 + for (int i = 0; i < UNIT_NUM; i++) + { +#else + i = m_nCurUnit; + m_nCurUnit++; +#endif + do { + if ((config.units[i].state & 0x01) != TRUE) break; + if (config.units[i].type != MASTER_UNIT) break; + MakeYKFrame(i); + MakeYTFrame(i); + if (sec_changed) { + publishAnalogData(i); + publishStateData(i); + } } + while (0); +#if 0 } +#endif return m_dataAcquisitionReload; } diff --git a/das-dn/cmg/ry.h b/das-dn/cmg/ry.h index df6dced6..354e9380 100644 --- a/das-dn/cmg/ry.h +++ b/das-dn/cmg/ry.h @@ -119,8 +119,9 @@ private: int msg_count = 0; #endif - DWORD last_connect_sec = 0; - int status; + DWORD last_connect_sec = 0; + int m_status; + int m_nCurUnit; bool m_dataAcquisitionReload = false; diff --git a/das-dn/hostadsbf/hostadsbf.cpp b/das-dn/hostadsbf/hostadsbf.cpp index 48b819f8..6dbec090 100644 --- a/das-dn/hostadsbf/hostadsbf.cpp +++ b/das-dn/hostadsbf/hostadsbf.cpp @@ -842,6 +842,7 @@ BOOLEAN CHostADSBFProcess::OnPreCreate(int id) //vLog(LOG_DEBUG, "%s local ip is: %s, netid is: %s, remote ip is: %s, and netid is: %s\n", m_pidName.c_str(), m_localIp.c_str(), m_localNetId.c_str(), m_remoteIp.c_str(), m_remoteNetId.c_str()); m_turbine = NULL; + m_bRouteAdded = FALSE; SetLocalAmsNetId(AmsNetId(m_localNetId)); #if 0 m_threadRun = TRUE; @@ -930,15 +931,19 @@ BOOLEAN CHostADSBFProcess::OnTimer(void) } #else //先添加一条路由 - long ret = AddRemoteRoute(m_remoteIp, m_localNetId, m_localIp, std::string("isoftstone"), std::string("admin"), std::string("admin")); - vLog(LOG_DEBUG, "bbb %s add route return value is: %d\n", m_pidName.c_str(), ret); - if (ret != 0) return TRUE; + if (m_bRouteAdded == FALSE) + { + long ret = AddRemoteRoute(m_remoteIp, m_localNetId, m_localIp, std::string("isoftstone"), std::string("admin"), std::string("admin")); + vLog(LOG_DEBUG, "%s add route return value is: %d\n", m_pidName.c_str(), ret); + if (ret != 0) return TRUE; + } + m_bRouteAdded = TRUE; //添加成功 m_turbine = new AdsDevice{m_remoteIp, AmsNetId(m_remoteNetId), m_remotePort}; const auto state = m_turbine->GetState(); if ((uint16_t)state.ads >= ADSSTATE::ADSSTATE_MAXSTATES || (uint16_t)state.device >= ADSSTATE::ADSSTATE_MAXSTATES) { - vLog(LOG_DEBUG, "read device state error.\n"); + vLog(LOG_DEBUG, "%s read device state error.\n", m_pidName.c_str()); delete m_turbine; m_turbine = NULL; } diff --git a/das-dn/hostadsbf/hostadsbf.h b/das-dn/hostadsbf/hostadsbf.h index 2bf22ed9..ed16c27b 100644 --- a/das-dn/hostadsbf/hostadsbf.h +++ b/das-dn/hostadsbf/hostadsbf.h @@ -45,6 +45,7 @@ private: std::string m_remoteNetId; AdsDevice *m_turbine; + BOOLEAN m_bRouteAdded; //路由是否添加成功 int m_total_length; DWORD last_sec; diff --git a/das-dn/third_party/AdsLib/AdsDevice.cpp b/das-dn/third_party/AdsLib/AdsDevice.cpp index 40fad4a3..4d3c20fe 100644 --- a/das-dn/third_party/AdsLib/AdsDevice.cpp +++ b/das-dn/third_party/AdsLib/AdsDevice.cpp @@ -33,7 +33,7 @@ AdsDevice::AdsDevice(const std::string& ipV4, AmsNetId amsNetId, uint16_t port) m_LocalPort(new long { OpenLocalPort() }, { CloseLocalPort }), m_Connected(false) { - if(*m_NetId.get() == amsNetId) { + if (*m_NetId.get() == amsNetId) { m_Connected = true; } } @@ -47,6 +47,15 @@ AdsDevice::~AdsDevice() } } +void AdsDevice::DisconnectDevice() +{ + if (m_Connected == true) + { + DisconnectTarget(*m_NetId.get()); + m_Connected = false; + } +} + long AdsDevice::DeleteNotificationHandle(uint32_t handle) const { if (handle) { @@ -239,8 +248,7 @@ AdsHandle AdsDevice::OpenFile(const std::string& filename, const uint32_t flags) long AdsDevice::ReadReqEx2(uint32_t group, uint32_t offset, size_t length, void* buffer, uint32_t* bytesRead) const { - if(m_Connected == false) - return ROUTERERR_HOSTDENY; + if (m_Connected == false) return ROUTERERR_HOSTDENY; if (length > std::numeric_limits::max()) { return ADSERR_DEVICE_INVALIDSIZE; diff --git a/das-dn/third_party/AdsLib/AdsDevice.h b/das-dn/third_party/AdsLib/AdsDevice.h index 8aad1bbf..61e31dbe 100644 --- a/das-dn/third_party/AdsLib/AdsDevice.h +++ b/das-dn/third_party/AdsLib/AdsDevice.h @@ -100,6 +100,7 @@ struct AdsDevice { const AmsAddr m_Addr; bool IsConnected() const { return m_Connected; } + void DisconnectDevice(); private: AdsResource m_LocalPort; long CloseFile(uint32_t handle) const; diff --git a/das-dn/third_party/AdsLib/AdsVariable.h b/das-dn/third_party/AdsLib/AdsVariable.h index ae82b050..fbb48d93 100644 --- a/das-dn/third_party/AdsLib/AdsVariable.h +++ b/das-dn/third_party/AdsLib/AdsVariable.h @@ -63,6 +63,7 @@ struct AdsVariable { if (error || (size != bytesRead)) { LOG_ERROR("AdsVariable read failed: "<< std::dec << error); + ((AdsDevice&)m_Route).DisconnectDevice(); //throw AdsException(error); } } @@ -73,6 +74,7 @@ struct AdsVariable { auto error = m_Route.WriteReqEx(m_IndexGroup, *m_Handle, size, data); if (error) { LOG_ERROR("AdsVariable write failed:" << std::dec << error); + ((AdsDevice&)m_Route).DisconnectDevice(); //throw AdsException(error); } } diff --git a/das-dn/third_party/AdsLib/Standalone/AmsConnection.cpp b/das-dn/third_party/AdsLib/Standalone/AmsConnection.cpp index b54e3cb9..31f6c8fb 100644 --- a/das-dn/third_party/AdsLib/Standalone/AmsConnection.cpp +++ b/das-dn/third_party/AdsLib/Standalone/AmsConnection.cpp @@ -153,8 +153,7 @@ AmsResponse* AmsConnection::Write(AmsRequest& request, const AmsAddr srcAddr) long AmsConnection::SendRequest(AmsRequest& request, const uint32_t timeout) { - if(IsConnected() == false) - return -1; + if (IsConnected() == false) return -1; AmsAddr srcAddr; const auto status = router.GetAmsAddr(request.srcPort, &srcAddr);