From 009eda749899fc85d3a75ad2016371f27436bf36 Mon Sep 17 00:00:00 2001
From: LAPTOP-T815PCOQ\25526 <mr.liuyang@126.com>
Date: 星期四, 09 一月 2025 14:04:16 +0800
Subject: [PATCH] Merge branch 'liuyang' into chenluhua

---
 SourceCode/Bond/BoounionPLC/Model.cpp |   51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 51 insertions(+), 0 deletions(-)

diff --git a/SourceCode/Bond/BoounionPLC/Model.cpp b/SourceCode/Bond/BoounionPLC/Model.cpp
index 307a16a..d89bf61 100644
--- a/SourceCode/Bond/BoounionPLC/Model.cpp
+++ b/SourceCode/Bond/BoounionPLC/Model.cpp
@@ -23,6 +23,10 @@
 
 CModel::~CModel()
 {
+	for (auto item : m_mapPlc) {
+		delete item.second;
+	}
+	m_mapPlc.clear();
 }
 
 IObservable* CModel::getObservable()
@@ -68,11 +72,19 @@
 	g_pModel = this;
 
 
+	// 模拟从文档或数据库加载PLC列表
+	addPlc("Test1", "127.0.0.1", 1001);
+	addPlc("Test2", "127.0.0.1", 1002);
+
+
 	return 0;
 }
 
 int CModel::term()
 {
+	for (auto item : m_mapPlc) {
+		item.second->term();
+	}
 	CLog::GetLog()->SetOnLogCallback(nullptr);
 	return 0;
 }
@@ -284,5 +296,44 @@
 
 void CModel::onTimer(UINT nTimerid)
 {
+	static int ii = 0;
+	ii++;
 
+
+	// 检测是否断开,重连
+	if (ii % 5 == 0) {
+		for (auto item : m_mapPlc) {
+			if (!item.second->isConnected()) {
+				item.second->connect();
+			}
+		}
+	}
+}
+
+std::map<std::string, CPLC*>& CModel::getPlcMap()
+{
+	return m_mapPlc;
+}
+
+int CModel::addPlc(const char* pszName, const char* pszIp, const unsigned int port)
+{
+	auto iter = m_mapPlc.find(pszName);
+	if (iter != m_mapPlc.end()) return -1;
+	CPLC* pPLC = new CPLC(pszName, pszIp, port);
+	pPLC->init();
+	m_mapPlc[pszName] = pPLC;
+
+	notifyPtr(RX_CODE_ADD_PLC, pPLC);
+	return 0;
+}
+
+int CModel::removePlc(const char* pszName)
+{
+	auto iter = m_mapPlc.find(pszName);
+	if (iter == m_mapPlc.end()) return -1;
+
+	notifyPtr(RX_CODE_REMOVE_PLC, iter->second);
+	delete iter->second;
+	m_mapPlc.erase(iter);
+	return 0;
 }

--
Gitblit v1.9.3