Monitor.cs 23 KB

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