Terminal.cs 72 KB

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