Terminal.cs 71 KB

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