Terminal.cs 62 KB

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