Monitor.cs 15 KB

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