diff --git a/ClientApp/Utils/Client.cs b/ClientApp/Utils/Client.cs index 3f0cd33..69f9f1b 100644 --- a/ClientApp/Utils/Client.cs +++ b/ClientApp/Utils/Client.cs @@ -139,12 +139,12 @@ namespace ClientApp.Utils Console.WriteLine("Session started!"); this.sessionRunning = true; if (engineConnection.Connected && !engineConnection.FollowingRoute) engineConnection.StartRouteFollow(); - sendMessage(DataParser.getStartSessionJson()); + Debug.WriteLine("start"); break; case DataParser.STOP_SESSION: Console.WriteLine("Stop session identifier"); this.sessionRunning = false; - sendMessage(DataParser.getStopSessionJson()); + Debug.WriteLine("stop"); break; case DataParser.SET_RESISTANCE: Console.WriteLine("Set resistance identifier"); @@ -181,7 +181,7 @@ namespace ClientApp.Utils /// starts sending a message to the server /// /// the message to send - private void sendMessage(byte[] message) + public void sendMessage(byte[] message) { stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); } @@ -297,6 +297,7 @@ namespace ClientApp.Utils public void Dispose() { Debug.WriteLine("client dispose called"); + sendMessage(DataParser.getDisconnectJson(LoginViewModel.Username)); this.stream.Dispose(); this.client.Dispose(); this.handler.stop(); diff --git a/DoctorApp/Utils/Client.cs b/DoctorApp/Utils/Client.cs index 26a87fb..a55151d 100644 --- a/DoctorApp/Utils/Client.cs +++ b/DoctorApp/Utils/Client.cs @@ -10,7 +10,7 @@ using Util; namespace DoctorApp.Utils { public delegate void EngineCallback(); - public class Client : IDataReceiver + public class Client { private TcpClient client; private NetworkStream stream; @@ -18,8 +18,6 @@ namespace DoctorApp.Utils private bool connected; private byte[] totalBuffer = new byte[1024]; private int totalBufferReceived = 0; - private bool sessionRunning = false; - private IHandler handler = null; private LoginViewModel LoginViewModel; private MainViewModel MainViewModel; private ClientInfoViewModel ClientInfoViewModel; @@ -83,6 +81,8 @@ namespace DoctorApp.Utils string identifier; bool isJson = DataParser.getJsonIdentifier(messageBytes, out identifier); + + Debug.WriteLine("doctor " + Encoding.ASCII.GetString(payloadbytes)); if (isJson) { switch (identifier) @@ -104,32 +104,19 @@ namespace DoctorApp.Utils break; case DataParser.START_SESSION: Console.WriteLine("Session started!"); - this.sessionRunning = true; - sendMessage(DataParser.getStartSessionJson()); break; case DataParser.STOP_SESSION: Console.WriteLine("Stop session identifier"); - this.sessionRunning = false; - sendMessage(DataParser.getStopSessionJson()); break; case DataParser.SET_RESISTANCE: Console.WriteLine("Set resistance identifier"); - if (this.handler == null) - { - Console.WriteLine("handler is null"); - sendMessage(DataParser.getSetResistanceResponseJson(false)); - } - else - { - this.handler.setResistance(DataParser.getResistanceFromJson(payloadbytes)); - sendMessage(DataParser.getSetResistanceResponseJson(true)); - } break; case DataParser.NEW_CONNECTION: + Debug.WriteLine("doctor client new connection"); this.MainViewModel.NewConnectedUser(DataParser.getUsernameFromResponseJson(payloadbytes)); break; case DataParser.DISCONNECT: - this.MainViewModel.NewConnectedUser(DataParser.getUsernameFromResponseJson(payloadbytes)); + this.MainViewModel.DisconnectedUser(DataParser.getUsernameFromResponseJson(payloadbytes)); break; default: Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(payloadbytes)}"); @@ -153,7 +140,7 @@ namespace DoctorApp.Utils /// starts sending a message to the server /// /// the message to send - private void sendMessage(byte[] message) + public void sendMessage(byte[] message) { stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); } @@ -167,63 +154,6 @@ namespace DoctorApp.Utils this.stream.EndWrite(ar); } - #region interface - //maybe move this to other place - /// - /// bpm method for receiving the BPM value from the bluetooth bike or the simulation - /// - /// the message - public void BPM(byte[] bytes) - { - if (!sessionRunning) - { - return; - } - if (bytes == null) - { - throw new ArgumentNullException("no bytes"); - } - byte[] message = DataParser.GetRawDataMessage(bytes); - - - this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); - } - - /// - /// method for receiving the bike message from the bluetooth bike or the simulation - /// - /// the message - public void Bike(byte[] bytes) - { - - if (!sessionRunning) - { - return; - } - if (bytes == null) - { - throw new ArgumentNullException("no bytes"); - } - byte[] message = DataParser.GetRawDataMessage(bytes); - - /* switch (bytes[0]) - { - - case 0x10: - - if (canSendToEngine) engineConnection.BikeSpeed = (bytes[4] | (bytes[5] << 8)) * 0.01f; - break; - case 0x19: - if (canSendToEngine) engineConnection.BikePower = (bytes[5]) | (bytes[6] & 0b00001111) << 8; - break; - }*/ - - - this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); - } - - #endregion - /// /// wether or not the client stream is connected /// @@ -246,15 +176,6 @@ namespace DoctorApp.Utils this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); } - /// - /// sets the handler for the client, so either the bike simulator or the bluetooth bike handler - /// - /// - public void SetHandler(IHandler handler) - { - this.handler = handler; - } - internal void SetLoginViewModel(LoginViewModel loginViewModel) { this.LoginViewModel = loginViewModel; @@ -275,7 +196,6 @@ namespace DoctorApp.Utils Debug.WriteLine("client dispose called"); this.stream.Dispose(); this.client.Dispose(); - this.handler?.stop(); } } } diff --git a/DoctorApp/ViewModels/ClientInfoViewModel.cs b/DoctorApp/ViewModels/ClientInfoViewModel.cs index 54d2df0..c40f6ea 100644 --- a/DoctorApp/ViewModels/ClientInfoViewModel.cs +++ b/DoctorApp/ViewModels/ClientInfoViewModel.cs @@ -1,13 +1,81 @@ -using System; +using DoctorApp.Utils; +using GalaSoft.MvvmLight.Command; +using System; using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.ComponentModel; using System.Text; +using System.Windows.Controls; +using System.Windows.Input; +using Util; namespace DoctorApp.ViewModels { - class ClientInfoViewModel + class ClientInfoViewModel : ObservableObject { + public ObservableCollection ChatLog { get; set; } public string Username { get; set; } - public string TabName { get; set; } + + public string Status { get; set; } + + public int Speed { get; set; } + + public int BPM { get; set; } + + public int Resistance { get; set; } + public int Acc_Power { get; set; } + + public int Curr_Power { get; set; } + + public int Distance { get; set; } + + public ICommand StartSession { get; set; } + + public ICommand StopSession { get; set; } + + public ICommand Chat { get; set; } + + public ICommand ChatToAll { get; set; } + + public ICommand ClientInfo { get; set; } + + public ICommand SetResistance { get; set; } + + public MainWindowViewModel MainWindowViewModel { get; set; } + private Client client; + + public ClientInfoViewModel(MainWindowViewModel mainWindowViewModel) + { + MainWindowViewModel = mainWindowViewModel; + ChatLog = new ObservableCollection(); + client = mainWindowViewModel.client; + + StartSession = new RelayCommand(()=>{ + client.sendMessage(DataParser.getStartSessionJson(Username)); + Status = "Session started"; + }); + + StopSession = new RelayCommand(() => { + client.sendMessage(DataParser.getStopSessionJson(Username)); + Status = "Session stopped, waiting to start again."; + }); + + Chat = new RelayCommand((parameter) => + { + client.sendMessage(DataParser.getChatJson(Username, ((TextBox)parameter).Text)); + ChatLog.Add(DateTime.Now+": "+ ((TextBox)parameter).Text); + }); + + //TODO RelayCommand ChatToAll + + SetResistance = new RelayCommand((parameter) => + { + client.sendMessage(DataParser.getSetResistanceJson(Username, float.Parse(((TextBox)parameter).Text))); + }); + + } + + } } diff --git a/DoctorApp/ViewModels/MainViewModel.cs b/DoctorApp/ViewModels/MainViewModel.cs index 2b34b32..90d4509 100644 --- a/DoctorApp/ViewModels/MainViewModel.cs +++ b/DoctorApp/ViewModels/MainViewModel.cs @@ -11,7 +11,7 @@ namespace DoctorApp.ViewModels { class MainViewModel : ObservableObject { - public ObservableCollection Tabs { get; set; } + public ObservableCollection Tabs { get; set; } public int Selected { get; set; } public MainWindowViewModel MainWindowViewModel { get; set; } @@ -21,26 +21,37 @@ namespace DoctorApp.ViewModels { this.MainWindowViewModel = mainWindowViewModel; client = this.MainWindowViewModel.client; - Tabs= new ObservableCollection(); + Tabs= new ObservableCollection(); } public void NewConnectedUser(string username) { + System.Diagnostics.Debug.WriteLine("CREATING TAB FOR " + username); App.Current.Dispatcher.Invoke((Action)delegate { - Tabs.Add(new ClientInfoViewModel + Tabs.Add(new ClientInfoViewModel(MainWindowViewModel) { Username = username, - TabName = username + Status = "Waiting to start" }); }); } public void DisconnectedUser(string username) { - + App.Current.Dispatcher.Invoke((Action)delegate + { + foreach (ClientInfoViewModel item in Tabs) + { + if (item.Username == username) + { + Tabs.Remove(item); + break; + } + } + }); } } - + } diff --git a/DoctorApp/Views/ClientInfoView.xaml b/DoctorApp/Views/ClientInfoView.xaml index 29eda51..33a0c12 100644 --- a/DoctorApp/Views/ClientInfoView.xaml +++ b/DoctorApp/Views/ClientInfoView.xaml @@ -24,8 +24,8 @@ -