-
Notifications
You must be signed in to change notification settings - Fork 22
Added 'Locate Device' and 'Save Permanent' functionality #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,31 +3,53 @@ | |
| using System.Reactive.Linq; | ||
| using System.Reactive.Subjects; | ||
| using System.Reactive.Threading.Tasks; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using System.Windows; | ||
| using ProfinetTools.Interfaces.Extensions; | ||
| using ProfinetTools.Interfaces.Models; | ||
| using ProfinetTools.Interfaces.Services; | ||
|
|
||
| using ReactiveUI; | ||
|
|
||
| namespace ProfinetTools.Gui.ViewModels | ||
| { | ||
| public class SettingsViewModel : ViewModelBase | ||
| { | ||
| public class SettingsViewModel : ViewModelBase | ||
| { | ||
| private readonly IDeviceService deviceService; | ||
| private readonly IAdaptersService adaptersService; | ||
| private readonly ISettingsService settingsService; | ||
| private Device device1; | ||
|
|
||
| public ReactiveUI.ReactiveCommand SaveCommand { get; set; } | ||
| private Device _selectedDevice; | ||
|
|
||
| private bool _signalActive = false; | ||
|
|
||
| private string _locateButtonText; | ||
| public string LocateButtonText | ||
| { | ||
| get { return _locateButtonText; } | ||
| set { this.RaiseAndSetIfChanged(ref _locateButtonText, value); } | ||
| } | ||
|
|
||
| private bool _isDeviceSelected; | ||
| public bool IsDeviceSelected | ||
| { | ||
| get { return _isDeviceSelected; } | ||
| set { this.RaiseAndSetIfChanged(ref _isDeviceSelected, value); } | ||
| } | ||
|
|
||
|
|
||
| public ReactiveUI.ReactiveCommand SaveCommand { get; set; } | ||
| public ReactiveUI.ReactiveCommand ResetCommand { get; set; } | ||
|
|
||
| public ReactiveUI.ReactiveCommand SavePermanentCommand { get; set; } | ||
|
|
||
| public ReactiveUI.ReactiveCommand LocateDeviceCommand { get; set; } | ||
|
|
||
| public SettingsViewModel(IDeviceService deviceService, IAdaptersService adaptersService, ISettingsService settingsService) | ||
| public SettingsViewModel(IDeviceService deviceService, IAdaptersService adaptersService, ISettingsService settingsService) | ||
| { | ||
| this.deviceService = deviceService; | ||
| this.adaptersService = adaptersService; | ||
| this.settingsService = settingsService; | ||
| } | ||
| } | ||
|
|
||
| public override void Init() | ||
| { | ||
|
|
@@ -40,9 +62,17 @@ public override void Init() | |
| SaveCommand = ReactiveUI.ReactiveCommand.CreateFromTask(SaveDeviceSettings) | ||
| .AddDisposableTo(Disposables); | ||
|
|
||
| ResetCommand = ReactiveUI.ReactiveCommand.CreateFromTask(ResetDevice) | ||
| SavePermanentCommand = ReactiveUI.ReactiveCommand.CreateFromTask(SavePermanentDeviceSettings) | ||
| .AddDisposableTo(Disposables); | ||
|
|
||
| ResetCommand = ReactiveUI.ReactiveCommand.CreateFromTask(ResetDevice) | ||
| .AddDisposableTo(Disposables); | ||
| } | ||
|
|
||
| LocateDeviceCommand = ReactiveUI.ReactiveCommand.CreateFromTask(LocateDevice) | ||
| .AddDisposableTo(Disposables); | ||
| LocateButtonText = "Start Blinking"; | ||
| IsDeviceSelected = false; | ||
| } | ||
|
|
||
|
|
||
| private async Task<Unit> ResetDevice() | ||
|
|
@@ -62,14 +92,57 @@ private async Task<Unit> ResetDevice() | |
| return Unit.Default; | ||
| } | ||
|
|
||
| private async Task<Unit> SaveDeviceSettings() | ||
| // TODO put some code for flashing in here | ||
| private async Task<Unit> LocateDevice() | ||
| { | ||
| var adapter = await adaptersService.SelectedAdapter.FirstAsync().ToTask(); | ||
| if (_signalActive) | ||
| { | ||
| _signalActive = false; | ||
| LocateButtonText = "Start Blinking"; | ||
| } | ||
| else | ||
| { | ||
| _signalActive = true; | ||
| var newThread = new Thread(new ThreadStart(LocationService)); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use Observable.Interval from System.Reactive here instead. |
||
| newThread.Start(); | ||
| LocateButtonText = "Stop Blinking"; | ||
| } | ||
| return Unit.Default; | ||
| } | ||
|
|
||
| // Send Signal request endlessly, until the button is pressed again | ||
| private async void LocationService() | ||
| { | ||
| SaveResult result; | ||
| var adapter = await adaptersService.SelectedAdapter.FirstAsync().ToTask(); | ||
|
|
||
| if (adapter == null) return; | ||
| if (Device == null) return; | ||
|
|
||
| while (_signalActive) | ||
| { | ||
| result = await settingsService.SendSignalRequest(adapter, Device.MAC); | ||
|
|
||
| if(!result.Success) | ||
| { | ||
| MessageBox.Show("Device refuse: " + result.ErrorMessage, "Device Error", MessageBoxButton.OK, MessageBoxImage.Exclamation); | ||
| _signalActive = false; | ||
| return; | ||
| } | ||
| Thread.Sleep(4000); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| private async Task<Unit> SaveDeviceSettings() | ||
| { | ||
| var adapter = await adaptersService.SelectedAdapter.FirstAsync().ToTask(); | ||
| if (adapter == null) return Unit.Default; | ||
|
|
||
| if (Device == null || !settingsService.TryParseNetworkConfiguration(Device)) return Unit.Default; | ||
|
|
||
| var result = await settingsService.SendSettings(adapter, Device.MAC, Device); | ||
| var result = await settingsService.SendSettings(adapter, Device.MAC, Device, false); | ||
| if (!result.Success) | ||
| MessageBox.Show("Device refuse: " + result.ErrorMessage, "Device Error", MessageBoxButton.OK, MessageBoxImage.Exclamation); | ||
| else | ||
|
|
@@ -78,16 +151,33 @@ private async Task<Unit> SaveDeviceSettings() | |
| return Unit.Default; | ||
| } | ||
|
|
||
| private async Task<Unit> SavePermanentDeviceSettings() | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please avoid code duplication here |
||
| { | ||
| // TODO permanent saveing setting | ||
| var adapter = await adaptersService.SelectedAdapter.FirstAsync().ToTask(); | ||
| if (adapter == null) return Unit.Default; | ||
|
|
||
| if (Device == null || !settingsService.TryParseNetworkConfiguration(Device)) return Unit.Default; | ||
|
|
||
| var result = await settingsService.SendSettings(adapter, Device.MAC, Device, true); | ||
| if (!result.Success) | ||
| MessageBox.Show("Device refuse: " + result.ErrorMessage, "Device Error", MessageBoxButton.OK, MessageBoxImage.Exclamation); | ||
| else | ||
| MessageBox.Show("All done!", "Device Info", MessageBoxButton.OK, MessageBoxImage.Information); | ||
|
|
||
| public Device Device | ||
| return Unit.Default; | ||
| } | ||
|
|
||
| public Device Device | ||
| { | ||
| get { return device1; } | ||
| get { return _selectedDevice; } | ||
| set | ||
| { | ||
| if (Equals(value, device1)) return; | ||
| device1 = value; | ||
| if (Equals(value, _selectedDevice)) return; | ||
| _selectedDevice = value; | ||
| raisePropertyChanged(); | ||
| } | ||
| } | ||
| IsDeviceSelected = _selectedDevice != null; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use Reactive Ui for that |
||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,9 +11,10 @@ | |
| <Grid> | ||
| <Grid.RowDefinitions> | ||
| <RowDefinition Height="*"/> | ||
| <RowDefinition Height="Auto"/> | ||
| <RowDefinition Height="24.277"/> | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please avoid fix height definition |
||
| <RowDefinition Height="27.723"/> | ||
| </Grid.RowDefinitions> | ||
| <GroupBox Header="Settings" Grid.Row="0" Margin="8,8" DataContext="{Binding Device}" HorizontalAlignment="Left"> | ||
| <GroupBox Header="Settings" Grid.Row="0" Margin="8,8,0,8" DataContext="{Binding Device}" HorizontalAlignment="Left"> | ||
| <Grid> | ||
| <Grid.RowDefinitions> | ||
| <RowDefinition Height="Auto"/> | ||
|
|
@@ -34,14 +35,23 @@ | |
| <Label Grid.Row="3" Grid.Column="0" HorizontalAlignment="Right">Gateway:</Label> | ||
| <TextBox Grid.Row="3" Grid.Column="1" Text="{Binding Gateway, UpdateSourceTrigger=PropertyChanged}" Margin="4" Width="250" HorizontalAlignment="Left"/> | ||
| </Grid> | ||
| </GroupBox> | ||
| <Grid Grid.Row="1" Margin="8" HorizontalAlignment="Left"> | ||
| </GroupBox> | ||
| <Grid Grid.Row="1" Margin="0,4" Grid.RowSpan="2"> | ||
| <Grid.ColumnDefinitions> | ||
| <ColumnDefinition /> | ||
| <ColumnDefinition /> | ||
| <ColumnDefinition Width="20*" /> | ||
| <ColumnDefinition Width="120*"/> | ||
| <ColumnDefinition Width="10*"/> | ||
| <ColumnDefinition Width="120*" /> | ||
| <ColumnDefinition Width="10*"/> | ||
| <ColumnDefinition Width="120*" /> | ||
| <ColumnDefinition Width="10*"/> | ||
| <ColumnDefinition Width="120*" /> | ||
| <ColumnDefinition Width="20*"/> | ||
| </Grid.ColumnDefinitions> | ||
| <Button Grid.Column="0" Margin="0,0,8,0" Padding="8" Command="{Binding SaveCommand}">Save</Button> | ||
| <Button Grid.Column="1" Margin="8,0,0,0" Padding="8" Command="{Binding ResetCommand}">Factory Reset</Button> | ||
| <Button IsEnabled="{Binding IsDeviceSelected, UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" Padding="8" Command="{Binding SaveCommand}" HorizontalAlignment="Left" Width="120" Content="Set" /> | ||
| <Button IsEnabled="{Binding IsDeviceSelected, UpdateSourceTrigger=PropertyChanged}" Grid.Column="3" Padding="8" Command="{Binding SavePermanentCommand}" HorizontalAlignment="Left" Width="120" Content="Set Permanent" /> | ||
| <Button IsEnabled="{Binding IsDeviceSelected, UpdateSourceTrigger=PropertyChanged}" Grid.Column="5" Padding="8" Command="{Binding ResetCommand}" HorizontalAlignment="Left" Width="120" Content="Factory Reset"/> | ||
| <Button IsEnabled="{Binding IsDeviceSelected, UpdateSourceTrigger=PropertyChanged}" Grid.Column="7" Padding="8" Command="{Binding LocateDeviceCommand}" Width ="120" Content="{Binding LocateButtonText, UpdateSourceTrigger=PropertyChanged}" /> | ||
| </Grid> | ||
| </Grid> | ||
| </UserControl> | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we really make two commands for saving settings?
An enum for "Permanent" and "Temporary" would be enough and would avoid code duplication.