SystemInitializer.cs 13 KB

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