SystemInitializer.cs 13 KB

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