123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- 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 snap7Enc;
- using Terminal;
- namespace Monitor
- {
- internal class SystemInitializer
- {
- /// <summary>
- /// 初始化各模块
- /// </summary>
- /// <returns></returns>
- 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"));
- Monitor.defaultLic = ConfigurationManager.AppSettings.Get("defaultLic");
- //数据库
- 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;
- Monitor.globalStatus = false;
- 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<string> list = new List<string>();
- //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("初始化成功,耗时:" + String.Format("{0:N2}秒" ,mt.GetInterval().TotalSeconds), parkMonitor.model.TextColor.Log);
- Monitor.globalStatus = true;
- 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);
- // }
- //});
- }
- /// <summary>
- /// PLC初始化与连接
- /// </summary>
- /// <param name="retryCount"></param>
- /// <returns></returns>
- 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);
- }
- //启动心跳进程
- Thread heartbeatThread = new Thread(()=> {
- while (!Monitor.isClosing)
- {
- if (Monitor.PLC != null && Monitor.PLC.isConnected)
- {
- Random rnd = new Random();
- MainBlockStru mbs = new MainBlockStru();
- mbs.centralHearbeat = (short)rnd.Next(100, 999);
- mbs.licenseReceived = (short)-1;
- mbs.bookParkCmd = (short)-1;
- mbs.bookFetchCmd = (short)-1;
- mbs.processCompleted = (short)-1;
- Monitor.PLC.WriteToPLC(mbs, PLCDataType.central);
- }
- Thread.Sleep(500);
- }
- });
- heartbeatThread.Start();
- if (Monitor.PLC.isConnected)
- {
- return true;
- }
- else { Monitor.PLC.PLCConnect(); }
- }
- return false;
- }
- /// <summary>
- /// 初始化更新广告
- /// </summary>
- /// <param name="retryCount"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 系统关闭
- /// </summary>
- 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();
- }
- }
- }
|