123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using BroadcastModule;
- using DatabaseDLL;
- using NumMachine;
- using parkMonitor.tools;
- using PLCS7;
- using S7.Net;
- using WebServer;
- namespace Entry
- {
- public class SystemInitializer
- {
- /// <summary>
- /// 中控系统总状态
- /// </summary>
- public static bool globalStatus = false;
- /// <summary>
- /// 初始化步骤
- /// </summary>
- public static int initializeState = 0;
- /// <summary>
- /// PLC对象句柄
- /// </summary>
- public static AbstractPLCLinker PLC = null;
- public static string plcIPAddr { get; set; }
- public static int plcRack { get; set; }
- public static int plcSlot { get; set; }
- public static string[] plcDatablockConfig { get; set; }
- public static int plcTerminalCount { get; set; }
- public static int plcParkingSpaceCount { get; set; }
- public static int plcRefreshInterval { get; set; }
- /// <summary>
- /// 远程数据库操作句柄
- /// </summary>
- public static DBOperation remoteDBOper;
- /// <summary>
- /// 本地数据库操作句柄
- /// </summary>
- public static DBOperation localDBOper;
- /// <summary>
- /// 显示板操作对象句柄
- /// </summary>
- public static BroadcastBoard allInOneMachine;
- public static string allInOneMachineIP { get; set; }
- public static int allInOneMachinePort { get; set; }
- /// <summary>
- /// 号牌机操作句柄
- /// </summary>
- public static INumMachineLinker numMachineLinker;
- /// <summary>
- /// 本地web操作句柄
- /// </summary>
- public static IWebServer webServer;
- public static int webPort { get; set; }
- /// <summary>
- /// 初始化各模块
- /// </summary>
- /// <returns></returns>
- public static void Init(IntPtr flpHandle)
- {
- bool initStatus = false;
- int retryCount = 0;
- string remoteDBConnStr, localDBConnStr;
- int DBtimeout;
- int garageID;
- MyTimer mt = new MyTimer();
- mt.StartTiming();
- try
- {
- retryCount = Convert.ToInt32(ConfigurationManager.AppSettings.Get("retryCount"));
- //数据库
- remoteDBConnStr = ConfigurationManager.AppSettings.Get("remoteDBConnStr");
- localDBConnStr = ConfigurationManager.AppSettings.Get("localDBConnStr");
- DBtimeout = Convert.ToInt32(ConfigurationManager.AppSettings.Get("DBtimeout"));
- //plc
- plcIPAddr = ConfigurationManager.AppSettings.Get("plcIpAddress");
- plcRack = Convert.ToInt32(ConfigurationManager.AppSettings.Get("plcRack"));
- plcSlot = Convert.ToInt32(ConfigurationManager.AppSettings.Get("plcSlot"));
- plcDatablockConfig = ConfigurationManager.AppSettings.Get("plcDatablockId").Split(',');
- plcTerminalCount = Convert.ToInt32(ConfigurationManager.AppSettings.Get("plcTerminalCount"));
- plcParkingSpaceCount = Convert.ToInt32(ConfigurationManager.AppSettings.Get("plcParkingSpaceCount"));
- plcRefreshInterval = Convert.ToInt32(ConfigurationManager.AppSettings.Get("plcRefreshInterval"));
- //显示屏
- allInOneMachineIP = ConfigurationManager.AppSettings.Get("allInOneMachineIP");
- allInOneMachinePort = Convert.ToInt32(ConfigurationManager.AppSettings.Get("allInOneMachinePort"));
- //webServer端口
- webPort = Convert.ToInt32(ConfigurationManager.AppSettings.Get("webPort"));
- garageID = Convert.ToInt32(ConfigurationManager.AppSettings.Get("garageID"));
- //配置文件读取结束,进入状态1
- initializeState = 1;
- initStatus = true;
- }
- catch (Exception e) { Console.WriteLine("初始化," + e.Message); return; }
- initStatus = initStatus && PLCInit(retryCount);
- //PLC初始化结束,进入状态2
- if (!initStatus) { return; }
- else { initializeState = 2;}
- //初始化数据库对象
- remoteDBOper = new DBOperation(remoteDBConnStr, DBtimeout);
- localDBOper = new DBOperation(localDBConnStr, DBtimeout);
- initStatus = initStatus && remoteDBOper.InitConnPooling(10);
- initStatus = initStatus && localDBOper.InitConnPooling(10);
- //数据库初始化结束,进入状态3
- if (!initStatus) { return; }
- else { initializeState = 3; }
- //初始化web对象
- webServer = new CentralForWeb();
- initStatus = initStatus && webServer.Start(webPort);
- //webServer初始化结束,进入状态4
- if (!initStatus) { return; }
- else { initializeState = 4; }
- //初始化号牌机对象
- numMachineLinker = new NumMachineLinker(flpHandle);
- numMachineLinker.Start();
- //初始化显示板对象,显示板udp面向无连接
- allInOneMachine = new BroadcastBoard(allInOneMachineIP, allInOneMachinePort);
- allInOneMachine.Refresh();
- //系统初始化结束,进入状态5
- if (!initStatus) { return; }
- else { initializeState = 5; }
- mt.EndTiming();
- Console.WriteLine("初始化耗时:"+mt.GetInterval().TotalSeconds);
- switch(initializeState){
- case 0:
- Console.WriteLine("配置文件读写异常");
- break;
- case 1:
- Console.WriteLine("PLC初始化异常");
- break;
- case 2:
- Console.WriteLine("数据库初始化异常");
- break;
- case 3:
- Console.WriteLine("webServer初始化异常");
- break;
- case 4:
- Console.WriteLine("其他异常");
- break;
- case 5:
- Console.WriteLine("初始化成功");
- break;
- }
- }
- /// <summary>
- /// PLC初始化与连接
- /// </summary>
- /// <param name="retryCount"></param>
- /// <returns></returns>
- public static bool PLCInit(int retryCount)
- {
- int temp = retryCount;
- //初始化PLC对象
- while (temp-- > 0)
- {
- if (PLC == null)
- {
- PLC = new PLCLinker(CpuType.S71500, plcIPAddr, (short)plcRack, (short)plcSlot,
- Int32.Parse(plcDatablockConfig[0]), Int32.Parse(plcDatablockConfig[1]), Int32.Parse(plcDatablockConfig[2]),
- plcTerminalCount, plcParkingSpaceCount);
- }
- else if (PLC.isConnected)
- {
- return true;
- }
- else { PLC.PLCConnect(); }
- }
- return false;
- }
- /// <summary>
- /// 系统关闭
- /// </summary>
- public static void Stop()
- {
- //关闭号牌机
- if (numMachineLinker != null) { numMachineLinker.Stop();}
- //关闭webServer
- if (webServer != null) { webServer.Stop();}
- //关闭数据库
- if (remoteDBOper != null) {remoteDBOper.DBClose(); }
- if (localDBOper != null) {localDBOper.DBClose(); }
- }
- }
- }
|