SpaceManager.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. //using parkMonitor.DataBase;
  7. using parkMonitor.Database2;
  8. using System.Configuration;
  9. using parkMonitor.entity;
  10. using parkMonitor.LOG;
  11. using parkMonitor.server.uiLogServer;
  12. using parkMonitor.controlPanel;
  13. using parkMonitor.model;
  14. namespace parkMonitor.server.CoreThread
  15. {
  16. /// <summary>
  17. /// 车位分配
  18. /// </summary>
  19. public class ParkingSpaceManager
  20. {
  21. public Dictionary<int, Parking_Space> parkingSpaceStatusMap = new Dictionary<int, Parking_Space>();
  22. private Dictionary<int, Parking_Space> lps = new Dictionary<int, Parking_Space>();
  23. private double xWeight;
  24. private double yWeight;
  25. private double zWeight;
  26. public static ParkingSpaceManager ins = null;
  27. public static int garageID;
  28. /// <summary>
  29. /// 初始化车位缓存
  30. /// </summary>
  31. public bool InitParkingSpace()
  32. {
  33. if (ins != null)
  34. {
  35. lock (ins.parkingSpaceStatusMap)
  36. {
  37. parkingSpaceStatusMap = new Dictionary<int, Parking_Space>();
  38. DBOperation oper = new DBOperation();
  39. garageID = 0;
  40. try
  41. {
  42. garageID = Int32.Parse(ConfigurationManager.AppSettings["garageID"]);
  43. }
  44. catch { UILogServer.ins.error("无法获取车库ID,请检查配置"); Log.WriteLog(LogType.NOT_DATABASE, LogFile.ERROR, "无车库ID相关配置"); return false; }
  45. try
  46. {
  47. xWeight = Convert.ToDouble(ConfigurationManager.AppSettings["xWeight"]);
  48. yWeight = Convert.ToDouble(ConfigurationManager.AppSettings["yWeight"]);
  49. zWeight = Convert.ToDouble(ConfigurationManager.AppSettings["zWeight"]);
  50. }
  51. catch { UILogServer.ins.error("无法获得车位权重,请检查配置"); Log.WriteLog(LogType.NOT_DATABASE, LogFile.ERROR, "无法获得车位权重"); return false; }
  52. int i = 0;
  53. if (garageID != 0)
  54. {
  55. while (((parkingSpaceStatusMap == null || parkingSpaceStatusMap.Count==0) && i++ < 2))
  56. {
  57. parkingSpaceStatusMap = oper.GetAllParkingSpace(EntityForCore.remoteBQ, garageID);
  58. }
  59. if (i >= 2)
  60. {
  61. parkingSpaceStatusMap = null;
  62. return false;
  63. }
  64. }
  65. else
  66. {
  67. parkingSpaceStatusMap = null;
  68. return false;
  69. }
  70. //if (garageID != 0)
  71. //{
  72. // parkingSpaceStatusMap = oper.GetAllParkingSpace(EntityForCore.remoteBQ, garageID);
  73. //}
  74. //else
  75. //{
  76. // parkingSpaceStatusMap = null;
  77. // return false;
  78. //}
  79. //车位校验,与PLC车位数据核对
  80. Log.WriteLog(LogType.NOT_DATABASE, LogFile.INFO, "车位表缓存成功");
  81. return true;
  82. }
  83. }
  84. else { return false; }
  85. }
  86. /// <summary>
  87. /// 最短路径分配,根据车位位置及状态分配目标车位,返回Parking Space的ID
  88. /// </summary>
  89. /// <param name="pt_Ent"></param>
  90. /// <param name="booking">进行预约,提前分配车位</param>
  91. /// <param name="queueCmd"></param>
  92. /// <param name="count"></param>
  93. /// <returns></returns>
  94. public Parking_Space MallocParkingSpace(CEntrance pt_Ent, Command queueCmd, bool booking, out int count)
  95. {
  96. count = 0;
  97. if (ins != null)
  98. {
  99. lock (ins.parkingSpaceStatusMap)
  100. {
  101. //获取车位前准备
  102. if (parkingSpaceStatusMap == null || zWeight == 0)
  103. {
  104. Log.WriteLog(LogType.NOT_DATABASE, LogFile.ERROR, "车辆表缓存或车位权重初始化异常");
  105. return null;
  106. }
  107. //获取空闲车位和判断是否已预约车位
  108. if (lps != null)
  109. {
  110. lps.Clear();
  111. }
  112. else
  113. {
  114. lps = new Dictionary<int, Parking_Space>();
  115. }
  116. Dictionary<int, Parking_Space>.Enumerator enumer = parkingSpaceStatusMap.GetEnumerator();
  117. Parking_Space temp = null;
  118. string log = "[";
  119. while (enumer.MoveNext())
  120. {
  121. if (enumer.Current.Value != null)
  122. {
  123. if (enumer.Current.Value.parkingSpaceState == 0)
  124. {
  125. lps.Add(enumer.Current.Key, enumer.Current.Value);
  126. //输出空闲车位
  127. log += enumer.Current.Key.ToString() + ",";
  128. }
  129. else if (enumer.Current.Value.parkingSpaceState == 2 && enumer.Current.Value.licence == queueCmd.LicenseNum)
  130. {
  131. temp = (Parking_Space)(enumer.Current.Value.Clone());
  132. }
  133. }
  134. }
  135. Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, log+"]");
  136. if (temp != null && !booking)
  137. {
  138. //分配已预约的车位后将该车位置为占用
  139. parkingSpaceStatusMap[temp.parkingSpaceID].parkingSpaceState = 1;
  140. return temp;
  141. }
  142. //无空闲车位则退出
  143. if (lps.Count == 0)
  144. {
  145. return null;
  146. }
  147. //分配车位
  148. Parking_Space rps = new Parking_Space();
  149. float min_dis_sq = 100000000; //初始10000单位距离
  150. Dictionary<int, Parking_Space>.Enumerator enumer2 = lps.GetEnumerator();
  151. while (enumer2.MoveNext())
  152. {
  153. if (enumer2.Current.Value != null)
  154. {
  155. float deltaX = (enumer2.Current.Value.parkingSpaceX - pt_Ent.parkingEntX) * (float)xWeight;
  156. float deltaY = (enumer2.Current.Value.parkingSpaceY - pt_Ent.parkingEntY) * (float)yWeight;
  157. float deltaZ = (enumer2.Current.Value.parkingSpaceZ - pt_Ent.parkingEntZ) * (float)zWeight;
  158. float dist_sq = deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ;
  159. if (dist_sq < min_dis_sq)
  160. {
  161. min_dis_sq = dist_sq;
  162. rps = enumer2.Current.Value;
  163. }
  164. }
  165. }
  166. count = lps.Count - 1;
  167. //分配后将该车位置为占用或已预约
  168. if (!booking)
  169. {
  170. parkingSpaceStatusMap[rps.parkingSpaceID].parkingSpaceState = 1;
  171. }
  172. else
  173. {
  174. parkingSpaceStatusMap[rps.parkingSpaceID].parkingSpaceState = 2;
  175. parkingSpaceStatusMap[rps.parkingSpaceID].licence = queueCmd.LicenseNum;
  176. }
  177. return rps;
  178. }
  179. }
  180. return null;
  181. }
  182. /// <summary>
  183. /// 取车完成将该车位释放
  184. /// </summary>
  185. /// <param name="parkingSpaceID"></param>
  186. /// <returns></returns>
  187. public bool SetParkingSpace(int parkingSpaceID, int status)
  188. {
  189. if (CheckIfSpaceExist(parkingSpaceID))
  190. {
  191. if (parkingSpaceStatusMap[parkingSpaceID].parkingSpaceState != status)
  192. {
  193. parkingSpaceStatusMap[parkingSpaceID].parkingSpaceState = status;
  194. Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "车位ID:" + parkingSpaceStatusMap[parkingSpaceID].parkingSpaceID + "置为" + status);
  195. //清空该车位号牌
  196. if (status == 0)
  197. {
  198. parkingSpaceStatusMap[parkingSpaceID].licence = "";
  199. }
  200. return true;
  201. }
  202. else { return false; }
  203. }
  204. else
  205. {
  206. return false;
  207. }
  208. }
  209. /// <summary>
  210. /// 判断车位是否存在
  211. /// </summary>
  212. /// <param name="parkingSpaceID"></param>
  213. /// <returns></returns>
  214. public bool CheckIfSpaceExist(int parkingSpaceID)
  215. {
  216. if (parkingSpaceStatusMap != null && parkingSpaceStatusMap.ContainsKey(parkingSpaceID))
  217. {
  218. return true;
  219. }
  220. else
  221. {
  222. return false;
  223. }
  224. }
  225. /// <summary>
  226. /// 获取空闲车位数
  227. /// </summary>
  228. /// <returns></returns>
  229. public int GetFreeSpaceCount()
  230. {
  231. int count = 0;
  232. if (parkingSpaceStatusMap != null)
  233. {
  234. Dictionary<int, Parking_Space>.Enumerator enumer = parkingSpaceStatusMap.GetEnumerator();
  235. while (enumer.MoveNext())
  236. {
  237. if (enumer.Current.Value != null && enumer.Current.Value.parkingSpaceState == 0) { count += 1; }
  238. }
  239. }
  240. return count;
  241. }
  242. /// <summary>
  243. /// 释放已预约占用的车位
  244. /// </summary>
  245. /// <param name="license"></param>
  246. /// <returns></returns>
  247. public bool CancleBooking(string license)
  248. {
  249. Dictionary<int, Parking_Space>.Enumerator enumer = parkingSpaceStatusMap.GetEnumerator();
  250. Parking_Space temp = null;
  251. while (enumer.MoveNext())
  252. {
  253. if (enumer.Current.Value != null && enumer.Current.Value.parkingSpaceState == 2 && enumer.Current.Value.licence == license)
  254. {
  255. temp = (Parking_Space)(enumer.Current.Value.Clone());
  256. }
  257. }
  258. if (temp != null)
  259. {
  260. parkingSpaceStatusMap[temp.parkingSpaceID].parkingSpaceState = 0;
  261. parkingSpaceStatusMap[temp.parkingSpaceID].licence = "";
  262. return true;
  263. }
  264. else { return false; }
  265. }
  266. }
  267. public class CEntrance
  268. {
  269. public int parkingEntX { get; set; }
  270. public int parkingEntY { get; set; }
  271. public int parkingEntZ { get; set; }
  272. public CEntrance(int entX, int entY, int entZ)
  273. {
  274. parkingEntX = entX;
  275. parkingEntY = entY;
  276. parkingEntZ = entZ;
  277. }
  278. public CEntrance()
  279. : this(0, 0, 0)
  280. {
  281. }
  282. }
  283. /// <summary>
  284. /// 车位单元
  285. /// </summary>
  286. public class Parking_Space : ICloneable
  287. {
  288. public static Object spaceLock = new Object();
  289. public static Object RecordLock = new Object();
  290. public int parkingSpaceID { get; set; }
  291. //public float parkingSpaceWeight { get; set; }
  292. public int garageID { get; set; }
  293. //public int parkingSpaceStatement { get; set; }
  294. public int parkingSpaceX { get; set; }
  295. public int parkingSpaceY { get; set; }
  296. public int parkingSpaceZ { get; set; }
  297. // 0.空闲 1.占用 2.预约
  298. public int parkingSpaceState { get; set; }
  299. public string licence { get; set; }
  300. public object Clone()
  301. {
  302. Parking_Space ps_new = new Parking_Space();
  303. ps_new.parkingSpaceID = parkingSpaceID;
  304. ps_new.garageID = garageID;
  305. ps_new.parkingSpaceState = parkingSpaceState;
  306. ps_new.parkingSpaceX = parkingSpaceX;
  307. ps_new.parkingSpaceY = parkingSpaceY;
  308. ps_new.parkingSpaceZ = parkingSpaceZ;
  309. ps_new.licence = licence;
  310. return ps_new;
  311. }
  312. }
  313. /// <summary>
  314. /// 缓冲位管理
  315. /// </summary>
  316. public class ParkingBufferManager
  317. {
  318. public static ParkingBufferManager ins { get; set; }
  319. public int bufferCount { get; set; }
  320. private int bufferAddress = 0, completeStatusAddress = 0;
  321. public bool checkedAuto { get; set; }
  322. public List<ParkingBuffer> bufferList = null;
  323. public bool InitParkingBuffers()
  324. {
  325. checkedAuto = false;
  326. bufferList = new List<ParkingBuffer>();
  327. try
  328. {
  329. bufferCount = Int32.Parse(ConfigurationManager.AppSettings["bufferCount"]);
  330. bufferAddress = Int32.Parse(ConfigurationManager.AppSettings["bufferAddress"]);
  331. completeStatusAddress = Int32.Parse(ConfigurationManager.AppSettings["completeStatusAddress"]);
  332. IEquipments plc = null;
  333. List<PLCNode> pList = null;
  334. while (plc == null)
  335. {
  336. plc = EquipmentSimpleFactory.ins.FindEquipment(EquipmentName.PLC);
  337. }
  338. pList = ((PLCMessage)plc.GetMessage()).originalPlcList;
  339. //根据plc中数据初始化缓冲位
  340. for (int i = 0; i < bufferCount; i++)
  341. {
  342. for (int j = 0; j < pList.Count; j++)
  343. {
  344. if (pList[j].Address == (bufferAddress + i).ToString())
  345. {
  346. if (pList[j].Value == "1")
  347. {
  348. bufferList.Add(new ParkingBuffer(i + 1, true));
  349. break;
  350. }
  351. else if (pList[j].Value == "0")
  352. {
  353. bufferList.Add(new ParkingBuffer(i + 1, false));
  354. break;
  355. }
  356. else
  357. {
  358. UILogServer.ins.error("缓冲位数据异常,请检查plc相关数据");
  359. return false;
  360. }
  361. }
  362. }
  363. }
  364. //UILogServer.ins.warn("请手动确认缓冲位状态");
  365. Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "初始化缓冲位成功");
  366. checkedAuto = true;
  367. return true;
  368. }
  369. catch { Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "初始化缓冲位失败"); return false; }
  370. }
  371. public ParkingBuffer MallocParkingBuffer(int numMachineID)
  372. {
  373. if (!checkedAuto)
  374. return null;
  375. int min_dis_sq = 2500; //初始默认缓冲位距号牌ID 50 个单位
  376. ParkingBuffer allocatedPB = null;
  377. for (int i = 0; i < bufferCount; i++)
  378. {
  379. if (!bufferList[i].occupied)
  380. {
  381. int diff_dis = bufferList[i].bufferID - numMachineID;
  382. if (min_dis_sq > diff_dis * diff_dis)
  383. {
  384. min_dis_sq = diff_dis * diff_dis;
  385. allocatedPB = (ParkingBuffer)bufferList[i].Clone();
  386. }
  387. }
  388. }
  389. if (allocatedPB != null && allocatedPB.bufferID > 0)
  390. {
  391. bufferList[allocatedPB.bufferID - 1].occupied = true;
  392. return allocatedPB;
  393. }
  394. else
  395. {
  396. return null;
  397. }
  398. }
  399. /// <summary>
  400. /// 暂时取车完成释放缓冲位
  401. /// </summary>
  402. /// <param name="parkingSpaceID"></param>
  403. /// <returns></returns>
  404. public bool ReleaseParkingBuffer(int bufferID)
  405. {
  406. if (CheckIfBufferExist(bufferID))
  407. {
  408. bufferList[bufferID - 1].occupied = false;
  409. return true;
  410. }
  411. else
  412. {
  413. return false;
  414. }
  415. }
  416. /// <summary>
  417. /// 判断缓冲位是否存在
  418. /// </summary>
  419. /// <param name="parkingSpaceID"></param>
  420. /// <returns></returns>
  421. public bool CheckIfBufferExist(int bufferID)
  422. {
  423. if (bufferList != null && bufferList.Count >= bufferID && bufferID > 0)
  424. {
  425. return true;
  426. }
  427. else
  428. {
  429. return false;
  430. }
  431. }
  432. public bool SetParkingBuffer(int parkingBufferID, bool status)
  433. {
  434. if (CheckIfBufferExist(parkingBufferID))
  435. {
  436. if (bufferList[parkingBufferID - 1].occupied != status)
  437. {
  438. bufferList[parkingBufferID - 1].occupied = status;
  439. return true;
  440. }
  441. else { return false; }
  442. }
  443. else
  444. {
  445. return false;
  446. }
  447. }
  448. }
  449. /// <summary>
  450. /// 缓冲位单元
  451. /// </summary>
  452. public class ParkingBuffer : ICloneable
  453. {
  454. /// <summary>
  455. /// 缓冲位ID
  456. /// </summary>
  457. public int bufferID;
  458. /// <summary>
  459. /// 占用标记
  460. /// </summary>
  461. public bool occupied;
  462. /// <summary>
  463. /// 单参数构造函数
  464. /// </summary>
  465. /// <param name="bufferID"></param>
  466. public ParkingBuffer(int bufferID)
  467. : this(bufferID, true)
  468. {
  469. }
  470. /// <summary>
  471. /// 双参数构造函数
  472. /// </summary>
  473. /// <param name="bufferID"></param>
  474. /// <param name="occupied"></param>
  475. public ParkingBuffer(int bufferID, bool occupied)
  476. {
  477. this.bufferID = bufferID;
  478. this.occupied = occupied;
  479. }
  480. public object Clone()
  481. {
  482. ParkingBuffer pbc = new ParkingBuffer(this.bufferID, this.occupied);
  483. return pbc;
  484. }
  485. }
  486. }