Terminal.cs 74 KB

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