Monitor.cs 19 KB

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