From 592c0397bd5bc333b37b0b762b1bfeedae11f770 Mon Sep 17 00:00:00 2001
From: LAPTOP-T815PCOQ\25526 <mr.liuyang@126.com>
Date: 星期四, 09 一月 2025 19:30:16 +0800
Subject: [PATCH] 1. 添加删除目录的辅助函数 2.添加PLC列表管理 3.进一步完善输出PLC功能

---
 SourceCode/Bond/BoounionPLC/Model.cpp |  163 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 160 insertions(+), 3 deletions(-)

diff --git a/SourceCode/Bond/BoounionPLC/Model.cpp b/SourceCode/Bond/BoounionPLC/Model.cpp
index e5c97c6..6ac7225 100644
--- a/SourceCode/Bond/BoounionPLC/Model.cpp
+++ b/SourceCode/Bond/BoounionPLC/Model.cpp
@@ -2,7 +2,10 @@
 #include "Model.h"
 #include "Log.h"
 #include "Common.h"
+#include "ToolUnits.h"
 
+// 常量
+#define ADDR_NIGHT_SHIFT_CAPACTITY		2027
 
 CModel* g_pModel = NULL;
 void CALLBACK ModerTimerProc(HWND hWnd, UINT nMsg, UINT nTimerid, DWORD dwTime)
@@ -72,11 +75,21 @@
 	g_pModel = this;
 
 
+	// 获取所有PLC信息
+	std::vector<PlcInfo> plcList;
+	m_configuration.getAllPLCInfo(plcList);
+	for (const auto& plc : plcList) {
+		addPlc(plc.strName, plc.strIp, plc.nPort);
+	}
+
 	return 0;
 }
 
 int CModel::term()
 {
+	for (auto item : m_mapPlc) {
+		item.second->term();
+	}
 	CLog::GetLog()->SetOnLogCallback(nullptr);
 	return 0;
 }
@@ -288,10 +301,21 @@
 
 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::gtPlcMap()
+std::map<std::string, CPLC*>& CModel::getPlcMap()
 {
 	return m_mapPlc;
 }
@@ -301,7 +325,13 @@
 	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;
+
+	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;
@@ -312,8 +342,135 @@
 	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);
+	//delete iter->second;
+	//m_mapPlc.erase(iter);		这个地方需要研究一下
+
 	return 0;
 }
