Monitor.cs 29 KB

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