device client implemented

This commit is contained in:
Vasya Ryzhkoff 2024-07-11 21:27:07 +07:00
parent 56484b54c8
commit d2eee41ed6
8 changed files with 121 additions and 10 deletions

View File

@ -23,6 +23,11 @@ namespace Yuna.Website.Server.API
app.MapGet("/api/device", GetAll)
.WithTags("device");
app.MapGet("/api/device/{deviceId:long}/fetch", FetchData)
.WithTags("device")
.Produces<string>(404)
.Produces(200);
app.MapPut("/api/device/{deviceId:long}", AddSkillsToDevice)
.WithTags("device");
}
@ -93,5 +98,18 @@ namespace Yuna.Website.Server.API
if (result is null) return Results.NotFound("device");
return Results.Ok(result);
}
[Authorize]
public async Task<IResult> FetchData(long deviceId, IDeviceService deviceService)
{
var device = await deviceService.GetById(deviceId);
if (device is null) return Results.NotFound("device");
var result = await deviceService.FetchPropsData(device);
if (result is null) return Results.NotFound("props");
return Results.Ok(result);
}
}
}

View File

@ -51,7 +51,8 @@ namespace Yuna.Website.Server.API
{
JsonValueName = request.JsonValueName,
MeasureName = request.MeasureName ?? "",
Name = request.Name
Name = request.Name,
Type = request.type.Equals("float") ? PropType.Float : PropType.Boolean
};
var result = await skillService.Create(prop);

View File

