Terminal.cs 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  1. using Terminal;
  2. using db;
  3. using System.Collections.Generic;
  4. using System.Threading.Tasks;
  5. using PLCS7;
  6. using System;
  7. using System.Text;
  8. using MySql.Data.MySqlClient;
  9. using System.Threading;
  10. using BroadcastModule;
  11. using parkMonitor.LOG;
  12. using centralController.model;
  13. namespace Terminal
  14. {
  15. public class Terminal
  16. {
  17. public static List<TerminalStru> terminalInfo = new List<PLCS7.TerminalStru>();
  18. /// <summary>
  19. /// 记录地面停车位id与获取号牌映射
  20. /// </summary>
  21. private static Dictionary<int, string> idLicMap = new Dictionary<int, string>();
  22. /// <summary>
  23. /// 取车地面位id与是否已计费标记映射
  24. /// </summary>
  25. public static Dictionary<int, bool> termUsedMap = new Dictionary<int, bool>();
  26. private static Dictionary<int, FetchInfo> fetchMap = new Dictionary<int, FetchInfo>();
  27. private static List<bool> lockList = new List<bool>();
  28. public static bool isClosing = false;
  29. public void FeeCal()
  30. {
  31. }
  32. public void GetTerminalState()
  33. {
  34. }
  35. private object SearchPaymentScheme()
  36. {
  37. return null;
  38. }
  39. private void UpdateParkingRecords(int Object)
  40. {
  41. }
  42. /// <summary>
  43. /// 从终端结构体中获得车牌号
  44. /// </summary>
  45. /// <param name="ts"></param>
  46. /// <returns></returns>
  47. private static string GetLicenseFromTerm(TerminalStru ts)
  48. {
  49. string header = Encoding.ASCII.GetString(BitConverter.GetBytes(ts.licenseCodeB));
  50. string identityA = Encoding.ASCII.GetString(BitConverter.GetBytes(ts.licenseCodeC));
  51. byte[] bytes = BitConverter.GetBytes(ts.licenseCodeD);
  52. List<byte> newBytes = new List<byte>();
  53. for (int i = 0; i < bytes.Length; i++)
  54. {
  55. if (bytes[i] != 0x00) { newBytes.Add(bytes[i]); }
  56. }
  57. string identityB = Encoding.ASCII.GetString(newBytes.ToArray());
  58. Log.WriteLog(LogType.process, LogFile.INFO, "已从终端" + ts.terminalID + "获得号牌:" + header + identityA + identityB);
  59. //根据省份对应的两位数字查找汉字
  60. string province = header.Substring(0, 2);
  61. string provinceIndex = header.Substring(2);
  62. string provinceQuerySql = "select NumberPlateHeader from numberplatemapping where mappedString = " + province + ";";
  63. lock (Monitor.Monitor.localDBOper)
  64. {
  65. //object[] result = Monitor.Monitor.localDBOper.Query(provinceQuerySql);
  66. MySqlDataReader reader = Monitor.Monitor.localDBOper.Query(provinceQuerySql);
  67. try
  68. {
  69. if (reader != null && reader.Read() && reader.HasRows)
  70. {
  71. province = reader.GetString("NumberPlateHeader");
  72. //Console.WriteLine(province);
  73. }
  74. }
  75. catch { }
  76. try
  77. {
  78. //if (result!=null && result.Length > 0) { province = (string)result[0]; }
  79. if (reader != null)
  80. {
  81. reader.Close();
  82. reader.Dispose();
  83. }
  84. }
  85. catch { }
  86. }
  87. return province + provinceIndex + identityA + identityB;
  88. }
  89. /// <summary>
  90. /// 更新所有车位信息
  91. /// </summary>
  92. private static void UpdateAllParkingSpace()
  93. {
  94. string findParkingSpace = "select parkingSpaceID from parkingspace;";
  95. HashSet<int> recordsIDSet = new HashSet<int>();
  96. lock (Monitor.Monitor.localDBOper)
  97. {
  98. //object[] result = Monitor.Monitor.localDBOper.Query(findParkingSpace);
  99. //if(result!=null && result.Length > 0)
  100. //{
  101. // for (int i = 0; i < result.Length; i++)
  102. // {
  103. // recordsIDSet.Add((int)result[i]);
  104. // }
  105. //}
  106. MySqlDataReader reader = Monitor.Monitor.localDBOper.Query(findParkingSpace);
  107. if (reader != null)
  108. {
  109. try
  110. {
  111. while (reader.Read())
  112. {
  113. if (reader.HasRows)
  114. {
  115. recordsIDSet.Add(reader.GetInt32("parkingSpaceID"));
  116. }
  117. }
  118. }
  119. catch { }
  120. try
  121. {
  122. reader.Close();
  123. reader.Dispose();
  124. }
  125. catch { }
  126. }
  127. }
  128. List<string> updateSpaceList = new List<string>();
  129. List<string> insertSpaceList = new List<string>();
  130. string updateParkingSpace = "";
  131. string insertParkingSpace = "";
  132. for (int i = 0; i < Monitor.Monitor.parkingSpaceInfo.Count; i++)
  133. {
  134. if (recordsIDSet.Contains(Monitor.Monitor.parkingSpaceInfo[i].parkingSpace))
  135. {
  136. updateParkingSpace = "update parkingspace set parkingSpaceX = " + Monitor.Monitor.parkingSpaceInfo[i].coordX +
  137. ",parkingSpaceY = " + Monitor.Monitor.parkingSpaceInfo[i].coordY + ",parkingSpaceZ = " + Monitor.Monitor.parkingSpaceInfo[i].floorNo + ",parkingSpaceState = " + Monitor.Monitor.parkingSpaceInfo[i].spaceStatus + " where (parkingSpaceID = " + Monitor.Monitor.parkingSpaceInfo[i].parkingSpace + ");";
  138. updateSpaceList.Add(updateParkingSpace);
  139. }
  140. else
  141. {
  142. insertParkingSpace = "insert into parkingspace (parkingSpaceID,parkingSpaceX,parkingSpaceY,parkingSpaceZ,parkingSpaceState) values (" + Monitor.Monitor.parkingSpaceInfo[i].parkingSpace + "," + Monitor.Monitor.parkingSpaceInfo[i].coordX +
  143. "," + Monitor.Monitor.parkingSpaceInfo[i].coordY + "," + Monitor.Monitor.parkingSpaceInfo[i].floorNo + "," + Monitor.Monitor.parkingSpaceInfo[i].spaceStatus + ");";
  144. insertSpaceList.Add(insertParkingSpace);
  145. }
  146. }
  147. try
  148. {
  149. lock (Monitor.Monitor.localDBOper)
  150. {
  151. Monitor.Monitor.localDBOper.UpdateTransaction(updateSpaceList);
  152. Monitor.Monitor.localDBOper.Insert(insertSpaceList);
  153. }
  154. }
  155. catch (Exception e) { Log.WriteLog(LogType.process, LogFile.ERROR, "更新本地所有车位异常"); Console.WriteLine("更新本地所有车位异常" + e.Message); }
  156. }
  157. /// <summary>
  158. /// 更新车辆状态
  159. /// </summary>
  160. /// <param name="lic"></param>
  161. /// <param name="state"></param>
  162. private static void UpdateVehicle(string lic, int state, int parkingRecordsID, bool park, bool remote, int parkingSpaceID)
  163. {
  164. if (lic != "")
  165. {
  166. MySqlDataReader reader;
  167. //查询车辆是否在车辆表中
  168. string checkVehicleState = "select * from vehicle where numberPlate = '" + lic + "';";
  169. if (!remote)
  170. {
  171. reader = Monitor.Monitor.localDBOper.Query(checkVehicleState);
  172. }
  173. else
  174. {
  175. reader = Monitor.Monitor.remoteDBOper.Query(checkVehicleState);
  176. }
  177. if (reader != null)
  178. {
  179. if (reader.Read() && reader.HasRows)
  180. {
  181. //更新车辆状态
  182. string updateVehicleState = "";
  183. string updateRemoteVehicleState = "";
  184. if (park)
  185. {
  186. if (parkingRecordsID > 0)
  187. {
  188. if (parkingSpaceID == 0)
  189. {
  190. updateVehicleState = "update vehicle set vehiclepParkState = " + state + " ,parkingRecordsID = " + parkingRecordsID + " where numberPlate = '" + lic + "';";
  191. updateRemoteVehicleState = "update vehicle set vehiclepParkState = " + state + " ,parkingRecordsID = " + parkingRecordsID + ",garageID = " + Monitor.Monitor.garageID + " where numberPlate = '" + lic + "';";
  192. }
  193. else
  194. {
  195. updateVehicleState = "update vehicle set vehiclepParkState = " + state + " ,parkingRecordsID = " + parkingRecordsID + ",parkingSpaceID = " + parkingSpaceID + " where numberPlate = '" + lic + "';";
  196. updateRemoteVehicleState = "update vehicle set vehiclepParkState = " + state + " ,parkingRecordsID = " + parkingRecordsID + ",parkingSpaceID = " + parkingSpaceID + ",garageID = " + Monitor.Monitor.garageID + " where numberPlate = '" + lic + "';";
  197. }
  198. }
  199. else
  200. {
  201. updateVehicleState = "update vehicle set vehiclepParkState = " + state + " where numberPlate = '" + lic + "';";
  202. updateRemoteVehicleState = updateVehicleState;
  203. }
  204. }
  205. else
  206. {
  207. updateVehicleState = "update vehicle set vehiclepParkState = " + state + " where numberPlate = '" + lic + "';";
  208. updateRemoteVehicleState = updateVehicleState;
  209. }
  210. List<string> list = new List<string>();
  211. if (!remote)
  212. {
  213. list.Add(updateVehicleState);
  214. Monitor.Monitor.localDBOper.UpdateTransaction(list);
  215. }
  216. else
  217. {
  218. list.Add(updateRemoteVehicleState);
  219. Monitor.Monitor.remoteDBOper.UpdateTransaction(list);
  220. }
  221. }
  222. else
  223. {
  224. //插入车辆
  225. string insertVehicleWithState = "";
  226. if (park)
  227. {
  228. if (parkingRecordsID > 0)
  229. insertVehicleWithState = "insert into vehicle (numberPlate,vehicleTypeID,vehiclepParkState,parkingRecordsID) values " +
  230. "('" + lic + "',NULL,'" + state + "'," + parkingRecordsID + ");";
  231. else
  232. insertVehicleWithState = "insert into vehicle (numberPlate,vehicleTypeID,vehiclepParkState) values " +
  233. "('" + lic + "',NULL,'" + state + "');";
  234. }
  235. else
  236. {
  237. Log.WriteLog(LogType.process, LogFile.ERROR, "取车无法获得车辆信息");
  238. Console.WriteLine("明显异常,取车发现无车辆");
  239. try
  240. {
  241. reader.Close();
  242. reader.Dispose();
  243. }
  244. catch { }
  245. return;
  246. }
  247. List<string> list = new List<string>();
  248. list.Add(insertVehicleWithState);
  249. if (!remote)
  250. {
  251. Monitor.Monitor.localDBOper.Insert(list);
  252. }
  253. else
  254. {
  255. Monitor.Monitor.remoteDBOper.Insert(list);
  256. }
  257. }
  258. try
  259. {
  260. reader.Close();
  261. reader.Dispose();
  262. }
  263. catch { }
  264. }
  265. }
  266. }
  267. /// <summary>
  268. /// 流程结束清除终端相关信息
  269. /// </summary>
  270. /// <param name="id"></param>
  271. private static void ClearTerminal(int id)
  272. {
  273. TerminalStru tsFromCentral = new TerminalStru
  274. {
  275. terminalID = (short)id,
  276. parkingFee = (short)32767,
  277. };
  278. TerminalStru tsFromTerminal = new TerminalStru
  279. {
  280. terminalID = (short)id,
  281. btnStatus = (short)0,
  282. };
  283. Monitor.Monitor.PLC.WriteToPLC(tsFromCentral, PLCDataType.central);
  284. Monitor.Monitor.PLC.WriteToPLC(tsFromTerminal, PLCDataType.terminal);
  285. }
  286. /// <summary>
  287. /// 验证会员信息
  288. /// </summary>
  289. /// <param name="ts"></param>
  290. /// <param name="userID"></param>
  291. /// <param name="license"></param>
  292. /// <param name="status"></param>
  293. private static void ValidateUserInfo(TerminalStru ts, int userID, string license, out int status)
  294. {
  295. status = 0;
  296. //与云端数据比对
  297. string checkNetSql = "select * from user where userID = 1;";
  298. MySqlDataReader readerTest = Monitor.Monitor.remoteDBOper.Query(checkNetSql);
  299. if (readerTest != null)
  300. {
  301. if (readerTest.Read() && readerTest.HasRows)
  302. {
  303. string userInfoCheckSql = "select * from usercarrelation where userID = '" + userID + "'and numberPlate = '" + license + "';";
  304. MySqlDataReader reader = Monitor.Monitor.remoteDBOper.Query(userInfoCheckSql);
  305. if (reader != null && reader.Read() && reader.HasRows && userID != 0 && license != "")
  306. {
  307. ts.licVerification = 1;//验证成功
  308. Monitor.Monitor.PLC.WriteToPLC(ts, PLCDataType.central);
  309. status = 1;
  310. Log.WriteLog(LogType.process, LogFile.INFO, "号牌验证成功");
  311. Monitor.Monitor.SetNotification("注册用户,号牌验证成功",parkMonitor.model.TextColor.Info);
  312. }
  313. else
  314. {
  315. ts.licVerification = 2;//验证失败
  316. Monitor.Monitor.PLC.WriteToPLC(ts, PLCDataType.central);
  317. status = 2;
  318. Log.WriteLog(LogType.process, LogFile.WARNING, "号牌验证失败");
  319. Monitor.Monitor.SetNotification("注册用户,号牌验证失败",parkMonitor.model.TextColor.Error);
  320. }
  321. try
  322. {
  323. reader.Close();
  324. reader.Dispose();
  325. }
  326. catch { }
  327. }
  328. else
  329. {
  330. ts.licVerification = 1;//网络异常,跳过验证
  331. Monitor.Monitor.PLC.WriteToPLC(ts, PLCDataType.central);
  332. status = 1;
  333. Log.WriteLog(LogType.process, LogFile.ERROR, "网络异常,无法验证号牌");
  334. }
  335. try
  336. {
  337. readerTest.Close();
  338. readerTest.Dispose();
  339. }
  340. catch { }
  341. }
  342. }
  343. /// <summary>
  344. /// 查询停车记录id
  345. /// </summary>
  346. /// <param name="querySql"></param>
  347. /// <param name="recordsID"></param>
  348. /// <param name="remote"></param>
  349. private static void QueryParkingRecordsID(string querySql, out int recordsID, bool remote)
  350. {
  351. recordsID = 0;
  352. MySqlDataReader reader = null;
  353. if (!remote)
  354. {
  355. reader = Monitor.Monitor.localDBOper.Query(querySql);
  356. }
  357. else
  358. {
  359. reader = Monitor.Monitor.remoteDBOper.Query(querySql);
  360. }
  361. if (reader != null)
  362. {
  363. try
  364. {
  365. if (reader.Read())
  366. {
  367. recordsID = reader.GetInt32("parkingRecordsID");
  368. }
  369. }
  370. catch (Exception e) { Log.WriteLog(LogType.process, LogFile.ERROR, "查询停车记录id失败"); ; Console.WriteLine(e.Message); };
  371. try
  372. {
  373. reader.Close();
  374. reader.Dispose();
  375. }
  376. catch { }
  377. }
  378. }
  379. private static void BookedCarProcess()
  380. {
  381. for (int i = 0; i < terminalInfo.Count; i++)
  382. {
  383. if (terminalInfo[i].groundStatus == 1 && terminalInfo[i].btnStatus == 0)
  384. {
  385. string license = Monitor.Monitor.numMachineLinker.GetLicensePlate(terminalInfo[i].terminalID);
  386. if (license == "") { break; }
  387. else
  388. {
  389. }
  390. }
  391. }
  392. }
  393. #region 获取号牌方法,已调整
  394. /// <summary>
  395. /// 停车流程,收到号牌机启动后操作过程
  396. /// </summary>
  397. private static void ParkNumSubProcess()
  398. {
  399. for (int i = 0; i < terminalInfo.Count; i++)
  400. {
  401. //启动指令与终端id匹配
  402. //if (numMachineLaunch == terminalInfo[i].terminalID)
  403. if (terminalInfo[i].numMachineLaunch == 1 && terminalInfo[i].licVerification == 0)
  404. {
  405. int numReceivedStatus = 0;//1获得,2终止
  406. TerminalStru term = terminalInfo[i];
  407. if (term.terminalStatus == (short)1)
  408. {
  409. string license = "";
  410. //判断之前是否已拍摄号牌
  411. if (!idLicMap.ContainsKey(terminalInfo[i].terminalID))
  412. {
  413. license = Monitor.Monitor.numMachineLinker.GetLicensePlate(terminalInfo[i].terminalID);
  414. Monitor.Monitor.SetNotification("启动" + terminalInfo[i].terminalID + "号号牌机,号牌:" + license,parkMonitor.model.TextColor.Info);
  415. }
  416. else
  417. {
  418. idLicMap.TryGetValue(terminalInfo[i].terminalID, out license);
  419. }
  420. //未获得号牌,告知PLC终止,告诉终端提示用户重新操作
  421. if (license == "")
  422. {
  423. MainBlockStru mb = new MainBlockStru
  424. {
  425. centralHearbeat = (short)-1,
  426. bookParkCmd = -1,
  427. bookFetchCmd = -1,
  428. licenseReceived = (short)2
  429. };
  430. Monitor.Monitor.PLC.WriteToPLC(mb, PLCDataType.central);
  431. TerminalStru ts = new TerminalStru
  432. {
  433. terminalID = (short)terminalInfo[i].terminalID,
  434. paymentStatus = -1,
  435. parkingFee = -1,
  436. userType = -1,
  437. licVerification = (short)2
  438. };
  439. Monitor.Monitor.PLC.WriteToPLC(ts, PLCDataType.central);
  440. Log.WriteLog(LogType.process, LogFile.ERROR, "未获得号牌");
  441. }
  442. else
  443. {
  444. //记录或更新当前号牌
  445. lock (idLicMap)
  446. {
  447. try
  448. {
  449. if (idLicMap.ContainsKey(terminalInfo[i].terminalID))
  450. {
  451. if (!idLicMap[terminalInfo[i].terminalID].Equals(license))
  452. idLicMap[terminalInfo[i].terminalID] = license;
  453. }
  454. else
  455. {
  456. idLicMap.Add(terminalInfo[i].terminalID, license);
  457. }
  458. }
  459. catch(Exception e) { Log.WriteLog(LogType.process, LogFile.ERROR, "号牌" + license + "记录号牌异常\n"+e.StackTrace); }
  460. }
  461. Log.WriteLog(LogType.process, LogFile.ERROR, "号牌" + license + "已记录");
  462. TerminalStru ts = new TerminalStru
  463. {
  464. terminalID = (short)terminalInfo[i].terminalID,
  465. paymentStatus = -1,
  466. parkingFee = -1,
  467. userType = -1
  468. };
  469. //注册用户
  470. if (term.btnStatus == (short)2 || term.btnStatus == (short)3)
  471. {
  472. int userID = term.licenseCodeA;
  473. string userLicense = GetLicenseFromTerm(term);
  474. //与云端数据比对
  475. ValidateUserInfo(ts, userID, userLicense, out numReceivedStatus);
  476. if (numReceivedStatus == 2)
  477. {
  478. idLicMap.Remove(terminalInfo[i].terminalID);
  479. }
  480. }
  481. //非注册用户
  482. else if (term.btnStatus == (short)1 || term.btnStatus == (short)4)
  483. {
  484. numReceivedStatus = 1;
  485. ts.licVerification = 1;
  486. Monitor.Monitor.PLC.WriteToPLC(ts, PLCDataType.central);
  487. }
  488. //无论是否注册,皆告知PLC,已获取号牌或比对异常、终止流程
  489. MainBlockStru mb = new MainBlockStru
  490. {
  491. centralHearbeat = -1,
  492. bookFetchCmd = -1,
  493. bookParkCmd = -1,
  494. licenseReceived = (short)numReceivedStatus
  495. };
  496. Monitor.Monitor.PLC.WriteToPLC(mb, PLCDataType.central);
  497. //号牌获取结果被清零才跳出
  498. Monitor.Monitor.SetNotification("已写入号牌获取结果,等待号牌机启动指令清零",parkMonitor.model.TextColor.Info);
  499. }
  500. int countdown = 5;
  501. while (countdown-- > 0)
  502. {
  503. if (term.numMachineLaunch == 0)
  504. {
  505. Monitor.Monitor.SetNotification("号牌机启动指令已被清零",parkMonitor.model.TextColor.Info); break;
  506. }
  507. else
  508. Thread.Sleep(1000);
  509. }
  510. }
  511. }
  512. }
  513. }
  514. #endregion
  515. //private static void ParkNumSubProcess()
  516. //{
  517. //}
  518. /// <summary>
  519. /// 停车流程,收到PLC停车完成信号后操作过程
  520. /// </summary>
  521. private static void ParkCompleteSubProcess()
  522. {
  523. int processAttrib = Monitor.Monitor.mainBlockInfo.parkingRunning;
  524. //int processCompleted = Monitor.Monitor.mainBlockInfo.processCompleted;
  525. int currentTerm = Monitor.Monitor.mainBlockInfo.terminalID;
  526. int parkingSpaceID = 0;
  527. if (processAttrib == 1)
  528. {
  529. TerminalStru term;
  530. string license;
  531. //等待完成信号
  532. while (Monitor.Monitor.mainBlockInfo.processCompleted != 1)
  533. {
  534. if (Monitor.Monitor.mainBlockInfo.processStopped == 1)
  535. return;
  536. Thread.Sleep(200);
  537. }
  538. //找到终端号,判断是否注册用户
  539. //拿到号牌
  540. if (/*terminalInfo[i].terminalID == currentTerm && */idLicMap.TryGetValue(currentTerm, out license) && license != "")
  541. {
  542. term = terminalInfo[currentTerm - 1];
  543. Thread.Sleep(1500);
  544. //找到停车位置
  545. for (int s = 0; s < Monitor.Monitor.parkingSpaceInfo.Count; s++)
  546. {
  547. Console.WriteLine(Monitor.Monitor.parkingSpaceInfo[s].receiptNum + "," + term.receiptNum);
  548. if (Monitor.Monitor.parkingSpaceInfo[s].receiptNum == term.receiptNum)
  549. {
  550. parkingSpaceID = Monitor.Monitor.parkingSpaceInfo[s].parkingSpace;
  551. break;
  552. }
  553. }
  554. //无车位信息则跳出当前循环
  555. if (parkingSpaceID == 0 || term.receiptNum == 0)
  556. {
  557. try { idLicMap.Remove(currentTerm); } catch { }
  558. Monitor.Monitor.SetNotification("无车位或凭证号",parkMonitor.model.TextColor.Warning);
  559. return;
  560. }
  561. UpdateVehicle(license, 0, 0, true, false, 0);
  562. UpdateVehicle(license, 0, 0, true, true, 0);
  563. //插入停车记录
  564. string parkingRecordsSql = "";
  565. if (term.licenseCodeA != 0)
  566. {
  567. parkingRecordsSql = "INSERT INTO parkingrecords (userID, numberPlate,parkingSpaceID,garageID,parkingRecordsState,realParkTime,receiptNum,parkingPrice)" +
  568. "values ('" + term.licenseCodeA + "','" + license + "','" + parkingSpaceID + "','" + Monitor.Monitor.garageID + "',3,'" + DateTime.Now.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss") + "','" + term.receiptNum + "',NULL);";
  569. }
  570. else
  571. {
  572. parkingRecordsSql = "INSERT INTO parkingrecords (userID, numberPlate,parkingSpaceID,garageID,parkingRecordsState,realParkTime,receiptNum,parkingPrice)" +
  573. "values ('" + 1 + "','" + license + "','" + parkingSpaceID + "','" + Monitor.Monitor.garageID + "',3,'" + DateTime.Now.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss") + "','" + term.receiptNum + "',NULL);";
  574. }
  575. List<string> list = new List<string>();
  576. list.Add(parkingRecordsSql);
  577. Monitor.Monitor.localDBOper.Insert(list);
  578. Monitor.Monitor.remoteDBOper.Insert(list);
  579. Log.WriteLog(LogType.process, LogFile.INFO, "停车记录已插入," + license + "停在" + parkingSpaceID + ",凭证号:" + term.receiptNum);
  580. Monitor.Monitor.SetNotification("停车记录已插入",parkMonitor.model.TextColor.Info);
  581. ////注册用户记录插入云端
  582. //if (term.btnStatus == 0)
  583. //{
  584. //Monitor.Monitor.remoteDBOper.Insert(list);
  585. //}
  586. //查询记录ID号
  587. string findRecordSql = "select parkingRecordsID from parkingrecords where receiptNum = " + term.receiptNum + ";";
  588. int parkingRecordsID = 0;
  589. int remoteParkingRecordsID = 0;
  590. QueryParkingRecordsID(findRecordSql, out parkingRecordsID, false);
  591. QueryParkingRecordsID(findRecordSql, out remoteParkingRecordsID, true);
  592. //更新本地车辆表
  593. if (parkingRecordsID != 0)
  594. {
  595. UpdateVehicle(license, 1, parkingRecordsID, true, false, parkingSpaceID);
  596. }
  597. if (remoteParkingRecordsID != 0)
  598. {
  599. UpdateVehicle(license, 1, remoteParkingRecordsID, true, true, parkingSpaceID);
  600. }
  601. Log.WriteLog(LogType.process, LogFile.INFO, "停车记录表已更新");
  602. //更新本地车位
  603. UpdateAllParkingSpace();
  604. //Monitor.Monitor.AddNotification("停车数据库已更新,等待流程结束状态清零。");
  605. //while (Monitor.Monitor.mainBlockInfo.processCompleted != 0)
  606. //{
  607. // Thread.Sleep(1000);
  608. //}
  609. //中控清除车牌、凭证号、号牌验证等信息
  610. ClearTerminal(term.terminalID);
  611. try { idLicMap.Remove(currentTerm); } catch { }
  612. Monitor.Monitor.SetNotification(license + " 数据库已更新,停车流程结束",parkMonitor.model.TextColor.Info);
  613. }
  614. }
  615. }
  616. /// <summary>
  617. /// 预约车辆检测
  618. /// </summary>
  619. private static void ReservedCarCheck()
  620. {
  621. if (lockList.Count != terminalInfo.Count)
  622. {
  623. lockList.Clear();
  624. for (int i = 0; i < terminalInfo.Count; i++)
  625. {
  626. lockList.Add(false);
  627. }
  628. }
  629. //找到停车终端,且有地感无按钮数据
  630. for (int i = 0; i < terminalInfo.Count; i++)
  631. {
  632. //Console.WriteLine(i);
  633. if (terminalInfo[i].terminalStatus == 1 && terminalInfo[i].groundStatus == 1 && terminalInfo[i].btnStatus == 0 && !lockList[i])
  634. {
  635. lockList[i] = true;
  636. Action<int> reserveAction = new Action<int>(ReservedOper);
  637. reserveAction(i);
  638. }
  639. }
  640. }
  641. private static void ReservedOper(int index)
  642. {
  643. Task.Factory.StartNew(() =>
  644. {
  645. string license = "";
  646. int countdown = 2;
  647. short registered = 4;
  648. while (license == "" && countdown-- > 0)
  649. {
  650. license = Monitor.Monitor.numMachineLinker.GetLicensePlate(terminalInfo[index].terminalID);
  651. }
  652. Monitor.Monitor.SetNotification(terminalInfo[index].terminalID + "号车位收到地感,拍到" + license,parkMonitor.model.TextColor.Log);
  653. if (license != "")
  654. {
  655. //map中加入或更新号牌,供完成时写入数据库用
  656. if (idLicMap.ContainsKey(terminalInfo[index].terminalID))
  657. {
  658. idLicMap[terminalInfo[index].terminalID] = license;
  659. }
  660. else
  661. {
  662. idLicMap.Add(terminalInfo[index].terminalID, license);
  663. }
  664. if (Monitor.Monitor.webServer.ReservedCarCheck(license))
  665. {
  666. registered = 3;
  667. Monitor.Monitor.SetNotification("预约车辆入场",parkMonitor.model.TextColor.Info);
  668. }
  669. #region 通过数据库查询是否预约车,方法待定
  670. //int vehicleState = -1;
  671. //string vehicleStateCheckSql = "select vehiclepParkState from vehicle where numberPlate = '" + license + "';";
  672. //MySqlDataReader reader = Monitor.Monitor.localDBOper.Query(vehicleStateCheckSql);
  673. //if (reader != null)
  674. //{
  675. // try
  676. // {
  677. // if (reader.Read())
  678. // {
  679. // vehicleState = reader.GetInt32("vehiclepParkState");
  680. // }
  681. // }
  682. // catch (Exception e) { Log.WriteLog(LogType.process, LogFile.ERROR, "预约检测,查询车辆状态异常"); ; Console.WriteLine(e.Message); };
  683. // try
  684. // {
  685. // reader.Close();
  686. // reader.Dispose();
  687. // }
  688. // catch { }
  689. //}
  690. //已预约车辆入场
  691. //if (vehicleState == 4)
  692. //{
  693. // registered = 3;
  694. //}
  695. #endregion
  696. TerminalStru ts = new TerminalStru
  697. {
  698. terminalID = terminalInfo[index].terminalID,
  699. paymentStatus = (short)-1,
  700. btnStatus = registered,
  701. cmd = (short)-1,
  702. licenseCodeA = -1,
  703. licenseCodeB = -1,
  704. licenseCodeC = -1,
  705. licenseCodeD = -1,
  706. receiptNum = -1
  707. };
  708. Monitor.Monitor.PLC.WriteToPLC(ts, PLCDataType.terminal);
  709. Monitor.Monitor.SetNotification("指令写入PLC:"+ts.btnStatus,parkMonitor.model.TextColor.Log);
  710. Thread.Sleep(1000);
  711. }
  712. else
  713. {
  714. Thread.Sleep(200);
  715. }
  716. lockList[index] = false;
  717. });
  718. }
  719. /// <summary>
  720. /// 停车相关操作
  721. /// 0.观测到地感信号,且按钮状态不为3,则启动号牌机获取号牌,并判断是否预约车
  722. /// 1.读取到号牌机启动指令,启动号牌机并根据是否注册用户进行操作
  723. /// 非注册拿到号牌后记录并告知plc
  724. /// 注册用户,核对号牌与用户关联信息
  725. /// 2.停车流程结束,写数据库
  726. /// </summary>
  727. ///
  728. private static void ParkProcess()
  729. {
  730. Task.Factory.StartNew(() =>
  731. {
  732. while (!isClosing)
  733. {
  734. ParkNumSubProcess();
  735. Thread.Sleep(Monitor.Monitor.plcRefreshInterval * 5);
  736. }
  737. });
  738. Task.Factory.StartNew(() =>
  739. {
  740. while (!isClosing)
  741. {
  742. ParkCompleteSubProcess();
  743. Thread.Sleep(Monitor.Monitor.plcRefreshInterval * 5);
  744. }
  745. });
  746. Task.Factory.StartNew(() =>
  747. {
  748. while (!isClosing)
  749. {
  750. ReservedCarCheck();
  751. Thread.Sleep(Monitor.Monitor.plcRefreshInterval * 5);
  752. }
  753. });
  754. }
  755. /// <summary>
  756. /// 根据凭证号查询停车记录
  757. /// </summary>
  758. /// <param name="receipt"></param>
  759. /// <param name="recordID"></param>
  760. /// <param name="userID"></param>
  761. /// <param name="license"></param>
  762. /// <param name="realParkTime"></param>
  763. private static bool FindLocalParkingRecord(int receipt, out int recordID, out int userID, out string license, out string realParkTime)
  764. {
  765. recordID = 0;
  766. userID = 0;
  767. license = "";
  768. realParkTime = "";
  769. bool result = false;
  770. string parkRecordsSql = "select parkingRecordsID,userID,numberPlate,realParkTime from parkingrecords where receiptNum = " + receipt + ";";
  771. MySqlDataReader reader = Monitor.Monitor.localDBOper.Query(parkRecordsSql);
  772. try
  773. {
  774. if (reader != null && reader.Read())
  775. {
  776. object[] receiver = new object[4];
  777. reader.GetValues(receiver);
  778. recordID = (int)receiver[0];
  779. userID = (int)receiver[1];
  780. license = (string)receiver[2];
  781. realParkTime = (string)receiver[3];
  782. result = true;
  783. }
  784. else
  785. {
  786. Log.WriteLog(LogType.process, LogFile.ERROR, "根据凭证号查询停车记录失败");
  787. Monitor.Monitor.SetNotification("取车流程, 根据凭证号查询停车记录失败",parkMonitor.model.TextColor.Error);
  788. result = false;
  789. }
  790. //if (reader != null)
  791. //{
  792. // reader.Close();
  793. // reader.Dispose();
  794. //}
  795. }
  796. catch (Exception e) { Log.WriteLog(LogType.process, LogFile.ERROR, "根据凭证号查询停车记录失败"); Console.WriteLine(e.Message); result = false; }
  797. try
  798. {
  799. if (reader != null)
  800. {
  801. reader.Close();
  802. reader.Dispose();
  803. }
  804. }
  805. catch { }
  806. Log.WriteLog(LogType.process, LogFile.INFO, "根据凭证号查询停车记录成功");
  807. return result;
  808. }
  809. private static bool FindRemoteParkingRecord(int receipt, out int recordID)
  810. {
  811. recordID = 0;
  812. bool result = false;
  813. string remoteParkRecordsSql = "select parkingRecordsID from parkingrecords where receiptNum = " + receipt + ";";
  814. MySqlDataReader reader = Monitor.Monitor.remoteDBOper.Query(remoteParkRecordsSql);
  815. try
  816. {
  817. if (reader != null && reader.Read())
  818. {
  819. object[] receiver = new object[1];
  820. reader.GetValues(receiver);
  821. recordID = (int)receiver[0];
  822. result = true;
  823. }
  824. }
  825. catch (Exception e) { Console.WriteLine(e.Message); result = false; }
  826. try
  827. {
  828. if (reader != null)
  829. {
  830. reader.Close();
  831. reader.Dispose();
  832. }
  833. }
  834. catch (Exception e) { Console.WriteLine(e.Message); result = false; }
  835. return result;
  836. }
  837. /// <summary>
  838. /// 根据号牌获取本地与远端用户VIP月卡及预约信息
  839. /// </summary>
  840. /// <param name="license"></param>
  841. /// <param name="orderRecordsID"></param>
  842. /// <param name="monthCardType"></param>
  843. /// <param name="monthCardTimeLength"></param>
  844. /// <param name="orderTimeLength"></param>
  845. /// <returns></returns>
  846. private static bool FindVipInfo(string license, ref int orderRecordsID, ref int monthCardType, ref TimeSpan monthCardTimeLength, ref TimeSpan orderTimeLength)
  847. {
  848. bool result = true;
  849. string monthCardTime = "";
  850. //本地VIP信息
  851. string vipInfoSql = "select orderRecordsID,monthCardType,monthCardTime from vehicle where numberPlate = '" + license + "';";
  852. MySqlDataReader reader = null;
  853. try
  854. {
  855. reader = Monitor.Monitor.localDBOper.Query(vipInfoSql);
  856. if (reader != null && reader.Read())
  857. {
  858. object[] receiver = new object[3];
  859. reader.GetValues(receiver);
  860. Console.WriteLine(receiver[2].GetType());
  861. if (!receiver[0].GetType().Equals(typeof(DBNull)))
  862. {
  863. orderRecordsID = (int)receiver[0];
  864. }
  865. if (!receiver[1].GetType().Equals(typeof(DBNull)))
  866. {
  867. monthCardType = (int)(UInt32)receiver[1];
  868. }
  869. if (!receiver[2].GetType().Equals(typeof(DBNull)))
  870. {
  871. monthCardTime = (string)receiver[2];
  872. }
  873. //orderRecordsID = (Int32)receiver[0];
  874. //monthCardType = (Int32)receiver[1];
  875. //monthCardTime = (string)receiver[2];
  876. monthCardTimeLength = DateTime.Parse(monthCardTime) - DateTime.Now;
  877. if (monthCardTimeLength.TotalHours <= 0)
  878. {
  879. monthCardType = 0;
  880. }
  881. Log.WriteLog(LogType.process, LogFile.INFO, "已获取本地预约与月卡信息");
  882. }
  883. }
  884. catch (Exception e) { Console.WriteLine(e.Message); result = false; }
  885. try
  886. {
  887. if (reader != null)
  888. {
  889. reader.Close();
  890. reader.Dispose();
  891. }
  892. }
  893. catch (Exception e) { Console.WriteLine(e.Message); result = false; }
  894. //云端VIP信息
  895. try
  896. {
  897. reader = Monitor.Monitor.remoteDBOper.Query(vipInfoSql);
  898. if (reader != null && reader.Read())
  899. {
  900. object[] receiver = new object[3];
  901. reader.GetValues(receiver);
  902. int cardType = (int)(UInt32)receiver[1];
  903. string CardTime = (string)receiver[2];
  904. TimeSpan t = DateTime.Parse(CardTime) - DateTime.Now;
  905. //云端
  906. if (t.TotalHours > 0 && cardType > monthCardType)
  907. {
  908. monthCardType = cardType;
  909. monthCardTimeLength = t;
  910. }
  911. Log.WriteLog(LogType.process, LogFile.INFO, "已获取云端预约与月卡信息");
  912. }
  913. }
  914. catch (Exception e) { Console.WriteLine(e.Message); result = false; }
  915. try
  916. {
  917. if (reader != null)
  918. {
  919. reader.Close();
  920. reader.Dispose();
  921. }
  922. }
  923. catch (Exception e) { Console.WriteLine(e.Message); result = false; }
  924. //有预约记录,判断记录有效性,有效则获取预约时长
  925. if (orderRecordsID != 0)
  926. {
  927. //修改orderTimeLength
  928. }
  929. return result;
  930. }
  931. /// <summary>
  932. /// 预约与停车费用计算
  933. /// </summary>
  934. /// <param name="scheme"></param>
  935. /// <param name="parkTime"></param>
  936. /// <param name="orderTimeLength"></param>
  937. /// <returns></returns>
  938. private static int FeeCalc(PaymentScheme scheme, DateTime parkTime, TimeSpan orderTimeLength)
  939. {
  940. try
  941. {
  942. int orderFee = scheme.bookCharge * (int)orderTimeLength.TotalHours;
  943. int parkFee = 0;
  944. int hours = (int)(DateTime.Now - parkTime).TotalHours;
  945. switch (scheme.schemeType)
  946. {
  947. //按时计费
  948. case 1:
  949. if (hours > scheme.firstChargeTime + scheme.freeTime)
  950. {
  951. if (hours > 24)
  952. {
  953. parkFee = hours/24 * scheme.upperBound + Math.Min(scheme.intervalCharge * (hours%24) / scheme.chargeInterval, scheme.upperBound);
  954. }
  955. else
  956. {
  957. parkFee = Math.Min(scheme.firstCharge + scheme.intervalCharge * hours / scheme.chargeInterval, scheme.upperBound);
  958. }
  959. }
  960. else if (hours > scheme.freeTime)
  961. {
  962. parkFee = scheme.firstCharge;
  963. }
  964. else
  965. {
  966. parkFee = 0;
  967. }
  968. break;
  969. //按次计费
  970. case 2:
  971. //Console.WriteLine(DateTime.Now.Date.ToString()+","+ parkTime.Date.ToString());
  972. int overnightCount = (DateTime.Now.Date - parkTime.Date).Days;
  973. parkFee = scheme.eachCharge + overnightCount * scheme.overnightCharge;
  974. break;
  975. //按时间段计费
  976. case 3:
  977. TimeSpan currentTimeOfDay = DateTime.Now.TimeOfDay;
  978. TimeSpan parkTimeOfDay = parkTime.TimeOfDay;
  979. TimeSpan startTimeOfDay = DateTime.Parse(scheme.startChargeTime).TimeOfDay;
  980. TimeSpan endTimeOfDay = DateTime.Parse(scheme.endChargeTime).TimeOfDay;
  981. double countingHoursOfDay = endTimeOfDay.TotalHours - startTimeOfDay.TotalHours;
  982. int days = (int)((DateTime.Now - parkTime).TotalHours - currentTimeOfDay.TotalHours + parkTimeOfDay.TotalHours);
  983. int previousOffset = (int)(-(Math.Max(0, parkTimeOfDay.TotalHours - startTimeOfDay.TotalHours)));
  984. int currentOffset = (int)Math.Min((currentTimeOfDay.TotalHours - startTimeOfDay.TotalHours), countingHoursOfDay);
  985. int peakTimeLength = days * (int)countingHoursOfDay + previousOffset + currentOffset;
  986. parkFee = peakTimeLength * scheme.chargeStandard + ((int)((DateTime.Now - parkTime).TotalHours) - peakTimeLength) * scheme.intervalCharge;
  987. break;
  988. }
  989. Log.WriteLog(LogType.process, LogFile.INFO, "停车费用" + parkFee);
  990. return parkFee;
  991. }
  992. catch (Exception e) { Console.WriteLine("费用计算异常"); return -1; }
  993. }
  994. /// <summary>
  995. /// 取车计费子操作
  996. /// </summary>
  997. private static void FetchFeeCalcSubProcess()
  998. {
  999. int receiptNum = 0;
  1000. int termIndex = -1;
  1001. bool failed = false;
  1002. TerminalStru ts = new TerminalStru();
  1003. for (int i = 0; i < terminalInfo.Count; i++)
  1004. {
  1005. ts = terminalInfo[i];
  1006. termIndex = ts.terminalID;
  1007. receiptNum = ts.receiptNum;
  1008. //取车状态、凭证号不为空,查询数据库计费
  1009. //Console.WriteLine((termIndex != -1) + "," + (termUsedMap.Count >= termIndex) + "," + termUsedMap.TryGetValue(termIndex, out bool valuee) + "," + (!valuee) + "," + (fetchState == 1) + "," + (receiptNum != 0));
  1010. if (termIndex != -1 && termUsedMap.Count >= termIndex && termUsedMap.TryGetValue(termIndex, out bool value) && !value && ts.cmd == 2 && receiptNum != 0)
  1011. {
  1012. //fetchState = Monitor.Monitor.mainBlockInfo.fetchingRunning;
  1013. Monitor.Monitor.SetNotification(termIndex + "号终端准备计算费用",parkMonitor.model.TextColor.Log);
  1014. termUsedMap[termIndex] = true;
  1015. Console.WriteLine("终端编号" + termIndex);
  1016. int parkingRecordsID = 0;
  1017. int remoteParkingRecordsID = 0;
  1018. int userID = 0;
  1019. string license = "";
  1020. string realParkTime = "";
  1021. //1.根据凭证号查询停车记录
  1022. if (!FindLocalParkingRecord(receiptNum, out parkingRecordsID, out userID, out license, out realParkTime))
  1023. {
  1024. failed = true;
  1025. //return;
  1026. }
  1027. else
  1028. {
  1029. FindRemoteParkingRecord(receiptNum, out remoteParkingRecordsID);
  1030. }
  1031. //本地存在号牌,后续操作
  1032. //2.根据号牌查询车辆月卡信息与预约记录
  1033. int orderRecordsID = 0;
  1034. int monthCardType = -1;
  1035. string monthCardTime = "";
  1036. TimeSpan monthCardTimeLength = new TimeSpan(0);
  1037. TimeSpan orderTimeLength = new TimeSpan(0);
  1038. if (license != "")
  1039. {
  1040. FindVipInfo(license, ref orderRecordsID, ref monthCardType, ref monthCardTimeLength, ref orderTimeLength);
  1041. }
  1042. if (monthCardType == -1) monthCardType = 0;
  1043. //3.获取停车时刻,根据用户类型计费发送给PLC,暂认为无预约
  1044. DateTime parkTime = DateTime.Parse(realParkTime);
  1045. DateTime currentTime = DateTime.Now;
  1046. int fee = 32767;
  1047. if (failed) { monthCardType = -2; }
  1048. if (PaymentScheme.ins != null)
  1049. {
  1050. if (monthCardTimeLength.TotalHours > 0)
  1051. {
  1052. fee = 0;
  1053. }
  1054. else if (failed)
  1055. {
  1056. fee = 0;
  1057. }
  1058. else
  1059. {
  1060. fee = FeeCalc(PaymentScheme.ins, parkTime, orderTimeLength);
  1061. }
  1062. try
  1063. {
  1064. if (Monitor.Monitor.PLC != null)
  1065. {
  1066. TerminalStru FeeMsg = new TerminalStru();
  1067. FeeMsg.terminalID = (short)termIndex;
  1068. FeeMsg.paymentStatus = (short)-1;
  1069. FeeMsg.licVerification = (short)-1;
  1070. FeeMsg.parkingFee = (short)fee;
  1071. FeeMsg.userType = (short)(monthCardType + 1);
  1072. Console.WriteLine(FeeMsg);
  1073. Monitor.Monitor.PLC.WriteToPLC(FeeMsg, PLCDataType.central);
  1074. Log.WriteLog(LogType.process, LogFile.INFO, "停车费用已发送至PLC");
  1075. }
  1076. }
  1077. catch { Console.WriteLine("error"); }
  1078. }
  1079. Monitor.Monitor.SetNotification("用户类型:" + (monthCardType+1) + ",费用:" + fee + ",等待凭证号被清除且流程结束后更新车辆状态",parkMonitor.model.TextColor.Log);
  1080. FetchInfo fetchInfo = new FetchInfo(license, parkingRecordsID, remoteParkingRecordsID, fee, receiptNum);
  1081. if (!fetchMap.ContainsKey(termIndex))
  1082. fetchMap.Add(termIndex, fetchInfo);
  1083. else
  1084. fetchMap[termIndex] = fetchInfo;
  1085. }
  1086. }
  1087. }
  1088. private static void FetchCompleteProcess()
  1089. {
  1090. int fetchState = Monitor.Monitor.mainBlockInfo.fetchingRunning;
  1091. int termIndex = -1;
  1092. TerminalStru ts = new TerminalStru();
  1093. if (fetchState == 1)
  1094. {
  1095. //找到当前取车终端
  1096. for (int i = 0; i < terminalInfo.Count; i++)
  1097. {
  1098. if (terminalInfo[i].terminalID == Monitor.Monitor.mainBlockInfo.terminalID)
  1099. {
  1100. ts = terminalInfo[i];
  1101. termIndex = ts.terminalID;
  1102. break;
  1103. }
  1104. }
  1105. //4.等待终端获得收费完成信号且将凭证号清除后,清除收费完成信号并将信息写入数据库
  1106. int count = 0;
  1107. while (!isClosing && termIndex != -1)
  1108. {
  1109. if (Monitor.Monitor.mainBlockInfo.processCompleted == 1)
  1110. {
  1111. while (terminalInfo[termIndex - 1].receiptNum != 0)
  1112. {
  1113. count++;
  1114. if (count == 1)
  1115. Monitor.Monitor.SetNotification("等待凭证号清除",parkMonitor.model.TextColor.Log);
  1116. if (count > 10000)//避免int型数据溢出
  1117. count = 2;
  1118. Thread.Sleep(200);
  1119. }
  1120. int paymentStatus = 0;
  1121. for (int i = 0; i < terminalInfo.Count; i++)
  1122. {
  1123. if (terminalInfo[i].terminalID == Monitor.Monitor.mainBlockInfo.terminalID)
  1124. {
  1125. paymentStatus = terminalInfo[i].paymentStatus;
  1126. break;
  1127. }
  1128. }
  1129. Log.WriteLog(LogType.process, LogFile.INFO, "检测到凭证号已被清除");
  1130. FetchInfo fi = null;
  1131. if (fetchMap.ContainsKey(termIndex))
  1132. {
  1133. fi = fetchMap[termIndex];
  1134. }
  1135. else
  1136. {
  1137. Monitor.Monitor.SetNotification("未查询到该取车终端存在计费操作",parkMonitor.model.TextColor.Error);
  1138. return;
  1139. }
  1140. //TerminalStru fetchReset = new TerminalStru
  1141. //{
  1142. // terminalID = (short)termIndex,
  1143. // paymentStatus = (short)0,
  1144. // licVerification = (short)0,
  1145. // parkingFee = (short)32767,
  1146. // userType = (short)0
  1147. //};
  1148. //Monitor.Monitor.PLC.WriteToPLC(fetchReset, PLCDataType.central);
  1149. ClearTerminal(termIndex);
  1150. //更新停车记录与车辆状态
  1151. string updateParkingRecordsSql = "update parkingrecords set parkingRecordsState = 6,realGetTime = '" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") +
  1152. "',parkingPrice = " + fi.fee + ",paymentStatus = " + paymentStatus + " where receiptNum = '" + fi.receiptNum + "';";
  1153. List<string> list = new List<string>();
  1154. list.Add(updateParkingRecordsSql);
  1155. lock (Monitor.Monitor.localDBOper)
  1156. {
  1157. Monitor.Monitor.localDBOper.UpdateTransaction(list);
  1158. UpdateVehicle(fi.license, 0, fi.parkingRecordsID, false, false, 0);
  1159. }
  1160. lock (Monitor.Monitor.remoteDBOper)
  1161. {
  1162. Monitor.Monitor.remoteDBOper.UpdateTransaction(list);
  1163. UpdateVehicle(fi.license, 0, fi.remoteParkingRecordsID, false, true, 0);
  1164. }
  1165. Monitor.Monitor.SetNotification(fi.license + " 取车流程结束",parkMonitor.model.TextColor.Info);
  1166. break;
  1167. }
  1168. Thread.Sleep(300);
  1169. }
  1170. termUsedMap[termIndex] = false;
  1171. fetchMap.Remove(termIndex);
  1172. }
  1173. }
  1174. /// <summary>
  1175. /// 取车相关操作
  1176. ///
  1177. /// </summary>
  1178. private static void FetchProcess()
  1179. {
  1180. Task.Factory.StartNew(() =>
  1181. {
  1182. while (!isClosing)
  1183. {
  1184. FetchFeeCalcSubProcess();
  1185. Thread.Sleep(Monitor.Monitor.plcRefreshInterval);
  1186. }
  1187. });
  1188. Task.Factory.StartNew(() =>
  1189. {
  1190. while (!isClosing)
  1191. {
  1192. FetchCompleteProcess();
  1193. Thread.Sleep(Monitor.Monitor.plcRefreshInterval);
  1194. }
  1195. });
  1196. }
  1197. public static void Start()
  1198. {
  1199. for (int i = 0; i < Monitor.Monitor.plcTerminalCount; i++)
  1200. {
  1201. lockList.Add(false);
  1202. }
  1203. ParkProcess();
  1204. FetchProcess();
  1205. }
  1206. public static void Stop()
  1207. {
  1208. isClosing = true;
  1209. }
  1210. public class FetchInfo
  1211. {
  1212. public string license;
  1213. public int parkingRecordsID;
  1214. public int remoteParkingRecordsID;
  1215. public int fee;
  1216. public int receiptNum;
  1217. public FetchInfo(string license, int parkingRecordsID, int remoteParkingRecordsID, int fee, int receiptNum)
  1218. {
  1219. this.license = license;
  1220. this.parkingRecordsID = parkingRecordsID;
  1221. this.remoteParkingRecordsID = remoteParkingRecordsID;
  1222. this.fee = fee;
  1223. this.receiptNum = receiptNum;
  1224. }
  1225. }
  1226. }
  1227. }