Monitor.cs 30 KB

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