diff --git a/DoctorApp/Utils/Client.cs b/DoctorApp/Utils/Client.cs index f6b8e02..fd9fd02 100644 --- a/DoctorApp/Utils/Client.cs +++ b/DoctorApp/Utils/Client.cs @@ -125,13 +125,17 @@ namespace DoctorApp.Utils { // read the .bin file that is in the message // update the view - + //Debug.WriteLine($"[DOCTOR CLIENT] Got raw bike data: " + Encoding.ASCII.GetString(payloadbytes)); MainViewModel.TransferDataToClientBike(payloadbytes); } else if (DataParser.isRawDataBPMDoctor(messageBytes)) { MainViewModel.TransferDataToClientBPM(payloadbytes); } + else if (DataParser.IsHistoricBikeData(messageBytes)) + { + Debug.WriteLine("[DOCTOR CLIENT] got historic bike data: " + Encoding.ASCII.GetString(payloadbytes)); + } Array.Copy(totalBuffer, expectedMessageLength, totalBuffer, 0, (totalBufferReceived - expectedMessageLength)); //maybe unsafe idk totalBufferReceived -= expectedMessageLength; expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0); diff --git a/DoctorApp/ViewModels/ClientInfoViewModel.cs b/DoctorApp/ViewModels/ClientInfoViewModel.cs index 6a352e1..655c04f 100644 --- a/DoctorApp/ViewModels/ClientInfoViewModel.cs +++ b/DoctorApp/ViewModels/ClientInfoViewModel.cs @@ -86,7 +86,7 @@ namespace DoctorApp.ViewModels // request the historic data from the server this.SaveHistoricData = new RelayCommand((parameter) => { - this.client.sendMessage(DataParser.GetGetFileMessage(PatientInfo.Username, DateTime.Now)); + this.client.sendMessage(DataParser.GetGetFileMessage(PatientInfo.Username)); // data is stored on the server // send request to server that we want to get the current historic data from the patient // server sends this back diff --git a/Hashing/DataParser.cs b/Hashing/DataParser.cs index 24cca89..e6d661d 100644 --- a/Hashing/DataParser.cs +++ b/Hashing/DataParser.cs @@ -243,6 +243,12 @@ namespace Util return bytes[4] == 0x05; } + public static bool IsHistoricBikeData(byte[] bytes) + { + if (bytes.Length <= 5) throw new ArgumentException("bytes too short"); + return bytes[4] == 0x06; + } + /// /// constructs a message with the payload, messageId and clientId @@ -473,7 +479,7 @@ namespace Util return data; } - public static byte[] GetGetFileMessage(string mUsername, DateTime mDateTime) + public static byte[] GetGetFileMessage(string mUsername) { if (mUsername == null) { @@ -482,7 +488,6 @@ namespace Util dynamic data = new { username = mUsername, - dateTime = mDateTime.ToString("yyyy-MM-dd HH-mm-ss") }; return getJsonMessage(GET_FILE, data); } @@ -499,7 +504,7 @@ namespace Util public static byte[] GetFileMessage(byte[] file) { - return getMessage(file, 0x04); + return getMessage(file, 0x06); } } } diff --git a/Server/Client.cs b/Server/Client.cs index c11e0bc..b4bc940 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -148,26 +148,7 @@ namespace Server communication.SendMessageToClient(DataParser.getUsernameFromJson(payloadbytes), message); break; case DataParser.GET_FILE: - //ugly - - //get the raw bike data of the user with the specified username - string username = DataParser.GetUsernameFromGetFileBytes(payloadbytes); - string path = Directory.GetCurrentDirectory() + "/" + username + "/" + DataParser.GetDateFromGetFileBytes(payloadbytes) + "/rawBike.bin"; - int length = Int32.MaxValue; - try - { - FileInfo fi = new FileInfo(path); - length = (int)fi.Length; - } - catch - { - Console.WriteLine("couldn't find file " + path); - } - if (length > 10240) - break; - - // we need to send it to the doctor and not to the user with the username - communication.SendMessageToClient(this.username, DataParser.GetFileMessage(File.ReadAllBytes(path))); + getClientBikeData(payloadbytes); break; default: Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(payloadbytes)}"); @@ -199,8 +180,57 @@ namespace Server Array.Copy(payloadbytes, 0, this.BPMDataBuffer, 0, 2); } //this.communication.Doctor?.sendMessage(DataParser.GetRawBPMDataDoctor(payloadbytes, this.username)); + } + + } + + private void getClientBikeData(byte[] payloadbytes) + { + //ugly + //get the raw bike data of the user with the specified username + string username = DataParser.GetUsernameFromGetFileBytes(payloadbytes); + string path = Directory.GetCurrentDirectory() + "/" + username + "/"; + string bytes = string.Empty; + StringBuilder sb = new StringBuilder(bytes); + int length = 0; + try + { + DirectoryInfo dirInf = new DirectoryInfo(Directory.GetCurrentDirectory() + "/" + username + "/"); + DirectoryInfo[] directoryInfos = dirInf.GetDirectories(); + for (int i = 0; i < directoryInfos.Length; i++) + { + DirectoryInfo info = directoryInfos[i]; + string newPath = path + info.Name + "/rawBike.bin"; + Debug.WriteLine("[SERVER CLIENT] checking for " + newPath); + + + sb.Append(getRawBikeDataFromFile(newPath)); + if (i != directoryInfos.Length - 1) sb.Append("\n"); + + FileInfo fi = new FileInfo(newPath); + length += (int)fi.Length; + } + + Debug.WriteLine("[SERVER CLIENT] made path to " + path); + } + catch + { + Debug.WriteLine("Folder for user " + username + " does not exist"); } + if (length > 10240) + { + Debug.WriteLine("[SERVER CLIENT] packet was too big!"); + return; + } + + communication.Doctor.sendMessage(DataParser.GetFileMessage(Encoding.ASCII.GetBytes(sb.ToString()))); + } + + public byte[] getRawBikeDataFromFile(string path) + { + + return File.ReadAllBytes(path); } private bool handleLogin(byte[] payloadbytes)