diff --git a/HomeAutomation.Core/Services/TelldusAPIService.cs b/HomeAutomation.Core/Services/TelldusAPIService.cs index 81c61eb..d8665f8 100644 --- a/HomeAutomation.Core/Services/TelldusAPIService.cs +++ b/HomeAutomation.Core/Services/TelldusAPIService.cs @@ -12,6 +12,7 @@ namespace HomeAutomation.Core.Services public interface ITelldusAPIService { event Action TelldusEventReceived; + event Action TelldusRawEventReceived; Task SendCommand(int id, TelldusDeviceMethods command); @@ -21,7 +22,9 @@ public interface ITelldusAPIService Task GetLastCommand(int id); void SendLogMessage(string message); + void SendLogMessage(string message, DateTime timestamp); + void SendRawLogMessage(string message); DeviceEvent ConvertCommandToEvent(TelldusDeviceMethods command); @@ -35,71 +38,44 @@ public class TelldusAPIService(IConfiguration configuration) : ITelldusAPIServic private readonly IConfiguration configuration = configuration; public event Action TelldusEventReceived; + public event Action TelldusRawEventReceived; - public Task> GetDevices() + public async Task> GetDevices() { - string[] telldusDevices = configuration.GetSection("Telldus:APIURL").Get(); - - List results = new(); - Parallel.ForEach(telldusDevices, baseUrl => - { - HttpRequestMessage request = new(HttpMethod.Get, $"{baseUrl}devices/"); + string baseUrl = configuration.GetSection("Telldus:APIUrl").Get(); - var sendTask = httpClient.SendAsync(request); - sendTask.Wait(); + HttpRequestMessage request = new(HttpMethod.Get, $"{baseUrl}devices/"); - HttpResponseMessage response = sendTask.Result; - response.EnsureSuccessStatusCode(); + var response = await httpClient.SendAsync(request); + response.EnsureSuccessStatusCode(); - var readTask = response.Content.ReadFromJsonAsync(); - readTask.Wait(); - results.AddRange(readTask.Result); - }); - - return Task.FromResult(results.Distinct()); + return await response.Content.ReadFromJsonAsync(); } public async Task SendCommand(int id, TelldusDeviceMethods command) { - List> tasks = new(); + string baseUrl = configuration.GetSection("Telldus:APIUrl").Get(); - string[] telldusControllers = configuration.GetSection("Telldus:APIURL").Get(); - for (int i = 0; i < telldusControllers.Length; i++) - { - // send the request to the controller - HttpRequestMessage request = new(HttpMethod.Post, $"{telldusControllers[i]}devices/{id}/send/{command}"); + // send the request to the controller + HttpRequestMessage request = new(HttpMethod.Post, $"{baseUrl}devices/{id}/send/{command}"); - // wait for an OK - var task = httpClient.SendAsync(request); - tasks.Add(task); - } + // wait for an OK + var result = await httpClient.SendAsync(request); - var results = await Task.WhenAll(tasks); - return results.All(x => x.IsSuccessStatusCode); + return result.IsSuccessStatusCode; } - public Task GetLastCommand(int id) + public async Task GetLastCommand(int id) { - string[] telldusDevices = configuration.GetSection("Telldus:APIURL").Get(); - - List results = new(); - Parallel.ForEach(telldusDevices, baseUrl => - { - HttpRequestMessage request = new(HttpMethod.Get, $"{baseUrl}devices/{id}/lastcommand"); - - var sendTask = httpClient.SendAsync(request); - sendTask.Wait(); + string baseUrl = configuration.GetSection("Telldus:APIUrl").Get(); - HttpResponseMessage response = sendTask.Result; - response.EnsureSuccessStatusCode(); + HttpRequestMessage request = new(HttpMethod.Get, $"{baseUrl}devices/{id}/lastcommand"); - var readTask = response.Content.ReadFromJsonAsync(); - readTask.Wait(); - results.Add(readTask.Result); - }); + var response = await httpClient.SendAsync(request); + response.EnsureSuccessStatusCode(); - return Task.FromResult(results.FirstOrDefault()); + return await response.Content.ReadFromJsonAsync(); } public void SendLogMessage(string message) @@ -138,4 +114,4 @@ public DeviceEvent ConvertCommandToEvent(TelldusDeviceMethods command) return null; } } -} +} \ No newline at end of file diff --git a/HomeAutomation/appsettings.json b/HomeAutomation/appsettings.json index ccc39dd..14695ca 100644 --- a/HomeAutomation/appsettings.json +++ b/HomeAutomation/appsettings.json @@ -12,7 +12,7 @@ "Token": "token from slack" }, "Telldus": { - "APIUrl": [ "telldus url" ], + "APIUrl": "telldus url", "IgnoreDuplicateWebhooksInSeconds": 15 }, "ZWave": {