Terminal.cs 61 KB

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