From 5751bbed8141f98a7e90cf6e9d926e072526d811 Mon Sep 17 00:00:00 2001 From: fabjuuuh Date: Fri, 16 Oct 2020 16:23:50 +0200 Subject: [PATCH] Progress with structure to send data --- DoctorApp/DoctorApp.csproj | 8 +++ DoctorApp/Utils/Client.cs | 11 +-- DoctorApp/ViewModels/ClientInfoViewModel.cs | 12 ++++ DoctorApp/ViewModels/MainViewModel.cs | 25 +++++++ Hashing/DataParser.cs | 77 ++++++++++++++++++++- Server/Client.cs | 26 +++---- 6 files changed, 134 insertions(+), 25 deletions(-) diff --git a/DoctorApp/DoctorApp.csproj b/DoctorApp/DoctorApp.csproj index 1dd8012..152865f 100644 --- a/DoctorApp/DoctorApp.csproj +++ b/DoctorApp/DoctorApp.csproj @@ -6,6 +6,14 @@ true + + true + + + + true + + diff --git a/DoctorApp/Utils/Client.cs b/DoctorApp/Utils/Client.cs index a55151d..00f3832 100644 --- a/DoctorApp/Utils/Client.cs +++ b/DoctorApp/Utils/Client.cs @@ -123,13 +123,14 @@ namespace DoctorApp.Utils break; } } - else if (DataParser.isRawData(messageBytes)) + else if (DataParser.isRawDataBikeDoctor(messageBytes)) { - Console.WriteLine($"Received data: {BitConverter.ToString(payloadbytes)}"); + MainViewModel.TransferDataToClientBike(payloadbytes); + } + else if (DataParser.isRawDataBPMDoctor(messageBytes)) + { + MainViewModel.TransferDataToClientBPM(payloadbytes); } - - totalBufferReceived -= expectedMessageLength; - expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0); } this.stream.BeginRead(this.buffer, 0, this.buffer.Length, new AsyncCallback(OnRead), null); diff --git a/DoctorApp/ViewModels/ClientInfoViewModel.cs b/DoctorApp/ViewModels/ClientInfoViewModel.cs index c40f6ea..faccc0a 100644 --- a/DoctorApp/ViewModels/ClientInfoViewModel.cs +++ b/DoctorApp/ViewModels/ClientInfoViewModel.cs @@ -76,6 +76,18 @@ namespace DoctorApp.ViewModels } + public void BPMData(byte [] bytes) + { + //TODO + //Parsen van de data you fuck + } + + public void BikeData(byte[] bytes) + { + //TODO + //Parsen van de data you fuck + } + } } diff --git a/DoctorApp/ViewModels/MainViewModel.cs b/DoctorApp/ViewModels/MainViewModel.cs index 90d4509..64df762 100644 --- a/DoctorApp/ViewModels/MainViewModel.cs +++ b/DoctorApp/ViewModels/MainViewModel.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; +using System.Security.Cryptography; using System.Text; using System.Windows.Controls; using Util; @@ -51,6 +52,30 @@ namespace DoctorApp.ViewModels } }); } + + public void TransferDataToClientBike(byte[] bytes) + { + string username = DataParser.getNameFromBytes(bytes); + foreach(ClientInfoViewModel item in Tabs) + { + if(item.Username == username) + { + item.BikeData(bytes); + } + } + } + + public void TransferDataToClientBPM(byte[] bytes) + { + string username = DataParser.getNameFromBytes(bytes); + foreach (ClientInfoViewModel item in Tabs) + { + if (item.Username == username) + { + item.BikeData(bytes); + } + } + } } diff --git a/Hashing/DataParser.cs b/Hashing/DataParser.cs index 20851f9..44141dc 100644 --- a/Hashing/DataParser.cs +++ b/Hashing/DataParser.cs @@ -4,6 +4,7 @@ using System; using System.Globalization; using System.Linq; using System.Runtime.InteropServices.WindowsRuntime; +using System.Security.Cryptography; using System.Text; namespace Util @@ -68,6 +69,27 @@ namespace Util return Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(json)); } + internal static string getNameFromBytesBike(byte[] bytes) + { + return ASCIIBytesToString(bytes, 8, bytes.Length - 8); + } + + internal static string getNameFromBytesBPM(byte[] bytes) + { + return ASCIIBytesToString(bytes, 2, bytes.Length - 2); + } + + private static string ASCIIBytesToString(byte[] bytes, int offset, int length) + { + unsafe + { + fixed (byte* pAscii = bytes) + { + return new String((sbyte*)pAscii, offset, length); + } + } + } + public static bool GetUsernamePassword(byte[] jsonbytes, out string username, out string password) { dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(jsonbytes)); @@ -146,7 +168,7 @@ namespace Util /// /// message /// if message contains raw data - public static bool isRawData(byte[] bytes) + public static bool isRawDataBikeServer(byte[] bytes) { if (bytes.Length <= 5) { @@ -155,6 +177,34 @@ namespace Util return bytes[4] == 0x02; } + public static bool isRawDataBPMServer(byte[] bytes) + { + if (bytes.Length <= 5) + { + throw new ArgumentException("bytes to short"); + } + return bytes[4] == 0x03; + } + + public static bool isRawDataBikeDoctor(byte[] bytes) + { + if (bytes.Length <= 5) + { + throw new ArgumentException("bytes to short"); + } + return bytes[4] == 0x04; + } + + public static bool isRawDataBPMDoctor(byte[] bytes) + { + if (bytes.Length <= 5) + { + throw new ArgumentException("bytes to short"); + } + return bytes[4] == 0x05; + } + + /// /// constructs a message with the payload, messageId and clientId /// @@ -179,11 +229,34 @@ namespace Util /// /// /// the message ready for sending - public static byte[] GetRawDataMessage(byte[] payload) + public static byte[] GetRawBikeDataMessageServer(byte[] payload) { return getMessage(payload, 0x02); } + public static byte[] GetRawBPMDataMessageServer(byte[] payload) + { + return getMessage(payload, 0x03); + } + + public static byte[] GetRawBikeDataDoctor(byte[] payload, string username) + { + + return GetRawDataDoctor(payload, username, 0x04); + + } + + public static byte[] GetRawBPMDataDoctor(byte[] payload, string username) + { + return GetRawDataDoctor(payload,username,0x05); + } + + private static byte[] GetRawDataDoctor(byte[] payload, string username, byte messageID) + { + + return getMessage(payload.Concat(Encoding.ASCII.GetBytes(username)).ToArray(), messageID); + + } /// /// constructs a message with the payload and clientId and assumes the payload is json /// diff --git a/Server/Client.cs b/Server/Client.cs index 0d13187..f9625b2 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -136,26 +136,16 @@ namespace Server dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(payloadbytes)); } - else if (DataParser.isRawData(message)) + else if (DataParser.isRawDataBikeServer(message)) { - // print the raw data - //Console.WriteLine(BitConverter.ToString(payloadbytes)); - // TODO change, checking for length is not that safe - 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)); - } + saveData?.WriteDataRAWBike(payloadbytes); + } - - + else if (DataParser.isRawDataBPMServer(message)) + { + saveData?.WriteDataRAWBPM(payloadbytes); + } + } private bool handleLogin(byte[] payloadbytes)