Terminal.cs 73 KB

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