SystemInitializer.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 centralController.WebServer;
  12. using db;
  13. using MySql.Data.MySqlClient;
  14. using NumMachine;
  15. using parkMonitor.LOG;
  16. using parkMonitor.tools;
  17. using PLCS7;
  18. using S7.Net;
  19. using Terminal;
  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. //Monitor.webServer = new MyWebServer();
  85. initStatus = initStatus && Monitor.webServer.Start(Monitor.webPort);
  86. //webServer初始化结束,进入状态4
  87. if (!initStatus) { return; }
  88. else { Monitor.initializeState = 4; }
  89. #region 测试数据库操作
  90. ////*******测试数据库操作
  91. //string sql = "select * from parkingrecords where userID = 1;";
  92. //MySqlDataReader reader = null;
  93. //object[] objs = null;
  94. //try
  95. //{
  96. // reader = Monitor.remoteDBOper.Query(sql);
  97. // if (reader != null && reader.Read())
  98. // {
  99. // objs = new object[reader.FieldCount];
  100. // reader.GetValues(objs);
  101. // Console.WriteLine("-------------");
  102. // for (int i = 0; i < objs.Length; i++)
  103. // {
  104. // Console.WriteLine(objs[i] + ",");
  105. // }
  106. // }
  107. //}
  108. //catch (Exception e) { Console.WriteLine(e.Message); }
  109. //string updateParkingRecordsSql = "update parkingrecords set parkingRecordsState = 6,realGetTime = '"
  110. // + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "',parkingPrice = '233' where receiptNum='11221122';";
  111. //List<string> list = new List<string>();
  112. //list.Add(updateParkingRecordsSql);
  113. //Monitor.localDBOper.UpdateTransaction(list);
  114. #endregion
  115. //初始化计费策略
  116. Monitor.SetNotification("读取计费策略...");
  117. PaymentScheme.ins = PaymentScheme.GetCurrentPaymentScheme(1);
  118. //初始化号牌机对象
  119. Monitor.SetNotification("扫描已接入号牌机...");
  120. Monitor.numMachineLinker = new NumMachineLinker(flpHandle);
  121. Monitor.numMachineLinker.Start();
  122. //初始化显示板对象,显示板udp面向无连接
  123. Monitor.SetNotification("初始化屏显...");
  124. BroadcastLinker.Start();
  125. Monitor.allInOneMachine = BroadcastLinker.ins;//new BroadcastBoard(Monitor.allInOneMachineIP, Monitor.allInOneMachinePort);
  126. //初始化广告实例
  127. Monitor.advertMgr = new AdvertManager();
  128. //initStatus = initStatus && AdvertMgrInit(3);
  129. //Monitor.AddNotification("test");
  130. Terminal.Terminal.Start();
  131. //系统初始化结束,进入状态5
  132. if (!initStatus) { return; }
  133. else { Monitor.initializeState = 5; }
  134. mt.EndTiming();
  135. Console.WriteLine("初始化耗时:" + mt.GetInterval().TotalSeconds);
  136. Log.WriteLog(LogType.process, LogFile.INFO, "初始化完成");
  137. switch (Monitor.initializeState)
  138. {
  139. case 0:
  140. Console.WriteLine("配置文件读写异常");
  141. Monitor.SetNotification("配置文件读写异常");
  142. break;
  143. case 1:
  144. Console.WriteLine("PLC初始化异常");
  145. Monitor.SetNotification("PLC初始化异常");
  146. break;
  147. case 2:
  148. Console.WriteLine("数据库初始化异常");
  149. Monitor.SetNotification("数据库初始化异常");
  150. break;
  151. case 3:
  152. Console.WriteLine("webServer初始化异常");
  153. Monitor.SetNotification("webServer初始化异常");
  154. break;
  155. case 4:
  156. Console.WriteLine("其他异常");
  157. Monitor.SetNotification("其他异常");
  158. break;
  159. case 5:
  160. Console.WriteLine("初始化成功");
  161. Monitor.SetNotification("clear");
  162. Monitor.SetNotification("初始化成功");
  163. break;
  164. }
  165. }
  166. /// <summary>
  167. /// PLC初始化与连接
  168. /// </summary>
  169. /// <param name="retryCount"></param>
  170. /// <returns></returns>
  171. internal bool PLCInit(int retryCount)
  172. {
  173. int temp = retryCount;
  174. //初始化PLC对象
  175. while (temp-- > 0)
  176. {
  177. if (Monitor.PLC == null)
  178. {
  179. Monitor.PLC = new PLCLinker(CpuType.S71500, Monitor.plcIPAddr, (short)Monitor.plcRack, (short)Monitor.plcSlot,
  180. Int32.Parse(Monitor.plcDatablockConfig[0]), Int32.Parse(Monitor.plcDatablockConfig[1]), Int32.Parse(Monitor.plcDatablockConfig[2]),
  181. Monitor.plcTerminalCount, Monitor.plcParkingSpaceCount);
  182. }
  183. else if (Monitor.PLC.isConnected)
  184. {
  185. return true;
  186. }
  187. else { Monitor.PLC.PLCConnect(); }
  188. }
  189. return false;
  190. }
  191. /// <summary>
  192. /// 初始化更新广告
  193. /// </summary>
  194. /// <param name="retryCount"></param>
  195. /// <returns></returns>
  196. internal bool AdvertMgrInit(int retryCount)
  197. {
  198. string adAlert = "";
  199. int temp = retryCount;
  200. while (temp-- > 0)
  201. {
  202. if (Monitor.advertMgr != null)
  203. {
  204. if (Monitor.advertMgr.UpdateAdvert(out adAlert))
  205. return true;
  206. }
  207. }
  208. return false;
  209. }
  210. /// <summary>
  211. /// 系统关闭
  212. /// </summary>
  213. internal void Stop()
  214. {
  215. //关闭号牌机
  216. if (Monitor.numMachineLinker != null) { Monitor.numMachineLinker.Stop(); }
  217. //关闭webServer
  218. if (Monitor.webServer != null) { Monitor.webServer.Stop(); }
  219. //关闭数据库
  220. if (Monitor.remoteDBOper != null) { Monitor.remoteDBOper.DBClose(); }
  221. if (Monitor.localDBOper != null) { Monitor.localDBOper.DBClose(); }
  222. Terminal.Terminal.Stop();
  223. }
  224. }
  225. }