Monitor.cs 24 KB

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