mrDarker
6 天以前 829fe6c6bc33d53fda9c31fd45a37e1df87befff
SourceCode/Bond/BoounionPLC/BoounionPLCDlg.cpp
@@ -8,11 +8,23 @@
#include "afxdialogex.h"
#include "Common.h"
#include "PlcView.h"
#include "Log.h"
#include "InputDialog.h"
#include "AddPLCInfo.h"
#include "AxisManager.h"
#include "IOManager.h"
// 测试
#include "AxisSettingsDlg.h"
#include "IOMonitoringDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#define LOG_WND_HEIGHT      258
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
@@ -60,6 +72,8 @@
   m_crBkgnd = APP_MAIN_DLG_BACKGROUND;
   m_hbrBkgnd = nullptr;
   m_pActiveView = nullptr;
   m_pPageLogcat = nullptr;
   m_bShowLogWnd = TRUE;
}
void CBoounionPLCDlg::DoDataExchange(CDataExchange* pDX)
@@ -78,6 +92,8 @@
   ON_UPDATE_COMMAND_UI(ID_MENU_FILE_EXIT, &CBoounionPLCDlg::OnUpdateMenuFileExit)
   ON_COMMAND(ID_MENU_FILE_SETTINGS, &CBoounionPLCDlg::OnMenuFileSettings)
   ON_UPDATE_COMMAND_UI(ID_MENU_FILE_SETTINGS, &CBoounionPLCDlg::OnUpdateMenuFileSettings)
   ON_COMMAND(ID_MENU_WND_LOG, &CBoounionPLCDlg::OnMenuWndLog)
   ON_UPDATE_COMMAND_UI(ID_MENU_WND_LOG, &CBoounionPLCDlg::OnUpdateMenuWndLog)
   ON_COMMAND(ID_MENU_HELP_ABOUT, &CBoounionPLCDlg::OnMenuHelpAbout)
   ON_WM_INITMENUPOPUP()
   ON_MESSAGE(ID_MSG_TOOLBAR_BTN_CLICKED, &CBoounionPLCDlg::OnToolbarBtnClicked)
