using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using allInOneMachine; using BroadcastModule; using centralController.advert; using centralController.model; using centralController.WebServer; using db; using MySql.Data.MySqlClient; using NumMachine; using parkMonitor.LOG; using parkMonitor.tools; using PLCS7; using S7.Net; using Terminal; namespace Monitor { internal class SystemInitializer { /// /// 初始化各模块 /// /// internal void Init(IntPtr flpHandle) { Log.WriteLog(LogType.process, LogFile.INFO, "***************** 初始化开始 ****************"); bool initStatus = false; int retryCount = 0; string remoteDBConnStr, localDBConnStr; int DBtimeout; MyTimer mt = new MyTimer(); mt.StartTiming(); try { Monitor.SetNotification("读取配置文件中...",parkMonitor.model.TextColor.Log); 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 Monitor.plcIPAddr = ConfigurationManager.AppSettings.Get("plcIpAddress"); Monitor.plcRack = Convert.ToInt32(ConfigurationManager.AppSettings.Get("plcRack")); Monitor.plcSlot = Convert.ToInt32(ConfigurationManager.AppSettings.Get("plcSlot")); Monitor.plcDatablockConfig = ConfigurationManager.AppSettings.Get("plcDatablockId").Split(','); Monitor.plcTerminalCount = Convert.ToInt32(ConfigurationManager.AppSettings.Get("plcTerminalCount")); Monitor.plcParkingSpaceCount = Convert.ToInt32(ConfigurationManager.AppSettings.Get("plcParkingSpaceCount")); Monitor.plcRefreshInterval = Convert.ToInt32(ConfigurationManager.AppSettings.Get("plcRefreshInterval")); //显示屏 //Monitor.allInOneMachineIP = ConfigurationManager.AppSettings.Get("allInOneMachineIP"); //Monitor.allInOneMachinePort = Convert.ToInt32(ConfigurationManager.AppSettings.Get("allInOneMachinePort")); //webServer端口 Monitor.webPort = Convert.ToInt32(ConfigurationManager.AppSettings.Get("webPort")); //车库ID Monitor.garageID = Convert.ToInt32(ConfigurationManager.AppSettings.Get("garageID")); //广告路径 Monitor.advertPath = ConfigurationManager.AppSettings.Get("advertPath"); //配置文件读取结束,进入状态1 Monitor.initializeState = 1; initStatus = true; } catch (Exception e) { Console.WriteLine("初始化," + e.Message); return; } Monitor.SetNotification("初始化PLC连接...",parkMonitor.model.TextColor.Log); initStatus = initStatus && PLCInit(retryCount); //PLC初始化结束,进入状态2 if (!initStatus) { Monitor.SetNotification("PLC连接失败,请检查设备",parkMonitor.model.TextColor.Error); return; } else { Monitor.initializeState = 2; } //初始化数据库对象 Monitor.SetNotification("初始化数据库...",parkMonitor.model.TextColor.Log); Monitor.remoteDBOper = new DBOperation(remoteDBConnStr, DBtimeout); Monitor.localDBOper = new DBOperation(localDBConnStr, DBtimeout); initStatus = initStatus && Monitor.remoteDBOper.InitConnPooling(10); initStatus = initStatus && Monitor.localDBOper.InitConnPooling(10); //数据库初始化结束,进入状态3 if (!initStatus) { return; } else { Monitor.initializeState = 3; } //初始化web对象 Monitor.SetNotification("初始化云端连接...",parkMonitor.model.TextColor.Log); //Monitor.webServer = new CentralForWeb(); Monitor.webServer = new MyWebServer(); initStatus = initStatus && Monitor.webServer.Start(Monitor.webPort); //webServer初始化结束,进入状态4 if (!initStatus) { return; } else { Monitor.initializeState = 4; } #region 测试数据库操作 ////*******测试数据库操作 //string sql = "select * from parkingrecords where userID = 1;"; //MySqlDataReader reader = null; //object[] objs = null; //try //{ // reader = Monitor.remoteDBOper.Query(sql); // if (reader != null && reader.Read()) // { // objs = new object[reader.FieldCount]; // reader.GetValues(objs); // Console.WriteLine("-------------"); // for (int i = 0; i < objs.Length; i++) // { // Console.WriteLine(objs[i] + ","); // } // } //} //catch (Exception e) { Console.WriteLine(e.Message); } //string updateParkingRecordsSql = "update parkingrecords set parkingRecordsState = 6,realGetTime = '" // + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "',parkingPrice = '233' where receiptNum='11221122';"; //List list = new List(); //list.Add(updateParkingRecordsSql); //Monitor.localDBOper.UpdateTransaction(list); #endregion //初始化计费策略 Monitor.SetNotification("读取计费策略...",parkMonitor.model.TextColor.Log); PaymentScheme.ins = PaymentScheme.GetCurrentPaymentScheme(1); //初始化号牌机对象 Monitor.SetNotification("扫描已接入号牌机...",parkMonitor.model.TextColor.Log); Monitor.numMachineLinker = new NumMachineLinker(flpHandle); Monitor.numMachineLinker.Start(); //初始化显示板对象,显示板udp面向无连接 Monitor.SetNotification("初始化屏显...",parkMonitor.model.TextColor.Log); //BroadcastLinker.Start(); //Monitor.allInOneMachine = BroadcastLinker.ins;//new BroadcastBoard(Monitor.allInOneMachineIP, Monitor.allInOneMachinePort); //初始化广告实例 Monitor.advertMgr = new AdvertManager(); //initStatus = initStatus && AdvertMgrInit(3); //Monitor.AddNotification("test"); Terminal.Terminal.Start(); //系统初始化结束,进入状态5 if (!initStatus) { return; } else { Monitor.initializeState = 5; } mt.EndTiming(); Console.WriteLine("初始化耗时:" + mt.GetInterval().TotalSeconds); Log.WriteLog(LogType.process, LogFile.INFO, "初始化完成"); switch (Monitor.initializeState) { case 0: Console.WriteLine("配置文件读写异常"); Monitor.SetNotification("配置文件读写异常",parkMonitor.model.TextColor.Error); break; case 1: Console.WriteLine("PLC初始化异常"); Monitor.SetNotification("PLC初始化异常",parkMonitor.model.TextColor.Error); break; case 2: Console.WriteLine("数据库初始化异常"); Monitor.SetNotification("数据库初始化异常",parkMonitor.model.TextColor.Error); break; case 3: Console.WriteLine("webServer初始化异常"); Monitor.SetNotification("webServer初始化异常",parkMonitor.model.TextColor.Error); break; case 4: Console.WriteLine("其他异常"); Monitor.SetNotification("其他异常",parkMonitor.model.TextColor.Error); break; case 5: Console.WriteLine("初始化成功"); Monitor.SetNotification("clear"); Monitor.SetNotification("初始化成功",parkMonitor.model.TextColor.Log); break; } //Monitor.SetNotification("aaaaaaaaaaaaaaaa", parkMonitor.model.TextColor.Info); //Monitor.SetNotification("bbbbbbbbbbbbbbbb", parkMonitor.model.TextColor.Log); //Monitor.SetNotification("cccccccccccccccc", parkMonitor.model.TextColor.Warning); //Monitor.SetNotification("dddddddddddddddd", parkMonitor.model.TextColor.Error); //Task.Factory.StartNew(()=> //{ // int count = 30; // while (count-- > 0) // { // Monitor.SetNotification("cccccccccccccccc", parkMonitor.model.TextColor.Warning); // Thread.Sleep(1500); // } //}); } /// /// PLC初始化与连接 /// /// /// internal bool PLCInit(int retryCount) { int temp = retryCount; //初始化PLC对象 while (temp-- > 0) { if (Monitor.PLC == null) { Monitor.PLC = new PLCLinker(CpuType.S71500, Monitor.plcIPAddr, (short)Monitor.plcRack, (short)Monitor.plcSlot, Int32.Parse(Monitor.plcDatablockConfig[0]), Int32.Parse(Monitor.plcDatablockConfig[1]), Int32.Parse(Monitor.plcDatablockConfig[2]), Monitor.plcTerminalCount, Monitor.plcParkingSpaceCount); } else if (Monitor.PLC.isConnected) { Random rnd = new Random(); //启动心跳进程 Task.Factory.StartNew(() => { while (!Monitor.isClosing) { if (Monitor.PLC.isConnected) { MainBlockStru mbs = new MainBlockStru(); List list = Monitor.PLC.ReadFromPLC(PLCDataType.central, 0); if (list.Count != 0) { mbs = (MainBlockStru)list[0]; mbs.centralHearbeat = (short)rnd.Next(100,999); mbs.licenseReceived = (short)-1; mbs.bookParkCmd = (short)-1; mbs.bookFetchCmd = (short)-1; Monitor.PLC.WriteToPLC(mbs, PLCDataType.central); } } Thread.Sleep(1000); } }); return true; } else { Monitor.PLC.PLCConnect(); } } return false; } /// /// 初始化更新广告 /// /// /// internal bool AdvertMgrInit(int retryCount) { string adAlert = ""; int temp = retryCount; while (temp-- > 0) { if (Monitor.advertMgr != null) { if (Monitor.advertMgr.UpdateAdvert(out adAlert)) return true; } } return false; } /// /// 系统关闭 /// internal void Stop() { //关闭号牌机 if (Monitor.numMachineLinker != null) { Monitor.numMachineLinker.Stop(); } //关闭webServer if (Monitor.webServer != null) { Monitor.webServer.Stop(); } //关闭数据库 if (Monitor.remoteDBOper != null) { Monitor.remoteDBOper.DBClose(); } if (Monitor.localDBOper != null) { Monitor.localDBOper.DBClose(); } Terminal.Terminal.Stop(); } } }