Terminal.cs 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  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. Monitor.Monitor.SetNotification("预约车辆入场");
  669. }
  670. #region 通过数据库查询是否预约车,方法待定
  671. //int vehicleState = -1;
  672. //string vehicleStateCheckSql = "select vehiclepParkState from vehicle where numberPlate = '" + license + "';";
  673. //MySqlDataReader reader = Monitor.Monitor.localDBOper.Query(vehicleStateCheckSql);
  674. //if (reader != null)
  675. //{
  676. // try
  677. // {
  678. // if (reader.Read())
  679. // {
  680. // vehicleState = reader.GetInt32("vehiclepParkState");
  681. // }
  682. // }
  683. // catch (Exception e) { Log.WriteLog(LogType.process, LogFile.ERROR, "预约检测,查询车辆状态异常"); ; Console.WriteLine(e.Message); };
  684. // try
  685. // {
  686. // reader.Close();
  687. // reader.Dispose();
  688. // }
  689. // catch { }
  690. //}
  691. //已预约车辆入场
  692. //if (vehicleState == 4)
  693. //{
  694. // registered = 3;
  695. //}
  696. #endregion
  697. TerminalStru ts = new TerminalStru
  698. {
  699. terminalID = terminalInfo[index].terminalID,
  700. paymentStatus = (short)-1,
  701. btnStatus = registered,
  702. cmd = (short)-1,
  703. licenseCodeA = -1,
  704. licenseCodeB = -1,
  705. licenseCodeC = -1,
  706. licenseCodeD = -1,
  707. receiptNum = -1
  708. };
  709. Monitor.Monitor.PLC.WriteToPLC(ts, PLCDataType.terminal);
  710. Monitor.Monitor.SetNotification("指令写入PLC:"+ts.btnStatus);
  711. Thread.Sleep(1000);
  712. }
  713. else
  714. {
  715. Thread.Sleep(200);
  716. }
  717. lockList[index] = false;
  718. });
  719. }
  720. /// <summary>
  721. /// 停车相关操作
  722. /// 0.观测到地感信号,且按钮状态不为3,则启动号牌机获取号牌,并判断是否预约车
  723. /// 1.读取到号牌机启动指令,启动号牌机并根据是否注册用户进行操作
  724. /// 非注册拿到号牌后记录并告知plc
  725. /// 注册用户,核对号牌与用户关联信息
  726. /// 2.停车流程结束,写数据库
  727. /// </summary>
  728. ///
  729. private static void ParkProcess()
  730. {
  731. Task.Factory.StartNew(() =>
  732. {
  733. while (!isClosing)
  734. {
  735. ParkNumSubProcess();
  736. Thread.Sleep(Monitor.Monitor.plcRefreshInterval * 5);
  737. }
  738. });
  739. Task.Factory.StartNew(() =>
  740. {
  741. while (!isClosing)
  742. {
  743. ParkCompleteSubProcess();
  744. Thread.Sleep(Monitor.Monitor.plcRefreshInterval * 5);
  745. }
  746. });
  747. Task.Factory.StartNew(() =>
  748. {
  749. while (!isClosing)
  750. {
  751. ReservedCarCheck();
  752. Thread.Sleep(Monitor.Monitor.plcRefreshInterval * 5);
  753. }
  754. });
  755. }
  756. /// <summary>
  757. /// 根据凭证号查询停车记录
  758. /// </summary>
  759. /// <param name="receipt"></param>
  760. /// <param name="recordID"></param>
  761. /// <param name="userID"></param>
  762. /// <param name="license"></param>
  763. /// <param name="realParkTime"></param>
  764. private static bool FindLocalParkingRecord(int receipt, out int recordID, out int userID, out string license, out string realParkTime)
  765. {
  766. recordID = 0;
  767. userID = 0;
  768. license = "";
  769. realParkTime = "";
  770. bool result = false;
  771. string parkRecordsSql = "select parkingRecordsID,userID,numberPlate,realParkTime from parkingrecords where receiptNum = " + receipt + ";";
  772. MySqlDataReader reader = Monitor.Monitor.localDBOper.Query(parkRecordsSql);
  773. try
  774. {
  775. if (reader != null && reader.Read())
  776. {
  777. object[] receiver = new object[4];
  778. reader.GetValues(receiver);
  779. recordID = (int)receiver[0];
  780. userID = (int)receiver[1];
  781. license = (string)receiver[2];
  782. realParkTime = (string)receiver[3];
  783. result = true;
  784. }
  785. else
  786. {
  787. Log.WriteLog(LogType.process, LogFile.ERROR, "根据凭证号查询停车记录失败");
  788. Monitor.Monitor.SetNotification("取车流程, 根据凭证号查询停车记录失败");
  789. result = false;
  790. }
  791. //if (reader != null)
  792. //{
  793. // reader.Close();
  794. // reader.Dispose();
  795. //}
  796. }
  797. catch (Exception e) { Log.WriteLog(LogType.process, LogFile.ERROR, "根据凭证号查询停车记录失败"); Console.WriteLine(e.Message); result = false; }
  798. try
  799. {
  800. if (reader != null)
  801. {
  802. reader.Close();
  803. reader.Dispose();
  804. }
  805. }
  806. catch { }
  807. Log.WriteLog(LogType.process, LogFile.INFO, "根据凭证号查询停车记录成功");
  808. return result;
  809. }
  810. private static bool FindRemoteParkingRecord(int receipt, out int recordID)
  811. {
  812. recordID = 0;
  813. bool result = false;
  814. string remoteParkRecordsSql = "select parkingRecordsID from parkingrecords where receiptNum = " + receipt + ";";
  815. MySqlDataReader reader = Monitor.Monitor.remoteDBOper.Query(remoteParkRecordsSql);
  816. try
  817. {
  818. if (reader != null && reader.Read())
  819. {
  820. object[] receiver = new object[1];
  821. reader.GetValues(receiver);
  822. recordID = (int)receiver[0];
  823. result = true;
  824. }
  825. }
  826. catch (Exception e) { Console.WriteLine(e.Message); result = false; }
  827. try
  828. {
  829. if (reader != null)
  830. {
  831. reader.Close();
  832. reader.Dispose();
  833. }
  834. }
  835. catch (Exception e) { Console.WriteLine(e.Message); result = false; }
  836. return result;
  837. }
  838. /// <summary>
  839. /// 根据号牌获取本地与远端用户VIP月卡及预约信息
  840. /// </summary>
  841. /// <param name="license"></param>
  842. /// <param name="orderRecordsID"></param>
  843. /// <param name="monthCardType"></param>
  844. /// <param name="monthCardTimeLength"></param>
  845. /// <param name="orderTimeLength"></param>
  846. /// <returns></returns>
  847. private static bool FindVipInfo(string license, ref int orderRecordsID, ref int monthCardType, ref TimeSpan monthCardTimeLength, ref TimeSpan orderTimeLength)
  848. {
  849. bool result = true;
  850. string monthCardTime = "";
  851. //本地VIP信息
  852. string vipInfoSql = "select orderRecordsID,monthCardType,monthCardTime from vehicle where numberPlate = '" + license + "';";
  853. MySqlDataReader reader = null;
  854. try
  855. {
  856. reader = Monitor.Monitor.localDBOper.Query(vipInfoSql);
  857. if (reader != null && reader.Read())
  858. {
  859. object[] receiver = new object[3];
  860. reader.GetValues(receiver);
  861. Console.WriteLine(receiver[2].GetType());
  862. if (!receiver[0].GetType().Equals(typeof(DBNull)))
  863. {
  864. orderRecordsID = (int)receiver[0];
  865. }
  866. if (!receiver[1].GetType().Equals(typeof(DBNull)))
  867. {
  868. monthCardType = (int)(UInt32)receiver[1];
  869. }
  870. if (!receiver[2].GetType().Equals(typeof(DBNull)))
  871. {
  872. monthCardTime = (string)receiver[2];
  873. }
  874. //orderRecordsID = (Int32)receiver[0];
  875. //monthCardType = (Int32)receiver[1];
  876. //monthCardTime = (string)receiver[2];
  877. monthCardTimeLength = DateTime.Parse(monthCardTime) - DateTime.Now;
  878. if (monthCardTimeLength.TotalHours <= 0)
  879. {
  880. monthCardType = 0;
  881. }
  882. Log.WriteLog(LogType.process, LogFile.INFO, "已获取本地预约与月卡信息");
  883. }
  884. }
  885. catch (Exception e) { Console.WriteLine(e.Message); result = false; }
  886. try
  887. {
  888. if (reader != null)
  889. {
  890. reader.Close();
  891. reader.Dispose();
  892. }
  893. }
  894. catch (Exception e) { Console.WriteLine(e.Message); result = false; }
  895. //云端VIP信息
  896. try
  897. {
  898. reader = Monitor.Monitor.remoteDBOper.Query(vipInfoSql);
  899. if (reader != null && reader.Read())
  900. {
  901. object[] receiver = new object[3];
  902. reader.GetValues(receiver);
  903. int cardType = (int)(UInt32)receiver[1];
  904. string CardTime = (string)receiver[2];
  905. TimeSpan t = DateTime.Parse(CardTime) - DateTime.Now;
  906. //云端
  907. if (t.TotalHours > 0 && cardType > monthCardType)
  908. {
  909. monthCardType = cardType;
  910. monthCardTimeLength = t;
  911. }
  912. Log.WriteLog(LogType.process, LogFile.INFO, "已获取云端预约与月卡信息");
  913. }
  914. }
  915. catch (Exception e) { Console.WriteLine(e.Message); result = false; }
  916. try
  917. {
  918. if (reader != null)
  919. {
  920. reader.Close();
  921. reader.Dispose();
  922. }
  923. }
  924. catch (Exception e) { Console.WriteLine(e.Message); result = false; }
  925. //有预约记录,判断记录有效性,有效则获取预约时长
  926. if (orderRecordsID != 0)
  927. {
  928. //修改orderTimeLength
  929. }
  930. return result;
  931. }
  932. /// <summary>
  933. /// 预约与停车费用计算
  934. /// </summary>
  935. /// <param name="scheme"></param>
  936. /// <param name="parkTime"></param>
  937. /// <param name="orderTimeLength"></param>
  938. /// <returns></returns>
  939. private static int FeeCalc(PaymentScheme scheme, DateTime parkTime, TimeSpan orderTimeLength)
  940. {
  941. try
  942. {
  943. int orderFee = scheme.bookCharge * (int)orderTimeLength.TotalHours;
  944. int parkFee = 0;
  945. int hours = (int)(DateTime.Now - parkTime).TotalHours;
  946. switch (scheme.schemeType)
  947. {
  948. //按时计费
  949. case 1:
  950. if (hours > scheme.firstChargeTime + scheme.freeTime)
  951. {
  952. if (hours > 24)
  953. {
  954. parkFee = hours/24 * scheme.upperBound + Math.Min(scheme.intervalCharge * (hours%24) / scheme.chargeInterval, scheme.upperBound);
  955. }
  956. else
  957. {
  958. parkFee = Math.Min(scheme.firstCharge + scheme.intervalCharge * hours / scheme.chargeInterval, scheme.upperBound);
  959. }
  960. }
  961. else if (hours > scheme.freeTime)
  962. {
  963. parkFee = scheme.firstCharge;
  964. }
  965. else
  966. {
  967. parkFee = 0;
  968. }
  969. break;
  970. //按次计费
  971. case 2:
  972. //Console.WriteLine(DateTime.Now.Date.ToString()+","+ parkTime.Date.ToString());
  973. int overnightCount = (DateTime.Now.Date - parkTime.Date).Days;
  974. parkFee = scheme.eachCharge + overnightCount * scheme.overnightCharge;
  975. break;
  976. //按时间段计费
  977. case 3:
  978. TimeSpan currentTimeOfDay = DateTime.Now.TimeOfDay;
  979. TimeSpan parkTimeOfDay = parkTime.TimeOfDay;
  980. TimeSpan startTimeOfDay = DateTime.Parse(scheme.startChargeTime).TimeOfDay;
  981. TimeSpan endTimeOfDay = DateTime.Parse(scheme.endChargeTime).TimeOfDay;
  982. double countingHoursOfDay = endTimeOfDay.TotalHours - startTimeOfDay.TotalHours;
  983. int days = (int)((DateTime.Now - parkTime).TotalHours - currentTimeOfDay.TotalHours + parkTimeOfDay.TotalHours);
  984. int previousOffset = (int)(-(Math.Max(0, parkTimeOfDay.TotalHours - startTimeOfDay.TotalHours)));
  985. int currentOffset = (int)Math.Min((currentTimeOfDay.TotalHours - startTimeOfDay.TotalHours), countingHoursOfDay);
  986. int peakTimeLength = days * (int)countingHoursOfDay + previousOffset + currentOffset;
  987. parkFee = peakTimeLength * scheme.chargeStandard + ((int)((DateTime.Now - parkTime).TotalHours) - peakTimeLength) * scheme.intervalCharge;
  988. break;
  989. }
  990. Log.WriteLog(LogType.process, LogFile.INFO, "停车费用" + parkFee);
  991. return parkFee;
  992. }
  993. catch (Exception e) { Console.WriteLine("费用计算异常"); return -1; }
  994. }
  995. /// <summary>
  996. /// 取车计费子操作
  997. /// </summary>
  998. private static void FetchFeeCalcSubProcess()
  999. {
  1000. int receiptNum = 0;
  1001. int termIndex = -1;
  1002. bool failed = false;
  1003. TerminalStru ts = new TerminalStru();
  1004. for (int i = 0; i < terminalInfo.Count; i++)
  1005. {
  1006. ts = terminalInfo[i];
  1007. termIndex = ts.terminalID;
  1008. receiptNum = ts.receiptNum;
  1009. //取车状态、凭证号不为空,查询数据库计费
  1010. //Console.WriteLine((termIndex != -1) + "," + (termUsedMap.Count >= termIndex) + "," + termUsedMap.TryGetValue(termIndex, out bool valuee) + "," + (!valuee) + "," + (fetchState == 1) + "," + (receiptNum != 0));
  1011. if (termIndex != -1 && termUsedMap.Count >= termIndex && termUsedMap.TryGetValue(termIndex, out bool value) && !value && ts.cmd == 2 && receiptNum != 0)
  1012. {
  1013. //fetchState = Monitor.Monitor.mainBlockInfo.fetchingRunning;
  1014. Monitor.Monitor.SetNotification(termIndex + "号终端准备计算费用");
  1015. termUsedMap[termIndex] = true;
  1016. Console.WriteLine("终端编号" + termIndex);
  1017. int parkingRecordsID = 0;
  1018. int remoteParkingRecordsID = 0;
  1019. int userID = 0;
  1020. string license = "";
  1021. string realParkTime = "";
  1022. //1.根据凭证号查询停车记录
  1023. if (!FindLocalParkingRecord(receiptNum, out parkingRecordsID, out userID, out license, out realParkTime))
  1024. {
  1025. failed = true;
  1026. //return;
  1027. }
  1028. else
  1029. {
  1030. FindRemoteParkingRecord(receiptNum, out remoteParkingRecordsID);
  1031. }
  1032. //本地存在号牌,后续操作
  1033. //2.根据号牌查询车辆月卡信息与预约记录
  1034. int orderRecordsID = 0;
  1035. int monthCardType = -1;
  1036. string monthCardTime = "";
  1037. TimeSpan monthCardTimeLength = new TimeSpan(0);
  1038. TimeSpan orderTimeLength = new TimeSpan(0);
  1039. if (license != "")
  1040. {
  1041. FindVipInfo(license, ref orderRecordsID, ref monthCardType, ref monthCardTimeLength, ref orderTimeLength);
  1042. }
  1043. if (monthCardType == -1) monthCardType = 0;
  1044. //3.获取停车时刻,根据用户类型计费发送给PLC,暂认为无预约
  1045. DateTime parkTime = DateTime.Parse(realParkTime);
  1046. DateTime currentTime = DateTime.Now;
  1047. int fee = 32767;
  1048. if (failed) { monthCardType = -2; }
  1049. if (PaymentScheme.ins != null)
  1050. {
  1051. if (monthCardTimeLength.TotalHours > 0)
  1052. {
  1053. fee = 0;
  1054. }
  1055. else if (failed)
  1056. {
  1057. fee = 0;
  1058. }
  1059. else
  1060. {
  1061. fee = FeeCalc(PaymentScheme.ins, parkTime, orderTimeLength);
  1062. }
  1063. try
  1064. {
  1065. if (Monitor.Monitor.PLC != null)
  1066. {
  1067. TerminalStru FeeMsg = new TerminalStru();
  1068. FeeMsg.terminalID = (short)termIndex;
  1069. FeeMsg.paymentStatus = (short)-1;
  1070. FeeMsg.licVerification = (short)-1;
  1071. FeeMsg.parkingFee = (short)fee;
  1072. FeeMsg.userType = (short)(monthCardType + 1);
  1073. Console.WriteLine(FeeMsg);
  1074. Monitor.Monitor.PLC.WriteToPLC(FeeMsg, PLCDataType.central);
  1075. Log.WriteLog(LogType.process, LogFile.INFO, "停车费用已发送至PLC");
  1076. }
  1077. }
  1078. catch { Console.WriteLine("error"); }
  1079. }
  1080. Monitor.Monitor.SetNotification("用户类型:" + (monthCardType+1) + ",费用:" + fee + ",等待凭证号被清除且流程结束后更新车辆状态");
  1081. FetchInfo fetchInfo = new FetchInfo(license, parkingRecordsID, remoteParkingRecordsID, fee, receiptNum);
  1082. if (!fetchMap.ContainsKey(termIndex))
  1083. fetchMap.Add(termIndex, fetchInfo);
  1084. else
  1085. fetchMap[termIndex] = fetchInfo;
  1086. }
  1087. }
  1088. }
  1089. private static void FetchCompleteProcess()
  1090. {
  1091. int fetchState = Monitor.Monitor.mainBlockInfo.fetchingRunning;
  1092. int termIndex = -1;
  1093. TerminalStru ts = new TerminalStru();
  1094. if (fetchState == 1)
  1095. {
  1096. //找到当前取车终端
  1097. for (int i = 0; i < terminalInfo.Count; i++)
  1098. {
  1099. if (terminalInfo[i].terminalID == Monitor.Monitor.mainBlockInfo.terminalID)
  1100. {
  1101. ts = terminalInfo[i];
  1102. termIndex = ts.terminalID;
  1103. break;
  1104. }
  1105. }
  1106. //4.等待终端获得收费完成信号且将凭证号清除后,清除收费完成信号并将信息写入数据库
  1107. int count = 0;
  1108. while (!isClosing && termIndex != -1)
  1109. {
  1110. if (Monitor.Monitor.mainBlockInfo.processCompleted == 1)
  1111. {
  1112. while (terminalInfo[termIndex - 1].receiptNum != 0)
  1113. {
  1114. count++;
  1115. if (count == 1)
  1116. Monitor.Monitor.SetNotification("等待凭证号清除");
  1117. if (count > 10000)//避免int型数据溢出
  1118. count = 2;
  1119. Thread.Sleep(200);
  1120. }
  1121. int paymentStatus = 0;
  1122. for (int i = 0; i < terminalInfo.Count; i++)
  1123. {
  1124. if (terminalInfo[i].terminalID == Monitor.Monitor.mainBlockInfo.terminalID)
  1125. {
  1126. paymentStatus = terminalInfo[i].paymentStatus;
  1127. break;
  1128. }
  1129. }
  1130. Log.WriteLog(LogType.process, LogFile.INFO, "检测到凭证号已被清除");
  1131. FetchInfo fi = null;
  1132. if (fetchMap.ContainsKey(termIndex))
  1133. {
  1134. fi = fetchMap[termIndex];
  1135. }
  1136. else
  1137. {
  1138. Monitor.Monitor.SetNotification("未查询到该取车终端存在计费操作");
  1139. return;
  1140. }
  1141. //TerminalStru fetchReset = new TerminalStru
  1142. //{
  1143. // terminalID = (short)termIndex,
  1144. // paymentStatus = (short)0,
  1145. // licVerification = (short)0,
  1146. // parkingFee = (short)32767,
  1147. // userType = (short)0
  1148. //};
  1149. //Monitor.Monitor.PLC.WriteToPLC(fetchReset, PLCDataType.central);
  1150. ClearTerminal(termIndex);
  1151. //更新停车记录与车辆状态
  1152. string updateParkingRecordsSql = "update parkingrecords set parkingRecordsState = 6,realGetTime = '" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") +
  1153. "',parkingPrice = " + fi.fee + ",paymentStatus = " + paymentStatus + " where receiptNum = '" + fi.receiptNum + "';";
  1154. List<string> list = new List<string>();
  1155. list.Add(updateParkingRecordsSql);
  1156. lock (Monitor.Monitor.localDBOper)
  1157. {
  1158. Monitor.Monitor.localDBOper.UpdateTransaction(list);
  1159. UpdateVehicle(fi.license, 0, fi.parkingRecordsID, false, false, 0);
  1160. }
  1161. lock (Monitor.Monitor.remoteDBOper)
  1162. {
  1163. Monitor.Monitor.remoteDBOper.UpdateTransaction(list);
  1164. UpdateVehicle(fi.license, 0, fi.remoteParkingRecordsID, false, true, 0);
  1165. }
  1166. Monitor.Monitor.SetNotification(fi.license + " 取车流程结束");
  1167. break;
  1168. }
  1169. Thread.Sleep(1000);
  1170. }
  1171. termUsedMap[termIndex] = false;
  1172. fetchMap.Remove(termIndex);
  1173. }
  1174. }
  1175. /// <summary>
  1176. /// 取车相关操作
  1177. ///
  1178. /// </summary>
  1179. private static void FetchProcess()
  1180. {
  1181. Task.Factory.StartNew(() =>
  1182. {
  1183. while (!isClosing)
  1184. {
  1185. FetchFeeCalcSubProcess();
  1186. Thread.Sleep(Monitor.Monitor.plcRefreshInterval);
  1187. }
  1188. });
  1189. Task.Factory.StartNew(() =>
  1190. {
  1191. while (!isClosing)
  1192. {
  1193. FetchCompleteProcess();
  1194. Thread.Sleep(Monitor.Monitor.plcRefreshInterval);
  1195. }
  1196. });
  1197. }
  1198. public static void Start()
  1199. {
  1200. for (int i = 0; i < Monitor.Monitor.plcTerminalCount; i++)
  1201. {
  1202. lockList.Add(false);
  1203. }
  1204. ParkProcess();
  1205. FetchProcess();
  1206. }
  1207. public static void Stop()
  1208. {
  1209. isClosing = true;
  1210. }
  1211. public class FetchInfo
  1212. {
  1213. public string license;
  1214. public int parkingRecordsID;
  1215. public int remoteParkingRecordsID;
  1216. public int fee;
  1217. public int receiptNum;
  1218. public FetchInfo(string license, int parkingRecordsID, int remoteParkingRecordsID, int fee, int receiptNum)
  1219. {
  1220. this.license = license;
  1221. this.parkingRecordsID = parkingRecordsID;
  1222. this.remoteParkingRecordsID = remoteParkingRecordsID;
  1223. this.fee = fee;
  1224. this.receiptNum = receiptNum;
  1225. }
  1226. }
  1227. }
  1228. }