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 S7.Net;
  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. //数据库
  43. remoteDBConnStr = ConfigurationManager.AppSettings.Get("remoteDBConnStr");
  44. localDBConnStr = ConfigurationManager.AppSettings.Get("localDBConnStr");
  45. DBtimeout = Convert.ToInt32(ConfigurationManager.AppSettings.Get("DBtimeout"));
  46. //plc
  47. Monitor.plcIPAddr = ConfigurationManager.AppSettings.Get("plcIpAddress");
  48. Monitor.plcRack = Convert.ToInt32(ConfigurationManager.AppSettings.Get("plcRack"));
  49. Monitor.plcSlot = Convert.ToInt32(ConfigurationManager.AppSettings.Get("plcSlot"));
  50. Monitor.plcDatablockConfig = ConfigurationManager.AppSettings.Get("plcDatablockId").Split(',');
  51. Monitor.plcTerminalCount = Convert.ToInt32(ConfigurationManager.AppSettings.Get("plcTerminalCount"));
  52. Monitor.plcParkingSpaceCount = Convert.ToInt32(ConfigurationManager.AppSettings.Get("plcParkingSpaceCount"));
  53. Monitor.plcRefreshInterval = Convert.ToInt32(ConfigurationManager.AppSettings.Get("plcRefreshInterval"));
  54. //显示屏
  55. //Monitor.allInOneMachineIP = ConfigurationManager.AppSettings.Get("allInOneMachineIP");
  56. //Monitor.allInOneMachinePort = Convert.ToInt32(ConfigurationManager.AppSettings.Get("allInOneMachinePort"));
  57. //webServer端口
  58. Monitor.webPort = Convert.ToInt32(ConfigurationManager.AppSettings.Get("webPort"));
  59. //车库ID
  60. Monitor.garageID = Convert.ToInt32(ConfigurationManager.AppSettings.Get("garageID"));
  61. //广告路径
  62. Monitor.advertPath = ConfigurationManager.AppSettings.Get("advertPath");
  63. //配置文件读取结束,进入状态1
  64. Monitor.initializeState = 1;
  65. initStatus = true;
  66. }
  67. catch (Exception e) { Console.WriteLine("初始化," + e.Message); return; }
  68. Monitor.SetNotification("初始化PLC连接...",parkMonitor.model.TextColor.Log);
  69. initStatus = initStatus && PLCInit(retryCount);
  70. //PLC初始化结束,进入状态2
  71. if (!initStatus) { Monitor.SetNotification("PLC连接失败,请检查设备",parkMonitor.model.TextColor.Error); return; }
  72. else { Monitor.initializeState = 2; }
  73. //初始化数据库对象
  74. Monitor.SetNotification("初始化数据库...",parkMonitor.model.TextColor.Log);
  75. Monitor.remoteDBOper = new DBOperation(remoteDBConnStr, DBtimeout);
  76. Monitor.localDBOper = new DBOperation(localDBConnStr, DBtimeout);
  77. initStatus = initStatus && Monitor.remoteDBOper.InitConnPooling(10);
  78. initStatus = initStatus && Monitor.localDBOper.InitConnPooling(10);
  79. //数据库初始化结束,进入状态3
  80. if (!initStatus) { return; }
  81. else { Monitor.initializeState = 3; }
  82. //初始化web对象
  83. Monitor.SetNotification("初始化云端连接...",parkMonitor.model.TextColor.Log);
  84. //Monitor.webServer = new CentralForWeb();
  85. Monitor.webServer = new MyWebServer();
  86. initStatus = initStatus && Monitor.webServer.Start(Monitor.webPort);
  87. //webServer初始化结束,进入状态4
  88. if (!initStatus) { return; }
  89. else { Monitor.initializeState = 4; }
  90. #region 测试数据库操作
  91. ////*******测试数据库操作
  92. //string sql = "select * from parkingrecords where userID = 1;";
  93. //MySqlDataReader reader = null;
  94. //object[] objs = null;
  95. //try
  96. //{
  97. // reader = Monitor.remoteDBOper.Query(sql);
  98. // if (reader != null && reader.Read())
  99. // {
  100. // objs = new object[reader.FieldCount];
  101. // reader.GetValues(objs);
  102. // Console.WriteLine("-------------");
  103. // for (int i = 0; i < objs.Length; i++)
  104. // {
  105. // Console.WriteLine(objs[i] + ",");
  106. // }
  107. // }
  108. //}
  109. //catch (Exception e) { Console.WriteLine(e.Message); }
  110. //string updateParkingRecordsSql = "update parkingrecords set parkingRecordsState = 6,realGetTime = '"
  111. // + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "',parkingPrice = '233' where receiptNum='11221122';";
  112. //List<string> list = new List<string>();
  113. //list.Add(updateParkingRecordsSql);
  114. //Monitor.localDBOper.UpdateTransaction(list);
  115. #endregion
  116. //初始化计费策略
  117. Monitor.SetNotification("读取计费策略...",parkMonitor.model.TextColor.Log);
  118. PaymentScheme.ins = PaymentScheme.GetCurrentPaymentScheme(1);
  119. //初始化号牌机对象
  120. Monitor.SetNotification("扫描已接入号牌机...",parkMonitor.model.TextColor.Log);
  121. Monitor.numMachineLinker = new NumMachineLinker(flpHandle);
  122. Monitor.numMachineLinker.Start();
  123. //初始化显示板对象,显示板udp面向无连接
  124. Monitor.SetNotification("初始化屏显...",parkMonitor.model.TextColor.Log);
  125. //BroadcastLinker.Start();
  126. //Monitor.allInOneMachine = BroadcastLinker.ins;//new BroadcastBoard(Monitor.allInOneMachineIP, Monitor.allInOneMachinePort);
  127. //初始化广告实例
  128. Monitor.advertMgr = new AdvertManager();
  129. //initStatus = initStatus && AdvertMgrInit(3);
  130. //Monitor.AddNotification("test");
  131. Terminal.Terminal.Start();
  132. //系统初始化结束,进入状态5
  133. if (!initStatus) { return; }
  134. else { Monitor.initializeState = 5; }
  135. mt.EndTiming();
  136. Console.WriteLine("初始化耗时:" + mt.GetInterval().TotalSeconds);
  137. Log.WriteLog(LogType.process, LogFile.INFO, "初始化完成");
  138. switch (Monitor.initializeState)
  139. {
  140. case 0:
  141. Console.WriteLine("配置文件读写异常");
  142. Monitor.SetNotification("配置文件读写异常",parkMonitor.model.TextColor.Error);
  143. break;
  144. case 1:
  145. Console.WriteLine("PLC初始化异常");
  146. Monitor.SetNotification("PLC初始化异常",parkMonitor.model.TextColor.Error);
  147. break;
  148. case 2:
  149. Console.WriteLine("数据库初始化异常");
  150. Monitor.SetNotification("数据库初始化异常",parkMonitor.model.TextColor.Error);
  151. break;
  152. case 3:
  153. Console.WriteLine("webServer初始化异常");
  154. Monitor.SetNotification("webServer初始化异常",parkMonitor.model.TextColor.Error);
  155. break;
  156. case 4:
  157. Console.WriteLine("其他异常");
  158. Monitor.SetNotification("其他异常",parkMonitor.model.TextColor.Error);
  159. break;
  160. case 5:
  161. Console.WriteLine("初始化成功");
  162. Monitor.SetNotification("clear");
  163. Monitor.SetNotification("初始化成功",parkMonitor.model.TextColor.Log);
  164. break;
  165. }
  166. //Monitor.SetNotification("aaaaaaaaaaaaaaaa", parkMonitor.model.TextColor.Info);
  167. //Monitor.SetNotification("bbbbbbbbbbbbbbbb", parkMonitor.model.TextColor.Log);
  168. //Monitor.SetNotification("cccccccccccccccc", parkMonitor.model.TextColor.Warning);
  169. //Monitor.SetNotification("dddddddddddddddd", parkMonitor.model.TextColor.Error);
  170. //Task.Factory.StartNew(()=>
  171. //{
  172. // int count = 30;
  173. // while (count-- > 0)
  174. // {
  175. // Monitor.SetNotification("cccccccccccccccc", parkMonitor.model.TextColor.Warning);
  176. // Thread.Sleep(1500);
  177. // }
  178. //});
  179. }
  180. /// <summary>
  181. /// PLC初始化与连接
  182. /// </summary>
  183. /// <param name="retryCount"></param>
  184. /// <returns></returns>
  185. internal bool PLCInit(int retryCount)
  186. {
  187. int temp = retryCount;
  188. //初始化PLC对象
  189. while (temp-- > 0)
  190. {
  191. if (Monitor.PLC == null)
  192. {
  193. Monitor.PLC = new PLCLinker(CpuType.S71500, Monitor.plcIPAddr, (short)Monitor.plcRack, (short)Monitor.plcSlot,
  194. Int32.Parse(Monitor.plcDatablockConfig[0]), Int32.Parse(Monitor.plcDatablockConfig[1]), Int32.Parse(Monitor.plcDatablockConfig[2]),
  195. Monitor.plcTerminalCount, Monitor.plcParkingSpaceCount);
  196. }
  197. else if (Monitor.PLC.isConnected)
  198. {
  199. Random rnd = new Random();
  200. //启动心跳进程
  201. Task.Factory.StartNew(() =>
  202. {
  203. while (!Monitor.isClosing)
  204. {
  205. if (Monitor.PLC.isConnected)
  206. {
  207. MainBlockStru mbs = new MainBlockStru();
  208. List<object> list = Monitor.PLC.ReadFromPLC(PLCDataType.central, 0);
  209. if (list.Count != 0)
  210. {
  211. mbs = (MainBlockStru)list[0];
  212. mbs.centralHearbeat = (short)rnd.Next(100,999);
  213. mbs.licenseReceived = (short)-1;
  214. mbs.bookParkCmd = (short)-1;
  215. mbs.bookFetchCmd = (short)-1;
  216. Monitor.PLC.WriteToPLC(mbs, PLCDataType.central);
  217. }
  218. }
  219. Thread.Sleep(1000);
  220. }
  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. }