+
+void CModel::setCurrentPlc(CPLC* pPlc)
+{
+	if (pPlc != nullptr) {
+		m_strCurrPlc = pPlc->getName();
+	}
+}
+
+CPLC* CModel::getCurrentPlc()
+{
+	auto item = m_mapPlc.find(m_strCurrPlc);
+	if (item != m_mapPlc.end()) {
+		return item->second;
+	}
+
+	return nullptr;
+}
+
+void CModel::readPLCDataRegularly()
+{
+	CPLC* pPlc = getCurrentPlc();
+	if (pPlc == nullptr || !pPlc->isConnected()) return;
+
+	{
+		auto funOnReadData = [this](IMcChannel* pChannel, int addr, char* pData, unsigned int nDataSize, int flag) -> void {
+			if (nDataSize == 2 && flag == 0) {
+				int nVelocityRatio = CToolUnits::toInt16(&pData[0]);
+				if (nVelocityRatio != m_nVelocityRatio) {
+					m_nVelocityRatio = nVelocityRatio;
+					notifyInt(RX_CODE_VELOCITY_RATIO, m_nVelocityRatio);
+				}
+			}
+		};
+		pPlc->readData(MC::D, 530, 2, funOnReadData);
+	}
+
+	{
+		auto funOnReadData = [this](IMcChannel* pChannel, int addr, char* pData, unsigned int nDataSize, int flag) -> void {
+			if (nDataSize == 2 && flag == 0) {
+				double dTactTime = (double)CToolUnits::toInt16(&pData[0]);
+				if (dTactTime != m_dTactTime) {
+					m_dTactTime = dTactTime;
+					notifyDouble(RX_CODE_TACT_TIME, m_dTactTime);
+				}
+			}
+		};
+		pPlc->readData(MC::ZR, 1500, 2, funOnReadData);
+	}
+
+	{
+		auto funOnReadData = [this](IMcChannel* pChannel, int addr, char* pData, unsigned int nDataSize, int flag) -> void {
+			if (nDataSize == (ADDR_NIGHT_SHIFT_CAPACTITY - 2012 + 1) * 2 && flag == 0) {
+				int nDayShiftCapacity = CToolUnits::toInt16(&pData[0]);
+				int nNightShiftCapacity = CToolUnits::toInt16(&pData[(ADDR_NIGHT_SHIFT_CAPACTITY - 2012) * 2]);
+				if (nDayShiftCapacity != m_nDayShiftCapacity) {
+					m_nDayShiftCapacity = nDayShiftCapacity;
+					notifyInt(RX_CODE_DAY_SHIFT_CAPACTITY, nDayShiftCapacity);
+				}
+				if (nNightShiftCapacity != m_nNightShiftCapacity) {
+					m_nNightShiftCapacity = nNightShiftCapacity;
+					notifyInt(RX_CODE_NIGHT_SHIFT_CAPACTITY, nNightShiftCapacity);
+				}
+
+			}
+		};
+		pPlc->readData(MC::ZR, 2012, (ADDR_NIGHT_SHIFT_CAPACTITY - 2012 + 1) * 2, funOnReadData);
+	}
+
+	{
+		int nStartAddress = 1000;
+		int nEndAddress = 1200;
+		int nReadSize = (nEndAddress - nStartAddress + 1) * 2;
+		auto funOnReadData = [this, nStartAddress, nReadSize](IMcChannel* pChannel, int addr, char* pData, unsigned int nDataSize, int flag) -> void {
+			if (nDataSize == nReadSize && flag == 0) {
+				bool bRun = CToolUnits::toInt16(&pData[(1103 - nStartAddress) * 2]) != 0;		// 启动
+				bool bAuto = CToolUnits::toInt16(&pData[(1100 - nStartAddress) * 2]) != 0;		// 自动
+				bool bPuase = CToolUnits::toInt16(&pData[(1104 - nStartAddress) * 2]) != 0;		// 暂停
+				bool bManual = CToolUnits::toInt16(&pData[(1100 - nStartAddress) * 2]) != 0;	// 手动
+				bool bBeep = CToolUnits::toInt16(&pData[(1003 - nStartAddress) * 2]) != 0;		// 静音
+				bool bResetting = CToolUnits::toInt16(&pData[(1150 - nStartAddress) * 2]) != 0; // 复位
+				bool bStop = CToolUnits::toInt16(&pData[(1114 - nStartAddress) * 2]) != 0;		// 停止
+
+				if (m_bBlBtnsStates[0] != bRun) {
+					m_bBlBtnsStates[0] = bRun;
+					notifyInt(RX_CODE_ACTIVATE, bRun);
+				}
+
+				if (m_bBlBtnsStates[1] != bAuto) {
+					m_bBlBtnsStates[1] = bAuto;
+					notifyInt(RX_CODE_AUTO, bAuto);
+				}
+
+				if (m_bBlBtnsStates[2] != bPuase) {
+					m_bBlBtnsStates[2] = bPuase;
+					notifyInt(RX_CODE_PUASE, bPuase);
+				}
+
+				if (m_bBlBtnsStates[3] != bManual) {
+					m_bBlBtnsStates[3] = bManual;
+					notifyInt(RX_CODE_MANUAL, bManual);
+				}
+
+				if (m_bBlBtnsStates[4] != bBeep) {
+					m_bBlBtnsStates[4] = bBeep;
+					notifyInt(RX_CODE_BEEP, bBeep);
+				}
+
+				if (m_bBlBtnsStates[5] != bResetting) {
+					m_bBlBtnsStates[5] = bResetting;
+					notifyInt(RX_CODE_RESETTING, bResetting);
+				}
+
+				if (m_bBlBtnsStates[6] != bStop) {
+					m_bBlBtnsStates[6] = bStop;
+					notifyInt(RX_CODE_STOP, bStop);
+				}
+			}
+		};
+		pPlc->readData(MC::M, nStartAddress, nReadSize, funOnReadData);
+	}
+}
\ No newline at end of file

--
Gitblit v1.9.3