diff --git a/das-dn/cmg/ry.cpp b/das-dn/cmg/ry.cpp index 4cc96be6..5efbec3f 100644 --- a/das-dn/cmg/ry.cpp +++ b/das-dn/cmg/ry.cpp @@ -450,10 +450,11 @@ BOOLEAN CRYDevice::GetUnitYCIsForceArchive(int uid, int order) const int udb; struUnit* pUnit; struUnitYC* pYC; - if (uid < 0 || uid >= UNIT_NUM) return 0; + if (uid < 0 || uid >= UNIT_NUM) return FALSE; pUnit = &config.units[uid]; - if ((pUnit->state & 0x01) != TRUE) return 0; - if (order < 0 || order >= pUnit->yccount) return 0; + if ((pUnit->state & 0x01) != TRUE) return FALSE; + if ((pUnit->state & 0x20) == 0x20) return TRUE; + if (order < 0 || order >= pUnit->yccount) return FALSE; pYC = &pUnit->ycs[order]; udb = pYC->order; if (udb < 0 || udb >= DATABASE_YC_NUM) @@ -468,10 +469,11 @@ BOOLEAN CRYDevice::GetUnitYXIsForceArchive(int uid, int order) const int udb; struUnit* pUnit; struUnitYX* pYX; - if (uid < 0 || uid >= UNIT_NUM) return 0; + if (uid < 0 || uid >= UNIT_NUM) return FALSE; pUnit = &config.units[uid]; - if ((pUnit->state & 0x01) != TRUE) return 0; - if (order < 0 || order >= pUnit->yxcount) return 0; + if ((pUnit->state & 0x01) != TRUE) return FALSE; + if ((pUnit->state & 0x20) == 0x20) return TRUE; + if (order < 0 || order >= pUnit->yxcount) return FALSE; pYX = &pUnit->yxs[order]; udb = pYX->order; if (udb < 0 || udb >= DATABASE_YX_NUM) diff --git a/das-dn/hostadsbf/hostadsbf.cpp b/das-dn/hostadsbf/hostadsbf.cpp index 71ca2ff1..9c86cf01 100644 --- a/das-dn/hostadsbf/hostadsbf.cpp +++ b/das-dn/hostadsbf/hostadsbf.cpp @@ -382,7 +382,7 @@ static void* ryftp_process(void* param) char* escaped_string = escape_char_in_string(mbt->m_remotePath, ' '); if (escaped_string == NULL) { - vLog(LOG_DEBUG, "路径转义错误!\n"); + vLog(LOG_DEBUG, "%s 路径转义错误!\n", processName); return ((void*)0); } snprintf(remotePath, sizeof(remotePath), "%s", escaped_string); @@ -488,7 +488,7 @@ static void* ryftp_process(void* param) vLog(LOG_DEBUG, "%s get %s to local memory, with name: %s, and password: %s okay, and file length is: %d.\n", processName, remote, user, password, chunk.size); ftpget_retry_count = 0; if (chunk.size <= (int)(sizeof(struRYDeviceData) << 5)) { - vLog(LOG_DEBUG, "PLC文件生成未完成!\n"); + vLog(LOG_DEBUG, "%s PLC文件生成未完成!\n", processName); continue; } @@ -784,6 +784,7 @@ BOOLEAN CHostADSBFProcess::calc(void) void CHostADSBFProcess::tryStartup() { m_turbine = NULL; + pthread_setname_np(pthread_self(), m_pidName.c_str()); while (m_threadRun) { if (m_turbine == NULL) { SetLocalAmsNetId(AmsNetId(m_localNetId)); @@ -836,11 +837,14 @@ BOOLEAN CHostADSBFProcess::OnPreCreate(int id) m_bHaveFTP = m_nOptions.bHaveFTP; calc(); - 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()); + //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; + SetLocalAmsNetId(AmsNetId(m_localNetId)); +#if 0 m_threadRun = TRUE; m_startup = std::thread(&CHostADSBFProcess::tryStartup, this); +#endif if (m_bHaveFTP) { snprintf(m_user, sizeof(m_user), "%s", m_nOptions.ftp.user); @@ -877,7 +881,7 @@ BOOLEAN CHostADSBFProcess::OnPreCreate(int id) pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); if (pthread_create(&m_pid, &attr, ryftp_process, this) < 0) { - vLog(LOG_ERROR, "create ryftp_process error(%d,%s).\n", errno, strerror(errno)); + vLog(LOG_ERROR, "ads create ryftp_process error(%d,%s).\n", errno, strerror(errno)); return TRUE; } char name[17]; @@ -910,9 +914,22 @@ BOOLEAN CHostADSBFProcess::OnTimer(void) } if (sec_changed) { - if (m_turbine == NULL) return TRUE; + if (m_turbine == NULL) { + m_turbine = new AdsDevice{m_remoteIp, AmsNetId(m_remoteNetId), m_remotePort}; + const auto state = m_turbine->GetState(); + //vLog(LOG_DEBUG, "%s ADS state: %d devState: %d\n", m_pidName.c_str(), (uint16_t)state.ads, (uint16_t)state.device); + if ((uint16_t)state.ads >= ADSSTATE::ADSSTATE_MAXSTATES || (uint16_t)state.device >= ADSSTATE::ADSSTATE_MAXSTATES) + { + vLog(LOG_DEBUG, "read device state error.\n"); + 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); + delete m_turbine; + m_turbine = NULL; + } + return TRUE; + } if (!m_turbine->IsConnected()) return TRUE; - try { + //try { if (m_bHaveFTP) { //启动时读取一次,后面自己维护序号 if ((m_currentDirNo == -1) && (m_currentFileNo == -1)) @@ -921,11 +938,11 @@ BOOLEAN CHostADSBFProcess::OnTimer(void) } } readRealData(); - } catch (const AdsException& ex) { - vLog(LOG_ERROR, "%s AdsException message: %s, remote is: %s:%d\n", m_pidName.c_str(), ex.what(), m_remoteIp.c_str(), m_remotePort); - } catch (const std::runtime_error& ex) { - vLog(LOG_ERROR, "%s %s\n", m_pidName.c_str(), ex.what()); - } +// } catch (const AdsException& ex) { +// vLog(LOG_ERROR, "%s AdsException message: %s, remote is: %s:%d\n", m_pidName.c_str(), ex.what(), m_remoteIp.c_str(), m_remotePort); +// } catch (const std::runtime_error& ex) { +// vLog(LOG_ERROR, "%s %s\n", m_pidName.c_str(), ex.what()); +// } } return TRUE; } @@ -981,7 +998,7 @@ BOOLEAN CHostADSBFProcess::readFileID() //判断是否有要读取的文件 if (m_curStartDirNo <= m_lastReadDirNo && m_curStartFileNo <= m_lastReadFileNo) { - vLog(LOG_DEBUG, "不存在未读的文件。\n"); + vLog(LOG_DEBUG, "%s 不存在未读的文件。\n", m_pidName.c_str()); m_bHaveUnReadFile = FALSE; } else diff --git a/das-dn/hostmodbustcpbf/host_modbus_tcp_bf.cpp b/das-dn/hostmodbustcpbf/host_modbus_tcp_bf.cpp index 659a24cf..a0454f3c 100644 --- a/das-dn/hostmodbustcpbf/host_modbus_tcp_bf.cpp +++ b/das-dn/hostmodbustcpbf/host_modbus_tcp_bf.cpp @@ -416,12 +416,12 @@ static void* ryftp_process(void* param) //char pathName[128] = "./"; char processName[128]; - snprintf(processName, sizeof(processName), "%s", mbt->GetCurProcessName()); + snprintf(processName, sizeof(processName), "%s", mbt->m_pidName.c_str()); //配置远方路径 char* escaped_string = escape_char_in_string(mbt->m_remotePath, ' '); if (escaped_string == NULL) { - vLog(LOG_DEBUG, "路径转义错误!\n"); + vLog(LOG_DEBUG, "%s 路径转义错误!\n", processName); return ((void*)0); } snprintf(remotePath, sizeof(remotePath), "%s", escaped_string); @@ -527,7 +527,7 @@ static void* ryftp_process(void* param) vLog(LOG_DEBUG, "%s get %s to local memory, with name: %s, and password: %s okay, and file length is: %d.\n", processName, remote, user, password, chunk.size); ftpget_retry_count = 0; if (chunk.size <= (int)(sizeof(struRYDeviceData) << 5)) { - vLog(LOG_DEBUG, "PLC文件生成未完成!\n"); + vLog(LOG_DEBUG, "%s PLC文件生成未完成!\n", processName); continue; } @@ -1115,6 +1115,8 @@ BOOLEAN CHostModbusTcpBFProcess::OnPreCreate(int id) return FALSE; } + m_pidName = std::string(GetCurProcessName()); + m_bHaveFTP = m_nOptions.bHaveFTP; m_nTimeout = 200; calc2(); @@ -1146,7 +1148,7 @@ BOOLEAN CHostModbusTcpBFProcess::OnPreCreate(int id) //启动后,创建ftp线程 if (m_pid <= 0) { m_bFtpRun = TRUE; - vLog(LOG_DEBUG, "create a ftp thread.\n"); + vLog(LOG_DEBUG, "rymodbus create a ftp thread.\n"); pthread_attr_t attr; pthread_attr_init(&attr); @@ -1154,11 +1156,11 @@ BOOLEAN CHostModbusTcpBFProcess::OnPreCreate(int id) pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); if (pthread_create(&m_pid, &attr, ryftp_process, this) < 0) { - vLog(LOG_ERROR, "create ryftp_process error(%d,%s).\n", errno, strerror(errno)); + vLog(LOG_ERROR, "rymodbus create ryftp_process error(%d,%s).\n", errno, strerror(errno)); return TRUE; } char name[17]; - snprintf(name, 16, "%s_ftp", GetCurProcessName()); + snprintf(name, 16, "%s_ftp", m_pidName.c_str()); pthread_setname_np(m_pid, name); pthread_attr_destroy(&attr); @@ -1754,7 +1756,7 @@ BOOLEAN CHostModbusTcpBFProcess::OnReceiveIDData(CHostModbusTcpBFProcessItem *pI m_iv = iv; if (iv) { - vLog(LOG_DEBUG, "路径信息无效\n"); + vLog(LOG_DEBUG, "%s 路径信息无效\n", m_pidName.c_str()); return FALSE; } //当前文件夹路径名 @@ -1770,7 +1772,7 @@ BOOLEAN CHostModbusTcpBFProcess::OnReceiveIDData(CHostModbusTcpBFProcessItem *pI //判断是否有要读取的文件 if (m_curStartDirNo <= m_lastReadDirNo && m_curStartFileNo <= m_lastReadFileNo) { - vLog(LOG_DEBUG, "不存在未读的文件。\n"); + vLog(LOG_DEBUG, "%s 不存在未读的文件。\n", m_pidName.c_str()); m_bHaveUnReadFile = FALSE; } else @@ -1778,10 +1780,10 @@ BOOLEAN CHostModbusTcpBFProcess::OnReceiveIDData(CHostModbusTcpBFProcessItem *pI m_bHaveUnReadFile = TRUE; int dir_count = m_curStartDirNo - m_lastReadDirNo + 1; int file_count = m_curStartFileNo - m_lastReadFileNo; - vLog(LOG_DEBUG, "总共有%d个目录的%d个文件未读取。\n", dir_count, file_count); + vLog(LOG_DEBUG, "%s 总共有%d个目录的%d个文件未读取。\n", m_pidName.c_str(), dir_count, file_count); } /*文件从保存的最后读取的文件目录的文件一直督导当前开始目录*/ - vLog(LOG_DEBUG, "最新文件夹编号: %ld, 最新文件名编号: %ld: 最新文件夹中第一个文件的编号: %ld\n", m_currentDirNo, m_currentFileNo, m_currentDirStartFileNo); + vLog(LOG_DEBUG, "%s 最新文件夹编号: %ld, 最新文件名编号: %ld: 最新文件夹中第一个文件的编号: %ld\n", m_pidName.c_str(), m_currentDirNo, m_currentFileNo, m_currentDirStartFileNo); return TRUE; } diff --git a/das-dn/hostmodbustcpbf/host_modbus_tcp_bf.h b/das-dn/hostmodbustcpbf/host_modbus_tcp_bf.h index 07c86ff0..9aa96834 100644 --- a/das-dn/hostmodbustcpbf/host_modbus_tcp_bf.h +++ b/das-dn/hostmodbustcpbf/host_modbus_tcp_bf.h @@ -63,6 +63,7 @@ private: BOOLEAN publishAnalogData(void); public: + std::string m_pidName; BOOLEAN m_bHaveFTP; //存在FTP协议 //ftp参数信息 diff --git a/das-dn/third_party/AdsLib/AdsDevice.cpp b/das-dn/third_party/AdsLib/AdsDevice.cpp index 4a593e0e..5a336270 100644 --- a/das-dn/third_party/AdsLib/AdsDevice.cpp +++ b/das-dn/third_party/AdsLib/AdsDevice.cpp @@ -144,12 +144,13 @@ long AdsDevice::GetLocalPort() const AdsDeviceState AdsDevice::GetState() const { - uint16_t state[2]; + uint16_t state[2] = {ADSSTATE::ADSSTATE_MAXSTATES, ADSSTATE::ADSSTATE_MAXSTATES}; if(m_Connected == true){ auto error = AdsSyncReadStateReqEx(GetLocalPort(), &m_Addr, &state[0], &state[1]); if (error) { LOG_ERROR("AdsDevice can not read device state."); //throw AdsException(error); + return {static_cast(state[0]), static_cast(state[1])}; } for (auto i : state) { diff --git a/das-dn/third_party/AdsLib/AdsVariable.h b/das-dn/third_party/AdsLib/AdsVariable.h index 0117ed01..ae82b050 100644 --- a/das-dn/third_party/AdsLib/AdsVariable.h +++ b/das-dn/third_party/AdsLib/AdsVariable.h @@ -53,6 +53,7 @@ struct AdsVariable { void Read(const size_t size, void* data) const { + if (!m_Route.IsConnected()) return; uint32_t bytesRead = 0; auto error = m_Route.ReadReqEx2(m_IndexGroup, *m_Handle, @@ -61,16 +62,17 @@ struct AdsVariable { &bytesRead); if (error || (size != bytesRead)) { - LOG_ERROR("AdsVariable read failed: "<