Terminal.cs 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454
  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 = 2;//验证失败
  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)2
  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 = 2;
  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. Log.WriteLog(LogType.process, LogFile.ERROR, "停车结束时未找到号牌");
  658. }
  659. break;
  660. }
  661. Thread.Sleep(200);
  662. }
  663. //找到终端号,判断是否注册用户
  664. //拿到号牌
  665. if (/*terminalInfo[i].terminalID == currentTerm && */idLicMap.TryGetValue(currentTerm, out license) && license != "")
  666. {
  667. UpdateSpecificParkingSpace();
  668. for (int i = 0; i < terminalInfo.Count; i++)
  669. {
  670. if (terminalInfo[i].terminalID == currentTerm)
  671. term = terminalInfo[i];
  672. }
  673. //term = terminalInfo[currentTerm - 1];
  674. Thread.Sleep(500);
  675. parkingSpaceID = Monitor.Monitor.mainBlockInfo.currentParkingSpaceID;
  676. int receipt = 0;
  677. idReceiptMap.TryGetValue(term.terminalID, out receipt);
  678. //无车位信息则跳出当前循环
  679. if (parkingSpaceID == 0 || receipt == 0)
  680. {
  681. try
  682. {
  683. idLicMap.Remove(currentTerm);
  684. ClearTerminal(term.terminalID, false);
  685. }
  686. catch { }
  687. Monitor.Monitor.SetNotification("无车位或凭证号,车位:" + parkingSpaceID + ",凭证号:" + receipt, parkMonitor.model.TextColor.Warning);
  688. return;
  689. }
  690. UpdateVehicle(license, 0, 0, true, false, 0);
  691. UpdateVehicle(license, 0, 0, true, true, 0);
  692. //插入停车记录
  693. string parkingRecordsSql = "";
  694. if (term.licenseCodeA != 0)
  695. {
  696. parkingRecordsSql = "INSERT INTO parkingrecords (userID, numberPlate,parkingSpaceID,garageID,parkingRecordsState,realParkTime,receiptNum,parkingPrice)" +
  697. "values ('" + term.licenseCodeA + "','" + license + "','" + parkingSpaceID + "','" + Monitor.Monitor.garageID + "',3,'" + DateTime.Now.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss") + "','" + receipt + "',NULL);";
  698. }
  699. else
  700. {
  701. parkingRecordsSql = "INSERT INTO parkingrecords (userID, numberPlate,parkingSpaceID,garageID,parkingRecordsState,realParkTime,receiptNum,parkingPrice)" +
  702. "values ('" + 1 + "','" + license + "','" + parkingSpaceID + "','" + Monitor.Monitor.garageID + "',3,'" + DateTime.Now.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss") + "','" + receipt + "',NULL);";
  703. }
  704. List<string> list = new List<string>();
  705. list.Add(parkingRecordsSql);
  706. int parkingRecordsID = 0;
  707. int remoteParkingRecordsID = 0;
  708. Monitor.Monitor.localDBOper.Insert(list, out parkingRecordsID);
  709. Monitor.Monitor.remoteDBOper.Insert(list, out remoteParkingRecordsID);
  710. Log.WriteLog(LogType.process, LogFile.INFO, "停车记录已插入," + license + "停在" + parkingSpaceID + ",凭证号:" + receipt);
  711. Monitor.Monitor.SetNotification("停车记录已插入", parkMonitor.model.TextColor.Info);
  712. //查询记录ID号
  713. //string findRecordSql = "select parkingRecordsID from parkingrecords where receiptNum = " + term.receiptNum + ";";
  714. //QueryParkingRecordsID(findRecordSql, out parkingRecordsID, false);
  715. //QueryParkingRecordsID(findRecordSql, out remoteParkingRecordsID, true);
  716. //更新车辆表
  717. if (parkingRecordsID != 0)
  718. {
  719. UpdateVehicle(license, 1, parkingRecordsID, true, false, parkingSpaceID);
  720. }
  721. if (remoteParkingRecordsID != 0)
  722. {
  723. UpdateVehicle(license, 1, remoteParkingRecordsID, true, true, parkingSpaceID);
  724. }
  725. Log.WriteLog(LogType.process, LogFile.INFO, "停车记录表已更新");
  726. //更新车位
  727. UpdateAllParkingSpace(false);
  728. //Monitor.Monitor.AddNotification("停车数据库已更新,等待流程结束状态清零。");
  729. //while (Monitor.Monitor.mainBlockInfo.processCompleted != 0)
  730. //{
  731. // Thread.Sleep(1000);
  732. //}
  733. //中控清除车牌、凭证号、号牌验证等信息
  734. ClearTerminal(term.terminalID, false);
  735. //停车流程确保已清除中控块完成信号
  736. Thread.Sleep(500);
  737. ResetCompleteSignal();
  738. try { idLicMap.Remove(currentTerm); }
  739. catch { }
  740. Monitor.Monitor.SetNotification(license + " 数据库已更新,停车流程结束", parkMonitor.model.TextColor.Info);
  741. }
  742. }
  743. }
  744. /// <summary>
  745. /// 预约车辆检测
  746. /// </summary>
  747. private static void ReservedCarCheck()
  748. {
  749. if (lockList.Count != terminalInfo.Count)
  750. {
  751. lockList.Clear();
  752. for (int i = 0; i < terminalInfo.Count; i++)
  753. {
  754. lockList.Add(false);
  755. }
  756. }
  757. //找到停车终端,且有地感无按钮数据
  758. for (int i = 0; i < terminalInfo.Count; i++)
  759. {
  760. //Console.WriteLine(i);
  761. if (terminalInfo[i].terminalStatus == 1 && terminalInfo[i].groundStatus == 1 && terminalInfo[i].btnStatus == 0 && !lockList[i])
  762. {
  763. lockList[i] = true;
  764. Action<int> reserveAction = new Action<int>(ReservedOper);
  765. reserveAction(i);
  766. }
  767. }
  768. }
  769. private static void ReservedOper(int index)
  770. {
  771. Task.Factory.StartNew(() =>
  772. {
  773. string license = "";
  774. int countdown = 2;
  775. short registered = 4;
  776. while (license == "" && countdown-- > 0)
  777. {
  778. license = Monitor.Monitor.numMachineLinker.GetLicensePlate(terminalInfo[index].terminalID);
  779. }
  780. if (license != "")
  781. {
  782. Monitor.Monitor.SetNotification(terminalInfo[index].terminalID + "号车位收到地感,拍到" + license, parkMonitor.model.TextColor.Log);
  783. //map中加入或更新号牌,供完成时写入数据库用
  784. if (idLicMap.ContainsKey(terminalInfo[index].terminalID))
  785. {
  786. idLicMap[terminalInfo[index].terminalID] = license;
  787. }
  788. else
  789. {
  790. idLicMap.Add(terminalInfo[index].terminalID, license);
  791. }
  792. if (Monitor.Monitor.webServer.ReservedCarCheck(license))
  793. {
  794. registered = 3;
  795. Monitor.Monitor.SetNotification("预约车辆入场", parkMonitor.model.TextColor.Info);
  796. }
  797. #region 通过数据库查询是否预约车,方法待定
  798. //int vehicleState = -1;
  799. //string vehicleStateCheckSql = "select vehiclepParkState from vehicle where numberPlate = '" + license + "';";
  800. //MySqlDataReader reader = Monitor.Monitor.localDBOper.Query(vehicleStateCheckSql);
  801. //if (reader != null)
  802. //{
  803. // try
  804. // {
  805. // if (reader.Read())
  806. // {
  807. // vehicleState = reader.GetInt32("vehiclepParkState");
  808. // }
  809. // }
  810. // catch (Exception e) { Log.WriteLog(LogType.process, LogFile.ERROR, "预约检测,查询车辆状态异常"); ; Console.WriteLine(e.Message); };
  811. // try
  812. // {
  813. // reader.Close();
  814. // reader.Dispose();
  815. // }
  816. // catch { }
  817. //}
  818. //已预约车辆入场
  819. //if (vehicleState == 4)
  820. //{
  821. // registered = 3;
  822. //}
  823. #endregion
  824. TerminalStru ts = new TerminalStru
  825. {
  826. terminalID = terminalInfo[index].terminalID,
  827. paymentStatus = (short)-1,
  828. btnStatus = registered,
  829. cmd = (short)-1,
  830. licenseCodeA = -1,
  831. licenseCodeB = -1,
  832. licenseCodeC = -1,
  833. licenseCodeD = -1,
  834. receiptNum = -1
  835. };
  836. Monitor.Monitor.PLC.WriteToPLC(ts, PLCDataType.terminal);
  837. Monitor.Monitor.SetNotification("指令写入PLC:" + ts.btnStatus, parkMonitor.model.TextColor.Log);
  838. Thread.Sleep(1000);
  839. }
  840. else
  841. {
  842. Thread.Sleep(200);
  843. }
  844. lockList[index] = false;
  845. });
  846. }
  847. /// <summary>
  848. /// 停车相关操作
  849. /// 0.观测到地感信号,且按钮状态不为3,则启动号牌机获取号牌,并判断是否预约车
  850. /// 1.读取到号牌机启动指令,启动号牌机并根据是否注册用户进行操作
  851. /// 非注册拿到号牌后记录并告知plc
  852. /// 注册用户,核对号牌与用户关联信息
  853. /// 2.停车流程结束,写数据库
  854. /// </summary>
  855. ///
  856. private static void ParkProcess()
  857. {
  858. Task.Factory.StartNew(() =>
  859. {
  860. while (!isClosing)
  861. {
  862. ParkNumSubProcess();
  863. Thread.Sleep(Monitor.Monitor.plcRefreshInterval * 5);
  864. }
  865. });
  866. Task.Factory.StartNew(() =>
  867. {
  868. while (!isClosing)
  869. {
  870. ParkCompleteSubProcess();
  871. Thread.Sleep(Monitor.Monitor.plcRefreshInterval * 5);
  872. }
  873. });
  874. Task.Factory.StartNew(() =>
  875. {
  876. while (!isClosing)
  877. {
  878. ReservedCarCheck();
  879. Thread.Sleep(Monitor.Monitor.plcRefreshInterval * 5);
  880. }
  881. });
  882. }
  883. /// <summary>
  884. /// 根据凭证号查询停车记录
  885. /// </summary>
  886. /// <param name="receipt"></param>
  887. /// <param name="recordID"></param>
  888. /// <param name="userID"></param>
  889. /// <param name="license"></param>
  890. /// <param name="realParkTime"></param>
  891. private static bool FindLocalParkingRecord(int receipt, out int recordID, out int userID, out string license, out string realParkTime)
  892. {
  893. recordID = 0;
  894. userID = 0;
  895. license = "";
  896. realParkTime = "";
  897. bool result = false;
  898. string parkRecordsSql = "select parkingRecordsID,userID,numberPlate,realParkTime from parkingrecords where receiptNum = " + receipt + ";";
  899. MySqlDataReader reader = Monitor.Monitor.localDBOper.Query(parkRecordsSql);
  900. try
  901. {
  902. if (reader != null && reader.Read())
  903. {
  904. object[] receiver = new object[4];
  905. reader.GetValues(receiver);
  906. recordID = (int)receiver[0];
  907. userID = (int)receiver[1];
  908. license = (string)receiver[2];
  909. realParkTime = (string)receiver[3];
  910. result = true;
  911. }
  912. else
  913. {
  914. Log.WriteLog(LogType.process, LogFile.ERROR, "根据凭证号" + receipt + "查询停车记录失败");
  915. Monitor.Monitor.SetNotification("取车流程, 根据凭证号查询停车记录失败", parkMonitor.model.TextColor.Error);
  916. result = false;
  917. }
  918. //if (reader != null)
  919. //{
  920. // reader.Close();
  921. // reader.Dispose();
  922. //}
  923. }
  924. catch (Exception e) { Log.WriteLog(LogType.process, LogFile.ERROR, "根据凭证号" + receipt + "查询停车记录失败"); Console.WriteLine(e.Message); result = false; }
  925. try
  926. {
  927. if (reader != null)
  928. {
  929. reader.Close();
  930. reader.Dispose();
  931. }
  932. }
  933. catch { }
  934. if (result)
  935. Log.WriteLog(LogType.process, LogFile.INFO, "根据凭证号查询停车记录成功");
  936. return result;
  937. }
  938. private static bool FindRemoteParkingRecord(int receipt, out int recordID)
  939. {
  940. recordID = 0;
  941. bool result = false;
  942. string remoteParkRecordsSql = "select parkingRecordsID from parkingrecords where receiptNum = " + receipt + ";";
  943. MySqlDataReader reader = Monitor.Monitor.remoteDBOper.Query(remoteParkRecordsSql);
  944. try
  945. {
  946. if (reader != null && reader.Read())
  947. {
  948. object[] receiver = new object[1];
  949. reader.GetValues(receiver);
  950. recordID = (int)receiver[0];
  951. result = true;
  952. }
  953. }
  954. catch (Exception e) { Console.WriteLine(e.Message); result = false; }
  955. try
  956. {
  957. if (reader != null)
  958. {
  959. reader.Close();
  960. reader.Dispose();
  961. }
  962. }
  963. catch (Exception e) { Console.WriteLine(e.Message); result = false; }
  964. return result;
  965. }
  966. /// <summary>
  967. /// 根据号牌获取本地与远端用户VIP月卡及预约信息
  968. /// </summary>
  969. /// <param name="license"></param>
  970. /// <param name="orderRecordsID"></param>
  971. /// <param name="monthCardType"></param>
  972. /// <param name="monthCardTimeLength"></param>
  973. /// <param name="orderTimeLength"></param>
  974. /// <returns></returns>
  975. private static bool FindVipInfo(string license, ref int orderRecordsID, ref int monthCardType, ref TimeSpan monthCardTimeLength, ref TimeSpan orderTimeLength)
  976. {
  977. bool result = true;
  978. string monthCardTime = "";
  979. //本地VIP信息
  980. string vipInfoSql = "select orderRecordsID,monthCardType,monthCardTime from vehicle where numberPlate = '" + license + "';";
  981. MySqlDataReader reader = null;
  982. try
  983. {
  984. reader = Monitor.Monitor.localDBOper.Query(vipInfoSql);
  985. if (reader != null && reader.Read())
  986. {
  987. object[] receiver = new object[3];
  988. reader.GetValues(receiver);
  989. Console.WriteLine(receiver[2].GetType());
  990. if (!receiver[0].GetType().Equals(typeof(DBNull)))
  991. {
  992. orderRecordsID = (int)receiver[0];
  993. }
  994. if (!receiver[1].GetType().Equals(typeof(DBNull)))
  995. {
  996. monthCardType = (int)(UInt32)receiver[1];
  997. }
  998. if (!receiver[2].GetType().Equals(typeof(DBNull)))
  999. {
  1000. monthCardTime = (string)receiver[2];
  1001. }
  1002. //orderRecordsID = (Int32)receiver[0];
  1003. //monthCardType = (Int32)receiver[1];
  1004. //monthCardTime = (string)receiver[2];
  1005. monthCardTimeLength = DateTime.Parse(monthCardTime) - DateTime.Now;
  1006. if (monthCardTimeLength.TotalHours <= 0)
  1007. {
  1008. monthCardType = 0;
  1009. }
  1010. Log.WriteLog(LogType.process, LogFile.INFO, "已获取本地预约与月卡信息");
  1011. }
  1012. }
  1013. catch (Exception e) { Console.WriteLine(e.Message); result = false; }
  1014. try
  1015. {
  1016. if (reader != null)
  1017. {
  1018. reader.Close();
  1019. reader.Dispose();
  1020. }
  1021. }
  1022. catch (Exception e) { Console.WriteLine(e.Message); result = false; }
  1023. //云端VIP信息
  1024. try
  1025. {
  1026. reader = Monitor.Monitor.remoteDBOper.Query(vipInfoSql);
  1027. if (reader != null && reader.Read())
  1028. {
  1029. object[] receiver = new object[3];
  1030. reader.GetValues(receiver);
  1031. int cardType = (int)(UInt32)receiver[1];
  1032. string CardTime = (string)receiver[2];
  1033. TimeSpan t = DateTime.Parse(CardTime) - DateTime.Now;
  1034. //云端
  1035. if (t.TotalHours > 0 && cardType > monthCardType)
  1036. {
  1037. monthCardType = cardType;
  1038. monthCardTimeLength = t;
  1039. }
  1040. Log.WriteLog(LogType.process, LogFile.INFO, "已获取云端预约与月卡信息");
  1041. }
  1042. }
  1043. catch (Exception e) { Console.WriteLine(e.Message); result = false; }
  1044. try
  1045. {
  1046. if (reader != null)
  1047. {
  1048. reader.Close();
  1049. reader.Dispose();
  1050. }
  1051. }
  1052. catch (Exception e) { Console.WriteLine(e.Message); result = false; }
  1053. //有预约记录,判断记录有效性,有效则获取预约时长
  1054. if (orderRecordsID != 0)
  1055. {
  1056. //修改orderTimeLength
  1057. string orderInfoSql = "select bookHour from orderrecords where orderRecordsID = '" + orderRecordsID + "';";
  1058. try
  1059. {
  1060. reader = Monitor.Monitor.localDBOper.Query(vipInfoSql);
  1061. if (reader != null && reader.Read())
  1062. {
  1063. orderTimeLength = new TimeSpan(reader.GetInt32("bookHour"), 0, 0);
  1064. }
  1065. }
  1066. catch { }
  1067. }
  1068. try
  1069. {
  1070. if (reader != null)
  1071. {
  1072. reader.Close();
  1073. reader.Dispose();
  1074. }
  1075. }
  1076. catch (Exception e) { Console.WriteLine(e.Message); }
  1077. return result;
  1078. }
  1079. /// <summary>
  1080. /// 预约与停车费用计算
  1081. /// </summary>
  1082. /// <param name="scheme"></param>
  1083. /// <param name="parkTime"></param>
  1084. /// <param name="orderTimeLength"></param>
  1085. /// <returns></returns>
  1086. private static int FeeCalc(PaymentScheme scheme, DateTime parkTime, TimeSpan orderTimeLength)
  1087. {
  1088. try
  1089. {
  1090. int orderFee = scheme.bookCharge * (int)orderTimeLength.TotalHours;
  1091. int parkFee = 0;
  1092. int hours = (int)(DateTime.Now - parkTime).TotalHours;
  1093. switch (scheme.schemeType)
  1094. {
  1095. //按时计费
  1096. case 1:
  1097. if (hours > scheme.firstChargeTime + scheme.freeTime)
  1098. {
  1099. if (hours > 24)
  1100. {
  1101. parkFee = hours / 24 * scheme.upperBound + Math.Min(scheme.intervalCharge * (hours % 24) / scheme.chargeInterval, scheme.upperBound);
  1102. }
  1103. else
  1104. {
  1105. parkFee = Math.Min(scheme.firstCharge + scheme.intervalCharge * hours / scheme.chargeInterval, scheme.upperBound);
  1106. }
  1107. }
  1108. else if (hours > scheme.freeTime)
  1109. {
  1110. parkFee = scheme.firstCharge;
  1111. }
  1112. else
  1113. {
  1114. parkFee = 0;
  1115. }
  1116. break;
  1117. //按次计费
  1118. case 2:
  1119. //Console.WriteLine(DateTime.Now.Date.ToString()+","+ parkTime.Date.ToString());
  1120. int overnightCount = (DateTime.Now.Date - parkTime.Date).Days;
  1121. parkFee = scheme.eachCharge + overnightCount * scheme.overnightCharge;
  1122. break;
  1123. //按时间段计费
  1124. case 3:
  1125. TimeSpan currentTimeOfDay = DateTime.Now.TimeOfDay;
  1126. TimeSpan parkTimeOfDay = parkTime.TimeOfDay;
  1127. TimeSpan startTimeOfDay = DateTime.Parse(scheme.startChargeTime).TimeOfDay;
  1128. TimeSpan endTimeOfDay = DateTime.Parse(scheme.endChargeTime).TimeOfDay;
  1129. double countingHoursOfDay = endTimeOfDay.TotalHours - startTimeOfDay.TotalHours;
  1130. int days = (int)((DateTime.Now - parkTime).TotalHours - currentTimeOfDay.TotalHours + parkTimeOfDay.TotalHours);
  1131. int previousOffset = (int)(-(Math.Max(0, parkTimeOfDay.TotalHours - startTimeOfDay.TotalHours)));
  1132. int currentOffset = (int)Math.Min((currentTimeOfDay.TotalHours - startTimeOfDay.TotalHours), countingHoursOfDay);
  1133. int peakTimeLength = days * (int)countingHoursOfDay + previousOffset + currentOffset;
  1134. parkFee = peakTimeLength * scheme.chargeStandard + ((int)((DateTime.Now - parkTime).TotalHours) - peakTimeLength) * scheme.intervalCharge;
  1135. break;
  1136. }
  1137. Log.WriteLog(LogType.process, LogFile.INFO, "停车费用" + parkFee);
  1138. return parkFee;
  1139. }
  1140. catch (Exception e) { Console.WriteLine("费用计算异常"); return -1; }
  1141. }
  1142. /// <summary>
  1143. /// 取车计费子操作
  1144. /// </summary>
  1145. private static void FetchFeeCalcSubProcess()
  1146. {
  1147. int receiptNum = 0;
  1148. int termIndex = -1;
  1149. bool failed = false;
  1150. TerminalStru ts = new TerminalStru();
  1151. for (int i = 0; i < terminalInfo.Count; i++)
  1152. {
  1153. ts = terminalInfo[i];
  1154. termIndex = ts.terminalID;
  1155. receiptNum = ts.receiptNum;
  1156. //取车状态、凭证号不为空,查询数据库计费
  1157. //Console.WriteLine((termIndex != -1) + "," + (termUsedMap.Count >= termIndex) + "," + termUsedMap.TryGetValue(termIndex, out bool valuee) + "," + (!valuee) + "," + (fetchState == 1) + "," + (receiptNum != 0));
  1158. bool value = false;
  1159. if (termIndex != -1 && termCalcMap.Count >= termIndex && termCalcMap.TryGetValue(termIndex, out value) && !value && ts.cmd == 2 && receiptNum != 0)
  1160. {
  1161. if (!Monitor.Monitor.globalStatus)
  1162. {
  1163. Monitor.Monitor.SetNotification("取车计费系统已暂停,请检查无误后手动开启", parkMonitor.model.TextColor.Warning);
  1164. Thread.Sleep(5000);
  1165. return;
  1166. }
  1167. //fetchState = Monitor.Monitor.mainBlockInfo.fetchingRunning;
  1168. Monitor.Monitor.SetNotification(termIndex + "号终端准备计算费用", parkMonitor.model.TextColor.Log);
  1169. termCalcMap[termIndex] = true;
  1170. //Console.WriteLine("终端编号" + termIndex);
  1171. int parkingRecordsID = 0;
  1172. int remoteParkingRecordsID = 0;
  1173. int userID = 0;
  1174. string license = "";
  1175. string realParkTime = "";
  1176. //1.根据凭证号查询停车记录
  1177. if (!FindLocalParkingRecord(receiptNum, out parkingRecordsID, out userID, out license, out realParkTime))
  1178. {
  1179. failed = true;
  1180. //return;
  1181. }
  1182. else
  1183. {
  1184. FindRemoteParkingRecord(receiptNum, out remoteParkingRecordsID);
  1185. }
  1186. //本地存在号牌,后续操作
  1187. //2.根据号牌查询车辆月卡信息与预约记录
  1188. int orderRecordsID = 0;
  1189. int monthCardType = -1;
  1190. string monthCardTime = "";
  1191. TimeSpan monthCardTimeLength = new TimeSpan(0);
  1192. TimeSpan orderTimeLength = new TimeSpan(0);
  1193. if (license != "")
  1194. {
  1195. FindVipInfo(license, ref orderRecordsID, ref monthCardType, ref monthCardTimeLength, ref orderTimeLength);
  1196. }
  1197. if (monthCardType == -1) monthCardType = 0;
  1198. //3.获取停车时刻,根据用户类型计费发送给PLC,暂认为无预约
  1199. DateTime parkTime = DateTime.Now, currentTime = DateTime.Now;
  1200. try
  1201. {
  1202. parkTime = DateTime.Parse(realParkTime);
  1203. }
  1204. catch { failed = true; }
  1205. int fee = 32767;
  1206. if (failed) { monthCardType = 0; }
  1207. if (PaymentScheme.ins != null)
  1208. {
  1209. if (monthCardTimeLength.TotalHours > 0)
  1210. {
  1211. fee = 0;
  1212. }
  1213. else if (failed)
  1214. {
  1215. fee = 0;
  1216. }
  1217. else
  1218. {
  1219. fee = FeeCalc(PaymentScheme.ins, parkTime, orderTimeLength);
  1220. }
  1221. try
  1222. {
  1223. if (Monitor.Monitor.PLC != null)
  1224. {
  1225. TerminalStru FeeMsg = new TerminalStru();
  1226. FeeMsg.terminalID = (short)termIndex;
  1227. FeeMsg.paymentStatus = (short)-1;
  1228. FeeMsg.licVerification = (short)-1;
  1229. FeeMsg.parkingFee = (short)fee;
  1230. FeeMsg.userType = (short)(monthCardType + 1);
  1231. Console.WriteLine(FeeMsg);
  1232. Monitor.Monitor.PLC.WriteToPLC(FeeMsg, PLCDataType.central);
  1233. Log.WriteLog(LogType.process, LogFile.INFO, "停车费用已发送至PLC");
  1234. }
  1235. }
  1236. catch { Console.WriteLine("error"); }
  1237. }
  1238. Monitor.Monitor.SetNotification("用户类型:" + (monthCardType + 1) + ",费用:" + fee + ",等待凭证号被清除且流程结束后更新车辆状态", parkMonitor.model.TextColor.Log);
  1239. FetchInfo fetchInfo = new FetchInfo(license, parkingRecordsID, remoteParkingRecordsID, fee, receiptNum);
  1240. if (!fetchInfoMap.ContainsKey(termIndex))
  1241. {
  1242. fetchInfoMap.Add(termIndex, fetchInfo);
  1243. fetchCompleteMap.Add(termIndex, true);
  1244. }
  1245. else
  1246. {
  1247. fetchInfoMap[termIndex] = fetchInfo;
  1248. fetchCompleteMap[termIndex] = true;
  1249. }
  1250. }
  1251. }
  1252. }
  1253. private static void FetchCompleteProcess()
  1254. {
  1255. int fetchState = Monitor.Monitor.mainBlockInfo.fetchingRunning;
  1256. int termIndex = -1;
  1257. try
  1258. {
  1259. if (fetchState == 1)
  1260. {
  1261. //找到当前取车终端
  1262. for (int i = 0; i < terminalInfo.Count; i++)
  1263. {
  1264. if (terminalInfo[i].terminalID == Monitor.Monitor.mainBlockInfo.terminalID && Monitor.Monitor.mainBlockInfo.terminalID != 0)
  1265. {
  1266. termIndex = i;
  1267. break;
  1268. }
  1269. }
  1270. if (fetchCompleteMap.ContainsKey(terminalInfo[termIndex].terminalID) && fetchCompleteMap[terminalInfo[termIndex].terminalID])
  1271. {
  1272. fetchCompleteMap[terminalInfo[termIndex].terminalID] = false;
  1273. Task.Factory.StartNew(() =>
  1274. {
  1275. //4.等待终端获得收费完成信号且将凭证号清除后,清除收费完成信号并将信息写入数据库
  1276. int count = 0;
  1277. while (!isClosing && termIndex != -1)
  1278. {
  1279. if (Monitor.Monitor.mainBlockInfo.processStopped == 1 || !Monitor.Monitor.globalStatus)
  1280. {
  1281. Log.WriteLog(LogType.process, LogFile.INFO, "取车流程中断");
  1282. Monitor.Monitor.SetNotification("取车流程中断", parkMonitor.model.TextColor.Warning);
  1283. ClearTerminal(terminalInfo[termIndex].terminalID, false);
  1284. //停车流程确保已清除中控块完成信号
  1285. Thread.Sleep(500);
  1286. ResetCompleteSignal();
  1287. break;
  1288. }
  1289. if (Monitor.Monitor.mainBlockInfo.processCompleted == 1)
  1290. {
  1291. UpdateSpecificParkingSpace();
  1292. //while (terminalInfo[termIndex].receiptNum != 0)
  1293. //{
  1294. // count++;
  1295. // if (count == 1)
  1296. // Monitor.Monitor.SetNotification("等待凭证号清除", parkMonitor.model.TextColor.Log);
  1297. // if (count > 10000)//避免int型数据溢出
  1298. // count = 2;
  1299. // Thread.Sleep(200);
  1300. //}
  1301. int paymentStatus = 0;
  1302. for (int i = 0; i < terminalInfo.Count; i++)
  1303. {
  1304. if (terminalInfo[i].terminalID == Monitor.Monitor.mainBlockInfo.terminalID)
  1305. {
  1306. paymentStatus = terminalInfo[i].paymentStatus;
  1307. break;
  1308. }
  1309. }
  1310. //Log.WriteLog(LogType.process, LogFile.INFO, "检测到凭证号已被清除");
  1311. FetchInfo fi = null;
  1312. UpdateAllParkingSpace(false);
  1313. ClearTerminal(terminalInfo[termIndex].terminalID, false);
  1314. //停车流程确保已清除中控块完成信号
  1315. Thread.Sleep(500);
  1316. ResetCompleteSignal();
  1317. if (fetchInfoMap.ContainsKey(terminalInfo[termIndex].terminalID))
  1318. {
  1319. fi = fetchInfoMap[terminalInfo[termIndex].terminalID];
  1320. }
  1321. else
  1322. {
  1323. Monitor.Monitor.SetNotification("未查询到该取车终端存在计费操作", parkMonitor.model.TextColor.Error);
  1324. Log.WriteLog(LogType.process, LogFile.ERROR, "终端" + terminalInfo[termIndex].terminalID + "无计费操作");
  1325. return;
  1326. }
  1327. //TerminalStru fetchReset = new TerminalStru
  1328. //{
  1329. // terminalID = (short)termIndex,
  1330. // paymentStatus = (short)0,
  1331. // licVerification = (short)0,
  1332. // parkingFee = (short)32767,
  1333. // userType = (short)0
  1334. //};
  1335. //Monitor.Monitor.PLC.WriteToPLC(fetchReset, PLCDataType.central);
  1336. //更新停车记录与车辆状态
  1337. string updateParkingRecordsSql = "update parkingrecords set parkingRecordsState = 6,realGetTime = '" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") +
  1338. "',parkingPrice = " + fi.fee + ",paymentStatus = " + paymentStatus + " where receiptNum = '" + fi.receiptNum + "';";
  1339. List<string> list = new List<string>();
  1340. list.Add(updateParkingRecordsSql);
  1341. lock (Monitor.Monitor.localDBOper)
  1342. {
  1343. Monitor.Monitor.localDBOper.UpdateTransaction(list);
  1344. UpdateVehicle(fi.license, 0, fi.parkingRecordsID, false, false, 0);
  1345. }
  1346. lock (Monitor.Monitor.remoteDBOper)
  1347. {
  1348. Monitor.Monitor.remoteDBOper.UpdateTransaction(list);
  1349. UpdateVehicle(fi.license, 0, fi.remoteParkingRecordsID, false, true, 0);
  1350. }
  1351. Monitor.Monitor.SetNotification(fi.license + " 取车流程结束", parkMonitor.model.TextColor.Info);
  1352. break;
  1353. }
  1354. Thread.Sleep(200);
  1355. }
  1356. termCalcMap[terminalInfo[termIndex].terminalID] = false;
  1357. fetchInfoMap.Remove(terminalInfo[termIndex].terminalID);
  1358. fetchCompleteMap.Remove(terminalInfo[termIndex].terminalID);
  1359. });
  1360. }
  1361. }
  1362. }
  1363. catch (Exception ex) { Log.WriteLog(LogType.process, LogFile.ERROR, "取车完成流程异常," + ex.StackTrace); }
  1364. }
  1365. /// <summary>
  1366. /// 取车相关操作
  1367. ///
  1368. /// </summary>
  1369. private static void FetchProcess()
  1370. {
  1371. Task.Factory.StartNew(() =>
  1372. {
  1373. while (!isClosing)
  1374. {
  1375. FetchFeeCalcSubProcess();
  1376. Thread.Sleep(Monitor.Monitor.plcRefreshInterval);
  1377. }
  1378. });
  1379. Task.Factory.StartNew(() =>
  1380. {
  1381. while (!isClosing)
  1382. {
  1383. FetchCompleteProcess();
  1384. Thread.Sleep(Monitor.Monitor.plcRefreshInterval);
  1385. }
  1386. });
  1387. }
  1388. public static void Start()
  1389. {
  1390. for (int i = 0; i < Monitor.Monitor.plcTerminalCount; i++)
  1391. {
  1392. lockList.Add(false);
  1393. }
  1394. ResetCompleteSignal();
  1395. ParkProcess();
  1396. FetchProcess();
  1397. //定时更新云端车位,5min
  1398. Task.Factory.StartNew(() =>
  1399. {
  1400. while (!isClosing)
  1401. {
  1402. UpdateAllParkingSpace(true);
  1403. Thread.Sleep(300000);
  1404. }
  1405. });
  1406. }
  1407. public static void Stop()
  1408. {
  1409. isClosing = true;
  1410. }
  1411. public class FetchInfo
  1412. {
  1413. public string license;
  1414. public int parkingRecordsID;
  1415. public int remoteParkingRecordsID;
  1416. public int fee;
  1417. public int receiptNum;
  1418. public FetchInfo(string license, int parkingRecordsID, int remoteParkingRecordsID, int fee, int receiptNum)
  1419. {
  1420. this.license = license;
  1421. this.parkingRecordsID = parkingRecordsID;
  1422. this.remoteParkingRecordsID = remoteParkingRecordsID;
  1423. this.fee = fee;
  1424. this.receiptNum = receiptNum;
  1425. }
  1426. }
  1427. }
  1428. }