SystemInitializer.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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("读取配置文件中...");
  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连接...");
  69. initStatus = initStatus && PLCInit(retryCount);
  70. //PLC初始化结束,进入状态2
  71. if (!initStatus) { Monitor.SetNotification("PLC连接失败,请检查设备"); return; }
  72. else { Monitor.initializeState = 2; }
  73. //初始化数据库对象
  74. Monitor.SetNotification("初始化数据库...");
  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("初始化云端连接...");
  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("读取计费策略...");
  118. PaymentScheme.ins = PaymentScheme.GetCurrentPaymentScheme(1);
  119. //初始化号牌机对象
  120. Monitor.SetNotification("扫描已接入号牌机...");
  121. Monitor.numMachineLinker = new NumMachineLinker(flpHandle);
  122. Monitor.numMachineLinker.Start();
  123. //初始化显示板对象,显示板udp面向无连接
  124. Monitor.SetNotification("初始化屏显...");
  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("配置文件读写异常");
  143. break;
  144. case 1:
  145. Console.WriteLine("PLC初始化异常");
  146. Monitor.SetNotification("PLC初始化异常");
  147. break;
  148. case 2:
  149. Console.WriteLine("数据库初始化异常");
  150. Monitor.SetNotification("数据库初始化异常");
  151. break;
  152. case 3:
  153. Console.WriteLine("webServer初始化异常");
  154. Monitor.SetNotification("webServer初始化异常");
  155. break;
  156. case 4:
  157. Console.WriteLine("其他异常");
  158. Monitor.SetNotification("其他异常");
  159. break;
  160. case 5:
  161. Console.WriteLine("初始化成功");
  162. Monitor.SetNotification("clear");
  163. Monitor.SetNotification("初始化成功");
  164. break;
  165. }
  166. }
  167. /// <summary>
  168. /// PLC初始化与连接
  169. /// </summary>
  170. /// <param name="retryCount"></param>
  171. /// <returns></returns>
  172. internal bool PLCInit(int retryCount)
  173. {
  174. int temp = retryCount;
  175. //初始化PLC对象
  176. while (temp-- > 0)
  177. {
  178. if (Monitor.PLC == null)
  179. {
  180. Monitor.PLC = new PLCLinker(CpuType.S71500, Monitor.plcIPAddr, (short)Monitor.plcRack, (short)Monitor.plcSlot,
  181. Int32.Parse(Monitor.plcDatablockConfig[0]), Int32.Parse(Monitor.plcDatablockConfig[1]), Int32.Parse(Monitor.plcDatablockConfig[2]),
  182. Monitor.plcTerminalCount, Monitor.plcParkingSpaceCount);
  183. }
  184. else if (Monitor.PLC.isConnected)
  185. {
  186. //启动心跳进程
  187. Task.Factory.StartNew(() =>
  188. {
  189. while (!Monitor.isClosing)
  190. {
  191. if (Monitor.PLC.isConnected)
  192. {
  193. MainBlockStru mbs = new MainBlockStru();
  194. List<object> list = Monitor.PLC.ReadFromPLC(PLCDataType.central, 0);
  195. if (list.Count != 0)
  196. {
  197. mbs = (MainBlockStru)list[0];
  198. mbs.centralHearbeat = (mbs.centralHearbeat == (short)254) ? (short)255 : (short)254;
  199. mbs.licenseReceived = (short)-1;
  200. Monitor.PLC.WriteToPLC(mbs, PLCDataType.central);
  201. }
  202. }
  203. Thread.Sleep(1000);
  204. }
  205. });
  206. return true;
  207. }
  208. else { Monitor.PLC.PLCConnect(); }
  209. }
  210. return false;
  211. }
  212. /// <summary>
  213. /// 初始化更新广告
  214. /// </summary>
  215. /// <param name="retryCount"></param>
  216. /// <returns></returns>
  217. internal bool AdvertMgrInit(int retryCount)
  218. {
  219. string adAlert = "";
  220. int temp = retryCount;
  221. while (temp-- > 0)
  222. {
  223. if (Monitor.advertMgr != null)
  224. {
  225. if (Monitor.advertMgr.UpdateAdvert(out adAlert))
  226. return true;
  227. }
  228. }
  229. return false;
  230. }
  231. /// <summary>
  232. /// 系统关闭
  233. /// </summary>
  234. internal void Stop()
  235. {
  236. //关闭号牌机
  237. if (Monitor.numMachineLinker != null) { Monitor.numMachineLinker.Stop(); }
  238. //关闭webServer
  239. if (Monitor.webServer != null) { Monitor.webServer.Stop(); }
  240. //关闭数据库
  241. if (Monitor.remoteDBOper != null) { Monitor.remoteDBOper.DBClose(); }
  242. if (Monitor.localDBOper != null) { Monitor.localDBOper.DBClose(); }
  243. Terminal.Terminal.Stop();
  244. }
  245. }
  246. }