Terminal.cs 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444
  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. Log.WriteLog(LogType.process, LogFile.ERROR, "未获得号牌");
  547. }
  548. else
  549. {
  550. TerminalStru ts = new TerminalStru
  551. {
  552. terminalID = (short)term.terminalID,
  553. paymentStatus = -1,
  554. parkingFee = -1,
  555. userType = -1
  556. };
  557. //注册用户
  558. if (term.btnStatus == (short)2 || term.btnStatus == (short)3)
  559. {
  560. int userID = term.licenseCodeA;
  561. string userLicense = GetLicenseFromTerm(term);
  562. //注册用户缺ID信息,转为普通用户停车,终端将限制该情况发生
  563. if (userID == 0)
  564. {
  565. term.btnStatus = 1;
  566. }
  567. else
  568. {
  569. //与云端数据比对
  570. ValidateUserInfo(ts, userID, userLicense, out numReceivedStatus);
  571. if (numReceivedStatus == 2)
  572. {
  573. idLicMap.Remove(term.terminalID);
  574. }
  575. }
  576. }
  577. //非注册用户
  578. if (term.btnStatus == (short)1 || term.btnStatus == (short)4)
  579. {
  580. numReceivedStatus = 1;
  581. ts.licVerification = 1;
  582. Monitor.Monitor.PLC.WriteToPLC(ts, PLCDataType.central);
  583. }
  584. //无论是否注册,皆告知PLC,已获取号牌或比对异常、终止流程
  585. MainBlockStru mbst = new MainBlockStru
  586. {
  587. centralHearbeat = -1,
  588. bookFetchCmd = -1,
  589. bookParkCmd = -1,
  590. processCompleted = (short)-1,
  591. licenseReceived = (short)numReceivedStatus
  592. };
  593. Monitor.Monitor.PLC.WriteToPLC(mbst, PLCDataType.central);
  594. //号牌获取结果被清零才跳出
  595. Monitor.Monitor.SetNotification("已写入号牌获取结果,等待号牌机启动指令清零", parkMonitor.model.TextColor.Info);
  596. }
  597. int countdown = 10;
  598. while (countdown-- > 0)
  599. {
  600. if (terminalInfo[i].numMachineLaunch == 0)
  601. {
  602. Monitor.Monitor.SetNotification("号牌机启动指令已被清零", parkMonitor.model.TextColor.Info); break;
  603. }
  604. else
  605. Thread.Sleep(500);
  606. }
  607. if (countdown == 0)
  608. Monitor.Monitor.SetNotification("提醒:未检测到号牌机启动指令被清零", parkMonitor.model.TextColor.Warning);
  609. }
  610. }
  611. }
  612. }
  613. #endregion
  614. /// <summary>
  615. /// 停车流程,收到PLC停车完成信号后操作过程
  616. /// </summary>
  617. private static void ParkCompleteSubProcess()
  618. {
  619. //Console.WriteLine(Monitor.Monitor.mainBlockInfo.parkingRunning+","+ Monitor.Monitor.mainBlockInfo.processCompleted);
  620. int processAttrib = Monitor.Monitor.mainBlockInfo.parkingRunning;
  621. //int processCompleted = Monitor.Monitor.mainBlockInfo.processCompleted;
  622. int currentTerm = Monitor.Monitor.mainBlockInfo.terminalID;
  623. int parkingSpaceID = 0;
  624. if (processAttrib == 1 && Monitor.Monitor.globalStatus)
  625. {
  626. TerminalStru term = new TerminalStru();
  627. string license;
  628. //等待完成信号
  629. while (true)
  630. {
  631. if (Monitor.Monitor.mainBlockInfo.processStopped == 1 || !Monitor.Monitor.globalStatus)
  632. {
  633. Log.WriteLog(LogType.process, LogFile.ERROR, "停车流程中断");
  634. Monitor.Monitor.SetNotification("停车流程中断", parkMonitor.model.TextColor.Warning);
  635. Thread.Sleep(2000);
  636. ClearTerminal(term.terminalID,false);
  637. ResetCompleteSignal();
  638. //流程终端清除记录的号牌
  639. try { idLicMap.Remove(currentTerm); }
  640. catch { }
  641. return;
  642. }
  643. if (Monitor.Monitor.mainBlockInfo.processCompleted == 1)
  644. {
  645. if (!idLicMap.ContainsKey(currentTerm))
  646. {
  647. Log.WriteLog(LogType.process, LogFile.ERROR, "停车结束时未找到号牌");
  648. }
  649. break;
  650. }
  651. Thread.Sleep(200);
  652. }
  653. //找到终端号,判断是否注册用户
  654. //拿到号牌
  655. if (/*terminalInfo[i].terminalID == currentTerm && */idLicMap.TryGetValue(currentTerm, out license) && license != "")
  656. {
  657. UpdateSpecificParkingSpace();
  658. for (int i = 0; i < terminalInfo.Count; i++)
  659. {
  660. if (terminalInfo[i].terminalID == currentTerm)
  661. term = terminalInfo[i];
  662. }
  663. //term = terminalInfo[currentTerm - 1];
  664. Thread.Sleep(500);
  665. parkingSpaceID = Monitor.Monitor.mainBlockInfo.currentParkingSpaceID;
  666. int receipt = 0;
  667. idReceiptMap.TryGetValue(term.terminalID, out receipt);
  668. //无车位信息则跳出当前循环
  669. if (parkingSpaceID == 0 || receipt == 0)
  670. {
  671. try
  672. {
  673. idLicMap.Remove(currentTerm);
  674. ClearTerminal(term.terminalID,false);
  675. }
  676. catch { }
  677. Monitor.Monitor.SetNotification("无车位或凭证号,车位:" + parkingSpaceID + ",凭证号:" + receipt, parkMonitor.model.TextColor.Warning);
  678. return;
  679. }
  680. UpdateVehicle(license, 0, 0, true, false, 0);
  681. UpdateVehicle(license, 0, 0, true, true, 0);
  682. //插入停车记录
  683. string parkingRecordsSql = "";
  684. if (term.licenseCodeA != 0)
  685. {
  686. parkingRecordsSql = "INSERT INTO parkingrecords (userID, numberPlate,parkingSpaceID,garageID,parkingRecordsState,realParkTime,receiptNum,parkingPrice)" +
  687. "values ('" + term.licenseCodeA + "','" + license + "','" + parkingSpaceID + "','" + Monitor.Monitor.garageID + "',3,'" + DateTime.Now.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss") + "','" + receipt + "',NULL);";
  688. }
  689. else
  690. {
  691. parkingRecordsSql = "INSERT INTO parkingrecords (userID, numberPlate,parkingSpaceID,garageID,parkingRecordsState,realParkTime,receiptNum,parkingPrice)" +
  692. "values ('" + 1 + "','" + license + "','" + parkingSpaceID + "','" + Monitor.Monitor.garageID + "',3,'" + DateTime.Now.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss") + "','" + receipt + "',NULL);";
  693. }
  694. List<string> list = new List<string>();
  695. list.Add(parkingRecordsSql);
  696. int parkingRecordsID = 0;
  697. int remoteParkingRecordsID = 0;
  698. Monitor.Monitor.localDBOper.Insert(list, out parkingRecordsID);
  699. Monitor.Monitor.remoteDBOper.Insert(list, out remoteParkingRecordsID);
  700. Log.WriteLog(LogType.process, LogFile.INFO, "停车记录已插入," + license + "停在" + parkingSpaceID + ",凭证号:" + receipt);
  701. Monitor.Monitor.SetNotification("停车记录已插入", parkMonitor.model.TextColor.Info);
  702. //查询记录ID号
  703. //string findRecordSql = "select parkingRecordsID from parkingrecords where receiptNum = " + term.receiptNum + ";";
  704. //QueryParkingRecordsID(findRecordSql, out parkingRecordsID, false);
  705. //QueryParkingRecordsID(findRecordSql, out remoteParkingRecordsID, true);
  706. //更新车辆表
  707. if (parkingRecordsID != 0)
  708. {
  709. UpdateVehicle(license, 1, parkingRecordsID, true, false, parkingSpaceID);
  710. }
  711. if (remoteParkingRecordsID != 0)
  712. {
  713. UpdateVehicle(license, 1, remoteParkingRecordsID, true, true, parkingSpaceID);
  714. }
  715. Log.WriteLog(LogType.process, LogFile.INFO, "停车记录表已更新");
  716. //更新车位
  717. UpdateAllParkingSpace(false);
  718. //Monitor.Monitor.AddNotification("停车数据库已更新,等待流程结束状态清零。");
  719. //while (Monitor.Monitor.mainBlockInfo.processCompleted != 0)
  720. //{
  721. // Thread.Sleep(1000);
  722. //}
  723. //中控清除车牌、凭证号、号牌验证等信息
  724. ClearTerminal(term.terminalID,false);
  725. //停车流程确保已清除中控块完成信号
  726. Thread.Sleep(2000);
  727. ResetCompleteSignal();
  728. try { idLicMap.Remove(currentTerm); }
  729. catch { }
  730. Monitor.Monitor.SetNotification(license + " 数据库已更新,停车流程结束", parkMonitor.model.TextColor.Info);
  731. }
  732. }
  733. }
  734. /// <summary>
  735. /// 预约车辆检测
  736. /// </summary>
  737. private static void ReservedCarCheck()
  738. {
  739. if (lockList.Count != terminalInfo.Count)
  740. {
  741. lockList.Clear();
  742. for (int i = 0; i < terminalInfo.Count; i++)
  743. {
  744. lockList.Add(false);
  745. }
  746. }
  747. //找到停车终端,且有地感无按钮数据
  748. for (int i = 0; i < terminalInfo.Count; i++)
  749. {
  750. //Console.WriteLine(i);
  751. if (terminalInfo[i].terminalStatus == 1 && terminalInfo[i].groundStatus == 1 && terminalInfo[i].btnStatus == 0 && !lockList[i])
  752. {
  753. lockList[i] = true;
  754. Action<int> reserveAction = new Action<int>(ReservedOper);
  755. reserveAction(i);
  756. }
  757. }
  758. }
  759. private static void ReservedOper(int index)
  760. {
  761. Task.Factory.StartNew(() =>
  762. {
  763. string license = "";
  764. int countdown = 2;
  765. short registered = 4;
  766. while (license == "" && countdown-- > 0)
  767. {
  768. license = Monitor.Monitor.numMachineLinker.GetLicensePlate(terminalInfo[index].terminalID);
  769. }
  770. Monitor.Monitor.SetNotification(terminalInfo[index].terminalID + "号车位收到地感,拍到" + license, parkMonitor.model.TextColor.Log);
  771. if (license != "")
  772. {
  773. //map中加入或更新号牌,供完成时写入数据库用
  774. if (idLicMap.ContainsKey(terminalInfo[index].terminalID))
  775. {
  776. idLicMap[terminalInfo[index].terminalID] = license;
  777. }
  778. else
  779. {
  780. idLicMap.Add(terminalInfo[index].terminalID, license);
  781. }
  782. if (Monitor.Monitor.webServer.ReservedCarCheck(license))
  783. {
  784. registered = 3;
  785. Monitor.Monitor.SetNotification("预约车辆入场", parkMonitor.model.TextColor.Info);
  786. }
  787. #region 通过数据库查询是否预约车,方法待定
  788. //int vehicleState = -1;
  789. //string vehicleStateCheckSql = "select vehiclepParkState from vehicle where numberPlate = '" + license + "';";
  790. //MySqlDataReader reader = Monitor.Monitor.localDBOper.Query(vehicleStateCheckSql);
  791. //if (reader != null)
  792. //{
  793. // try
  794. // {
  795. // if (reader.Read())
  796. // {
  797. // vehicleState = reader.GetInt32("vehiclepParkState");
  798. // }
  799. // }
  800. // catch (Exception e) { Log.WriteLog(LogType.process, LogFile.ERROR, "预约检测,查询车辆状态异常"); ; Console.WriteLine(e.Message); };
  801. // try
  802. // {
  803. // reader.Close();
  804. // reader.Dispose();
  805. // }
  806. // catch { }
  807. //}
  808. //已预约车辆入场
  809. //if (vehicleState == 4)
  810. //{
  811. // registered = 3;
  812. //}
  813. #endregion
  814. TerminalStru ts = new TerminalStru
  815. {
  816. terminalID = terminalInfo[index].terminalID,
  817. paymentStatus = (short)-1,
  818. btnStatus = registered,
  819. cmd = (short)-1,
  820. licenseCodeA = -1,
  821. licenseCodeB = -1,
  822. licenseCodeC = -1,
  823. licenseCodeD = -1,
  824. receiptNum = -1
  825. };
  826. Monitor.Monitor.PLC.WriteToPLC(ts, PLCDataType.terminal);
  827. Monitor.Monitor.SetNotification("指令写入PLC:" + ts.btnStatus, parkMonitor.model.TextColor.Log);
  828. Thread.Sleep(1000);
  829. }
  830. else
  831. {
  832. Thread.Sleep(200);
  833. }
  834. lockList[index] = false;
  835. });
  836. }
  837. /// <summary>
  838. /// 停车相关操作
  839. /// 0.观测到地感信号,且按钮状态不为3,则启动号牌机获取号牌,并判断是否预约车
  840. /// 1.读取到号牌机启动指令,启动号牌机并根据是否注册用户进行操作
  841. /// 非注册拿到号牌后记录并告知plc
  842. /// 注册用户,核对号牌与用户关联信息
  843. /// 2.停车流程结束,写数据库
  844. /// </summary>
  845. ///
  846. private static void ParkProcess()
  847. {
  848. Task.Factory.StartNew(() =>
  849. {
  850. while (!isClosing)
  851. {
  852. ParkNumSubProcess();
  853. Thread.Sleep(Monitor.Monitor.plcRefreshInterval * 5);
  854. }
  855. });
  856. Task.Factory.StartNew(() =>
  857. {
  858. while (!isClosing)
  859. {
  860. ParkCompleteSubProcess();
  861. Thread.Sleep(Monitor.Monitor.plcRefreshInterval * 5);
  862. }
  863. });
  864. Task.Factory.StartNew(() =>
  865. {
  866. while (!isClosing)
  867. {
  868. ReservedCarCheck();
  869. Thread.Sleep(Monitor.Monitor.plcRefreshInterval * 5);
  870. }
  871. });
  872. }
  873. /// <summary>
  874. /// 根据凭证号查询停车记录
  875. /// </summary>
  876. /// <param name="receipt"></param>
  877. /// <param name="recordID"></param>
  878. /// <param name="userID"></param>
  879. /// <param name="license"></param>
  880. /// <param name="realParkTime"></param>
  881. private static bool FindLocalParkingRecord(int receipt, out int recordID, out int userID, out string license, out string realParkTime)
  882. {
  883. recordID = 0;
  884. userID = 0;
  885. license = "";
  886. realParkTime = "";
  887. bool result = false;
  888. string parkRecordsSql = "select parkingRecordsID,userID,numberPlate,realParkTime from parkingrecords where receiptNum = " + receipt + ";";
  889. MySqlDataReader reader = Monitor.Monitor.localDBOper.Query(parkRecordsSql);
  890. try
  891. {
  892. if (reader != null && reader.Read())
  893. {
  894. object[] receiver = new object[4];
  895. reader.GetValues(receiver);
  896. recordID = (int)receiver[0];
  897. userID = (int)receiver[1];
  898. license = (string)receiver[2];
  899. realParkTime = (string)receiver[3];
  900. result = true;
  901. }
  902. else
  903. {
  904. Log.WriteLog(LogType.process, LogFile.ERROR, "根据凭证号" + receipt + "查询停车记录失败");
  905. Monitor.Monitor.SetNotification("取车流程, 根据凭证号查询停车记录失败", parkMonitor.model.TextColor.Error);
  906. result = false;
  907. }
  908. //if (reader != null)
  909. //{
  910. // reader.Close();
  911. // reader.Dispose();
  912. //}
  913. }
  914. catch (Exception e) { Log.WriteLog(LogType.process, LogFile.ERROR, "根据凭证号" + receipt + "查询停车记录失败"); Console.WriteLine(e.Message); result = false; }
  915. try
  916. {
  917. if (reader != null)
  918. {
  919. reader.Close();
  920. reader.Dispose();
  921. }
  922. }
  923. catch { }
  924. if (result)
  925. Log.WriteLog(LogType.process, LogFile.INFO, "根据凭证号查询停车记录成功");
  926. return result;
  927. }
  928. private static bool FindRemoteParkingRecord(int receipt, out int recordID)
  929. {
  930. recordID = 0;
  931. bool result = false;
  932. string remoteParkRecordsSql = "select parkingRecordsID from parkingrecords where receiptNum = " + receipt + ";";
  933. MySqlDataReader reader = Monitor.Monitor.remoteDBOper.Query(remoteParkRecordsSql);
  934. try
  935. {
  936. if (reader != null && reader.Read())
  937. {
  938. object[] receiver = new object[1];
  939. reader.GetValues(receiver);
  940. recordID = (int)receiver[0];
  941. result = true;
  942. }
  943. }
  944. catch (Exception e) { Console.WriteLine(e.Message); result = false; }
  945. try
  946. {
  947. if (reader != null)
  948. {
  949. reader.Close();
  950. reader.Dispose();
  951. }
  952. }
  953. catch (Exception e) { Console.WriteLine(e.Message); result = false; }
  954. return result;
  955. }
  956. /// <summary>
  957. /// 根据号牌获取本地与远端用户VIP月卡及预约信息
  958. /// </summary>
  959. /// <param name="license"></param>
  960. /// <param name="orderRecordsID"></param>
  961. /// <param name="monthCardType"></param>
  962. /// <param name="monthCardTimeLength"></param>
  963. /// <param name="orderTimeLength"></param>
  964. /// <returns></returns>
  965. private static bool FindVipInfo(string license, ref int orderRecordsID, ref int monthCardType, ref TimeSpan monthCardTimeLength, ref TimeSpan orderTimeLength)
  966. {
  967. bool result = true;
  968. string monthCardTime = "";
  969. //本地VIP信息
  970. string vipInfoSql = "select orderRecordsID,monthCardType,monthCardTime from vehicle where numberPlate = '" + license + "';";
  971. MySqlDataReader reader = null;
  972. try
  973. {
  974. reader = Monitor.Monitor.localDBOper.Query(vipInfoSql);
  975. if (reader != null && reader.Read())
  976. {
  977. object[] receiver = new object[3];
  978. reader.GetValues(receiver);
  979. Console.WriteLine(receiver[2].GetType());
  980. if (!receiver[0].GetType().Equals(typeof(DBNull)))
  981. {
  982. orderRecordsID = (int)receiver[0];
  983. }
  984. if (!receiver[1].GetType().Equals(typeof(DBNull)))
  985. {
  986. monthCardType = (int)(UInt32)receiver[1];
  987. }
  988. if (!receiver[2].GetType().Equals(typeof(DBNull)))
  989. {
  990. monthCardTime = (string)receiver[2];
  991. }
  992. //orderRecordsID = (Int32)receiver[0];
  993. //monthCardType = (Int32)receiver[1];
  994. //monthCardTime = (string)receiver[2];
  995. monthCardTimeLength = DateTime.Parse(monthCardTime) - DateTime.Now;
  996. if (monthCardTimeLength.TotalHours <= 0)
  997. {
  998. monthCardType = 0;
  999. }
  1000. Log.WriteLog(LogType.process, LogFile.INFO, "已获取本地预约与月卡信息");
  1001. }
  1002. }
  1003. catch (Exception e) { Console.WriteLine(e.Message); result = false; }
  1004. try
  1005. {
  1006. if (reader != null)
  1007. {
  1008. reader.Close();
  1009. reader.Dispose();
  1010. }
  1011. }
  1012. catch (Exception e) { Console.WriteLine(e.Message); result = false; }
  1013. //云端VIP信息
  1014. try
  1015. {
  1016. reader = Monitor.Monitor.remoteDBOper.Query(vipInfoSql);
  1017. if (reader != null && reader.Read())
  1018. {
  1019. object[] receiver = new object[3];
  1020. reader.GetValues(receiver);
  1021. int cardType = (int)(UInt32)receiver[1];
  1022. string CardTime = (string)receiver[2];
  1023. TimeSpan t = DateTime.Parse(CardTime) - DateTime.Now;
  1024. //云端
  1025. if (t.TotalHours > 0 && cardType > monthCardType)
  1026. {
  1027. monthCardType = cardType;
  1028. monthCardTimeLength = t;
  1029. }
  1030. Log.WriteLog(LogType.process, LogFile.INFO, "已获取云端预约与月卡信息");
  1031. }
  1032. }
  1033. catch (Exception e) { Console.WriteLine(e.Message); result = false; }
  1034. try
  1035. {
  1036. if (reader != null)
  1037. {
  1038. reader.Close();
  1039. reader.Dispose();
  1040. }
  1041. }
  1042. catch (Exception e) { Console.WriteLine(e.Message); result = false; }
  1043. //有预约记录,判断记录有效性,有效则获取预约时长
  1044. if (orderRecordsID != 0)
  1045. {
  1046. //修改orderTimeLength
  1047. string orderInfoSql = "select bookHour from orderrecords where orderRecordsID = '" + orderRecordsID + "';";
  1048. try
  1049. {
  1050. reader = Monitor.Monitor.localDBOper.Query(vipInfoSql);
  1051. if (reader != null && reader.Read())
  1052. {
  1053. orderTimeLength = new TimeSpan(reader.GetInt32("bookHour"), 0, 0);
  1054. }
  1055. }
  1056. catch { }
  1057. }
  1058. try
  1059. {
  1060. if (reader != null)
  1061. {
  1062. reader.Close();
  1063. reader.Dispose();
  1064. }
  1065. }
  1066. catch (Exception e) { Console.WriteLine(e.Message); }
  1067. return result;
  1068. }
  1069. /// <summary>
  1070. /// 预约与停车费用计算
  1071. /// </summary>
  1072. /// <param name="scheme"></param>
  1073. /// <param name="parkTime"></param>
  1074. /// <param name="orderTimeLength"></param>
  1075. /// <returns></returns>
  1076. private static int FeeCalc(PaymentScheme scheme, DateTime parkTime, TimeSpan orderTimeLength)
  1077. {
  1078. try
  1079. {
  1080. int orderFee = scheme.bookCharge * (int)orderTimeLength.TotalHours;
  1081. int parkFee = 0;
  1082. int hours = (int)(DateTime.Now - parkTime).TotalHours;
  1083. switch (scheme.schemeType)
  1084. {
  1085. //按时计费
  1086. case 1:
  1087. if (hours > scheme.firstChargeTime + scheme.freeTime)
  1088. {
  1089. if (hours > 24)
  1090. {
  1091. parkFee = hours / 24 * scheme.upperBound + Math.Min(scheme.intervalCharge * (hours % 24) / scheme.chargeInterval, scheme.upperBound);
  1092. }
  1093. else
  1094. {
  1095. parkFee = Math.Min(scheme.firstCharge + scheme.intervalCharge * hours / scheme.chargeInterval, scheme.upperBound);
  1096. }
  1097. }
  1098. else if (hours > scheme.freeTime)
  1099. {
  1100. parkFee = scheme.firstCharge;
  1101. }
  1102. else
  1103. {
  1104. parkFee = 0;
  1105. }
  1106. break;
  1107. //按次计费
  1108. case 2:
  1109. //Console.WriteLine(DateTime.Now.Date.ToString()+","+ parkTime.Date.ToString());
  1110. int overnightCount = (DateTime.Now.Date - parkTime.Date).Days;
  1111. parkFee = scheme.eachCharge + overnightCount * scheme.overnightCharge;
  1112. break;
  1113. //按时间段计费
  1114. case 3:
  1115. TimeSpan currentTimeOfDay = DateTime.Now.TimeOfDay;
  1116. TimeSpan parkTimeOfDay = parkTime.TimeOfDay;
  1117. TimeSpan startTimeOfDay = DateTime.Parse(scheme.startChargeTime).TimeOfDay;
  1118. TimeSpan endTimeOfDay = DateTime.Parse(scheme.endChargeTime).TimeOfDay;
  1119. double countingHoursOfDay = endTimeOfDay.TotalHours - startTimeOfDay.TotalHours;
  1120. int days = (int)((DateTime.Now - parkTime).TotalHours - currentTimeOfDay.TotalHours + parkTimeOfDay.TotalHours);
  1121. int previousOffset = (int)(-(Math.Max(0, parkTimeOfDay.TotalHours - startTimeOfDay.TotalHours)));
  1122. int currentOffset = (int)Math.Min((currentTimeOfDay.TotalHours - startTimeOfDay.TotalHours), countingHoursOfDay);
  1123. int peakTimeLength = days * (int)countingHoursOfDay + previousOffset + currentOffset;
  1124. parkFee = peakTimeLength * scheme.chargeStandard + ((int)((DateTime.Now - parkTime).TotalHours) - peakTimeLength) * scheme.intervalCharge;
  1125. break;
  1126. }
  1127. Log.WriteLog(LogType.process, LogFile.INFO, "停车费用" + parkFee);
  1128. return parkFee;
  1129. }
  1130. catch (Exception e) { Console.WriteLine("费用计算异常"); return -1; }
  1131. }
  1132. /// <summary>
  1133. /// 取车计费子操作
  1134. /// </summary>
  1135. private static void FetchFeeCalcSubProcess()
  1136. {
  1137. int receiptNum = 0;
  1138. int termIndex = -1;
  1139. bool failed = false;
  1140. TerminalStru ts = new TerminalStru();
  1141. for (int i = 0; i < terminalInfo.Count; i++)
  1142. {
  1143. ts = terminalInfo[i];
  1144. termIndex = ts.terminalID;
  1145. receiptNum = ts.receiptNum;
  1146. //取车状态、凭证号不为空,查询数据库计费
  1147. //Console.WriteLine((termIndex != -1) + "," + (termUsedMap.Count >= termIndex) + "," + termUsedMap.TryGetValue(termIndex, out bool valuee) + "," + (!valuee) + "," + (fetchState == 1) + "," + (receiptNum != 0));
  1148. bool value = false;
  1149. if (termIndex != -1 && termCalcMap.Count >= termIndex && termCalcMap.TryGetValue(termIndex, out value) && !value && ts.cmd == 2 && receiptNum != 0)
  1150. {
  1151. if (!Monitor.Monitor.globalStatus)
  1152. {
  1153. Monitor.Monitor.SetNotification("取车计费系统已暂停,请检查无误后手动开启", parkMonitor.model.TextColor.Warning);
  1154. Thread.Sleep(5000);
  1155. return;
  1156. }
  1157. //fetchState = Monitor.Monitor.mainBlockInfo.fetchingRunning;
  1158. Monitor.Monitor.SetNotification(termIndex + "号终端准备计算费用", parkMonitor.model.TextColor.Log);
  1159. termCalcMap[termIndex] = true;
  1160. //Console.WriteLine("终端编号" + termIndex);
  1161. int parkingRecordsID = 0;
  1162. int remoteParkingRecordsID = 0;
  1163. int userID = 0;
  1164. string license = "";
  1165. string realParkTime = "";
  1166. //1.根据凭证号查询停车记录
  1167. if (!FindLocalParkingRecord(receiptNum, out parkingRecordsID, out userID, out license, out realParkTime))
  1168. {
  1169. failed = true;
  1170. //return;
  1171. }
  1172. else
  1173. {
  1174. FindRemoteParkingRecord(receiptNum, out remoteParkingRecordsID);
  1175. }
  1176. //本地存在号牌,后续操作
  1177. //2.根据号牌查询车辆月卡信息与预约记录
  1178. int orderRecordsID = 0;
  1179. int monthCardType = -1;
  1180. string monthCardTime = "";
  1181. TimeSpan monthCardTimeLength = new TimeSpan(0);
  1182. TimeSpan orderTimeLength = new TimeSpan(0);
  1183. if (license != "")
  1184. {
  1185. FindVipInfo(license, ref orderRecordsID, ref monthCardType, ref monthCardTimeLength, ref orderTimeLength);
  1186. }
  1187. if (monthCardType == -1) monthCardType = 0;
  1188. //3.获取停车时刻,根据用户类型计费发送给PLC,暂认为无预约
  1189. DateTime parkTime = DateTime.Now, currentTime = DateTime.Now;
  1190. try
  1191. {
  1192. parkTime = DateTime.Parse(realParkTime);
  1193. }
  1194. catch { failed = true; }
  1195. int fee = 32767;
  1196. if (failed) { monthCardType = 0; }
  1197. if (PaymentScheme.ins != null)
  1198. {
  1199. if (monthCardTimeLength.TotalHours > 0)
  1200. {
  1201. fee = 0;
  1202. }
  1203. else if (failed)
  1204. {
  1205. fee = 0;
  1206. }
  1207. else
  1208. {
  1209. fee = FeeCalc(PaymentScheme.ins, parkTime, orderTimeLength);
  1210. }
  1211. try
  1212. {
  1213. if (Monitor.Monitor.PLC != null)
  1214. {
  1215. TerminalStru FeeMsg = new TerminalStru();
  1216. FeeMsg.terminalID = (short)termIndex;
  1217. FeeMsg.paymentStatus = (short)-1;
  1218. FeeMsg.licVerification = (short)-1;
  1219. FeeMsg.parkingFee = (short)fee;
  1220. FeeMsg.userType = (short)(monthCardType + 1);
  1221. Console.WriteLine(FeeMsg);
  1222. Monitor.Monitor.PLC.WriteToPLC(FeeMsg, PLCDataType.central);
  1223. Log.WriteLog(LogType.process, LogFile.INFO, "停车费用已发送至PLC");
  1224. }
  1225. }
  1226. catch { Console.WriteLine("error"); }
  1227. }
  1228. Monitor.Monitor.SetNotification("用户类型:" + (monthCardType + 1) + ",费用:" + fee + ",等待凭证号被清除且流程结束后更新车辆状态", parkMonitor.model.TextColor.Log);
  1229. FetchInfo fetchInfo = new FetchInfo(license, parkingRecordsID, remoteParkingRecordsID, fee, receiptNum);
  1230. if (!fetchInfoMap.ContainsKey(termIndex))
  1231. {
  1232. fetchInfoMap.Add(termIndex, fetchInfo);
  1233. fetchCompleteMap.Add(termIndex, true);
  1234. }
  1235. else
  1236. {
  1237. fetchInfoMap[termIndex] = fetchInfo;
  1238. fetchCompleteMap[termIndex] = true;
  1239. }
  1240. }
  1241. }
  1242. }
  1243. private static void FetchCompleteProcess()
  1244. {
  1245. int fetchState = Monitor.Monitor.mainBlockInfo.fetchingRunning;
  1246. int termIndex = -1;
  1247. try
  1248. {
  1249. if (fetchState == 1)
  1250. {
  1251. //找到当前取车终端
  1252. for (int i = 0; i < terminalInfo.Count; i++)
  1253. {
  1254. if (terminalInfo[i].terminalID == Monitor.Monitor.mainBlockInfo.terminalID && Monitor.Monitor.mainBlockInfo.terminalID != 0)
  1255. {
  1256. termIndex = i;
  1257. break;
  1258. }
  1259. }
  1260. if (fetchCompleteMap.ContainsKey(terminalInfo[termIndex].terminalID) && fetchCompleteMap[terminalInfo[termIndex].terminalID])
  1261. {
  1262. fetchCompleteMap[terminalInfo[termIndex].terminalID] = false;
  1263. Task.Factory.StartNew(() =>
  1264. {
  1265. //4.等待终端获得收费完成信号且将凭证号清除后,清除收费完成信号并将信息写入数据库
  1266. int count = 0;
  1267. while (!isClosing && termIndex != -1)
  1268. {
  1269. if (Monitor.Monitor.mainBlockInfo.processStopped == 1 || !Monitor.Monitor.globalStatus)
  1270. {
  1271. Log.WriteLog(LogType.process, LogFile.INFO, "取车流程中断");
  1272. Monitor.Monitor.SetNotification("取车流程中断", parkMonitor.model.TextColor.Warning);
  1273. ClearTerminal(terminalInfo[termIndex].terminalID,false);
  1274. //停车流程确保已清除中控块完成信号
  1275. Thread.Sleep(2000);
  1276. ResetCompleteSignal();
  1277. break;
  1278. }
  1279. if (Monitor.Monitor.mainBlockInfo.processCompleted == 1)
  1280. {
  1281. UpdateSpecificParkingSpace();
  1282. //while (terminalInfo[termIndex].receiptNum != 0)
  1283. //{
  1284. // count++;
  1285. // if (count == 1)
  1286. // Monitor.Monitor.SetNotification("等待凭证号清除", parkMonitor.model.TextColor.Log);
  1287. // if (count > 10000)//避免int型数据溢出
  1288. // count = 2;
  1289. // Thread.Sleep(200);
  1290. //}
  1291. int paymentStatus = 0;
  1292. for (int i = 0; i < terminalInfo.Count; i++)
  1293. {
  1294. if (terminalInfo[i].terminalID == Monitor.Monitor.mainBlockInfo.terminalID)
  1295. {
  1296. paymentStatus = terminalInfo[i].paymentStatus;
  1297. break;
  1298. }
  1299. }
  1300. //Log.WriteLog(LogType.process, LogFile.INFO, "检测到凭证号已被清除");
  1301. FetchInfo fi = null;
  1302. UpdateAllParkingSpace(false);
  1303. ClearTerminal(terminalInfo[termIndex].terminalID,false);
  1304. //停车流程确保已清除中控块完成信号
  1305. Thread.Sleep(2000);
  1306. ResetCompleteSignal();
  1307. if (fetchInfoMap.ContainsKey(terminalInfo[termIndex].terminalID))
  1308. {
  1309. fi = fetchInfoMap[terminalInfo[termIndex].terminalID];
  1310. }
  1311. else
  1312. {
  1313. Monitor.Monitor.SetNotification("未查询到该取车终端存在计费操作", parkMonitor.model.TextColor.Error);
  1314. Log.WriteLog(LogType.process, LogFile.ERROR, "终端" + terminalInfo[termIndex].terminalID + "无计费操作");
  1315. return;
  1316. }
  1317. //TerminalStru fetchReset = new TerminalStru
  1318. //{
  1319. // terminalID = (short)termIndex,
  1320. // paymentStatus = (short)0,
  1321. // licVerification = (short)0,
  1322. // parkingFee = (short)32767,
  1323. // userType = (short)0
  1324. //};
  1325. //Monitor.Monitor.PLC.WriteToPLC(fetchReset, PLCDataType.central);
  1326. //更新停车记录与车辆状态
  1327. string updateParkingRecordsSql = "update parkingrecords set parkingRecordsState = 6,realGetTime = '" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") +
  1328. "',parkingPrice = " + fi.fee + ",paymentStatus = " + paymentStatus + " where receiptNum = '" + fi.receiptNum + "';";
  1329. List<string> list = new List<string>();
  1330. list.Add(updateParkingRecordsSql);
  1331. lock (Monitor.Monitor.localDBOper)
  1332. {
  1333. Monitor.Monitor.localDBOper.UpdateTransaction(list);
  1334. UpdateVehicle(fi.license, 0, fi.parkingRecordsID, false, false, 0);
  1335. }
  1336. lock (Monitor.Monitor.remoteDBOper)
  1337. {
  1338. Monitor.Monitor.remoteDBOper.UpdateTransaction(list);
  1339. UpdateVehicle(fi.license, 0, fi.remoteParkingRecordsID, false, true, 0);
  1340. }
  1341. Monitor.Monitor.SetNotification(fi.license + " 取车流程结束", parkMonitor.model.TextColor.Info);
  1342. break;
  1343. }
  1344. Thread.Sleep(200);
  1345. }
  1346. termCalcMap[terminalInfo[termIndex].terminalID] = false;
  1347. fetchInfoMap.Remove(terminalInfo[termIndex].terminalID);
  1348. fetchCompleteMap.Remove(terminalInfo[termIndex].terminalID);
  1349. });
  1350. }
  1351. }
  1352. }
  1353. catch (Exception ex) { Log.WriteLog(LogType.process, LogFile.ERROR, "取车完成流程异常," + ex.StackTrace); }
  1354. }
  1355. /// <summary>
  1356. /// 取车相关操作
  1357. ///
  1358. /// </summary>
  1359. private static void FetchProcess()
  1360. {
  1361. Task.Factory.StartNew(() =>
  1362. {
  1363. while (!isClosing)
  1364. {
  1365. FetchFeeCalcSubProcess();
  1366. Thread.Sleep(Monitor.Monitor.plcRefreshInterval);
  1367. }
  1368. });
  1369. Task.Factory.StartNew(() =>
  1370. {
  1371. while (!isClosing)
  1372. {
  1373. FetchCompleteProcess();
  1374. Thread.Sleep(Monitor.Monitor.plcRefreshInterval);
  1375. }
  1376. });
  1377. }
  1378. public static void Start()
  1379. {
  1380. for (int i = 0; i < Monitor.Monitor.plcTerminalCount; i++)
  1381. {
  1382. lockList.Add(false);
  1383. }
  1384. ResetCompleteSignal();
  1385. ParkProcess();
  1386. FetchProcess();
  1387. //定时更新云端车位,5min
  1388. Task.Factory.StartNew(() =>
  1389. {
  1390. while (!isClosing)
  1391. {
  1392. UpdateAllParkingSpace(true);
  1393. Thread.Sleep(300000);
  1394. }
  1395. });
  1396. }
  1397. public static void Stop()
  1398. {
  1399. isClosing = true;
  1400. }
  1401. public class FetchInfo
  1402. {
  1403. public string license;
  1404. public int parkingRecordsID;
  1405. public int remoteParkingRecordsID;
  1406. public int fee;
  1407. public int receiptNum;
  1408. public FetchInfo(string license, int parkingRecordsID, int remoteParkingRecordsID, int fee, int receiptNum)
  1409. {
  1410. this.license = license;
  1411. this.parkingRecordsID = parkingRecordsID;
  1412. this.remoteParkingRecordsID = remoteParkingRecordsID;
  1413. this.fee = fee;
  1414. this.receiptNum = receiptNum;
  1415. }
  1416. }
  1417. }
  1418. }