From 124eee5f1273b7f8081dfccdd21e19299002c6f2 Mon Sep 17 00:00:00 2001 From: shinichi Date: Fri, 9 Oct 2020 11:16:51 +0200 Subject: [PATCH 1/3] implemented stop for BLEHandler and BikeSimulator --- ProftaakRH/BLEHandler.cs | 8 +++++++- ProftaakRH/BikeSimulator.cs | 10 +++++++++- ProftaakRH/IHandler.cs | 2 ++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/ProftaakRH/BLEHandler.cs b/ProftaakRH/BLEHandler.cs index c368072..99b166c 100644 --- a/ProftaakRH/BLEHandler.cs +++ b/ProftaakRH/BLEHandler.cs @@ -44,7 +44,7 @@ namespace Hardware /// public void Connect() { - + BLE bleBike = new BLE(); Thread.Sleep(1000); // We need some time to list available devices @@ -203,5 +203,11 @@ namespace Hardware bleBike.WriteCharacteristic("6e40fec3-b5a3-f393-e0a9-e50e24dcca9e", antMessage); } + + public void stop() + { + bleBike.SubscriptionValueChanged -= BleBike_SubscriptionValueChanged; + bleHeart.SubscriptionValueChanged -= BleBike_SubscriptionValueChanged; + } } } diff --git a/ProftaakRH/BikeSimulator.cs b/ProftaakRH/BikeSimulator.cs index 4dad948..faa8517 100644 --- a/ProftaakRH/BikeSimulator.cs +++ b/ProftaakRH/BikeSimulator.cs @@ -28,6 +28,8 @@ namespace Hardware.Simulators byte[] powerArray; byte[] accPowerArray; + bool running = false; + public BikeSimulator(IDataReceiver dataReceiver) @@ -51,12 +53,14 @@ namespace Hardware.Simulators //Example BLE Message //4A-09-4E-05-19-16-00-FF-28-00-00-20-F0 + this.running = true; + float x = 0.0f; //Perlin for Random values ImprovedPerlin improvedPerlin = new ImprovedPerlin(0, LibNoise.NoiseQuality.Best); - while (true) + while (this.running) { CalculateVariables(improvedPerlin.GetValue(x) + 1); @@ -124,6 +128,10 @@ namespace Hardware.Simulators this.resistance = (byte)Math.Max(Math.Min(Math.Round(percentage / 0.5), 255), 0); } + public void stop() + { + this.running = false; + } } } diff --git a/ProftaakRH/IHandler.cs b/ProftaakRH/IHandler.cs index 4f52042..266f4c2 100644 --- a/ProftaakRH/IHandler.cs +++ b/ProftaakRH/IHandler.cs @@ -7,5 +7,7 @@ namespace ProftaakRH public interface IHandler { void setResistance(float percentage); + + void stop(); } } From 2cb3fd5ec303e9e8a634d4101a98b549b864c40c Mon Sep 17 00:00:00 2001 From: shinichi Date: Fri, 9 Oct 2020 11:33:26 +0200 Subject: [PATCH 2/3] ble and simulation stop when window closes --- ClientApp/Views/MainWindow.xaml.cs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/ClientApp/Views/MainWindow.xaml.cs b/ClientApp/Views/MainWindow.xaml.cs index d718f1e..c86bbde 100644 --- a/ClientApp/Views/MainWindow.xaml.cs +++ b/ClientApp/Views/MainWindow.xaml.cs @@ -16,6 +16,7 @@ using System.Windows.Shapes; using ClientApp.Utils; using Hardware.Simulators; using System.Threading; +using ProftaakRH; namespace ClientApp { @@ -24,31 +25,33 @@ namespace ClientApp /// public partial class MainWindow : Window { + private IHandler handler; public MainWindow() { Client client = new Client(); - InitializeComponent(); DataContext = new MainWindowViewModel(client); - //BLEHandler bLEHandler = new BLEHandler(client); //bLEHandler.Connect(); //client.setHandler(bLEHandler); - BikeSimulator bikeSimulator = new BikeSimulator(client); Thread newThread = new Thread(new ThreadStart(bikeSimulator.StartSimulation)); newThread.Start(); - client.SetHandler(bikeSimulator); + handler = bikeSimulator; + } - + protected override void OnClosed(EventArgs e) + { + base.OnClosed(e); + handler.stop(); } } } From dffeb393370a462bfe9c69e307a2e79a68b10593 Mon Sep 17 00:00:00 2001 From: shinichi Date: Fri, 9 Oct 2020 11:40:52 +0200 Subject: [PATCH 3/3] mainView is now binded --- ClientApp/ViewModels/MainViewModel.cs | 2 +- ClientApp/Views/MainView.xaml | 2 +- Server/Communication.cs | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ClientApp/ViewModels/MainViewModel.cs b/ClientApp/ViewModels/MainViewModel.cs index 872b33a..ee13d90 100644 --- a/ClientApp/ViewModels/MainViewModel.cs +++ b/ClientApp/ViewModels/MainViewModel.cs @@ -9,7 +9,7 @@ namespace ClientApp.ViewModels { public ICommand RetryServerCommand { get; set; } public ICommand RetryVREngineCommand { get; set; } - public MainWindowViewModel MainWindowViewModel; + public MainWindowViewModel MainWindowViewModel { get; set; } public MainViewModel(MainWindowViewModel mainWindowViewModel) diff --git a/ClientApp/Views/MainView.xaml b/ClientApp/Views/MainView.xaml index 14d43b4..72a0ff2 100644 --- a/ClientApp/Views/MainView.xaml +++ b/ClientApp/Views/MainView.xaml @@ -21,7 +21,7 @@ diff --git a/Server/Communication.cs b/Server/Communication.cs index f3432c8..47bdb68 100644 --- a/Server/Communication.cs +++ b/Server/Communication.cs @@ -1,4 +1,4 @@ -using Client; +using ClientApp.Utils; using System; using System.Collections.Generic; using System.IO.Pipes; @@ -31,7 +31,7 @@ namespace Server private void OnConnect(IAsyncResult ar) { - + var tcpClient = listener.EndAcceptTcpClient(ar); Console.WriteLine($"Client connected from {tcpClient.Client.RemoteEndPoint}"); clients.Add(new Client(this, tcpClient));