LAPTOP-T815PCOQ\25526
2025-01-09 0ba3414c4745224a7ee6f68fbc640bf74e3ee87d
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)
@@ -23,6 +26,10 @@
CModel::~CModel()
{
   for (auto item : m_mapPlc) {
      delete item.second;
   }
   m_mapPlc.clear();
}
IObservable* CModel::getObservable()
@@ -68,11 +75,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 +299,167 @@
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);
   m_strCurrPlc = "";
   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);
   }
}