SystemInitializer.cs 14 KB

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