|  |  | 1 |  | using LGDXRobotCloud.API.Repositories; | 
|  |  | 2 |  | using LGDXRobotCloud.Data.Models.Redis; | 
|  |  | 3 |  | using LGDXRobotCloud.Protos; | 
|  |  | 4 |  | using LGDXRobotCloud.Utilities.Enums; | 
|  |  | 5 |  |  | 
|  |  | 6 |  | namespace LGDXRobotCloud.API.Services.Navigation; | 
|  |  | 7 |  |  | 
|  |  | 8 |  | public interface ISlamService | 
|  |  | 9 |  | { | 
|  |  | 10 |  |   Task<bool> StartSlamAsync(Guid robotId); | 
|  |  | 11 |  |   Task StopSlamAsync(Guid robotId); | 
|  |  | 12 |  |  | 
|  |  | 13 |  |   // Client to Server | 
|  |  | 14 |  |   Task UpdateSlamDataAsync(Guid robotId, RobotClientsSlamStatus status, RobotClientsMapData? mapData); | 
|  |  | 15 |  |  | 
|  |  | 16 |  |   // Server to Client | 
|  |  | 17 |  |   Task<bool> AddSlamCommandAsync(int realmId, RobotClientsSlamCommands commands); | 
|  |  | 18 |  | } | 
|  |  | 19 |  |  | 
|  | 0 | 20 |  | public class SlamService( | 
|  | 0 | 21 |  |   ISlamDataRepository slamDataRepository, | 
|  | 0 | 22 |  |   IRobotService robotService | 
|  | 0 | 23 |  | ) : ISlamService | 
|  |  | 24 |  | { | 
|  | 0 | 25 |  |   private readonly ISlamDataRepository _slamDataRepository = slamDataRepository ?? throw new ArgumentNullException(nameo | 
|  | 0 | 26 |  |   private readonly IRobotService _robotService = robotService ?? throw new ArgumentNullException(nameof(robotService)); | 
|  |  | 27 |  |  | 
|  |  | 28 |  |   public async Task<bool> StartSlamAsync(Guid robotId) | 
|  | 0 | 29 |  |   { | 
|  | 0 | 30 |  |     var realmId = await _robotService.GetRobotRealmIdAsync(robotId) ?? 0; | 
|  | 0 | 31 |  |     return await _slamDataRepository.StartSlamAsync(realmId, robotId); | 
|  | 0 | 32 |  |   } | 
|  |  | 33 |  |  | 
|  |  | 34 |  |   public async Task StopSlamAsync(Guid robotId) | 
|  | 0 | 35 |  |   { | 
|  | 0 | 36 |  |     var realmId = await _robotService.GetRobotRealmIdAsync(robotId) ?? 0; | 
|  | 0 | 37 |  |     await _slamDataRepository.StopSlamAsync(realmId, robotId); | 
|  | 0 | 38 |  |   } | 
|  |  | 39 |  |  | 
|  |  | 40 |  |   public async Task UpdateSlamDataAsync(Guid robotId, RobotClientsSlamStatus status, RobotClientsMapData? mapData) | 
|  | 0 | 41 |  |   { | 
|  | 0 | 42 |  |     var realmId = await _robotService.GetRobotRealmIdAsync(robotId) ?? 0; | 
|  | 0 | 43 |  |     MapData? map = null; | 
|  | 0 | 44 |  |     if (mapData != null && mapData.Data.Count > 0) | 
|  | 0 | 45 |  |     { | 
|  | 0 | 46 |  |       map = new MapData | 
|  | 0 | 47 |  |       { | 
|  | 0 | 48 |  |         Resolution = mapData.Resolution, | 
|  | 0 | 49 |  |         Width = mapData.Width, | 
|  | 0 | 50 |  |         Height = mapData.Height, | 
|  | 0 | 51 |  |         Origin = new RobotDof | 
|  | 0 | 52 |  |         { | 
|  | 0 | 53 |  |           X = mapData.Origin.X, | 
|  | 0 | 54 |  |           Y = mapData.Origin.Y, | 
|  | 0 | 55 |  |           Rotation = mapData.Origin.Rotation, | 
|  | 0 | 56 |  |         }, | 
|  | 0 | 57 |  |         Data = [.. mapData.Data.Select(x => (short)x)] | 
|  | 0 | 58 |  |       }; | 
|  | 0 | 59 |  |     } | 
|  | 0 | 60 |  |     var data = new SlamData | 
|  | 0 | 61 |  |     { | 
|  | 0 | 62 |  |       RobotId = robotId, | 
|  | 0 | 63 |  |       RealmId = realmId, | 
|  | 0 | 64 |  |       SlamStatus = (SlamStatus)status, | 
|  | 0 | 65 |  |       MapData = map | 
|  | 0 | 66 |  |     }; | 
|  | 0 | 67 |  |     await _slamDataRepository.SetSlamExchangeAsync(realmId, data); | 
|  | 0 | 68 |  |   } | 
|  |  | 69 |  |  | 
|  |  | 70 |  |   public async Task<bool> AddSlamCommandAsync(int realmId, RobotClientsSlamCommands commands) | 
|  | 0 | 71 |  |   { | 
|  | 0 | 72 |  |     return await _slamDataRepository.AddSlamCommandAsync(realmId, commands); | 
|  | 0 | 73 |  |   } | 
|  |  | 74 |  | } |