Terminal.cs 62 KB

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