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)