Terminal.cs 69 KB

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