Skip to content

Commit

Permalink
fix route on admin monitoring routing
Browse files Browse the repository at this point in the history
  • Loading branch information
LucHeart committed Dec 12, 2023
1 parent 0c1e34d commit 2fac5af
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 70 deletions.
134 changes: 67 additions & 67 deletions API/Controller/Admin/OnlineDevicesController.cs
Original file line number Diff line number Diff line change
@@ -1,71 +1,71 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using OpenShock.Common.Models;
using OpenShock.Common.Redis;
using OpenShock.Common.Serialization;
using OpenShock.ServicesCommon.Utils;
using Semver;
using System.Net;
using System.Text.Json.Serialization;

namespace OpenShock.API.Controller.Admin;

public sealed partial class AdminController
{
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using OpenShock.Common.Models;
using OpenShock.Common.Redis;
using OpenShock.Common.Serialization;
using OpenShock.ServicesCommon.Utils;
using Semver;
using System.Net;
using System.Text.Json.Serialization;

namespace OpenShock.API.Controller.Admin;

public sealed partial class AdminController
{
/// <summary>
/// Gets all online devices
/// </summary>
/// <response code="200">All online devices</response>
/// <response code="401">Unauthorized</response>
[HttpGet(Name = "GetOnlineDevices")]
[ProducesResponseType((int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.Unauthorized)]
public async Task<BaseResponse<object>> Get()
{
var devicesOnline = _redis.RedisCollection<DeviceOnline>(false);

var allOnlineDevices = await devicesOnline.ToListAsync();
var dbLookup = await _db.Devices.Where(x => allOnlineDevices.Select(y => y.Id)
.Contains(x.Id)).Select(
x =>
new
{
x.Id,
x.Name,
Owner = new GenericIni
{
Id = x.OwnerNavigation.Id,
Image = x.OwnerNavigation.GetImage(),
Name = x.OwnerNavigation.Name
}
}).ToListAsync();

return new BaseResponse<object>
{
Data = allOnlineDevices.Select(x =>
{
var dbItem = dbLookup.First(y => y.Id == x.Id);
return new AdminOnlineDeviceResponse
{
Id = x.Id,
FirmwareVersion = x.FirmwareVersion,
Gateway = x.Gateway,
Owner = dbItem.Owner,
Name = dbItem.Name
};
})
};
}

public class AdminOnlineDeviceResponse
{
public required Guid Id { get; set; }
public required string Name { get; set; }
public required GenericIni Owner { get; set; }

[JsonConverter(typeof(SemVersionJsonConverter))]
public SemVersion? FirmwareVersion { get; set; }

public string? Gateway { get; set; }
}
/// <response code="200">All online devices</response>
/// <response code="401">Unauthorized</response>
[HttpGet("monitoring/onlineDevices", Name = "GetOnlineDevices")]
[ProducesResponseType((int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.Unauthorized)]
public async Task<BaseResponse<object>> Get()
{
var devicesOnline = _redis.RedisCollection<DeviceOnline>(false);

var allOnlineDevices = await devicesOnline.ToListAsync();
var dbLookup = await _db.Devices.Where(x => allOnlineDevices.Select(y => y.Id)
.Contains(x.Id)).Select(
x =>
new
{
x.Id,
x.Name,
Owner = new GenericIni
{
Id = x.OwnerNavigation.Id,
Image = x.OwnerNavigation.GetImage(),
Name = x.OwnerNavigation.Name
}
}).ToListAsync();

return new BaseResponse<object>
{
Data = allOnlineDevices.Select(x =>
{
var dbItem = dbLookup.First(y => y.Id == x.Id);
return new AdminOnlineDeviceResponse
{
Id = x.Id,
FirmwareVersion = x.FirmwareVersion,
Gateway = x.Gateway,
Owner = dbItem.Owner,
Name = dbItem.Name
};
})
};
}

public class AdminOnlineDeviceResponse
{
public required Guid Id { get; set; }
public required string Name { get; set; }
public required GenericIni Owner { get; set; }

[JsonConverter(typeof(SemVersionJsonConverter))]
public SemVersion? FirmwareVersion { get; set; }

public string? Gateway { get; set; }
}
}
3 changes: 0 additions & 3 deletions LiveControlGateway/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,6 @@ public void ConfigureServices(IServiceCollection services)
redis.Connection.CreateIndex(typeof(LcgNode));
services.AddSingleton<IRedisConnectionProvider>(redis);

// TODO: Is this needed?
//services.AddStackExchangeRedisExtensions<NewtonsoftSerializer>(redisConf);

services.AddMemoryCache();
services.AddHttpContextAccessor();

Expand Down

0 comments on commit 2fac5af

Please sign in to comment.