SystemInitializer.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 BroadcastModule;
  8. using DatabaseDLL;
  9. using NumMachine;
  10. using parkMonitor.tools;
  11. using PLCS7;
  12. using S7.Net;
  13. using WebServer;
  14. namespace Entry
  15. {
  16. public class SystemInitializer
  17. {
  18. /// <summary>
  19. /// 中控系统总状态
  20. /// </summary>
  21. public static bool globalStatus = false;
  22. /// <summary>
  23. /// 初始化步骤
  24. /// </summary>
  25. public static int initializeState = 0;
  26. /// <summary>
  27. /// PLC对象句柄
  28. /// </summary>
  29. public static AbstractPLCLinker PLC = null;
  30. public static string plcIPAddr { get; set; }
  31. public static int plcRack { get; set; }
  32. public static int plcSlot { get; set; }
  33. public static string[] plcDatablockConfig { get; set; }
  34. public static int plcTerminalCount { get; set; }
  35. public static int plcParkingSpaceCount { get; set; }
  36. public static int plcRefreshInterval { get; set; }
  37. /// <summary>
  38. /// 远程数据库操作句柄
  39. /// </summary>
  40. public static DBOperation remoteDBOper;
  41. /// <summary>
  42. /// 本地数据库操作句柄
  43. /// </summary>
  44. public static DBOperation localDBOper;
  45. /// <summary>
  46. /// 显示板操作对象句柄
  47. /// </summary>
  48. public static BroadcastBoard allInOneMachine;
  49. public static string allInOneMachineIP { get; set; }
  50. public static int allInOneMachinePort { get; set; }
  51. /// <summary>
  52. /// 号牌机操作句柄
  53. /// </summary>
  54. public static INumMachineLinker numMachineLinker;
  55. /// <summary>
  56. /// 本地web操作句柄
  57. /// </summary>
  58. public static IWebServer webServer;
  59. public static int webPort { get; set; }
  60. /// <summary>
  61. /// 初始化各模块
  62. /// </summary>
  63. /// <returns></returns>
  64. public static void Init(IntPtr flpHandle)
  65. {
  66. bool initStatus = false;
  67. int retryCount = 0;
  68. string remoteDBConnStr, localDBConnStr;
  69. int DBtimeout;
  70. int garageID;
  71. MyTimer mt = new MyTimer();
  72. mt.StartTiming();
  73. try
  74. {
  75. retryCount = Convert.ToInt32(ConfigurationManager.AppSettings.Get("retryCount"));
  76. //数据库
  77. remoteDBConnStr = ConfigurationManager.AppSettings.Get("remoteDBConnStr");
  78. localDBConnStr = ConfigurationManager.AppSettings.Get("localDBConnStr");
  79. DBtimeout = Convert.ToInt32(ConfigurationManager.AppSettings.Get("DBtimeout"));
  80. //plc
  81. plcIPAddr = ConfigurationManager.AppSettings.Get("plcIpAddress");
  82. plcRack = Convert.ToInt32(ConfigurationManager.AppSettings.Get("plcRack"));
  83. plcSlot = Convert.ToInt32(ConfigurationManager.AppSettings.Get("plcSlot"));
  84. plcDatablockConfig = ConfigurationManager.AppSettings.Get("plcDatablockId").Split(',');
  85. plcTerminalCount = Convert.ToInt32(ConfigurationManager.AppSettings.Get("plcTerminalCount"));
  86. plcParkingSpaceCount = Convert.ToInt32(ConfigurationManager.AppSettings.Get("plcParkingSpaceCount"));
  87. plcRefreshInterval = Convert.ToInt32(ConfigurationManager.AppSettings.Get("plcRefreshInterval"));
  88. //显示屏
  89. allInOneMachineIP = ConfigurationManager.AppSettings.Get("allInOneMachineIP");
  90. allInOneMachinePort = Convert.ToInt32(ConfigurationManager.AppSettings.Get("allInOneMachinePort"));
  91. //webServer端口
  92. webPort = Convert.ToInt32(ConfigurationManager.AppSettings.Get("webPort"));
  93. garageID = Convert.ToInt32(ConfigurationManager.AppSettings.Get("garageID"));
  94. //配置文件读取结束,进入状态1
  95. initializeState = 1;
  96. initStatus = true;
  97. }
  98. catch (Exception e) { Console.WriteLine("初始化," + e.Message); return; }
  99. initStatus = initStatus && PLCInit(retryCount);
  100. //PLC初始化结束,进入状态2
  101. if (!initStatus) { return; }
  102. else { initializeState = 2;}
  103. //初始化数据库对象
  104. remoteDBOper = new DBOperation(remoteDBConnStr, DBtimeout);
  105. localDBOper = new DBOperation(localDBConnStr, DBtimeout);
  106. initStatus = initStatus && remoteDBOper.InitConnPooling(10);
  107. initStatus = initStatus && localDBOper.InitConnPooling(10);
  108. //数据库初始化结束,进入状态3
  109. if (!initStatus) { return; }
  110. else { initializeState = 3; }
  111. //初始化web对象
  112. webServer = new CentralForWeb();
  113. initStatus = initStatus && webServer.Start(webPort);
  114. //webServer初始化结束,进入状态4
  115. if (!initStatus) { return; }
  116. else { initializeState = 4; }
  117. //初始化号牌机对象
  118. numMachineLinker = new NumMachineLinker(flpHandle);
  119. numMachineLinker.Start();
  120. //初始化显示板对象,显示板udp面向无连接
  121. allInOneMachine = new BroadcastBoard(allInOneMachineIP, allInOneMachinePort);
  122. allInOneMachine.Refresh();
  123. //系统初始化结束,进入状态5
  124. if (!initStatus) { return; }
  125. else { initializeState = 5; }
  126. mt.EndTiming();
  127. Console.WriteLine("初始化耗时:"+mt.GetInterval().TotalSeconds);
  128. switch(initializeState){
  129. case 0:
  130. Console.WriteLine("配置文件读写异常");
  131. break;
  132. case 1:
  133. Console.WriteLine("PLC初始化异常");
  134. break;
  135. case 2:
  136. Console.WriteLine("数据库初始化异常");
  137. break;
  138. case 3:
  139. Console.WriteLine("webServer初始化异常");
  140. break;
  141. case 4:
  142. Console.WriteLine("其他异常");
  143. break;
  144. case 5:
  145. Console.WriteLine("初始化成功");
  146. break;
  147. }
  148. }
  149. /// <summary>
  150. /// PLC初始化与连接
  151. /// </summary>
  152. /// <param name="retryCount"></param>
  153. /// <returns></returns>
  154. public static bool PLCInit(int retryCount)
  155. {
  156. int temp = retryCount;
  157. //初始化PLC对象
  158. while (temp-- > 0)
  159. {
  160. if (PLC == null)
  161. {
  162. PLC = new PLCLinker(CpuType.S71500, plcIPAddr, (short)plcRack, (short)plcSlot,
  163. Int32.Parse(plcDatablockConfig[0]), Int32.Parse(plcDatablockConfig[1]), Int32.Parse(plcDatablockConfig[2]),
  164. plcTerminalCount, plcParkingSpaceCount);
  165. }
  166. else if (PLC.isConnected)
  167. {
  168. return true;
  169. }
  170. else { PLC.PLCConnect(); }
  171. }
  172. return false;
  173. }
  174. /// <summary>
  175. /// 系统关闭
  176. /// </summary>
  177. public static void Stop()
  178. {
  179. //关闭号牌机
  180. if (numMachineLinker != null) { numMachineLinker.Stop();}
  181. //关闭webServer
  182. if (webServer != null) { webServer.Stop();}
  183. //关闭数据库
  184. if (remoteDBOper != null) {remoteDBOper.DBClose(); }
  185. if (localDBOper != null) {localDBOper.DBClose(); }
  186. }
  187. }
  188. }