Monitor.cs 20 KB

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