|
|
@ -1,123 +1,123 @@
|
|
|
|
using Avalonia;
|
|
|
|
using Avalonia;
|
|
|
|
using Avalonia.Controls;
|
|
|
|
using Avalonia.Controls;
|
|
|
|
using Avalonia.Markup.Xaml;
|
|
|
|
using Avalonia.Markup.Xaml;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using hass_workstation_service.Communication.NamedPipe;
|
|
|
|
using hass_workstation_service.Communication.NamedPipe;
|
|
|
|
using JKang.IpcServiceFramework.Client;
|
|
|
|
using JKang.IpcServiceFramework.Client;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Avalonia.Interactivity;
|
|
|
|
using Avalonia.Interactivity;
|
|
|
|
using System.Reactive.Linq;
|
|
|
|
using System.Reactive.Linq;
|
|
|
|
using UserInterface.ViewModels;
|
|
|
|
using UserInterface.ViewModels;
|
|
|
|
using System.Security;
|
|
|
|
using System.Security;
|
|
|
|
using hass_workstation_service.Communication.InterProcesCommunication.Models;
|
|
|
|
using hass_workstation_service.Communication.InterProcesCommunication.Models;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Linq;
|
|
|
|
using Avalonia.Controls.ApplicationLifetimes;
|
|
|
|
using Avalonia.Controls.ApplicationLifetimes;
|
|
|
|
|
|
|
|
|
|
|
|
namespace UserInterface.Views
|
|
|
|
namespace UserInterface.Views
|
|
|
|
{
|
|
|
|
{
|
|
|
|
public class SensorSettings : UserControl
|
|
|
|
public class SensorSettings : UserControl
|
|
|
|
{
|
|
|
|
{
|
|
|
|
private readonly IIpcClient<ServiceContractInterfaces> _client;
|
|
|
|
private readonly IIpcClient<ServiceContractInterfaces> _client;
|
|
|
|
private readonly DataGrid _dataGrid;
|
|
|
|
private readonly DataGrid _dataGrid;
|
|
|
|
private bool _sensorsNeedToRefresh;
|
|
|
|
private bool _sensorsNeedToRefresh;
|
|
|
|
|
|
|
|
|
|
|
|
public SensorSettings()
|
|
|
|
public SensorSettings()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
this.InitializeComponent();
|
|
|
|
this.InitializeComponent();
|
|
|
|
// register IPC clients
|
|
|
|
// register IPC clients
|
|
|
|
ServiceProvider serviceProvider = new ServiceCollection()
|
|
|
|
ServiceProvider serviceProvider = new ServiceCollection()
|
|
|
|
.AddNamedPipeIpcClient<ServiceContractInterfaces>("sensors", pipeName: "pipeinternal")
|
|
|
|
.AddNamedPipeIpcClient<ServiceContractInterfaces>("sensors", pipeName: "pipeinternal")
|
|
|
|
.BuildServiceProvider();
|
|
|
|
.BuildServiceProvider();
|
|
|
|
|
|
|
|
|
|
|
|
// resolve IPC client factory
|
|
|
|
// resolve IPC client factory
|
|
|
|
IIpcClientFactory<ServiceContractInterfaces> clientFactory = serviceProvider
|
|
|
|
IIpcClientFactory<ServiceContractInterfaces> clientFactory = serviceProvider
|
|
|
|
.GetRequiredService<IIpcClientFactory<ServiceContractInterfaces>>();
|
|
|
|
.GetRequiredService<IIpcClientFactory<ServiceContractInterfaces>>();
|
|
|
|
|
|
|
|
|
|
|
|
// create client
|
|
|
|
// create client
|
|
|
|
this._client = clientFactory.CreateClient("sensors");
|
|
|
|
this._client = clientFactory.CreateClient("sensors");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DataContext = new SensorSettingsViewModel();
|
|
|
|
DataContext = new SensorSettingsViewModel();
|
|
|
|
GetConfiguredSensors();
|
|
|
|
GetConfiguredSensors();
|
|
|
|
|
|
|
|
|
|
|
|
this._dataGrid = this.FindControl<DataGrid>("Grid");
|
|
|
|
this._dataGrid = this.FindControl<DataGrid>("Grid");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async void GetConfiguredSensors()
|
|
|
|
public async void GetConfiguredSensors()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_sensorsNeedToRefresh = false;
|
|
|
|
_sensorsNeedToRefresh = false;
|
|
|
|
List<ConfiguredSensorModel> status = await this._client.InvokeAsync(x => x.GetConfiguredSensors());
|
|
|
|
List<ConfiguredSensorModel> status = await this._client.InvokeAsync(x => x.GetConfiguredSensors());
|
|
|
|
|
|
|
|
|
|
|
|
((SensorSettingsViewModel)this.DataContext).ConfiguredSensors = status.Select(s =>
|
|
|
|
((SensorSettingsViewModel)this.DataContext).ConfiguredSensors = status.Select(s =>
|
|
|
|
new SensorViewModel()
|
|
|
|
new SensorViewModel()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Name = s.Name,
|
|
|
|
Name = s.Name,
|
|
|
|
Type = s.Type,
|
|
|
|
Type = s.Type,
|
|
|
|
Value = s.Value,
|
|
|
|
Value = s.Value,
|
|
|
|
Id = s.Id,
|
|
|
|
Id = s.Id,
|
|
|
|
UpdateInterval = s.UpdateInterval,
|
|
|
|
UpdateInterval = s.UpdateInterval,
|
|
|
|
UnitOfMeasurement = s.UnitOfMeasurement
|
|
|
|
UnitOfMeasurement = s.UnitOfMeasurement
|
|
|
|
}).ToList();
|
|
|
|
}).ToList();
|
|
|
|
|
|
|
|
|
|
|
|
while (!_sensorsNeedToRefresh)
|
|
|
|
while (!_sensorsNeedToRefresh)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
await Task.Delay(1000);
|
|
|
|
await Task.Delay(1000);
|
|
|
|
List<ConfiguredSensorModel> statusUpdated = await this._client.InvokeAsync(x => x.GetConfiguredSensors());
|
|
|
|
List<ConfiguredSensorModel> statusUpdated = await this._client.InvokeAsync(x => x.GetConfiguredSensors());
|
|
|
|
var configuredSensors = ((SensorSettingsViewModel)this.DataContext).ConfiguredSensors;
|
|
|
|
var configuredSensors = ((SensorSettingsViewModel)this.DataContext).ConfiguredSensors;
|
|
|
|
// this is a workaround for the list showing before it has been completely loaded in the service
|
|
|
|
// this is a workaround for the list showing before it has been completely loaded in the service
|
|
|
|
if (statusUpdated.Count != configuredSensors.Count)
|
|
|
|
if (statusUpdated.Count != configuredSensors.Count)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_sensorsNeedToRefresh = true;
|
|
|
|
_sensorsNeedToRefresh = true;
|
|
|
|
GetConfiguredSensors();
|
|
|
|
GetConfiguredSensors();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
statusUpdated.ForEach(s =>
|
|
|
|
statusUpdated.ForEach(s =>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var configuredSensor = configuredSensors.FirstOrDefault(cs => cs.Id == s.Id);
|
|
|
|
var configuredSensor = configuredSensors.FirstOrDefault(cs => cs.Id == s.Id);
|
|
|
|
if (configuredSensor != null)
|
|
|
|
if (configuredSensor != null)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
configuredSensor.Value = s.Value;
|
|
|
|
configuredSensor.Value = s.Value;
|
|
|
|
|
|
|
|
|
|
|
|
configuredSensors.FirstOrDefault(cs => cs.Id == s.Id).Value = s.Value;
|
|
|
|
configuredSensors.FirstOrDefault(cs => cs.Id == s.Id).Value = s.Value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async void AddSensor(object sender, RoutedEventArgs args)
|
|
|
|
public async void AddSensor(object sender, RoutedEventArgs args)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var dialog = new AddSensorDialog();
|
|
|
|
var dialog = new AddSensorDialog();
|
|
|
|
if (Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
|
|
|
|
if (Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
await dialog.ShowDialog(desktop.MainWindow);
|
|
|
|
await dialog.ShowDialog(desktop.MainWindow);
|
|
|
|
_sensorsNeedToRefresh = true;
|
|
|
|
_sensorsNeedToRefresh = true;
|
|
|
|
GetConfiguredSensors();
|
|
|
|
GetConfiguredSensors();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void EditSensor(object sender, RoutedEventArgs args)
|
|
|
|
public void EditSensor(object sender, RoutedEventArgs args)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void DeleteSensor(object sender, RoutedEventArgs args)
|
|
|
|
public void DeleteSensor(object sender, RoutedEventArgs args)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (_dataGrid.SelectedItem is not SensorViewModel item)
|
|
|
|
if (_dataGrid.SelectedItem is not SensorViewModel item)
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
this._client.InvokeAsync(x => x.RemoveSensorById(item.Id));
|
|
|
|
this._client.InvokeAsync(x => x.RemoveSensorById(item.Id));
|
|
|
|
|
|
|
|
|
|
|
|
if (DataContext is not SensorSettingsViewModel viewModel)
|
|
|
|
if (DataContext is not SensorSettingsViewModel viewModel)
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
viewModel.ConfiguredSensors.Remove(item);
|
|
|
|
viewModel.ConfiguredSensors.Remove(item);
|
|
|
|
_dataGrid.SelectedIndex = -1;
|
|
|
|
_dataGrid.SelectedIndex = -1;
|
|
|
|
viewModel.TriggerUpdate();
|
|
|
|
viewModel.TriggerUpdate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void InitializeComponent()
|
|
|
|
private void InitializeComponent()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
AvaloniaXamlLoader.Load(this);
|
|
|
|
AvaloniaXamlLoader.Load(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|