Terminal.cs 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259
  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("注册用户,号牌验证成功");
  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("注册用户,号牌验证失败");
  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);
  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)
  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)
  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("已写入号牌获取结果,等待号牌机启动指令清零");
  499. }
  500. int countdown = 5;
  501. while (countdown-- > 0)
  502. {
  503. if (term.numMachineLaunch == 0)
  504. {
  505. Monitor.Monitor.SetNotification("号牌机启动指令已被清零"); 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. Thread.Sleep(200);
  535. if (Monitor.Monitor.mainBlockInfo.processStopped == 1)
  536. return;
  537. Thread.Sleep(200);
  538. }
  539. //找到终端号,判断是否注册用户
  540. //拿到号牌
  541. if (/*terminalInfo[i].terminalID == currentTerm && */idLicMap.TryGetValue(currentTerm, out license) && license != "")
  542. {
  543. term = terminalInfo[currentTerm - 1];
  544. Thread.Sleep(1500);
  545. //找到停车位置
  546. for (int s = 0; s < Monitor.Monitor.parkingSpaceInfo.Count; s++)
  547. {
  548. Console.WriteLine(Monitor.Monitor.parkingSpaceInfo[s].receiptNum + "," + term.receiptNum);
  549. if (Monitor.Monitor.parkingSpaceInfo[s].receiptNum == term.receiptNum)
  550. {
  551. parkingSpaceID = Monitor.Monitor.parkingSpaceInfo[s].parkingSpace;
  552. break;
  553. }
  554. }
  555. //无车位信息则跳出当前循环
  556. if (parkingSpaceID == 0 || term.receiptNum == 0)
  557. {
  558. try { idLicMap.Remove(currentTerm); } catch { }
  559. Monitor.Monitor.SetNotification("无车位或凭证号");
  560. return;
  561. }
  562. UpdateVehicle(license, 0, 0, true, false, 0);
  563. UpdateVehicle(license, 0, 0, true, true, 0);
  564. //插入停车记录
  565. string parkingRecordsSql = "";
  566. if (term.licenseCodeA != 0)
  567. {
  568. parkingRecordsSql = "INSERT INTO parkingrecords (userID, numberPlate,parkingSpaceID,garageID,parkingRecordsState,realParkTime,receiptNum,parkingPrice)" +
  569. "values ('" + term.licenseCodeA + "','" + license + "','" + parkingSpaceID + "','" + Monitor.Monitor.garageID + "',3,'" + DateTime.Now.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss") + "','" + term.receiptNum + "',NULL);";
  570. }
  571. else
  572. {
  573. parkingRecordsSql = "INSERT INTO parkingrecords (userID, numberPlate,parkingSpaceID,garageID,parkingRecordsState,realParkTime,receiptNum,parkingPrice)" +
  574. "values ('" + 1 + "','" + license + "','" + parkingSpaceID + "','" + Monitor.Monitor.garageID + "',3,'" + DateTime.Now.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss") + "','" + term.receiptNum + "',NULL);";
  575. }
  576. List<string> list = new List<string>();
  577. list.Add(parkingRecordsSql);
  578. Monitor.Monitor.localDBOper.Insert(list);
  579. Monitor.Monitor.remoteDBOper.Insert(list);
  580. Log.WriteLog(LogType.process, LogFile.INFO, "停车记录已插入," + license + "停在" + parkingSpaceID + ",凭证号:" + term.receiptNum);
  581. Monitor.Monitor.SetNotification("停车记录已插入");
  582. ////注册用户记录插入云端
  583. //if (term.btnStatus == 0)
  584. //{
  585. //Monitor.Monitor.remoteDBOper.Insert(list);
  586. //}
  587. //查询记录ID号
  588. string findRecordSql = "select parkingRecordsID from parkingrecords where receiptNum = " + term.receiptNum + ";";
  589. int parkingRecordsID = 0;
  590. int remoteParkingRecordsID = 0;
  591. QueryParkingRecordsID(findRecordSql, out parkingRecordsID, false);
  592. QueryParkingRecordsID(findRecordSql, out remoteParkingRecordsID, true);
  593. //更新本地车辆表
  594. if (parkingRecordsID != 0)
  595. {
  596. UpdateVehicle(license, 1, parkingRecordsID, true, false, parkingSpaceID);
  597. }
  598. if (remoteParkingRecordsID != 0)
  599. {
  600. UpdateVehicle(license, 1, remoteParkingRecordsID, true, true, parkingSpaceID);
  601. }
  602. Log.WriteLog(LogType.process, LogFile.INFO, "停车记录表已更新");
  603. //更新本地车位
  604. UpdateAllParkingSpace();
  605. //Monitor.Monitor.AddNotification("停车数据库已更新,等待流程结束状态清零。");
  606. //while (Monitor.Monitor.mainBlockInfo.processCompleted != 0)
  607. //{
  608. // Thread.Sleep(1000);
  609. //}
  610. //中控清除车牌、凭证号、号牌验证等信息
  611. ClearTerminal(term.terminalID);
  612. try { idLicMap.Remove(currentTerm); } catch { }
  613. Monitor.Monitor.SetNotification(license + " 数据库已更新,停车流程结束");
  614. }
  615. }
  616. }
  617. /// <summary>
  618. /// 预约车辆检测
  619. /// </summary>
  620. private static void ReservedCarCheck()
  621. {
  622. if (lockList.Count != terminalInfo.Count)
  623. {
  624. lockList.Clear();
  625. for (int i = 0; i < terminalInfo.Count; i++)
  626. {
  627. lockList.Add(false);
  628. }
  629. }
  630. //找到停车终端,且有地感无按钮数据
  631. for (int i = 0; i < terminalInfo.Count; i++)
  632. {
  633. //Console.WriteLine(i);
  634. if (terminalInfo[i].terminalStatus == 1 && terminalInfo[i].groundStatus == 1 && terminalInfo[i].btnStatus == 0 && !lockList[i])
  635. {
  636. lockList[i] = true;
  637. Action<int> reserveAction = new Action<int>(ReservedOper);
  638. reserveAction(i);
  639. }
  640. }
  641. }
  642. private static void ReservedOper(int index)
  643. {
  644. Task.Factory.StartNew(() =>
  645. {
  646. string license = "";
  647. int countdown = 2;
  648. short registered = 4;
  649. while (license == "" && countdown-- > 0)
  650. {
  651. license = Monitor.Monitor.numMachineLinker.GetLicensePlate(terminalInfo[index].terminalID);
  652. }
  653. Monitor.Monitor.SetNotification(terminalInfo[index].terminalID + "号车位收到地感,拍到" + license);
  654. if (license != "")
  655. {
  656. //map中加入或更新号牌,供完成时写入数据库用
  657. if (idLicMap.ContainsKey(terminalInfo[index].terminalID))
  658. {
  659. idLicMap[terminalInfo[index].terminalID] = license;
  660. }
  661. else
  662. {
  663. idLicMap.Add(terminalInfo[index].terminalID, license);
  664. }
  665. if (Monitor.Monitor.webServer.ReservedCarCheck(license))
  666. {
  667. registered = 3;
  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. Thread.Sleep(1000);
  710. }
  711. else
  712. {
  713. Thread.Sleep(200);
  714. }
  715. lockList[index] = false;
  716. });
  717. }
  718. /// <summary>
  719. /// 停车相关操作
  720. /// 0.观测到地感信号,且按钮状态不为3,则启动号牌机获取号牌,并判断是否预约车
  721. /// 1.读取到号牌机启动指令,启动号牌机并根据是否注册用户进行操作
  722. /// 非注册拿到号牌后记录并告知plc
  723. /// 注册用户,核对号牌与用户关联信息
  724. /// 2.停车流程结束,写数据库
  725. /// </summary>
  726. ///
  727. private static void ParkProcess()
  728. {
  729. Task.Factory.StartNew(() =>
  730. {
  731. while (!isClosing)
  732. {
  733. ParkNumSubProcess();
  734. Thread.Sleep(Monitor.Monitor.plcRefreshInterval * 5);
  735. }
  736. });
  737. Task.Factory.StartNew(() =>
  738. {
  739. while (!isClosing)
  740. {
  741. ParkCompleteSubProcess();
  742. Thread.Sleep(Monitor.Monitor.plcRefreshInterval * 5);
  743. }
  744. });
  745. Task.Factory.StartNew(() =>
  746. {
  747. while (!isClosing)
  748. {
  749. ReservedCarCheck();
  750. Thread.Sleep(Monitor.Monitor.plcRefreshInterval * 5);
  751. }
  752. });
  753. }
  754. /// <summary>
  755. /// 根据凭证号查询停车记录
  756. /// </summary>
  757. /// <param name="receipt"></param>
  758. /// <param name="recordID"></param>
  759. /// <param name="userID"></param>
  760. /// <param name="license"></param>
  761. /// <param name="realParkTime"></param>
  762. private static bool FindLocalParkingRecord(int receipt, out int recordID, out int userID, out string license, out string realParkTime)
  763. {
  764. recordID = 0;
  765. userID = 0;
  766. license = "";
  767. realParkTime = "";
  768. bool result = false;
  769. string parkRecordsSql = "select parkingRecordsID,userID,numberPlate,realParkTime from parkingrecords where receiptNum = " + receipt + ";";
  770. MySqlDataReader reader = Monitor.Monitor.localDBOper.Query(parkRecordsSql);
  771. try
  772. {
  773. if (reader != null && reader.Read())
  774. {
  775. object[] receiver = new object[4];
  776. reader.GetValues(receiver);
  777. recordID = (int)receiver[0];
  778. userID = (int)receiver[1];
  779. license = (string)receiver[2];
  780. realParkTime = (string)receiver[3];
  781. result = true;
  782. }
  783. else
  784. {
  785. Log.WriteLog(LogType.process, LogFile.ERROR, "根据凭证号查询停车记录失败");
  786. Monitor.Monitor.SetNotification("取车流程, 根据凭证号查询停车记录失败");
  787. result = false;
  788. }
  789. //if (reader != null)
  790. //{
  791. // reader.Close();
  792. // reader.Dispose();
  793. //}
  794. }
  795. catch (Exception e) { Log.WriteLog(LogType.process, LogFile.ERROR, "根据凭证号查询停车记录失败"); Console.WriteLine(e.Message); result = false; }
  796. try
  797. {
  798. if (reader != null)
  799. {
  800. reader.Close();
  801. reader.Dispose();
  802. }
  803. }
  804. catch { }
  805. Log.WriteLog(LogType.process, LogFile.INFO, "根据凭证号查询停车记录成功");
  806. return result;
  807. }
  808. private static bool FindRemoteParkingRecord(int receipt, out int recordID)
  809. {
  810. recordID = 0;
  811. bool result = false;
  812. string remoteParkRecordsSql = "select parkingRecordsID from parkingrecords where receiptNum = " + receipt + ";";
  813. MySqlDataReader reader = Monitor.Monitor.remoteDBOper.Query(remoteParkRecordsSql);
  814. try
  815. {
  816. if (reader != null && reader.Read())
  817. {
  818. object[] receiver = new object[1];
  819. reader.GetValues(receiver);
  820. recordID = (int)receiver[0];
  821. result = true;
  822. }
  823. }
  824. catch (Exception e) { Console.WriteLine(e.Message); result = false; }
  825. try
  826. {
  827. if (reader != null)
  828. {
  829. reader.Close();
  830. reader.Dispose();
  831. }
  832. }
  833. catch (Exception e) { Console.WriteLine(e.Message); result = false; }
  834. return result;
  835. }
  836. /// <summary>
  837. /// 根据号牌获取本地与远端用户VIP月卡及预约信息
  838. /// </summary>
  839. /// <param name="license"></param>
  840. /// <param name="orderRecordsID"></param>
  841. /// <param name="monthCardType"></param>
  842. /// <param name="monthCardTimeLength"></param>
  843. /// <param name="orderTimeLength"></param>
  844. /// <returns></returns>
  845. private static bool FindVipInfo(string license, ref int orderRecordsID, ref int monthCardType, ref TimeSpan monthCardTimeLength, ref TimeSpan orderTimeLength)
  846. {
  847. bool result = true;
  848. string monthCardTime = "";
  849. //本地VIP信息
  850. string vipInfoSql = "select orderRecordsID,monthCardType,monthCardTime from vehicle where numberPlate = '" + license + "';";
  851. MySqlDataReader reader = null;
  852. try
  853. {
  854. reader = Monitor.Monitor.localDBOper.Query(vipInfoSql);
  855. if (reader != null && reader.Read())
  856. {
  857. object[] receiver = new object[3];
  858. reader.GetValues(receiver);
  859. Console.WriteLine(receiver[2].GetType());
  860. if (!receiver[0].GetType().Equals(typeof(DBNull)))
  861. {
  862. orderRecordsID = (int)receiver[0];
  863. }
  864. if (!receiver[1].GetType().Equals(typeof(DBNull)))
  865. {
  866. monthCardType = (int)(UInt32)receiver[1];
  867. }
  868. if (!receiver[2].GetType().Equals(typeof(DBNull)))
  869. {
  870. monthCardTime = (string)receiver[2];
  871. }
  872. //orderRecordsID = (Int32)receiver[0];
  873. //monthCardType = (Int32)receiver[1];
  874. //monthCardTime = (string)receiver[2];
  875. monthCardTimeLength = DateTime.Parse(monthCardTime) - DateTime.Now;
  876. if (monthCardTimeLength.TotalHours <= 0)
  877. {
  878. monthCardType = 0;
  879. }
  880. Log.WriteLog(LogType.process, LogFile.INFO, "已获取本地预约与月卡信息");
  881. }
  882. }
  883. catch (Exception e) { Console.WriteLine(e.Message); result = false; }
  884. try
  885. {
  886. if (reader != null)
  887. {
  888. reader.Close();
  889. reader.Dispose();
  890. }
  891. }
  892. catch (Exception e) { Console.WriteLine(e.Message); result = false; }
  893. //云端VIP信息
  894. try
  895. {
  896. reader = Monitor.Monitor.remoteDBOper.Query(vipInfoSql);
  897. if (reader != null && reader.Read())
  898. {
  899. object[] receiver = new object[3];
  900. reader.GetValues(receiver);
  901. int cardType = (int)(UInt32)receiver[1];
  902. string CardTime = (string)receiver[2];
  903. TimeSpan t = DateTime.Parse(CardTime) - DateTime.Now;
  904. //云端
  905. if (t.TotalHours > 0 && cardType > monthCardType)
  906. {
  907. monthCardType = cardType;
  908. monthCardTimeLength = t;
  909. }
  910. Log.WriteLog(LogType.process, LogFile.INFO, "已获取云端预约与月卡信息");
  911. }
  912. }
  913. catch (Exception e) { Console.WriteLine(e.Message); result = false; }
  914. try
  915. {
  916. if (reader != null)
  917. {
  918. reader.Close();
  919. reader.Dispose();
  920. }
  921. }
  922. catch (Exception e) { Console.WriteLine(e.Message); result = false; }
  923. //有预约记录,判断记录有效性,有效则获取预约时长
  924. if (orderRecordsID != 0)
  925. {
  926. //修改orderTimeLength
  927. }
  928. return result;
  929. }
  930. /// <summary>
  931. /// 预约与停车费用计算
  932. /// </summary>
  933. /// <param name="scheme"></param>
  934. /// <param name="parkTime"></param>
  935. /// <param name="orderTimeLength"></param>
  936. /// <returns></returns>
  937. private static int FeeCalc(PaymentScheme scheme, DateTime parkTime, TimeSpan orderTimeLength)
  938. {
  939. try
  940. {
  941. int orderFee = scheme.bookCharge * (int)orderTimeLength.TotalHours;
  942. int parkFee = 0;
  943. int hours = (int)(DateTime.Now - parkTime).TotalHours;
  944. switch (scheme.schemeType)
  945. {
  946. //按时计费
  947. case 1:
  948. if (hours > scheme.firstChargeTime + scheme.freeTime)
  949. {
  950. if (hours > 24)
  951. {
  952. parkFee = hours/24 * scheme.upperBound + Math.Min(scheme.intervalCharge * (hours%24) / scheme.chargeInterval, scheme.upperBound);
  953. }
  954. else
  955. {
  956. parkFee = Math.Min(scheme.firstCharge + scheme.intervalCharge * hours / scheme.chargeInterval, scheme.upperBound);
  957. }
  958. }
  959. else if (hours > scheme.freeTime)
  960. {
  961. parkFee = scheme.firstCharge;
  962. }
  963. else
  964. {
  965. parkFee = 0;
  966. }
  967. break;
  968. //按次计费
  969. case 2:
  970. //Console.WriteLine(DateTime.Now.Date.ToString()+","+ parkTime.Date.ToString());
  971. int overnightCount = (DateTime.Now.Date - parkTime.Date).Days;
  972. parkFee = scheme.eachCharge + overnightCount * scheme.overnightCharge;
  973. break;
  974. //按时间段计费
  975. case 3:
  976. TimeSpan currentTimeOfDay = DateTime.Now.TimeOfDay;
  977. TimeSpan parkTimeOfDay = parkTime.TimeOfDay;
  978. TimeSpan startTimeOfDay = DateTime.Parse(scheme.startChargeTime).TimeOfDay;
  979. TimeSpan endTimeOfDay = DateTime.Parse(scheme.endChargeTime).TimeOfDay;
  980. double countingHoursOfDay = endTimeOfDay.TotalHours - startTimeOfDay.TotalHours;
  981. int days = (int)((DateTime.Now - parkTime).TotalHours - currentTimeOfDay.TotalHours + parkTimeOfDay.TotalHours);
  982. int previousOffset = (int)(-(Math.Max(0, parkTimeOfDay.TotalHours - startTimeOfDay.TotalHours)));
  983. int currentOffset = (int)Math.Min((currentTimeOfDay.TotalHours - startTimeOfDay.TotalHours), countingHoursOfDay);
  984. int peakTimeLength = days * (int)countingHoursOfDay + previousOffset + currentOffset;
  985. parkFee = peakTimeLength * scheme.chargeStandard + ((int)((DateTime.Now - parkTime).TotalHours) - peakTimeLength) * scheme.intervalCharge;
  986. break;
  987. }
  988. Log.WriteLog(LogType.process, LogFile.INFO, "停车费用" + parkFee);
  989. return parkFee;
  990. }
  991. catch (Exception e) { Console.WriteLine("费用计算异常"); return -1; }
  992. }
  993. /// <summary>
  994. /// 取车计费子操作
  995. /// </summary>
  996. private static void FetchFeeCalcSubProcess()
  997. {
  998. int receiptNum = 0;
  999. int termIndex = -1;
  1000. bool failed = false;
  1001. TerminalStru ts = new TerminalStru();
  1002. for (int i = 0; i < terminalInfo.Count; i++)
  1003. {
  1004. ts = terminalInfo[i];
  1005. termIndex = ts.terminalID;
  1006. receiptNum = ts.receiptNum;
  1007. //取车状态、凭证号不为空,查询数据库计费
  1008. //Console.WriteLine((termIndex != -1) + "," + (termUsedMap.Count >= termIndex) + "," + termUsedMap.TryGetValue(termIndex, out bool valuee) + "," + (!valuee) + "," + (fetchState == 1) + "," + (receiptNum != 0));
  1009. if (termIndex != -1 && termUsedMap.Count >= termIndex && termUsedMap.TryGetValue(termIndex, out bool value) && !value && ts.cmd == 2 && receiptNum != 0)
  1010. {
  1011. //fetchState = Monitor.Monitor.mainBlockInfo.fetchingRunning;
  1012. Monitor.Monitor.SetNotification(termIndex + "号终端准备计算费用");
  1013. termUsedMap[termIndex] = true;
  1014. Console.WriteLine("终端编号" + termIndex);
  1015. int parkingRecordsID = 0;
  1016. int remoteParkingRecordsID = 0;
  1017. int userID = 0;
  1018. string license = "";
  1019. string realParkTime = "";
  1020. //1.根据凭证号查询停车记录
  1021. if (!FindLocalParkingRecord(receiptNum, out parkingRecordsID, out userID, out license, out realParkTime))
  1022. {
  1023. failed = true;
  1024. //return;
  1025. }
  1026. else
  1027. {
  1028. FindRemoteParkingRecord(receiptNum, out remoteParkingRecordsID);
  1029. }
  1030. //本地存在号牌,后续操作
  1031. //2.根据号牌查询车辆月卡信息与预约记录
  1032. int orderRecordsID = 0;
  1033. int monthCardType = -1;
  1034. string monthCardTime = "";
  1035. TimeSpan monthCardTimeLength = new TimeSpan(0);
  1036. TimeSpan orderTimeLength = new TimeSpan(0);
  1037. if (license != "")
  1038. {
  1039. FindVipInfo(license, ref orderRecordsID, ref monthCardType, ref monthCardTimeLength, ref orderTimeLength);
  1040. }
  1041. if (monthCardType == -1) monthCardType = 0;
  1042. //3.获取停车时刻,根据用户类型计费发送给PLC,暂认为无预约
  1043. DateTime parkTime = DateTime.Parse(realParkTime);
  1044. DateTime currentTime = DateTime.Now;
  1045. int fee = 32767;
  1046. if (failed) { monthCardType = -2; }
  1047. if (PaymentScheme.ins != null)
  1048. {
  1049. if (monthCardTimeLength.TotalHours > 0)
  1050. {
  1051. fee = 0;
  1052. }
  1053. else if (failed)
  1054. {
  1055. fee = 0;
  1056. }
  1057. else
  1058. {
  1059. fee = FeeCalc(PaymentScheme.ins, parkTime, orderTimeLength);
  1060. }
  1061. try
  1062. {
  1063. if (Monitor.Monitor.PLC != null)
  1064. {
  1065. TerminalStru FeeMsg = new TerminalStru();
  1066. FeeMsg.terminalID = (short)termIndex;
  1067. FeeMsg.paymentStatus = (short)-1;
  1068. FeeMsg.licVerification = (short)-1;
  1069. FeeMsg.parkingFee = (short)fee;
  1070. FeeMsg.userType = (short)(monthCardType + 1);
  1071. Console.WriteLine(FeeMsg);
  1072. Monitor.Monitor.PLC.WriteToPLC(FeeMsg, PLCDataType.central);
  1073. Log.WriteLog(LogType.process, LogFile.INFO, "停车费用已发送至PLC");
  1074. }
  1075. }
  1076. catch { Console.WriteLine("error"); }
  1077. }
  1078. Monitor.Monitor.SetNotification("用户类型:" + (monthCardType+1) + ",费用:" + fee + ",等待凭证号被清除且流程结束后更新车辆状态");
  1079. FetchInfo fetchInfo = new FetchInfo(license, parkingRecordsID, remoteParkingRecordsID, fee, receiptNum);
  1080. if (!fetchMap.ContainsKey(termIndex))
  1081. fetchMap.Add(termIndex, fetchInfo);
  1082. else
  1083. fetchMap[termIndex] = fetchInfo;
  1084. }
  1085. }
  1086. }
  1087. private static void FetchCompleteProcess()
  1088. {
  1089. int fetchState = Monitor.Monitor.mainBlockInfo.fetchingRunning;
  1090. int termIndex = -1;
  1091. TerminalStru ts = new TerminalStru();
  1092. if (fetchState == 1)
  1093. {
  1094. //找到当前取车终端
  1095. for (int i = 0; i < terminalInfo.Count; i++)
  1096. {
  1097. if (terminalInfo[i].terminalID == Monitor.Monitor.mainBlockInfo.terminalID)
  1098. {
  1099. ts = terminalInfo[i];
  1100. termIndex = ts.terminalID;
  1101. break;
  1102. }
  1103. }
  1104. //4.等待终端获得收费完成信号且将凭证号清除后,清除收费完成信号并将信息写入数据库
  1105. int count = 0;
  1106. while (!isClosing && termIndex != -1)
  1107. {
  1108. if (Monitor.Monitor.mainBlockInfo.processCompleted == 1)
  1109. {
  1110. while (terminalInfo[termIndex - 1].receiptNum != 0)
  1111. {
  1112. count++;
  1113. if (count == 1)
  1114. Monitor.Monitor.SetNotification("等待凭证号清除");
  1115. if (count > 10000)//避免int型数据溢出
  1116. count = 2;
  1117. Thread.Sleep(200);
  1118. }
  1119. int paymentStatus = 0;
  1120. for (int i = 0; i < terminalInfo.Count; i++)
  1121. {
  1122. if (terminalInfo[i].terminalID == Monitor.Monitor.mainBlockInfo.terminalID)
  1123. {
  1124. paymentStatus = terminalInfo[i].paymentStatus;
  1125. break;
  1126. }
  1127. }
  1128. Log.WriteLog(LogType.process, LogFile.INFO, "检测到凭证号已被清除");
  1129. FetchInfo fi = null;
  1130. if (fetchMap.ContainsKey(termIndex))
  1131. {
  1132. fi = fetchMap[termIndex];
  1133. }
  1134. else
  1135. {
  1136. Monitor.Monitor.SetNotification("未查询到该取车终端存在计费操作");
  1137. return;
  1138. }
  1139. //TerminalStru fetchReset = new TerminalStru
  1140. //{
  1141. // terminalID = (short)termIndex,
  1142. // paymentStatus = (short)0,
  1143. // licVerification = (short)0,
  1144. // parkingFee = (short)32767,
  1145. // userType = (short)0
  1146. //};
  1147. //Monitor.Monitor.PLC.WriteToPLC(fetchReset, PLCDataType.central);
  1148. ClearTerminal(termIndex);
  1149. //更新停车记录与车辆状态
  1150. string updateParkingRecordsSql = "update parkingrecords set parkingRecordsState = 6,realGetTime = '" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") +
  1151. "',parkingPrice = " + fi.fee + ",paymentStatus = " + paymentStatus + " where receiptNum = '" + fi.receiptNum + "';";
  1152. List<string> list = new List<string>();
  1153. list.Add(updateParkingRecordsSql);
  1154. lock (Monitor.Monitor.localDBOper)
  1155. {
  1156. Monitor.Monitor.localDBOper.UpdateTransaction(list);
  1157. UpdateVehicle(fi.license, 0, fi.parkingRecordsID, false, false, 0);
  1158. }
  1159. lock (Monitor.Monitor.remoteDBOper)
  1160. {
  1161. Monitor.Monitor.remoteDBOper.UpdateTransaction(list);
  1162. UpdateVehicle(fi.license, 0, fi.remoteParkingRecordsID, false, true, 0);
  1163. }
  1164. Monitor.Monitor.SetNotification(fi.license + " 取车流程结束");
  1165. break;
  1166. }
  1167. Thread.Sleep(1000);
  1168. }
  1169. termUsedMap[termIndex] = false;
  1170. fetchMap.Remove(termIndex);
  1171. }
  1172. }
  1173. /// <summary>
  1174. /// 取车相关操作
  1175. ///
  1176. /// </summary>
  1177. private static void FetchProcess()
  1178. {
  1179. Task.Factory.StartNew(() =>
  1180. {
  1181. while (!isClosing)
  1182. {
  1183. FetchFeeCalcSubProcess();
  1184. Thread.Sleep(Monitor.Monitor.plcRefreshInterval);
  1185. }
  1186. });
  1187. Task.Factory.StartNew(() =>
  1188. {
  1189. while (!isClosing)
  1190. {
  1191. FetchCompleteProcess();
  1192. Thread.Sleep(Monitor.Monitor.plcRefreshInterval);
  1193. }
  1194. });
  1195. }
  1196. public static void Start()
  1197. {
  1198. for (int i = 0; i < Monitor.Monitor.plcTerminalCount; i++)
  1199. {
  1200. lockList.Add(false);
  1201. }
  1202. ParkProcess();
  1203. FetchProcess();
  1204. }
  1205. public static void Stop()
  1206. {
  1207. isClosing = true;
  1208. }
  1209. public class FetchInfo
  1210. {
  1211. public string license;
  1212. public int parkingRecordsID;
  1213. public int remoteParkingRecordsID;
  1214. public int fee;
  1215. public int receiptNum;
  1216. public FetchInfo(string license, int parkingRecordsID, int remoteParkingRecordsID, int fee, int receiptNum)
  1217. {
  1218. this.license = license;
  1219. this.parkingRecordsID = parkingRecordsID;
  1220. this.remoteParkingRecordsID = remoteParkingRecordsID;
  1221. this.fee = fee;
  1222. this.receiptNum = receiptNum;
  1223. }
  1224. }
  1225. }
  1226. }