Terminal.cs 70 KB

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