using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using System; using System.Diagnostics; using System.Globalization; using System.Linq; using System.Printing.IndexedProperties; using System.Runtime.InteropServices.WindowsRuntime; using System.Security.Cryptography; using System.Text; using System.Windows.Media.Animation; namespace Util { 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 NEW_CONNECTION = "NEW CONNECTION"; public const string DISCONNECT = "DISCONNECT"; public const string LOGIN_DOCTOR = "LOGIN DOCTOR"; public const string MESSAGE = "MESSAGE"; /// /// 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)); } internal static string getNameFromBytesBike(byte[] bytes) { return getName(bytes, 8, bytes.Length - 8); } /// /// converts the given string parameter into a message using our protocol. /// /// the message string to send /// a byte array using our protocol to send the message public static byte[] GetMessageToSend(string messageToSend) { dynamic json = new { identifier = MESSAGE, data = new { message = messageToSend } }; return Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(json)); } internal static string getNameFromBytesBPM(byte[] bytes) { return getName(bytes, 2, bytes.Length - 2); } private static string getName(byte[] bytes , int offset, int lenght) { byte[] nameArray = new byte[lenght]; Array.Copy(bytes, offset, nameArray, 0, lenght); return Encoding.UTF8.GetString(nameArray); } /// /// creates a message for when the doctor wants to log in. /// /// the username of the doctor /// the (hashed) password of the doctor /// a byte array using our protocol that contains the username and password of the doctor public static byte[] LoginAsDoctor(string mUsername, string mPassword) { dynamic json = new { identifier = LOGIN_DOCTOR, data = new { username = mUsername, password = mPassword, } }; return Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(json)); } /// /// gets the username and password from a given message array. /// /// the array of bytes containing the message /// the username variable that the username will be put into /// the password variable that the password will be put into /// true if the username and password were received correctly, false otherwise 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; } } /// /// gets message using our protocol of the given identifier and data. /// /// the identifier string of the message /// the payload data of the message /// a byte array containing the json message with the given parameters, using our protocol. private static byte[] getJsonMessage(string mIdentifier, dynamic data) { dynamic json = new { identifier = mIdentifier, data }; return getMessage(Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(json)), 0x01); } /// /// gets a message using our protocol with only the given identifier string. /// /// the identifier to put into the message /// a byte array containing the json with only the identifier, using our protocol. private static byte[] getJsonMessage(string mIdentifier) { dynamic json = new { identifier = mIdentifier, }; return getMessage(Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(json)), 0x01); } /// /// gets the login response of the given status /// /// the status of the response /// a byte array containing the response for the given status, using our protocol. public static byte[] getLoginResponse(string mStatus) { return getJsonMessage(LOGIN_RESPONSE, new { status = mStatus }); } /// /// gets the status of the given json message /// /// the byte array containing a json message using our protocol /// the response of the message 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 isRawDataBikeServer(byte[] bytes) { if (bytes.Length <= 5) { throw new ArgumentException("bytes to short"); } 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 /// /// /// /// /// 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[] 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) { Debug.WriteLine(BitConverter.ToString(Encoding.ASCII.GetBytes(username))); byte[] nameArray = Encoding.ASCII.GetBytes(username); byte[] total = new byte[nameArray.Length + payload.Length]; Array.Copy(payload, 0, total, 0, payload.Length); Array.Copy(nameArray,0,total,payload.Length,nameArray.Length); return getMessage(total,messageID); } /// /// 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); } /// /// gets the message to start a session with the given user username /// /// the username of the user we want to start the session for /// a byte array containing the message to start the session of the given user, using our protocol. public static byte[] getStartSessionJson(string user) { dynamic data = new { username = user }; return getJsonMessage(START_SESSION, data); } /// /// gets the message to stop a session with the given user username /// /// the username of the user we want to stop the session for /// a byte array containing the message to stop the session of the given user, using our protocol. public static byte[] getStopSessionJson(string user) { dynamic data = new { username = user }; return getJsonMessage(STOP_SESSION, data); } /// /// gets the message to set the resistance of the given user with the given resistance. /// /// the username to set the resistance of. /// the resistance value to set /// a byte array containing a json messsage to set the user's resistance, using our protocol. public static byte[] getSetResistanceJson(string user,float mResistance) { dynamic data = new { username = user, resistance = mResistance }; return getJsonMessage(SET_RESISTANCE, data); } /// /// gets the response message with the given value. /// /// the boolean value to indicate if the operation we want to send a response for was successful or not. /// a byte array containing a json message with the response and the given value. public static byte[] getSetResistanceResponseJson(bool mWorked) { dynamic data = new { worked = mWorked }; return getJsonMessage(SET_RESISTANCE, data); } /// /// gets the message to indicate a new connection for the given user. /// /// the username of the user to start a connection for. /// a byte array containing a json message to indicate a new connection for the given user, using our protocol. public static byte[] getNewConnectionJson(string user) { if (user == null) throw new ArgumentNullException("user null"); dynamic data = new { username = user }; return getJsonMessage(NEW_CONNECTION, data); } /// /// gets the message for when a user has been disconnected. /// /// the username of the user that has been disconnected /// a byte array containing a json message to indicate that the given user has disconnected, using our protocol. public static byte[] getDisconnectJson(string user) { dynamic data = new { username = user }; return getJsonMessage(DISCONNECT, data); } /// /// gets the resistance from the given json message /// /// the json messag /// the resistance that was in the message public static float getResistanceFromJson(byte[] json) { return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.resistance; } /// /// gets the resistance response from the given json message /// /// the byte array containin the json message /// the response of the message, so wether it was successful or not. public static bool getResistanceFromResponseJson(byte[] json) { Debug.WriteLine("got message " + Encoding.ASCII.GetString(json)); return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.worked; } /// /// gets the username from the given response message. /// /// the byte array containin the json message /// the username in the message. public static string getUsernameFromResponseJson(byte[] json) { return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.username; } /// /// gets the chat message from the given json message. /// /// the byte array containin the json message /// the chat message in the json message public static string getChatMessageFromJson(byte[] json) { return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.chat; } /// /// gets the username from the given json message. /// /// the byte array containin the json message /// the username that is in the message public static string getUsernameFromJson(byte[] json) { return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.username; } /// /// gets the byte array with the json message to send a message with the given parameters. /// /// the username of the user that wants to send the message /// the message the user wants to send /// a byte array containing a json message with the username and corresponding message, using our protocol. public static byte[] getChatJson(string user, string message) { dynamic data = new { username = user, chat = message }; return getJsonMessage(MESSAGE, data); } public static byte[] getDataWithoutName(byte[] bytes, int offset, int length) { byte[] data = new byte[length]; Array.Copy(bytes, offset, data, 0, length); return data; } } }