@ -18,9 +18,9 @@ namespace UserInterface.Views
{
{
public class SensorSettings : UserControl
public class SensorSettings : UserControl
{
{
private readonly IIpcClient < ServiceContractInterfaces > client;
private readonly IIpcClient < ServiceContractInterfaces > _ client;
private DataGrid _dataGrid { get ; set ; }
private readonly DataGrid _dataGrid ;
private bool sensorsNeedToRefresh { get ; set ; }
private bool _ sensorsNeedToRefresh;
public SensorSettings ( )
public SensorSettings ( )
{
{
@ -35,7 +35,7 @@ namespace UserInterface.Views
. 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 ( ) ;
@ -44,21 +44,31 @@ namespace UserInterface.Views
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 = > new SensorViewModel ( ) { Name = s . Name , Type = s . Type , Value = s . Value , Id = s . Id , UpdateInterval = s . UpdateInterval , UnitOfMeasurement = s . UnitOfMeasurement } ) . ToList ( ) ;
( ( SensorSettingsViewModel ) this . DataContext ) . ConfiguredSensors = status . Select ( s = >
while ( ! sensorsNeedToRefresh )
new SensorViewModel ( )
{
Name = s . Name ,
Type = s . Type ,
Value = s . Value ,
Id = s . Id ,
UpdateInterval = s . UpdateInterval ,
UnitOfMeasurement = s . UnitOfMeasurement
} ) . ToList ( ) ;
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 = >
@ -72,33 +82,42 @@ namespace UserInterface.Views
}
}
} ) ;
} ) ;
}
}
}
public void Delete ( object sender , RoutedEventArgs args )
{
var item = ( ( SensorViewModel ) this . _dataGrid . SelectedItem ) ;
this . client . InvokeAsync ( x = > x . RemoveSensorById ( item . Id ) ) ;
( ( SensorSettingsViewModel ) this . DataContext ) . ConfiguredSensors . Remove ( item ) ;
this . _dataGrid . SelectedIndex = - 1 ;
( ( SensorSettingsViewModel ) this . DataContext ) . TriggerUpdate ( ) ;
}
}
public async void AddSensor ( object sender , RoutedEventArgs args )
public async void AddSensor ( object sender , RoutedEventArgs args )
{
{
AddSensorDialog 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 ( ) ;
}
}
}
}
p rivate void InitializeComponent ( )
p ublic void EditSensor ( object sender , RoutedEventArgs args )
{
{
AvaloniaXamlLoader . Load ( this ) ;
}
}
public void DeleteSensor ( object sender , RoutedEventArgs args )
{
if ( _dataGrid . SelectedItem is not SensorViewModel item )
return ;
this . _client . InvokeAsync ( x = > x . RemoveSensorById ( item . Id ) ) ;
if ( DataContext is not SensorSettingsViewModel viewModel )
return ;
viewModel . ConfiguredSensors . Remove ( item ) ;
_dataGrid . SelectedIndex = - 1 ;
viewModel . TriggerUpdate ( ) ;
}
private void InitializeComponent ( )
{
AvaloniaXamlLoader . Load ( this ) ;
}
}
}
}
}