mirror of
https://github.com/Stefan-5422/-T5-Elektrifikatsiya.git
synced 2026-09-04 08:55:59 +02:00
Improve MdnsDiscovery
This commit is contained in:
@@ -47,8 +47,9 @@
|
||||
@code {
|
||||
List<IPAddress> ipAddresses = new List<IPAddress>();
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
ipAddresses.AddRange(MdnsDiscovery.GetChachedDevices());
|
||||
MdnsDiscovery.OnDeviceFound += address =>
|
||||
{
|
||||
ipAddresses.Add(address);
|
||||
|
||||
+16
-5
@@ -13,12 +13,14 @@ public class DeviceManagmentService : IDeviceManagmentService
|
||||
private readonly DeviceManagmentDatabaseContext deviceManagmentDatabaseContext;
|
||||
private readonly IServiceScopeFactory serviceScopeFactory;
|
||||
private readonly IDeviceStatusService deviceStatusService;
|
||||
private readonly ILogger<DeviceManagmentService> logger;
|
||||
private readonly HttpClient httpClient;
|
||||
|
||||
public DeviceManagmentService(IServiceScopeFactory serviceScopeFactory, IDeviceStatusService deviceStatusService, HttpClient httpClient)
|
||||
public DeviceManagmentService(IServiceScopeFactory serviceScopeFactory, IDeviceStatusService deviceStatusService, ILogger<DeviceManagmentService> logger, HttpClient httpClient)
|
||||
{
|
||||
this.serviceScopeFactory = serviceScopeFactory;
|
||||
this.deviceStatusService = deviceStatusService;
|
||||
this.logger = logger;
|
||||
this.httpClient = httpClient;
|
||||
|
||||
}
|
||||
@@ -83,7 +85,16 @@ public class DeviceManagmentService : IDeviceManagmentService
|
||||
using IServiceScope scope = serviceScopeFactory.CreateScope();
|
||||
DeviceManagmentDatabaseContext deviceManagmentDatabaseContext = scope.ServiceProvider.GetRequiredService<DeviceManagmentDatabaseContext>();
|
||||
|
||||
ShellyResponse? shellyResponse = await httpClient.GetFromJsonAsync<ShellyResponse>($"http://{ip}/shelly");
|
||||
ShellyResponse? shellyResponse = null;
|
||||
|
||||
try
|
||||
{
|
||||
shellyResponse = await httpClient.GetFromJsonAsync<ShellyResponse>($"http://{ip}/shelly");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError("HTTP request failed! {message}", ex.Message);
|
||||
}
|
||||
|
||||
if (shellyResponse is null || shellyResponse.Type != "SHPLG-S")
|
||||
{
|
||||
@@ -99,7 +110,7 @@ public class DeviceManagmentService : IDeviceManagmentService
|
||||
|
||||
Device device = new Device(mac, name ?? mac, ip, user, room);
|
||||
|
||||
deviceManagmentDatabaseContext.Add(device);
|
||||
_ = deviceManagmentDatabaseContext.Add(device);
|
||||
Result saveDatabaseChangesResult = await Result.Try(Task () => deviceManagmentDatabaseContext.SaveChangesAsync());
|
||||
|
||||
Result trackDeviceResult = deviceStatusService.TrackDevice(device);
|
||||
@@ -126,7 +137,7 @@ public class DeviceManagmentService : IDeviceManagmentService
|
||||
return untrackDeviceResult;
|
||||
}
|
||||
|
||||
deviceManagmentDatabaseContext.Remove(getDeviceResult.Value);
|
||||
_ = deviceManagmentDatabaseContext.Remove(getDeviceResult.Value);
|
||||
|
||||
return await Result.Try(Task () => deviceManagmentDatabaseContext.SaveChangesAsync());
|
||||
}
|
||||
@@ -140,7 +151,7 @@ public class DeviceManagmentService : IDeviceManagmentService
|
||||
return result;
|
||||
}
|
||||
|
||||
deviceManagmentDatabaseContext.Update(device);
|
||||
_ = deviceManagmentDatabaseContext.Update(device);
|
||||
|
||||
return await Result.Try(Task () => deviceManagmentDatabaseContext.SaveChangesAsync());
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
using System.Net;
|
||||
using FluentResults;
|
||||
|
||||
using Tmds.MDns;
|
||||
|
||||
namespace Elektrifikatsiya.Utilities;
|
||||
|
||||
public static class MdnsDiscovery
|
||||
{
|
||||
public static event Action<IPAddress> OnDeviceFound;
|
||||
public static event Action<IPAddress> OnDeviceFound = null!;
|
||||
|
||||
static ServiceBrowser serviceBrowser = new ServiceBrowser();
|
||||
private static readonly ServiceBrowser serviceBrowser = new ServiceBrowser();
|
||||
|
||||
static MdnsDiscovery()
|
||||
{
|
||||
@@ -17,4 +17,9 @@ public static class MdnsDiscovery
|
||||
serviceBrowser.ServiceChanged += (_, eventArgs) => OnDeviceFound?.Invoke(eventArgs.Announcement.Addresses.First());
|
||||
serviceBrowser.ServiceRemoved += (_, eventArgs) => OnDeviceFound?.Invoke(eventArgs.Announcement.Addresses.First());
|
||||
}
|
||||
|
||||
public static IEnumerable<IPAddress> GetChachedDevices()
|
||||
{
|
||||
return serviceBrowser.Services.Select(s => s.Addresses.First());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user