Monitor.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. bool disconnected = false;
  99. while (!isClosing)
  100. {
  101. if (PLC != null)
  102. {
  103. if (PLC.isConnected)
  104. {
  105. if (disconnected) {disconnected = false; AddNotification("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. AddNotification("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. public void DisplayPLCInfo()
  285. {
  286. }
  287. public void DisplayTerminalState()
  288. {
  289. }
  290. public void DisplayLicensePlate()
  291. {
  292. }
  293. public void DisplayImage()
  294. {
  295. }
  296. /// <summary>
  297. /// 获取提示信息
  298. /// </summary>
  299. /// <returns></returns>
  300. public static string GetNotification()
  301. {
  302. StringBuilder notificationStr = new StringBuilder();
  303. lock (notificationQueue)
  304. {
  305. Queue<string>.Enumerator notiEnumer = notificationQueue.GetEnumerator();
  306. while (notiEnumer.MoveNext())
  307. {
  308. notificationStr.Append(notiEnumer.Current);
  309. }
  310. }
  311. return notificationStr.ToString();
  312. }
  313. /// <summary>
  314. /// 添加提示信息
  315. /// </summary>
  316. /// <param name="notification"></param>
  317. public static void AddNotification(string notification)
  318. {
  319. string time = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "\r\n";
  320. string notificationStr = time + notification + "\r\n";
  321. lock (notificationQueue)
  322. {
  323. int count = notificationQueue.Count;
  324. if (count >= MAXLINES)
  325. {
  326. notificationQueue.Dequeue();
  327. }
  328. notificationQueue.Enqueue(notificationStr);
  329. }
  330. }
  331. /// <summary>
  332. /// 清除提示信息
  333. /// </summary>
  334. public static void ClearNotification()
  335. {
  336. lock (notificationQueue)
  337. {
  338. notificationQueue.Clear();
  339. }
  340. }
  341. /// <summary>
  342. /// 返回系统信息字符串
  343. /// </summary>
  344. /// <returns></returns>
  345. public static string GetSysInfo()
  346. {
  347. string info = "";
  348. Language lng = Language.ins;
  349. try
  350. {
  351. string endl = "\r\n";
  352. info += endl + lng.autoPackSys + " " + lng.centralPort + endl + endl;
  353. info += lng.version + ":" + SysConst.version() + endl + endl;
  354. info += lng.hostNmae + ":" + Dns.GetHostName() + endl + endl;
  355. info += lng.os + ":" + getOsVersion() + endl + endl;
  356. info += lng.cpu + ":" + getCPUName() + endl + endl;
  357. info += lng.mem + ":" + getMenSize() + endl + endl;
  358. info += lng.gpu + ":" + getGPUName() + endl + endl;
  359. }
  360. catch (Exception) { }
  361. return info;
  362. }
  363. /// <summary>
  364. /// 返回停车记录信息
  365. /// </summary>
  366. /// <returns></returns>
  367. public static List<object[]> GetParkingRecords(string license = "", string startTime = "", string endTime = "")
  368. {
  369. DateTime now = DateTime.Now;
  370. List<object[]> result = new List<object[]>();
  371. string getParkingRecordsSql = "";
  372. if (startTime == "" || endTime == "")
  373. {
  374. DateTime yesterday = DateTime.Now - (new TimeSpan(1, 0, 0, 0));
  375. DateTime twoDaysAgo = DateTime.Now - (new TimeSpan(2, 0, 0, 0));
  376. getParkingRecordsSql = "select parkingRecordsID,userID,numberPlate,parkingSpaceID,realParkTime,realGetTime,receiptNum,parkingPrice " +
  377. "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") + "%');";
  378. }
  379. else
  380. {
  381. getParkingRecordsSql = "select parkingRecordsID,userID,numberPlate,parkingSpaceID,realParkTime,realGetTime,receiptNum,parkingPrice " +
  382. "from parkingrecords where numberPlate " + (license == "" ? "like '%" : "= '" + license) + "' and realParkTime >= '" + startTime + "' and realParkTime <= '" + endTime + "';";
  383. }
  384. if (localDBOper != null)
  385. {
  386. lock (localDBOper)
  387. {
  388. MySqlDataReader reader = localDBOper.Query(getParkingRecordsSql);
  389. try
  390. {
  391. //MySqlDataAdapter adapter = localDBOper.Display(getParkingRecordsSql);
  392. //DataSet ds = new DataSet();
  393. //adapter.Fill(ds);
  394. //DataTable dt = ds.Tables[0];
  395. //for (int i = 0; i < dt.Rows.Count; i++)
  396. //{
  397. // object[] objArray = new object[10];
  398. // for (int j = 0; j < dt.Columns.Count; j++)
  399. // {
  400. // objArray[j] = dt.Rows[i][j];
  401. // }
  402. // result.Add(objArray);
  403. //}
  404. //adapter.Dispose();
  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()
  435. {
  436. return null;
  437. }
  438. /// <summary>
  439. /// 返回空闲正常车位数
  440. /// </summary>
  441. /// <returns></returns>
  442. public static int GetFreeSpaceCount()
  443. {
  444. int freeSpaceCount = 0;
  445. if (parkingSpaceInfo != null)
  446. {
  447. foreach (ParkingSpaceStru psStru in parkingSpaceInfo)
  448. {
  449. if (psStru.spaceStatus != 1 && psStru.spaceStatus != 3)
  450. {
  451. freeSpaceCount++;
  452. }
  453. }
  454. }
  455. return freeSpaceCount;
  456. }
  457. /// <summary>
  458. /// 返回可预约车位数
  459. /// </summary>
  460. /// <returns></returns>
  461. public static int GetBookableSpaceCount()
  462. {
  463. int bookableSpaceCount = 0;
  464. string bookableSpaceSql = "select currentBookableSpace from garageproperties where garageID = " + garageID + ";";
  465. if (localDBOper != null)
  466. {
  467. MySqlDataReader reader = localDBOper.Query(bookableSpaceSql);
  468. if (reader != null)
  469. {
  470. try
  471. {
  472. if (reader.Read() && reader.HasRows)
  473. {
  474. bookableSpaceCount = reader.GetInt32("currentBookableSpace");
  475. }
  476. }
  477. catch { }
  478. try
  479. {
  480. reader.Close();
  481. reader.Dispose();
  482. }
  483. catch { }
  484. }
  485. }
  486. return bookableSpaceCount;
  487. }
  488. /// <summary>
  489. /// 系统初始化,启动plc监控
  490. /// </summary>
  491. public void Start()
  492. {
  493. if (flpHandle != IntPtr.Zero)
  494. {
  495. //初始化系统
  496. if (sysInitializer == null)
  497. {
  498. sysInitializer = new SystemInitializer();
  499. }
  500. Task.Factory.StartNew(() =>
  501. {
  502. sysInitializer.Init(flpHandle);
  503. });
  504. //更新PLC数据
  505. Task.Factory.StartNew(() =>
  506. {
  507. PLCUpdate();
  508. });
  509. }
  510. }
  511. /// <summary>
  512. /// 系统停止
  513. /// </summary>
  514. public void Stop()
  515. {
  516. sysInitializer.Stop();
  517. }
  518. }
  519. //public class ParkingRecord
  520. //{
  521. // int parkingRecordsID;
  522. // int userID;
  523. // string numberPlate;
  524. // int parkingSpaceID;
  525. //}
  526. }