correctly implemented IHandler
This commit is contained in:
@@ -14,6 +14,7 @@ namespace Client
|
|||||||
public const string LOGIN_RESPONSE = "LOGIN RESPONSE";
|
public const string LOGIN_RESPONSE = "LOGIN RESPONSE";
|
||||||
public const string START_SESSION = "START SESSION";
|
public const string START_SESSION = "START SESSION";
|
||||||
public const string STOP_SESSION = "STOP SESSION";
|
public const string STOP_SESSION = "STOP SESSION";
|
||||||
|
public const string SET_RESISTANCE = "SET RESISTANCE";
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// makes the json object with LOGIN identifier and username and password
|
/// makes the json object with LOGIN identifier and username and password
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -62,6 +63,15 @@ namespace Client
|
|||||||
return getMessage(Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(json)), 0x01);
|
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)
|
public static byte[] getLoginResponse(string mStatus)
|
||||||
{
|
{
|
||||||
return getJsonMessage(LOGIN_RESPONSE, new { status = mStatus });
|
return getJsonMessage(LOGIN_RESPONSE, new { status = mStatus });
|
||||||
@@ -153,30 +163,28 @@ namespace Client
|
|||||||
return getMessage(payload, 0x01);
|
return getMessage(payload, 0x01);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// constructs a message with the message and clientId
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="message"></param>
|
|
||||||
/// <param name="clientId"></param>
|
|
||||||
/// <returns>the message ready for sending</returns>
|
|
||||||
public static byte[] getJsonMessage(string message)
|
|
||||||
{
|
|
||||||
return getJsonMessage(Encoding.ASCII.GetBytes(message));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static byte[] getStartSessionJson()
|
public static byte[] getStartSessionJson()
|
||||||
{
|
{
|
||||||
return getJsonMessage(START_SESSION, null);
|
return getJsonMessage(START_SESSION);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] getStopSessionJson()
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ namespace Hardware
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// <c>BLEHandler</c> class that handles connection and traffic to and from the bike
|
/// <c>BLEHandler</c> class that handles connection and traffic to and from the bike
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class BLEHandler
|
public class BLEHandler : IHandler
|
||||||
{
|
{
|
||||||
List<IDataReceiver> dataReceivers;
|
List<IDataReceiver> dataReceivers;
|
||||||
private BLE bleBike;
|
private BLE bleBike;
|
||||||
|
|||||||
@@ -98,32 +98,7 @@ namespace Hardware.Simulators
|
|||||||
return hartByte;
|
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
|
//Calculates the needed variables
|
||||||
//Input perlin value
|
//Input perlin value
|
||||||
@@ -143,20 +118,11 @@ namespace Hardware.Simulators
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Set resistance in simulated bike
|
//Set resistance in simulated bike
|
||||||
public void setResistance(byte[] bytes)
|
public void setResistance(float percentage)
|
||||||
{
|
{
|
||||||
//TODO check if message is correct
|
this.resistance = (byte)Math.Max(Math.Min(Math.Round(percentage / 0.5), 255), 0);
|
||||||
if (bytes.Length == 13)
|
|
||||||
{
|
|
||||||
this.resistance = Convert.ToDouble(bytes[11]) / 2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Interface for receiving a message on the simulated bike
|
|
||||||
interface IHandler
|
|
||||||
{
|
|
||||||
void setResistance(byte[] bytes);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
11
ProftaakRH/IHandler.cs
Normal file
11
ProftaakRH/IHandler.cs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace ProftaakRH
|
||||||
|
{
|
||||||
|
interface IHandler
|
||||||
|
{
|
||||||
|
void setResistance(float percentage);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user