fix login message, implemented IDatareceiver, added Raw and json message

This commit is contained in:
shinichi
2020-09-23 13:46:38 +02:00
parent fd36e420d1
commit f899c90ff7
5 changed files with 67 additions and 12 deletions

View File

@@ -2,10 +2,11 @@
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
using System.Net.Sockets; using System.Net.Sockets;
using ProftaakRH;
namespace Client namespace Client
{ {
class Client class Client : IDataReceiver
{ {
private TcpClient client; private TcpClient client;
private NetworkStream stream; private NetworkStream stream;
@@ -47,14 +48,14 @@ namespace Client
Console.WriteLine("enter password"); Console.WriteLine("enter password");
string password = Console.ReadLine(); string password = Console.ReadLine();
byte[] payload = DataParser.GetLoginJson(username, password); byte[] message = DataParser.getJsonMessage(DataParser.GetLoginJson(username, password));
this.stream.BeginWrite(payload, 0, payload.Length, new AsyncCallback(onWrite), null); this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null);
this.stream.BeginRead(this.buffer, 0, this.buffer.Length, new AsyncCallback(onRead), null); this.stream.BeginRead(this.buffer, 0, this.buffer.Length, new AsyncCallback(OnRead), null);
} }
private void onRead(IAsyncResult ar) private void OnRead(IAsyncResult ar)
{ {
int receivedBytes = this.stream.EndRead(ar); int receivedBytes = this.stream.EndRead(ar);
byte[] lengthBytes = new byte[4]; byte[] lengthBytes = new byte[4];
@@ -72,7 +73,7 @@ namespace Client
{ {
//message hasn't completely arrived yet //message hasn't completely arrived yet
this.bytesReceived += receivedBytes; this.bytesReceived += receivedBytes;
this.stream.BeginRead(this.buffer, this.bytesReceived, this.buffer.Length - this.bytesReceived, new AsyncCallback(onRead), null); this.stream.BeginRead(this.buffer, this.bytesReceived, this.buffer.Length - this.bytesReceived, new AsyncCallback(OnRead), null);
} }
else else
@@ -96,10 +97,34 @@ namespace Client
} }
} }
private void onWrite(IAsyncResult ar) private void OnWrite(IAsyncResult ar)
{ {
this.stream.EndWrite(ar); this.stream.EndWrite(ar);
//stuff idk //stuff idk
} }
#region interface
//maybe move this to other place
public void BPM(byte[] bytes)
{
if (bytes == null)
{
throw new ArgumentNullException("no bytes");
}
byte[] message = DataParser.GetRawDataMessage(bytes);
this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null);
}
public void Bike(byte[] bytes)
{
if (bytes == null)
{
throw new ArgumentNullException("no bytes");
}
byte[] message = DataParser.GetRawDataMessage(bytes);
this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null);
}
#endregion
} }
} }

View File

@@ -11,6 +11,7 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Message\Message.csproj" /> <ProjectReference Include="..\Message\Message.csproj" />
<ProjectReference Include="..\ProftaakRH\ProftaakRH.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -4,6 +4,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
using System.Security.Cryptography;
using System.Text; using System.Text;
namespace Client namespace Client
@@ -54,5 +55,33 @@ namespace Client
} }
return bytes[5] == 0x02; return bytes[5] == 0x02;
} }
private static byte[] getMessage(byte[] payload, byte messageId)
{
byte[] res = new byte[payload.Length + 5];
Array.Copy(BitConverter.GetBytes(payload.Length), 0, res, 0, 4);
res[4] = messageId;
Array.Copy(payload, 0, res, 5, payload.Length);
return res;
}
public static byte[] GetRawDataMessage(byte[] payload)
{
return getMessage(payload, 0x02);
}
public static byte[] getJsonMessage(byte[] payload)
{
return getMessage(payload, 0x01);
}
public static byte[] getJsonMessage(string message)
{
return getJsonMessage(Encoding.ASCII.GetBytes(message));
}
} }
} }

View File

@@ -4,7 +4,7 @@ using System.Text;
namespace ProftaakRH namespace ProftaakRH
{ {
interface IDataReceiver public interface IDataReceiver
{ {
void BPM(byte[] bytes); void BPM(byte[] bytes);
void Bike(byte[] bytes); void Bike(byte[] bytes);

View File

@@ -7,12 +7,12 @@ using Hardware.Simulators;
namespace ProftaakRH namespace ProftaakRH
{ {
class Program class Program
{ {
static void Main(string[] agrs) static void Main(string[] agrs)
{ {
IDataConverter dataConverter = new DataConverter(); IDataReceiver dataReceiver = new DataConverter();
BLEHandler bLEHandler = new BLEHandler(dataConverter); BLEHandler bLEHandler = new BLEHandler(dataReceiver);
//BikeSimulator bikeSimulator = new BikeSimulator(dataConverter); //BikeSimulator bikeSimulator = new BikeSimulator(dataConverter);
//bikeSimulator.setResistance(bikeSimulator.GenerateResistance(1f)); //bikeSimulator.setResistance(bikeSimulator.GenerateResistance(1f));
//bikeSimulator.StartSimulation(); //bikeSimulator.StartSimulation();
@@ -24,7 +24,7 @@ namespace ProftaakRH
string input = Console.ReadLine(); string input = Console.ReadLine();
input.ToLower(); input.ToLower();
input.Trim(); input.Trim();
if(input == "quit") if (input == "quit")
{ {
running = false; running = false;
break; break;