From 6f1ab57fe4e3f1cbdefc6773858b6972f027c7a7 Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 30 Sep 2020 13:00:13 +0200 Subject: [PATCH 1/7] saving in binairy format took forever --- Server/Client.cs | 3 +-- Server/SaveData.cs | 23 ++++++++++++++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/Server/Client.cs b/Server/Client.cs index 5e385be..78abf58 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -123,7 +123,6 @@ namespace Server Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(payloadbytes)}"); break; } - Array.Copy(message, 5, payloadbytes, 0, message.Length - 5); dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(payloadbytes)); saveData.WriteDataJSON(Encoding.ASCII.GetString(payloadbytes)); @@ -131,7 +130,7 @@ namespace Server else if (DataParser.isRawData(message)) { Console.WriteLine(BitConverter.ToString(message)); - saveData.WriteDataRAW(ByteArrayToString(message)); + saveData.WriteDataRAW(message); } diff --git a/Server/SaveData.cs b/Server/SaveData.cs index 0286bde..ffaaf90 100644 --- a/Server/SaveData.cs +++ b/Server/SaveData.cs @@ -22,20 +22,33 @@ namespace Server /// /// Every line is a new data entry /// - + public void WriteDataJSON(string data) { - using (StreamWriter sw = File.AppendText(this.path + "/json"+filename+".txt")) + using (StreamWriter sw = File.AppendText(this.path + "/json" + filename + ".txt")) { sw.WriteLine(data); } } - public void WriteDataRAW(string data) + public void WriteDataRAW(byte[] data) { - using (StreamWriter sw = File.AppendText(this.path + "/raw" + filename + ".txt")) + int length = 0; + try { - sw.WriteLine(data); + FileInfo fi = new FileInfo(this.path + "/raw" + filename + ".bin"); + length = (int)fi.Length; + } + catch + { + + } + using (BinaryWriter sw = new BinaryWriter(File.Open(this.path + "/raw" + filename + ".bin", FileMode.Create))) + { + + Console.WriteLine("head position " + sw.Seek(length, SeekOrigin.End)); + sw.Write(data); + sw.Flush(); } } } From 82f2d6b71c513ff03d59b2ff4bb64676690dff5c Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 30 Sep 2020 14:17:26 +0200 Subject: [PATCH 2/7] saving bike and bpm data in separate files --- Server/Client.cs | 15 +++++++++++++-- Server/SaveData.cs | 29 ++++++++++++++++++++++++----- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/Server/Client.cs b/Server/Client.cs index 78abf58..30d7efc 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -129,8 +129,19 @@ namespace Server } else if (DataParser.isRawData(message)) { - Console.WriteLine(BitConverter.ToString(message)); - saveData.WriteDataRAW(message); + Console.WriteLine(BitConverter.ToString(payloadbytes)); + if (payloadbytes.Length == 8) + { + saveData.WriteDataRAWBike(payloadbytes); + } + else if (payloadbytes.Length == 2) + { + saveData.WriteDataRAWBPM(payloadbytes); + } + else + { + Console.WriteLine("received raw data with weird lenght " + BitConverter.ToString(payloadbytes)); + } } diff --git a/Server/SaveData.cs b/Server/SaveData.cs index ffaaf90..be5412b 100644 --- a/Server/SaveData.cs +++ b/Server/SaveData.cs @@ -31,22 +31,41 @@ namespace Server } } - public void WriteDataRAW(byte[] data) + public void WriteDataRAWBPM(byte[] data) { int length = 0; try { - FileInfo fi = new FileInfo(this.path + "/raw" + filename + ".bin"); + FileInfo fi = new FileInfo(this.path + "/rawBPM" + filename + ".bin"); length = (int)fi.Length; } catch { - + // do nothing } - using (BinaryWriter sw = new BinaryWriter(File.Open(this.path + "/raw" + filename + ".bin", FileMode.Create))) + using (BinaryWriter sw = new BinaryWriter(File.Open(this.path + "/rawBPM" + filename + ".bin", FileMode.Create))) { + sw.Seek(length, SeekOrigin.End); + sw.Write(data); + sw.Flush(); + } + } - Console.WriteLine("head position " + sw.Seek(length, SeekOrigin.End)); + public void WriteDataRAWBike(byte[] data) + { + int length = 0; + try + { + FileInfo fi = new FileInfo(this.path + "/rawBike" + filename + ".bin"); + length = (int)fi.Length; + } + catch + { + // do nothing + } + using (BinaryWriter sw = new BinaryWriter(File.Open(this.path + "/rawBike" + filename + ".bin", FileMode.Create))) + { + sw.Seek(length, SeekOrigin.End); sw.Write(data); sw.Flush(); } From a65c36b8d12d07a6d9e752bd6c63539a813cd9a1 Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 30 Sep 2020 14:24:38 +0200 Subject: [PATCH 3/7] fix bug and more efficient code --- Server/Client.cs | 4 +--- Server/SaveData.cs | 42 +++++++++++++++++++++--------------------- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/Server/Client.cs b/Server/Client.cs index 30d7efc..fc4baca 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -123,9 +123,7 @@ namespace Server Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(payloadbytes)}"); break; } - dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(payloadbytes)); - saveData.WriteDataJSON(Encoding.ASCII.GetString(payloadbytes)); - + saveData?.WriteDataJSON(Encoding.ASCII.GetString(payloadbytes)); } else if (DataParser.isRawData(message)) { diff --git a/Server/SaveData.cs b/Server/SaveData.cs index be5412b..4163471 100644 --- a/Server/SaveData.cs +++ b/Server/SaveData.cs @@ -32,18 +32,36 @@ namespace Server } public void WriteDataRAWBPM(byte[] data) + { + if (data.Length != 2) + { + throw new ArgumentException("data should have length of 2"); + } + WriteRawData(data, this.path + "/rawBPM" + filename + ".bin"); + } + + public void WriteDataRAWBike(byte[] data) + { + if (data.Length != 8) + { + throw new ArgumentException("data should have length of 8"); + } + WriteRawData(data, this.path + "/rawBike" + filename + ".bin"); + } + + private void WriteRawData(byte[] data, string fileLocation) { int length = 0; try { - FileInfo fi = new FileInfo(this.path + "/rawBPM" + filename + ".bin"); + FileInfo fi = new FileInfo(fileLocation); length = (int)fi.Length; } catch { // do nothing } - using (BinaryWriter sw = new BinaryWriter(File.Open(this.path + "/rawBPM" + filename + ".bin", FileMode.Create))) + using (BinaryWriter sw = new BinaryWriter(File.Open(fileLocation, FileMode.Create))) { sw.Seek(length, SeekOrigin.End); sw.Write(data); @@ -51,24 +69,6 @@ namespace Server } } - public void WriteDataRAWBike(byte[] data) - { - int length = 0; - try - { - FileInfo fi = new FileInfo(this.path + "/rawBike" + filename + ".bin"); - length = (int)fi.Length; - } - catch - { - // do nothing - } - using (BinaryWriter sw = new BinaryWriter(File.Open(this.path + "/rawBike" + filename + ".bin", FileMode.Create))) - { - sw.Seek(length, SeekOrigin.End); - sw.Write(data); - sw.Flush(); - } - } + } } From bd8994ad5b2db24ae1af97ef1876246c0c7a7527 Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 30 Sep 2020 15:12:10 +0200 Subject: [PATCH 4/7] added start and stop session --- Client/Client.cs | 21 ++++++++++++++++++++- Client/DataParser.cs | 20 +++++++++++++++++++- Server/Client.cs | 13 ++++++++++--- Server/SaveData.cs | 10 ++++------ 4 files changed, 53 insertions(+), 11 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index e65204f..ca6b53f 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -15,6 +15,7 @@ namespace Client private byte[] totalBuffer = new byte[1024]; private int totalBufferReceived = 0; private EngineConnection engineConnection; + private bool sessionRunning = false; public Client() : this("localhost", 5555) @@ -40,7 +41,7 @@ namespace Client private void OnConnect(IAsyncResult ar) { this.client.EndConnect(ar); - Console.WriteLine("Verbonden!"); + Console.WriteLine("TCP client Verbonden!"); this.stream = this.client.GetStream(); @@ -91,6 +92,16 @@ namespace Client tryLogin(); } break; + case DataParser.START_SESSION: + this.sessionRunning = true; + byte[] startSession = DataParser.getStartSessionJson(); + stream.BeginWrite(startSession, 0, startSession.Length, new AsyncCallback(OnWrite), null); + break; + case DataParser.STOP_SESSION: + this.sessionRunning = false; + byte[] stopSession = DataParser.getStopSessionJson(); + stream.BeginWrite(stopSession, 0, stopSession.Length, new AsyncCallback(OnWrite), null); + break; default: Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(payloadbytes)}"); break; @@ -118,6 +129,10 @@ namespace Client //maybe move this to other place public void BPM(byte[] bytes) { + if (!sessionRunning) + { + return; + } if (bytes == null) { throw new ArgumentNullException("no bytes"); @@ -128,6 +143,10 @@ namespace Client public void Bike(byte[] bytes) { + if (!sessionRunning) + { + return; + } if (bytes == null) { throw new ArgumentNullException("no bytes"); diff --git a/Client/DataParser.cs b/Client/DataParser.cs index c83b86a..fd1c7e8 100644 --- a/Client/DataParser.cs +++ b/Client/DataParser.cs @@ -3,6 +3,7 @@ using Newtonsoft.Json.Serialization; using System; using System.Globalization; using System.Linq; +using System.Runtime.InteropServices.WindowsRuntime; using System.Text; namespace Client @@ -10,7 +11,9 @@ namespace Client public class DataParser { public const string LOGIN = "LOGIN"; - public const string LOGIN_RESPONSE = "LOGIN_RESPONSE"; + public const string LOGIN_RESPONSE = "LOGIN RESPONSE"; + public const string START_SESSION = "START SESSION"; + public const string STOP_SESSION = "STOP SESSION"; /// /// makes the json object with LOGIN identifier and username and password /// @@ -161,6 +164,21 @@ namespace Client return getJsonMessage(Encoding.ASCII.GetBytes(message)); } + public static byte[] getStartSessionJson() + { + return getJsonMessage(START_SESSION, null); + } + + public static byte[] getStopSessionJson() + { + return getJsonMessage(STOP_SESSION, null); + } + + public static byte[] getSetResistanceJson() + { + return null; + } + } } diff --git a/Server/Client.cs b/Server/Client.cs index fc4baca..1b357be 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -105,7 +105,8 @@ namespace Server this.username = username; byte[] response = DataParser.getLoginResponse("OK"); stream.BeginWrite(response, 0, response.Length, new AsyncCallback(OnWrite), null); - this.saveData = new SaveData(Directory.GetCurrentDirectory() + "/" + username, sessionStart.ToString("yyyy-MM-dd HH-mm-ss")); + byte[] startSession = DataParser.getStartSessionJson(); + stream.BeginWrite(startSession, 0, startSession.Length, new AsyncCallback(OnWrite), null); } else { @@ -119,6 +120,12 @@ namespace Server stream.BeginWrite(response, 0, response.Length, new AsyncCallback(OnWrite), null); } break; + case DataParser.START_SESSION: + this.saveData = new SaveData(Directory.GetCurrentDirectory() + "/" + this.username + "/" + sessionStart.ToString("yyyy-MM-dd HH-mm-ss")); + break; + case DataParser.STOP_SESSION: + this.saveData = null; + break; default: Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(payloadbytes)}"); break; @@ -130,11 +137,11 @@ namespace Server Console.WriteLine(BitConverter.ToString(payloadbytes)); if (payloadbytes.Length == 8) { - saveData.WriteDataRAWBike(payloadbytes); + saveData?.WriteDataRAWBike(payloadbytes); } else if (payloadbytes.Length == 2) { - saveData.WriteDataRAWBPM(payloadbytes); + saveData?.WriteDataRAWBPM(payloadbytes); } else { diff --git a/Server/SaveData.cs b/Server/SaveData.cs index 4163471..15fd0d4 100644 --- a/Server/SaveData.cs +++ b/Server/SaveData.cs @@ -8,11 +8,9 @@ namespace Server class SaveData { private string path; - private string filename; - public SaveData(string path, string filename) + public SaveData(string path) { this.path = path; - this.filename = filename; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); @@ -25,7 +23,7 @@ namespace Server public void WriteDataJSON(string data) { - using (StreamWriter sw = File.AppendText(this.path + "/json" + filename + ".txt")) + using (StreamWriter sw = File.AppendText(this.path + "/json" + ".txt")) { sw.WriteLine(data); } @@ -37,7 +35,7 @@ namespace Server { throw new ArgumentException("data should have length of 2"); } - WriteRawData(data, this.path + "/rawBPM" + filename + ".bin"); + WriteRawData(data, this.path + "/rawBPM" + ".bin"); } public void WriteDataRAWBike(byte[] data) @@ -46,7 +44,7 @@ namespace Server { throw new ArgumentException("data should have length of 8"); } - WriteRawData(data, this.path + "/rawBike" + filename + ".bin"); + WriteRawData(data, this.path + "/rawBike" + ".bin"); } private void WriteRawData(byte[] data, string fileLocation) From 599b79ceee69c3ff8701b75917547b0bf3fadaa2 Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 30 Sep 2020 15:26:34 +0200 Subject: [PATCH 5/7] correctly implemented IHandler --- Client/DataParser.cs | 38 +++++++++++++++++++++-------------- ProftaakRH/BLEHandler.cs | 2 +- ProftaakRH/BikeSimulator.cs | 40 +++---------------------------------- ProftaakRH/IHandler.cs | 11 ++++++++++ 4 files changed, 38 insertions(+), 53 deletions(-) create mode 100644 ProftaakRH/IHandler.cs diff --git a/Client/DataParser.cs b/Client/DataParser.cs index fd1c7e8..5289c63 100644 --- a/Client/DataParser.cs +++ b/Client/DataParser.cs @@ -14,6 +14,7 @@ namespace Client public const string LOGIN_RESPONSE = "LOGIN RESPONSE"; public const string START_SESSION = "START SESSION"; public const string STOP_SESSION = "STOP SESSION"; + public const string SET_RESISTANCE = "SET RESISTANCE"; /// /// makes the json object with LOGIN identifier and username and password /// @@ -62,6 +63,15 @@ namespace Client return getMessage(Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(json)), 0x01); } + private static byte[] getJsonMessage(string mIdentifier) + { + dynamic json = new + { + identifier = mIdentifier, + }; + return getMessage(Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(json)), 0x01); + } + public static byte[] getLoginResponse(string mStatus) { return getJsonMessage(LOGIN_RESPONSE, new { status = mStatus }); @@ -153,30 +163,28 @@ namespace Client return getMessage(payload, 0x01); } - /// - /// constructs a message with the message and clientId - /// - /// - /// - /// the message ready for sending - public static byte[] getJsonMessage(string message) - { - return getJsonMessage(Encoding.ASCII.GetBytes(message)); - } - public static byte[] getStartSessionJson() { - return getJsonMessage(START_SESSION, null); + return getJsonMessage(START_SESSION); } public static byte[] getStopSessionJson() { - return getJsonMessage(STOP_SESSION, null); + return getJsonMessage(STOP_SESSION); } - public static byte[] getSetResistanceJson() + public static byte[] getSetResistanceJson(float mResistance) { - return null; + dynamic data = new + { + resistance = mResistance + }; + return getJsonMessage(SET_RESISTANCE, data); + } + + public static float getResistanceFromJson(byte[] json) + { + return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.resistance; } diff --git a/ProftaakRH/BLEHandler.cs b/ProftaakRH/BLEHandler.cs index 6baf843..56de648 100644 --- a/ProftaakRH/BLEHandler.cs +++ b/ProftaakRH/BLEHandler.cs @@ -11,7 +11,7 @@ namespace Hardware /// /// BLEHandler class that handles connection and traffic to and from the bike /// - public class BLEHandler + public class BLEHandler : IHandler { List dataReceivers; private BLE bleBike; diff --git a/ProftaakRH/BikeSimulator.cs b/ProftaakRH/BikeSimulator.cs index daea355..3d11209 100644 --- a/ProftaakRH/BikeSimulator.cs +++ b/ProftaakRH/BikeSimulator.cs @@ -98,32 +98,7 @@ namespace Hardware.Simulators return hartByte; } - //Generate an ANT message for resistance - public byte[] GenerateResistance(float percentage) - { - byte[] antMessage = new byte[13]; - antMessage[0] = 0x4A; - antMessage[1] = 0x09; - antMessage[2] = 0x4E; - antMessage[3] = 0x05; - antMessage[4] = 0x30; - for (int i = 5; i < 11; i++) - { - antMessage[i] = 0xFF; - } - antMessage[11] = (byte)Math.Max(Math.Min(Math.Round(percentage / 0.5), 255), 0); - //antMessage[11] = 50; //hardcoded for testing - byte checksum = 0; - for (int i = 0; i < 12; i++) - { - checksum ^= antMessage[i]; - } - - antMessage[12] = checksum;//reminder that i am dumb :P - - return antMessage; - } //Calculates the needed variables //Input perlin value @@ -143,20 +118,11 @@ namespace Hardware.Simulators } //Set resistance in simulated bike - public void setResistance(byte[] bytes) + public void setResistance(float percentage) { - //TODO check if message is correct - if (bytes.Length == 13) - { - this.resistance = Convert.ToDouble(bytes[11]) / 2; - } + this.resistance = (byte)Math.Max(Math.Min(Math.Round(percentage / 0.5), 255), 0); } - } - - //Interface for receiving a message on the simulated bike - interface IHandler - { - void setResistance(byte[] bytes); } + } diff --git a/ProftaakRH/IHandler.cs b/ProftaakRH/IHandler.cs new file mode 100644 index 0000000..c1ce11e --- /dev/null +++ b/ProftaakRH/IHandler.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace ProftaakRH +{ + interface IHandler + { + void setResistance(float percentage); + } +} From adea08cfb7b0027eae40239c818b2b5d7d2f18c1 Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 30 Sep 2020 16:22:26 +0200 Subject: [PATCH 6/7] server can set resistance --- Client/Client.cs | 16 ++++++++++++++++ Client/Program.cs | 14 +++++++++----- ProftaakRH/BLEHandler.cs | 8 ++++++++ ProftaakRH/IHandler.cs | 2 +- ProftaakRH/Main.cs | 2 +- Server/Client.cs | 18 ++++++++++-------- 6 files changed, 45 insertions(+), 15 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index ca6b53f..d52c349 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -16,6 +16,7 @@ namespace Client private int totalBufferReceived = 0; private EngineConnection engineConnection; private bool sessionRunning = false; + private IHandler handler = null; public Client() : this("localhost", 5555) @@ -102,6 +103,16 @@ namespace Client byte[] stopSession = DataParser.getStopSessionJson(); stream.BeginWrite(stopSession, 0, stopSession.Length, new AsyncCallback(OnWrite), null); break; + case DataParser.SET_RESISTANCE: + if (this.handler == null) + { + Console.WriteLine("handler is null"); + } + else + { + this.handler.setResistance(DataParser.getResistanceFromJson(payloadbytes)); + } + break; default: Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(payloadbytes)}"); break; @@ -174,5 +185,10 @@ namespace Client initEngine(); this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); } + + public void setHandler(IHandler handler) + { + this.handler = handler; + } } } diff --git a/Client/Program.cs b/Client/Program.cs index 9853e51..f8dd547 100644 --- a/Client/Program.cs +++ b/Client/Program.cs @@ -1,7 +1,6 @@ using System; using Hardware; using Hardware.Simulators; -using RH_Engine; namespace Client { @@ -20,13 +19,18 @@ namespace Client { } - //BLEHandler bLEHandler = new BLEHandler(client); + BLEHandler bLEHandler = new BLEHandler(client); - //bLEHandler.Connect(); + bLEHandler.Connect(); - BikeSimulator bikeSimulator = new BikeSimulator(client); + client.setHandler(bLEHandler); - bikeSimulator.StartSimulation(); + + //BikeSimulator bikeSimulator = new BikeSimulator(client); + + //bikeSimulator.StartSimulation(); + + //client.setHandler(bikeSimulator); while (true) { diff --git a/ProftaakRH/BLEHandler.cs b/ProftaakRH/BLEHandler.cs index 56de648..269b0a1 100644 --- a/ProftaakRH/BLEHandler.cs +++ b/ProftaakRH/BLEHandler.cs @@ -25,11 +25,13 @@ namespace Hardware public BLEHandler(IDataReceiver dataReceiver) { this.dataReceivers = new List { dataReceiver }; + } public BLEHandler(List dataReceivers) { this.dataReceivers = dataReceivers; + } public void addDataReceiver(IDataReceiver dataReceiver) @@ -43,6 +45,7 @@ namespace Hardware public void Connect() { BLE bleBike = new BLE(); + Thread.Sleep(1000); // We need some time to list available devices // List available devices @@ -170,6 +173,11 @@ namespace Hardware /// The precentage of resistance to set public void setResistance(float percentage) { + if (!this.Running) + { + Console.WriteLine("BLE is not running"); + return; + } byte[] antMessage = new byte[13]; antMessage[0] = 0x4A; antMessage[1] = 0x09; diff --git a/ProftaakRH/IHandler.cs b/ProftaakRH/IHandler.cs index c1ce11e..4f52042 100644 --- a/ProftaakRH/IHandler.cs +++ b/ProftaakRH/IHandler.cs @@ -4,7 +4,7 @@ using System.Text; namespace ProftaakRH { - interface IHandler + public interface IHandler { void setResistance(float percentage); } diff --git a/ProftaakRH/Main.cs b/ProftaakRH/Main.cs index 5ffcab4..41780f0 100644 --- a/ProftaakRH/Main.cs +++ b/ProftaakRH/Main.cs @@ -14,7 +14,7 @@ namespace ProftaakRH IDataReceiver dataReceiver = new DataConverter(); BLEHandler bLEHandler = new BLEHandler(dataReceiver); BikeSimulator bikeSimulator = new BikeSimulator(dataReceiver); - bikeSimulator.setResistance(bikeSimulator.GenerateResistance(1f)); + bikeSimulator.setResistance(1); bikeSimulator.StartSimulation(); diff --git a/Server/Client.cs b/Server/Client.cs index 1b357be..d486793 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -103,21 +103,17 @@ namespace Server { Console.WriteLine("Log in"); this.username = username; - byte[] response = DataParser.getLoginResponse("OK"); - stream.BeginWrite(response, 0, response.Length, new AsyncCallback(OnWrite), null); - byte[] startSession = DataParser.getStartSessionJson(); - stream.BeginWrite(startSession, 0, startSession.Length, new AsyncCallback(OnWrite), null); + sendMessage(DataParser.getLoginResponse("OK")); + sendMessage(DataParser.getStartSessionJson()); } else { - byte[] response = DataParser.getLoginResponse("wrong username or password"); - stream.BeginWrite(response, 0, response.Length, new AsyncCallback(OnWrite), null); + sendMessage(DataParser.getLoginResponse("wrong username or password")); } } else { - byte[] response = DataParser.getLoginResponse("invalid json"); - stream.BeginWrite(response, 0, response.Length, new AsyncCallback(OnWrite), null); + sendMessage(DataParser.getLoginResponse("invalid json")); } break; case DataParser.START_SESSION: @@ -142,6 +138,7 @@ namespace Server else if (payloadbytes.Length == 2) { saveData?.WriteDataRAWBPM(payloadbytes); + sendMessage(DataParser.getSetResistanceJson(50)); } else { @@ -152,6 +149,11 @@ namespace Server } + private void sendMessage(byte[] message) + { + stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); + } + private bool verifyLogin(string username, string password) { return username == password; From a5e679e6fbb0a3d379f29e06d350d6656b71d957 Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 30 Sep 2020 19:40:37 +0200 Subject: [PATCH 7/7] server now gets response when resistance is set --- Client/Client.cs | 13 +++++++++---- Client/DataParser.cs | 14 ++++++++++++++ Server/Client.cs | 6 +++++- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index d52c349..c6f6f48 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -95,22 +95,22 @@ namespace Client break; case DataParser.START_SESSION: this.sessionRunning = true; - byte[] startSession = DataParser.getStartSessionJson(); - stream.BeginWrite(startSession, 0, startSession.Length, new AsyncCallback(OnWrite), null); + sendMessage(DataParser.getStartSessionJson()); break; case DataParser.STOP_SESSION: this.sessionRunning = false; - byte[] stopSession = DataParser.getStopSessionJson(); - stream.BeginWrite(stopSession, 0, stopSession.Length, new AsyncCallback(OnWrite), null); + sendMessage(DataParser.getStopSessionJson()); break; case DataParser.SET_RESISTANCE: 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; default: @@ -131,6 +131,11 @@ namespace Client } + private void sendMessage(byte[] message) + { + stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); + } + private void OnWrite(IAsyncResult ar) { this.stream.EndWrite(ar); diff --git a/Client/DataParser.cs b/Client/DataParser.cs index 5289c63..8eddb4c 100644 --- a/Client/DataParser.cs +++ b/Client/DataParser.cs @@ -182,11 +182,25 @@ namespace Client return getJsonMessage(SET_RESISTANCE, data); } + public static byte[] getSetResistanceResponseJson(bool mWorked) + { + dynamic data = new + { + worked = mWorked + }; + return getJsonMessage(SET_RESISTANCE, data); + } + public static float getResistanceFromJson(byte[] json) { return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.resistance; } + public static bool getResistanceFromResponseJson(byte[] json) + { + return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.worked; + } + } } diff --git a/Server/Client.cs b/Server/Client.cs index d486793..02abad4 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -122,6 +122,11 @@ namespace Server case DataParser.STOP_SESSION: this.saveData = null; break; + case DataParser.SET_RESISTANCE: + worked = DataParser.getResistanceFromResponseJson(payloadbytes); + Console.WriteLine($"set resistance worked is " + worked); + //set resistance on doctor GUI + break; default: Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(payloadbytes)}"); break; @@ -138,7 +143,6 @@ namespace Server else if (payloadbytes.Length == 2) { saveData?.WriteDataRAWBPM(payloadbytes); - sendMessage(DataParser.getSetResistanceJson(50)); } else {