From 2a6b8de3ee1ed419bbe54213dcb8428d2c436a69 Mon Sep 17 00:00:00 2001
From: LAPTOP-SNT8I5JK\Boounion <Chenluhua@qq.com>
Date: 星期一, 13 一月 2025 08:56:24 +0800
Subject: [PATCH] 1.PLC调机程序,警告和其它完善;

---
 SourceCode/Bond/BoounionPLC/Model.cpp |   75 +++++++++++++++++++++++++++++++++++--
 1 files changed, 71 insertions(+), 4 deletions(-)

diff --git a/SourceCode/Bond/BoounionPLC/Model.cpp b/SourceCode/Bond/BoounionPLC/Model.cpp
index d89bf61..59587a7 100644
--- a/SourceCode/Bond/BoounionPLC/Model.cpp
+++ b/SourceCode/Bond/BoounionPLC/Model.cpp
@@ -2,6 +2,9 @@
 #include "Model.h"
 #include "Log.h"
 #include "Common.h"
+#include "ToolUnits.h"
+
+// 常量
 
 
 CModel* g_pModel = NULL;
@@ -72,10 +75,12 @@
 	g_pModel = this;
 
 
-	// 模拟从文档或数据库加载PLC列表
-	addPlc("Test1", "127.0.0.1", 1001);
-	addPlc("Test2", "127.0.0.1", 1002);
-
+	// 获取所有PLC信息
+	std::vector<PlcInfo> plcList;
+	m_configuration.getAllPLCInfo(plcList);
+	for (const auto& plc : plcList) {
+		addPlc(plc.strName, plc.strIp, plc.nPort);
+	}
 
 	return 0;
 }
@@ -308,6 +313,13 @@
 			}
 		}
 	}
+
+
+	// 读取产能信息
+	CPLC* pPlc = getCurrentPlc();
+	if (pPlc != nullptr) {
+		pPlc->readPLCDataRegularly();
+	}
 }
 
 std::map<std::string, CPLC*>& CModel::getPlcMap()
@@ -319,9 +331,39 @@
 {
 	auto iter = m_mapPlc.find(pszName);
 	if (iter != m_mapPlc.end()) return -1;
+	CString strPlcDir;
+	strPlcDir.Format(_T("%s\\Plcs\\%s"), (LPTSTR)(LPCTSTR)m_strWorkDir, pszName);
 	CPLC* pPLC = new CPLC(pszName, pszIp, port);
+	pPLC->setWorkDir((LPTSTR)(LPCTSTR)strPlcDir);
+	PLCListener listener;
+	listener.onStateChanged = [&](void* pFrom, int state) -> void {
+		LOGD("PLC状态改变,%d", state);
+	};
+	listener.onMonitorData = [&](void* pFrom, int id) -> void {
+		LOGD("PLConMonitorData,%d", id);
+	};
+	listener.onAlarm = [&](void* pFrom, CAlarm* pAlarm, int flag) -> void {
+		LOGE("onAlarm,%d %s", pAlarm->getId(), flag != 0 ? "ON" : "Off");
+		if (flag == 1) {
+			pAlarm->addRef();
+			notifyObjAndPtr(RX_CODE_ALARM_ON, pAlarm, pFrom);
+			pAlarm->release();
+		}
+		else {
+			pAlarm->addRef();
+			notifyObjAndPtr(RX_CODE_ALARM_OFF, pAlarm, pFrom);
+			pAlarm->release();
+		}
+
+	};
+	pPLC->setListener(listener);
 	pPLC->init();
 	m_mapPlc[pszName] = pPLC;
+
+	CString strDir;
+	strDir.Format(_T("%s\\PLCs\\%s"), (LPTSTR)(LPCTSTR)m_strWorkDir, (LPTSTR)(LPCTSTR)pszName);
+	CToolUnits::createDir(strDir);
+	m_configuration.addPLC(pszName, pszIp, port);
 
 	notifyPtr(RX_CODE_ADD_PLC, pPLC);
 	return 0;
@@ -332,8 +374,33 @@
 	auto iter = m_mapPlc.find(pszName);
 	if (iter == m_mapPlc.end()) return -1;
 
+	CString strDir;
+	strDir.Format(_T("%s\\PLCs\\%s"), (LPTSTR)(LPCTSTR)m_strWorkDir, (LPTSTR)(LPCTSTR)pszName);
+	CToolUnits::deleteDir(strDir);
+	m_configuration.removePLC(pszName);
+
 	notifyPtr(RX_CODE_REMOVE_PLC, iter->second);
 	delete iter->second;
 	m_mapPlc.erase(iter);
+
 	return 0;
 }
+
+void CModel::setCurrentPlc(CPLC* pPlc)
+{
+	if (pPlc != nullptr) {
+		m_strCurrPlc = pPlc->getName();
+	} else {
+		m_strCurrPlc = "";
+	}
+}
+
+CPLC* CModel::getCurrentPlc()
+{
+	auto item = m_mapPlc.find(m_strCurrPlc);
+	if (item != m_mapPlc.end()) {
+		return item->second;
+	}
+
+	return nullptr;
+}
\ No newline at end of file

--
Gitblit v1.9.3