From 9ef51ddeca9598c27195d16712f67fb4e4a69dbe Mon Sep 17 00:00:00 2001 From: shinichi Date: Fri, 16 Oct 2020 12:18:03 +0200 Subject: [PATCH 1/4] connecting to doctor after works --- ClientApp/Utils/EngineConnection.cs | 8 +++--- Hashing/DataParser.cs | 4 +++ Server/Client.cs | 10 ++++---- Server/Communication.cs | 40 ++++++++++++++++++++++------- 4 files changed, 44 insertions(+), 18 deletions(-) diff --git a/ClientApp/Utils/EngineConnection.cs b/ClientApp/Utils/EngineConnection.cs index a82dfb7..ec3e5a5 100644 --- a/ClientApp/Utils/EngineConnection.cs +++ b/ClientApp/Utils/EngineConnection.cs @@ -230,9 +230,9 @@ namespace ClientApp.Utils WriteTextMessage(mainCommand.ShowRoute("showRouteFalse", false)); }); }); - setEnvironment(); + setEnvironment(); + - } private void setEnvironment() @@ -298,7 +298,7 @@ namespace ClientApp.Utils { // TODO check if is drawn }); - SendMessageAndOnResponse(stream, mainCommand.showMessage(panelId, "message", lastMessage), "message", + SendMessageAndOnResponse(mainCommand.showMessage(panelId, "message", lastMessage), "message", (message) => { // TODO check if is drawn @@ -322,7 +322,7 @@ namespace ClientApp.Utils ImprovedPerlin improvedPerlin = new ImprovedPerlin(0, LibNoise.NoiseQuality.Best); for (int i = 0; i < 256 * 256; i++) { - height[i] = improvedPerlin.GetValue(x /10, x / 10, x * 100) / 3.5f + 1; + height[i] = improvedPerlin.GetValue(x / 10, x / 10, x * 100) / 3.5f + 1; //if (height[i] > 1.1f) //{ diff --git a/Hashing/DataParser.cs b/Hashing/DataParser.cs index b4b3be5..4c3e995 100644 --- a/Hashing/DataParser.cs +++ b/Hashing/DataParser.cs @@ -128,6 +128,8 @@ namespace Util } byte messageId = bytes[4]; + System.Diagnostics.Debug.WriteLine(Encoding.ASCII.GetString(bytes.Skip(5).ToArray())); + if (messageId == 0x01) { dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(bytes.Skip(5).ToArray())); @@ -225,6 +227,8 @@ namespace Util public static byte[] getNewConnectionJson(string user) { + if (user == null) + throw new ArgumentNullException("user null"); dynamic data = new { username = user diff --git a/Server/Client.cs b/Server/Client.cs index 2184dcf..6e3272a 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -97,16 +97,17 @@ namespace Server switch (identifier) { case DataParser.LOGIN: - handleLogin(payloadbytes); + if (handleLogin(payloadbytes)) + communication.NewLogin(this); break; case DataParser.LOGIN_DOCTOR: - if (communication.doctor != null) + if (communication.Doctor != null) return; if (handleLogin(payloadbytes)) { - communication.doctor = this; - Console.WriteLine("Set doctor to " + communication.doctor + " , this is " + this); + communication.Doctor = this; + Console.WriteLine("Set doctor to " + communication.Doctor + " , this is " + this); } break; case DataParser.START_SESSION: @@ -168,7 +169,6 @@ namespace Server this.username = username; sendMessage(DataParser.getLoginResponse("OK")); sendMessage(DataParser.getStartSessionJson()); - communication.NewLogin(this); return true; } else diff --git a/Server/Communication.cs b/Server/Communication.cs index 1169597..d064de4 100644 --- a/Server/Communication.cs +++ b/Server/Communication.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO.Pipes; using System.Linq; using System.Net.Sockets; @@ -13,7 +14,24 @@ namespace Server { private TcpListener listener; private List clients; - public Client doctor; + private Client mDoctor; + public Client Doctor + { + get + { + return this.mDoctor; + } + set + { + this.mDoctor = value; + this.clients.ForEach((client) => + { + var dinges = DataParser.getNewConnectionJson(client.username); + Debug.WriteLine("foreach " + Encoding.ASCII.GetString(dinges)); + this.mDoctor.sendMessage(dinges); + }); + } + } public Communication(TcpListener listener) { this.listener = listener; @@ -34,7 +52,7 @@ namespace Server var tcpClient = listener.EndAcceptTcpClient(ar); Console.WriteLine($"Client connected from {tcpClient.Client.RemoteEndPoint}"); - clients.Add(new Client(this, tcpClient)); + new Client(this, tcpClient); listener.BeginAcceptTcpClient(new AsyncCallback(OnConnect), null); } @@ -45,15 +63,19 @@ namespace Server public void NewLogin(Client client) { - if (doctor == null) + clients.Add(client); + var dinges = DataParser.getNewConnectionJson(client.username); + Debug.WriteLine("new login" + Encoding.ASCII.GetString(dinges)); + Doctor?.sendMessage(dinges); + } + + public void LogOff(Client client) + { + if (this.Doctor == client) { - doctor = client; + this.Doctor = null; } - else - { - doctor.sendMessage(DataParser.getNewConnectionJson(client.username)); - } - + this.clients.Remove(client); } } } From 44abffd7e7638e4e80eff745318f589b2ebd0143 Mon Sep 17 00:00:00 2001 From: shinichi Date: Fri, 16 Oct 2020 12:18:46 +0200 Subject: [PATCH 2/4] closing windows works --- ClientApp/Utils/Client.cs | 1 + ClientApp/Utils/EngineConnection.cs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/ClientApp/Utils/Client.cs b/ClientApp/Utils/Client.cs index e292e0e..2dd2d3f 100644 --- a/ClientApp/Utils/Client.cs +++ b/ClientApp/Utils/Client.cs @@ -299,6 +299,7 @@ namespace ClientApp.Utils this.stream.Dispose(); this.client.Dispose(); this.handler.stop(); + this.engineConnection.Stop(); } } } diff --git a/ClientApp/Utils/EngineConnection.cs b/ClientApp/Utils/EngineConnection.cs index ec3e5a5..c8ac28e 100644 --- a/ClientApp/Utils/EngineConnection.cs +++ b/ClientApp/Utils/EngineConnection.cs @@ -156,7 +156,7 @@ namespace ClientApp.Utils Console.WriteLine("set tunnel id to " + tunnelId); if (tunnelId == null) { - Write("could not find a valid tunnel id!"); + //Write("could not find a valid tunnel id!"); OnNoTunnelId?.Invoke(); Connected = false; FollowingRoute = false; From 540a640bc66bbbf829dba8a3d816b152c2626801 Mon Sep 17 00:00:00 2001 From: shinichi Date: Fri, 16 Oct 2020 12:49:06 +0200 Subject: [PATCH 3/4] removed debuggers --- Hashing/DataParser.cs | 2 -- Server/Client.cs | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/Hashing/DataParser.cs b/Hashing/DataParser.cs index 4c3e995..9a6bc3e 100644 --- a/Hashing/DataParser.cs +++ b/Hashing/DataParser.cs @@ -128,8 +128,6 @@ namespace Util } byte messageId = bytes[4]; - System.Diagnostics.Debug.WriteLine(Encoding.ASCII.GetString(bytes.Skip(5).ToArray())); - if (messageId == 0x01) { dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(bytes.Skip(5).ToArray())); diff --git a/Server/Client.cs b/Server/Client.cs index 6e3272a..ed8e0c0 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -137,7 +137,7 @@ namespace Server else if (DataParser.isRawData(message)) { // print the raw data - Console.WriteLine(BitConverter.ToString(payloadbytes)); + //Console.WriteLine(BitConverter.ToString(payloadbytes)); // TODO change, checking for length is not that safe if (payloadbytes.Length == 8) { From fd9b32129202f48c53d96a8dd9620b5596949292 Mon Sep 17 00:00:00 2001 From: shinichi Date: Fri, 16 Oct 2020 15:02:19 +0200 Subject: [PATCH 4/4] doctor can see patients who logged on before --- DoctorApp/Utils/Client.cs | 88 ++------------------------- DoctorApp/ViewModels/MainViewModel.cs | 5 +- Server/Client.cs | 4 +- Server/Communication.cs | 8 ++- 4 files changed, 15 insertions(+), 90 deletions(-) diff --git a/DoctorApp/Utils/Client.cs b/DoctorApp/Utils/Client.cs index 26a87fb..668315b 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,28 +104,15 @@ 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: @@ -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/MainViewModel.cs b/DoctorApp/ViewModels/MainViewModel.cs index 2b34b32..52bd0cf 100644 --- a/DoctorApp/ViewModels/MainViewModel.cs +++ b/DoctorApp/ViewModels/MainViewModel.cs @@ -21,11 +21,12 @@ 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 @@ -42,5 +43,5 @@ namespace DoctorApp.ViewModels } } - + } diff --git a/Server/Client.cs b/Server/Client.cs index ed8e0c0..9e87382 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -6,6 +6,7 @@ using Newtonsoft.Json; using ClientApp.Utils; using System.Diagnostics; using Util; +using System.Linq; namespace Server { @@ -168,7 +169,7 @@ namespace Server Console.WriteLine("Log in"); this.username = username; sendMessage(DataParser.getLoginResponse("OK")); - sendMessage(DataParser.getStartSessionJson()); + //sendMessage(DataParser.getStartSessionJson()); return true; } else @@ -185,6 +186,7 @@ namespace Server public void sendMessage(byte[] message) { + Debug.WriteLine("serverclient " + Encoding.ASCII.GetString(message.Skip(5).ToArray())); stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); } diff --git a/Server/Communication.cs b/Server/Communication.cs index d064de4..c5bff8c 100644 --- a/Server/Communication.cs +++ b/Server/Communication.cs @@ -26,8 +26,9 @@ namespace Server this.mDoctor = value; this.clients.ForEach((client) => { - var dinges = DataParser.getNewConnectionJson(client.username); - Debug.WriteLine("foreach " + Encoding.ASCII.GetString(dinges)); + Debug.WriteLine("foreach called for " + client.username); + byte[] dinges = DataParser.getNewConnectionJson(client.username); + Debug.WriteLine("foreach " + Encoding.ASCII.GetString(dinges.Skip(5).ToArray())); this.mDoctor.sendMessage(dinges); }); } @@ -63,7 +64,8 @@ namespace Server public void NewLogin(Client client) { - clients.Add(client); + this.clients.Add(client); + Debug.WriteLine("amount of clients is now " + this.clients.Count); var dinges = DataParser.getNewConnectionJson(client.username); Debug.WriteLine("new login" + Encoding.ASCII.GetString(dinges)); Doctor?.sendMessage(dinges);