Terminal.cs 63 KB

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