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
{
///
/// 中控系统总状态
///
public static bool globalStatus = false;
///
/// 初始化步骤
///
public static int initializeState = 0;
///
/// PLC对象句柄
///
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; }
///
/// 远程数据库操作句柄
///
public static DBOperation remoteDBOper;
///
/// 本地数据库操作句柄
///
public static DBOperation localDBOper;
///
/// 显示板操作对象句柄
///
public static BroadcastBoard allInOneMachine;
public static string allInOneMachineIP { get; set; }
public static int allInOneMachinePort { get; set; }
///
/// 号牌机操作句柄
///
public static INumMachineLinker numMachineLinker;
///
/// 本地web操作句柄
///
public static IWebServer webServer;
public static int webPort { get; set; }
///
/// 初始化各模块
///
///
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;
}
}
///
/// PLC初始化与连接
///
///
///
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;
}
///
/// 系统关闭
///
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(); }
}
}
}