@ -3,6 +3,7 @@
public class Prop
{
public long Id { get; set; }
public PropType Type { get; set; }
public string Name { get; set; } = null!;
public string MeasureName { get; set; } = null!;
public string JsonValueName { get; set; } = null!;

View File

@ -0,0 +1,8 @@
namespace Yuna.Website.Server.Model
{
public enum PropType
{
Float = 0,
Boolean = 1,
}
}

View File

@ -4,6 +4,8 @@ using Yuna.Website.Server.Model;
using Yuna.Website.Server.Services.DeviceSkillService;
using Yuna.Website.Server.Storage.Repositories.Device;
using Microsoft.IdentityModel.Tokens;
using System.Text.Json;
namespace Yuna.Website.Server.Services.DeviceService
{
@ -11,10 +13,19 @@ namespace Yuna.Website.Server.Services.DeviceService
{
private readonly IPropService _propService;
private readonly IDeviceRepository _deviceRepository;
public DeviceService(IPropService propService, IDeviceRepository repository)
private readonly IHttpClientFactory _httpClientFactory;
private readonly ILogger<DeviceService> _logger;
public DeviceService(
IPropService propService,
IDeviceRepository repository,
IHttpClientFactory factory,
ILogger<DeviceService> logger)
{
_propService = propService;
_deviceRepository = repository;
_httpClientFactory = factory;
_logger = logger;
}
public async Task<Device?> AddProps(IReadOnlyList<Prop> props, long deviceId)
@ -28,6 +39,65 @@ namespace Yuna.Website.Server.Services.DeviceService
return device;
}
public async Task<Dictionary<long, string>?> FetchPropsData(Device device)
{
var result = new Dictionary<long, string>();
if (device.Props.IsNullOrEmpty()) return result;
var client = _httpClientFactory.CreateClient(device.DeviceUrl);
try
{
_logger.LogTrace("Trying to get data\n " +
"from device: {Id}\n" +
"via url: {DeviceUrl}", device.Id, device.DeviceUrl);
var response = await client.GetAsync(device.DeviceUrl);
_logger.LogTrace("Got response\n " +
"from device: {Id}\n" +
"via url: {DeviceUrl}", device.Id, device.DeviceUrl);
var strContent = await response.Content.ReadAsStringAsync();
result = ParseDeviceJson(device.Props, strContent);
}
catch (Exception ex)
{
_logger.LogError("Fail: {Message}", ex.Message);
return null;
}
return result;
}
private Dictionary<long, string> ParseDeviceJson(IEnumerable<Prop> props, string jsonStr)
{
var result = new Dictionary<long, string>();
using JsonDocument responseJson = JsonDocument.Parse(jsonStr);
foreach (var prop in props)
{
var propExists = responseJson.RootElement.TryGetProperty(prop.JsonValueName, out var propValue);
if (!propExists) throw new Exception($"Unable to find jsonValue {prop.JsonValueName}, aborting");
if(propValue.TryGetDouble(out var value))
{
result.Add(prop.Id, value.ToString());
continue;
}
result.Add(prop.Id, propValue.GetBoolean().ToString()!);
_logger.LogTrace("Obtained value for {JsonValueName}", prop.JsonValueName);
}
return result;
}
public async Task<Device?> Create(Device device)
{
var result = await _deviceRepository.Create(device);

View File

@ -10,5 +10,6 @@ namespace Yuna.Website.Server.Services.DeviceService
//public Task<User?> Update(User user);
public Task<Device?> Delete(long id);
public Task<Device?> AddProps(IReadOnlyList<Prop> props, long deviceId);
public Task<Dictionary<long, string>?> FetchPropsData(Device device);
}
}

View File

@ -48,10 +48,12 @@ namespace Yuna.Website.Server.Storage.Repositories.Device
d.""Id"" as {nameof(Model.Device.Id)},
d.""Name"" as {nameof(Model.Device.Name)},
d.""Description"" as {nameof(Model.Device.Description)},
d.""DeviceUrl"" as {nameof(Model.Device.DeviceUrl)},
p.""Id"" as {nameof(Model.Prop.Id)},
p.""Name"" as {nameof(Model.Prop.Name)},
p.""JsonValueName"" as {nameof(Model.Prop.JsonValueName)},
p.""MeasureName"" as {nameof(Model.Prop.MeasureName)}
p.""MeasureName"" as {nameof(Model.Prop.MeasureName)},
p.""Type"" as {nameof(Model.Prop.Type)}
FROM ""Yuna_Devices"" d
LEFT JOIN ""Yuna_Props_In_Devices"" pd ON d.""Id"" = pd.""DeviceId""
LEFT JOIN ""Yuna_Props"" p ON pd.""PropId"" = p.""Id""
@ -84,10 +86,12 @@ namespace Yuna.Website.Server.Storage.Repositories.Device
d.""Id"" as {nameof(Model.Device.Id)},
d.""Name"" as {nameof(Model.Device.Name)},
d.""Description"" as {nameof(Model.Device.Description)},
d.""DeviceUrl"" as {nameof(Model.Device.DeviceUrl)},
p.""Id"" as {nameof(Model.Prop.Id)},
p.""Name"" as {nameof(Model.Prop.Name)},
p.""JsonValueName"" as {nameof(Model.Prop.JsonValueName)},
p.""MeasureName"" as {nameof(Model.Prop.MeasureName)}
p.""MeasureName"" as {nameof(Model.Prop.MeasureName)},
p.""Type"" as {nameof(Model.Prop.Type)}
FROM ""Yuna_Devices"" d
LEFT JOIN ""Yuna_Props_In_Devices"" pd ON d.""Id"" = pd.""DeviceId""
LEFT JOIN ""Yuna_Props"" p ON pd.""PropId"" = p.""Id""";

View File

@ -15,9 +15,13 @@ namespace Yuna.Website.Server.Storage.Repositories.Prop
{
var query =
$@"INSERT INTO ""Yuna_Props""
(""Name"", ""MeasureName"", ""JsonValueName"")
(""Name"", ""MeasureName"", ""JsonValueName"", ""Type"" )
VALUES
(@{nameof(Model.Prop.Name)}, @{nameof(Model.Prop.MeasureName)}, @{nameof(Model.Prop.JsonValueName)})
(
@{nameof(Model.Prop.Name)},
@{nameof(Model.Prop.MeasureName)},
@{nameof(Model.Prop.JsonValueName)},
@{nameof(Model.Prop.Type)})
RETURNING *";
var result = await _context.Connection.QuerySingleAsync<Model.Prop?>(query, value);
@ -36,7 +40,8 @@ namespace Yuna.Website.Server.Storage.Repositories.Prop
p.""Id"" as {nameof(Model.Prop.Id)},
p.""Name"" as {nameof(Model.Prop.Name)},
p.""MeasureName"" as {nameof(Model.Prop.MeasureName)},
p.""JsonValueName"" as {nameof(Model.Prop.JsonValueName)}
p.""JsonValueName"" as {nameof(Model.Prop.JsonValueName)},
p.""Type"" as {nameof(Model.Prop.Type)}
FROM ""Yuna_Props"" p
WHERE p.""Id"" = {id}
LIMIT 1";
@ -53,7 +58,8 @@ namespace Yuna.Website.Server.Storage.Repositories.Prop
p.""Id"" as {nameof(Model.Prop.Id)},
p.""Name"" as {nameof(Model.Prop.Name)},
p.""MeasureName"" as {nameof(Model.Prop.MeasureName)},
p.""JsonValueName"" as {nameof(Model.Prop.JsonValueName)}
p.""JsonValueName"" as {nameof(Model.Prop.JsonValueName)},
p.""Type"" as {nameof(Model.Prop.Type)}
FROM ""Yuna_Props"" p
WHERE p.""Id"" IN ({idList})";
@ -69,7 +75,8 @@ namespace Yuna.Website.Server.Storage.Repositories.Prop
p.""Id"" as {nameof(Model.Prop.Id)},
p.""Name"" as {nameof(Model.Prop.Name)},
p.""MeasureName"" as {nameof(Model.Prop.MeasureName)},
p.""JsonValueName"" as {nameof(Model.Prop.JsonValueName)}
p.""JsonValueName"" as {nameof(Model.Prop.JsonValueName)},
p.""Type"" as {nameof(Model.Prop.Type)}
FROM ""Yuna_Props"" p
WHERE LOWER(p.""Name"") = '{value.ToLower()}'
LIMIT 1";
@ -85,7 +92,8 @@ namespace Yuna.Website.Server.Storage.Repositories.Prop
p.""Id"" as {nameof(Model.Prop.Id)},
p.""Name"" as {nameof(Model.Prop.Name)},
p.""MeasureName"" as {nameof(Model.Prop.MeasureName)},
p.""JsonValueName"" as {nameof(Model.Prop.JsonValueName)}
p.""JsonValueName"" as {nameof(Model.Prop.JsonValueName)},
p.""Type"" as {nameof(Model.Prop.Type)}
FROM ""Yuna_Props"" p";
var result = await _context.Connection.QueryAsync<Model.Prop>(query);