Monitor.cs 32 KB

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