Functional1
This commit is contained in:
parent
fb4730effe
commit
fcacfe127a
4 changed files with 98 additions and 31 deletions
|
|
@ -11,7 +11,8 @@
|
||||||
ExtendClientAreaToDecorationsHint="True"
|
ExtendClientAreaToDecorationsHint="True"
|
||||||
ExtendClientAreaTitleBarHeightHint="0">
|
ExtendClientAreaTitleBarHeightHint="0">
|
||||||
|
|
||||||
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Spacing="10">
|
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" Spacing="0">
|
||||||
<Button x:Name="StatusText" Content="Stream" Width="100" Height="50" Click="Button_Click"/>
|
<Button x:Name="StatusText" Content="Stream" Width="70" Height="50" Click="Button_Click"/>
|
||||||
|
<Button x:Name="SettingsButton" Content="..." Width="30" Height="50" Click="SettingsButton_Click"/>
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
</Window>
|
</Window>
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.IO;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Text.Json;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
|
|
@ -9,15 +11,42 @@ namespace HAcontrol;
|
||||||
|
|
||||||
public partial class MainWindow : Window
|
public partial class MainWindow : Window
|
||||||
{
|
{
|
||||||
|
private Settings? settings = null;
|
||||||
|
|
||||||
private System.Threading.CancellationTokenSource? _cts;
|
private System.Threading.CancellationTokenSource? _cts;
|
||||||
|
|
||||||
|
private static MainWindow self;
|
||||||
|
|
||||||
|
public static MainWindow Self => self;
|
||||||
|
|
||||||
|
|
||||||
public MainWindow()
|
public MainWindow()
|
||||||
{
|
{
|
||||||
|
self = this;
|
||||||
|
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
|
|
||||||
|
LoadSettings();
|
||||||
|
|
||||||
_cts = new System.Threading.CancellationTokenSource();
|
_cts = new System.Threading.CancellationTokenSource();
|
||||||
StartStatePolling(_cts.Token);
|
StartStatePolling(_cts.Token);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void LoadSettings()
|
||||||
|
{
|
||||||
|
if (!File.Exists(SettingsWindow.SettingsFile))
|
||||||
|
{
|
||||||
|
// Create default settings file if it does not exist
|
||||||
|
var defaultSettings = new Settings();
|
||||||
|
var json = JsonSerializer.Serialize(defaultSettings, new JsonSerializerOptions { WriteIndented = true });
|
||||||
|
File.WriteAllText(SettingsWindow.SettingsFile, json);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var json = File.ReadAllText(SettingsWindow.SettingsFile);
|
||||||
|
settings = JsonSerializer.Deserialize<Settings>(json);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected override void OnClosed(EventArgs e)
|
protected override void OnClosed(EventArgs e)
|
||||||
{
|
{
|
||||||
base.OnClosed(e);
|
base.OnClosed(e);
|
||||||
|
|
@ -43,7 +72,14 @@ public partial class MainWindow : Window
|
||||||
|
|
||||||
private async void Button_Click(object sender, Avalonia.Interactivity.RoutedEventArgs e)
|
private async void Button_Click(object sender, Avalonia.Interactivity.RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
string webhookUrl = "/api/webhook/";
|
if (settings == null)
|
||||||
|
{
|
||||||
|
StatusText.Content = "Einstellungen fehlen!";
|
||||||
|
StatusText.Foreground = new Avalonia.Media.SolidColorBrush(Avalonia.Media.Colors.Red);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
string webhookUrl = $"{settings.HomeAssistantUrl}/api/webhook/{settings.WebhookId}";
|
||||||
string payload = "{\"event\": \"\"}";
|
string payload = "{\"event\": \"\"}";
|
||||||
|
|
||||||
// HttpClientHandler to ignore SSL certificate errors (for development only!)
|
// HttpClientHandler to ignore SSL certificate errors (for development only!)
|
||||||
|
|
@ -61,6 +97,15 @@ public partial class MainWindow : Window
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task GetState()
|
private async Task GetState()
|
||||||
|
{
|
||||||
|
if (settings == null)
|
||||||
|
{
|
||||||
|
StatusText.Content = "Einstellungen fehlen!";
|
||||||
|
StatusText.Foreground = new Avalonia.Media.SolidColorBrush(Avalonia.Media.Colors.Red);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
{
|
{
|
||||||
// HttpClientHandler to ignore SSL certificate errors (for development only!)
|
// HttpClientHandler to ignore SSL certificate errors (for development only!)
|
||||||
var handler = new HttpClientHandler();
|
var handler = new HttpClientHandler();
|
||||||
|
|
@ -68,8 +113,8 @@ public partial class MainWindow : Window
|
||||||
|
|
||||||
using (var client = new HttpClient(handler))
|
using (var client = new HttpClient(handler))
|
||||||
{
|
{
|
||||||
string statusUrl = "/api/states/";
|
string statusUrl = $"{settings.HomeAssistantUrl}/api/states/{settings.Entity}";
|
||||||
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", "");
|
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", settings.AccessToken);
|
||||||
|
|
||||||
using var response = await client.GetAsync(statusUrl);
|
using var response = await client.GetAsync(statusUrl);
|
||||||
string statusJson = await response.Content.ReadAsStringAsync();
|
string statusJson = await response.Content.ReadAsStringAsync();
|
||||||
|
|
@ -95,4 +140,17 @@ public partial class MainWindow : Window
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
StatusText.Content = "Fehler!";
|
||||||
|
StatusText.Foreground = new Avalonia.Media.SolidColorBrush(Avalonia.Media.Colors.Red);
|
||||||
|
Console.WriteLine($"Error fetching state: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SettingsButton_Click(object sender, Avalonia.Interactivity.RoutedEventArgs e)
|
||||||
|
{
|
||||||
|
var settingsWindow = new SettingsWindow();
|
||||||
|
settingsWindow.ShowDialog(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
x:Class="HAcontrol.SettingsWindow"
|
x:Class="HAcontrol.SettingsWindow"
|
||||||
Title="Einstellungen" Width="400" Height="250">
|
Title="Einstellungen" Width="400" Height="340">
|
||||||
<StackPanel Margin="20" Spacing="10">
|
<StackPanel Margin="20" Spacing="10">
|
||||||
<TextBlock Text="Home Assistant URL:"/>
|
<TextBlock Text="Home Assistant URL:"/>
|
||||||
<TextBox x:Name="UrlBox"/>
|
<TextBox x:Name="UrlBox"/>
|
||||||
|
|
@ -11,6 +11,8 @@
|
||||||
<TextBox x:Name="TokenBox"/>
|
<TextBox x:Name="TokenBox"/>
|
||||||
<TextBlock Text="Webhook ID:"/>
|
<TextBlock Text="Webhook ID:"/>
|
||||||
<TextBox x:Name="WebhookBox"/>
|
<TextBox x:Name="WebhookBox"/>
|
||||||
|
<TextBlock Text="HA Entity:"/>
|
||||||
|
<TextBox x:Name="EntityBox"/>
|
||||||
<Button Content="Speichern" HorizontalAlignment="Right" Click="Save_Click"/>
|
<Button Content="Speichern" HorizontalAlignment="Right" Click="Save_Click"/>
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
</Window>
|
</Window>
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
using Avalonia.Interactivity;
|
using Avalonia.Interactivity;
|
||||||
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
|
||||||
|
|
@ -7,7 +8,8 @@ namespace HAcontrol;
|
||||||
|
|
||||||
public partial class SettingsWindow : Window
|
public partial class SettingsWindow : Window
|
||||||
{
|
{
|
||||||
private const string SettingsFile = "settings.json";
|
public static readonly string SettingsFile =
|
||||||
|
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "ha_control_settings.json");
|
||||||
|
|
||||||
public SettingsWindow()
|
public SettingsWindow()
|
||||||
{
|
{
|
||||||
|
|
@ -26,6 +28,7 @@ public partial class SettingsWindow : Window
|
||||||
UrlBox.Text = settings.HomeAssistantUrl;
|
UrlBox.Text = settings.HomeAssistantUrl;
|
||||||
TokenBox.Text = settings.AccessToken;
|
TokenBox.Text = settings.AccessToken;
|
||||||
WebhookBox.Text = settings.WebhookId;
|
WebhookBox.Text = settings.WebhookId;
|
||||||
|
EntityBox.Text = settings.Entity;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -36,11 +39,14 @@ public partial class SettingsWindow : Window
|
||||||
{
|
{
|
||||||
HomeAssistantUrl = UrlBox.Text ?? string.Empty,
|
HomeAssistantUrl = UrlBox.Text ?? string.Empty,
|
||||||
AccessToken = TokenBox.Text ?? string.Empty,
|
AccessToken = TokenBox.Text ?? string.Empty,
|
||||||
WebhookId = WebhookBox.Text ?? string.Empty
|
WebhookId = WebhookBox.Text ?? string.Empty,
|
||||||
|
Entity = EntityBox.Text ?? string.Empty
|
||||||
};
|
};
|
||||||
var json = JsonSerializer.Serialize(settings, new JsonSerializerOptions { WriteIndented = true });
|
var json = JsonSerializer.Serialize(settings, new JsonSerializerOptions { WriteIndented = true });
|
||||||
File.WriteAllText(SettingsFile, json);
|
File.WriteAllText(SettingsFile, json);
|
||||||
this.Close();
|
this.Close();
|
||||||
|
|
||||||
|
MainWindow.Self.LoadSettings();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue