device client implemented
This commit is contained in:
@ -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);
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user