From 599b79ceee69c3ff8701b75917547b0bf3fadaa2 Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 30 Sep 2020 15:26:34 +0200 Subject: [PATCH] correctly implemented IHandler --- Client/DataParser.cs | 38 +++++++++++++++++++++-------------- ProftaakRH/BLEHandler.cs | 2 +- ProftaakRH/BikeSimulator.cs | 40 +++---------------------------------- ProftaakRH/IHandler.cs | 11 ++++++++++ 4 files changed, 38 insertions(+), 53 deletions(-) create mode 100644 ProftaakRH/IHandler.cs diff --git a/Client/DataParser.cs b/Client/DataParser.cs index fd1c7e8..5289c63 100644 --- a/Client/DataParser.cs +++ b/Client/DataParser.cs @@ -14,6 +14,7 @@ namespace Client 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"; /// /// makes the json object with LOGIN identifier and username and password /// @@ -62,6 +63,15 @@ namespace Client 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 }); @@ -153,30 +163,28 @@ namespace Client return getMessage(payload, 0x01); } - /// - /// constructs a message with the message and clientId - /// - /// - /// - /// the message ready for sending - public static byte[] getJsonMessage(string message) - { - return getJsonMessage(Encoding.ASCII.GetBytes(message)); - } - public static byte[] getStartSessionJson() { - return getJsonMessage(START_SESSION, null); + return getJsonMessage(START_SESSION); } public static byte[] getStopSessionJson() { - return getJsonMessage(STOP_SESSION, null); + return getJsonMessage(STOP_SESSION); } - public static byte[] getSetResistanceJson() + public static byte[] getSetResistanceJson(float mResistance) { - return null; + dynamic data = new + { + resistance = mResistance + }; + return getJsonMessage(SET_RESISTANCE, data); + } + + public static float getResistanceFromJson(byte[] json) + { + return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.resistance; } diff --git a/ProftaakRH/BLEHandler.cs b/ProftaakRH/BLEHandler.cs index 6baf843..56de648 100644 --- a/ProftaakRH/BLEHandler.cs +++ b/ProftaakRH/BLEHandler.cs @@ -11,7 +11,7 @@ namespace Hardware /// /// BLEHandler class that handles connection and traffic to and from the bike /// - public class BLEHandler + public class BLEHandler : IHandler { List dataReceivers; private BLE bleBike; diff --git a/ProftaakRH/BikeSimulator.cs b/ProftaakRH/BikeSimulator.cs index daea355..3d11209 100644 --- a/ProftaakRH/BikeSimulator.cs +++ b/ProftaakRH/BikeSimulator.cs @@ -98,32 +98,7 @@ namespace Hardware.Simulators return hartByte; } - //Generate an ANT message for resistance - public byte[] GenerateResistance(float percentage) - { - byte[] antMessage = new byte[13]; - antMessage[0] = 0x4A; - antMessage[1] = 0x09; - antMessage[2] = 0x4E; - antMessage[3] = 0x05; - antMessage[4] = 0x30; - for (int i = 5; i < 11; i++) - { - antMessage[i] = 0xFF; - } - antMessage[11] = (byte)Math.Max(Math.Min(Math.Round(percentage / 0.5), 255), 0); - //antMessage[11] = 50; //hardcoded for testing - byte checksum = 0; - for (int i = 0; i < 12; i++) - { - checksum ^= antMessage[i]; - } - - antMessage[12] = checksum;//reminder that i am dumb :P - - return antMessage; - } //Calculates the needed variables //Input perlin value @@ -143,20 +118,11 @@ namespace Hardware.Simulators } //Set resistance in simulated bike - public void setResistance(byte[] bytes) + public void setResistance(float percentage) { - //TODO check if message is correct - if (bytes.Length == 13) - { - this.resistance = Convert.ToDouble(bytes[11]) / 2; - } + this.resistance = (byte)Math.Max(Math.Min(Math.Round(percentage / 0.5), 255), 0); } - } - - //Interface for receiving a message on the simulated bike - interface IHandler - { - void setResistance(byte[] bytes); } + } diff --git a/ProftaakRH/IHandler.cs b/ProftaakRH/IHandler.cs new file mode 100644 index 0000000..c1ce11e --- /dev/null +++ b/ProftaakRH/IHandler.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace ProftaakRH +{ + interface IHandler + { + void setResistance(float percentage); + } +}