| | 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 | |
|
| | 103 | | // Generate Access Token |
| 2 | 104 | | var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_lgdxRobotCloudSecretConfiguration.RobotClientsJwt |
| 2 | 105 | | var credentials = new SigningCredentials(securityKey, _lgdxRobotCloudSecretConfiguration.RobotClientsJwtAlgorithm); |
| 2 | 106 | | var secToken = new JwtSecurityToken( |
| 2 | 107 | | _lgdxRobotCloudSecretConfiguration.RobotClientsJwtIssuer, |
| 2 | 108 | | _lgdxRobotCloudSecretConfiguration.RobotClientsJwtIssuer, |
| 2 | 109 | | [new Claim(ClaimTypes.NameIdentifier, robot.Id.ToString())], |
| 2 | 110 | | DateTime.UtcNow, |
| 2 | 111 | | DateTime.UtcNow.AddMinutes(_lgdxRobotCloudSecretConfiguration.RobotClientsJwtExpireMins), |
| 2 | 112 | | credentials); |
| 2 | 113 | | var token = new JwtSecurityTokenHandler().WriteToken(secToken); |
| | 114 | |
|
| 2 | 115 | | await _onlineRobotsService.AddRobotAsync(robotId); |
| | 116 | |
|
| 2 | 117 | | return new RobotClientsGreetRespond { |
| 2 | 118 | | Status = RobotClientsResultStatus.Success, |
| 2 | 119 | | AccessToken = token, |
| 2 | 120 | | IsRealtimeExchange = robot.IsRealtimeExchange |
| 2 | 121 | | }; |
| 7 | 122 | | } |
| | 123 | |
|
| | 124 | | [RobotClientShouldOnline] |
| | 125 | | public override async Task<RobotClientsRespond> Exchange(RobotClientsExchange request, ServerCallContext context) |
| 3 | 126 | | { |
| 3 | 127 | | var robotId = GetRobotId(context); |
| | 128 | |
|
| 3 | 129 | | await _onlineRobotsService.UpdateRobotDataAsync(robotId, request); |
| 3 | 130 | | var manualAutoTask = _onlineRobotsService.GetAutoTaskNextApi(robotId); |
| 3 | 131 | | if (manualAutoTask != null) |
| 1 | 132 | | { |
| | 133 | | // Triggered by API |
| 1 | 134 | | return new RobotClientsRespond { |
| 1 | 135 | | Status = RobotClientsResultStatus.Success, |
| 1 | 136 | | Commands = _onlineRobotsService.GetRobotCommands(robotId), |
| 1 | 137 | | Task = await _autoTaskSchedulerService.AutoTaskNextConstructAsync(manualAutoTask) |
| 1 | 138 | | }; |
| | 139 | | } |
| | 140 | | else |
| 2 | 141 | | { |
| | 142 | | // Get AutoTask |
| 2 | 143 | | RobotClientsAutoTask? task = null; |
| 2 | 144 | | if (request.RobotStatus == RobotClientsRobotStatus.Idle) |
| 1 | 145 | | { |
| 1 | 146 | | task = await _autoTaskSchedulerService.GetAutoTaskAsync(robotId); |
| 1 | 147 | | } |
| 2 | 148 | | return new RobotClientsRespond { |
| 2 | 149 | | Status = RobotClientsResultStatus.Success, |
| 2 | 150 | | Commands = _onlineRobotsService.GetRobotCommands(robotId), |
| 2 | 151 | | Task = task |
| 2 | 152 | | }; |
| | 153 | | } |
| 3 | 154 | | } |
| | 155 | |
|
| | 156 | | [RobotClientShouldOnline] |
| | 157 | | public override async Task ExchangeStream(IAsyncStreamReader<RobotClientsExchange> requestStream, IServerStreamWriter< |
| 0 | 158 | | { |
| 0 | 159 | | var robotId = GetRobotId(context); |
| | 160 | |
|
| 0 | 161 | | _streamingRobotId = robotId; |
| 0 | 162 | | _eventService.RobotCommandsUpdated += OnRobotCommandsUpdated; |
| 0 | 163 | | _eventService.RobotHasNextTask += OnRobotHasNextTask; |
| 0 | 164 | | _eventService.AutoTaskCreated += OnAutoTaskCreated; |
| | 165 | |
|
| 0 | 166 | | var clientToServer = ExchangeStreamClientToServerAsync(robotId, requestStream, context); |
| 0 | 167 | | var serverToClient = ExchangeStreamServerToClientAsync(responseStream, context); |
| 0 | 168 | | await Task.WhenAll(clientToServer, serverToClient); |
| 0 | 169 | | } |
| | 170 | |
|
| | 171 | | private async Task ExchangeStreamClientToServerAsync(Guid robotId, IAsyncStreamReader<RobotClientsExchange> requestStr |
| 0 | 172 | | { |
| 0 | 173 | | while (await requestStream.MoveNext(CancellationToken.None) && !context.CancellationToken.IsCancellationRequested) |
| 0 | 174 | | { |
| 0 | 175 | | var request = requestStream.Current; |
| 0 | 176 | | _streamingRobotStatus = request.RobotStatus; |
| | 177 | | // Get auto task |
| 0 | 178 | | if (request.RobotStatus == RobotClientsRobotStatus.Idle) |
| 0 | 179 | | { |
| 0 | 180 | | var task = await _autoTaskSchedulerService.GetAutoTaskAsync(robotId); |
| 0 | 181 | | if (task != null) |
| 0 | 182 | | { |
| 0 | 183 | | await _streamMessageQueue.Writer.WriteAsync(new RobotClientsRespond { |
| 0 | 184 | | Status = RobotClientsResultStatus.Success, |
| 0 | 185 | | Commands = _onlineRobotsService.GetRobotCommands(_streamingRobotId), |
| 0 | 186 | | Task = task |
| 0 | 187 | | }); |
| 0 | 188 | | } |
| 0 | 189 | | } |
| 0 | 190 | | await _onlineRobotsService.UpdateRobotDataAsync(robotId, request); |
| 0 | 191 | | } |
| | 192 | |
|
| | 193 | | // The reading stream is completed, stop wirting task |
| 0 | 194 | | _eventService.RobotCommandsUpdated -= OnRobotCommandsUpdated; |
| 0 | 195 | | _eventService.RobotHasNextTask -= OnRobotHasNextTask; |
| 0 | 196 | | _eventService.AutoTaskCreated -= OnAutoTaskCreated; |
| | 197 | |
|
| | 198 | | // Assume the robot going offline |
| 0 | 199 | | await _onlineRobotsService.RemoveRobotAsync(robotId); |
| | 200 | |
|
| 0 | 201 | | _streamMessageQueue.Writer.TryComplete(); |
| 0 | 202 | | await _streamMessageQueue.Reader.Completion; |
| 0 | 203 | | } |
| | 204 | |
|
| | 205 | | private async Task ExchangeStreamServerToClientAsync(IServerStreamWriter<RobotClientsRespond> responseStream, ServerCa |
| 0 | 206 | | { |
| 0 | 207 | | await foreach (var message in _streamMessageQueue.Reader.ReadAllAsync(context.CancellationToken)) |
| 0 | 208 | | { |
| 0 | 209 | | await responseStream.WriteAsync(message); |
| 0 | 210 | | } |
| 0 | 211 | | } |
| | 212 | |
|
| | 213 | | private async void OnRobotCommandsUpdated(object? sender, Guid robotId) |
| 0 | 214 | | { |
| 0 | 215 | | if (_streamingRobotId != robotId) |
| 0 | 216 | | return; |
| 0 | 217 | | await _streamMessageQueue.Writer.WriteAsync(new RobotClientsRespond { |
| 0 | 218 | | Status = RobotClientsResultStatus.Success, |
| 0 | 219 | | Commands = _onlineRobotsService.GetRobotCommands(_streamingRobotId), |
| 0 | 220 | | }); |
| 0 | 221 | | } |
| | 222 | |
|
| | 223 | | private async void OnAutoTaskCreated(object? sender, EventArgs e) |
| 0 | 224 | | { |
| | 225 | | // Read |
| 0 | 226 | | if (_streamingRobotStatus != RobotClientsRobotStatus.Idle) |
| 0 | 227 | | return; |
| 0 | 228 | | var autoTask = await _autoTaskSchedulerService.GetAutoTaskAsync(_streamingRobotId); |
| 0 | 229 | | if (autoTask != null) |
| 0 | 230 | | { |
| 0 | 231 | | await _streamMessageQueue.Writer.WriteAsync(new RobotClientsRespond { |
| 0 | 232 | | Status = RobotClientsResultStatus.Success, |
| 0 | 233 | | Commands = _onlineRobotsService.GetRobotCommands(_streamingRobotId), |
| 0 | 234 | | Task = autoTask |
| 0 | 235 | | }); |
| 0 | 236 | | } |
| 0 | 237 | | } |
| | 238 | |
|
| | 239 | | private async void OnRobotHasNextTask(object? sender, Guid robotId) |
| 0 | 240 | | { |
| | 241 | | // When an auto task is completed by API |
| 0 | 242 | | if (_streamingRobotId != robotId) |
| 0 | 243 | | return; |
| 0 | 244 | | var manualAutoTask = _onlineRobotsService.GetAutoTaskNextApi(robotId); |
| 0 | 245 | | RobotClientsAutoTask? task = null; |
| 0 | 246 | | if (manualAutoTask != null) |
| 0 | 247 | | { |
| 0 | 248 | | task = await _autoTaskSchedulerService.AutoTaskNextConstructAsync(manualAutoTask); |
| 0 | 249 | | } |
| 0 | 250 | | await _streamMessageQueue.Writer.WriteAsync(new RobotClientsRespond { |
| 0 | 251 | | Status = RobotClientsResultStatus.Success, |
| 0 | 252 | | Commands = _onlineRobotsService.GetRobotCommands(robotId), |
| 0 | 253 | | Task = task |
| 0 | 254 | | }); |
| 0 | 255 | | } |
| | 256 | |
|
| | 257 | | [RobotClientShouldOnline] |
| | 258 | | public override async Task<RobotClientsRespond> AutoTaskNext(RobotClientsNextToken request, ServerCallContext context) |
| 2 | 259 | | { |
| 2 | 260 | | var robotId = GetRobotId(context); |
| | 261 | |
|
| 2 | 262 | | var task = await _autoTaskSchedulerService.AutoTaskNextAsync(robotId, request.TaskId, request.NextToken); |
| 2 | 263 | | return new RobotClientsRespond { |
| 2 | 264 | | Status = task != null ? RobotClientsResultStatus.Success : RobotClientsResultStatus.Failed, |
| 2 | 265 | | Commands = _onlineRobotsService.GetRobotCommands(robotId), |
| 2 | 266 | | Task = task |
| 2 | 267 | | }; |
| 2 | 268 | | } |
| | 269 | |
|
| | 270 | | [RobotClientShouldOnline] |
| | 271 | | public override async Task<RobotClientsRespond> AutoTaskAbort(RobotClientsAbortToken request, ServerCallContext contex |
| 2 | 272 | | { |
| 2 | 273 | | var robotId = GetRobotId(context); |
| | 274 | |
|
| 2 | 275 | | await _onlineRobotsService.SetAbortTaskAsync(robotId, false); |
| | 276 | |
|
| 2 | 277 | | AutoTaskAbortReason autoTaskAbortReason = (AutoTaskAbortReason)(int)request.AbortReason; |
| 2 | 278 | | var task = await _autoTaskSchedulerService.AutoTaskAbortAsync(robotId, request.TaskId, request.NextToken, autoTaskAb |
| 2 | 279 | | return new RobotClientsRespond { |
| 2 | 280 | | Status = task != null ? RobotClientsResultStatus.Success : RobotClientsResultStatus.Failed, |
| 2 | 281 | | Commands = _onlineRobotsService.GetRobotCommands(robotId), |
| 2 | 282 | | Task = task |
| 2 | 283 | | }; |
| 2 | 284 | | } |
| | 285 | | } |