diff --git a/ClientApp/ViewModels/LoginViewModel.cs b/ClientApp/ViewModels/LoginViewModel.cs index 0ec1488..3ca025d 100644 --- a/ClientApp/ViewModels/LoginViewModel.cs +++ b/ClientApp/ViewModels/LoginViewModel.cs @@ -16,7 +16,7 @@ namespace ClientApp.ViewModels public string Username { get; set; } public ICommand LoginCommand { get; set; } - public bool LoginStatus { get; set; } + public bool LoginStatus { get; set; } = false; public bool InvertedLoginStatus { get; set; } diff --git a/DoctorApp/DoctorApp.csproj b/DoctorApp/DoctorApp.csproj index 86a6834..9cd21db 100644 --- a/DoctorApp/DoctorApp.csproj +++ b/DoctorApp/DoctorApp.csproj @@ -25,6 +25,7 @@ + diff --git a/DoctorApp/Utils/Client.cs b/DoctorApp/Utils/Client.cs index fd9fd02..02c49ee 100644 --- a/DoctorApp/Utils/Client.cs +++ b/DoctorApp/Utils/Client.cs @@ -1,9 +1,11 @@ using System; using System.Diagnostics; +using System.IO; using System.Linq; using System.Net.Sockets; using System.Text; using DoctorApp.ViewModels; +using Microsoft.Win32; using ProftaakRH; using Util; @@ -123,9 +125,6 @@ namespace DoctorApp.Utils } else if (DataParser.isRawDataBikeDoctor(messageBytes)) { - // 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)) @@ -134,7 +133,18 @@ namespace DoctorApp.Utils } else if (DataParser.IsHistoricBikeData(messageBytes)) { - Debug.WriteLine("[DOCTOR CLIENT] got historic bike data: " + Encoding.ASCII.GetString(payloadbytes)); + + //todo read bytes, they are seperated in pairs of 8 + string result = "PATIENT INFO FOR " + ClientInfoViewModel.PatientInfo.Username + "\n\n"; + for (int i = 0; i < payloadbytes.Length; i += 8) + { + byte[] res = new byte[8]; + Array.Copy(payloadbytes, i, res, 0, 8); + result += handleBikeHistory(res); + } + + string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/" + ClientInfoViewModel.PatientInfo.Username + ".txt"; + File.WriteAllText(path,result); } Array.Copy(totalBuffer, expectedMessageLength, totalBuffer, 0, (totalBufferReceived - expectedMessageLength)); //maybe unsafe idk totalBufferReceived -= expectedMessageLength; @@ -147,11 +157,40 @@ namespace DoctorApp.Utils } - /// - /// starts sending a message to the server - /// - /// the message to send - public void sendMessage(byte[] message) + private string handleBikeHistory(byte[] bytes) + { + string res = string.Empty; + switch (bytes[0]) + { + case 0x10: + res += "Time elapsed: " + bytes[2] / 4 + "s\n"; + res += "Distance traveled: " + bytes[3] + "\n"; + int input = bytes[4] | (bytes[5] << 8); + res += $"Speed {input * 0.01}m/s\n"; + res += $"Heart rate: { Convert.ToString(bytes[6], 2)}"; + break; + case 0x19: + res += $"RPM: {bytes[2]}\n"; + int accumPower = bytes[3] | (bytes[4] << 8); + res += $"Accumulated power: {accumPower} watt\n"; + int instantPower = (bytes[5]) | (bytes[6] & 0b00001111) << 8; + if (instantPower != 0xFFF) + { + res += $"Instant power: {instantPower} watt\n"; + } + break; + + } + return res + "\n"; + + + } + + /// + /// starts sending a message to the server + /// + /// the message to send + public void sendMessage(byte[] message) { stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); } diff --git a/Server/Client.cs b/Server/Client.cs index b8fbb83..c9eeb87 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -192,28 +192,15 @@ namespace Server 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 = directoryInfos.Length-1; i >= 0; i--) - { + DirectoryInfo latest = directoryInfos[directoryInfos.Length - 1]; + path = path + latest.Name + "/rawBike.bin"; + FileInfo fi = new FileInfo(path); - DirectoryInfo info = directoryInfos[i]; - string newPath = path + info.Name + "/rawBike.bin"; - Debug.WriteLine("[SERVER CLIENT] checking for " + newPath); - - FileInfo fi = new FileInfo(newPath); - length += (int)fi.Length; - - if (length >= 1024) break; - string append = Encoding.ASCII.GetString(File.ReadAllBytes(newPath)); - Debug.WriteLine("[SERVER CLIENT] appending " + append); - sb.Append(append); - if (i != 0) sb.Append("\n"); - - } + if ((int)fi.Length >= 1024) return; } catch (Exception e) { @@ -221,7 +208,7 @@ namespace Server } Debug.WriteLine("[SERVER CLIENT] about to send " +sb.ToString()); - communication.Doctor.sendMessage(DataParser.GetFileMessage(Encoding.ASCII.GetBytes(sb.ToString()))); + communication.Doctor.sendMessage(DataParser.GetFileMessage(File.ReadAllBytes(path))); } private bool handleLogin(byte[] payloadbytes)