Develop #10

Merged
SemvdH merged 229 commits from develop into master 2020-10-29 22:50:49 +00:00
4 changed files with 38 additions and 53 deletions
Showing only changes of commit 599b79ceee - Show all commits

View File

@@ -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";
/// <summary>
/// makes the json object with LOGIN identifier and username and password
/// </summary>
@@ -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);
}
/// <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()
{
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;
}

View File

@@ -11,7 +11,7 @@ namespace Hardware
/// <summary>
/// <c>BLEHandler</c> class that handles connection and traffic to and from the bike
/// </summary>
public class BLEHandler
public class BLEHandler : IHandler
{
List<IDataReceiver> dataReceivers;
private BLE bleBike;

View File

@@ -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);
}
}

11
ProftaakRH/IHandler.cs Normal file
View File

@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace ProftaakRH
{
interface IHandler
{
void setResistance(float percentage);
}
}