Terminal.cs 67 KB

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