From c5c74a3d7a157aa9425ebc01f0604fd084ae224d Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 14 Oct 2020 14:59:00 +0200 Subject: [PATCH] moved dataparser to util --- ClientApp/Utils/Client.cs | 1 + ClientApp/Utils/DataParser.cs | 207 --------------------- DoctorApp/Utils/Client.cs | 1 + {DoctorApp/Utils => Hashing}/DataParser.cs | 2 +- Hashing/Hashing.projitems | 1 + Server/Client.cs | 1 + Server/Communication.cs | 1 + 7 files changed, 6 insertions(+), 208 deletions(-) delete mode 100644 ClientApp/Utils/DataParser.cs rename {DoctorApp/Utils => Hashing}/DataParser.cs (99%) diff --git a/ClientApp/Utils/Client.cs b/ClientApp/Utils/Client.cs index 5b869fa..16527fd 100644 --- a/ClientApp/Utils/Client.cs +++ b/ClientApp/Utils/Client.cs @@ -5,6 +5,7 @@ using System.Net.Sockets; using System.Text; using ClientApp.ViewModels; using ProftaakRH; +using Util; namespace ClientApp.Utils { diff --git a/ClientApp/Utils/DataParser.cs b/ClientApp/Utils/DataParser.cs deleted file mode 100644 index 44208b6..0000000 --- a/ClientApp/Utils/DataParser.cs +++ /dev/null @@ -1,207 +0,0 @@ -using Newtonsoft.Json; -using Newtonsoft.Json.Serialization; -using System; -using System.Globalization; -using System.Linq; -using System.Runtime.InteropServices.WindowsRuntime; -using System.Text; - -namespace ClientApp.Utils -{ - public class DataParser - { - public const string LOGIN = "LOGIN"; - 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"; - public const string LOGIN_DOCTOR = "LOGIN DOCTOR"; - /// - /// makes the json object with LOGIN identifier and username and password - /// - /// username - /// password - /// json object to ASCII to bytes - public static byte[] GetLoginJson(string mUsername, string mPassword) - { - dynamic json = new - { - identifier = LOGIN, - data = new - { - username = mUsername, - password = mPassword, - } - }; - - return Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(json)); - } - - public static bool GetUsernamePassword(byte[] jsonbytes, out string username, out string password) - { - dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(jsonbytes)); - try - { - username = json.data.username; - password = json.data.password; - return true; - } - catch - { - username = null; - password = null; - return false; - } - } - - private static byte[] getJsonMessage(string mIdentifier, dynamic data) - { - dynamic json = new - { - identifier = mIdentifier, - data - }; - 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 }); - } - - public static string getResponseStatus(byte[] json) - { - return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.status; - } - - /// - /// get the identifier from json - /// - /// json in ASCII - /// gets the identifier - /// if it sucseeded - public static bool getJsonIdentifier(byte[] bytes, out string identifier) - { - if (bytes.Length <= 5) - { - throw new ArgumentException("bytes to short"); - } - byte messageId = bytes[4]; - - if (messageId == 0x01) - { - dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(bytes.Skip(5).ToArray())); - identifier = json.identifier; - return true; - } - else - { - identifier = ""; - return false; - } - } - - /// - /// checks if the de message is raw data according to the protocol - /// - /// message - /// if message contains raw data - public static bool isRawData(byte[] bytes) - { - if (bytes.Length <= 5) - { - throw new ArgumentException("bytes to short"); - } - return bytes[4] == 0x02; - } - - /// - /// constructs a message with the payload, messageId and clientId - /// - /// - /// - /// - /// the message ready for sending - private static byte[] getMessage(byte[] payload, byte messageId) - { - byte[] res = new byte[payload.Length + 5]; - - Array.Copy(BitConverter.GetBytes(payload.Length + 5), 0, res, 0, 4); - res[4] = messageId; - Array.Copy(payload, 0, res, 5, payload.Length); - - return res; - } - - /// - /// constructs a message with the payload and clientId and assumes the payload is raw data - /// - /// - /// - /// the message ready for sending - public static byte[] GetRawDataMessage(byte[] payload) - { - return getMessage(payload, 0x02); - } - - /// - /// constructs a message with the payload and clientId and assumes the payload is json - /// - /// - /// - /// the message ready for sending - public static byte[] getJsonMessage(byte[] payload) - { - return getMessage(payload, 0x01); - } - - public static byte[] getStartSessionJson() - { - return getJsonMessage(START_SESSION); - } - - public static byte[] getStopSessionJson() - { - return getJsonMessage(STOP_SESSION); - } - - public static byte[] getSetResistanceJson(float mResistance) - { - dynamic data = new - { - resistance = mResistance - }; - 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/DoctorApp/Utils/Client.cs b/DoctorApp/Utils/Client.cs index 79c2fba..da77925 100644 --- a/DoctorApp/Utils/Client.cs +++ b/DoctorApp/Utils/Client.cs @@ -5,6 +5,7 @@ using System.Net.Sockets; using System.Text; using DoctorApp.ViewModels; using ProftaakRH; +using Util; namespace DoctorApp.Utils { diff --git a/DoctorApp/Utils/DataParser.cs b/Hashing/DataParser.cs similarity index 99% rename from DoctorApp/Utils/DataParser.cs rename to Hashing/DataParser.cs index b46b993..9403bdb 100644 --- a/DoctorApp/Utils/DataParser.cs +++ b/Hashing/DataParser.cs @@ -6,7 +6,7 @@ using System.Linq; using System.Runtime.InteropServices.WindowsRuntime; using System.Text; -namespace DoctorApp.Utils +namespace Util { public class DataParser { diff --git a/Hashing/Hashing.projitems b/Hashing/Hashing.projitems index 127b36a..73a08de 100644 --- a/Hashing/Hashing.projitems +++ b/Hashing/Hashing.projitems @@ -9,6 +9,7 @@ Hashing + \ No newline at end of file diff --git a/Server/Client.cs b/Server/Client.cs index 0da1408..092fe70 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -5,6 +5,7 @@ using System.Text; using Newtonsoft.Json; using ClientApp.Utils; using System.Diagnostics; +using Util; namespace Server { diff --git a/Server/Communication.cs b/Server/Communication.cs index a8ff762..ab956c9 100644 --- a/Server/Communication.cs +++ b/Server/Communication.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Net.Sockets; using System.Text; using DoctorApp.Utils; +using Util; namespace Server {