< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 66
Coverable lines: 66
Total lines: 114
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 8
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
File 1: .cctor()100%210%
File 2: .ctor(...)0%2040%
File 2: StartSlamAsync()0%620%
File 2: StopSlamAsync()100%210%
File 2: SetSlamExchangeAsync()100%210%
File 2: AddSlamCommandAsync()0%620%

File(s)

/builds/yukaitung/lgdxrobot2-cloud/LGDXRobotCloud.API/obj/Debug/net9.0/Microsoft.Extensions.Logging.Generators/Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator/LoggerMessage.g.cs

File '/builds/yukaitung/lgdxrobot2-cloud/LGDXRobotCloud.API/obj/Debug/net9.0/Microsoft.Extensions.Logging.Generators/Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator/LoggerMessage.g.cs' does not exist (any more).

/builds/yukaitung/lgdxrobot2-cloud/LGDXRobotCloud.API/Repositories/SlamDataRepository.cs

#LineLine coverage
 1using LGDXRobotCloud.Data.Models.Redis;
 2using LGDXRobotCloud.Protos;
 3using LGDXRobotCloud.Utilities.Helpers;
 4using NRedisStack;
 5using NRedisStack.RedisStackCommands;
 6using StackExchange.Redis;
 7using static StackExchange.Redis.RedisChannel;
 8
 9namespace LGDXRobotCloud.API.Repositories;
 10
 11public interface ISlamDataRepository
 12{
 13  Task<bool> StartSlamAsync(int realmId, Guid robotId);
 14  Task StopSlamAsync(int realmId, Guid robotId);
 15  Task SetSlamExchangeAsync(int realmId, SlamData exchange);
 16  Task<bool> AddSlamCommandAsync(int realmId, RobotClientsSlamCommands commands);
 17}
 18
 019public partial class SlamDataRepository(
 020    IConnectionMultiplexer redisConnection,
 021    ILogger<SlamDataRepository> logger,
 022    IRobotDataRepository robotDataRepository
 023  ) : ISlamDataRepository
 24{
 025  private readonly IConnectionMultiplexer _redisConnection = redisConnection ?? throw new ArgumentNullException(nameof(r
 026  private readonly IRobotDataRepository _robotDataRepository = robotDataRepository ?? throw new ArgumentNullException(na
 27
 28  [LoggerMessage(EventId = 0, Level = LogLevel.Error, Message = "Redis SlamDataRepository Exception: {Msg}")]
 29  public partial void LogException(string msg);
 30
 31  public async Task<bool> StartSlamAsync(int realmId, Guid robotId)
 032  {
 033    var db = _redisConnection.GetDatabase();
 34    try
 035    {
 036      bool result = await db.JSON().SetAsync(RedisHelper.GetSlamData(realmId), "$", new SlamData(), When.NotExists);
 037      if (!result)
 038      {
 39        // Only one robot can running SLAM at a time
 040        return false;
 41      }
 042      await db.KeyExpireAsync(RedisHelper.GetSlamData(realmId), TimeSpan.FromMinutes(5));
 043    }
 044    catch (Exception ex)
 045    {
 046      LogException(ex.Message);
 047      return false;
 48    }
 049    await _robotDataRepository.StartExchangeAsync(realmId, robotId);
 050    return true;
 051  }
 52
 53  public async Task StopSlamAsync(int realmId, Guid robotId)
 054  {
 055    var db = _redisConnection.GetDatabase();
 56    try
 057    {
 058      await db.KeyDeleteAsync(RedisHelper.GetSlamData(realmId));
 059    }
 060    catch (Exception ex)
 061    {
 062      LogException(ex.Message);
 063    }
 064    await _robotDataRepository.StopExchangeAsync(realmId, robotId);
 065  }
 66
 67  public async Task SetSlamExchangeAsync(int realmId, SlamData exchange)
 068  {
 069    var db = _redisConnection.GetDatabase();
 070    var pipeline = new Pipeline(db);
 071    List<Task> tasks = [];
 72    try
 073    {
 074      tasks.Add(pipeline.Json.SetAsync(RedisHelper.GetSlamData(realmId), "$", exchange));
 075      tasks.Add(db.KeyExpireAsync(RedisHelper.GetSlamData(realmId), TimeSpan.FromMinutes(5)));
 076      pipeline.Execute();
 077      await Task.WhenAll(tasks);
 078    }
 079    catch (Exception ex)
 080    {
 081      LogException(ex.Message);
 082    }
 083  }
 84
 85  public async Task<bool> AddSlamCommandAsync(int realmId, RobotClientsSlamCommands commands)
 086  {
 087    var db = _redisConnection.GetDatabase();
 88    try
 089    {
 090      if (!await db.KeyExistsAsync(RedisHelper.GetSlamData(realmId)))
 091        return false;
 092      var subscriber = _redisConnection.GetSubscriber();
 093      var base64 = SerialiserHelper.ToBase64(commands);
 094      await subscriber.PublishAsync(new RedisChannel(RedisHelper.GetSlamExchangeQueue(realmId), PatternMode.Literal), ba
 095    }
 096    catch (Exception ex)
 097    {
 098      LogException(ex.Message);
 099      return false;
 100    }
 0101    return true;
 0102  }
 103}