Terminal.cs 65 KB

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