Monitor.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. using allInOneMachine;
  2. using BroadcastModule;
  3. using centralController.advert;
  4. using centralController.WebServer;
  5. using db;
  6. using Monitor;
  7. using MySql.Data.MySqlClient;
  8. using NumMachine;
  9. using parkMonitor.language;
  10. using parkMonitor.model;
  11. using PLCS7;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Data;
  15. using System.Diagnostics;
  16. using System.Management;
  17. using System.Net;
  18. using System.Text;
  19. using System.Threading;
  20. using System.Threading.Tasks;
  21. using System.Windows.Forms;
  22. namespace Monitor
  23. {
  24. public class Monitor : IMonitor
  25. {
  26. /// <summary>
  27. /// 监控模块单例
  28. /// </summary>
  29. public static Monitor ins { get; set; }
  30. /// <summary>
  31. /// 中控系统总状态
  32. /// </summary>
  33. public static bool globalStatus = false;
  34. /// <summary>
  35. /// 初始化步骤
  36. /// </summary>
  37. public static int initializeState = 0;
  38. /// <summary>
  39. /// PLC对象句柄
  40. /// </summary>
  41. public static AbstractPLCLinker PLC = null;
  42. public static string plcIPAddr { get; set; }
  43. public static int plcRack { get; set; }
  44. public static int plcSlot { get; set; }
  45. public static string[] plcDatablockConfig { get; set; }
  46. public static int plcTerminalCount { get; set; }
  47. public static int plcParkingSpaceCount { get; set; }
  48. public static int plcRefreshInterval { get; set; }
  49. public static MainBlockStru mainBlockInfo { get; set; }
  50. public static List<ParkingSpaceStru> parkingSpaceInfo { get; set; }
  51. /// <summary>
  52. /// 远程数据库操作句柄
  53. /// </summary>
  54. public static DBOperation remoteDBOper;
  55. /// <summary>
  56. /// 本地数据库操作句柄
  57. /// </summary>
  58. public static DBOperation localDBOper;
  59. /// <summary>
  60. /// 显示板操作对象句柄
  61. /// </summary>
  62. public static BroadcastLinker allInOneMachine;
  63. public static string allInOneMachineIP { get; set; }
  64. public static int allInOneMachinePort { get; set; }
  65. /// <summary>
  66. /// 号牌机操作句柄
  67. /// </summary>
  68. public static NumMachineLinker numMachineLinker;
  69. public static IntPtr flpHandle;
  70. /// <summary>
  71. /// 本地web操作句柄
  72. /// </summary>
  73. public static IWebServer webServer;
  74. public static int webPort { get; set; }
  75. /// <summary>
  76. /// 广告路径
  77. /// </summary>
  78. public static string advertPath { get; set; }
  79. public static AdvertManager advertMgr;
  80. public static centralController.centralController mainWin { get; set; }
  81. public static string defaultLic{get;set;}
  82. /// <summary>
  83. /// 系统初始化器句柄
  84. /// </summary>
  85. internal static SystemInitializer sysInitializer;
  86. /// <summary>
  87. /// 系统关闭状态
  88. /// </summary>
  89. public static bool isClosing;
  90. /// <summary>
  91. /// 车库ID
  92. /// </summary>
  93. public static int garageID;
  94. /// <summary>
  95. /// PLC读取锁
  96. /// </summary>
  97. public static object PLCReadLock = new object();
  98. /// <summary>
  99. /// 是否初始化PLC数据更新次数的计数,重连PLC后初始化该值以刷新车位
  100. /// </summary>
  101. public static bool updateCount = false;
  102. /// <summary>
  103. /// 将显示在界面的提示字符串
  104. /// </summary>
  105. private static Queue<string> notificationQueue = new Queue<string>();
  106. private const int MAXLINES = 80;
  107. private void PLCUpdate()
  108. {
  109. int linkCount = 0;
  110. int count = 0;
  111. bool disconnected = false;
  112. while (!isClosing)
  113. {
  114. if (updateCount)
  115. {
  116. count = 0;
  117. updateCount = false;
  118. }
  119. if (count > 10000)
  120. count = 11;
  121. count++;
  122. if (PLC != null)
  123. {
  124. if (PLC.isConnected)
  125. {
  126. if (disconnected) {disconnected = false; SetNotification("PLC已重新连接",TextColor.Info); }
  127. linkCount = 0;
  128. lock (PLCReadLock)
  129. {
  130. List<object> received = PLC.ReadFromPLC(PLCDataType.terminal, 0);
  131. //首先获取所有终端信息
  132. try
  133. {
  134. //终端总数相同
  135. if (Terminal.Terminal.terminalInfo.Count == plcTerminalCount)
  136. {
  137. for (int i = 0; i < plcTerminalCount; i++)
  138. {
  139. //一旦发现差异立刻更新
  140. if (!Terminal.Terminal.terminalInfo[i].Equals(received[i]))
  141. {
  142. Terminal.Terminal.terminalInfo[i] = (TerminalStru)received[i];
  143. }
  144. }
  145. }
  146. else
  147. {
  148. //初始化终端信息列表
  149. Terminal.Terminal.terminalInfo.Clear();
  150. for (int i = 0; i < plcTerminalCount; i++)
  151. {
  152. Terminal.Terminal.terminalInfo.Add((TerminalStru)received[i]);
  153. if (!Terminal.Terminal.termCalcMap.ContainsKey(((TerminalStru)received[i]).terminalID))
  154. Terminal.Terminal.termCalcMap.Add(((TerminalStru)received[i]).terminalID, false);
  155. else
  156. Terminal.Terminal.termCalcMap[((TerminalStru)received[i]).terminalID] = false;
  157. }
  158. }
  159. }
  160. catch (Exception ex) { Console.WriteLine("PLC监控终端数据," + ex.Message); }
  161. //接下来获取中控监控信息
  162. try
  163. {
  164. received = PLC.ReadFromPLC(PLCDataType.central, 0);
  165. if (received.Count > 0 && !mainBlockInfo.Equals(received[0]))
  166. {
  167. mainBlockInfo = (MainBlockStru)received[0];
  168. }
  169. }
  170. catch (Exception ex) { Console.WriteLine("PLC监控中控数据," + ex.Message); }
  171. //最后获得所有车位信息
  172. if (count < 10)
  173. {
  174. try
  175. {
  176. received = PLC.ReadFromPLC(PLCDataType.parkingSpace, 0);
  177. //Console.WriteLine(parkingSpaceInfo.Count+","+ plcParkingSpaceCount);
  178. //车位总数相同
  179. if (parkingSpaceInfo.Count == plcParkingSpaceCount)
  180. {
  181. for (int i = 0; i < plcParkingSpaceCount; i++)
  182. {
  183. //一旦发现差异立刻更新
  184. if (!parkingSpaceInfo[i].Equals(received[i]))
  185. {
  186. parkingSpaceInfo[i] = (ParkingSpaceStru)received[i];
  187. }
  188. }
  189. }
  190. else
  191. {
  192. parkingSpaceInfo.Clear();
  193. for (int i = 0; i < plcParkingSpaceCount; i++)
  194. {
  195. parkingSpaceInfo.Add((ParkingSpaceStru)received[i]);
  196. }
  197. }
  198. }
  199. catch (Exception ex) { Console.WriteLine("PLC监控车位数据," + ex.Message); }
  200. }
  201. }
  202. }
  203. else
  204. {
  205. linkCount += 1;
  206. if (linkCount == 1)
  207. {
  208. disconnected = true;
  209. SetNotification("PLC掉线,请检查连接",TextColor.Warning);
  210. }else if (linkCount == 5)
  211. {
  212. Terminal.Terminal.terminalInfo.Clear();
  213. parkingSpaceInfo.Clear();
  214. mainBlockInfo = new MainBlockStru();
  215. }
  216. }
  217. }
  218. Thread.Sleep(300);
  219. }
  220. }
  221. /// <summary>
  222. /// CPU名
  223. /// </summary>
  224. /// <returns></returns>
  225. private static string getCPUName()
  226. {
  227. try
  228. {
  229. string str = string.Empty;
  230. ManagementClass mcCPU = new ManagementClass("Win32_Processor");
  231. ManagementObjectCollection mocCPU = mcCPU.GetInstances();
  232. foreach (ManagementObject m in mocCPU)
  233. {
  234. string name = m["Name"].ToString();
  235. return name;
  236. }
  237. }
  238. catch { }
  239. return "";
  240. }
  241. /// <summary>
  242. /// 操作系统版本
  243. /// </summary>
  244. private static string getOsVersion()
  245. {
  246. string str = "Windows 10";
  247. try
  248. {
  249. string hdId = string.Empty;
  250. ManagementClass hardDisk = new ManagementClass("Win32_OperatingSystem");
  251. ManagementObjectCollection hardDiskC = hardDisk.GetInstances();
  252. foreach (ManagementObject m in hardDiskC)
  253. {
  254. str = m["Name"].ToString().Split('|')[0].Replace("Microsoft", "").Trim();
  255. break;
  256. }
  257. }
  258. catch
  259. {
  260. }
  261. return str;
  262. }
  263. /// <summary>
  264. /// 显卡名
  265. /// </summary>
  266. private static string getGPUName()
  267. {
  268. string result = "";
  269. try
  270. {
  271. ManagementClass hardDisk = new ManagementClass("Win32_VideoController");
  272. ManagementObjectCollection hardDiskC = hardDisk.GetInstances();
  273. foreach (ManagementObject m in hardDiskC)
  274. {
  275. result = m["VideoProcessor"].ToString();
  276. break;
  277. }
  278. }
  279. catch
  280. {
  281. }
  282. return result;
  283. }
  284. /// <summary>
  285. /// 获取系统内存大小
  286. /// </summary>
  287. private static string getMenSize()
  288. {
  289. ManagementObjectSearcher searcher = new ManagementObjectSearcher(); //用于查询一些如系统信息的管理对象
  290. searcher.Query = new SelectQuery("Win32_PhysicalMemory", "", new string[] { "Capacity" });//设置查询条件
  291. ManagementObjectCollection collection = searcher.Get(); //获取内存容量
  292. ManagementObjectCollection.ManagementObjectEnumerator em = collection.GetEnumerator();
  293. long capacity = 0;
  294. while (em.MoveNext())
  295. {
  296. ManagementBaseObject baseObj = em.Current;
  297. if (baseObj.Properties["Capacity"].Value != null)
  298. {
  299. try
  300. {
  301. capacity += long.Parse(baseObj.Properties["Capacity"].Value.ToString());
  302. }
  303. catch
  304. {
  305. return "-GB";
  306. }
  307. }
  308. }
  309. int gb = 1024 * 1024 * 1024;
  310. return ((double)capacity / gb).ToString("0.0") + "GB";
  311. }
  312. //************************************ 公有方法 **********************************
  313. public Monitor(centralController.centralController mainWin, IntPtr flpHandle)
  314. {
  315. Monitor.mainWin = mainWin;
  316. Monitor.flpHandle = flpHandle;
  317. parkingSpaceInfo = new List<ParkingSpaceStru>();
  318. mainBlockInfo = new MainBlockStru();
  319. }
  320. ///// <summary>
  321. ///// 获取提示信息
  322. ///// </summary>
  323. ///// <returns></returns>
  324. //public string GetNotification()
  325. //{
  326. // StringBuilder notificationStr = new StringBuilder();
  327. // lock (notificationQueue)
  328. // {
  329. // string[] strs = notificationQueue.ToArray();
  330. // Array.Reverse(strs);
  331. // for (int i = 0; i < strs.Length; i++)
  332. // {
  333. // notificationStr.Append(strs[i]);
  334. // }
  335. // //Queue<string>.Enumerator notiEnumer = notificationQueue.GetEnumerator();
  336. // //while (notiEnumer.MoveNext())
  337. // //{
  338. // // notificationStr.Append(notiEnumer.Current);
  339. // //}
  340. // }
  341. // return notificationStr.ToString();
  342. //}
  343. /// <summary>
  344. /// 添加提示信息
  345. /// </summary>
  346. /// <param name="notification"></param>
  347. public static void SetNotification(string notification, TextColor textColor=TextColor.Log)
  348. {
  349. if (notification == "clear")
  350. {
  351. //lock (notificationQueue)
  352. //{
  353. // notificationQueue.Clear();
  354. //}
  355. if (mainWin != null)
  356. {
  357. mainWin.Invoke(new Action(() => {
  358. mainWin.UpdateText(notification, textColor,MAXLINES);
  359. }));
  360. }
  361. return;
  362. }
  363. else
  364. {
  365. string time = DateTime.Now + "\r\n";
  366. string notificationStr = time + notification + "\r\n";
  367. //lock (notificationQueue)
  368. //{
  369. // int count = notificationQueue.Count;
  370. // if (count >= MAXLINES)
  371. // {
  372. // notificationQueue.Dequeue();
  373. // }
  374. // notificationQueue.Enqueue(notificationStr);
  375. //}
  376. if (mainWin != null)
  377. {
  378. mainWin.Invoke(new Action(() => {
  379. mainWin.UpdateText(notificationStr, textColor, MAXLINES);
  380. }));
  381. }
  382. }
  383. }
  384. /// <summary>
  385. /// 返回系统信息字符串
  386. /// </summary>
  387. /// <returns></returns>
  388. public static string GetSysInfo()
  389. {
  390. string info = "";
  391. Language lng = Language.ins;
  392. try
  393. {
  394. string endl = "\r\n";
  395. info += endl + lng.autoPackSys + " " + lng.centralPort + endl + endl;
  396. info += lng.version + ":" + SysConst.version() + endl + endl;
  397. info += lng.hostNmae + ":" + Dns.GetHostName() + endl + endl;
  398. info += lng.os + ":" + getOsVersion() + endl + endl;
  399. info += lng.cpu + ":" + getCPUName() + endl + endl;
  400. info += lng.mem + ":" + getMenSize() + endl + endl;
  401. info += lng.gpu + ":" + getGPUName() + endl + endl;
  402. }
  403. catch (Exception) { }
  404. return info;
  405. }
  406. /// <summary>
  407. /// 返回停车记录信息
  408. /// </summary>
  409. /// <returns></returns>
  410. public static List<object[]> GetParkingRecords(string license = "", string startTime = "", string endTime = "")
  411. {
  412. DateTime now = DateTime.Now;
  413. List<object[]> result = new List<object[]>();
  414. string getParkingRecordsSql = "";
  415. if (startTime == "" || endTime == "")
  416. {
  417. DateTime yesterday = DateTime.Now - (new TimeSpan(1, 0, 0, 0));
  418. DateTime twoDaysAgo = DateTime.Now - (new TimeSpan(2, 0, 0, 0));
  419. getParkingRecordsSql = "select parkingRecordsID,userID,numberPlate,parkingSpaceID,realParkTime,realGetTime,receiptNum,parkingPrice,paymentStatus " +
  420. "from parkingrecords where numberPlate " + (license == "" ? "like '%" : "= '" + license) + "' and (realParkTime like '" + now.ToString("yyyy-MM-dd") + "%' or realParkTime like '" + yesterday.ToString("yyyy-MM-dd") + "%' or realParkTime like '" + twoDaysAgo.ToString("yyyy-MM-dd") + "%');";
  421. }
  422. else
  423. {
  424. getParkingRecordsSql = "select parkingRecordsID,userID,numberPlate,parkingSpaceID,realParkTime,realGetTime,receiptNum,parkingPrice,paymentStatus " +
  425. "from parkingrecords where numberPlate " + (license == "" ? "like '%" : "= '" + license) + "' and realParkTime >= '" + startTime + "' and realParkTime <= '" + endTime + "';";
  426. }
  427. if (localDBOper != null)
  428. {
  429. lock (localDBOper)
  430. {
  431. MySqlDataReader reader = localDBOper.Query(getParkingRecordsSql);
  432. try
  433. {
  434. while (reader != null && reader.Read())
  435. {
  436. if (reader.HasRows)
  437. {
  438. object[] temp = new object[reader.FieldCount];
  439. reader.GetValues(temp);
  440. result.Add(temp);
  441. }
  442. }
  443. }
  444. catch (Exception e) { Console.WriteLine(e.Message); }
  445. try
  446. {
  447. if (reader != null)
  448. {
  449. reader.Close();
  450. reader.Dispose();
  451. }
  452. }
  453. catch (Exception e) { Console.WriteLine(e.Message); }
  454. }
  455. }
  456. result.Reverse();
  457. return result;
  458. }
  459. /// <summary>
  460. /// 返回预约记录信息
  461. /// </summary>
  462. /// <returns></returns>
  463. public static List<object[]> GetOrderRecords(bool localDB, string license="", string startTime = "", string endTime = "")
  464. {
  465. DateTime now = DateTime.Now;
  466. List<object[]> result = new List<object[]>();
  467. string getOrderRecordsSql = "";
  468. if (startTime == "" || endTime == "")
  469. {
  470. DateTime yesterday = DateTime.Now - (new TimeSpan(1, 0, 0, 0));
  471. DateTime twoDaysAgo = DateTime.Now - (new TimeSpan(2, 0, 0, 0));
  472. getOrderRecordsSql = "select orderRecordsID,userID,numberPlate,bookParkTime,cancelBookTime,bookFetchTime,bookHour,bookPrice,bookState " +
  473. "from orderrecords where numberPlate " + (license == "" ? "like '%" : "= '" + license) + "' and (bookParkTime like '" + now.ToString("yyyy-MM-dd") +
  474. "%' or bookParkTime like '" + yesterday.ToString("yyyy-MM-dd") + "%' or bookFetchTime like '" + now.ToString("yyyy-MM-dd") + "%' or bookFetchTime like '" + yesterday.ToString("yyyy-MM-dd") + "%');";
  475. }
  476. else
  477. {
  478. getOrderRecordsSql = "select orderRecordsID,userID,numberPlate,bookParkTime,cancelBookTime,bookFetchTime,bookHour,bookPrice,bookState " +
  479. "from orderrecords where numberPlate " + (license == "" ? "like '%" : "= '" + license) + "' and ((bookParkTime >= '" + startTime + "' and bookParkTime <= '" + endTime + "') or (bookFetchTime >= '" + startTime + "' and bookFetchTime <= '" + endTime + "'));";
  480. }
  481. if (localDBOper != null)
  482. {
  483. lock (localDBOper)
  484. {
  485. MySqlDataReader reader;
  486. if (localDB)
  487. reader = localDBOper.Query(getOrderRecordsSql);
  488. else
  489. reader = remoteDBOper.Query(getOrderRecordsSql);
  490. try
  491. {
  492. while (reader != null && reader.Read())
  493. {
  494. if (reader.HasRows)
  495. {
  496. object[] temp = new object[reader.FieldCount];
  497. reader.GetValues(temp);
  498. result.Add(temp);
  499. }
  500. }
  501. }
  502. catch (Exception e) { Console.WriteLine(e.Message); }
  503. try
  504. {
  505. if (reader != null)
  506. {
  507. reader.Close();
  508. reader.Dispose();
  509. }
  510. }
  511. catch (Exception e) { Console.WriteLine(e.Message); }
  512. }
  513. }
  514. result.Reverse();
  515. return result;
  516. }
  517. /// <summary>
  518. /// 返回空闲正常车位数
  519. /// </summary>
  520. /// <param name="state">状态:0.空闲与保留,1.空闲,2.保留,3.预约车位总数</param>
  521. /// <returns></returns>
  522. public int GetFreeSpaceCount(int state)
  523. {
  524. int freeSpaceCount = 0;
  525. int bookableSpaceCount = 0;
  526. int bookedSpaceCount = 0;
  527. if(state == 3)
  528. {
  529. return mainBlockInfo.reserveTotalSpace;
  530. }
  531. try
  532. {
  533. if (parkingSpaceInfo != null)
  534. {
  535. foreach (ParkingSpaceStru psStru in parkingSpaceInfo)
  536. {
  537. if (psStru.spaceStatus == 3)
  538. bookedSpaceCount++;
  539. if (psStru.spaceStatus != 1 && psStru.spaceStatus != 3)
  540. {
  541. freeSpaceCount++;
  542. if (psStru.spaceStatus == 2)
  543. bookableSpaceCount++;
  544. }
  545. }
  546. }
  547. }
  548. catch { Console.WriteLine("空闲车位数,计数异常");return 0; }
  549. switch (state)
  550. {
  551. case 0:
  552. return freeSpaceCount;//0+2
  553. case 1:
  554. return freeSpaceCount - bookableSpaceCount;//0
  555. case 2:
  556. return bookableSpaceCount;//2
  557. case 4:
  558. return bookedSpaceCount + bookableSpaceCount;//2+3
  559. default:
  560. return freeSpaceCount;
  561. }
  562. }
  563. /// <summary>
  564. /// 暂未使用,查询本地数据库返回可预约车位数
  565. /// </summary>
  566. /// <returns></returns>
  567. //public int GetBookableSpaceCount()
  568. //{
  569. // int bookableSpaceCount = 0;
  570. // string bookableSpaceSql = "select currentBookableSpace from garageproperties where garageID = " + garageID + ";";
  571. // if (localDBOper != null)
  572. // {
  573. // MySqlDataReader reader = localDBOper.Query(bookableSpaceSql);
  574. // if (reader != null)
  575. // {
  576. // try
  577. // {
  578. // if (reader.Read() && reader.HasRows)
  579. // {
  580. // bookableSpaceCount = reader.GetInt32("currentBookableSpace");
  581. // }
  582. // }
  583. // catch { }
  584. // try
  585. // {
  586. // reader.Close();
  587. // reader.Dispose();
  588. // }
  589. // catch { }
  590. // }
  591. // }
  592. // return bookableSpaceCount;
  593. //}
  594. /// <summary>
  595. /// 系统初始化,启动plc监控
  596. /// </summary>
  597. public void Start()
  598. {
  599. if (flpHandle != IntPtr.Zero)
  600. {
  601. //初始化系统
  602. if (sysInitializer == null)
  603. {
  604. sysInitializer = new SystemInitializer();
  605. }
  606. Task.Factory.StartNew(() =>
  607. {
  608. sysInitializer.Init(flpHandle);
  609. });
  610. //更新PLC数据
  611. Thread PLCUpdateThread = new Thread(PLCUpdate);
  612. PLCUpdateThread.Start();
  613. //System.Windows.Forms.Timer PLCUpdateTimer = new System.Windows.Forms.Timer();
  614. //PLCUpdateTimer.Interval = 300;
  615. //PLCUpdateTimer.Tick += new EventHandler(PLCUpdate);
  616. //PLCUpdateTimer.Start();
  617. }
  618. }
  619. /// <summary>
  620. /// 系统停止
  621. /// </summary>
  622. public void Stop()
  623. {
  624. sysInitializer.Stop();
  625. System.Environment.Exit(0);
  626. }
  627. }
  628. //public class ParkingRecord
  629. //{
  630. // int parkingRecordsID;
  631. // int userID;
  632. // string numberPlate;
  633. // int parkingSpaceID;
  634. //}
  635. }