Terminal.cs 73 KB

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