SystemInitializer.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using allInOneMachine;
  9. using BroadcastModule;
  10. using centralController.advert;
  11. using centralController.model;
  12. using centralController.WebServer;
  13. using db;
  14. using MySql.Data.MySqlClient;
  15. using NumMachine;
  16. using parkMonitor.LOG;
  17. using parkMonitor.tools;
  18. using PLCS7;
  19. using snap7Enc;
  20. using Terminal;
  21. namespace Monitor
  22. {
  23. internal class SystemInitializer
  24. {
  25. /// <summary>
  26. /// 初始化各模块
  27. /// </summary>
  28. /// <returns></returns>
  29. internal void Init(IntPtr flpHandle)
  30. {
  31. Log.WriteLog(LogType.process, LogFile.INFO, "***************** 初始化开始 ****************");
  32. bool initStatus = false;
  33. int retryCount = 0;
  34. string remoteDBConnStr, localDBConnStr;
  35. int DBtimeout;
  36. MyTimer mt = new MyTimer();
  37. mt.StartTiming();
  38. try
  39. {
  40. Monitor.SetNotification("读取配置文件中...", parkMonitor.model.TextColor.Log);
  41. retryCount = Convert.ToInt32(ConfigurationManager.AppSettings.Get("retryCount"));
  42. Monitor.defaultLic = ConfigurationManager.AppSettings.Get("defaultLic");
  43. //数据库
  44. remoteDBConnStr = ConfigurationManager.AppSettings.Get("remoteDBConnStr");
  45. localDBConnStr = ConfigurationManager.AppSettings.Get("localDBConnStr");
  46. DBtimeout = Convert.ToInt32(ConfigurationManager.AppSettings.Get("DBtimeout"));
  47. //plc
  48. Monitor.plcIPAddr = ConfigurationManager.AppSettings.Get("plcIpAddress");
  49. Monitor.plcRack = Convert.ToInt32(ConfigurationManager.AppSettings.Get("plcRack"));
  50. Monitor.plcSlot = Convert.ToInt32(ConfigurationManager.AppSettings.Get("plcSlot"));
  51. Monitor.plcDatablockConfig = ConfigurationManager.AppSettings.Get("plcDatablockId").Split(',');
  52. Monitor.plcTerminalCount = Convert.ToInt32(ConfigurationManager.AppSettings.Get("plcTerminalCount"));
  53. Monitor.plcParkingSpaceCount = Convert.ToInt32(ConfigurationManager.AppSettings.Get("plcParkingSpaceCount"));
  54. Monitor.plcRefreshInterval = Convert.ToInt32(ConfigurationManager.AppSettings.Get("plcRefreshInterval"));
  55. //显示屏
  56. //Monitor.allInOneMachineIP = ConfigurationManager.AppSettings.Get("allInOneMachineIP");
  57. //Monitor.allInOneMachinePort = Convert.ToInt32(ConfigurationManager.AppSettings.Get("allInOneMachinePort"));
  58. //webServer端口
  59. Monitor.webPort = Convert.ToInt32(ConfigurationManager.AppSettings.Get("webPort"));
  60. //车库ID
  61. Monitor.garageID = Convert.ToInt32(ConfigurationManager.AppSettings.Get("garageID"));
  62. //广告路径
  63. Monitor.advertPath = ConfigurationManager.AppSettings.Get("advertPath");
  64. //配置文件读取结束,进入状态1
  65. Monitor.initializeState = 1;
  66. Monitor.globalStatus = false;
  67. initStatus = true;
  68. }
  69. catch (Exception e) { Console.WriteLine("初始化," + e.Message); return; }
  70. Monitor.SetNotification("初始化PLC连接...", parkMonitor.model.TextColor.Log);
  71. initStatus = initStatus && PLCInit(retryCount);
  72. //PLC初始化结束,进入状态2
  73. if (!initStatus) { Monitor.SetNotification("PLC连接失败,请检查设备", parkMonitor.model.TextColor.Error); return; }
  74. else { Monitor.initializeState = 2; }
  75. //初始化数据库对象
  76. Monitor.SetNotification("初始化数据库...", parkMonitor.model.TextColor.Log);
  77. Monitor.remoteDBOper = new DBOperation(remoteDBConnStr, DBtimeout);
  78. Monitor.localDBOper = new DBOperation(localDBConnStr, DBtimeout);
  79. initStatus = initStatus && Monitor.remoteDBOper.InitConnPooling(10);
  80. initStatus = initStatus && Monitor.localDBOper.InitConnPooling(10);
  81. //数据库初始化结束,进入状态3
  82. if (!initStatus) { return; }
  83. else { Monitor.initializeState = 3; }
  84. //初始化web对象
  85. Monitor.SetNotification("初始化云端连接...", parkMonitor.model.TextColor.Log);
  86. //Monitor.webServer = new CentralForWeb();
  87. Monitor.webServer = new MyWebServer();
  88. initStatus = initStatus && Monitor.webServer.Start(Monitor.webPort);
  89. //webServer初始化结束,进入状态4
  90. if (!initStatus) { return; }
  91. else { Monitor.initializeState = 4; }
  92. #region 测试数据库操作
  93. ////*******测试数据库操作
  94. //string sql = "select * from parkingrecords where userID = 1;";
  95. //MySqlDataReader reader = null;
  96. //object[] objs = null;
  97. //try
  98. //{
  99. // reader = Monitor.remoteDBOper.Query(sql);
  100. // if (reader != null && reader.Read())
  101. // {
  102. // objs = new object[reader.FieldCount];
  103. // reader.GetValues(objs);
  104. // Console.WriteLine("-------------");
  105. // for (int i = 0; i < objs.Length; i++)
  106. // {
  107. // Console.WriteLine(objs[i] + ",");
  108. // }
  109. // }
  110. //}
  111. //catch (Exception e) { Console.WriteLine(e.Message); }
  112. //string updateParkingRecordsSql = "update parkingrecords set parkingRecordsState = 6,realGetTime = '"
  113. // + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "',parkingPrice = '233' where receiptNum='11221122';";
  114. //List<string> list = new List<string>();
  115. //list.Add(updateParkingRecordsSql);
  116. //Monitor.localDBOper.UpdateTransaction(list);
  117. #endregion
  118. //初始化计费策略
  119. Monitor.SetNotification("读取计费策略...", parkMonitor.model.TextColor.Log);
  120. PaymentScheme.ins = PaymentScheme.GetCurrentPaymentScheme(1);
  121. //初始化号牌机对象
  122. Monitor.SetNotification("扫描已接入号牌机...", parkMonitor.model.TextColor.Log);
  123. Monitor.numMachineLinker = new NumMachineLinker(flpHandle);
  124. Monitor.numMachineLinker.Start();
  125. //初始化显示板对象,显示板udp面向无连接
  126. Monitor.SetNotification("初始化屏显...", parkMonitor.model.TextColor.Log);
  127. //BroadcastLinker.Start();
  128. //Monitor.allInOneMachine = BroadcastLinker.ins;//new BroadcastBoard(Monitor.allInOneMachineIP, Monitor.allInOneMachinePort);
  129. //初始化广告实例
  130. Monitor.advertMgr = new AdvertManager();
  131. //initStatus = initStatus && AdvertMgrInit(3);
  132. //Monitor.AddNotification("test");
  133. Terminal.Terminal.Start();
  134. //系统初始化结束,进入状态5
  135. if (!initStatus) { return; }
  136. else { Monitor.initializeState = 5; }
  137. mt.EndTiming();
  138. Console.WriteLine("初始化耗时:" + mt.GetInterval().TotalSeconds);
  139. Log.WriteLog(LogType.process, LogFile.INFO, "初始化完成");
  140. switch (Monitor.initializeState)
  141. {
  142. case 0:
  143. Console.WriteLine("配置文件读写异常");
  144. Monitor.SetNotification("配置文件读写异常", parkMonitor.model.TextColor.Error);
  145. break;
  146. case 1:
  147. Console.WriteLine("PLC初始化异常");
  148. Monitor.SetNotification("PLC初始化异常", parkMonitor.model.TextColor.Error);
  149. break;
  150. case 2:
  151. Console.WriteLine("数据库初始化异常");
  152. Monitor.SetNotification("数据库初始化异常", parkMonitor.model.TextColor.Error);
  153. break;
  154. case 3:
  155. Console.WriteLine("webServer初始化异常");
  156. Monitor.SetNotification("webServer初始化异常", parkMonitor.model.TextColor.Error);
  157. break;
  158. case 4:
  159. Console.WriteLine("其他异常");
  160. Monitor.SetNotification("其他异常", parkMonitor.model.TextColor.Error);
  161. break;
  162. case 5:
  163. Console.WriteLine("初始化成功");
  164. Monitor.SetNotification("clear");
  165. Monitor.SetNotification("初始化成功,耗时:" + String.Format("{0:N2}秒" ,mt.GetInterval().TotalSeconds), parkMonitor.model.TextColor.Log);
  166. Monitor.globalStatus = true;
  167. break;
  168. }
  169. //Monitor.SetNotification("aaaaaaaaaaaaaaaa", parkMonitor.model.TextColor.Info);
  170. //Monitor.SetNotification("bbbbbbbbbbbbbbbb", parkMonitor.model.TextColor.Log);
  171. //Monitor.SetNotification("cccccccccccccccc", parkMonitor.model.TextColor.Warning);
  172. //Monitor.SetNotification("dddddddddddddddd", parkMonitor.model.TextColor.Error);
  173. //Task.Factory.StartNew(()=>
  174. //{
  175. // int count = 30;
  176. // while (count-- > 0)
  177. // {
  178. // Monitor.SetNotification("cccccccccccccccc", parkMonitor.model.TextColor.Warning);
  179. // Thread.Sleep(1500);
  180. // }
  181. //});
  182. }
  183. /// <summary>
  184. /// PLC初始化与连接
  185. /// </summary>
  186. /// <param name="retryCount"></param>
  187. /// <returns></returns>
  188. internal bool PLCInit(int retryCount)
  189. {
  190. int temp = retryCount;
  191. //初始化PLC对象
  192. while (temp-- > 0)
  193. {
  194. if (Monitor.PLC == null)
  195. {
  196. Monitor.PLC = new PLCLinker(CpuType.S71500, Monitor.plcIPAddr, (short)Monitor.plcRack, (short)Monitor.plcSlot,
  197. Int32.Parse(Monitor.plcDatablockConfig[0]), Int32.Parse(Monitor.plcDatablockConfig[1]), Int32.Parse(Monitor.plcDatablockConfig[2]),
  198. Monitor.plcTerminalCount, Monitor.plcParkingSpaceCount);
  199. }
  200. //启动心跳进程
  201. Thread heartbeatThread = new Thread(()=> {
  202. while (!Monitor.isClosing)
  203. {
  204. if (Monitor.PLC != null && Monitor.PLC.isConnected)
  205. {
  206. Random rnd = new Random();
  207. MainBlockStru mbs = new MainBlockStru();
  208. mbs.centralHearbeat = (short)rnd.Next(100, 999);
  209. mbs.licenseReceived = (short)-1;
  210. mbs.bookParkCmd = (short)-1;
  211. mbs.bookFetchCmd = (short)-1;
  212. mbs.processCompleted = (short)-1;
  213. Monitor.PLC.WriteToPLC(mbs, PLCDataType.central);
  214. }
  215. Thread.Sleep(500);
  216. }
  217. });
  218. heartbeatThread.Start();
  219. if (Monitor.PLC.isConnected)
  220. {
  221. return true;
  222. }
  223. else { Monitor.PLC.PLCConnect(); }
  224. }
  225. return false;
  226. }
  227. /// <summary>
  228. /// 初始化更新广告
  229. /// </summary>
  230. /// <param name="retryCount"></param>
  231. /// <returns></returns>
  232. internal bool AdvertMgrInit(int retryCount)
  233. {
  234. string adAlert = "";
  235. int temp = retryCount;
  236. while (temp-- > 0)
  237. {
  238. if (Monitor.advertMgr != null)
  239. {
  240. if (Monitor.advertMgr.UpdateAdvert(out adAlert))
  241. return true;
  242. }
  243. }
  244. return false;
  245. }
  246. /// <summary>
  247. /// 系统关闭
  248. /// </summary>
  249. internal void Stop()
  250. {
  251. //关闭号牌机
  252. if (Monitor.numMachineLinker != null) { Monitor.numMachineLinker.Stop(); }
  253. //关闭webServer
  254. if (Monitor.webServer != null) { Monitor.webServer.Stop(); }
  255. //关闭数据库
  256. if (Monitor.remoteDBOper != null) { Monitor.remoteDBOper.DBClose(); }
  257. if (Monitor.localDBOper != null) { Monitor.localDBOper.DBClose(); }
  258. Terminal.Terminal.Stop();
  259. }
  260. }
  261. }