Terminal.cs 69 KB

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