-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix route on admin monitoring routing
- Loading branch information
Showing
2 changed files
with
67 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters