Skip to content

Commit

Permalink
only support one telldus endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
trembon committed Dec 16, 2024
1 parent b4d245b commit c3bfc74
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 48 deletions.
70 changes: 23 additions & 47 deletions HomeAutomation.Core/Services/TelldusAPIService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace HomeAutomation.Core.Services
public interface ITelldusAPIService
{
event Action<TelldusEventModel> TelldusEventReceived;

event Action<TelldusEventModel> TelldusRawEventReceived;

Task<bool> SendCommand(int id, TelldusDeviceMethods command);
Expand All @@ -21,7 +22,9 @@ public interface ITelldusAPIService
Task<TelldusDeviceMethods> GetLastCommand(int id);

void SendLogMessage(string message);

void SendLogMessage(string message, DateTime timestamp);

void SendRawLogMessage(string message);

DeviceEvent ConvertCommandToEvent(TelldusDeviceMethods command);
Expand All @@ -35,71 +38,44 @@ public class TelldusAPIService(IConfiguration configuration) : ITelldusAPIServic
private readonly IConfiguration configuration = configuration;

public event Action<TelldusEventModel> TelldusEventReceived;

public event Action<TelldusEventModel> TelldusRawEventReceived;

public Task<IEnumerable<TelldusDeviceModel>> GetDevices()
public async Task<IEnumerable<TelldusDeviceModel>> GetDevices()
{
string[] telldusDevices = configuration.GetSection("Telldus:APIURL").Get<string[]>();

List<TelldusDeviceModel> results = new();
Parallel.ForEach(telldusDevices, baseUrl =>
{
HttpRequestMessage request = new(HttpMethod.Get, $"{baseUrl}devices/");
string baseUrl = configuration.GetSection("Telldus:APIUrl").Get<string>();

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<TelldusDeviceModel[]>();
readTask.Wait();
results.AddRange(readTask.Result);
});

return Task.FromResult(results.Distinct());
return await response.Content.ReadFromJsonAsync<TelldusDeviceModel[]>();
}

public async Task<bool> SendCommand(int id, TelldusDeviceMethods command)
{
List<Task<HttpResponseMessage>> tasks = new();
string baseUrl = configuration.GetSection("Telldus:APIUrl").Get<string>();

string[] telldusControllers = configuration.GetSection("Telldus:APIURL").Get<string[]>();
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<TelldusDeviceMethods> GetLastCommand(int id)
public async Task<TelldusDeviceMethods> GetLastCommand(int id)
{
string[] telldusDevices = configuration.GetSection("Telldus:APIURL").Get<string[]>();

List<TelldusDeviceMethods> 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<string>();

HttpResponseMessage response = sendTask.Result;
response.EnsureSuccessStatusCode();
HttpRequestMessage request = new(HttpMethod.Get, $"{baseUrl}devices/{id}/lastcommand");

var readTask = response.Content.ReadFromJsonAsync<TelldusDeviceMethods>();
readTask.Wait();
results.Add(readTask.Result);
});
var response = await httpClient.SendAsync(request);
response.EnsureSuccessStatusCode();

return Task.FromResult(results.FirstOrDefault());
return await response.Content.ReadFromJsonAsync<TelldusDeviceMethods>();
}

public void SendLogMessage(string message)
Expand Down Expand Up @@ -138,4 +114,4 @@ public DeviceEvent ConvertCommandToEvent(TelldusDeviceMethods command)
return null;
}
}
}
}
2 changes: 1 addition & 1 deletion HomeAutomation/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"Token": "token from slack"
},
"Telldus": {
"APIUrl": [ "telldus url" ],
"APIUrl": "telldus url",
"IgnoreDuplicateWebhooksInSeconds": 15
},
"ZWave": {
Expand Down

0 comments on commit c3bfc74

Please sign in to comment.