Terminal.cs 66 KB

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