diff --git a/UserInterface/Views/AddCommandDialog.axaml b/UserInterface/Views/AddCommandDialog.axaml
index dc2e967..30bdcf7 100644
--- a/UserInterface/Views/AddCommandDialog.axaml
+++ b/UserInterface/Views/AddCommandDialog.axaml
@@ -5,24 +5,26 @@
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="UserInterface.Views.AddCommandDialog"
SizeToContent="WidthAndHeight"
- Title="Add command">
-
- Command type
+ Title="Add / edit command">
+
+ Command type
-
-
-
- Name
-
+
+
+
+ Name
+
-
- Command
-
- Key
-
-
-
-
+
+ Command
+
+ Key
+
+
+
+
+
+
diff --git a/UserInterface/Views/AddCommandDialog.axaml.cs b/UserInterface/Views/AddCommandDialog.axaml.cs
index fbf0974..45005e2 100644
--- a/UserInterface/Views/AddCommandDialog.axaml.cs
+++ b/UserInterface/Views/AddCommandDialog.axaml.cs
@@ -7,7 +7,6 @@ using hass_workstation_service.Communication.NamedPipe;
using JKang.IpcServiceFramework.Client;
using Microsoft.Extensions.DependencyInjection;
using System;
-using System.Dynamic;
using System.Linq;
using System.Text.Json;
using UserInterface.Util;
@@ -17,16 +16,25 @@ namespace UserInterface.Views
{
public class AddCommandDialog : Window
{
- private readonly IIpcClient client;
- public ComboBox comboBox { get; set; }
- public ComboBox detectionModecomboBox { get; set; }
+ private readonly IIpcClient _client;
+ public ComboBox ComboBox { get; set; }
+ public ComboBox DetectionModecomboBox { get; set; }
+ public Guid CommandId { get; }
+
+ public AddCommandDialog(Guid commandId) : this()
+ {
+ CommandId = commandId;
+ GetCommandInfo(CommandId);
+ Title = "Edit command";
+ }
+
public AddCommandDialog()
{
- this.InitializeComponent();
+ InitializeComponent();
DataContext = new AddCommandViewModel();
- this.comboBox = this.FindControl("ComboBox");
- this.comboBox.Items = Enum.GetValues(typeof(AvailableCommands)).Cast().OrderBy(v => v.ToString());
- this.comboBox.SelectedIndex = 0;
+ ComboBox = this.FindControl("ComboBox");
+ ComboBox.Items = Enum.GetValues(typeof(AvailableCommands)).Cast().OrderBy(v => v.ToString());
+ ComboBox.SelectedIndex = 0;
// register IPC clients
ServiceProvider serviceProvider = new ServiceCollection()
@@ -38,22 +46,53 @@ namespace UserInterface.Views
.GetRequiredService>();
// create client
- this.client = clientFactory.CreateClient("addCommand");
+ _client = clientFactory.CreateClient("addCommand");
+ Title = "Add sensor";
+
+ }
+
+ private void InitializeComponent()
+ {
+ AvaloniaXamlLoader.Load(this);
+ }
+
+ private async void GetCommandInfo(Guid commandId)
+ {
+ var command = await _client.InvokeAsync(x => x.GetConfiguredCommand(commandId));
+
+ ComboBox.SelectedItem = command.Type;
+ FillDefaultValues();
+ ComboBox.IsEnabled = false;
+ var item = (AddCommandViewModel)DataContext;
+ item.SelectedType = command.Type;
+ item.Name = command.Name;
+ //item.UpdateInterval = command.UpdateInterval;
+ //item.WindowName =
+ //item.Query =
}
public async void Save(object sender, RoutedEventArgs args)
{
- var item = ((AddCommandViewModel)this.DataContext);
- dynamic model = new { item.Name, item.Command, item.Key};
+ var item = (AddCommandViewModel)DataContext;
+ dynamic model = new { item.Name, item.Command, item.Key };
string json = JsonSerializer.Serialize(model);
- await this.client.InvokeAsync(x => x.AddCommand(item.SelectedType, json));
+ if (CommandId == Guid.Empty)
+ await _client.InvokeAsync(x => x.AddCommand(item.SelectedType, json));
+ else
+ await _client.InvokeAsync(x => x.UpdateCommandById(CommandId, json));
+
Close();
}
public void ComboBoxClosed(object sender, SelectionChangedEventArgs args)
{
- var item = ((AddCommandViewModel)this.DataContext);
- switch (this.comboBox.SelectedItem)
+ FillDefaultValues();
+ }
+
+ private void FillDefaultValues()
+ {
+ var item = (AddCommandViewModel)DataContext;
+ switch (ComboBox.SelectedItem)
{
case AvailableCommands.CustomCommand:
item.Description = "This command lets you execute any command you want. It will run in a Windows Command Prompt silently. ";
@@ -129,28 +168,26 @@ namespace UserInterface.Views
break;
}
}
- public void OpenInfo(object sender, RoutedEventArgs args)
+
+ public void OpenInfo(object sender, RoutedEventArgs args)
{
- var item = ((AddCommandViewModel)this.DataContext);
+ var item = (AddCommandViewModel)DataContext;
BrowserUtil.OpenBrowser(item.MoreInfoLink);
}
public void Test(object sender, RoutedEventArgs args)
{
- var item = ((AddCommandViewModel)this.DataContext);
+ var item = (AddCommandViewModel)DataContext;
- System.Diagnostics.Process process = new System.Diagnostics.Process();
- System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
- startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
- startInfo.FileName = "cmd.exe";
- startInfo.Arguments = $"/k {"echo You won't see this window normally. &&" + item.Command}";
+ var process = new System.Diagnostics.Process();
+ var startInfo = new System.Diagnostics.ProcessStartInfo
+ {
+ WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal,
+ FileName = "cmd.exe",
+ Arguments = $"/k {"echo You won't see this window normally. &&" + item.Command}"
+ };
process.StartInfo = startInfo;
process.Start();
}
-
- private void InitializeComponent()
- {
- AvaloniaXamlLoader.Load(this);
- }
}
-}
+}
\ No newline at end of file
diff --git a/UserInterface/Views/AddSensorDialog.axaml b/UserInterface/Views/AddSensorDialog.axaml
index 10d62ec..8b3b295 100644
--- a/UserInterface/Views/AddSensorDialog.axaml
+++ b/UserInterface/Views/AddSensorDialog.axaml
@@ -5,27 +5,27 @@
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="UserInterface.Views.AddSensorDialog"
SizeToContent="WidthAndHeight"
- Title="Add sensor">
-
- Sensor type
+ Title="Add / edit sensor">
+
+ Sensor type
-
-
-
- Name
-
+
+
+
+ Name
+
- Update interval
-
-
-
-
-
- Query
-
- Window name
- This is case-insensitive and loosely matched. A window called "Spotify Premium" will match "spotify" or "premium".
-
+ Update interval
+
+
+
+
+
+ Query
+
+ Window name
+ This is case-insensitive and loosely matched. A window called "Spotify Premium" will match "spotify" or "premium".
+
-
+
diff --git a/UserInterface/Views/AddSensorDialog.axaml.cs b/UserInterface/Views/AddSensorDialog.axaml.cs
index 024e78a..9b5d82f 100644
--- a/UserInterface/Views/AddSensorDialog.axaml.cs
+++ b/UserInterface/Views/AddSensorDialog.axaml.cs
@@ -7,7 +7,6 @@ using hass_workstation_service.Communication.NamedPipe;
using JKang.IpcServiceFramework.Client;
using Microsoft.Extensions.DependencyInjection;
using System;
-using System.Dynamic;
using System.Linq;
using System.Text.Json;
using UserInterface.Util;
@@ -17,16 +16,25 @@ namespace UserInterface.Views
{
public class AddSensorDialog : Window
{
- private readonly IIpcClient client;
- public ComboBox comboBox { get; set; }
- public ComboBox detectionModecomboBox { get; set; }
+ private readonly IIpcClient _client;
+ public ComboBox ComboBox { get; set; }
+ public ComboBox DetectionModecomboBox { get; set; }
+ public Guid SensorId { get; }
+
+ public AddSensorDialog(Guid sensorId) : this()
+ {
+ SensorId = sensorId;
+ GetSensorInfo(SensorId);
+ Title = "Edit sensor";
+ }
+
public AddSensorDialog()
{
- this.InitializeComponent();
+ InitializeComponent();
DataContext = new AddSensorViewModel();
- this.comboBox = this.FindControl("ComboBox");
- this.comboBox.Items = Enum.GetValues(typeof(AvailableSensors)).Cast().OrderBy(v => v.ToString());
- this.comboBox.SelectedIndex = 0;
+ ComboBox = this.FindControl("ComboBox");
+ ComboBox.Items = Enum.GetValues(typeof(AvailableSensors)).Cast().OrderBy(v => v.ToString());
+ ComboBox.SelectedIndex = 0;
// register IPC clients
ServiceProvider serviceProvider = new ServiceCollection()
@@ -38,22 +46,52 @@ namespace UserInterface.Views
.GetRequiredService>();
// create client
- this.client = clientFactory.CreateClient("addsensor");
+ _client = clientFactory.CreateClient("addsensor");
+ Title = "Add sensor";
+ }
+
+ private void InitializeComponent()
+ {
+ AvaloniaXamlLoader.Load(this);
+ }
+
+ private async void GetSensorInfo(Guid sensorId)
+ {
+ var sensor = await _client.InvokeAsync(x => x.GetConfiguredSensor(sensorId));
+
+ ComboBox.SelectedItem = sensor.Type;
+ FillDefaultValues();
+ ComboBox.IsEnabled = false;
+ var item = (AddSensorViewModel)DataContext;
+ item.SelectedType = sensor.Type;
+ item.Name = sensor.Name;
+ item.UpdateInterval = sensor.UpdateInterval;
+ //item.WindowName =
+ //item.Query =
}
public async void Save(object sender, RoutedEventArgs args)
{
- var item = ((AddSensorViewModel)this.DataContext);
- dynamic model = new { item.Name, item.Query, item.UpdateInterval, item.WindowName};
+ var item = (AddSensorViewModel)DataContext;
+ dynamic model = new { item.Name, item.Query, item.UpdateInterval, item.WindowName };
string json = JsonSerializer.Serialize(model);
- await this.client.InvokeAsync(x => x.AddSensor(item.SelectedType, json));
+ if (SensorId == Guid.Empty)
+ await _client.InvokeAsync(x => x.AddSensor(item.SelectedType, json));
+ else
+ await _client.InvokeAsync(x => x.UpdateSensorById(SensorId, json));
+
Close();
}
public void ComboBoxClosed(object sender, SelectionChangedEventArgs args)
{
- var item = ((AddSensorViewModel)this.DataContext);
- switch (this.comboBox.SelectedItem)
+ FillDefaultValues();
+ }
+
+ private void FillDefaultValues()
+ {
+ var item = (AddSensorViewModel)DataContext;
+ switch (ComboBox.SelectedItem)
{
case AvailableSensors.UserNotificationStateSensor:
item.Description = "This sensor watches the UserNotificationState. This is normally used in applications to determine if it is appropriate to send a notification but we can use it to expose this state. \n ";
@@ -181,15 +219,11 @@ namespace UserInterface.Views
break;
}
}
+
public void OpenInfo(object sender, RoutedEventArgs args)
{
- var item = ((AddSensorViewModel)this.DataContext);
+ var item = (AddSensorViewModel)DataContext;
BrowserUtil.OpenBrowser(item.MoreInfoLink);
}
-
- private void InitializeComponent()
- {
- AvaloniaXamlLoader.Load(this);
- }
}
-}
+}
\ No newline at end of file
diff --git a/UserInterface/Views/CommandSettings.axaml b/UserInterface/Views/CommandSettings.axaml
index 6998808..0bc4aac 100644
--- a/UserInterface/Views/CommandSettings.axaml
+++ b/UserInterface/Views/CommandSettings.axaml
@@ -22,7 +22,7 @@
-
+
diff --git a/UserInterface/Views/CommandSettings.axaml.cs b/UserInterface/Views/CommandSettings.axaml.cs
index b4379da..4c8d7bf 100644
--- a/UserInterface/Views/CommandSettings.axaml.cs
+++ b/UserInterface/Views/CommandSettings.axaml.cs
@@ -4,27 +4,26 @@ using Avalonia.Markup.Xaml;
using Microsoft.Extensions.DependencyInjection;
using hass_workstation_service.Communication.NamedPipe;
using JKang.IpcServiceFramework.Client;
-using System.Threading.Tasks;
using Avalonia.Interactivity;
using System.Reactive.Linq;
using UserInterface.ViewModels;
-using System.Security;
using hass_workstation_service.Communication.InterProcesCommunication.Models;
using System.Collections.Generic;
using System.Linq;
using Avalonia.Controls.ApplicationLifetimes;
-
+using System.Threading.Tasks;
+
namespace UserInterface.Views
{
public class CommandSettings : UserControl
{
private readonly IIpcClient _client;
private readonly DataGrid _dataGrid;
- private bool _sensorsNeedToRefresh;
+ private bool _commandsNeedToRefresh;
public CommandSettings()
{
- this.InitializeComponent();
+ InitializeComponent();
// register IPC clients
ServiceProvider serviceProvider = new ServiceCollection()
.AddNamedPipeIpcClient("commands", pipeName: "pipeinternal")
@@ -35,63 +34,75 @@ namespace UserInterface.Views
.GetRequiredService>();
// create client
- this._client = clientFactory.CreateClient("commands");
-
+ _client = clientFactory.CreateClient("commands");
+ _dataGrid = this.FindControl("Grid");
DataContext = new CommandSettingsViewModel();
GetConfiguredCommands();
+ }
- this._dataGrid = this.FindControl("Grid");
+ private void InitializeComponent()
+ {
+ AvaloniaXamlLoader.Load(this);
}
public async void GetConfiguredCommands()
{
- _sensorsNeedToRefresh = false;
- List status = await this._client.InvokeAsync(x => x.GetConfiguredCommands());
+ List status = await _client.InvokeAsync(x => x.GetConfiguredCommands());
- ((CommandSettingsViewModel)this.DataContext).ConfiguredCommands = status.Select(s =>
+ ((CommandSettingsViewModel)DataContext).ConfiguredCommands = status.Select(s =>
new CommandViewModel()
{
Name = s.Name,
Type = s.Type,
Id = s.Id
}).ToList();
- }
- public async void AddCommand(object sender, RoutedEventArgs args)
- {
- var dialog = new AddCommandDialog();
- if (Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
+ if (_commandsNeedToRefresh)
{
- await dialog.ShowDialog(desktop.MainWindow);
- _sensorsNeedToRefresh = true;
- GetConfiguredCommands();
- }
- }
-
- public void EditCommand(object sender, RoutedEventArgs args)
- {
-
- }
-
- public void DeleteCommand(object sender, RoutedEventArgs args)
- {
- if (_dataGrid.SelectedItem is not CommandViewModel item)
- return;
-
- this._client.InvokeAsync(x => x.RemoveCommandById(item.Id));
-
- if (DataContext is not CommandSettingsViewModel viewModel)
- return;
-
- viewModel.ConfiguredCommands.Remove(item);
- _dataGrid.SelectedIndex = -1;
- viewModel.TriggerUpdate();
- }
-
- private void InitializeComponent()
- {
- AvaloniaXamlLoader.Load(this);
- }
+ await Task.Delay(1000);
+ GetConfiguredCommands();
+ _commandsNeedToRefresh = false;
+ }
+ }
+
+ public async void AddCommand(object sender, RoutedEventArgs args)
+ {
+ var dialog = new AddCommandDialog();
+ if (Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
+ {
+ await dialog.ShowDialog(desktop.MainWindow);
+ GetConfiguredCommands();
+ }
+ }
+
+ public async void EditCommand(object sender, RoutedEventArgs args)
+ {
+ if (_dataGrid.SelectedItem is not CommandViewModel item)
+ return;
+
+ var dialog = new AddCommandDialog(item.Id);
+ if (Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
+ {
+ await dialog.ShowDialog(desktop.MainWindow);
+ _commandsNeedToRefresh = true;
+ GetConfiguredCommands();
+ }
+ }
+
+ public void DeleteCommand(object sender, RoutedEventArgs args)
+ {
+ if (_dataGrid.SelectedItem is not CommandViewModel item)
+ return;
+
+ _client.InvokeAsync(x => x.RemoveCommandById(item.Id));
+
+ if (DataContext is not CommandSettingsViewModel viewModel)
+ return;
+
+ viewModel.ConfiguredCommands.Remove(item);
+ _dataGrid.SelectedIndex = -1;
+ viewModel.TriggerUpdate();
+ }
}
}
\ No newline at end of file
diff --git a/UserInterface/Views/SensorSettings.axaml b/UserInterface/Views/SensorSettings.axaml
index 81c7e10..c5e6a75 100644
--- a/UserInterface/Views/SensorSettings.axaml
+++ b/UserInterface/Views/SensorSettings.axaml
@@ -28,7 +28,7 @@
-
+
diff --git a/UserInterface/Views/SensorSettings.axaml.cs b/UserInterface/Views/SensorSettings.axaml.cs
index dff4900..4b7ea67 100644
--- a/UserInterface/Views/SensorSettings.axaml.cs
+++ b/UserInterface/Views/SensorSettings.axaml.cs
@@ -8,7 +8,6 @@ using System.Threading.Tasks;
using Avalonia.Interactivity;
using System.Reactive.Linq;
using UserInterface.ViewModels;
-using System.Security;
using hass_workstation_service.Communication.InterProcesCommunication.Models;
using System.Collections.Generic;
using System.Linq;
@@ -24,7 +23,7 @@ namespace UserInterface.Views
public SensorSettings()
{
- this.InitializeComponent();
+ InitializeComponent();
// register IPC clients
ServiceProvider serviceProvider = new ServiceCollection()
.AddNamedPipeIpcClient("sensors", pipeName: "pipeinternal")
@@ -35,21 +34,24 @@ namespace UserInterface.Views
.GetRequiredService>();
// create client
- this._client = clientFactory.CreateClient("sensors");
-
+ _client = clientFactory.CreateClient("sensors");
+ _dataGrid = this.FindControl("Grid");
DataContext = new SensorSettingsViewModel();
GetConfiguredSensors();
+ }
- this._dataGrid = this.FindControl("Grid");
+ private void InitializeComponent()
+ {
+ AvaloniaXamlLoader.Load(this);
}
public async void GetConfiguredSensors()
{
_sensorsNeedToRefresh = false;
- List status = await this._client.InvokeAsync(x => x.GetConfiguredSensors());
+ List status = await _client.InvokeAsync(x => x.GetConfiguredSensors());
- ((SensorSettingsViewModel)this.DataContext).ConfiguredSensors = status.Select(s =>
+ ((SensorSettingsViewModel)DataContext).ConfiguredSensors = status.Select(s =>
new SensorViewModel()
{
Name = s.Name,
@@ -63,8 +65,8 @@ namespace UserInterface.Views
while (!_sensorsNeedToRefresh)
{
await Task.Delay(1000);
- List statusUpdated = await this._client.InvokeAsync(x => x.GetConfiguredSensors());
- var configuredSensors = ((SensorSettingsViewModel)this.DataContext).ConfiguredSensors;
+ List statusUpdated = await _client.InvokeAsync(x => x.GetConfiguredSensors());
+ var configuredSensors = ((SensorSettingsViewModel)DataContext).ConfiguredSensors;
// this is a workaround for the list showing before it has been completely loaded in the service
if (statusUpdated.Count != configuredSensors.Count)
{
@@ -95,9 +97,18 @@ namespace UserInterface.Views
}
}
- public void EditSensor(object sender, RoutedEventArgs args)
+ public async void EditSensor(object sender, RoutedEventArgs args)
{
+ if (_dataGrid.SelectedItem is not SensorViewModel item)
+ return;
+ var dialog = new AddSensorDialog(item.Id);
+ if (Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
+ {
+ await dialog.ShowDialog(desktop.MainWindow);
+ _sensorsNeedToRefresh = true;
+ GetConfiguredSensors();
+ }
}
public void DeleteSensor(object sender, RoutedEventArgs args)
@@ -105,7 +116,7 @@ namespace UserInterface.Views
if (_dataGrid.SelectedItem is not SensorViewModel item)
return;
- this._client.InvokeAsync(x => x.RemoveSensorById(item.Id));
+ _client.InvokeAsync(x => x.RemoveSensorById(item.Id));
if (DataContext is not SensorSettingsViewModel viewModel)
return;
@@ -114,10 +125,5 @@ namespace UserInterface.Views
_dataGrid.SelectedIndex = -1;
viewModel.TriggerUpdate();
}
-
- private void InitializeComponent()
- {
- AvaloniaXamlLoader.Load(this);
- }
}
}
\ No newline at end of file