Terminal.cs 69 KB

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