Terminal.cs 66 KB

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