diff --git a/Client/Client.cs b/Client/Client.cs index 40f9d9e..bcbdea9 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -2,10 +2,11 @@ using System.Globalization; using System.Linq; using System.Net.Sockets; +using ProftaakRH; namespace Client { - class Client + class Client : IDataReceiver { private TcpClient client; private NetworkStream stream; @@ -47,14 +48,14 @@ namespace Client Console.WriteLine("enter password"); 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); byte[] lengthBytes = new byte[4]; @@ -72,7 +73,7 @@ namespace Client { //message hasn't completely arrived yet 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 @@ -96,10 +97,34 @@ namespace Client } } - private void onWrite(IAsyncResult ar) + private void OnWrite(IAsyncResult ar) { this.stream.EndWrite(ar); //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 } } diff --git a/Client/Client.csproj b/Client/Client.csproj index bfed53d..dd9af6e 100644 --- a/Client/Client.csproj +++ b/Client/Client.csproj @@ -11,6 +11,7 @@ + diff --git a/Client/DataParser.cs b/Client/DataParser.cs index 4103d5a..cf1f700 100644 --- a/Client/DataParser.cs +++ b/Client/DataParser.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Globalization; using System.Linq; +using System.Security.Cryptography; using System.Text; namespace Client @@ -54,5 +55,33 @@ namespace Client } 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)); + } + + } } diff --git a/ProftaakRH/IDataReceiver.cs b/ProftaakRH/IDataReceiver.cs index ff578dc..7105c47 100644 --- a/ProftaakRH/IDataReceiver.cs +++ b/ProftaakRH/IDataReceiver.cs @@ -4,7 +4,7 @@ using System.Text; namespace ProftaakRH { - interface IDataReceiver + public interface IDataReceiver { void BPM(byte[] bytes); void Bike(byte[] bytes); diff --git a/ProftaakRH/Main.cs b/ProftaakRH/Main.cs index 0938089..eb0c2a5 100644 --- a/ProftaakRH/Main.cs +++ b/ProftaakRH/Main.cs @@ -7,12 +7,12 @@ using Hardware.Simulators; namespace ProftaakRH { - class Program + class Program { static void Main(string[] agrs) { - IDataConverter dataConverter = new DataConverter(); - BLEHandler bLEHandler = new BLEHandler(dataConverter); + IDataReceiver dataReceiver = new DataConverter(); + BLEHandler bLEHandler = new BLEHandler(dataReceiver); //BikeSimulator bikeSimulator = new BikeSimulator(dataConverter); //bikeSimulator.setResistance(bikeSimulator.GenerateResistance(1f)); //bikeSimulator.StartSimulation(); @@ -24,7 +24,7 @@ namespace ProftaakRH string input = Console.ReadLine(); input.ToLower(); input.Trim(); - if(input == "quit") + if (input == "quit") { running = false; break;