123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601 |
- // MFCServiceDlg.cpp : 实现文件
- //
- #include "stdafx.h"
- #include "MFCService.h"
- #include "MFCServiceDlg.h"
- #include "SysLib.h"
- #include "ascprocess.h"
- #include "tcpprocess.h"
- #include "rtuprocess.h"
- #include "icpdasprocess.h"
- #include "SnmpProcess.h"
- #include "NoticeQueue.h"
- #include "MainPro.h"
- #include "CheckDog.h"
- #include "upsparadigmProcess.h"
- #include "stulzprocess.h"
- #include "Separate.h"
- #include "MostDevice.h"
- #include "stdio.h"
- #include "tchar.h"
- #include <windows.h>
- #include "winsvc.h"
- #include "ping.h"
- #include ".\mfcservicedlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- HINSTANCE g_hLangDLL;
- int CALLBACK Sms_NoticeConfig( BOOL bEnableAlarm )
- {
- if( bEnableAlarm==1 )
- LOG4C((LOG_NOTICE, "收到恢复所有报警通知短信"));
- if( bEnableAlarm==0 )
- LOG4C((LOG_NOTICE, "收到关闭所有报警通知短信"));
- g_bAlarmNoticeFlag = bEnableAlarm;
- return 0;
- }
- // 用于应用程序“关于”菜单项的 CAboutDlg 对话框
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
- // 对话框数据
- enum { IDD = IDD_ABOUTBOX };
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
- // 实现
- protected:
- DECLARE_MESSAGE_MAP()
- };
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- }
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- }
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- END_MESSAGE_MAP()
- // CMFCServiceDlg 对话框
- CMFCServiceDlg::CMFCServiceDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CMFCServiceDlg::IDD, pParent)
- {
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- m_switching = SERVICE_OFF;
- isNotify=FALSE;
- }
- void CMFCServiceDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- }
- BEGIN_MESSAGE_MAP(CMFCServiceDlg, CDialog)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- //}}AFX_MSG_MAP
- ON_MESSAGE(WM_SHOWTASK,onShowTask)
- ON_BN_CLICKED(Btn_Start, OnBnClickedStart)
- ON_BN_CLICKED(Btn_Stop, OnBnClickedStop)
- ON_WM_DESTROY()
- ON_WM_TIMER()
- END_MESSAGE_MAP()
- // CMFCServiceDlg 消息处理程序
- BOOL CMFCServiceDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // 将\“关于...\”菜单项添加到系统菜单中。
- // IDM_ABOUTBOX 必须在系统命令范围内。
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- if (pSysMenu != NULL)
- {
- CString strAboutMenu;
- strAboutMenu.LoadString(IDS_ABOUTBOX);
- if (!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
- }
- }
- // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
- // 执行此操作
- SetIcon(m_hIcon, TRUE); // 设置大图标
- SetIcon(m_hIcon, FALSE); // 设置小图标
- // TODO: 在此添加额外的初始化代码
- if (m_switching == SERVICE_OFF)
- {
- (CButton *)(GetDlgItem(Btn_Start))->EnableWindow(TRUE);
- (CButton *)(GetDlgItem(Btn_Stop))->EnableWindow(FALSE);
- sprintf(nid.szTip,_T("名称:MFCIOService\n作用:采集设备数据\n公司:tryto\n服务状态:停止"));//信息提示条为"计划任务提醒"
- }
- else
- {
- (CButton *)(GetDlgItem(Btn_Start))->EnableWindow(FALSE);
- (CButton *)(GetDlgItem(Btn_Stop))->EnableWindow(TRUE);
- }
-
- return TRUE; // 除非设置了控件的焦点,否则返回 TRUE
- }
- void CMFCServiceDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else if ((nID & 0xFFF0) == SC_CLOSE) //触发关闭按钮
- {
- //OnClose(); //不进行关闭;
- LOG4C((LOG_NOTICE,"开启托盘"));
- ExcuteNofify(); //开启托盘;
- //SetTimer(0,100,NULL);
- }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
- // 如果向对话框添加最小化按钮,则需要下面的代码
- // 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
- // 这将由框架自动完成。
- void CMFCServiceDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // 用于绘制的设备上下文
- SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
- // 使图标在工作矩形中居中
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
- // 绘制图标
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
- //当用户拖动最小化窗口时系统调用此函数取得光标显示。
- HCURSOR CMFCServiceDlg::OnQueryDragIcon()
- {
- return static_cast<HCURSOR>(m_hIcon);
- }
- LRESULT CMFCServiceDlg::onShowTask(WPARAM wParam,LPARAM lParam)//wParam接收的是图标的ID,而lParam接收的是鼠标的行为
- {
- if(wParam!=IDR_MAINFRAME)
- return 1;
- switch(lParam)
- {
- case WM_RBUTTONUP: //右键起来时弹出快捷菜单,这里只有一个"关闭"
- {
- LPPOINT lpoint=new tagPOINT;
- ::GetCursorPos(lpoint); //得到鼠标位置
- CMenu menu;
- menu.CreatePopupMenu(); //声明一个弹出式菜单
- if (m_switching == SERVICE_OFF)
- {
- menu.AppendMenu(MF_STRING,Btn_Start,_T("启动服务"));
- }
- else
- menu.AppendMenu(MF_STRING,Btn_Stop,_T("停止服务"));
- menu.AppendMenu(MF_STRING,WM_DESTROY,_T("退出服务")); //增加菜单项"关闭",点击则发送消息WM_DESTROY给主窗口(已隐藏),将程序结束。
- menu.TrackPopupMenu(TPM_RIGHTALIGN|TPM_TOPALIGN,lpoint->x,lpoint->y,this);//确定弹出式菜单的位置
- SetForegroundWindow();
- HMENU hmenu=menu.Detach(); //资源回收
- menu.DestroyMenu();
- delete lpoint;
- }break;
- case WM_LBUTTONDBLCLK: //双击左键的处理
- {
- //this->ShowWindow(SW_SHOW); //简单的显示主窗口完事儿
- //KillTimer(0);
- }break;
- }
- return 0;
- }
- void CMFCServiceDlg::ExcuteNofify()
- {
- // TODO: 在此添加控件通知处理程序代码
- if (!isNotify)
- {
- //CString strOnTip = "名称:MFCIOService\n作用:采集设备数据\n公司:StoneU\n服务开启";
- //CString strOffTip = "名称:MFCIOService\n作用:采集设备数据\n公司:StoneU\n服务停止";
- nid.cbSize=(DWORD)sizeof(NOTIFYICONDATA);
- nid.hWnd=this->m_hWnd;
- nid.uID=IDR_MAINFRAME;
- nid.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP|NIF_INFO;
- nid.uCallbackMessage=WM_SHOWTASK;//自定义的消息名称,注意:这里的消息是用户自定义消息
- nid.hIcon=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME));
- if(m_switching == SERVICE_OFF)
- {
- sprintf(nid.szTip,_T("名称:MFCIOService\n作用:采集设备数据\n公司:tryto\n服务状态:停止"));//信息提示条为"计划任务提醒"
- sprintf(nid.szInfo,_T("服务已停止"));
- }
- else
- {
- sprintf(nid.szTip,_T("名称:MFCIOService\n作用:采集设备数据\n公司:tryto\n服务状态:开启"));//信息提示条为"计划任务提醒"
- sprintf(nid.szInfo,_T("服务已开启"));
- }
- //sprintf(nid.szTip,_T("名称:MFCIOService\n作用:采集设备数据\n公司:StoneU\n"));//信息提示条为"计划任务提醒"
-
- sprintf(nid.szInfoTitle,_T("内容"));
- nid.dwInfoFlags=NIIF_INFO;
- nid.uTimeout=5000;
- Shell_NotifyIcon(NIM_ADD,&nid);//在托盘区添加图标
- }
- //AnimateWindow(1000,AW_HIDE|AW_BLEND); //可以缓慢消失窗口
- // KillTimer(0);
- ShowWindow(SW_HIDE);//隐藏主窗口
- }
- BOOL CMFCServiceDlg::InitIOService()
- {
- // 创建运行控制事件;
- MTVERIFY (g_hRunObject = ::CreateEvent(
- NULL, /* Security */
- TRUE, /* Manual event */
- 0, /* Clear on creation */
- 0));
- //软件时间限制代码
- //if( ProgramEncrypt(NULL) == 0 ) return FALSE;
- g_pDevicesManager = new CDevicesManager();
- // 初始化软件狗
- #if IS_USE_DOG
- if( InitDog() != 0 )
- {
- if( g_hRunObject != NULL )
- {
- MTVERIFY( CloseHandle( g_hRunObject ) );
- g_hRunObject = NULL;
- }
- return FALSE;
- }
- #endif
- //LOG4C((LOG_NOTICE, "%s", g_strConnectString));
- //连接数据库
- //LOG4C((LOG_NOTICE, "Enter OpenDataBase"));
- CDBConnection::GetInstancePtr()->OpenDataBase(g_strConnectString);
- //LOG4C((LOG_NOTICE, "Leave OpenDataBase"));
- //LOG4C((LOG_NOTICE, "Enter DBConnectionCheckThreadStart"));
-
- // 数据库连接状态 判断线程;
- CDBConnection::GetInstancePtr()->DBConnectionCheckThreadStart();
- //LOG4C((LOG_NOTICE, "Leave DBConnectionCheckThreadStart"));
- //LOG4C((LOG_NOTICE, "Enter SetDBType"));
- // 设置数据库类型;
- CDBInterface::GetInstancePtr()->SetDBType(g_strDBType);
- //LOG4C((LOG_NOTICE, "Leave SetDBType"));
- //LOG4C((LOG_NOTICE, "Enter InitUserRoleInfo"));
- // 分配用户角色
- CDBInterface::GetInstancePtr()->InitUserRoleInfo();
- //LOG4C((LOG_NOTICE, "Leave InitUserRoleInfo"));
- //LOG4C((LOG_NOTICE, "Enter InitNotice"));
- // 创建 变量删除控制 事件;
- MTVERIFY( g_hDeleteVarSem = CreateEvent(NULL, TRUE, TRUE, "DeleteVarList") );
- // 初始化 变量提示互斥;
- InitializeCriticalSection( &g_csVarNotice );
- // 初始化 发送报警互斥;
- InitializeCriticalSection( &g_csSendNotice );
- // ***************初始化 语音报警功能模块***************;
- if( g_nSendNotice )
- {
- InitNotice();
- }
- else
- LOG4C((LOG_NOTICE, "没有启用语言报警"));
- // ***************初始化 短信报警功能模块***************;
- if( g_nSendSms==1 )
- {
- CString strCommPort = CString(g_strSmsComPort);
- strCommPort = strCommPort.Mid( 3 );
- if( 0==InitSms( 0, atoi(strCommPort), g_nSmsRate, g_nSmsDataBit,
- g_nSmsStopBit, g_nSmsParity, g_nSmsInterval, g_nSmsLanguageTrans, g_nSmsMaxChar, g_nSmsMakeCall ) )
- {
- #if 1
- if( pSmsDllSetCallBack )
- {
- LOG4C((LOG_NOTICE, "启用短信回调函数"));
- pSmsDllSetCallBack( Sms_NoticeConfig );
- }else
- LOG4C((LOG_NOTICE,"open com failed~"));
- #endif
- }
- }
- else if( g_nSendSms==2 )
- {
- LOG4C((LOG_NOTICE, "启用支持短信平台服务"));
- }
- else
- LOG4C((LOG_NOTICE, "没有启用短信报警"));
- // ***************初始化 Email报警功能模块***************;
- if( g_nSendEmail )
- {
- InitEmail(g_strEmailSMTPSrv, g_nEmailSMTPPort, g_nEmailIsNeed, g_strEmailUserAcc, g_strEmailUserPwd,
- g_strEmailFromAddr, g_nEmailTimeOut);
- }
- else
- LOG4C((LOG_NOTICE, "没有启用email报警"));
- #if 0
- // 创建 TCP通信服务器 = 所有变量只用一个端口进行通信;
- g_pTcpServer = new CServerSocket();
- CString strPort;
- strPort.Format("%d", SOCKET_SERVER_PORT);
- // 开始 客户端 与 服务端通信线程;
- g_pTcpServer->SvrStart(strPort);
- #else
- //LOG4C((LOG_NOTICE, "open listen port"));
- // 创建 TCP通信服务器 = 多个端口进行通信,按变量每份100进行端口创建;
- g_pServerSocket[0] = new CServerSocket();
- CString strPort;
- strPort.Format("%d", SOCKET_SERVER_PORT);
- // 开始 客户端 与 服务端通信线程;
- g_pServerSocket[0]->SvrStart(strPort);
- #endif
- // 初始化ping命令 功能模块;
- InitPing();
- // 开始采集I/O设备
- StartMain();
- //UnInitIOService();
- return TRUE;
- }
- void CMFCServiceDlg::UnInitIOService()
- {
- MTVERIFY( SetEvent( g_hDeleteVarSem ) );
- DeleteCriticalSection( &g_csVarNotice );
- DeleteCriticalSection( &g_csSendNotice );
- if( g_hDeleteVarSem )
- {
- MTVERIFY( CloseHandle( g_hDeleteVarSem) );
- g_hDeleteVarSem = NULL;
- }
- if (g_hRunObject != NULL)
- MTVERIFY( SetEvent( g_hRunObject ) );
- EndMain();
- #if 0
- if (g_pTcpServer != NULL)
- {
- g_pTcpServer->SocketStop();
- delete g_pTcpServer;
- g_pTcpServer = NULL;
- }
- #else
- for( int i = 0; i < MAX_SERVER_LISTEN; i++ )
- {
- if( NULL != g_pServerSocket[i] )
- {
- g_pServerSocket[i]->SocketStop();
- delete g_pServerSocket[i];
- g_pServerSocket[i] = NULL;
- }
- }
- #endif
- UnInitRtuDll();
- UnInitTcpDll();
- UnInitAscDll();
- UnInitIcpdasDll();
- UnInitSnmpDll();
- UnInitParadigmDll();
- UnInitStulzDll();
- UnInitSeparateDll();
- UnInitNotice();
- if( g_nSendEmail )
- {
- UnInitEmail();
- }
- if( g_nSendSms==1 )
- {
- UnInitSms();
- }
- //回收用户角色
- CDBInterface::GetInstancePtr()->UnInitUserRoleInfo();
- CDBConnection::GetInstancePtr()->DBConnectionCheckThreadStop();
- // 释放软件狗
- #if IS_USE_DOG
- UnInitDog();
- #endif
- if( g_pDevicesManager )
- {
- delete g_pDevicesManager;
- g_pDevicesManager = NULL;
- }
- if( g_hRunObject != NULL )
- {
- MTVERIFY( CloseHandle( g_hRunObject ) );
- }
- if(g_hLangDLL)
- {
- AfxFreeLibrary(g_hLangDLL);
- }
- UnInitPing();
- }
- void CMFCServiceDlg::OnBnClickedStart()
- {
- (CButton *)(GetDlgItem(Btn_Start))->EnableWindow(FALSE);
- (CButton *)(GetDlgItem(Btn_Stop))->EnableWindow();
- if ( !InitIOService())
- {
- MessageBox("服务退出");
- (CButton *)(GetDlgItem(Btn_Start))->EnableWindow();
- (CButton *)(GetDlgItem(Btn_Stop))->EnableWindow(FALSE);
- return;
- }
-
- if (m_switching == SERVICE_OFF)
- {
- m_switching = SEEVICE_ON;
- }
- nid.cbSize=(DWORD)sizeof(NOTIFYICONDATA);
- nid.hWnd=this->m_hWnd;
- nid.uID=IDR_MAINFRAME;
- nid.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP|NIF_INFO;
- nid.uCallbackMessage=WM_SHOWTASK;//自定义的消息名称,注意:这里的消息是用户自定义消息
- nid.hIcon=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME));
- sprintf(nid.szTip,_T("名称:MFCIOService\n作用:采集设备数据\n公司:tryto\n服务状态:开启"));//信息提示条为"计划任务提醒"
- //sprintf(nid.szTip,_T("名称:MFCIOService\n作用:采集设备数据\n公司:StoneU\n"));//信息提示条为"计划任务提醒"
- sprintf(nid.szInfo,_T("服务已启动"));
- sprintf(nid.szInfoTitle,_T("内容"));
- nid.dwInfoFlags=NIIF_INFO;
- nid.uTimeout=5000;
- Shell_NotifyIcon(NIM_MODIFY,&nid);//在托盘区添加图标
- }
- void CMFCServiceDlg::OnBnClickedStop()
- {
- (CButton *)(GetDlgItem(Btn_Start))->EnableWindow();
- (CButton *)(GetDlgItem(Btn_Stop))->EnableWindow(FALSE);
- UnInitIOService();
- if (m_switching == SEEVICE_ON)
- {
- m_switching = SERVICE_OFF;
- }
- nid.cbSize=(DWORD)sizeof(NOTIFYICONDATA);
- nid.hWnd=this->m_hWnd;
- nid.uID=IDR_MAINFRAME;
- nid.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP|NIF_INFO;
- nid.uCallbackMessage=WM_SHOWTASK;//自定义的消息名称,注意:这里的消息是用户自定义消息
- nid.hIcon=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME));
- sprintf(nid.szTip,_T("名称:MFCIOService\n作用:采集设备数据\n公司:tryto\n服务状态:停止"));//信息提示条为"计划任务提醒"
- //sprintf(nid.szTip,_T("名称:MFCIOService\n作用:采集设备数据\n公司:StoneU\n"));//信息提示条为"计划任务提醒"
- sprintf(nid.szInfo,_T("服务已停止"));
- sprintf(nid.szInfoTitle,_T("内容"));
- nid.dwInfoFlags=NIIF_INFO;
- nid.uTimeout=5000;
- Shell_NotifyIcon(NIM_MODIFY,&nid);//在托盘区添加图标
- }
- void CMFCServiceDlg::OnDestroy()
- {
- CDialog::OnDestroy();
- //KillTimer(0);
- Shell_NotifyIcon(NIM_DELETE,&nid);
- }
- void CMFCServiceDlg::OnTimer(UINT nIDEvent)
- {
- // TODO: 在此添加消息处理程序代码和/或调用默认值
- LOG4C((LOG_NOTICE,"时间控制器"));
- nid.cbSize=(DWORD)sizeof(NOTIFYICONDATA);
- nid.hWnd=this->m_hWnd;
- nid.uID=IDR_MAINFRAME;
- nid.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP|NIF_INFO;
- nid.uCallbackMessage=WM_SHOWTASK;//自定义的消息名称,注意:这里的消息是用户自定义消息
- nid.hIcon=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME));
- if(m_switching == SERVICE_OFF)
- {
- sprintf(nid.szTip,_T("名称:MFCIOService\n作用:采集设备数据\n公司:tryto\n服务状态:停止"));//信息提示条为"计划任务提醒"
- }
- else
- {
- sprintf(nid.szTip,_T("名称:MFCIOService\n作用:采集设备数据\n公司:tryto\n服务状态:开启"));//信息提示条为"计划任务提醒"
- }
- //sprintf(nid.szTip,_T("名称:MFCIOService\n作用:采集设备数据\n公司:StoneU\n"));//信息提示条为"计划任务提醒"
- sprintf(nid.szInfo,_T("采集服务"));
- sprintf(nid.szInfoTitle,_T("内容"));
- nid.dwInfoFlags=NIIF_INFO;
- nid.uTimeout=5000;
- Shell_NotifyIcon(NIM_ADD,&nid);//在托盘区添加图标
- CDialog::OnTimer(nIDEvent);
- }
|