-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExtensions.cs
More file actions
372 lines (348 loc) · 14.7 KB
/
Extensions.cs
File metadata and controls
372 lines (348 loc) · 14.7 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
using AGVSystemCommonNet6.AGVDispatch;
using AGVSystemCommonNet6.AGVDispatch.Messages;
using AGVSystemCommonNet6.AGVDispatch.Model;
using AGVSystemCommonNet6.MAP;
using KGSWebAGVSystemAPI.Sys;
using Newtonsoft.Json;
using RosSharp.RosBridgeClient.MessageTypes.Geometry;
using RosSharp.RosBridgeClient.MessageTypes.Std;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;
using static AGVSystemCommonNet6.MAP.MapPoint;
namespace AGVSystemCommonNet6
{
public static class Extensions
{
private static Map _mapUse = null;
private static Map mapUse
{
get
{
if (_mapUse == null)
{
string mapPath = KGSSettingsHelper.GetCurrentMapUseFilePath();
_mapUse = MapManager.LoadMapFromFile(mapPath, out string errMsg, auto_create_segment: false, auto_check_path_error: false);
}
return _mapUse;
}
}
public static Time ToStdTime(this DateTime _time)
{
return new Time()
{
secs = (uint)(_time.Subtract(new DateTime(1970, 1, 1)).TotalSeconds),
nsecs = (uint)(_time.Millisecond * 1000000),
};
}
public static string ToAGVSTimeFormat(this DateTime _time)
{
return _time.ToString("yyyyMMdd HH:mm:ss");
}
/// <summary>
/// 將角度值轉換為 Quaternion(四位元)
/// </summary>
/// <param name="Theta"></param>
/// <returns></returns>
public static Quaternion ToQuaternion(this double Theta)
{
double yaw_radians = (float)Theta * Math.PI / 180.0;
double cos_yaw = Math.Cos(yaw_radians / 2.0);
double sin_yaw = Math.Sin(yaw_radians / 2.0);
return new Quaternion(0.0f, 0.0f, (float)sin_yaw, (float)cos_yaw);
}
public static int[] GetRemainPath(this IEnumerable<clsMapPoint> points, int startTag)
{
if (points.Count() == 0)
return new int[1] { startTag };
int find_index(int tag)
{
return points.ToList().FindIndex(p => p.Point_ID == tag);
}
var startTag_index = find_index(startTag);
return points.ToList().FindAll(p => find_index(p.Point_ID) >= startTag_index).Select(pt => pt.Point_ID).ToArray();
}
public static double ToTheta(this RosSharp.RosBridgeClient.MessageTypes.Geometry.Quaternion orientation)
{
double yaw;
double x = orientation.x;
double y = orientation.y;
double z = orientation.z;
double w = orientation.w;
// 計算角度
double siny_cosp = 2.0 * (w * z + x * y);
double cosy_cosp = 1.0 - 2.0 * (y * y + z * z);
yaw = Math.Atan2(siny_cosp, cosy_cosp);
return yaw * 180.0 / Math.PI;
}
public static string ToJsonWithNewtonsoft(this object obj, Formatting format = Formatting.Indented)
{
try
{
return JsonConvert.SerializeObject(obj, format);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return "{}";
}
}
public static string ToJson(this object obj, Formatting format = Formatting.Indented)
{
try
{
var options = new JsonSerializerOptions
{
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
WriteIndented = format == Formatting.Indented // 美化輸出
};
string json = System.Text.Json.JsonSerializer.Serialize(obj, options);
return json;
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
return "{}";
}
}
public static T Clone<T>(this T obt)
{
var _type = obt.GetType();
try
{
var json = obt.ToJson(Formatting.None);
return System.Text.Json.JsonSerializer.Deserialize<T>(json);
}
catch (Exception ex)
{
return obt;
}
}
public static string GetString(this byte[] byte_data, Encoding encoder)
{
try
{
return encoder.GetString(byte_data);
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
return "{}";
}
}
/// <summary>
/// 該站點是否可充電
/// </summary>
/// <param name="map_station"></param>
/// <returns></returns>
public static bool IsChargeAble(this MapPoint map_station)
{
STATION_TYPE station_type = map_station.StationType;
return station_type == STATION_TYPE.Charge || station_type == STATION_TYPE.Charge_Buffer || station_type == STATION_TYPE.Charge_STK;
}
/// <summary>
/// 該站點是否可供AGV Load/Unload
/// </summary>
/// <param name="map_station"></param>
/// <returns></returns>
public static bool IsLoadAble(this MapPoint map_station)
{
STATION_TYPE station_type = map_station.StationType;
return station_type == STATION_TYPE.EQ || station_type == STATION_TYPE.EQ_LD
|| station_type == STATION_TYPE.STK || station_type == STATION_TYPE.STK_LD
|| station_type == STATION_TYPE.Charge_STK;
}
/// <summary>
/// 該站點是否可供AGV Load/Unload
/// </summary>
/// <param name="map_station"></param>
/// <returns></returns>
public static bool IsUnloadAble(this MapPoint map_station)
{
STATION_TYPE station_type = map_station.StationType;
return station_type == STATION_TYPE.EQ || station_type == STATION_TYPE.EQ_ULD
|| station_type == STATION_TYPE.STK || station_type == STATION_TYPE.STK_ULD
|| station_type == STATION_TYPE.Charge_STK;
}
/// <summary>
/// 計算與站點的距離
/// </summary>
/// <param name="map_station"></param>
/// <param name="from_loc_x"></param>
/// <param name="from_loc_y"></param>
/// <returns></returns>
public static double CalculateDistance(this MapPoint map_station, double from_loc_x, double from_loc_y, int tag = -1)
{
if (map_station == null)
return -1;
double distance = Math.Sqrt(Math.Pow(map_station.X - from_loc_x, 2) + Math.Pow(map_station.Y - from_loc_y, 2));
//Console.WriteLine($"Distance from ({map_station.X},{map_station.Y}) (Tag:{map_station.TagNumber}) to ({from_loc_x},{from_loc_y})(Tag:{tag}) is {distance}");
return distance;
}
/// <summary>
/// 計算站點距離 (單位:m)
/// </summary>
/// <param name="map_station"></param>
/// <param name="coord"></param>
/// <returns></returns>
public static double CalculateDistance(this MapPoint map_station, clsCoordination coord)
{
return map_station.CalculateDistance(coord.X, coord.Y);
}
public static double CalculateDistance(this clsCoordination from, clsCoordination to)
{
double distance = Math.Sqrt(Math.Pow(from.X - to.X, 2) + Math.Pow(from.Y - to.Y, 2));
return distance;
}
/// <summary>
/// 計算與站點的距離
/// </summary>
/// <param name="map_station"></param>
/// <param name="from_loc_x"></param>
/// <param name="from_loc_y"></param>
/// <returns></returns>
public static double CalculateDistance(this MapPoint map_station, MapPoint from_station)
{
return map_station.CalculateDistance(from_station.X, from_station.Y, from_station.TagNumber);
}
public static double TotalTravelDistance(this IEnumerable<MapPoint> points)
{
double distance = 0;
for (int i = 0; i < points.Count() - 1; i++)
{
distance += points.ElementAt(i).CalculateDistance(points.ElementAt(i + 1));
}
return distance;
}
/// <summary>
/// 將整數轉成2進位,每個bit用boolean表示0/1 (0:false, 1:ture)
/// </summary>
/// <param name="int_val"></param>
/// <returns></returns>
public static bool[] To4Booleans(this int int_val)
{
bool[] bool_ary = new bool[4];
for (int i = 0; i < 4; i++)
{
bool_ary[i] = ((int_val >> i) & 1) != 1;
}
return bool_ary;
}
public static int ToInt(this bool[] bool_ary)
{
return BitConverter.ToInt16(bool_ary.Select(b => b ? (byte)0x1 : (byte)0x00).Reverse().ToArray(), 0);
}
/// <summary>
/// 將GPM任務訂單轉成 KGS 任務API需要的查詢參數封裝
/// </summary>
/// <param name="gpmDto"></param>
/// <returns></returns>
public static KGSWebAGVSystemAPI.TaskOrder.MissionRequestParams ToKGSMissionRequestParam(this clsTaskDto gpmDto)
{
KGSWebAGVSystemAPI.TaskOrder.MissionRequestParams param = new KGSWebAGVSystemAPI.TaskOrder.MissionRequestParams();
param.CarName = gpmDto.DesignatedAGVName;
if (param.CarName != null)
{
try
{
param.AGVID = int.Parse(param.CarName.Split('_')[1]);
}
catch (Exception)
{
}
}
string fromStationName = mapUse.Points.Values.FirstOrDefault(pt => pt.TagNumber == gpmDto.From_Station_Tag)?.Name;
string toStationName = mapUse.Points.Values.FirstOrDefault(pt => pt.TagNumber == gpmDto.To_Station_Tag)?.Name;
param.Action = gpmDto.Action.ToString();
param.Priority = gpmDto.Priority;
param.FromStation = fromStationName;
param.ToStation = toStationName;
param.CSTID = gpmDto.Carrier_ID;
param.CSTType = gpmDto.CST_TYPE;
param.RepeatTime = 1;
return param;
}
public static clsTaskDto ToGPMTaskDto(this KGSWebAGVSystemAPI.Models.ExecutingTask kgTask)
{
KGSWebAGVSystemAPI.Models.Task _Task = JsonConvert.DeserializeObject<KGSWebAGVSystemAPI.Models.Task>(JsonConvert.SerializeObject(kgTask));
var dto = _Task.ToGPMTaskDto();
return dto;
}
public static clsTaskDto ToGPMTaskDto(this KGSWebAGVSystemAPI.Models.Task kgTask)
{
try
{
var dto = new clsTaskDto
{
Action = kgTask.ActionType == "Transfer" ? ACTION_TYPE.Carry : Enum.GetValues<ACTION_TYPE>().Cast<ACTION_TYPE>().FirstOrDefault(enuu => enuu.ToString() == kgTask.ActionType),
TaskName = kgTask.Name,
DesignatedAGVName = "AGV_" + kgTask.ExeVehicleID.ToString("X3"),
DispatcherName = kgTask.AssignUserName,
FailureReason = kgTask.FailReason,
Carrier_ID = kgTask.CSTID,
CST_TYPE = kgTask.CSTType == null ? 0 : (int)kgTask.CSTType,
From_Slot = kgTask.FromStationPortNo + "",
To_Slot = kgTask.ToStationPortNo + "",
FinishTime = kgTask.EndTime == null ? DateTime.MinValue : (DateTime)kgTask.EndTime,
RecieveTime = kgTask.Receive_Time,
StartTime = kgTask.StartTime == null ? DateTime.MinValue : (DateTime)kgTask.StartTime,
State = GetTaskStatus(kgTask.Status),
Priority = kgTask.Priority,
};
if (kgTask.FromStationId != null && mapUse.Points.TryGetValue((int)kgTask.FromStationId, out MapPoint fromPt))
{
dto.From_Station = fromPt.TagNumber + "";
}
if (kgTask.ToStationId != null && mapUse.Points.TryGetValue((int)kgTask.ToStationId, out MapPoint toPt))
{
dto.To_Station = toPt.TagNumber + "";
}
return dto;
TASK_RUN_STATUS GetTaskStatus(int status)
{
switch (status)
{
case 98:
return TASK_RUN_STATUS.WAIT;
case 1:
return TASK_RUN_STATUS.NAVIGATING;
case 2:
return TASK_RUN_STATUS.NAVIGATING;
case 3:
return TASK_RUN_STATUS.NAVIGATING;
case 4:
return TASK_RUN_STATUS.NAVIGATING;
case 90:
return TASK_RUN_STATUS.NAVIGATING;
case 100:
return TASK_RUN_STATUS.ACTION_FINISH;
case 101:
return TASK_RUN_STATUS.CANCEL;
default:
return TASK_RUN_STATUS.WAIT;
}
}
}
catch (Exception ex)
{
return new clsTaskDto()
{
TaskName = null
};
}
}
public static List<clsTaskDto> ToGPMTaskCollection(this IEnumerable<KGSWebAGVSystemAPI.Models.Task> tasks)
{
List<clsTaskDto> task_ = new List<clsTaskDto>();
task_ = tasks.Select(kgTask => kgTask.ToGPMTaskDto()).ToList();
return task_.Where(tk => tk.TaskName != null).ToList();
}
public static List<clsTaskDto> ToGPMTaskCollection(this IEnumerable<KGSWebAGVSystemAPI.Models.ExecutingTask> tasks)
{
List<clsTaskDto> task_ = new List<clsTaskDto>();
task_ = tasks.Select(kgTask => kgTask.ToGPMTaskDto()).ToList();
return task_.Where(tk => tk.TaskName != null).ToList();
}
}
}