|  |  | 1 |  | using LGDXRobotCloud.API.Repositories; | 
|  |  | 2 |  | using LGDXRobotCloud.API.Services.Navigation; | 
|  |  | 3 |  | using LGDXRobotCloud.Data.DbContexts; | 
|  |  | 4 |  | using LGDXRobotCloud.Data.Entities; | 
|  |  | 5 |  | using LGDXRobotCloud.Protos; | 
|  |  | 6 |  | using LGDXRobotCloud.Utilities.Enums; | 
|  |  | 7 |  | using Microsoft.EntityFrameworkCore; | 
|  |  | 8 |  |  | 
|  |  | 9 |  | namespace LGDXRobotCloud.API.Services.Automation; | 
|  |  | 10 |  |  | 
|  |  | 11 |  | public interface IAutoTaskPathPlannerService | 
|  |  | 12 |  | { | 
|  |  | 13 |  |   Task<List<RobotClientsPath>> GeneratePath(AutoTask autoTask); | 
|  |  | 14 |  | } | 
|  |  | 15 |  |  | 
|  | 0 | 16 |  | public partial class AutoTaskPathPlannerService( | 
|  | 0 | 17 |  |     ILogger<AutoTaskPathPlannerService> logger, | 
|  | 0 | 18 |  |     IMapEditorService mapEditorService, | 
|  | 0 | 19 |  |     IRobotDataRepository robotDataRepository, | 
|  | 0 | 20 |  |     LgdxContext context | 
|  | 0 | 21 |  |   ) : IAutoTaskPathPlannerService | 
|  |  | 22 |  | { | 
|  | 0 | 23 |  |   private readonly IMapEditorService _mapEditorService = mapEditorService ?? throw new ArgumentNullException(nameof(mapE | 
|  | 0 | 24 |  |   private readonly IRobotDataRepository _robotDataRepository = robotDataRepository ?? throw new ArgumentNullException(na | 
|  | 0 | 25 |  |   private readonly LgdxContext _context = context ?? throw new ArgumentNullException(nameof(context)); | 
|  |  | 26 |  |  | 
|  |  | 27 |  |   [LoggerMessage(EventId = 0, Level = LogLevel.Error, Message = "AutoTaskPathPlannerService Path planning: The task deta | 
|  |  | 28 |  |   public partial void TheTaskDetailDoesNotHaveWaypoint(); | 
|  |  | 29 |  |  | 
|  |  | 30 |  |   [LoggerMessage(EventId = 1, Level = LogLevel.Error, Message = "AutoTaskPathPlannerService Path planning: Unable to fin | 
|  |  | 31 |  |   public partial void TheTaskDetailDoesNotHaveWaypoint(int startWaypointId, int endWaypointId); | 
|  |  | 32 |  |  | 
|  |  | 33 |  |   [LoggerMessage(EventId = 2, Level = LogLevel.Error, Message = "AutoTaskPathPlannerService Path planning: Robot data no | 
|  |  | 34 |  |   public partial void RobotDataNotFoundForRobotId(Guid robotId); | 
|  |  | 35 |  |  | 
|  |  | 36 |  |   private static double EuclideanDistance(double x1, double y1, double x2, double y2) | 
|  | 0 | 37 |  |   { | 
|  | 0 | 38 |  |     return Math.Sqrt(Math.Pow(x2 - x1, 2) + Math.Pow(y2 - y1, 2)); | 
|  | 0 | 39 |  |   } | 
|  |  | 40 |  |  | 
|  |  | 41 |  |   private static double ManhattanDistance(double x1, double y1, double x2, double y2) | 
|  | 0 | 42 |  |   { | 
|  | 0 | 43 |  |     return Math.Abs(x2 - x1) + Math.Abs(y2 - y1); | 
|  | 0 | 44 |  |   } | 
|  |  | 45 |  |  | 
|  |  | 46 |  |   private static List<RobotClientsDof> PathPlanningGeneratePath(Dictionary<int, int> parentMap, int currentId, Waypoints | 
|  | 0 | 47 |  |   { | 
|  | 0 | 48 |  |     List<int> path = [currentId]; | 
|  | 0 | 49 |  |     while (parentMap.TryGetValue(currentId, out int parentId)) | 
|  | 0 | 50 |  |     { | 
|  | 0 | 51 |  |       path.Add(parentId); | 
|  | 0 | 52 |  |       currentId = parentId; | 
|  | 0 | 53 |  |     } | 
|  | 0 | 54 |  |     path.Reverse(); | 
|  |  | 55 |  |  | 
|  | 0 | 56 |  |     var pathPlanningPath = new List<RobotClientsDof>(); | 
|  | 0 | 57 |  |     foreach (var id in path) | 
|  | 0 | 58 |  |     { | 
|  | 0 | 59 |  |       var waypoint = waypointsTraffic.Waypoints[id]!; | 
|  | 0 | 60 |  |       pathPlanningPath.Add(new RobotClientsDof | 
|  | 0 | 61 |  |       { | 
|  | 0 | 62 |  |         X = waypoint.X, | 
|  | 0 | 63 |  |         Y = waypoint.Y, | 
|  | 0 | 64 |  |         Rotation = waypoint.Rotation, | 
|  | 0 | 65 |  |       }); | 
|  | 0 | 66 |  |     } | 
|  | 0 | 67 |  |     return pathPlanningPath; | 
|  | 0 | 68 |  |   } | 
|  |  | 69 |  |  | 
|  |  | 70 |  |   private List<RobotClientsDof> PathPlanning(AutoTaskDetail start, AutoTaskDetail end, WaypointsTraffic waypointsTraffic | 
|  | 0 | 71 |  |   { | 
|  |  | 72 |  |     // Check waypoint | 
|  | 0 | 73 |  |     if (start.WaypointId == null || end.WaypointId == null) | 
|  | 0 | 74 |  |     { | 
|  | 0 | 75 |  |       TheTaskDetailDoesNotHaveWaypoint(); | 
|  | 0 | 76 |  |       throw new Exception(); | 
|  |  | 77 |  |     } | 
|  | 0 | 78 |  |     int startWaypointId = (int)start.WaypointId; | 
|  | 0 | 79 |  |     List<int> openList = [startWaypointId]; | 
|  | 0 | 80 |  |     List<int> closedList = []; | 
|  |  | 81 |  |  | 
|  | 0 | 82 |  |     var gScore = new Dictionary<int, double> { [startWaypointId] = 0 }; | 
|  | 0 | 83 |  |     var hScore = new Dictionary<int, double> { [startWaypointId] = ManhattanDistance(start.Waypoint!.X, start.Waypoint!. | 
|  | 0 | 84 |  |     var parentMap = new Dictionary<int, int>(); | 
|  |  | 85 |  |  | 
|  | 0 | 86 |  |     while (openList.Count > 0) | 
|  | 0 | 87 |  |     { | 
|  | 0 | 88 |  |       var currentId = openList.OrderBy(w => gScore[w] + hScore[w]).First(); | 
|  | 0 | 89 |  |       if (currentId == end.WaypointId) | 
|  | 0 | 90 |  |       { | 
|  | 0 | 91 |  |         return PathPlanningGeneratePath(parentMap, currentId, waypointsTraffic); | 
|  |  | 92 |  |       } | 
|  |  | 93 |  |  | 
|  | 0 | 94 |  |       openList.Remove(currentId); | 
|  | 0 | 95 |  |       closedList.Add(currentId); | 
|  |  | 96 |  |  | 
|  | 0 | 97 |  |       foreach (var neighborId in waypointsTraffic.WaypointTraffics[currentId]) | 
|  | 0 | 98 |  |       { | 
|  | 0 | 99 |  |         var neighbor = waypointsTraffic.Waypoints[neighborId]; | 
|  | 0 | 100 |  |         if (closedList.Contains(neighbor.Id)) | 
|  | 0 | 101 |  |         { | 
|  | 0 | 102 |  |           continue; | 
|  |  | 103 |  |         } | 
|  |  | 104 |  |  | 
|  | 0 | 105 |  |         var current = waypointsTraffic.Waypoints[currentId]; | 
|  | 0 | 106 |  |         double newGScore = gScore[current.Id] + ManhattanDistance(current.X, current.Y, neighbor.X, neighbor.Y); | 
|  | 0 | 107 |  |         if (!gScore.TryGetValue(neighbor.Id, out double currentGScore) || newGScore < currentGScore) | 
|  | 0 | 108 |  |         { | 
|  |  | 109 |  |           // Update gScore and hScore | 
|  | 0 | 110 |  |           gScore[neighbor.Id] = newGScore; | 
|  | 0 | 111 |  |           hScore[neighbor.Id] = ManhattanDistance(neighbor.X, neighbor.Y, end.Waypoint!.X, end.Waypoint!.Y); | 
|  |  | 112 |  |  | 
|  |  | 113 |  |           // Add the neighbor to the open list | 
|  | 0 | 114 |  |           parentMap[neighbor.Id] = current.Id; | 
|  | 0 | 115 |  |           if (!openList.Contains(neighbor.Id)) | 
|  | 0 | 116 |  |           { | 
|  | 0 | 117 |  |             openList.Add(neighbor.Id); | 
|  | 0 | 118 |  |           } | 
|  | 0 | 119 |  |         } | 
|  | 0 | 120 |  |       } | 
|  | 0 | 121 |  |     } | 
|  |  | 122 |  |  | 
|  | 0 | 123 |  |     TheTaskDetailDoesNotHaveWaypoint(start.WaypointId.Value, end.WaypointId.Value); | 
|  | 0 | 124 |  |     throw new Exception(""); | 
|  | 0 | 125 |  |   } | 
|  |  | 126 |  |  | 
|  |  | 127 |  |   private static RobotClientsDof GenerateWaypoint(AutoTaskDetail taskDetail) | 
|  | 0 | 128 |  |   { | 
|  | 0 | 129 |  |     if (taskDetail.Waypoint != null) | 
|  | 0 | 130 |  |     { | 
|  | 0 | 131 |  |       var waypoint = new RobotClientsDof | 
|  | 0 | 132 |  |       { X = taskDetail.Waypoint.X, Y = taskDetail.Waypoint.Y, Rotation = taskDetail.Waypoint.Rotation }; | 
|  | 0 | 133 |  |       if (taskDetail.CustomX != null) | 
|  | 0 | 134 |  |         waypoint.X = (double)taskDetail.CustomX; | 
|  | 0 | 135 |  |       if (taskDetail.CustomY != null) | 
|  | 0 | 136 |  |         waypoint.X = (double)taskDetail.CustomY; | 
|  | 0 | 137 |  |       if (taskDetail.CustomRotation != null) | 
|  | 0 | 138 |  |         waypoint.X = (double)taskDetail.CustomRotation; | 
|  | 0 | 139 |  |       return waypoint; | 
|  |  | 140 |  |     } | 
|  |  | 141 |  |     else | 
|  | 0 | 142 |  |     { | 
|  | 0 | 143 |  |       return new RobotClientsDof | 
|  | 0 | 144 |  |       { | 
|  | 0 | 145 |  |         X = taskDetail.CustomX != null ? (double)taskDetail.CustomX : 0, | 
|  | 0 | 146 |  |         Y = taskDetail.CustomY != null ? (double)taskDetail.CustomY : 0, | 
|  | 0 | 147 |  |         Rotation = taskDetail.CustomRotation != null ? (double)taskDetail.CustomRotation : 0 | 
|  | 0 | 148 |  |       }; | 
|  |  | 149 |  |     } | 
|  | 0 | 150 |  |   } | 
|  |  | 151 |  |  | 
|  |  | 152 |  |   public async Task<List<RobotClientsPath>> GeneratePath(AutoTask autoTask) | 
|  | 0 | 153 |  |   { | 
|  | 0 | 154 |  |     var realmId = autoTask.RealmId; | 
|  | 0 | 155 |  |     var hasWaypointsTrafficControl = _context.Realms.AsNoTracking() | 
|  | 0 | 156 |  |       .Where(r => r.Id == realmId) | 
|  | 0 | 157 |  |       .Select(r => r.HasWaypointsTrafficControl) | 
|  | 0 | 158 |  |       .FirstOrDefault(); | 
|  |  | 159 |  |  | 
|  | 0 | 160 |  |     List<RobotClientsPath> paths = []; | 
|  | 0 | 161 |  |     List<AutoTaskDetail> taskDetails = []; | 
|  | 0 | 162 |  |     if (autoTask.CurrentProgressId == (int)ProgressState.PreMoving) | 
|  | 0 | 163 |  |     { | 
|  | 0 | 164 |  |       var firstTaskDetail = await _context.AutoTasksDetail.AsNoTracking() | 
|  | 0 | 165 |  |         .Where(t => t.AutoTaskId == autoTask.Id) | 
|  | 0 | 166 |  |         .Include(t => t.Waypoint) | 
|  | 0 | 167 |  |         .OrderBy(t => t.Order) | 
|  | 0 | 168 |  |         .FirstOrDefaultAsync(); | 
|  | 0 | 169 |  |       if (firstTaskDetail != null) | 
|  | 0 | 170 |  |         taskDetails.Add(firstTaskDetail); | 
|  | 0 | 171 |  |     } | 
|  | 0 | 172 |  |     else if (autoTask.CurrentProgressId == (int)ProgressState.Moving) | 
|  | 0 | 173 |  |     { | 
|  | 0 | 174 |  |       taskDetails = await _context.AutoTasksDetail.AsNoTracking() | 
|  | 0 | 175 |  |         .Where(t => t.AutoTaskId == autoTask.Id) | 
|  | 0 | 176 |  |         .Include(t => t.Waypoint) | 
|  | 0 | 177 |  |         .OrderBy(t => t.Order) | 
|  | 0 | 178 |  |         .ToListAsync(); | 
|  | 0 | 179 |  |     } | 
|  | 0 | 180 |  |     if (taskDetails.Count == 0) | 
|  | 0 | 181 |  |     { | 
|  | 0 | 182 |  |       return []; | 
|  |  | 183 |  |     } | 
|  |  | 184 |  |  | 
|  | 0 | 185 |  |     if (hasWaypointsTrafficControl) | 
|  | 0 | 186 |  |     { | 
|  |  | 187 |  |       // Has waypoints traffic control, return the waypoints by respecting the traffic control | 
|  |  | 188 |  |       // Prepare waypoints traffic | 
|  | 0 | 189 |  |       var waypointsTraffic = await _mapEditorService.GetWaypointTrafficAsync(realmId); | 
|  |  | 190 |  |  | 
|  |  | 191 |  |       // Find the nearest waypoint | 
|  | 0 | 192 |  |       var robotData = await _robotDataRepository.GetRobotDataAsync(realmId, (Guid)autoTask.AssignedRobotId!); | 
|  | 0 | 193 |  |       if (robotData == null) | 
|  | 0 | 194 |  |       { | 
|  | 0 | 195 |  |         RobotDataNotFoundForRobotId((Guid)autoTask.AssignedRobotId!); | 
|  | 0 | 196 |  |         throw new Exception(); | 
|  |  | 197 |  |       } | 
|  | 0 | 198 |  |       int selectedWaypointId = 0; | 
|  | 0 | 199 |  |       double selectedWaypointDistance = double.MaxValue; | 
|  | 0 | 200 |  |       foreach (var waypoint in waypointsTraffic.Waypoints) | 
|  | 0 | 201 |  |       { | 
|  | 0 | 202 |  |         var distance = EuclideanDistance(waypoint.Value.X, waypoint.Value.Y, robotData.Position.X, robotData.Position.Y) | 
|  | 0 | 203 |  |         if (distance < selectedWaypointDistance) | 
|  | 0 | 204 |  |         { | 
|  | 0 | 205 |  |           selectedWaypointId = waypoint.Key; | 
|  | 0 | 206 |  |           selectedWaypointDistance = distance; | 
|  | 0 | 207 |  |         } | 
|  | 0 | 208 |  |       } | 
|  | 0 | 209 |  |       taskDetails.Insert(0, new AutoTaskDetail | 
|  | 0 | 210 |  |       { | 
|  | 0 | 211 |  |         Id = 0, | 
|  | 0 | 212 |  |         Order = 0, | 
|  | 0 | 213 |  |         WaypointId = selectedWaypointId, | 
|  | 0 | 214 |  |         Waypoint = waypointsTraffic.Waypoints[selectedWaypointId], | 
|  | 0 | 215 |  |         AutoTaskId = autoTask.Id, | 
|  | 0 | 216 |  |         AutoTask = autoTask | 
|  | 0 | 217 |  |       }); | 
|  |  | 218 |  |  | 
|  |  | 219 |  |       // Path planning using A* | 
|  | 0 | 220 |  |       for (int i = 1; i < taskDetails.Count; i++) | 
|  | 0 | 221 |  |       { | 
|  | 0 | 222 |  |         var start = taskDetails[i - 1]; | 
|  | 0 | 223 |  |         var end = taskDetails[i]; | 
|  | 0 | 224 |  |         var planningPath = PathPlanning(start, end, waypointsTraffic); | 
|  |  | 225 |  |  | 
|  |  | 226 |  |         // Use custom waypoint for end | 
|  | 0 | 227 |  |         var planningPathEnd = planningPath.Last(); | 
|  | 0 | 228 |  |         if (end.CustomX != null) | 
|  | 0 | 229 |  |         { | 
|  | 0 | 230 |  |           planningPathEnd.X = (double)end.CustomX; | 
|  | 0 | 231 |  |         } | 
|  | 0 | 232 |  |         if (end.CustomY != null) | 
|  | 0 | 233 |  |         { | 
|  | 0 | 234 |  |           planningPathEnd.Y = (double)end.CustomY; | 
|  | 0 | 235 |  |         } | 
|  | 0 | 236 |  |         if (end.CustomRotation != null) | 
|  | 0 | 237 |  |         { | 
|  | 0 | 238 |  |           planningPathEnd.Rotation = (double)end.CustomRotation; | 
|  | 0 | 239 |  |         } | 
|  | 0 | 240 |  |         planningPath.RemoveAt(planningPath.Count - 1); | 
|  | 0 | 241 |  |         planningPath.Add(planningPathEnd); | 
|  |  | 242 |  |  | 
|  | 0 | 243 |  |         paths.Add(new RobotClientsPath | 
|  | 0 | 244 |  |         { | 
|  | 0 | 245 |  |           Waypoints = { planningPath } | 
|  | 0 | 246 |  |         }); | 
|  | 0 | 247 |  |       } | 
|  | 0 | 248 |  |     } | 
|  |  | 249 |  |     else | 
|  | 0 | 250 |  |     { | 
|  |  | 251 |  |       // No waypoints traffic control, just return the waypoints | 
|  | 0 | 252 |  |       foreach (var t in taskDetails) | 
|  | 0 | 253 |  |       { | 
|  | 0 | 254 |  |         paths.Add(new RobotClientsPath | 
|  | 0 | 255 |  |         { | 
|  | 0 | 256 |  |           Waypoints = { GenerateWaypoint(t) } | 
|  | 0 | 257 |  |         }); | 
|  | 0 | 258 |  |       } | 
|  | 0 | 259 |  |     } | 
|  | 0 | 260 |  |     return paths; | 
|  | 0 | 261 |  |   } | 
|  |  | 262 |  | } |