Terminal.cs 68 KB

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