| | 1 | | using Grpc.Core; |
| | 2 | | using LGDXRobotCloud.API.Configurations; |
| | 3 | | using LGDXRobotCloud.API.Services.Navigation; |
| | 4 | | using LGDXRobotCloud.Protos; |
| | 5 | | using LGDXRobotCloud.Utilities.Constants; |
| | 6 | | using LGDXRobotCloud.Utilities.Enums; |
| | 7 | | using Microsoft.AspNetCore.Authorization; |
| | 8 | | using Microsoft.Extensions.Options; |
| | 9 | | using Microsoft.IdentityModel.Tokens; |
| | 10 | | using static LGDXRobotCloud.Protos.RobotClientsService; |
| | 11 | | using System.IdentityModel.Tokens.Jwt; |
| | 12 | | using System.Security.Claims; |
| | 13 | | using System.Text; |
| | 14 | | using LGDXRobotCloud.Data.Models.Business.Navigation; |
| | 15 | | using LGDXRobotCloud.API.Services.Automation; |
| | 16 | | using System.Threading.Channels; |
| | 17 | | using LGDXRobotCloud.API.Services.Common; |
| | 18 | | using LGDXRobotCloud.API.Authorisation; |
| | 19 | |
|
| | 20 | | namespace LGDXRobotCloud.API.Services; |
| | 21 | |
|
| | 22 | | [Authorize(AuthenticationSchemes = LgdxRobotCloudAuthenticationSchemes.RobotClientsJwtScheme)] |
| 14 | 23 | | public class RobotClientsService( |
| 14 | 24 | | IAutoTaskSchedulerService autoTaskSchedulerService, |
| 14 | 25 | | IEventService eventService, |
| 14 | 26 | | IOnlineRobotsService OnlineRobotsService, |
| 14 | 27 | | IOptionsSnapshot<LgdxRobotCloudSecretConfiguration> lgdxRobotCloudSecretConfiguration, |
| 14 | 28 | | IRobotService robotService |
| 14 | 29 | | ) : RobotClientsServiceBase |
| | 30 | | { |
| 14 | 31 | | private readonly IAutoTaskSchedulerService _autoTaskSchedulerService = autoTaskSchedulerService; |
| 14 | 32 | | private readonly IEventService _eventService = eventService; |
| 14 | 33 | | private readonly IOnlineRobotsService _onlineRobotsService = OnlineRobotsService; |
| 14 | 34 | | private readonly LgdxRobotCloudSecretConfiguration _lgdxRobotCloudSecretConfiguration = lgdxRobotCloudSecretConfigurat |
| 14 | 35 | | private readonly IRobotService _robotService = robotService; |
| 14 | 36 | | private Guid _streamingRobotId = Guid.Empty; |
| 14 | 37 | | private RobotClientsRobotStatus _streamingRobotStatus = RobotClientsRobotStatus.Offline; |
| 14 | 38 | | private readonly Channel<RobotClientsRespond> _streamMessageQueue = Channel.CreateUnbounded<RobotClientsRespond>(); |
| | 39 | |
|
| | 40 | | private static Guid GetRobotId(ServerCallContext context) |
| 7 | 41 | | { |
| 7 | 42 | | var robotClaim = context.GetHttpContext().User.FindFirst(ClaimTypes.NameIdentifier); |
| 7 | 43 | | return Guid.Parse(robotClaim!.Value); |
| 7 | 44 | | } |
| | 45 | |
|
| | 46 | | [Authorize(AuthenticationSchemes = LgdxRobotCloudAuthenticationSchemes.RobotClientsCertificateScheme)] |
| | 47 | | public override async Task<RobotClientsGreetRespond> Greet(RobotClientsGreet request, ServerCallContext context) |
| 7 | 48 | | { |
| 7 | 49 | | var robotClaim = context.GetHttpContext().User.FindFirst(ClaimTypes.NameIdentifier); |
| 7 | 50 | | if (robotClaim == null || !Guid.TryParse(robotClaim.Value, out var robotId)) |
| 2 | 51 | | return new RobotClientsGreetRespond { |
| 2 | 52 | | Status = RobotClientsResultStatus.Failed, |
| 2 | 53 | | AccessToken = string.Empty |
| 2 | 54 | | }; |
| | 55 | |
|
| 5 | 56 | | var robotIdGuid = robotId; |
| 5 | 57 | | var robot = await _robotService.GetRobotAsync(robotIdGuid); |
| 5 | 58 | | if (robot == null) |
| 1 | 59 | | return new RobotClientsGreetRespond { |
| 1 | 60 | | Status = RobotClientsResultStatus.Failed, |
| 1 | 61 | | AccessToken = string.Empty |
| 1 | 62 | | }; |
| | 63 | |
|
| | 64 | | // Compare System Info |
| 4 | 65 | | var incomingSystemInfo = new RobotSystemInfoBusinessModel { |
| 4 | 66 | | Id = 0, |
| 4 | 67 | | Cpu = request.SystemInfo.Cpu, |
| 4 | 68 | | IsLittleEndian = request.SystemInfo.IsLittleEndian, |
| 4 | 69 | | Motherboard = request.SystemInfo.Motherboard, |
| 4 | 70 | | MotherboardSerialNumber = request.SystemInfo.MotherboardSerialNumber, |
| 4 | 71 | | RamMiB = request.SystemInfo.RamMiB, |
| 4 | 72 | | Gpu = request.SystemInfo.Gpu, |
| 4 | 73 | | Os = request.SystemInfo.Os, |
| 4 | 74 | | Is32Bit = request.SystemInfo.Is32Bit, |
| 4 | 75 | | McuSerialNumber = request.SystemInfo.McuSerialNumber, |
| 4 | 76 | | }; |
| 4 | 77 | | var systemInfo = robot.RobotSystemInfo; |
| 4 | 78 | | if (systemInfo == null) |
| 1 | 79 | | { |
| | 80 | | // Create Robot System Info for the first time |
| 1 | 81 | | await _robotService.CreateRobotSystemInfoAsync(robotIdGuid, incomingSystemInfo.ToCreateBusinessModel()); |
| 1 | 82 | | } |
| | 83 | | else |
| 3 | 84 | | { |
| | 85 | | // Hardware Protection |
| 3 | 86 | | if (robot.IsProtectingHardwareSerialNumber && incomingSystemInfo.MotherboardSerialNumber != systemInfo.Motherboard |
| 1 | 87 | | { |
| 1 | 88 | | return new RobotClientsGreetRespond { |
| 1 | 89 | | Status = RobotClientsResultStatus.Failed, |
| 1 | 90 | | AccessToken = string.Empty |
| 1 | 91 | | }; |
| | 92 | | } |
| 2 | 93 | | if (robot.IsProtectingHardwareSerialNumber && incomingSystemInfo.McuSerialNumber != systemInfo.McuSerialNumber) |
| 1 | 94 | | { |
| 1 | 95 | | return new RobotClientsGreetRespond { |
| 1 | 96 | | Status = RobotClientsResultStatus.Failed, |
| 1 | 97 | | AccessToken = string.Empty |
| 1 | 98 | | }; |
| | 99 | | } |
| 1 | 100 | | await _robotService.UpdateRobotSystemInfoAsync(robotIdGuid, incomingSystemInfo.ToUpdateBusinessModel()); |
| 1 | 101 | | } |
| | 102 | |
|
| 2 | 103 | | var chassisInfo = await _robotService.GetRobotChassisInfoAsync(robotIdGuid); |
| | 104 | |
|
| | 105 | | // Generate Access Token |
| 2 | 106 | | var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_lgdxRobotCloudSecretConfiguration.RobotClientsJwt |
| 2 | 107 | | var credentials = new SigningCredentials(securityKey, _lgdxRobotCloudSecretConfiguration.RobotClientsJwtAlgorithm); |
| 2 | 108 | | var secToken = new JwtSecurityToken( |
| 2 | 109 | | _lgdxRobotCloudSecretConfiguration.RobotClientsJwtIssuer, |
| 2 | 110 | | _lgdxRobotCloudSecretConfiguration.RobotClientsJwtIssuer, |
| 2 | 111 | | [new Claim(ClaimTypes.NameIdentifier, robot.Id.ToString())], |
| 2 | 112 | | DateTime.UtcNow, |
| 2 | 113 | | DateTime.UtcNow.AddMinutes(_lgdxRobotCloudSecretConfiguration.RobotClientsJwtExpireMins), |
| 2 | 114 | | credentials); |
| 2 | 115 | | var token = new JwtSecurityTokenHandler().WriteToken(secToken); |
| | 116 | |
|
| 2 | 117 | | await _onlineRobotsService.AddRobotAsync(robotId); |
| | 118 | |
|
| 2 | 119 | | return new RobotClientsGreetRespond |
| 2 | 120 | | { |
| 2 | 121 | | Status = RobotClientsResultStatus.Success, |
| 2 | 122 | | AccessToken = token, |
| 2 | 123 | | IsRealtimeExchange = robot.IsRealtimeExchange, |
| 2 | 124 | | ChassisInfo = new RobotClientsChassisInfo { |
| 2 | 125 | | RobotTypeId = chassisInfo!.RobotTypeId, |
| 2 | 126 | | ChassisLX = chassisInfo.ChassisLengthX, |
| 2 | 127 | | ChassisLY = chassisInfo.ChassisLengthY, |
| 2 | 128 | | ChassisWheelCount = chassisInfo.ChassisWheelCount, |
| 2 | 129 | | ChassisWheelRadius = chassisInfo.ChassisWheelRadius, |
| 2 | 130 | | BatteryCount = chassisInfo.BatteryCount, |
| 2 | 131 | | BatteryMaxVoltage = chassisInfo.BatteryMaxVoltage, |
| 2 | 132 | | BatteryMinVoltage = chassisInfo.BatteryMinVoltage, |
| 2 | 133 | | } |
| 2 | 134 | | }; |
| 7 | 135 | | } |
| | 136 | |
|
| | 137 | | [RobotClientShouldOnline] |
| | 138 | | public override async Task<RobotClientsRespond> Exchange(RobotClientsExchange request, ServerCallContext context) |
| 3 | 139 | | { |
| 3 | 140 | | var robotId = GetRobotId(context); |
| | 141 | |
|
| 3 | 142 | | await _onlineRobotsService.UpdateRobotDataAsync(robotId, request); |
| 3 | 143 | | var manualAutoTask = _onlineRobotsService.GetAutoTaskNextApi(robotId); |
| 3 | 144 | | if (manualAutoTask != null) |
| 1 | 145 | | { |
| | 146 | | // Triggered by API |
| 1 | 147 | | return new RobotClientsRespond { |
| 1 | 148 | | Status = RobotClientsResultStatus.Success, |
| 1 | 149 | | Commands = _onlineRobotsService.GetRobotCommands(robotId), |
| 1 | 150 | | Task = await _autoTaskSchedulerService.AutoTaskNextConstructAsync(manualAutoTask) |
| 1 | 151 | | }; |
| | 152 | | } |
| | 153 | | else |
| 2 | 154 | | { |
| | 155 | | // Get AutoTask |
| 2 | 156 | | RobotClientsAutoTask? task = null; |
| 2 | 157 | | if (request.RobotStatus == RobotClientsRobotStatus.Idle) |
| 1 | 158 | | { |
| 1 | 159 | | task = await _autoTaskSchedulerService.GetAutoTaskAsync(robotId); |
| 1 | 160 | | } |
| 2 | 161 | | return new RobotClientsRespond { |
| 2 | 162 | | Status = RobotClientsResultStatus.Success, |
| 2 | 163 | | Commands = _onlineRobotsService.GetRobotCommands(robotId), |
| 2 | 164 | | Task = task |
| 2 | 165 | | }; |
| | 166 | | } |
| 3 | 167 | | } |
| | 168 | |
|
| | 169 | | [RobotClientShouldOnline] |
| | 170 | | public override async Task ExchangeStream(IAsyncStreamReader<RobotClientsExchange> requestStream, IServerStreamWriter< |
| 0 | 171 | | { |
| 0 | 172 | | var robotId = GetRobotId(context); |
| | 173 | |
|
| 0 | 174 | | _streamingRobotId = robotId; |
| 0 | 175 | | _eventService.RobotCommandsUpdated += OnRobotCommandsUpdated; |
| 0 | 176 | | _eventService.RobotHasNextTask += OnRobotHasNextTask; |
| 0 | 177 | | _eventService.AutoTaskCreated += OnAutoTaskCreated; |
| | 178 | |
|
| 0 | 179 | | var clientToServer = ExchangeStreamClientToServerAsync(robotId, requestStream, context); |
| 0 | 180 | | var serverToClient = ExchangeStreamServerToClientAsync(responseStream, context); |
| 0 | 181 | | await Task.WhenAll(clientToServer, serverToClient); |
| 0 | 182 | | } |
| | 183 | |
|
| | 184 | | private async Task ExchangeStreamClientToServerAsync(Guid robotId, IAsyncStreamReader<RobotClientsExchange> requestStr |
| 0 | 185 | | { |
| 0 | 186 | | while (await requestStream.MoveNext(CancellationToken.None) && !context.CancellationToken.IsCancellationRequested) |
| 0 | 187 | | { |
| 0 | 188 | | var request = requestStream.Current; |
| 0 | 189 | | _streamingRobotStatus = request.RobotStatus; |
| | 190 | | // Get auto task |
| 0 | 191 | | if (request.RobotStatus == RobotClientsRobotStatus.Idle) |
| 0 | 192 | | { |
| 0 | 193 | | var task = await _autoTaskSchedulerService.GetAutoTaskAsync(robotId); |
| 0 | 194 | | if (task != null) |
| 0 | 195 | | { |
| 0 | 196 | | await _streamMessageQueue.Writer.WriteAsync(new RobotClientsRespond { |
| 0 | 197 | | Status = RobotClientsResultStatus.Success, |
| 0 | 198 | | Commands = _onlineRobotsService.GetRobotCommands(_streamingRobotId), |
| 0 | 199 | | Task = task |
| 0 | 200 | | }); |
| 0 | 201 | | } |
| 0 | 202 | | } |
| 0 | 203 | | await _onlineRobotsService.UpdateRobotDataAsync(robotId, request); |
| 0 | 204 | | } |
| | 205 | |
|
| | 206 | | // The reading stream is completed, stop wirting task |
| 0 | 207 | | _eventService.RobotCommandsUpdated -= OnRobotCommandsUpdated; |
| 0 | 208 | | _eventService.RobotHasNextTask -= OnRobotHasNextTask; |
| 0 | 209 | | _eventService.AutoTaskCreated -= OnAutoTaskCreated; |
| | 210 | |
|
| | 211 | | // Assume the robot going offline |
| 0 | 212 | | await _onlineRobotsService.RemoveRobotAsync(robotId); |
| | 213 | |
|
| 0 | 214 | | _streamMessageQueue.Writer.TryComplete(); |
| 0 | 215 | | await _streamMessageQueue.Reader.Completion; |
| 0 | 216 | | } |
| | 217 | |
|
| | 218 | | private async Task ExchangeStreamServerToClientAsync(IServerStreamWriter<RobotClientsRespond> responseStream, ServerCa |
| 0 | 219 | | { |
| 0 | 220 | | await foreach (var message in _streamMessageQueue.Reader.ReadAllAsync(context.CancellationToken)) |
| 0 | 221 | | { |
| 0 | 222 | | await responseStream.WriteAsync(message); |
| 0 | 223 | | } |
| 0 | 224 | | } |
| | 225 | |
|
| | 226 | | private async void OnRobotCommandsUpdated(object? sender, Guid robotId) |
| 0 | 227 | | { |
| 0 | 228 | | if (_streamingRobotId != robotId) |
| 0 | 229 | | return; |
| 0 | 230 | | await _streamMessageQueue.Writer.WriteAsync(new RobotClientsRespond { |
| 0 | 231 | | Status = RobotClientsResultStatus.Success, |
| 0 | 232 | | Commands = _onlineRobotsService.GetRobotCommands(_streamingRobotId), |
| 0 | 233 | | }); |
| 0 | 234 | | } |
| | 235 | |
|
| | 236 | | private async void OnAutoTaskCreated(object? sender, EventArgs e) |
| 0 | 237 | | { |
| | 238 | | // Read |
| 0 | 239 | | if (_streamingRobotStatus != RobotClientsRobotStatus.Idle) |
| 0 | 240 | | return; |
| 0 | 241 | | var autoTask = await _autoTaskSchedulerService.GetAutoTaskAsync(_streamingRobotId); |
| 0 | 242 | | if (autoTask != null) |
| 0 | 243 | | { |
| 0 | 244 | | await _streamMessageQueue.Writer.WriteAsync(new RobotClientsRespond { |
| 0 | 245 | | Status = RobotClientsResultStatus.Success, |
| 0 | 246 | | Commands = _onlineRobotsService.GetRobotCommands(_streamingRobotId), |
| 0 | 247 | | Task = autoTask |
| 0 | 248 | | }); |
| 0 | 249 | | } |
| 0 | 250 | | } |
| | 251 | |
|
| | 252 | | private async void OnRobotHasNextTask(object? sender, Guid robotId) |
| 0 | 253 | | { |
| | 254 | | // When an auto task is completed by API |
| 0 | 255 | | if (_streamingRobotId != robotId) |
| 0 | 256 | | return; |
| 0 | 257 | | var manualAutoTask = _onlineRobotsService.GetAutoTaskNextApi(robotId); |
| 0 | 258 | | RobotClientsAutoTask? task = null; |
| 0 | 259 | | if (manualAutoTask != null) |
| 0 | 260 | | { |
| 0 | 261 | | task = await _autoTaskSchedulerService.AutoTaskNextConstructAsync(manualAutoTask); |
| 0 | 262 | | } |
| 0 | 263 | | await _streamMessageQueue.Writer.WriteAsync(new RobotClientsRespond { |
| 0 | 264 | | Status = RobotClientsResultStatus.Success, |
| 0 | 265 | | Commands = _onlineRobotsService.GetRobotCommands(robotId), |
| 0 | 266 | | Task = task |
| 0 | 267 | | }); |
| 0 | 268 | | } |
| | 269 | |
|
| | 270 | | [RobotClientShouldOnline] |
| | 271 | | public override async Task<RobotClientsRespond> AutoTaskNext(RobotClientsNextToken request, ServerCallContext context) |
| 2 | 272 | | { |
| 2 | 273 | | var robotId = GetRobotId(context); |
| | 274 | |
|
| 2 | 275 | | var task = await _autoTaskSchedulerService.AutoTaskNextAsync(robotId, request.TaskId, request.NextToken); |
| 2 | 276 | | return new RobotClientsRespond { |
| 2 | 277 | | Status = task != null ? RobotClientsResultStatus.Success : RobotClientsResultStatus.Failed, |
| 2 | 278 | | Commands = _onlineRobotsService.GetRobotCommands(robotId), |
| 2 | 279 | | Task = task |
| 2 | 280 | | }; |
| 2 | 281 | | } |
| | 282 | |
|
| | 283 | | [RobotClientShouldOnline] |
| | 284 | | public override async Task<RobotClientsRespond> AutoTaskAbort(RobotClientsAbortToken request, ServerCallContext contex |
| 2 | 285 | | { |
| 2 | 286 | | var robotId = GetRobotId(context); |
| | 287 | |
|
| 2 | 288 | | await _onlineRobotsService.SetAbortTaskAsync(robotId, false); |
| | 289 | |
|
| 2 | 290 | | AutoTaskAbortReason autoTaskAbortReason = (AutoTaskAbortReason)(int)request.AbortReason; |
| 2 | 291 | | var task = await _autoTaskSchedulerService.AutoTaskAbortAsync(robotId, request.TaskId, request.NextToken, autoTaskAb |
| 2 | 292 | | return new RobotClientsRespond { |
| 2 | 293 | | Status = task != null ? RobotClientsResultStatus.Success : RobotClientsResultStatus.Failed, |
| 2 | 294 | | Commands = _onlineRobotsService.GetRobotCommands(robotId), |
| 2 | 295 | | Task = task |
| 2 | 296 | | }; |
| 2 | 297 | | } |
| | 298 | | } |