Monitor.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. using allInOneMachine;
  2. using BroadcastModule;
  3. using centralController.advert;
  4. using centralController.WebServer;
  5. using db;
  6. using Monitor;
  7. using MySql.Data.MySqlClient;
  8. using NumMachine;
  9. using parkMonitor.language;
  10. using parkMonitor.LOG;
  11. using parkMonitor.model;
  12. using PLCS7;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Data;
  16. using System.Diagnostics;
  17. using System.Management;
  18. using System.Net;
  19. using System.Text;
  20. using System.Threading;
  21. using System.Threading.Tasks;
  22. using System.Windows.Forms;
  23. namespace Monitor
  24. {
  25. public class Monitor : IMonitor
  26. {
  27. /// <summary>
  28. /// 监控模块单例
  29. /// </summary>
  30. public static Monitor ins { get; set; }
  31. /// <summary>
  32. /// 中控系统总状态
  33. /// </summary>
  34. public static bool globalStatus = false;
  35. /// <summary>
  36. /// 初始化步骤
  37. /// </summary>
  38. public static int initializeState = 0;
  39. /// <summary>
  40. /// PLC对象句柄
  41. /// </summary>
  42. public static AbstractPLCLinker PLC = null;
  43. public static string plcIPAddr { get; set; }
  44. public static int plcRack { get; set; }
  45. public static int plcSlot { get; set; }
  46. public static string[] plcDatablockConfig { get; set; }
  47. public static int plcTerminalCount { get; set; }
  48. public static int plcParkingSpaceCount { get; set; }
  49. public static int plcRefreshInterval { get; set; }
  50. public static MainBlockStru mainBlockInfo { get; set; }
  51. public static List<ParkingSpaceStru> parkingSpaceInfo { get; set; }
  52. /// <summary>
  53. /// 远程数据库操作句柄
  54. /// </summary>
  55. public static DBOperation remoteDBOper;
  56. /// <summary>
  57. /// 本地数据库操作句柄
  58. /// </summary>
  59. public static DBOperation localDBOper;
  60. /// <summary>
  61. /// 显示板操作对象句柄
  62. /// </summary>
  63. public static BroadcastLinker allInOneMachine;
  64. public static string allInOneMachineIP { get; set; }
  65. public static int allInOneMachinePort { get; set; }
  66. /// <summary>
  67. /// 号牌机操作句柄
  68. /// </summary>
  69. public static NumMachineLinker numMachineLinker;
  70. public static IntPtr flpHandle;
  71. /// <summary>
  72. /// 本地web操作句柄
  73. /// </summary>
  74. public static IWebServer webServer;
  75. public static int webPort { get; set; }
  76. /// <summary>
  77. /// 广告路径
  78. /// </summary>
  79. public static string advertPath { get; set; }
  80. public static AdvertManager advertMgr;
  81. public static centralController.centralController mainWin { get; set; }
  82. public static string defaultLic{get;set;}
  83. /// <summary>
  84. /// 车库层数
  85. /// </summary>
  86. public static int floors { get; set; }
  87. /// <summary>
  88. /// 每层分块数
  89. /// </summary>
  90. public static int rows { get; set; }
  91. /// <summary>
  92. /// 每块车位数
  93. /// </summary>
  94. public static int spacesInRow { get; set; }
  95. /// <summary>
  96. /// 系统初始化器句柄
  97. /// </summary>
  98. internal static SystemInitializer sysInitializer;
  99. /// <summary>
  100. /// 系统关闭状态
  101. /// </summary>
  102. public static bool isClosing;
  103. /// <summary>
  104. /// 车库ID
  105. /// </summary>
  106. public static int garageID;
  107. /// <summary>
  108. /// PLC读取锁
  109. /// </summary>
  110. public static object PLCReadLock = new object();
  111. /// <summary>
  112. /// 是否初始化PLC数据更新次数的计数,重连PLC后初始化该值以刷新车位
  113. /// </summary>
  114. public static bool updateCount = false;
  115. /// <summary>
  116. /// 将显示在界面的提示字符串
  117. /// </summary>
  118. private static Queue<string> notificationQueue = new Queue<string>();
  119. private const int MAXLINES = 80;
  120. private void PLCUpdate()
  121. {
  122. int linkCount = 0;
  123. int count = 0;
  124. bool disconnected = false;
  125. while (!isClosing)
  126. {
  127. if (updateCount)
  128. {
  129. count = 0;
  130. updateCount = false;
  131. }
  132. if (count > 50)
  133. count = 3;
  134. count++;
  135. if (PLC != null)
  136. {
  137. if (PLC.isConnected)
  138. {
  139. if (disconnected) {disconnected = false; SetNotification("PLC已重新连接",TextColor.Info); }
  140. linkCount = 0;
  141. lock (PLCReadLock)
  142. {
  143. List<object> received = PLC.ReadFromPLC(PLCDataType.terminal, 0);
  144. //首先获取所有终端信息
  145. try
  146. {
  147. //终端总数相同
  148. if (Terminal.Terminal.terminalInfo.Count == plcTerminalCount)
  149. {
  150. for (int i = 0; i < plcTerminalCount; i++)
  151. {
  152. //一旦发现差异立刻更新
  153. if (!Terminal.Terminal.terminalInfo[i].Equals(received[i]))
  154. {
  155. Terminal.Terminal.terminalInfo[i] = (TerminalStru)received[i];
  156. }
  157. }
  158. }
  159. else
  160. {
  161. //初始化终端信息列表
  162. Terminal.Terminal.terminalInfo.Clear();
  163. for (int i = 0; i < plcTerminalCount; i++)
  164. {
  165. Terminal.Terminal.terminalInfo.Add((TerminalStru)received[i]);
  166. if (!Terminal.Terminal.termCalcMap.ContainsKey(((TerminalStru)received[i]).terminalID))
  167. Terminal.Terminal.termCalcMap.Add(((TerminalStru)received[i]).terminalID, false);
  168. else
  169. Terminal.Terminal.termCalcMap[((TerminalStru)received[i]).terminalID] = false;
  170. }
  171. }
  172. }
  173. catch (Exception ex) { Console.WriteLine("PLC监控终端数据," + ex.Message); }
  174. //接下来获取中控监控信息
  175. try
  176. {
  177. received = PLC.ReadFromPLC(PLCDataType.central, 0);
  178. if (received.Count > 0 && !mainBlockInfo.Equals(received[0]))
  179. {
  180. mainBlockInfo = (MainBlockStru)received[0];
  181. }
  182. }
  183. catch (Exception ex) { Console.WriteLine("PLC监控中控数据," + ex.Message); }
  184. //最后获得所有车位信息
  185. if (count < 10 && count % 5 == 0)
  186. {
  187. try
  188. {
  189. received = PLC.ReadFromPLC(PLCDataType.parkingSpace, 0);
  190. //Console.WriteLine(parkingSpaceInfo.Count+","+ plcParkingSpaceCount);
  191. //车位总数相同
  192. if (parkingSpaceInfo.Count == plcParkingSpaceCount)
  193. {
  194. for (int i = 0; i < plcParkingSpaceCount; i++)
  195. {
  196. //一旦发现差异立刻更新
  197. if (!parkingSpaceInfo[i].Equals(received[i]))
  198. {
  199. parkingSpaceInfo[i] = (ParkingSpaceStru)received[i];
  200. }
  201. }
  202. }
  203. else
  204. {
  205. parkingSpaceInfo.Clear();
  206. for (int i = 0; i < plcParkingSpaceCount; i++)
  207. {
  208. parkingSpaceInfo.Add((ParkingSpaceStru)received[i]);
  209. }
  210. }
  211. }
  212. catch (Exception ex) { Console.WriteLine("PLC监控车位数据," + ex.Message); }
  213. }
  214. }
  215. }
  216. else
  217. {
  218. linkCount += 1;
  219. if (linkCount == 1)
  220. {
  221. disconnected = true;
  222. SetNotification("PLC掉线,请检查连接",TextColor.Warning);
  223. }else if (linkCount == 5)
  224. {
  225. Terminal.Terminal.terminalInfo.Clear();
  226. parkingSpaceInfo.Clear();
  227. mainBlockInfo = new MainBlockStru();
  228. }
  229. }
  230. }
  231. Thread.Sleep(300);
  232. }
  233. }
  234. /// <summary>
  235. /// CPU名
  236. /// </summary>
  237. /// <returns></returns>
  238. private static string getCPUName()
  239. {
  240. try
  241. {
  242. string str = string.Empty;
  243. ManagementClass mcCPU = new ManagementClass("Win32_Processor");
  244. ManagementObjectCollection mocCPU = mcCPU.GetInstances();
  245. foreach (ManagementObject m in mocCPU)
  246. {
  247. string name = m["Name"].ToString();
  248. return name;
  249. }
  250. }
  251. catch { }
  252. return "";
  253. }
  254. /// <summary>
  255. /// 操作系统版本
  256. /// </summary>
  257. private static string getOsVersion()
  258. {
  259. string str = "Windows 10";
  260. try
  261. {
  262. string hdId = string.Empty;
  263. ManagementClass hardDisk = new ManagementClass("Win32_OperatingSystem");
  264. ManagementObjectCollection hardDiskC = hardDisk.GetInstances();
  265. foreach (ManagementObject m in hardDiskC)
  266. {
  267. str = m["Name"].ToString().Split('|')[0].Replace("Microsoft", "").Trim();
  268. break;
  269. }
  270. }
  271. catch
  272. {
  273. }
  274. return str;
  275. }
  276. /// <summary>
  277. /// 显卡名
  278. /// </summary>
  279. private static string getGPUName()
  280. {
  281. string result = "";
  282. try
  283. {
  284. ManagementClass hardDisk = new ManagementClass("Win32_VideoController");
  285. ManagementObjectCollection hardDiskC = hardDisk.GetInstances();
  286. foreach (ManagementObject m in hardDiskC)
  287. {
  288. result = m["VideoProcessor"].ToString();
  289. break;
  290. }
  291. }
  292. catch
  293. {
  294. }
  295. return result;
  296. }
  297. /// <summary>
  298. /// 获取系统内存大小
  299. /// </summary>
  300. private static string getMenSize()
  301. {
  302. ManagementObjectSearcher searcher = new ManagementObjectSearcher(); //用于查询一些如系统信息的管理对象
  303. searcher.Query = new SelectQuery("Win32_PhysicalMemory", "", new string[] { "Capacity" });//设置查询条件
  304. ManagementObjectCollection collection = searcher.Get(); //获取内存容量
  305. ManagementObjectCollection.ManagementObjectEnumerator em = collection.GetEnumerator();
  306. long capacity = 0;
  307. while (em.MoveNext())
  308. {
  309. ManagementBaseObject baseObj = em.Current;
  310. if (baseObj.Properties["Capacity"].Value != null)
  311. {
  312. try
  313. {
  314. capacity += long.Parse(baseObj.Properties["Capacity"].Value.ToString());
  315. }
  316. catch
  317. {
  318. return "-GB";
  319. }
  320. }
  321. }
  322. int gb = 1024 * 1024 * 1024;
  323. return ((double)capacity / gb).ToString("0.0") + "GB";
  324. }
  325. //************************************ 公有方法 **********************************
  326. public Monitor(centralController.centralController mainWin, IntPtr flpHandle)
  327. {
  328. Monitor.mainWin = mainWin;
  329. Monitor.flpHandle = flpHandle;
  330. parkingSpaceInfo = new List<ParkingSpaceStru>();
  331. mainBlockInfo = new MainBlockStru();
  332. }
  333. ///// <summary>
  334. ///// 获取提示信息
  335. ///// </summary>
  336. ///// <returns></returns>
  337. //public string GetNotification()
  338. //{
  339. // StringBuilder notificationStr = new StringBuilder();
  340. // lock (notificationQueue)
  341. // {
  342. // string[] strs = notificationQueue.ToArray();
  343. // Array.Reverse(strs);
  344. // for (int i = 0; i < strs.Length; i++)
  345. // {
  346. // notificationStr.Append(strs[i]);
  347. // }
  348. // //Queue<string>.Enumerator notiEnumer = notificationQueue.GetEnumerator();
  349. // //while (notiEnumer.MoveNext())
  350. // //{
  351. // // notificationStr.Append(notiEnumer.Current);
  352. // //}
  353. // }
  354. // return notificationStr.ToString();
  355. //}
  356. /// <summary>
  357. /// 添加提示信息
  358. /// </summary>
  359. /// <param name="notification"></param>
  360. public static void SetNotification(string notification, TextColor textColor=TextColor.Log)
  361. {
  362. if (notification == "clear")
  363. {
  364. //lock (notificationQueue)
  365. //{
  366. // notificationQueue.Clear();
  367. //}
  368. if (mainWin != null)
  369. {
  370. mainWin.Invoke(new Action(() => {
  371. mainWin.UpdateText(notification, textColor,MAXLINES);
  372. }));
  373. }
  374. return;
  375. }
  376. else
  377. {
  378. string time = DateTime.Now + "\r\n";
  379. string notificationStr = time + notification + "\r\n";
  380. //lock (notificationQueue)
  381. //{
  382. // int count = notificationQueue.Count;
  383. // if (count >= MAXLINES)
  384. // {
  385. // notificationQueue.Dequeue();
  386. // }
  387. // notificationQueue.Enqueue(notificationStr);
  388. //}
  389. if (mainWin != null)
  390. {
  391. mainWin.Invoke(new Action(() => {
  392. mainWin.UpdateText(notificationStr, textColor, MAXLINES);
  393. }));
  394. }
  395. }
  396. }
  397. /// <summary>
  398. /// 返回系统信息字符串
  399. /// </summary>
  400. /// <returns></returns>
  401. public static string GetSysInfo()
  402. {
  403. string info = "";
  404. Language lng = Language.ins;
  405. try
  406. {
  407. string endl = "\r\n";
  408. info += endl + lng.autoPackSys + " " + lng.centralPort + endl + endl;
  409. info += lng.version + ":" + SysConst.version() + endl + endl;
  410. info += lng.hostNmae + ":" + Dns.GetHostName() + endl + endl;
  411. info += lng.os + ":" + getOsVersion() + endl + endl;
  412. info += lng.cpu + ":" + getCPUName() + endl + endl;
  413. info += lng.mem + ":" + getMenSize() + endl + endl;
  414. info += lng.gpu + ":" + getGPUName() + endl + endl;
  415. }
  416. catch (Exception) { }
  417. return info;
  418. }
  419. /// <summary>
  420. /// 返回关于我们信息字符串
  421. /// </summary>
  422. /// <returns></returns>
  423. public static string GetAboutInfo()
  424. {
  425. return Language.ins.aboutWinText;
  426. }
  427. /// <summary>
  428. /// 返回停车记录信息
  429. /// </summary>
  430. /// <returns></returns>
  431. public static List<object[]> GetParkingRecords(string license = "", string startTime = "", string endTime = "")
  432. {
  433. DateTime now = DateTime.Now;
  434. List<object[]> result = new List<object[]>();
  435. string getParkingRecordsSql = "";
  436. if (startTime == "" || endTime == "")
  437. {
  438. DateTime yesterday = DateTime.Now - (new TimeSpan(1, 0, 0, 0));
  439. DateTime twoDaysAgo = DateTime.Now - (new TimeSpan(2, 0, 0, 0));
  440. getParkingRecordsSql = "select parkingRecordsID,userID,numberPlate,parkingSpaceID,realParkTime,realGetTime,receiptNum,parkingPrice,paymentStatus " +
  441. "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") + "%');";
  442. }
  443. else
  444. {
  445. getParkingRecordsSql = "select parkingRecordsID,userID,numberPlate,parkingSpaceID,realParkTime,realGetTime,receiptNum,parkingPrice,paymentStatus " +
  446. "from parkingrecords where numberPlate " + (license == "" ? "like '%" : "= '" + license) + "' and realParkTime >= '" + startTime + "' and realParkTime <= '" + endTime + "';";
  447. }
  448. if (localDBOper != null)
  449. {
  450. lock (localDBOper)
  451. {
  452. MySqlDataReader reader = localDBOper.Query(getParkingRecordsSql);
  453. try
  454. {
  455. while (reader != null && reader.Read())
  456. {
  457. if (reader.HasRows)
  458. {
  459. object[] temp = new object[reader.FieldCount];
  460. reader.GetValues(temp);
  461. result.Add(temp);
  462. }
  463. }
  464. }
  465. catch (Exception e) { Console.WriteLine(e.Message); }
  466. try
  467. {
  468. if (reader != null)
  469. {
  470. reader.Close();
  471. reader.Dispose();
  472. }
  473. }
  474. catch (Exception e) { Console.WriteLine(e.Message); }
  475. }
  476. }
  477. result.Reverse();
  478. return result;
  479. }
  480. /// <summary>
  481. /// 返回预约记录信息
  482. /// </summary>
  483. /// <returns></returns>
  484. public static List<object[]> GetOrderRecords(bool localDB, string license="", string startTime = "", string endTime = "")
  485. {
  486. DateTime now = DateTime.Now;
  487. List<object[]> result = new List<object[]>();
  488. string getOrderRecordsSql = "";
  489. if (startTime == "" || endTime == "")
  490. {
  491. DateTime yesterday = DateTime.Now - (new TimeSpan(1, 0, 0, 0));
  492. DateTime twoDaysAgo = DateTime.Now - (new TimeSpan(2, 0, 0, 0));
  493. getOrderRecordsSql = "select orderRecordsID,userID,numberPlate,bookParkTime,cancelBookTime,bookFetchTime,bookHour,bookPrice,bookState " +
  494. "from orderrecords where numberPlate " + (license == "" ? "like '%" : "= '" + license) + "' and (bookParkTime like '" + now.ToString("yyyy-MM-dd") +
  495. "%' or bookParkTime like '" + yesterday.ToString("yyyy-MM-dd") + "%' or bookFetchTime like '" + now.ToString("yyyy-MM-dd") + "%' or bookFetchTime like '" + yesterday.ToString("yyyy-MM-dd") + "%');";
  496. }
  497. else
  498. {
  499. getOrderRecordsSql = "select orderRecordsID,userID,numberPlate,bookParkTime,cancelBookTime,bookFetchTime,bookHour,bookPrice,bookState " +
  500. "from orderrecords where numberPlate " + (license == "" ? "like '%" : "= '" + license) + "' and ((bookParkTime >= '" + startTime + "' and bookParkTime <= '" + endTime + "') or (bookFetchTime >= '" + startTime + "' and bookFetchTime <= '" + endTime + "'));";
  501. }
  502. if (localDBOper != null)
  503. {
  504. lock (localDBOper)
  505. {
  506. MySqlDataReader reader;
  507. if (localDB)
  508. reader = localDBOper.Query(getOrderRecordsSql);
  509. else
  510. reader = remoteDBOper.Query(getOrderRecordsSql);
  511. try
  512. {
  513. while (reader != null && reader.Read())
  514. {
  515. if (reader.HasRows)
  516. {
  517. object[] temp = new object[reader.FieldCount];
  518. reader.GetValues(temp);
  519. result.Add(temp);
  520. }
  521. }
  522. }
  523. catch (Exception e) { Console.WriteLine(e.Message); }
  524. try
  525. {
  526. if (reader != null)
  527. {
  528. reader.Close();
  529. reader.Dispose();
  530. }
  531. }
  532. catch (Exception e) { Console.WriteLine(e.Message); }
  533. }
  534. }
  535. result.Reverse();
  536. return result;
  537. }
  538. /// <summary>
  539. /// 更新停车记录关于支付的信息
  540. /// </summary>
  541. /// <param name="license"></param>
  542. /// <param name="receiptNum"></param>
  543. /// <param name="parkingFee"></param>
  544. /// <param name="paymentStatus"></param>
  545. /// <returns></returns>
  546. public static bool UpdateParkingRecords(string license, string receiptNum, string parkingFee, string paymentStatus, bool local)
  547. {
  548. bool result = false;
  549. string updateRecordsSql = "update parkingrecords set parkingPrice = " + parkingFee + " ,paymentStatus = " + paymentStatus + " where numberPlate = '" + license + "' and receiptNum = " + receiptNum+";";
  550. List<string> updateList = new List<string>();
  551. updateList.Add(updateRecordsSql);
  552. if (local && localDBOper != null)
  553. {
  554. lock (localDBOper)
  555. {
  556. try
  557. {
  558. result = localDBOper.UpdateTransaction(updateList);
  559. }
  560. catch { Log.WriteLog(LogType.database, "更新本地停车记录中支付信息出现异常。\n" + updateRecordsSql); return false; }
  561. }
  562. }else if(!local && remoteDBOper != null)
  563. {
  564. lock (remoteDBOper)
  565. {
  566. try
  567. {
  568. result = remoteDBOper.UpdateTransaction(updateList);
  569. }
  570. catch { Log.WriteLog(LogType.database, "更新云端记录中支付信息出现异常。\n" + updateRecordsSql); return false; }
  571. }
  572. }
  573. return result;
  574. }
  575. /// <summary>
  576. /// 更新终端停车费用,便于车辆放行
  577. /// </summary>
  578. /// <returns></returns>
  579. public static bool UpdateTermFeeStatus(string receiptNum)
  580. {
  581. for (int i = 0; i < Terminal.Terminal.terminalInfo.Count; i++)
  582. {
  583. if(Terminal.Terminal.terminalInfo[i].receiptNum.ToString() == receiptNum)
  584. {
  585. if(Terminal.Terminal.terminalInfo[i].terminalStatus==2 && Terminal.Terminal.terminalInfo[i].paymentStatus == 3)
  586. {
  587. TerminalStru tsFromCentral = new TerminalStru
  588. {
  589. terminalID = (short)Terminal.Terminal.terminalInfo[i].terminalID,
  590. parkingFee = (short)-1,
  591. receiptNum = -1,
  592. paymentStatus = 3,
  593. };
  594. PLC.WriteToPLC(tsFromCentral, PLCDataType.central);
  595. return true;
  596. }
  597. else
  598. {
  599. SetNotification((Terminal.Terminal.terminalInfo[i].terminalStatus == 1 ? "停车" : "非取车状态")+"终端,支付状态:"+ Terminal.Terminal.terminalInfo[i].paymentStatus+",不进行修改",TextColor.Warning);
  600. Log.WriteLog(LogType.process,LogFile.ERROR, (Terminal.Terminal.terminalInfo[i].terminalStatus == 1 ? "停车" : "非取车状态") + "终端,支付状态:" + Terminal.Terminal.terminalInfo[i].paymentStatus + ",不进行修改");
  601. return false;
  602. }
  603. }
  604. }
  605. SetNotification("未找到待修改终端", TextColor.Warning);
  606. Log.WriteLog(LogType.process, LogFile.ERROR, "未找到待修改终端");
  607. return false;
  608. }
  609. /// <summary>
  610. /// 返回空闲正常车位数
  611. /// </summary>
  612. /// <param name="state">状态:0.空闲与保留,1.空闲,2.保留,3.预约车位总数</param>
  613. /// <returns></returns>
  614. public int GetFreeSpaceCount(int state)
  615. {
  616. int freeSpaceCount = 0;
  617. int bookableSpaceCount = 0;
  618. int bookedSpaceCount = 0;
  619. if(state == 3)
  620. {
  621. return mainBlockInfo.reserveTotalSpace;
  622. }
  623. try
  624. {
  625. if (parkingSpaceInfo != null)
  626. {
  627. foreach (ParkingSpaceStru psStru in parkingSpaceInfo)
  628. {
  629. if (psStru.spaceStatus == 3)
  630. bookedSpaceCount++;
  631. if (psStru.spaceStatus != 1 && psStru.spaceStatus != 3)
  632. {
  633. freeSpaceCount++;
  634. if (psStru.spaceStatus == 2)
  635. bookableSpaceCount++;
  636. }
  637. }
  638. }
  639. }
  640. catch { Console.WriteLine("空闲车位数,计数异常");return 0; }
  641. switch (state)
  642. {
  643. case 0:
  644. return freeSpaceCount;//0+2
  645. case 1:
  646. return freeSpaceCount - bookableSpaceCount;//0
  647. case 2:
  648. return bookableSpaceCount;//2
  649. case 4:
  650. return bookedSpaceCount + bookableSpaceCount;//2+3
  651. default:
  652. return freeSpaceCount;
  653. }
  654. }
  655. /// <summary>
  656. /// 暂未使用,查询本地数据库返回可预约车位数
  657. /// </summary>
  658. /// <returns></returns>
  659. //public int GetBookableSpaceCount()
  660. //{
  661. // int bookableSpaceCount = 0;
  662. // string bookableSpaceSql = "select currentBookableSpace from garageproperties where garageID = " + garageID + ";";
  663. // if (localDBOper != null)
  664. // {
  665. // MySqlDataReader reader = localDBOper.Query(bookableSpaceSql);
  666. // if (reader != null)
  667. // {
  668. // try
  669. // {
  670. // if (reader.Read() && reader.HasRows)
  671. // {
  672. // bookableSpaceCount = reader.GetInt32("currentBookableSpace");
  673. // }
  674. // }
  675. // catch { }
  676. // try
  677. // {
  678. // reader.Close();
  679. // reader.Dispose();
  680. // }
  681. // catch { }
  682. // }
  683. // }
  684. // return bookableSpaceCount;
  685. //}
  686. /// <summary>
  687. /// 系统初始化,启动plc监控
  688. /// </summary>
  689. public void Start()
  690. {
  691. if (flpHandle != IntPtr.Zero)
  692. {
  693. //初始化系统
  694. if (sysInitializer == null)
  695. {
  696. sysInitializer = new SystemInitializer();
  697. }
  698. Task.Factory.StartNew(() =>
  699. {
  700. sysInitializer.Init(flpHandle);
  701. });
  702. //更新PLC数据
  703. Thread PLCUpdateThread = new Thread(PLCUpdate);
  704. PLCUpdateThread.Start();
  705. //System.Windows.Forms.Timer PLCUpdateTimer = new System.Windows.Forms.Timer();
  706. //PLCUpdateTimer.Interval = 300;
  707. //PLCUpdateTimer.Tick += new EventHandler(PLCUpdate);
  708. //PLCUpdateTimer.Start();
  709. }
  710. }
  711. /// <summary>
  712. /// 系统停止
  713. /// </summary>
  714. public void Stop()
  715. {
  716. sysInitializer.Stop();
  717. System.Environment.Exit(0);
  718. }
  719. }
  720. //public class ParkingRecord
  721. //{
  722. // int parkingRecordsID;
  723. // int userID;
  724. // string numberPlate;
  725. // int parkingSpaceID;
  726. //}
  727. }