SystemInitializer.cs 10 KB

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