Monitor.cs 19 KB

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