Monitor.cs 24 KB

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