Terminal.cs 61 KB

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