forked from gpmagvs/VMSystem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaMap.cs
More file actions
712 lines (637 loc) · 28.4 KB
/
StaMap.cs
File metadata and controls
712 lines (637 loc) · 28.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
using AGVSystemCommonNet6;
using AGVSystemCommonNet6.Configuration;
using AGVSystemCommonNet6.MAP;
using AGVSystemCommonNet6.MAP.Geometry;
using AGVSystemCommonNet6.Microservices.AGVS;
using AGVSystemCommonNet6.Notify;
using NLog;
using System.Collections.Concurrent;
using VMSystem.AGV;
using VMSystem.Dispatch.Regions;
using VMSystem.TrafficControl;
using VMSystem.VMS;
using VMSystem.Extensions;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using VMSystem.Dispatch;
namespace VMSystem
{
public class StaMap
{
private static string MapFile => AGVSConfigulator.SysConfigs.PATHES_STORE[SystemConfigs.PATH_ENUMS.CURRENT_MAP_FILE_PATH];
internal static event EventHandler<ConcurrentDictionary<int, clsPointRegistInfo>> OnRegistedPointStatusChanged;
public static ConcurrentDictionary<int, clsPointRegistInfo> RegistDictionary = new ConcurrentDictionary<int, clsPointRegistInfo>();
public static Dictionary<int, Dictionary<int, double>> Dict_AllPointDistance = new Dictionary<int, Dictionary<int, double>>();
public static event EventHandler<List<MapPoint>> OnPointsDisabled;
public static event EventHandler<Map> OnDefaultMapChanged;
private static Logger logger = LogManager.GetCurrentClassLogger();
private static SemaphoreSlim _DisablePtChangeDectectProcSemaphore = new SemaphoreSlim(1, 1);
private static Map _Map = new Map();
public static Map Map
{
get => _Map;
set
{
if (value == null)
{
NotifyServiceHelper.WARNING($"VMS-地圖管理警告:嘗試將 null 物件指派給當前地圖物件!");
return;
}
_Map = value;
OnDefaultMapChanged?.Invoke("StaMap", value);
VehicleExecuteOrderCostCalculator.ClearPathCache();
}
}
public static void Download()
{
var _Map = MapManager.LoadMapFromFile(false, false).Clone();
if (_Map == null)
{
logger.Error("圖資載入失敗,請檢查圖資檔案是否存在或格式是否正確。");
return;
}
lock (Map)
{
try
{
var _oriPoints = CloneMapPoints(_Map);
Dict_AllPointDistance = GetAllPointDistance(_Map, 1);
PathFinder.defaultMap = _Map;
DisablePointsChangedDetecter(_oriPoints);
RegionManager.Initialze();
Map = _Map;
logger.Info($"圖資載入完成:{Map.Name} ,Version:{Map.Note}");
}
catch (Exception ex)
{
}
}
}
private static async Task DisablePointsChangedDetecter(List<MapPoint> oriPoints)
{
try
{
await _DisablePtChangeDectectProcSemaphore.WaitAsync();
if (oriPoints == null)
return;
var _newPoints = CloneMapPoints(Map);
var oriPtEnableStatus = oriPoints.ToDictionary(pt => pt, pt => pt.Enable);
var newPtEnableStatus = _newPoints.ToDictionary(pt => pt, pt => pt.Enable);
var changeToDisablePoints = newPtEnableStatus.Where(pt => pt.Value == false && oriPtEnableStatus.First(_pt => _pt.Key.TagNumber == pt.Key.TagNumber).Value == true)
.Select(pt => pt.Key); ;
if (changeToDisablePoints.Any())
{
logger.Trace($"Detect Tags: {string.Join(",", changeToDisablePoints.Select(pt => pt.TagNumber))} changed to DISABLE");
OnPointsDisabled?.Invoke("", changeToDisablePoints.ToList());
}
}
catch (Exception ex)
{
logger.Error(ex);
}
finally
{
_DisablePtChangeDectectProcSemaphore.Release();
}
}
private static List<MapPoint> CloneMapPoints(Map map)
{
if (map == null || map.Points == null)
return new List<MapPoint>();
var cloneMap = map.Clone();
return cloneMap.Points.Values.ToList();
}
public static Map GetMapClone()
{
Map mapClone = new Map();
mapClone.Points = Map.Points.ToDictionary(v => v.Key, v => v.Value);
mapClone.Segments = Map.Segments.ToList();
mapClone.Regions = Map.Regions.ToList();
mapClone.TagNoStopOfForkAGV = Map.TagNoStopOfForkAGV.ToList();
mapClone.TagNoStopOfSubmarineAGV = Map.TagNoStopOfSubmarineAGV.ToList();
return mapClone;
}
internal static List<MapPoint> GetParkableStations()
{
if (Map == null)
Download();
var chargeableStations = Map.Points.Values.ToList().FindAll(sta => sta.IsParking);
return chargeableStations;
}
internal static async Task<List<MapPoint>> GetChargeableStations(IAGV TargetAGV = null)
{
if (TargetAGV == null)
{
var chargeableStations = Map.Points.Values.Where(sta => sta.Enable && sta.IsChargeAble()).ToList();
return chargeableStations;
}
var response = await AGVSSerivces.TRAFFICS.GetUseableChargeStationTags(TargetAGV.Name);
return response.usableChargeStationTags.Select(tag => StaMap.GetPointByTagNumber(tag)).Where(pt => pt.Enable).ToList();
}
internal static List<MapPoint> GetAvoidStations()
{
if (Map == null)
Download();
var avoidStations = Map.Points.Values.ToList().FindAll(sta => sta.IsAvoid);
return avoidStations;
}
internal static string GetBayNameByMesLocation(string location)
{
var Bay = Map.Bays.FirstOrDefault(bay => bay.Value.Points.Contains(location));
if (Bay.Key != null)
return Bay.Key;
else
return "Unknown_Bay";
}
internal static bool TryGetPointByTagNumber(int tagNumber, out MapPoint point)
{
point = Map.Points.Values.FirstOrDefault(pt => pt.TagNumber == tagNumber);
return point != null;
}
internal static MapPoint GetPointByTagNumber(int tagNumber)
{
var point = Map.Points.Values.FirstOrDefault(pt => pt.TagNumber == tagNumber);
if (point == null)
{
point = new MapPoint
{
TagNumber = tagNumber,
Name = tagNumber.ToString(),
X = 1000,
Y = 1000
};
}
return point.Clone();
}
internal static List<MapPoint> GetPointByTagNumber(List<int> List_TagNumber)
{
if (List_TagNumber == null)
{
List_TagNumber = new List<int>();
}
var ReturnData = List_TagNumber.Select(item => GetPointByTagNumber(item)).ToList();
return ReturnData.Clone();
}
internal static MapPoint GetPointByName(string name)
{
var point = Map.Points.FirstOrDefault(pt => pt.Value.Graph.Display == name);
if (point.Value != null)
return point.Value.Clone();
return null;
}
internal static MapPoint GetPointByIndex(int index)
{
var point = Map.Points.FirstOrDefault(pt => pt.Key == index);
if (point.Value != null)
return point.Value.Clone();
return new MapPoint()
{
TagNumber = -1,
StationType = MapPoint.STATION_TYPE.Unknown,
};
}
internal static int GetIndexOfPoint(MapPoint mapPoint)
{
try
{
int index = Map.Points.FirstOrDefault(k => k.Value.TagNumber == mapPoint.TagNumber).Key;
return index;
}
catch (Exception ex)
{
return -1;
}
}
internal static bool RegistPoint(string Name, IEnumerable<int> Tags, out string error_message)
{
return RegistPoint(Name, Tags.Select(tag => GetPointByTagNumber(tag)), out error_message, out MapPoint registFailPt);
}
internal static bool RegistPoint(string Name, IEnumerable<MapPoint> mapPoints, out string error_message, out MapPoint registFailPt)
{
registFailPt = null;
error_message = string.Empty;
foreach (var item in mapPoints)
{
if (!RegistPoint(Name, item, out error_message))
{
registFailPt = item;
return false;
}
}
return true;
}
internal static bool RegistPoint(string Name, IEnumerable<MapPoint> mapPoints, out string error_message, out List<MapPoint> registFailPtList)
{
registFailPtList = new List<MapPoint>();
error_message = string.Empty;
foreach (var item in mapPoints)
{
if (!RegistPoint(Name, item, out error_message))
{
registFailPtList.Add(item);
}
}
return registFailPtList.Count() == 0;
}
internal static bool RegistPointBySystem(MapPoint mapPoint, out string error_message, string AGVName = "")
{
return RegistPoint("System", mapPoint, out error_message, true);
}
private static void RegistNearPoints(string VehicleName, MapPoint CenterPoints)
{
IAGV registAgv = VMSManager.GetAGVByName(VehicleName);
var centerCircleArea = CenterPoints.GetCircleArea(ref registAgv);
var normalPoints = Map.Points.Values.Where(pt => pt.StationType == MapPoint.STATION_TYPE.Normal);
var conflicPoints = normalPoints.Where(pt => pt.GetCircleArea(ref registAgv).IsIntersectionTo(centerCircleArea));
foreach (var point in conflicPoints)
{
bool success = RegistPoint(VehicleName, point, out var errMsg, registNearPoints: false);
if (!success)
{
}
else
{
RegistDictionary[point.TagNumber].NearRegistInfo.IsRegisted = true;
RegistDictionary[point.TagNumber].NearRegistInfo.RegistByPointTag = CenterPoints.TagNumber;
}
}
}
internal static bool RegistPoint(string Name, MapPoint mapPoint, out string error_message, bool IsBySystem = false, bool registNearPoints = true)
{
try
{
IAGV agv = VMSManager.GetAGVByName(Name);
_unregistSemaphore.Wait();
_registSemaphore.Wait();
error_message = string.Empty;
var TagNumber = mapPoint.TagNumber;
if (IsBySystem)
{
mapPoint.RegistInfo = new clsPointRegistInfo("System");
RegistDictionary.Remove(TagNumber, out clsPointRegistInfo _info);
RegistDictionary.TryAdd(TagNumber, mapPoint.RegistInfo);
//logger.Trace($"{Name} Regist Tag {TagNumber}");
return true;
}
List<MapPoint> toRegistMapPoints = new List<MapPoint> { mapPoint };
toRegistMapPoints.AddRange(mapPoint.GetRegistPoints().ToList());
foreach (var mpt in toRegistMapPoints)
{
bool _IsPtRegisted = _IsPtRegistedByOthers(mpt, Name);
if (_IsPtRegisted && mpt.TagNumber == mapPoint.TagNumber)
{
error_message = $"{mapPoint.Graph.Display} is Registed...";
return false;
}
if (!_IsPtRegisted)
{
mpt.RegistInfo = new clsPointRegistInfo(Name);
bool _registSuccess = RegistDictionary.TryAdd(mpt.TagNumber, mpt.RegistInfo);
if (_registSuccess)
{
logger.Info($"{Name} Regist {TagNumber} (Now is {agv?.currentMapPoint.TagNumber})");
}
else if (mpt.RegistInfo.RegisterAGVName == agv.Name)
{
logger.Info($"{Name} Regist {TagNumber} (Now is {agv.currentMapPoint.TagNumber})[Registed Previous.]");
}
}
}
return true;
}
catch (Exception ex)
{
error_message = ex.Message;
return false;
}
finally
{
OnRegistedPointStatusChanged?.Invoke("", RegistDictionary);
_registSemaphore.Release();
_unregistSemaphore.Release();
}
}
private static bool _IsPtRegistedByOthers(MapPoint mpt, string name)
{
if (!RegistDictionary.TryGetValue(mpt.TagNumber, out clsPointRegistInfo registInfo))
return false;
return registInfo?.RegisterAGVName != name;
}
internal static async Task<(bool success, string error_message)> UnRegistPointBySystem(MapPoint mapPoint)
{
return await UnRegistPoint("System", mapPoint, true);
}
internal static async Task<(bool success, string error_message)> UnRegistPoints(string Name, IEnumerable<int> TagNumbers, bool isBySystem = false, bool UnRegistAddictionRegistPt = true)
{
foreach (var tag in TagNumbers)
{
await UnRegistPoint(Name, tag, isBySystem, UnRegistAddictionRegistPt);
}
return (true, "");
}
private static SemaphoreSlim _unregistSemaphore = new SemaphoreSlim(1, 1);
private static SemaphoreSlim _registSemaphore = new SemaphoreSlim(1, 1);
internal static async Task<(bool success, string error_message)> UnRegistPoint(string Name, int TagNumber, bool IsBySystem = false, bool UnRegistAddictionRegistPt = true)
{
try
{
await _unregistSemaphore.WaitAsync();
await _registSemaphore.WaitAsync();
IAGV agv = VMSManager.GetAGVByName(Name);
if (agv == null && !IsBySystem)
return (false, "AGV Entity NULL");
var mapPoint = StaMap.GetPointByTagNumber(TagNumber);
if (IsBySystem)
{
RegistDictionary.Remove(TagNumber, out var _);
mapPoint.RegistInfo = new clsPointRegistInfo();
logger.Trace($"{Name} UnRegist Tag {TagNumber}");
return (true, "");
}
bool isUnregistPtIsAGVCurrentPtRegisted = agv.currentMapPoint.GetRegistPoints().GetTagCollection().Contains(mapPoint.TagNumber);
List<MapPoint> toUnregistMapPoints = new List<MapPoint> { };
if (!isUnregistPtIsAGVCurrentPtRegisted)
toUnregistMapPoints.Add(mapPoint);
var currentNavigationTags = agv.NavigationState.NextNavigtionPoints.GetTagCollection().ToList();
if (UnRegistAddictionRegistPt)
toUnregistMapPoints.AddRange(mapPoint.GetRegistPoints().Where(pt => !currentNavigationTags.Contains(pt.TagNumber)).ToList());
foreach (var item in toUnregistMapPoints)
{
if (!_IsPtRegistedByOthers(item, Name) && RegistDictionary.TryRemove(item.TagNumber, out clsPointRegistInfo info))
{
item.RegistInfo = new clsPointRegistInfo();
}
}
logger.Info($"{Name} Unregist {TagNumber} (Now is {agv.currentMapPoint.TagNumber})");
return (true, "");
}
catch (Exception ex)
{
var error_message = ex.Message;
logger.Warn($"{Name} UnRegist Tag {TagNumber} Fail : {error_message}");
return (false, error_message);
}
finally
{
OnRegistedPointStatusChanged?.Invoke("", RegistDictionary);
_unregistSemaphore.Release();
_registSemaphore.Release();
}
}
internal static async Task<(bool success, string error_message)> UnRegistPoint(string Name, MapPoint mapPoint, bool IsBySystem = false, bool UnRegistAddictionRegistPt = true)
{
return await UnRegistPoint(Name, mapPoint.TagNumber, IsBySystem, UnRegistAddictionRegistPt);
}
internal static bool CheckTagExistOnMap(int currentTag)
{
return Map.Points.Select(pt => pt.Value.TagNumber).Contains(currentTag);
}
internal static List<MapPoint> GetRegistedPointWithNearPointOfPath(List<MapPoint> OriginPath, Dictionary<int, List<MapPoint>> DictAllNearPoint, string NowAGV)
{
List<MapPoint> OutputData = new List<MapPoint>();
var RegistDictWithoutNowAGV = RegistDictionary.Where(item => item.Value.RegisterAGVName != NowAGV).ToDictionary(item => item.Key, item => item.Value);
foreach (var item in DictAllNearPoint)
{
if (item.Value.Any(nearPoint => RegistDictWithoutNowAGV.ContainsKey(nearPoint.TagNumber)))
{
OutputData.Add(StaMap.GetPointByTagNumber(item.Key));
}
}
return OutputData.OrderBy(pt => OriginPath.IndexOf(pt)).ToList();
}
internal static List<MapPoint> GetRegistedPointsOfPath(List<MapPoint> path_to_nav, string navigating_agv_name)
{
IReadOnlyList<MapPoint> registPoints = RegistDictionary.Where(kp => kp.Value.RegisterAGVName != navigating_agv_name).Select(kp => StaMap.GetPointByTagNumber(kp.Key)).ToList();
List<MapPoint> commonItems = registPoints.Intersect(path_to_nav, new MapPointComparer()).ToList();
return commonItems.OrderBy(pt => path_to_nav.IndexOf(pt)).ToList();
}
internal static string GetStationNameByTag(int tag)
{
var point = Map.Points.Values.FirstOrDefault(pt => pt.TagNumber == tag);
return point == null ? tag + "" : point.Graph.Display;
}
internal static List<MapPoint> GetAllRegistedPointsByName(string name)
{
return RegistDictionary.Where(pt => pt.Value.RegisterAGVName == name)
.Select(pt => GetPointByTagNumber(pt.Key))
.ToList();
return Map.Points.Values.Where(pt => pt.RegistInfo != null).Where(pt => pt.RegistInfo.RegisterAGVName == name).ToList();
}
internal static async Task UnRegistPoints(string name, List<MapPoint> unRegistList, bool includesSubRegistedPt = true)
{
foreach (var point in unRegistList)
{
await UnRegistPoint(name, point, UnRegistAddictionRegistPt: includesSubRegistedPt);
}
}
internal static bool IsMapPointRegisted(MapPoint waitingPoint, string queryer_name)
{
if (RegistDictionary.TryGetValue(waitingPoint.TagNumber, out var result))
{
return result.RegisterAGVName != queryer_name;
}
else
return false;
}
internal static async Task<bool> UnRegistPointsOfAGVRegisted(IAGV agv, bool isIncludesSubRegistedPt = false)
{
try
{
await _unregistSemaphore.WaitAsync();
if (agv.main_state == clsEnums.MAIN_STATUS.RUN && agv.currentMapPoint.StationType == MapPoint.STATION_TYPE.Normal)
{
logger.Warn($"{agv.Name} 狀態為RUN但嘗試解除註冊點 已禁止");
return false;
}
int agvLastVisitTag = agv.currentMapPoint.TagNumber;
List<int> tagsToUnregist = new List<int>();
var currentPointRegistedsPtTags = agv.currentMapPoint.GetRegistPoints().GetTagCollection().ToList();
var allRegistedPtOfAGV = RegistDictionary.Where(pt => pt.Value.RegisterAGVName == agv.Name);//&& !currentPointRegistedsPtTags.Contains(pt.Key)
allRegistedPtOfAGV = allRegistedPtOfAGV.Where(pt => pt.Key != agvLastVisitTag);
//除了當前點位sub 註冊點以外的點位
var toRemoveForcing = allRegistedPtOfAGV.Where(pt => !currentPointRegistedsPtTags.Contains(pt.Key)).ToList();
foreach (var item in toRemoveForcing)
{
if (!RegistDictionary.TryRemove(item.Key, out _))
logger.Warn($"AGV {agv.Name} UnRegist Point {item.Key} Fail");
else
logger.Trace($"AGV {agv.Name} UnRegist Point {item.Key}");
}
if (isIncludesSubRegistedPt)
{
RegistDictionary.Where(pt => pt.Value.RegisterAGVName == agv.Name)
.Where(pt => pt.Key != agvLastVisitTag)
.ToList()
.ForEach(pt =>
{
if (!RegistDictionary.TryRemove(pt.Key, out _))
logger.Warn($"AGV {agv.Name} UnRegist Point {pt.Key} Fail");
else
logger.Trace($"AGV {agv.Name} UnRegist Point {pt.Key}");
});
}
OnRegistedPointStatusChanged?.Invoke("", RegistDictionary);
return true;
}
catch (Exception ex)
{
logger.Fatal(ex);
return false;
}
finally
{
_unregistSemaphore.Release();
}
}
internal static bool GetPointRegisterName(int tagNumber, out string agvName)
{
agvName = string.Empty;
if (!RegistDictionary.TryGetValue(tagNumber, out var _rinfo))
return false;
agvName = _rinfo.RegisterAGVName;
return agvName != null;
}
internal static bool GetNearPointRegisterName(int tagNumber, string TargetAGV, out string AGVName, out int NearPointTag)
{
AGVName = string.Empty;
NearPointTag = -1;
var TargetPoint = GetPointByTagNumber(tagNumber);
var AGVItem = VMSManager.AllAGV.First(item => item.Name == TargetAGV);
//var List_NearPointTag = TargetPoint.Target.Where(item => (item.Value * 100) < 100).Select(item => item.Key);
var List_NearPointTag = Dict_AllPointDistance[tagNumber].Where(item => item.Value < AGVItem.options.VehicleLength / 100).Select(item => item.Key);
foreach (var item in List_NearPointTag)
{
var MapPointData = GetPointByTagNumber(item);
if (RegistDictionary.TryGetValue(MapPointData.TagNumber, out var _RegistInfo))
{
if (_RegistInfo.RegisterAGVName == TargetAGV) //自己跟自己不卡控
continue;
AGVName = _RegistInfo.RegisterAGVName;
NearPointTag = item;
return true;
}
}
return false;
}
internal static Dictionary<int, Dictionary<int, double>> GetAllPointDistance(Map MapData, double DistanceLimit)
{
Dictionary<int, Dictionary<int, double>> Dict_OutputData = new Dictionary<int, Dictionary<int, double>>();
foreach (var TargetPoint in MapData.Points)
{
var TargetPointData = TargetPoint.Value;
Dict_OutputData.Add(TargetPointData.TagNumber, new Dictionary<int, double>());
foreach (var RelatePoint in MapData.Points)
{
if (RelatePoint.Key == TargetPoint.Key)
{
continue;
}
var RelatePointData = RelatePoint.Value;
var Distance = CalculateDistance(TargetPointData.X, RelatePointData.X, TargetPointData.Y, RelatePointData.Y);
Dict_OutputData[TargetPointData.TagNumber].Add(RelatePointData.TagNumber, Distance);
}
}
return Dict_OutputData;
}
internal static List<int> GetNearPointListByPointAndDistance(int TagNumber, double DistanceLimit)
{
if (!Dict_AllPointDistance.ContainsKey(TagNumber))
return new List<int>();
return Dict_AllPointDistance[TagNumber].Where(item => item.Value < DistanceLimit).Select(item => item.Key).ToList();
}
internal static List<int> GetNearPointListByPathAndDistance(List<int> List_PathTags, double DistanceLimit)
{
List<int> OutputData = new List<int>();
foreach (var item in List_PathTags)
{
OutputData.AddRange(GetNearPointListByPointAndDistance(item, DistanceLimit));
}
OutputData = OutputData.Distinct().ToList();
return OutputData;
}
internal static double CalculateDistance(double X1, double X2, double Y1, double Y2)
{
return Math.Pow(Math.Pow(X1 - X2, 2) + Math.Pow(Y1 - Y2, 2) * 1.0, 0.5);
}
internal static List<MapPoint> GetNoStopPointsByAGVModel(clsEnums.AGV_TYPE model)
{
IReadOnlyList<int> tags = GetNoStopTagsByAGVModel(model);
return tags.Select(tag => GetPointByTagNumber(tag)).ToList();
}
internal static IReadOnlyList<int> GetNoStopTagsByAGVModel(clsEnums.AGV_TYPE model)
{
List<int> tags = new List<int>();
switch (model)
{
case clsEnums.AGV_TYPE.FORK:
tags = Map.TagForbiddenForForkAGV.ToList();
break;
case clsEnums.AGV_TYPE.YUNTECH_FORK_AGV:
break;
case clsEnums.AGV_TYPE.INSPECTION_AGV:
break;
case clsEnums.AGV_TYPE.SUBMERGED_SHIELD:
tags = Map.TagForbiddenForSubMarineAGV.ToList().Clone();
break;
case clsEnums.AGV_TYPE.SUBMERGED_SHIELD_Parts:
break;
case clsEnums.AGV_TYPE.Any:
break;
default:
break;
}
return tags;
}
private static SemaphoreSlim pathDynamicEditSemaphoreSlim = new SemaphoreSlim(1, 1);
internal static async Task<bool> TryRemovePathDynamic(MapPath path)
{
return Map.TryRemovePathDynamic(path);
}
internal static async Task<(bool confirmed, MapPath path)> TryRemovePathDynamic(MapPoint fromPt, MapPoint toPt)
{
try
{
await pathDynamicEditSemaphoreSlim.WaitAsync();
return Map.TryRemovePathDynamic(fromPt, toPt);
}
catch (Exception ex)
{
throw ex;
}
finally
{
pathDynamicEditSemaphoreSlim.Release();
}
}
internal static async Task<bool> AddPathDynamic(MapPath path)
{
try
{
await pathDynamicEditSemaphoreSlim.WaitAsync();
return Map.AddPathDynamic(path);
}
catch (Exception ex)
{
return false;
}
finally
{
pathDynamicEditSemaphoreSlim.Release();
}
}
public class MapPointComparer : IEqualityComparer<MapPoint>
{
public bool Equals(MapPoint x, MapPoint y)
{
if (x == null || y == null)
return false;
return x.TagNumber == y.TagNumber;
}
public int GetHashCode(MapPoint obj)
{
return obj.TagNumber.GetHashCode();
}
}
}
}