98 lines
No EOL
3.4 KiB
C#
98 lines
No EOL
3.4 KiB
C#
using System;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
using Avalonia.Controls;
|
|
|
|
namespace HAcontrol;
|
|
|
|
public partial class MainWindow : Window
|
|
{
|
|
private System.Threading.CancellationTokenSource? _cts;
|
|
|
|
public MainWindow()
|
|
{
|
|
InitializeComponent();
|
|
_cts = new System.Threading.CancellationTokenSource();
|
|
StartStatePolling(_cts.Token);
|
|
}
|
|
|
|
protected override void OnClosed(EventArgs e)
|
|
{
|
|
base.OnClosed(e);
|
|
_cts?.Cancel();
|
|
_cts?.Dispose();
|
|
}
|
|
|
|
private async void StartStatePolling(System.Threading.CancellationToken token)
|
|
{
|
|
while (!token.IsCancellationRequested)
|
|
{
|
|
await GetState();
|
|
try
|
|
{
|
|
await Task.Delay(10000, token);
|
|
}
|
|
catch (TaskCanceledException)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
private async void Button_Click(object sender, Avalonia.Interactivity.RoutedEventArgs e)
|
|
{
|
|
string webhookUrl = "/api/webhook/";
|
|
string payload = "{\"event\": \"\"}";
|
|
|
|
// HttpClientHandler to ignore SSL certificate errors (for development only!)
|
|
var handler = new HttpClientHandler();
|
|
handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true;
|
|
|
|
using (var client = new HttpClient(handler))
|
|
{
|
|
var content = new StringContent(payload, Encoding.UTF8, "application/json");
|
|
using var _ = await client.PostAsync(webhookUrl, content);
|
|
await Task.Delay(100);
|
|
}
|
|
|
|
await GetState();
|
|
}
|
|
|
|
private async Task GetState()
|
|
{
|
|
// HttpClientHandler to ignore SSL certificate errors (for development only!)
|
|
var handler = new HttpClientHandler();
|
|
handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true;
|
|
|
|
using (var client = new HttpClient(handler))
|
|
{
|
|
string statusUrl = "/api/states/";
|
|
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", "");
|
|
|
|
using var response = await client.GetAsync(statusUrl);
|
|
string statusJson = await response.Content.ReadAsStringAsync();
|
|
|
|
// Parse the JSON and extract the state
|
|
try
|
|
{
|
|
var jsonDoc = System.Text.Json.JsonDocument.Parse(statusJson);
|
|
string? state = jsonDoc.RootElement.GetProperty("state").GetString();
|
|
string? entity = jsonDoc.RootElement.GetProperty("entity_id").GetString();
|
|
StatusText.Content = $"{state}";
|
|
if (state == "on")
|
|
StatusText.Foreground = new Avalonia.Media.SolidColorBrush(Avalonia.Media.Colors.Green);
|
|
else if (state == "off")
|
|
StatusText.Foreground = new Avalonia.Media.SolidColorBrush(Avalonia.Media.Colors.Red);
|
|
else
|
|
StatusText.Foreground = new Avalonia.Media.SolidColorBrush(Avalonia.Media.Colors.Orange);
|
|
}
|
|
catch
|
|
{
|
|
StatusText.Content = "Fehler!";
|
|
StatusText.Foreground = new Avalonia.Media.SolidColorBrush(Avalonia.Media.Colors.Red);
|
|
}
|
|
}
|
|
}
|
|
} |