| | 1 | | using LGDXRobotCloud.API.Services.Common; |
| | 2 | | using LGDXRobotCloud.Data.Contracts; |
| | 3 | | using LGDXRobotCloud.Data.Entities; |
| | 4 | | using LGDXRobotCloud.Protos; |
| | 5 | | using LGDXRobotCloud.Utilities.Enums; |
| | 6 | | using MassTransit; |
| | 7 | | using Microsoft.Extensions.Caching.Distributed; |
| | 8 | | using Microsoft.Extensions.Caching.Memory; |
| | 9 | |
|
| | 10 | | namespace LGDXRobotCloud.API.Services.Navigation; |
| | 11 | |
|
| | 12 | | public class RobotCommandsEventArgs : EventArgs |
| | 13 | | { |
| | 14 | | public Guid RobotId { get; set; } |
| | 15 | | public required RobotClientsRobotCommands Commands { get; set; } |
| | 16 | | } |
| | 17 | |
|
| | 18 | | public interface IOnlineRobotsService |
| | 19 | | { |
| | 20 | | Task AddRobotAsync(Guid robotId); |
| | 21 | | Task RemoveRobotAsync(Guid robotId); |
| | 22 | | Task UpdateRobotDataAsync(Guid robotId, RobotClientsExchange data); |
| | 23 | | RobotClientsRobotCommands? GetRobotCommands(Guid robotId); |
| | 24 | |
|
| | 25 | | Task<bool> IsRobotOnlineAsync(Guid robotId); |
| | 26 | | Task<bool> SetAbortTaskAsync(Guid robotId, bool enable); |
| | 27 | | Task<bool> SetSoftwareEmergencyStopAsync(Guid robotId, bool enable); |
| | 28 | | Task<bool> SetPauseTaskAssigementAsync(Guid robotId, bool enable); |
| | 29 | | bool GetPauseAutoTaskAssignment(Guid robotId); |
| | 30 | |
|
| | 31 | | void SetAutoTaskNextApi(Guid robotId, AutoTask task); |
| | 32 | | AutoTask? GetAutoTaskNextApi(Guid robotId); |
| | 33 | | } |
| | 34 | |
|
| 0 | 35 | | public class OnlineRobotsService( |
| 0 | 36 | | IBus bus, |
| 0 | 37 | | IEmailService emailService, |
| 0 | 38 | | IEventService eventService, |
| 0 | 39 | | IMemoryCache memoryCache, |
| 0 | 40 | | IRobotService robotService |
| 0 | 41 | | ) : IOnlineRobotsService |
| | 42 | | { |
| 0 | 43 | | private readonly IBus _bus = bus ?? throw new ArgumentNullException(nameof(bus)); |
| 0 | 44 | | private readonly IEmailService _emailService = emailService ?? throw new ArgumentNullException(nameof(emailService)); |
| 0 | 45 | | private readonly IEventService _eventService = eventService ?? throw new ArgumentNullException(nameof(eventService)); |
| 0 | 46 | | private readonly IMemoryCache _memoryCache = memoryCache ?? throw new ArgumentNullException(nameof(memoryCache)); |
| 0 | 47 | | private readonly IRobotService _robotService = robotService ?? throw new ArgumentNullException(nameof(robotService)); |
| 0 | 48 | | private static string GetOnlineRobotsKey(int realmId) => $"OnlineRobotsService_OnlineRobots_{realmId}"; |
| 0 | 49 | | private static string GetRobotCommandsKey(Guid robotId) => $"OnlineRobotsService_RobotCommands_{robotId}"; |
| | 50 | |
|
| | 51 | | private static RobotStatus ConvertRobotStatus(RobotClientsRobotStatus robotStatus) |
| 0 | 52 | | { |
| 0 | 53 | | return robotStatus switch |
| 0 | 54 | | { |
| 0 | 55 | | RobotClientsRobotStatus.Idle => RobotStatus.Idle, |
| 0 | 56 | | RobotClientsRobotStatus.Running => RobotStatus.Running, |
| 0 | 57 | | RobotClientsRobotStatus.Stuck => RobotStatus.Stuck, |
| 0 | 58 | | RobotClientsRobotStatus.Aborting => RobotStatus.Aborting, |
| 0 | 59 | | RobotClientsRobotStatus.Paused => RobotStatus.Paused, |
| 0 | 60 | | RobotClientsRobotStatus.Critical => RobotStatus.Critical, |
| 0 | 61 | | RobotClientsRobotStatus.Charging => RobotStatus.Charging, |
| 0 | 62 | | RobotClientsRobotStatus.Offline => RobotStatus.Offline, |
| 0 | 63 | | _ => RobotStatus.Offline, |
| 0 | 64 | | }; |
| 0 | 65 | | } |
| | 66 | |
|
| | 67 | | static private bool GenerateUnresolvableCriticalStatus(RobotClientsRobotCriticalStatus criticalStatus) |
| 0 | 68 | | { |
| 0 | 69 | | if (criticalStatus.HardwareEmergencyStop || |
| 0 | 70 | | criticalStatus.BatteryLow.Count > 0 || |
| 0 | 71 | | criticalStatus.MotorDamaged.Count > 0) |
| 0 | 72 | | { |
| 0 | 73 | | return true; |
| | 74 | | } |
| 0 | 75 | | return false; |
| 0 | 76 | | } |
| | 77 | |
|
| | 78 | | public async Task AddRobotAsync(Guid robotId) |
| 0 | 79 | | { |
| 0 | 80 | | var realmId = await _robotService.GetRobotRealmIdAsync(robotId) ?? 0; |
| 0 | 81 | | var OnlineRobotsIds = _memoryCache.Get<HashSet<Guid>>(GetOnlineRobotsKey(realmId)) ?? []; |
| 0 | 82 | | OnlineRobotsIds.Add(robotId); |
| | 83 | | // Register the robot |
| 0 | 84 | | _memoryCache.Set(GetOnlineRobotsKey(realmId), OnlineRobotsIds); |
| 0 | 85 | | _memoryCache.Set(GetRobotCommandsKey(robotId), new RobotClientsRobotCommands()); |
| 0 | 86 | | } |
| | 87 | |
|
| | 88 | | public async Task RemoveRobotAsync(Guid robotId) |
| 0 | 89 | | { |
| 0 | 90 | | var realmId = await _robotService.GetRobotRealmIdAsync(robotId) ?? 0; |
| | 91 | | // Unregister the robot |
| 0 | 92 | | var OnlineRobotsIds = _memoryCache.Get<HashSet<Guid>>(GetOnlineRobotsKey(realmId)); |
| 0 | 93 | | if (OnlineRobotsIds != null && OnlineRobotsIds.Contains(robotId)) |
| 0 | 94 | | { |
| 0 | 95 | | OnlineRobotsIds.Remove(robotId); |
| 0 | 96 | | _memoryCache.Set(GetOnlineRobotsKey(realmId), OnlineRobotsIds); |
| 0 | 97 | | } |
| 0 | 98 | | _memoryCache.Remove(GetRobotCommandsKey(robotId)); |
| 0 | 99 | | } |
| | 100 | |
|
| | 101 | | public async Task UpdateRobotDataAsync(Guid robotId, RobotClientsExchange data) |
| 0 | 102 | | { |
| 0 | 103 | | if (_memoryCache.TryGetValue<bool>($"OnlineRobotsService_RobotData_Pause_{robotId}", out var _)) |
| 0 | 104 | | { |
| | 105 | | // Blocking too much data to rabbitmq |
| 0 | 106 | | return; |
| | 107 | | } |
| 0 | 108 | | _memoryCache.Set($"OnlineRobotsService_RobotData_Pause_{robotId}", true, TimeSpan.FromSeconds(1)); |
| | 109 | |
|
| 0 | 110 | | var realmId = await _robotService.GetRobotRealmIdAsync(robotId) ?? 0; |
| 0 | 111 | | var robotStatus = ConvertRobotStatus(data.RobotStatus); |
| 0 | 112 | | await _bus.Publish(new RobotDataContract { |
| 0 | 113 | | RobotId = robotId, |
| 0 | 114 | | RealmId = realmId, |
| 0 | 115 | | RobotStatus = robotStatus, |
| 0 | 116 | | CriticalStatus = new RobotCriticalStatus { |
| 0 | 117 | | HardwareEmergencyStop = data.CriticalStatus.HardwareEmergencyStop, |
| 0 | 118 | | SoftwareEmergencyStop = data.CriticalStatus.SoftwareEmergencyStop, |
| 0 | 119 | | BatteryLow = [.. data.CriticalStatus.BatteryLow], |
| 0 | 120 | | MotorDamaged = [.. data.CriticalStatus.MotorDamaged] |
| 0 | 121 | | }, |
| 0 | 122 | | Batteries = [.. data.Batteries], |
| 0 | 123 | | Position = new RobotDof { |
| 0 | 124 | | X = data.Position.X, |
| 0 | 125 | | Y = data.Position.Y, |
| 0 | 126 | | Rotation = data.Position.Rotation |
| 0 | 127 | | }, |
| 0 | 128 | | NavProgress = new AutoTaskNavProgress { |
| 0 | 129 | | Eta = data.NavProgress.Eta, |
| 0 | 130 | | Recoveries = data.NavProgress.Recoveries, |
| 0 | 131 | | DistanceRemaining = data.NavProgress.DistanceRemaining, |
| 0 | 132 | | WaypointsRemaining = data.NavProgress.WaypointsRemaining |
| 0 | 133 | | } |
| 0 | 134 | | }); |
| | 135 | |
|
| 0 | 136 | | if(_memoryCache.TryGetValue<RobotClientsRobotCommands>(GetRobotCommandsKey(robotId), out var robotCommands)) |
| 0 | 137 | | { |
| 0 | 138 | | if (robotCommands != null) |
| 0 | 139 | | { |
| 0 | 140 | | await _bus.Publish(new RobotCommandsContract { |
| 0 | 141 | | RobotId = robotId, |
| 0 | 142 | | RealmId = realmId, |
| 0 | 143 | | Commands = new RobotCommands { |
| 0 | 144 | | AbortTask = robotCommands.AbortTask, |
| 0 | 145 | | PauseTaskAssigement = robotCommands.PauseTaskAssigement, |
| 0 | 146 | | SoftwareEmergencyStop = robotCommands.SoftwareEmergencyStop, |
| 0 | 147 | | RenewCertificate = robotCommands.RenewCertificate |
| 0 | 148 | | } |
| 0 | 149 | | }); |
| 0 | 150 | | } |
| 0 | 151 | | } |
| | 152 | |
|
| 0 | 153 | | if (robotStatus == RobotStatus.Stuck) |
| 0 | 154 | | { |
| 0 | 155 | | if (!_memoryCache.TryGetValue<bool>($"OnlineRobotsService_RobotStuck_{robotId}", out var _)) |
| 0 | 156 | | { |
| | 157 | | // First stuck in 5 minutes, sending email |
| 0 | 158 | | await _emailService.SendRobotStuckEmailAsync(robotId, data.Position.X, data.Position.Y); |
| 0 | 159 | | } |
| 0 | 160 | | _memoryCache.Set($"OnlineRobotsService_RobotStuck_{robotId}", true, TimeSpan.FromMinutes(5)); |
| 0 | 161 | | } |
| 0 | 162 | | } |
| | 163 | |
|
| | 164 | | private async Task SetRobotCommandsAsync(Guid robotId, RobotClientsRobotCommands commands) |
| 0 | 165 | | { |
| 0 | 166 | | _memoryCache.Set(GetRobotCommandsKey(robotId), commands); |
| 0 | 167 | | var realmId = await _robotService.GetRobotRealmIdAsync(robotId) ?? 0; |
| 0 | 168 | | await _bus.Publish(new RobotCommandsContract { |
| 0 | 169 | | RobotId = robotId, |
| 0 | 170 | | RealmId = realmId, |
| 0 | 171 | | Commands = new RobotCommands { |
| 0 | 172 | | AbortTask = commands.AbortTask, |
| 0 | 173 | | PauseTaskAssigement = commands.PauseTaskAssigement, |
| 0 | 174 | | SoftwareEmergencyStop = commands.SoftwareEmergencyStop, |
| 0 | 175 | | RenewCertificate = commands.RenewCertificate |
| 0 | 176 | | } |
| 0 | 177 | | }); |
| 0 | 178 | | } |
| | 179 | |
|
| | 180 | | public RobotClientsRobotCommands? GetRobotCommands(Guid robotId) |
| 0 | 181 | | { |
| 0 | 182 | | return _memoryCache.Get<RobotClientsRobotCommands>(GetRobotCommandsKey(robotId)); |
| 0 | 183 | | } |
| | 184 | |
|
| | 185 | | public async Task<bool> IsRobotOnlineAsync(Guid robotId) |
| 0 | 186 | | { |
| 0 | 187 | | var realmId = await _robotService.GetRobotRealmIdAsync(robotId) ?? 0; |
| 0 | 188 | | var onlineRobotsIds = _memoryCache.Get<HashSet<Guid>>(GetOnlineRobotsKey((int)realmId)); |
| 0 | 189 | | return onlineRobotsIds != null && onlineRobotsIds.Contains(robotId); |
| 0 | 190 | | } |
| | 191 | |
|
| | 192 | | public async Task<bool> SetAbortTaskAsync(Guid robotId, bool enable) |
| 0 | 193 | | { |
| 0 | 194 | | var robotCommands = GetRobotCommands(robotId); |
| 0 | 195 | | if (robotCommands != null) |
| 0 | 196 | | { |
| 0 | 197 | | robotCommands.AbortTask = enable; |
| 0 | 198 | | await SetRobotCommandsAsync(robotId, robotCommands); |
| 0 | 199 | | _eventService.RobotCommandsHasUpdated(robotId); |
| 0 | 200 | | return true; |
| | 201 | | } |
| | 202 | | else |
| 0 | 203 | | { |
| 0 | 204 | | return false; |
| | 205 | | } |
| 0 | 206 | | } |
| | 207 | |
|
| | 208 | | public async Task<bool> SetSoftwareEmergencyStopAsync(Guid robotId, bool enable) |
| 0 | 209 | | { |
| 0 | 210 | | var robotCommands = GetRobotCommands(robotId); |
| 0 | 211 | | if (robotCommands != null) |
| 0 | 212 | | { |
| 0 | 213 | | robotCommands.SoftwareEmergencyStop = enable; |
| 0 | 214 | | await SetRobotCommandsAsync(robotId, robotCommands); |
| 0 | 215 | | _eventService.RobotCommandsHasUpdated(robotId); |
| 0 | 216 | | return true; |
| | 217 | | } |
| | 218 | | else |
| 0 | 219 | | { |
| 0 | 220 | | return false; |
| | 221 | | } |
| 0 | 222 | | } |
| | 223 | |
|
| | 224 | | public async Task<bool> SetPauseTaskAssigementAsync(Guid robotId, bool enable) |
| 0 | 225 | | { |
| 0 | 226 | | var robotCommands = GetRobotCommands(robotId); |
| 0 | 227 | | if (robotCommands != null) |
| 0 | 228 | | { |
| 0 | 229 | | robotCommands.PauseTaskAssigement = enable; |
| 0 | 230 | | await SetRobotCommandsAsync(robotId, robotCommands); |
| 0 | 231 | | _eventService.RobotCommandsHasUpdated(robotId); |
| 0 | 232 | | return true; |
| | 233 | | } |
| | 234 | | else |
| 0 | 235 | | { |
| 0 | 236 | | return false; |
| | 237 | | } |
| 0 | 238 | | } |
| | 239 | |
|
| | 240 | | public bool GetPauseAutoTaskAssignment(Guid robotId) |
| 0 | 241 | | { |
| 0 | 242 | | var robotCommands = GetRobotCommands(robotId); |
| 0 | 243 | | if (robotCommands != null) |
| 0 | 244 | | { |
| 0 | 245 | | return robotCommands.PauseTaskAssigement; |
| | 246 | | } |
| | 247 | | else |
| 0 | 248 | | { |
| 0 | 249 | | return false; |
| | 250 | | } |
| 0 | 251 | | } |
| | 252 | |
|
| | 253 | | public void SetAutoTaskNextApi(Guid robotId, AutoTask autoTask) |
| 0 | 254 | | { |
| 0 | 255 | | _memoryCache.Set($"OnlineRobotsService_RobotHasNextTask_{robotId}", autoTask); |
| 0 | 256 | | _eventService.RobotHasNextTaskTriggered(robotId); |
| 0 | 257 | | } |
| | 258 | |
|
| | 259 | | public AutoTask? GetAutoTaskNextApi(Guid robotId) |
| 0 | 260 | | { |
| 0 | 261 | | if (_memoryCache.TryGetValue($"OnlineRobotsService_RobotHasNextTask_{robotId}", out AutoTask? autoTask)) |
| 0 | 262 | | { |
| 0 | 263 | | _memoryCache.Remove($"OnlineRobotsService_RobotHasNextTask_{robotId}"); |
| 0 | 264 | | return autoTask; |
| | 265 | | } |
| | 266 | | else |
| 0 | 267 | | { |
| 0 | 268 | | return null; |
| | 269 | | } |
| 0 | 270 | | } |
| | 271 | | } |