@@ -104,18 +120,46 @@
         if (RX_CODE_SELECT_PLC == code) {
            CPLC* pPlc;
            if (pAny->getPtrValue("ptr", (void*&)pPlc)) {
               ASSERT(m_pMainContainer != nullptr);
               if (m_pActiveView == nullptr) {
                  m_pActiveView = (CPlcView*)CreatePlcView(pPlc);
               if (pPlc != nullptr) {
                  ASSERT(m_pMainContainer != nullptr);
                  if (m_pActiveView == nullptr) {
                     m_pActiveView = (CPlcView*)CreatePlcView(pPlc);
                  }
                  ASSERT(m_pActiveView);
                  if (m_pActiveView->GetContext() != (void*)pPlc) {
                     CString strTitle;
                     strTitle.Format(_T("%s[%s:%d]"), pPlc->getName().c_str(), pPlc->getIp().c_str(),
                        pPlc->getPort());
                     m_pActiveView->SetWindowText(strTitle);
                     m_pActiveView->SetContext(pPlc);
                     m_pActiveView->SendMessage(WM_NCPAINT, 0, 0);
                  }
               } else {
                  CloseView(m_pActiveView);
               }
               ASSERT(m_pActiveView);
               if (m_pActiveView->GetContext() != (void*)pPlc) {
                  m_pActiveView->SetWindowText(pPlc->getName().c_str());
                  m_pActiveView->SetContext(pPlc);
               }
               theApp.m_model.setCurrentPlc(pPlc);
            }
         }
         else if (RX_PLC_STATE_CHANGED == code) {
         }
         else if (code == RX_CODE_ALARM_ON) {
            CPLC* pPlc;
            if (pAny->getPtrValue("ptr", (void*&)pPlc) && pPlc == theApp.m_model.getCurrentPlc()) {
               AlarmOn(pPlc);
               CAlarmMonitor* pComponent = (CAlarmMonitor*)pPlc->getComponent(ALARM_MONITOR);
               m_pTopToolbar->GetBtn(IDC_BUTTON_ALARM)->EnableWindow(pComponent->isAlarming());
            }
         }
         else if (code == RX_CODE_ALARM_OFF) {
            CPLC* pPlc;
            if (pAny->getPtrValue("ptr", (void*&)pPlc) && pPlc == theApp.m_model.getCurrentPlc()) {
               AlarmOff(pPlc);
               CAlarmMonitor* pComponent = (CAlarmMonitor*)pPlc->getComponent(ALARM_MONITOR);
               m_pTopToolbar->GetBtn(IDC_BUTTON_ALARM)->EnableWindow(pComponent->isAlarming());
            }
         }
         pAny->release();
      }, [&]() -> void {
         // onComplete
@@ -163,6 +207,7 @@
   // toolbar
   m_pTopToolbar = new CTopToolbar();
   m_pTopToolbar->Create(IDD_TOP_TOOLBAR, this);
   m_pTopToolbar->GetBtn(IDC_BUTTON_ALARM)->EnableWindow(FALSE);
   m_pTopToolbar->ShowWindow(SW_SHOW);
   HMENU hMenu = m_pTopToolbar->GetOperatorMenu();
   ASSERT(hMenu);
@@ -179,6 +224,13 @@
   m_pMainContainer = new CMainContainer();
   m_pMainContainer->Create(IDD_MAIN_CONTAINER, this);
   m_pMainContainer->ShowWindow(SW_SHOW);
   // 日志
   m_pPageLogcat = new CPageLogcat();
   m_pPageLogcat->Create(IDD_PAGE_LOGCAT, m_pMainContainer);
   m_pPageLogcat->ShowWindow(SW_SHOW);
   m_pMainContainer->SetBottomWnd(m_pPageLogcat, LOG_WND_HEIGHT);
   // 菜单
@@ -331,6 +383,12 @@
      m_pPagePlcList = nullptr;
   }
   
   if (m_pPageLogcat != nullptr) {
      m_pPageLogcat->DestroyWindow();
      delete m_pPageLogcat;
      m_pPageLogcat = nullptr;
   }
   if (m_pMainContainer != nullptr) {
      m_pMainContainer->DestroyWindow();
      delete m_pMainContainer;
@@ -415,6 +473,29 @@
   pCmdUI->Enable(TRUE);
}
void CBoounionPLCDlg::OnMenuWndLog()
{
   m_bShowLogWnd = !m_bShowLogWnd;
   if (m_bShowLogWnd) {
      m_pPageLogcat->ShowWindow(SW_SHOW);
      m_pPageLogcat->SetParent(m_pMainContainer);
      m_pMainContainer->SetBottomWnd(m_pPageLogcat, LOG_WND_HEIGHT);
      m_pMainContainer->Resize();
   }
   else {
      m_pPageLogcat->ShowWindow(SW_HIDE);
      m_pPageLogcat->SetParent(this);
      m_pMainContainer->SetBottomWnd(nullptr, 0);
      m_pMainContainer->Resize();
   }
}
void CBoounionPLCDlg::OnUpdateMenuWndLog(CCmdUI* pCmdUI)
{
   pCmdUI->SetCheck(m_bShowLogWnd);
}
void CBoounionPLCDlg::OnMenuHelpAbout()
{
   CAboutDlg dlgAbout;
@@ -425,20 +506,43 @@
{
   int id = (int)lParam;
   if (id == IDC_BUTTON_ADD) {
      static int i = 0;
      char szName[256];
      sprintf_s(szName, 256, "PLC%d", ++i);
      theApp.m_model.addPlc(szName, "192.168.1.188", 1001);
      CAddPLCInfo dlgAddPLCInfo;
      if (dlgAddPLCInfo.DoModal() != IDOK) {
         return 0;
      }
      CString strPLCName = dlgAddPLCInfo.GetPLCName();
      CString strIP = dlgAddPLCInfo.GetIP();
      CString strPort = dlgAddPLCInfo.GetPort();
      if (!strPLCName.IsEmpty() && !strIP.IsEmpty() && !strPort.IsEmpty()) {
         theApp.m_model.addPlc((LPTSTR)(LPCTSTR)strPLCName, (LPTSTR)(LPCTSTR)strIP, std::stoi((LPTSTR)(LPCTSTR)strPort));
         // 新建轴文件
         AxisManager axisManager;
         axisManager.SaveAxis((LPTSTR)(LPCTSTR)strPLCName);
         // 新建IO文件
         IOManager ioManager;
         ioManager.SaveToFile((LPTSTR)(LPCTSTR)strPLCName);
      }
   }
   else if (id == IDC_BUTTON_DELETE) {
      static int i = 0;
      i += 1;
      char szName[256];
      sprintf_s(szName, 256, "PLC%d", i);
      theApp.m_model.removePlc(szName);
      CPLC* pPlc = theApp.m_model.getCurrentPlc();
      if (pPlc != nullptr) {
         theApp.m_model.removePlc(pPlc->getName().c_str());
      }
   }
   else if (id == IDC_BUTTON_SETTINGS) {
      // 测试 IO模块
      CPLC* pPLC = theApp.m_model.getCurrentPlc();
      if (pPLC != nullptr) {
         //CIOMonitoringDlg dlg;
         //dlg.DoModal();
         CAxisSettingsDlg dlg;
         dlg.DoModal();
      }
   }
   else if (id == IDC_BUTTON_OPERATOR) {
      /*
@@ -554,3 +658,25 @@
   m_pActiveView = nullptr;
   m_pMainContainer->Resize();
}
void CBoounionPLCDlg::AlarmOn(CPLC* pPlc)
{
   if (m_pAlarmWnd == nullptr) {
      m_pAlarmWnd = new CAlarmPopupDlg();
      m_pAlarmWnd->SetPLC(pPlc);
      m_pAlarmWnd->Create(IDD_DIALOG_POPUP_ALARM, this);
      m_pAlarmWnd->CenterWindow();
   }
   m_pAlarmWnd->AlarmOn();
}
void CBoounionPLCDlg::AlarmOff(CPLC* pPlc)
{
   if (m_pAlarmWnd == nullptr) {
      m_pAlarmWnd = new CAlarmPopupDlg();
      m_pAlarmWnd->SetPLC(pPlc);
      m_pAlarmWnd->Create(IDD_DIALOG_POPUP_ALARM, this);
      m_pAlarmWnd->CenterWindow();
   }
   m_pAlarmWnd->AlarmOff();
}