diff --git a/ProfinetTools.Gui/ViewModels/SettingsViewModel.cs b/ProfinetTools.Gui/ViewModels/SettingsViewModel.cs index 8e21f56..cdd874c 100644 --- a/ProfinetTools.Gui/ViewModels/SettingsViewModel.cs +++ b/ProfinetTools.Gui/ViewModels/SettingsViewModel.cs @@ -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 ResetDevice() @@ -62,14 +92,57 @@ private async Task ResetDevice() return Unit.Default; } - private async Task SaveDeviceSettings() + // TODO put some code for flashing in here + private async Task 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)); + 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 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 SaveDeviceSettings() return Unit.Default; } + private async Task SavePermanentDeviceSettings() + { + // 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; + } + } } } \ No newline at end of file diff --git a/ProfinetTools.Gui/Views/SettingsView.xaml b/ProfinetTools.Gui/Views/SettingsView.xaml index 2c6b9e6..8d28f12 100644 --- a/ProfinetTools.Gui/Views/SettingsView.xaml +++ b/ProfinetTools.Gui/Views/SettingsView.xaml @@ -11,9 +11,10 @@ - + + - + @@ -34,14 +35,23 @@ - - + + - - + + + + + + + + + - - +