Merge branch 'develop' of https://github.com/SemvdH/Proftaak-RH-B4 into develop

This commit is contained in:
Sem van der Hoeven
2020-10-02 11:44:30 +02:00
9 changed files with 195 additions and 75 deletions

View File

@@ -15,6 +15,8 @@ namespace Client
private byte[] totalBuffer = new byte[1024];
private int totalBufferReceived = 0;
private EngineConnection engineConnection;
private bool sessionRunning = false;
private IHandler handler = null;
public Client() : this("localhost", 5555)
@@ -48,7 +50,7 @@ namespace Client
private void OnConnect(IAsyncResult ar)
{
this.client.EndConnect(ar);
Console.WriteLine("Verbonden!");
Console.WriteLine("TCP client Verbonden!");
this.stream = this.client.GetStream();
@@ -100,6 +102,26 @@ namespace Client
tryLogin();
}
break;
case DataParser.START_SESSION:
this.sessionRunning = true;
sendMessage(DataParser.getStartSessionJson());
break;
case DataParser.STOP_SESSION:
this.sessionRunning = false;
sendMessage(DataParser.getStopSessionJson());
break;
case DataParser.SET_RESISTANCE:
if (this.handler == null)
{
Console.WriteLine("handler is null");
sendMessage(DataParser.getSetResistanceResponseJson(false));
}
else
{
this.handler.setResistance(DataParser.getResistanceFromJson(payloadbytes));
sendMessage(DataParser.getSetResistanceResponseJson(true));
}
break;
default:
Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(payloadbytes)}");
break;
@@ -118,6 +140,11 @@ namespace Client
}
private void sendMessage(byte[] message)
{
stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null);
}
private void OnWrite(IAsyncResult ar)
{
this.stream.EndWrite(ar);
@@ -127,6 +154,10 @@ namespace Client
//maybe move this to other place
public void BPM(byte[] bytes)
{
if (!sessionRunning)
{
return;
}
if (bytes == null)
{
throw new ArgumentNullException("no bytes");
@@ -137,6 +168,10 @@ namespace Client
public void Bike(byte[] bytes)
{
if (!sessionRunning)
{
return;
}
if (bytes == null)
{
throw new ArgumentNullException("no bytes");
@@ -167,5 +202,10 @@ namespace Client
this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null);
}
public void setHandler(IHandler handler)
{
this.handler = handler;
}
}
}

View File

@@ -3,6 +3,7 @@ using Newtonsoft.Json.Serialization;
using System;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
namespace Client
@@ -10,7 +11,10 @@ namespace Client
public class DataParser
{
public const string LOGIN = "LOGIN";
public const string LOGIN_RESPONSE = "LOGIN_RESPONSE";
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>
@@ -59,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 });
@@ -150,15 +163,42 @@ 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)
public static byte[] getStartSessionJson()
{
return getJsonMessage(Encoding.ASCII.GetBytes(message));
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;
}

View File

@@ -1,7 +1,6 @@
using System;
using Hardware;
using Hardware.Simulators;
using RH_Engine;
namespace Client
{
@@ -20,13 +19,18 @@ namespace Client
{
}
//BLEHandler bLEHandler = new BLEHandler(client);
BLEHandler bLEHandler = new BLEHandler(client);
//bLEHandler.Connect();
bLEHandler.Connect();
BikeSimulator bikeSimulator = new BikeSimulator(client);
client.setHandler(bLEHandler);
bikeSimulator.StartSimulation();
//BikeSimulator bikeSimulator = new BikeSimulator(client);
//bikeSimulator.StartSimulation();
//client.setHandler(bikeSimulator);
while (true)
{