diff --git a/ClientApp/ClientApp.csproj b/ClientApp/ClientApp.csproj index 13750b2..edc95d3 100644 --- a/ClientApp/ClientApp.csproj +++ b/ClientApp/ClientApp.csproj @@ -29,13 +29,13 @@ Always - Always + PreserveNewest - Always + PreserveNewest - Always + PreserveNewest Always diff --git a/ClientApp/Utils/Client.cs b/ClientApp/Utils/Client.cs index 367fe2d..27d980a 100644 --- a/ClientApp/Utils/Client.cs +++ b/ClientApp/Utils/Client.cs @@ -43,6 +43,7 @@ namespace ClientApp.Utils /// private void initEngine() { + Debug.WriteLine("init engine"); engineConnection = EngineConnection.INSTANCE; engineConnection.OnNoTunnelId = RetryEngineConnection; engineConnection.OnSuccessFullConnection = engineConnected; @@ -88,6 +89,7 @@ namespace ClientApp.Utils /// the result of the async read private void OnRead(IAsyncResult ar) { + Debug.WriteLine("got message in client app"); if (ar == null || (!ar.IsCompleted) || (!this.stream.CanRead)) return; @@ -136,7 +138,7 @@ namespace ClientApp.Utils } break; case DataParser.START_SESSION: - Console.WriteLine("Session started!"); + Debug.WriteLine("Session started!"); this.sessionRunning = true; if (engineConnection.Connected && !engineConnection.FollowingRoute) engineConnection.StartRouteFollow(); Debug.WriteLine("start"); @@ -147,18 +149,33 @@ namespace ClientApp.Utils Debug.WriteLine("stop"); break; case DataParser.SET_RESISTANCE: - Console.WriteLine("Set resistance identifier"); + Debug.WriteLine("Set resistance identifier"); if (this.handler == null) { - Console.WriteLine("handler is null"); + // send that the operation was not successful if the handler is null + Debug.WriteLine("handler is null"); sendMessage(DataParser.getSetResistanceResponseJson(false)); } else { - this.handler.setResistance(DataParser.getResistanceFromJson(payloadbytes)); + // set the resistance in the vr scene and send that it was successful + float resistance = DataParser.getResistanceFromJson(payloadbytes); + Debug.WriteLine("resistance set was " + resistance); + this.handler.setResistance(resistance); + engineConnection.BikeResistance = resistance; sendMessage(DataParser.getSetResistanceResponseJson(true)); } break; + case DataParser.MESSAGE: + Debug.WriteLine("client has received message from doctor"); + engineConnection.DoctorMessage = DataParser.getChatMessageFromJson(payloadbytes); + break; + case DataParser.NEW_CONNECTION: + this.LoginViewModel.DoctorConnected(DataParser.getUsernameFromJson(payloadbytes)); + break; + case DataParser.DISCONNECT: + this.LoginViewModel.DoctorDisconnected(DataParser.getUsernameFromJson(payloadbytes)); + break; default: Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(payloadbytes)}"); break; diff --git a/ClientApp/Utils/EngineConnection.cs b/ClientApp/Utils/EngineConnection.cs index 9ff92e6..5113262 100644 --- a/ClientApp/Utils/EngineConnection.cs +++ b/ClientApp/Utils/EngineConnection.cs @@ -43,8 +43,8 @@ namespace ClientApp.Utils private static string headId = string.Empty; private static string groundPlaneId = string.Empty; private static string terrainId = string.Empty; - private static string lastMessage = "No message received yet"; + public string DoctorMessage { get; set; } public float BikeSpeed { get; set; } public float BikePower { get; set; } public float BikeBPM { get; set; } @@ -65,6 +65,7 @@ namespace ClientApp.Utils BikePower = 0; BikeBPM = 0; BikeResistance = 50; + DoctorMessage = "No message received yet"; updateTimer = new System.Timers.Timer(1000); updateTimer.Elapsed += UpdateTimer_Elapsed; updateTimer.AutoReset = true; @@ -74,7 +75,7 @@ namespace ClientApp.Utils noVRResponseTimer.Elapsed += noVRResponseTimeout; noVRResponseTimer.AutoReset = false; noVRResponseTimer.Enabled = false; - + } private void UpdateTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) @@ -224,7 +225,6 @@ namespace ClientApp.Utils string handLeftId = JSONParser.GetIdSceneInfoChild(message, "LeftHand"); string handRightId = JSONParser.GetIdSceneInfoChild(message, "RightHand"); groundPlaneId = JSONParser.GetIdSceneInfoChild(message, "GroundPlane"); - Write("--- Ground plane id is " + groundPlaneId); }); // add the route and set the route id CreateTerrain(); @@ -324,7 +324,7 @@ namespace ClientApp.Utils { // TODO check if is drawn }); - SendMessageAndOnResponse(mainCommand.showMessage(panelId, "message", lastMessage), "message", + SendMessageAndOnResponse(mainCommand.showMessage(panelId, "message", DoctorMessage), "message", (message) => { // TODO check if is drawn @@ -337,6 +337,7 @@ namespace ClientApp.Utils private void SetFollowSpeed(float speed) { + Write("starting route follow"); WriteTextMessage(mainCommand.RouteFollow(routeId, bikeId, speed, new float[] { 0, -(float)Math.PI / 2f, 0 }, new float[] { 0, 0, 0 })); WriteTextMessage(mainCommand.RouteFollow(routeId, cameraId, speed)); } diff --git a/ClientApp/ViewModels/LoginViewModel.cs b/ClientApp/ViewModels/LoginViewModel.cs index 7b9bfcf..0ec1488 100644 --- a/ClientApp/ViewModels/LoginViewModel.cs +++ b/ClientApp/ViewModels/LoginViewModel.cs @@ -42,5 +42,15 @@ namespace ClientApp.ViewModels this.MainWindowViewModel.SelectedViewModel = new MainViewModel(MainWindowViewModel); } } + + internal void DoctorConnected(string name) + { + this.MainWindowViewModel.InfoModel.DoctorConnected = true; + } + + internal void DoctorDisconnected(string name) + { + this.MainWindowViewModel.InfoModel.DoctorConnected = false; + } } } diff --git a/DoctorApp/App.xaml b/DoctorApp/App.xaml index aac18d8..86b2dd4 100644 --- a/DoctorApp/App.xaml +++ b/DoctorApp/App.xaml @@ -9,11 +9,11 @@ - + - + diff --git a/DoctorApp/DoctorApp.csproj b/DoctorApp/DoctorApp.csproj index 64333af..86a6834 100644 --- a/DoctorApp/DoctorApp.csproj +++ b/DoctorApp/DoctorApp.csproj @@ -9,12 +9,16 @@ + Always + + PreserveNewest + diff --git a/DoctorApp/Utils/Client.cs b/DoctorApp/Utils/Client.cs index dcb56f8..1087346 100644 --- a/DoctorApp/Utils/Client.cs +++ b/DoctorApp/Utils/Client.cs @@ -111,11 +111,10 @@ namespace DoctorApp.Utils Console.WriteLine("Set resistance identifier"); break; case DataParser.NEW_CONNECTION: - Debug.WriteLine("doctor client new connection"); - this.MainViewModel.NewConnectedUser(DataParser.getUsernameFromResponseJson(payloadbytes)); + this.MainViewModel.NewConnectedUser(DataParser.getUsernameFromJson(payloadbytes)); break; case DataParser.DISCONNECT: - this.MainViewModel.DisconnectedUser(DataParser.getUsernameFromResponseJson(payloadbytes)); + this.MainViewModel.DisconnectedUser(DataParser.getUsernameFromJson(payloadbytes)); break; default: Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(payloadbytes)}"); @@ -135,6 +134,8 @@ namespace DoctorApp.Utils expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0); } + if (ar == null || (!ar.IsCompleted) || (!this.stream.CanRead) || !this.client.Connected) + return; this.stream.BeginRead(this.buffer, 0, this.buffer.Length, new AsyncCallback(OnRead), null); } @@ -197,6 +198,7 @@ namespace DoctorApp.Utils public void Dispose() { Debug.WriteLine("client dispose called"); + sendMessage(DataParser.getDisconnectJson(LoginViewModel.Username)); this.stream.Dispose(); this.client.Dispose(); } diff --git a/DoctorApp/ViewModels/ClientInfoViewModel.cs b/DoctorApp/ViewModels/ClientInfoViewModel.cs index 6a81617..ae3d5dd 100644 --- a/DoctorApp/ViewModels/ClientInfoViewModel.cs +++ b/DoctorApp/ViewModels/ClientInfoViewModel.cs @@ -17,7 +17,6 @@ namespace DoctorApp.ViewModels class ClientInfoViewModel : ObservableObject { public PatientInfo PatientInfo { get; set; } - public ObservableCollection ChatLog { get; set; } private string _mySelectedItem; public string MySelectedItem @@ -53,28 +52,24 @@ namespace DoctorApp.ViewModels this.PatientInfo = new PatientInfo() { Username = username, Status = "Waiting to start" }; this.Chart = new Chart(this.PatientInfo); PatientInfo.ChatLog = new ObservableCollection(); - ChatLog = new ObservableCollection(); client = mainWindowViewModel.client; StartSession = new RelayCommand(() => { client.sendMessage(DataParser.getStartSessionJson(PatientInfo.Username)); PatientInfo.Status = "Session started"; - System.Diagnostics.Debug.WriteLine("patient info status" + PatientInfo.Status); }); StopSession = new RelayCommand(() => { client.sendMessage(DataParser.getStopSessionJson(PatientInfo.Username)); PatientInfo.Status = "Session stopped, waiting to start again."; - System.Diagnostics.Debug.WriteLine("patient info status" + PatientInfo.Status); }); Chat = new RelayCommand((parameter) => { client.sendMessage(DataParser.getChatJson(PatientInfo.Username, ((TextBox)parameter).Text)); PatientInfo.ChatLog.Add(DateTime.Now + ": " + ((TextBox)parameter).Text); - ChatLog.Add(DateTime.Now + ":derp: " + ((TextBox)parameter).Text); }); //TODO RelayCommand ChatToAll diff --git a/DoctorApp/ViewModels/MainViewModel.cs b/DoctorApp/ViewModels/MainViewModel.cs index 2c9f787..0c41d33 100644 --- a/DoctorApp/ViewModels/MainViewModel.cs +++ b/DoctorApp/ViewModels/MainViewModel.cs @@ -3,7 +3,7 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; -using System.Security.Cryptography; +using System.Diagnostics; using System.Text; using System.Windows.Controls; using Util; @@ -27,6 +27,7 @@ namespace DoctorApp.ViewModels public void NewConnectedUser(string username) { + Debug.WriteLine("new tab with name " + username); App.Current.Dispatcher.Invoke((Action)delegate { Tabs.Add(new ClientInfoViewModel(MainWindowViewModel, username)); diff --git a/DoctorApp/Views/MainView.xaml b/DoctorApp/Views/MainView.xaml index 80cf8c9..26ab386 100644 --- a/DoctorApp/Views/MainView.xaml +++ b/DoctorApp/Views/MainView.xaml @@ -11,7 +11,10 @@ - + + + + diff --git a/DoctorApp/img/patient.png b/DoctorApp/img/patient.png new file mode 100644 index 0000000..03dda30 Binary files /dev/null and b/DoctorApp/img/patient.png differ diff --git a/Hashing/DataParser.cs b/Hashing/DataParser.cs index ed132cd..17360a0 100644 --- a/Hashing/DataParser.cs +++ b/Hashing/DataParser.cs @@ -1,6 +1,7 @@ using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using System; +using System.Diagnostics; using System.Globalization; using System.Linq; using System.Runtime.InteropServices.WindowsRuntime; @@ -40,7 +41,11 @@ namespace Util return Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(json)); } - + /// + /// converts the given string parameter into a message using our protocol. + /// + /// the message string to send + /// a byte array using our protocol to send the message public static byte[] GetMessageToSend(string messageToSend) { dynamic json = new @@ -54,8 +59,12 @@ namespace Util return Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(json)); } - - + /// + /// creates a message for when the doctor wants to log in. + /// + /// the username of the doctor + /// the (hashed) password of the doctor + /// a byte array using our protocol that contains the username and password of the doctor public static byte[] LoginAsDoctor(string mUsername, string mPassword) { dynamic json = new @@ -71,23 +80,13 @@ namespace Util return Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(json)); } - internal static string getNameFromBytesBike(byte[] bytes) - { - return ASCIIBytesToString(bytes, 8, bytes.Length - 8); - } - - internal static string getNameFromBytesBPM(byte[] bytes) - { - return ASCIIBytesToString(bytes, 2, bytes.Length - 2); - } - - private static string ASCIIBytesToString(byte[] bytes, int offset, int length) - { - byte[] nameArray = new byte[length]; - Array.Copy(bytes, offset, nameArray, 0, length); - return Encoding.UTF8.GetString(nameArray); - } - + /// + /// gets the username and password from a given message array. + /// + /// the array of bytes containing the message + /// the username variable that the username will be put into + /// the password variable that the password will be put into + /// true if the username and password were received correctly, false otherwise public static bool GetUsernamePassword(byte[] jsonbytes, out string username, out string password) { dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(jsonbytes)); @@ -105,6 +104,12 @@ namespace Util } } + /// + /// gets message using our protocol of the given identifier and data. + /// + /// the identifier string of the message + /// the payload data of the message + /// a byte array containing the json message with the given parameters, using our protocol. private static byte[] getJsonMessage(string mIdentifier, dynamic data) { dynamic json = new @@ -115,6 +120,11 @@ namespace Util return getMessage(Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(json)), 0x01); } + /// + /// gets a message using our protocol with only the given identifier string. + /// + /// the identifier to put into the message + /// a byte array containing the json with only the identifier, using our protocol. private static byte[] getJsonMessage(string mIdentifier) { dynamic json = new @@ -124,11 +134,21 @@ namespace Util return getMessage(Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(json)), 0x01); } + /// + /// gets the login response of the given status + /// + /// the status of the response + /// a byte array containing the response for the given status, using our protocol. public static byte[] getLoginResponse(string mStatus) { return getJsonMessage(LOGIN_RESPONSE, new { status = mStatus }); } + /// + /// gets the status of the given json message + /// + /// the byte array containing a json message using our protocol + /// the response of the message public static string getResponseStatus(byte[] json) { return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.status; @@ -266,6 +286,11 @@ namespace Util return getMessage(payload, 0x01); } + /// + /// gets the message to start a session with the given user username + /// + /// the username of the user we want to start the session for + /// a byte array containing the message to start the session of the given user, using our protocol. public static byte[] getStartSessionJson(string user) { dynamic data = new @@ -275,6 +300,11 @@ namespace Util return getJsonMessage(START_SESSION, data); } + /// + /// gets the message to stop a session with the given user username + /// + /// the username of the user we want to stop the session for + /// a byte array containing the message to stop the session of the given user, using our protocol. public static byte[] getStopSessionJson(string user) { dynamic data = new @@ -283,7 +313,13 @@ namespace Util }; return getJsonMessage(STOP_SESSION, data); } - + + /// + /// gets the message to set the resistance of the given user with the given resistance. + /// + /// the username to set the resistance of. + /// the resistance value to set + /// a byte array containing a json messsage to set the user's resistance, using our protocol. public static byte[] getSetResistanceJson(string user,float mResistance) { dynamic data = new @@ -294,6 +330,11 @@ namespace Util return getJsonMessage(SET_RESISTANCE, data); } + /// + /// gets the response message with the given value. + /// + /// the boolean value to indicate if the operation we want to send a response for was successful or not. + /// a byte array containing a json message with the response and the given value. public static byte[] getSetResistanceResponseJson(bool mWorked) { dynamic data = new @@ -303,6 +344,11 @@ namespace Util return getJsonMessage(SET_RESISTANCE, data); } + /// + /// gets the message to indicate a new connection for the given user. + /// + /// the username of the user to start a connection for. + /// a byte array containing a json message to indicate a new connection for the given user, using our protocol. public static byte[] getNewConnectionJson(string user) { if (user == null) @@ -314,6 +360,11 @@ namespace Util return getJsonMessage(NEW_CONNECTION, data); } + /// + /// gets the message for when a user has been disconnected. + /// + /// the username of the user that has been disconnected + /// a byte array containing a json message to indicate that the given user has disconnected, using our protocol. public static byte[] getDisconnectJson(string user) { dynamic data = new @@ -323,31 +374,63 @@ namespace Util return getJsonMessage(DISCONNECT, data); } + /// + /// gets the resistance from the given json message + /// + /// the json messag + /// the resistance that was in the message public static float getResistanceFromJson(byte[] json) { return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.resistance; } + /// + /// gets the resistance response from the given json message + /// + /// the byte array containin the json message + /// the response of the message, so wether it was successful or not. public static bool getResistanceFromResponseJson(byte[] json) { + Debug.WriteLine("got message " + Encoding.ASCII.GetString(json)); return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.worked; } + /// + /// gets the username from the given response message. + /// + /// the byte array containin the json message + /// the username in the message. public static string getUsernameFromResponseJson(byte[] json) { return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.username; } + /// + /// gets the chat message from the given json message. + /// + /// the byte array containin the json message + /// the chat message in the json message public static string getChatMessageFromJson(byte[] json) { return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.chat; } + /// + /// gets the username from the given json message. + /// + /// the byte array containin the json message + /// the username that is in the message public static string getUsernameFromJson(byte[] json) { return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.username; } + /// + /// gets the byte array with the json message to send a message with the given parameters. + /// + /// the username of the user that wants to send the message + /// the message the user wants to send + /// a byte array containing a json message with the username and corresponding message, using our protocol. public static byte[] getChatJson(string user, string message) { dynamic data = new diff --git a/ProftaakRH/BikeSimulator.cs b/ProftaakRH/BikeSimulator.cs index d1c3ae7..e0d0ccd 100644 --- a/ProftaakRH/BikeSimulator.cs +++ b/ProftaakRH/BikeSimulator.cs @@ -93,19 +93,25 @@ namespace Hardware.Simulators //Generate an ANT message for page 0x10 private byte[] GenerateBike0x10() { - //SOMEONE FIX THIS!!!!!!!!! + try { - byte[] bikeByte = { 0x10, Convert.ToByte(equipmentType), Convert.ToByte(elapsedTime * 4 % 64), Convert.ToByte(distanceTraveled), speedArray[0], speedArray[1], Convert.ToByte(BPM), 0xFF }; + byte[] bikeByte = { 0x10, check(equipmentType), check(elapsedTime * 4 % 64), check((int)Math.Round(distanceTraveled)), speedArray[0], speedArray[1], check(BPM), 0xFF }; return bikeByte; } catch (OverflowException e) { Debug.WriteLine(e); - return GenerateBike0x10(); + byte[] res = { 0x10,0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0xFF}; + return res; } } + private byte check(int value) + { + return value > 255 ? Convert.ToByte(255) : Convert.ToByte(value); + } + //Generate an ANT message for BPM private byte[] GenerateHeart() { diff --git a/Server/Client.cs b/Server/Client.cs index f5973f4..b253ad0 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -34,7 +34,7 @@ namespace Server private void OnRead(IAsyncResult ar) { - if (ar == null || (!ar.IsCompleted) || (!this.stream.CanRead)) + if (ar == null || (!ar.IsCompleted) || (!this.stream.CanRead) || !this.tcpClient.Client.Connected) return; int receivedBytes = this.stream.EndRead(ar); @@ -65,6 +65,8 @@ namespace Server } } + if (ar == null || (!ar.IsCompleted) || (!this.stream.CanRead) || !this.tcpClient.Client.Connected) + return; this.stream.BeginRead(this.buffer, 0, this.buffer.Length, new AsyncCallback(OnRead), null); } @@ -117,11 +119,16 @@ namespace Server this.communication.StopSessionUser(DataParser.getUsernameFromJson(payloadbytes)); break; case DataParser.SET_RESISTANCE: - bool worked = DataParser.getResistanceFromResponseJson(payloadbytes); + //bool worked = DataParser.getResistanceFromResponseJson(payloadbytes); + communication.SendMessageToClient(DataParser.getUsernameFromJson(payloadbytes), message); //set resistance on doctor GUI + break; case DataParser.DISCONNECT: - communication.Disconnect(this); + communication.LogOff(this); + break; + case DataParser.MESSAGE: + communication.SendMessageToClient(DataParser.getUsernameFromJson(payloadbytes), message); break; default: Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(payloadbytes)}"); diff --git a/Server/Communication.cs b/Server/Communication.cs index 6e173cd..563efc6 100644 --- a/Server/Communication.cs +++ b/Server/Communication.cs @@ -19,10 +19,12 @@ namespace Server set { this.mDoctor = value; - this.clients.ForEach((client) => - { - this.mDoctor.sendMessage(DataParser.getNewConnectionJson(client.username)); - }); + if (this.mDoctor != null) + this.clients.ForEach((client) => + { + this.mDoctor.sendMessage(DataParser.getNewConnectionJson(client.username)); + client.sendMessage(DataParser.getNewConnectionJson(this.mDoctor.username)); + }); } } public Communication(TcpListener listener) @@ -49,24 +51,27 @@ namespace Server listener.BeginAcceptTcpClient(new AsyncCallback(OnConnect), null); } - internal void Disconnect(Client client) - { - clients.Remove(client); - Doctor.sendMessage(DataParser.getDisconnectJson(client.username)); - } - public void NewLogin(Client client) { this.clients.Add(client); - Doctor?.sendMessage(DataParser.getNewConnectionJson(client.username)); + if (this.Doctor != null) + { + Doctor.sendMessage(DataParser.getNewConnectionJson(client.username)); + client.sendMessage(DataParser.getNewConnectionJson(Doctor.username)); + } } public void LogOff(Client client) { if (this.Doctor == client) { + this.clients.ForEach((client) => + { + client.sendMessage(DataParser.getDisconnectJson(this.mDoctor.username)); + }); this.Doctor = null; } + Doctor?.sendMessage(DataParser.getDisconnectJson(client.username)); this.clients.Remove(client); } @@ -94,5 +99,16 @@ namespace Server } } + + public void SendMessageToClient(string user, byte[] message) + { + foreach (Client c in clients) + { + if (c.username == user) + { + c.sendMessage(message); + } + } + } } }