From b391583107cc75412469655a0f3a8376b4b576ae Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 23 Sep 2020 09:57:36 +0200 Subject: [PATCH 01/54] added other project to solution --- ProftaakRH/ProftaakRH.sln | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/ProftaakRH/ProftaakRH.sln b/ProftaakRH/ProftaakRH.sln index a756a82..79f23ee 100644 --- a/ProftaakRH/ProftaakRH.sln +++ b/ProftaakRH/ProftaakRH.sln @@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.30413.136 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProftaakRH", "ProftaakRH.csproj", "{0F053CC5-D969-4970-9501-B3428EA3D777}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProftaakRH", "ProftaakRH.csproj", "{0F053CC5-D969-4970-9501-B3428EA3D777}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RH-Engine", "..\RH-Engine\RH-Engine.csproj", "{984E295E-47A2-41E7-90E5-50FDB9E67694}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -15,6 +17,10 @@ Global {0F053CC5-D969-4970-9501-B3428EA3D777}.Debug|Any CPU.Build.0 = Debug|Any CPU {0F053CC5-D969-4970-9501-B3428EA3D777}.Release|Any CPU.ActiveCfg = Release|Any CPU {0F053CC5-D969-4970-9501-B3428EA3D777}.Release|Any CPU.Build.0 = Release|Any CPU + {984E295E-47A2-41E7-90E5-50FDB9E67694}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {984E295E-47A2-41E7-90E5-50FDB9E67694}.Debug|Any CPU.Build.0 = Debug|Any CPU + {984E295E-47A2-41E7-90E5-50FDB9E67694}.Release|Any CPU.ActiveCfg = Release|Any CPU + {984E295E-47A2-41E7-90E5-50FDB9E67694}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE -- 2.47.2 From 1041aa03918399647100b16c3dc89b0beda8d754 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 23 Sep 2020 10:43:03 +0200 Subject: [PATCH 02/54] added empty server and client projects --- Client/Client.csproj | 8 ++++++++ Client/Program.cs | 12 ++++++++++++ ProftaakRH/ProftaakRH.sln | 12 ++++++++++++ Server/Program.cs | 12 ++++++++++++ Server/Server.csproj | 8 ++++++++ 5 files changed, 52 insertions(+) create mode 100644 Client/Client.csproj create mode 100644 Client/Program.cs create mode 100644 Server/Program.cs create mode 100644 Server/Server.csproj diff --git a/Client/Client.csproj b/Client/Client.csproj new file mode 100644 index 0000000..c73e0d1 --- /dev/null +++ b/Client/Client.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.1 + + + diff --git a/Client/Program.cs b/Client/Program.cs new file mode 100644 index 0000000..920b199 --- /dev/null +++ b/Client/Program.cs @@ -0,0 +1,12 @@ +using System; + +namespace Client +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello World!"); + } + } +} diff --git a/ProftaakRH/ProftaakRH.sln b/ProftaakRH/ProftaakRH.sln index 79f23ee..4ea030c 100644 --- a/ProftaakRH/ProftaakRH.sln +++ b/ProftaakRH/ProftaakRH.sln @@ -7,6 +7,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProftaakRH", "ProftaakRH.cs EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RH-Engine", "..\RH-Engine\RH-Engine.csproj", "{984E295E-47A2-41E7-90E5-50FDB9E67694}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Server", "..\Server\Server.csproj", "{B1AB6F51-A20D-4162-9A7F-B3350B7510FD}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client", "..\Client\Client.csproj", "{5759DD20-7A4F-4D8D-B986-A70A7818C112}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -21,6 +25,14 @@ Global {984E295E-47A2-41E7-90E5-50FDB9E67694}.Debug|Any CPU.Build.0 = Debug|Any CPU {984E295E-47A2-41E7-90E5-50FDB9E67694}.Release|Any CPU.ActiveCfg = Release|Any CPU {984E295E-47A2-41E7-90E5-50FDB9E67694}.Release|Any CPU.Build.0 = Release|Any CPU + {B1AB6F51-A20D-4162-9A7F-B3350B7510FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B1AB6F51-A20D-4162-9A7F-B3350B7510FD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B1AB6F51-A20D-4162-9A7F-B3350B7510FD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B1AB6F51-A20D-4162-9A7F-B3350B7510FD}.Release|Any CPU.Build.0 = Release|Any CPU + {5759DD20-7A4F-4D8D-B986-A70A7818C112}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5759DD20-7A4F-4D8D-B986-A70A7818C112}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5759DD20-7A4F-4D8D-B986-A70A7818C112}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5759DD20-7A4F-4D8D-B986-A70A7818C112}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Server/Program.cs b/Server/Program.cs new file mode 100644 index 0000000..72c96d1 --- /dev/null +++ b/Server/Program.cs @@ -0,0 +1,12 @@ +using System; + +namespace Server +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello World!"); + } + } +} diff --git a/Server/Server.csproj b/Server/Server.csproj new file mode 100644 index 0000000..c73e0d1 --- /dev/null +++ b/Server/Server.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.1 + + + -- 2.47.2 From dcae307754a195d24dd91e66768ef92ff9802b6e Mon Sep 17 00:00:00 2001 From: fabjuuuh Date: Wed, 23 Sep 2020 11:00:45 +0200 Subject: [PATCH 03/54] Classes to server --- Server/Client.cs | 10 ++++++++++ Server/Comms.cs | 10 ++++++++++ 2 files changed, 20 insertions(+) create mode 100644 Server/Client.cs create mode 100644 Server/Comms.cs diff --git a/Server/Client.cs b/Server/Client.cs new file mode 100644 index 0000000..88be22b --- /dev/null +++ b/Server/Client.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Server +{ + class Client + { + } +} diff --git a/Server/Comms.cs b/Server/Comms.cs new file mode 100644 index 0000000..af2a9a1 --- /dev/null +++ b/Server/Comms.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Server +{ + class Comms + { + } +} -- 2.47.2 From a52f1ea7ede510d279669194927eafefb3f05e69 Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 23 Sep 2020 11:12:10 +0200 Subject: [PATCH 04/54] started client --- Client/Client.cs | 31 +++++++++++++++++++++++++++++++ Client/Program.cs | 12 ------------ 2 files changed, 31 insertions(+), 12 deletions(-) create mode 100644 Client/Client.cs delete mode 100644 Client/Program.cs diff --git a/Client/Client.cs b/Client/Client.cs new file mode 100644 index 0000000..b8a1ea7 --- /dev/null +++ b/Client/Client.cs @@ -0,0 +1,31 @@ +using System; +using System.Net.Sockets; + +namespace Client +{ + class Client + { + private TcpClient client; + + static void Main(string[] args) + { + Console.WriteLine("Hello World!"); + //connect fiets + + + + + } + + public Client() : this("localhost", 5555) + { + + } + + public Client(string adress, int port) + { + this.client = new TcpClient(); + client.BeginConnect(adress, 15243, new AsyncCallback(OnConnect), null); + } + } +} diff --git a/Client/Program.cs b/Client/Program.cs deleted file mode 100644 index 920b199..0000000 --- a/Client/Program.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; - -namespace Client -{ - class Program - { - static void Main(string[] args) - { - Console.WriteLine("Hello World!"); - } - } -} -- 2.47.2 From 28242ff052755afb243e55e4da7b167113635239 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 23 Sep 2020 11:50:25 +0200 Subject: [PATCH 05/54] added message class and added references to server and client projects --- Client/Client.csproj | 6 +++++- Message/Message.csproj | 12 ++++++++++++ Message/Program.cs | 34 ++++++++++++++++++++++++++++++++++ ProftaakRH/ProftaakRH.sln | 6 ++++++ Server/Server.csproj | 4 ++++ 5 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 Message/Message.csproj create mode 100644 Message/Program.cs diff --git a/Client/Client.csproj b/Client/Client.csproj index c73e0d1..b664ee3 100644 --- a/Client/Client.csproj +++ b/Client/Client.csproj @@ -1,8 +1,12 @@ - + Exe netcoreapp3.1 + + + + diff --git a/Message/Message.csproj b/Message/Message.csproj new file mode 100644 index 0000000..0eaee1b --- /dev/null +++ b/Message/Message.csproj @@ -0,0 +1,12 @@ + + + + Exe + netcoreapp3.1 + + + + + + + diff --git a/Message/Program.cs b/Message/Program.cs new file mode 100644 index 0000000..f844f99 --- /dev/null +++ b/Message/Program.cs @@ -0,0 +1,34 @@ +using Newtonsoft.Json; +using System; + +namespace Message +{ + public class Message + { + public string Identifier + { + get;set; + } + + public string Payload + { + get;set; + } + + public Message(string identifier, string payload) + { + this.Identifier = identifier; + this.Payload = payload; + } + + public string Serialize() + { + return JsonConvert.SerializeObject(this); + } + + public static Message Deserialize(string json) + { + return (Message)JsonConvert.DeserializeObject(json); + } + } +} diff --git a/ProftaakRH/ProftaakRH.sln b/ProftaakRH/ProftaakRH.sln index 4ea030c..069a13b 100644 --- a/ProftaakRH/ProftaakRH.sln +++ b/ProftaakRH/ProftaakRH.sln @@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Server", "..\Server\Server. EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client", "..\Client\Client.csproj", "{5759DD20-7A4F-4D8D-B986-A70A7818C112}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Message", "..\Message\Message.csproj", "{9ED6832D-B0FB-4460-9BCD-FAA58863B0CE}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -33,6 +35,10 @@ Global {5759DD20-7A4F-4D8D-B986-A70A7818C112}.Debug|Any CPU.Build.0 = Debug|Any CPU {5759DD20-7A4F-4D8D-B986-A70A7818C112}.Release|Any CPU.ActiveCfg = Release|Any CPU {5759DD20-7A4F-4D8D-B986-A70A7818C112}.Release|Any CPU.Build.0 = Release|Any CPU + {9ED6832D-B0FB-4460-9BCD-FAA58863B0CE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9ED6832D-B0FB-4460-9BCD-FAA58863B0CE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9ED6832D-B0FB-4460-9BCD-FAA58863B0CE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9ED6832D-B0FB-4460-9BCD-FAA58863B0CE}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Server/Server.csproj b/Server/Server.csproj index c73e0d1..4c98a00 100644 --- a/Server/Server.csproj +++ b/Server/Server.csproj @@ -5,4 +5,8 @@ netcoreapp3.1 + + + + -- 2.47.2 From 1a67e97f75e123568f2d908bc2b27874340f96fc Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 23 Sep 2020 11:51:22 +0200 Subject: [PATCH 06/54] removed unused boolean warning --- ProftaakRH/BLEHandler.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/ProftaakRH/BLEHandler.cs b/ProftaakRH/BLEHandler.cs index da2d90a..9df61d8 100644 --- a/ProftaakRH/BLEHandler.cs +++ b/ProftaakRH/BLEHandler.cs @@ -24,7 +24,6 @@ namespace Hardware public BLEHandler(IDataConverter dataConverter) { this.dataConverter = dataConverter; - bool running = false; } /// -- 2.47.2 From 603703b3f628596601494fec816b9647b824b3de Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 23 Sep 2020 11:52:49 +0200 Subject: [PATCH 07/54] rename program to message --- Message/Program.cs | 34 ---------------------------------- 1 file changed, 34 deletions(-) delete mode 100644 Message/Program.cs diff --git a/Message/Program.cs b/Message/Program.cs deleted file mode 100644 index f844f99..0000000 --- a/Message/Program.cs +++ /dev/null @@ -1,34 +0,0 @@ -using Newtonsoft.Json; -using System; - -namespace Message -{ - public class Message - { - public string Identifier - { - get;set; - } - - public string Payload - { - get;set; - } - - public Message(string identifier, string payload) - { - this.Identifier = identifier; - this.Payload = payload; - } - - public string Serialize() - { - return JsonConvert.SerializeObject(this); - } - - public static Message Deserialize(string json) - { - return (Message)JsonConvert.DeserializeObject(json); - } - } -} -- 2.47.2 From 31ef1beb821dfe7d4a82d9c667d9bab07d2843cc Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 23 Sep 2020 11:57:20 +0200 Subject: [PATCH 08/54] added comments to message --- Message/Message.cs | 59 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 Message/Message.cs diff --git a/Message/Message.cs b/Message/Message.cs new file mode 100644 index 0000000..f60fe2e --- /dev/null +++ b/Message/Message.cs @@ -0,0 +1,59 @@ +using Newtonsoft.Json; +using System; + +namespace Message +{ + /// + /// Message class to handle traffic between clients and server + /// + public class Message + { + /// + /// identifier for the message + /// + public string Identifier + { + get;set; + } + + /// + /// payload of the message, the actual text + /// + public string Payload + { + get;set; + } + + /// + /// constructs a new message with the given parameters + /// + /// the identifier + /// the payload + public Message(string identifier, string payload) + { + this.Identifier = identifier; + this.Payload = payload; + } + + /// + /// serializes this object to a JSON string + /// + /// a JSON representation of this object + public string Serialize() + { + return JsonConvert.SerializeObject(this); + } + + /// + /// deserializes a JSON string into a new Message object + /// + /// the JSON string to deserialize + /// a new Message object from the JSON string + public static Message Deserialize(string json) + { + return (Message)JsonConvert.DeserializeObject(json); + } + } + + +} -- 2.47.2 From 60cf23d9807592db7d3d7bd5a85e44f1857acb68 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 23 Sep 2020 11:58:36 +0200 Subject: [PATCH 09/54] added identifier enum and made message use it --- Message/Message.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Message/Message.cs b/Message/Message.cs index f60fe2e..3f580fb 100644 --- a/Message/Message.cs +++ b/Message/Message.cs @@ -11,7 +11,7 @@ namespace Message /// /// identifier for the message /// - public string Identifier + public Identifier Identifier { get;set; } @@ -29,7 +29,7 @@ namespace Message /// /// the identifier /// the payload - public Message(string identifier, string payload) + public Message(Identifier identifier, string payload) { this.Identifier = identifier; this.Payload = payload; @@ -55,5 +55,9 @@ namespace Message } } - + public enum Identifier + { + LOGIN, + CHAT, + } } -- 2.47.2 From 797fb1646517c8f929fe4281d62d3cd7303e022b Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 23 Sep 2020 12:00:40 +0200 Subject: [PATCH 10/54] client stuff --- Client/Client.cs | 79 ++++++++++++++++++++++++++++++++++++++++++-- Client/DataParser.cs | 58 ++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+), 3 deletions(-) create mode 100644 Client/DataParser.cs diff --git a/Client/Client.cs b/Client/Client.cs index b8a1ea7..e036a42 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -1,4 +1,6 @@ using System; +using System.Globalization; +using System.Linq; using System.Net.Sockets; namespace Client @@ -6,14 +8,17 @@ namespace Client class Client { private TcpClient client; + private NetworkStream stream; + private byte[] buffer = new byte[1024]; + private int bytesReceived; static void Main(string[] args) { Console.WriteLine("Hello World!"); - //connect fiets - + //connect fiets? + Client client = new Client(); } @@ -25,7 +30,75 @@ namespace Client public Client(string adress, int port) { this.client = new TcpClient(); - client.BeginConnect(adress, 15243, new AsyncCallback(OnConnect), null); + this.bytesReceived = 0; + client.BeginConnect(adress, port, new AsyncCallback(OnConnect), null); + } + + private void OnConnect(IAsyncResult ar) + { + this.client.EndConnect(ar); + Console.WriteLine("Verbonden!"); + this.stream = this.client.GetStream(); + + //TODO File in lezen + Console.WriteLine("enter username"); + string username = Console.ReadLine(); + Console.WriteLine("enter password"); + string password = Console.ReadLine(); + + byte[] payload = DataParser.GetLoginJson(username, password); + + this.stream.BeginWrite(payload, 0, payload.Length, new AsyncCallback(onWrite), null); + + this.stream.BeginRead(this.buffer, 0, this.buffer.Length, new AsyncCallback(onRead), null); + } + + private void onRead(IAsyncResult ar) + { + int receivedBytes = this.stream.EndRead(ar); + byte[] lengthBytes = new byte[4]; + + Array.Copy(this.buffer, 0, lengthBytes, 0, 4); + + int expectedMessageLength = BitConverter.ToInt32(lengthBytes); + + if (expectedMessageLength > this.buffer.Length) + { + throw new OutOfMemoryException("buffer to small"); + } + + if (expectedMessageLength > this.bytesReceived + receivedBytes) + { + //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); + + } + else + { + //message completely arrived + if (expectedMessageLength != this.bytesReceived + receivedBytes) + { + Console.WriteLine("something has gone completely wrong"); + } + + string identifier; + bool isJson = DataParser.getJsonIdentifier(this.buffer, out identifier); + if (isJson) + { + throw new NotImplementedException(); + } + else if (DataParser.isRawData(this.buffer)) + { + throw new NotImplementedException(); + } + } + } + + private void onWrite(IAsyncResult ar) + { + this.stream.EndWrite(ar); + //stuff idk } } } diff --git a/Client/DataParser.cs b/Client/DataParser.cs new file mode 100644 index 0000000..4103d5a --- /dev/null +++ b/Client/DataParser.cs @@ -0,0 +1,58 @@ +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; + +namespace Client +{ + class DataParser + { + public static byte[] GetLoginJson(string mUsername, string mPassword) + { + dynamic json = new + { + identifier = "LOGIN", + data = new + { + username = mUsername, + password = mPassword, + } + }; + + return Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(json)); + } + + public static bool getJsonIdentifier(byte[] bytes, out string identifier) + { + if (bytes.Length <= 5) + { + throw new ArgumentException("bytes to short"); + } + byte messageId = bytes[4]; + + if (messageId == 1) + { + dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(bytes.Skip(5).ToArray())); + identifier = json.identifier; + return true; + } + else + { + identifier = ""; + return false; + } + } + + public static bool isRawData(byte[] bytes) + { + if (bytes.Length <= 5) + { + throw new ArgumentException("bytes to short"); + } + return bytes[5] == 0x02; + } + } +} -- 2.47.2 From 748f7eed4679a4ae250d09c5b3ed5b4c2a36d4e7 Mon Sep 17 00:00:00 2001 From: fabjuuuh Date: Wed, 23 Sep 2020 12:11:32 +0200 Subject: [PATCH 11/54] Server --- Server/Client.cs | 55 +++++++++++++++++++++++++++++++++++++++++ Server/Comms.cs | 10 -------- Server/Communication.cs | 39 +++++++++++++++++++++++++++++ Server/Program.cs | 10 +++++++- Server/Server.csproj | 4 +++ 5 files changed, 107 insertions(+), 11 deletions(-) delete mode 100644 Server/Comms.cs create mode 100644 Server/Communication.cs diff --git a/Server/Client.cs b/Server/Client.cs index 88be22b..4a92aac 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -1,10 +1,65 @@ using System; using System.Collections.Generic; +using System.IO; +using System.Net; +using System.Net.Sockets; using System.Text; +using Newtonsoft; +using Newtonsoft.Json; namespace Server { class Client { + private Communication communication; + private TcpClient tcpClient; + private NetworkStream stream; + private byte[] buffer = new byte[1024]; + private byte[] totalBuffer = new byte[1024]; + + public string Username { get; set; } + + public Client(Communication communication, TcpClient tcpClient) + { + this.communication = communication; + this.tcpClient = tcpClient; + this.stream = this.tcpClient.GetStream(); + stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null); + } + + private void OnRead(IAsyncResult ar) + { + try + { + int receivedBytes = stream.EndRead(ar); + + } + catch (IOException) + { + communication.Disconnect(this); + return; + } + + int counter = 0; + + while (buffer.Length > counter) + { + byte[] lenghtBytes = new byte[4]; + Array.Copy(buffer, counter, lenghtBytes, 0, 4); + int length = Convert.ToInt32(lenghtBytes); + byte[] packet = new byte[length]; + Array.Copy(buffer, counter + 4, packet, 0, length-4); + HandleData(Encoding.Default.GetString(packet)); + counter += length; + } + + stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null); + } + + private void HandleData(string packet) + { + Console.WriteLine(packet); + JsonConvert.DeserializeObject(packet); + } } } diff --git a/Server/Comms.cs b/Server/Comms.cs deleted file mode 100644 index af2a9a1..0000000 --- a/Server/Comms.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace Server -{ - class Comms - { - } -} diff --git a/Server/Communication.cs b/Server/Communication.cs new file mode 100644 index 0000000..1eed0b0 --- /dev/null +++ b/Server/Communication.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.IO.Pipes; +using System.Net.Sockets; +using System.Text; + +namespace Server +{ + class Communication + { + private TcpListener listener; + private List clients; + + public Communication(TcpListener listener) + { + this.listener = listener; + this.clients = new List(); + } + + public void Start() + { + listener.Start(); + listener.BeginAcceptTcpClient(new AsyncCallback(OnConnect), null); + } + + private void OnConnect(IAsyncResult ar) + { + var tcpClient = listener.EndAcceptTcpClient(ar); + Console.WriteLine($"Client connected from {tcpClient.Client.RemoteEndPoint}"); + clients.Add(new Client(this, tcpClient)); + listener.BeginAcceptTcpClient(new AsyncCallback(OnConnect), null); + } + + internal void Disconnect(Client client) + { + clients.Remove(client); + } + } +} diff --git a/Server/Program.cs b/Server/Program.cs index 72c96d1..bf66448 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -1,4 +1,6 @@ using System; +using System.Net; +using System.Net.Sockets; namespace Server { @@ -6,7 +8,13 @@ namespace Server { static void Main(string[] args) { - Console.WriteLine("Hello World!"); + Communication communication = new Communication(new TcpListener(IPAddress.Any, 5555)); + communication.Start(); + + while (true) + { + + } } } } diff --git a/Server/Server.csproj b/Server/Server.csproj index c73e0d1..0eaee1b 100644 --- a/Server/Server.csproj +++ b/Server/Server.csproj @@ -5,4 +5,8 @@ netcoreapp3.1 + + + + -- 2.47.2 From a8d7e0333136109cdf0e0a9d3084983aeaa02a7c Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 23 Sep 2020 12:11:36 +0200 Subject: [PATCH 12/54] added comment to enum --- Message/Message.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Message/Message.cs b/Message/Message.cs index 3f580fb..6852548 100644 --- a/Message/Message.cs +++ b/Message/Message.cs @@ -55,6 +55,9 @@ namespace Message } } + /// + /// Identifier enum for the Message objects + /// public enum Identifier { LOGIN, -- 2.47.2 From 0bf41b5c07b0a165e3e2681e6cf6470746518408 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 23 Sep 2020 12:31:55 +0200 Subject: [PATCH 13/54] fix projects not seeing message? --- Client/Client.csproj | 4 ++++ Message/Message.cs | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/Client/Client.csproj b/Client/Client.csproj index b664ee3..bfed53d 100644 --- a/Client/Client.csproj +++ b/Client/Client.csproj @@ -5,6 +5,10 @@ netcoreapp3.1 + + + + diff --git a/Message/Message.cs b/Message/Message.cs index 3f580fb..1ff6812 100644 --- a/Message/Message.cs +++ b/Message/Message.cs @@ -8,6 +8,11 @@ namespace Message /// public class Message { + + public static void Main(string[] args) + { + + } /// /// identifier for the message /// -- 2.47.2 From e0e910ac25a0dead87fe30576583060c0dcf54b4 Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 23 Sep 2020 13:06:03 +0200 Subject: [PATCH 14/54] added newtonsoft to proftaakRH --- ProftaakRH/ProftaakRH.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/ProftaakRH/ProftaakRH.csproj b/ProftaakRH/ProftaakRH.csproj index 8f98a2c..d6280ab 100644 --- a/ProftaakRH/ProftaakRH.csproj +++ b/ProftaakRH/ProftaakRH.csproj @@ -7,6 +7,7 @@ + -- 2.47.2 From e315e1cf3f7e35b550e2b704abf85e396ad29a05 Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 23 Sep 2020 13:14:49 +0200 Subject: [PATCH 15/54] edited clients --- Client/Client.cs | 5 +++-- Server/Client.cs | 5 ++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index e036a42..40f9d9e 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -19,9 +19,10 @@ namespace Client Client client = new Client(); - + while (true) + { + } } - public Client() : this("localhost", 5555) { diff --git a/Server/Client.cs b/Server/Client.cs index 4a92aac..e153004 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -32,7 +32,6 @@ namespace Server try { int receivedBytes = stream.EndRead(ar); - } catch (IOException) { @@ -46,9 +45,9 @@ namespace Server { byte[] lenghtBytes = new byte[4]; Array.Copy(buffer, counter, lenghtBytes, 0, 4); - int length = Convert.ToInt32(lenghtBytes); + int length = BitConverter.ToInt32(lenghtBytes); byte[] packet = new byte[length]; - Array.Copy(buffer, counter + 4, packet, 0, length-4); + Array.Copy(buffer, counter + 4, packet, 0, length - 4); HandleData(Encoding.Default.GetString(packet)); counter += length; } -- 2.47.2 From fd36e420d164f3acda9dc65e697a3bd5026eb7ac Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 23 Sep 2020 13:22:14 +0200 Subject: [PATCH 16/54] changed IDataConverter to IDataReceiver --- ProftaakRH/BLEHandler.cs | 17 ++++++++-------- ProftaakRH/BikeSimulator.cs | 39 +++++++++++++++++++------------------ ProftaakRH/DataConverter.cs | 28 ++++++++++---------------- ProftaakRH/IDataReceiver.cs | 12 ++++++++++++ 4 files changed, 51 insertions(+), 45 deletions(-) create mode 100644 ProftaakRH/IDataReceiver.cs diff --git a/ProftaakRH/BLEHandler.cs b/ProftaakRH/BLEHandler.cs index 9df61d8..08b57f6 100644 --- a/ProftaakRH/BLEHandler.cs +++ b/ProftaakRH/BLEHandler.cs @@ -4,6 +4,7 @@ using System.Text; using Avans.TI.BLE; using System.Threading; using System.Security.Cryptography; +using ProftaakRH; namespace Hardware { @@ -12,7 +13,7 @@ namespace Hardware /// class BLEHandler { - IDataConverter dataConverter; + IDataReceiver dataReceiver; private BLE bleBike; private BLE bleHeart; public bool Running { get; set; } @@ -20,10 +21,10 @@ namespace Hardware /// /// Makes a new BLEHandler object /// - /// the dataconverter object - public BLEHandler(IDataConverter dataConverter) + /// the dataconverter object + public BLEHandler(IDataReceiver dataReceiver) { - this.dataConverter = dataConverter; + this.dataReceiver = dataReceiver; } /// @@ -119,16 +120,16 @@ namespace Hardware /// the value changed event private void BleBike_SubscriptionValueChanged(object sender, BLESubscriptionValueChangedEventArgs e) { - + if (e.ServiceName == "6e40fec2-b5a3-f393-e0a9-e50e24dcca9e") { byte[] payload = new byte[8]; Array.Copy(e.Data, 4, payload, 0, 8); - this.dataConverter.Bike(payload); + this.dataReceiver.Bike(payload); } else if (e.ServiceName == "00002a37-0000-1000-8000-00805f9b34fb") { - this.dataConverter.BPM(e.Data); + this.dataReceiver.BPM(e.Data); } else { @@ -164,7 +165,7 @@ namespace Hardware antMessage[i] = 0xFF; } antMessage[11] = (byte)Math.Max(Math.Min(Math.Round(percentage / 0.5), 255), 0); - + byte checksum = 0; for (int i = 0; i < 12; i++) diff --git a/ProftaakRH/BikeSimulator.cs b/ProftaakRH/BikeSimulator.cs index 5664f9e..86dddc9 100644 --- a/ProftaakRH/BikeSimulator.cs +++ b/ProftaakRH/BikeSimulator.cs @@ -1,4 +1,5 @@ using LibNoise.Primitive; +using ProftaakRH; using System; using System.Collections.Generic; using System.Linq; @@ -11,7 +12,7 @@ namespace Hardware.Simulators { class BikeSimulator : IHandler { - IDataConverter dataConverter; + IDataReceiver dataReceiver; private int elapsedTime = 0; private int eventCounter = 0; private double distanceTraveled = 0; @@ -29,9 +30,9 @@ namespace Hardware.Simulators - public BikeSimulator(IDataConverter dataConverter) + public BikeSimulator(IDataReceiver dataReceiver) { - this.dataConverter = dataConverter; + this.dataReceiver = dataReceiver; } public void StartSimulation() { @@ -41,16 +42,16 @@ namespace Hardware.Simulators float x = 0.0f; //Perlin for Random values - ImprovedPerlin improvedPerlin = new ImprovedPerlin(0,LibNoise.NoiseQuality.Best); - + ImprovedPerlin improvedPerlin = new ImprovedPerlin(0, LibNoise.NoiseQuality.Best); + while (true) { - CalculateVariables(improvedPerlin.GetValue(x)+1); + CalculateVariables(improvedPerlin.GetValue(x) + 1); //Simulate sending data - dataConverter.Bike(GenerateBike0x19()); - dataConverter.Bike(GenerateBike0x10()); - dataConverter.BPM(GenerateHeart()); + dataReceiver.Bike(GenerateBike0x19()); + dataReceiver.Bike(GenerateBike0x10()); + dataReceiver.BPM(GenerateHeart()); Thread.Sleep(1000); @@ -65,21 +66,21 @@ namespace Hardware.Simulators private byte[] GenerateBike0x19() { byte statByte = (byte)(powerArray[1] >> 4); - byte[] bikeByte = { 0x19, Convert.ToByte(eventCounter%256), Convert.ToByte(cadence%254), accPowerArray[0], accPowerArray[1], powerArray[0], statByte, 0x20 }; + byte[] bikeByte = { 0x19, Convert.ToByte(eventCounter % 256), Convert.ToByte(cadence % 254), accPowerArray[0], accPowerArray[1], powerArray[0], statByte, 0x20 }; return bikeByte; } //Generate an ANT message for page 0x10 private byte[] GenerateBike0x10() { - byte[] bikeByte = { 0x10, Convert.ToByte(equipmentType), Convert.ToByte(elapsedTime*4%64), Convert.ToByte(distanceTraveled), speedArray[0], speedArray[1], Convert.ToByte(BPM), 0xFF }; + byte[] bikeByte = { 0x10, Convert.ToByte(equipmentType), Convert.ToByte(elapsedTime * 4 % 64), Convert.ToByte(distanceTraveled), speedArray[0], speedArray[1], Convert.ToByte(BPM), 0xFF }; return bikeByte; } //Generate an ANT message for BPM private byte[] GenerateHeart() { - byte[] hartByte = { 0x00, Convert.ToByte(BPM)}; + byte[] hartByte = { 0x00, Convert.ToByte(BPM) }; return hartByte; } @@ -114,13 +115,13 @@ namespace Hardware.Simulators //Input perlin value private void CalculateVariables(float perlin) { - this.speed = perlin * 5 / 0.01 ; + this.speed = perlin * 5 / 0.01; short sped = (short)speed; speedArray = BitConverter.GetBytes(sped); - this.distanceTraveled = (distanceTraveled+(speed*0.01)) % 256; - this.BPM = (int) (perlin * 80); - this.cadence = (int)speed/6; - this.power = ((1 + resistance) * speed)/14 % 4094; + this.distanceTraveled = (distanceTraveled + (speed * 0.01)) % 256; + this.BPM = (int)(perlin * 80); + this.cadence = (int)speed / 6; + this.power = ((1 + resistance) * speed) / 14 % 4094; this.accPower = (this.accPower + this.power) % 65536; // TO DO power to power LSB & MSN powerArray = BitConverter.GetBytes((short)this.power); @@ -131,9 +132,9 @@ namespace Hardware.Simulators public void setResistance(byte[] bytes) { //TODO check if message is correct - if(bytes.Length == 13) + if (bytes.Length == 13) { - this.resistance = Convert.ToDouble(bytes[11])/2; + this.resistance = Convert.ToDouble(bytes[11]) / 2; } } } diff --git a/ProftaakRH/DataConverter.cs b/ProftaakRH/DataConverter.cs index 7c93ff2..69eb49e 100644 --- a/ProftaakRH/DataConverter.cs +++ b/ProftaakRH/DataConverter.cs @@ -1,4 +1,5 @@ -using System; +using ProftaakRH; +using System; using System.Collections.Generic; using System.Text; @@ -7,7 +8,7 @@ namespace Hardware /// /// DataConverter class that handles all conversion of received data from the BLE bike. /// - class DataConverter : IDataConverter + class DataConverter : IDataReceiver { /// /// Receives, parses and displays any incoming data from the bike. @@ -22,7 +23,7 @@ namespace Hardware else if (bytes.Length == 8) { - + switch (bytes[0]) { case 0x10: @@ -37,7 +38,7 @@ namespace Hardware Console.WriteLine($"Speed is : {input * 0.01}m/s (Range 65.534m/4)"); if (bytes[6] != 0xFF) { - Console.WriteLine("Heart rate byte: {0}", Convert.ToString(bytes[6],2)); + Console.WriteLine("Heart rate byte: {0}", Convert.ToString(bytes[6], 2)); } break; case 0x19: @@ -45,17 +46,17 @@ namespace Hardware if (bytes[2] != 0xFF) { Console.WriteLine($"Instantaneous cadence: {bytes[2]} RPM (Range 0-254)"); - + } int accumPower = bytes[3] | (bytes[4] << 8); - + Console.WriteLine($"Accumulated power: {accumPower} watt (Rollover 65536)"); - int instantPower = (bytes[5]) | (bytes[6] & 0b00001111)<<8; - + int instantPower = (bytes[5]) | (bytes[6] & 0b00001111) << 8; + if (instantPower != 0xFFF) - Console.WriteLine($"Instant power: {instantPower} watt (Range 0-4094)"); + Console.WriteLine($"Instant power: {instantPower} watt (Range 0-4094)"); int trainerStatus = bytes[6] & 0b11110000; // bit 4-7 int flags = bytes[7] >> 4; @@ -103,13 +104,4 @@ namespace Hardware Console.WriteLine(); } } - - /// - /// Dataconverter interface for handling data received from the bike - /// - interface IDataConverter - { - void BPM(byte[] bytes); - void Bike(byte[] bytes); - } } diff --git a/ProftaakRH/IDataReceiver.cs b/ProftaakRH/IDataReceiver.cs new file mode 100644 index 0000000..ff578dc --- /dev/null +++ b/ProftaakRH/IDataReceiver.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace ProftaakRH +{ + interface IDataReceiver + { + void BPM(byte[] bytes); + void Bike(byte[] bytes); + } +} -- 2.47.2 From 40cbff209bb53ad0d68c0419d3ca67371fbb5571 Mon Sep 17 00:00:00 2001 From: fabjuuuh Date: Wed, 23 Sep 2020 13:25:59 +0200 Subject: [PATCH 17/54] Auto stash before merge of "client" and "origin/client" --- Server/Server.csproj | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Server/Server.csproj b/Server/Server.csproj index 506f4bb..0eaee1b 100644 --- a/Server/Server.csproj +++ b/Server/Server.csproj @@ -9,8 +9,4 @@ - - - - -- 2.47.2 From be9553cc513b1704a854941adcf87688765c0ea4 Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 23 Sep 2020 13:46:38 +0200 Subject: [PATCH 18/54] fix login message, implemented IDatareceiver, added Rawa and jsom messag --- Client/Client.cs | 39 ++++++++++++++++++++++++++++++------- Client/Client.csproj | 1 + Client/DataParser.cs | 29 +++++++++++++++++++++++++++ ProftaakRH/IDataReceiver.cs | 2 +- ProftaakRH/Main.cs | 8 ++++---- 5 files changed, 67 insertions(+), 12 deletions(-) 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; -- 2.47.2 From f899c90ff7438f34d20b62cc4e5eccf6abf1d13b Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 23 Sep 2020 13:46:38 +0200 Subject: [PATCH 19/54] fix login message, implemented IDatareceiver, added Raw and json message --- Client/Client.cs | 39 ++++++++++++++++++++++++++++++------- Client/Client.csproj | 1 + Client/DataParser.cs | 29 +++++++++++++++++++++++++++ ProftaakRH/IDataReceiver.cs | 2 +- ProftaakRH/Main.cs | 8 ++++---- 5 files changed, 67 insertions(+), 12 deletions(-) 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; -- 2.47.2 From 5f7f1c85102e35f72cdb6f4ba96402520955c406 Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 23 Sep 2020 14:12:33 +0200 Subject: [PATCH 20/54] connected to bike? --- Client/Client.cs | 11 +---------- Client/Program.cs | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 10 deletions(-) create mode 100644 Client/Program.cs diff --git a/Client/Client.cs b/Client/Client.cs index bcbdea9..3f6eac0 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -12,18 +12,9 @@ namespace Client private NetworkStream stream; private byte[] buffer = new byte[1024]; private int bytesReceived; - - static void Main(string[] args) - { - Console.WriteLine("Hello World!"); - //connect fiets? + private bool connected = false; - Client client = new Client(); - while (true) - { - } - } public Client() : this("localhost", 5555) { diff --git a/Client/Program.cs b/Client/Program.cs new file mode 100644 index 0000000..3043651 --- /dev/null +++ b/Client/Program.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Client +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello World!"); + //connect fiets? + + + Client client = new Client(); + while (true) + { + } + } + } +} -- 2.47.2 From cd5f23d3e48b64af40b9b27190f4254115b4fdcc Mon Sep 17 00:00:00 2001 From: fabjuuuh Date: Wed, 23 Sep 2020 14:12:35 +0200 Subject: [PATCH 21/54] Works i think --- Server/Client.cs | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/Server/Client.cs b/Server/Client.cs index e153004..0fbf022 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -43,21 +43,39 @@ namespace Server while (buffer.Length > counter) { + //Console.WriteLine(buffer.Length); byte[] lenghtBytes = new byte[4]; Array.Copy(buffer, counter, lenghtBytes, 0, 4); int length = BitConverter.ToInt32(lenghtBytes); - byte[] packet = new byte[length]; - Array.Copy(buffer, counter + 4, packet, 0, length - 4); - HandleData(Encoding.Default.GetString(packet)); + Console.WriteLine(buffer[5]); + if (length == 0) + { + break; + } + else if(buffer[counter+4]==0x02) + { + + } + else if(buffer[counter+4]==0x01) + { + byte[] packet = new byte[length]; + Console.WriteLine(Encoding.ASCII.GetString(buffer)+" "+length); + Array.Copy(buffer, counter+5, packet, 0, length); + Console.WriteLine(Encoding.ASCII.GetString(packet)); + HandleData(Encoding.ASCII.GetString(packet)); + } + counter += length; } + Console.WriteLine("Done"); + stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null); } private void HandleData(string packet) { - Console.WriteLine(packet); + Console.WriteLine("Data "+packet); JsonConvert.DeserializeObject(packet); } } -- 2.47.2 From d67c8448ad29444322d1c16cf98c42e3cf333c74 Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 23 Sep 2020 14:31:37 +0200 Subject: [PATCH 22/54] client and server connect --- Client/Client.cs | 15 +++++- Client/DataParser.cs | 2 +- Client/Program.cs | 11 ++++ ProftaakRH/BLEHandler.cs | 2 +- Server/Client.cs | 114 +++++++++++++++++++++++++++------------ 5 files changed, 107 insertions(+), 37 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index 3f6eac0..26f27ed 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -12,7 +12,7 @@ namespace Client private NetworkStream stream; private byte[] buffer = new byte[1024]; private int bytesReceived; - private bool connected = false; + private bool connected; public Client() : this("localhost", 5555) @@ -24,6 +24,7 @@ namespace Client { this.client = new TcpClient(); this.bytesReceived = 0; + this.connected = false; client.BeginConnect(adress, port, new AsyncCallback(OnConnect), null); } @@ -31,6 +32,8 @@ namespace Client { this.client.EndConnect(ar); Console.WriteLine("Verbonden!"); + + this.stream = this.client.GetStream(); //TODO File in lezen @@ -44,6 +47,9 @@ namespace Client this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); this.stream.BeginRead(this.buffer, 0, this.buffer.Length, new AsyncCallback(OnRead), null); + + //temp moet eigenlijk een ok bericht ontvangen + this.connected = true; } private void OnRead(IAsyncResult ar) @@ -91,7 +97,7 @@ namespace Client private void OnWrite(IAsyncResult ar) { this.stream.EndWrite(ar); - //stuff idk + Console.WriteLine("wrote some stuff"); } #region interface @@ -117,5 +123,10 @@ namespace Client } #endregion + + public bool IsConnected() + { + return this.connected; + } } } diff --git a/Client/DataParser.cs b/Client/DataParser.cs index cf1f700..4215cf6 100644 --- a/Client/DataParser.cs +++ b/Client/DataParser.cs @@ -60,7 +60,7 @@ namespace Client { byte[] res = new byte[payload.Length + 5]; - Array.Copy(BitConverter.GetBytes(payload.Length), 0, res, 0, 4); + Array.Copy(BitConverter.GetBytes(payload.Length + 5), 0, res, 0, 4); res[4] = messageId; Array.Copy(payload, 0, res, 5, payload.Length); diff --git a/Client/Program.cs b/Client/Program.cs index 3043651..9598930 100644 --- a/Client/Program.cs +++ b/Client/Program.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; using System.Text; +using Hardware; +using ProftaakRH; namespace Client { @@ -13,6 +15,15 @@ namespace Client Client client = new Client(); + + + while (!client.IsConnected()) + { + + } + BLEHandler bLEHandler = new BLEHandler(client); + + bLEHandler.Connect(); while (true) { } diff --git a/ProftaakRH/BLEHandler.cs b/ProftaakRH/BLEHandler.cs index 08b57f6..238bb4c 100644 --- a/ProftaakRH/BLEHandler.cs +++ b/ProftaakRH/BLEHandler.cs @@ -11,7 +11,7 @@ namespace Hardware /// /// BLEHandler class that handles connection and traffic to and from the bike /// - class BLEHandler + public class BLEHandler { IDataReceiver dataReceiver; private BLE bleBike; diff --git a/Server/Client.cs b/Server/Client.cs index 0fbf022..56e1685 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; @@ -16,6 +17,8 @@ namespace Server private NetworkStream stream; private byte[] buffer = new byte[1024]; private byte[] totalBuffer = new byte[1024]; + private int bytesReceived; + public string Username { get; set; } @@ -27,55 +30,100 @@ namespace Server stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null); } + //private void OnRead(IAsyncResult ar) + //{ + // try + // { + // int receivedBytes = stream.EndRead(ar); + // } + // catch (IOException) + // { + // communication.Disconnect(this); + // return; + // } + + // int counter = 0; + + // while (buffer.Length > counter) + // { + // //Console.WriteLine(buffer.Length); + // byte[] lenghtBytes = new byte[4]; + // Array.Copy(buffer, counter, lenghtBytes, 0, 4); + // int length = BitConverter.ToInt32(lenghtBytes); + // Console.WriteLine(buffer[5]); + // if (length == 0) + // { + // break; + // } + // else if(buffer[counter+4]==0x02) + // { + + // } + // else if(buffer[counter+4]==0x01) + // { + // byte[] packet = new byte[length]; + // Console.WriteLine(Encoding.ASCII.GetString(buffer)+" "+length); + // Array.Copy(buffer, counter+5, packet, 0, length); + // Console.WriteLine(Encoding.ASCII.GetString(packet)); + // HandleData(Encoding.ASCII.GetString(packet)); + // } + + // counter += length; + // } + + // Console.WriteLine("Done"); + + // stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null); + //} + private void OnRead(IAsyncResult ar) { - try + int receivedBytes = this.stream.EndRead(ar); + byte[] lengthBytes = new byte[4]; + + Array.Copy(this.buffer, 0, lengthBytes, 0, 4); + + int expectedMessageLength = BitConverter.ToInt32(lengthBytes); + + if (expectedMessageLength > this.buffer.Length) { - int receivedBytes = stream.EndRead(ar); - } - catch (IOException) - { - communication.Disconnect(this); - return; + throw new OutOfMemoryException("buffer to small"); } - int counter = 0; - - while (buffer.Length > counter) + if (expectedMessageLength > this.bytesReceived + receivedBytes) { - //Console.WriteLine(buffer.Length); - byte[] lenghtBytes = new byte[4]; - Array.Copy(buffer, counter, lenghtBytes, 0, 4); - int length = BitConverter.ToInt32(lenghtBytes); - Console.WriteLine(buffer[5]); - if (length == 0) - { - break; - } - else if(buffer[counter+4]==0x02) - { + //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); - } - else if(buffer[counter+4]==0x01) + } + else + { + //message completely arrived + if (expectedMessageLength != this.bytesReceived + receivedBytes) { - byte[] packet = new byte[length]; - Console.WriteLine(Encoding.ASCII.GetString(buffer)+" "+length); - Array.Copy(buffer, counter+5, packet, 0, length); + Console.WriteLine("something has gone completely wrong"); + Console.WriteLine($"expected: {expectedMessageLength} bytesReceive: {bytesReceived} receivedBytes: {receivedBytes}"); + } + else if (buffer[4] == 0x02) + { + Console.WriteLine($"received raw data {BitConverter.ToString(buffer.Skip(5).ToArray(), 16)}"); + } + else if (buffer[4] == 0x01) + { + byte[] packet = new byte[expectedMessageLength]; + Console.WriteLine(Encoding.ASCII.GetString(buffer) + " " + expectedMessageLength); + Array.Copy(buffer, 5, packet, 0, expectedMessageLength - 5); Console.WriteLine(Encoding.ASCII.GetString(packet)); HandleData(Encoding.ASCII.GetString(packet)); } - - counter += length; + } - - Console.WriteLine("Done"); - - stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null); } private void HandleData(string packet) { - Console.WriteLine("Data "+packet); + Console.WriteLine("Data " + packet); JsonConvert.DeserializeObject(packet); } } -- 2.47.2 From d5eadbe52936c7b1229584b9f3a9d028281c4ab6 Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 23 Sep 2020 14:32:07 +0200 Subject: [PATCH 23/54] forgot to stage --- Server/Client.cs | 46 ---------------------------------------------- 1 file changed, 46 deletions(-) diff --git a/Server/Client.cs b/Server/Client.cs index 56e1685..fc29226 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -30,52 +30,6 @@ namespace Server stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null); } - //private void OnRead(IAsyncResult ar) - //{ - // try - // { - // int receivedBytes = stream.EndRead(ar); - // } - // catch (IOException) - // { - // communication.Disconnect(this); - // return; - // } - - // int counter = 0; - - // while (buffer.Length > counter) - // { - // //Console.WriteLine(buffer.Length); - // byte[] lenghtBytes = new byte[4]; - // Array.Copy(buffer, counter, lenghtBytes, 0, 4); - // int length = BitConverter.ToInt32(lenghtBytes); - // Console.WriteLine(buffer[5]); - // if (length == 0) - // { - // break; - // } - // else if(buffer[counter+4]==0x02) - // { - - // } - // else if(buffer[counter+4]==0x01) - // { - // byte[] packet = new byte[length]; - // Console.WriteLine(Encoding.ASCII.GetString(buffer)+" "+length); - // Array.Copy(buffer, counter+5, packet, 0, length); - // Console.WriteLine(Encoding.ASCII.GetString(packet)); - // HandleData(Encoding.ASCII.GetString(packet)); - // } - - // counter += length; - // } - - // Console.WriteLine("Done"); - - // stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null); - //} - private void OnRead(IAsyncResult ar) { int receivedBytes = this.stream.EndRead(ar); -- 2.47.2 From c782301cf270d306ee49c8f95a710c11d68ce69f Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 23 Sep 2020 15:05:09 +0200 Subject: [PATCH 24/54] connection from bike/simulator to client to server --- Client/Client.cs | 3 ++- Client/Program.cs | 6 ++++++ ProftaakRH/BikeSimulator.cs | 3 ++- Server/Client.cs | 9 ++++++++- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index 26f27ed..86fba5e 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -92,12 +92,13 @@ namespace Client throw new NotImplementedException(); } } + this.stream.BeginRead(this.buffer, 0, this.buffer.Length, new AsyncCallback(OnRead), null); + } private void OnWrite(IAsyncResult ar) { this.stream.EndWrite(ar); - Console.WriteLine("wrote some stuff"); } #region interface diff --git a/Client/Program.cs b/Client/Program.cs index 9598930..60cf68e 100644 --- a/Client/Program.cs +++ b/Client/Program.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Text; using Hardware; +using Hardware.Simulators; using ProftaakRH; namespace Client @@ -24,6 +25,11 @@ namespace Client BLEHandler bLEHandler = new BLEHandler(client); bLEHandler.Connect(); + + //BikeSimulator bikeSimulator = new BikeSimulator(client); + + //bikeSimulator.StartSimulation(); + while (true) { } diff --git a/ProftaakRH/BikeSimulator.cs b/ProftaakRH/BikeSimulator.cs index 86dddc9..304a00d 100644 --- a/ProftaakRH/BikeSimulator.cs +++ b/ProftaakRH/BikeSimulator.cs @@ -10,7 +10,7 @@ using System.Threading; namespace Hardware.Simulators { - class BikeSimulator : IHandler + public class BikeSimulator : IHandler { IDataReceiver dataReceiver; private int elapsedTime = 0; @@ -34,6 +34,7 @@ namespace Hardware.Simulators { this.dataReceiver = dataReceiver; } + public void StartSimulation() { //Example BLE Message diff --git a/Server/Client.cs b/Server/Client.cs index fc29226..d1d5599 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -48,6 +48,7 @@ namespace Server { //message hasn't completely arrived yet this.bytesReceived += receivedBytes; + Console.WriteLine("segmented message, {0} arrived", receivedBytes); this.stream.BeginRead(this.buffer, this.bytesReceived, this.buffer.Length - this.bytesReceived, new AsyncCallback(OnRead), null); } @@ -58,10 +59,12 @@ namespace Server { Console.WriteLine("something has gone completely wrong"); Console.WriteLine($"expected: {expectedMessageLength} bytesReceive: {bytesReceived} receivedBytes: {receivedBytes}"); + Console.WriteLine($"received WEIRD data {BitConverter.ToString(buffer.Take(receivedBytes).ToArray())} string {Encoding.ASCII.GetString(buffer.Take(receivedBytes).ToArray())}"); + } else if (buffer[4] == 0x02) { - Console.WriteLine($"received raw data {BitConverter.ToString(buffer.Skip(5).ToArray(), 16)}"); + Console.WriteLine($"received raw data {BitConverter.ToString(buffer.Skip(5).Take(expectedMessageLength).ToArray())}"); } else if (buffer[4] == 0x01) { @@ -71,8 +74,12 @@ namespace Server Console.WriteLine(Encoding.ASCII.GetString(packet)); HandleData(Encoding.ASCII.GetString(packet)); } + this.bytesReceived = 0; } + + this.stream.BeginRead(this.buffer, 0, this.buffer.Length, new AsyncCallback(OnRead), null); + } private void HandleData(string packet) -- 2.47.2 From 81dcebb5e88eb43be2a953ad1b185f41d2a6442e Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 23 Sep 2020 15:11:13 +0200 Subject: [PATCH 25/54] added client id to protocol --- Client/Client.cs | 8 +++++--- Client/DataParser.cs | 26 +++++++++++++------------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index 86fba5e..9eb787d 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -13,6 +13,7 @@ namespace Client private byte[] buffer = new byte[1024]; private int bytesReceived; private bool connected; + private byte clientId = 0; public Client() : this("localhost", 5555) @@ -42,12 +43,13 @@ namespace Client Console.WriteLine("enter password"); string password = Console.ReadLine(); - byte[] message = DataParser.getJsonMessage(DataParser.GetLoginJson(username, password)); + byte[] message = DataParser.getJsonMessage(DataParser.GetLoginJson(username, password), this.clientId); this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); this.stream.BeginRead(this.buffer, 0, this.buffer.Length, new AsyncCallback(OnRead), null); + //TODO lees OK message //temp moet eigenlijk een ok bericht ontvangen this.connected = true; } @@ -109,7 +111,7 @@ namespace Client { throw new ArgumentNullException("no bytes"); } - byte[] message = DataParser.GetRawDataMessage(bytes); + byte[] message = DataParser.GetRawDataMessage(bytes, clientId); this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); } @@ -119,7 +121,7 @@ namespace Client { throw new ArgumentNullException("no bytes"); } - byte[] message = DataParser.GetRawDataMessage(bytes); + byte[] message = DataParser.GetRawDataMessage(bytes, clientId); this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); } diff --git a/Client/DataParser.cs b/Client/DataParser.cs index 4215cf6..137242e 100644 --- a/Client/DataParser.cs +++ b/Client/DataParser.cs @@ -28,7 +28,7 @@ namespace Client public static bool getJsonIdentifier(byte[] bytes, out string identifier) { - if (bytes.Length <= 5) + if (bytes.Length <= 6) { throw new ArgumentException("bytes to short"); } @@ -36,7 +36,7 @@ namespace Client if (messageId == 1) { - dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(bytes.Skip(5).ToArray())); + dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(bytes.Skip(6).ToArray())); identifier = json.identifier; return true; } @@ -49,37 +49,37 @@ namespace Client public static bool isRawData(byte[] bytes) { - if (bytes.Length <= 5) + if (bytes.Length <= 6) { throw new ArgumentException("bytes to short"); } return bytes[5] == 0x02; } - private static byte[] getMessage(byte[] payload, byte messageId) + private static byte[] getMessage(byte[] payload, byte messageId, byte clientId) { - byte[] res = new byte[payload.Length + 5]; + byte[] res = new byte[payload.Length + 6]; - Array.Copy(BitConverter.GetBytes(payload.Length + 5), 0, res, 0, 4); + Array.Copy(BitConverter.GetBytes(payload.Length + 6), 0, res, 0, 4); res[4] = messageId; - Array.Copy(payload, 0, res, 5, payload.Length); + Array.Copy(payload, 0, res, 6, payload.Length); return res; } - public static byte[] GetRawDataMessage(byte[] payload) + public static byte[] GetRawDataMessage(byte[] payload, byte clientId) { - return getMessage(payload, 0x02); + return getMessage(payload, 0x02, clientId); } - public static byte[] getJsonMessage(byte[] payload) + public static byte[] getJsonMessage(byte[] payload, byte clientId) { - return getMessage(payload, 0x01); + return getMessage(payload, 0x01, clientId); } - public static byte[] getJsonMessage(string message) + public static byte[] getJsonMessage(string message, byte clientId) { - return getJsonMessage(Encoding.ASCII.GetBytes(message)); + return getJsonMessage(Encoding.ASCII.GetBytes(message), clientId); } -- 2.47.2 From 8c6cb443e3effd33b086843c835523268ed560de Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 23 Sep 2020 15:22:45 +0200 Subject: [PATCH 26/54] deleted stuff --- Client/Client.cs | 2 -- Client/DataParser.cs | 4 ---- Client/Program.cs | 4 ---- Server/Client.cs | 4 ---- Server/Program.cs | 3 +-- 5 files changed, 1 insertion(+), 16 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index 9eb787d..e6727d3 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -1,6 +1,4 @@ using System; -using System.Globalization; -using System.Linq; using System.Net.Sockets; using ProftaakRH; diff --git a/Client/DataParser.cs b/Client/DataParser.cs index 137242e..14014d6 100644 --- a/Client/DataParser.cs +++ b/Client/DataParser.cs @@ -1,10 +1,6 @@ using Newtonsoft.Json; -using Newtonsoft.Json.Linq; using System; -using System.Collections.Generic; -using System.Globalization; using System.Linq; -using System.Security.Cryptography; using System.Text; namespace Client diff --git a/Client/Program.cs b/Client/Program.cs index 60cf68e..e98dfc7 100644 --- a/Client/Program.cs +++ b/Client/Program.cs @@ -1,9 +1,5 @@ using System; -using System.Collections.Generic; -using System.Text; using Hardware; -using Hardware.Simulators; -using ProftaakRH; namespace Client { diff --git a/Server/Client.cs b/Server/Client.cs index d1d5599..9dfc946 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -1,11 +1,7 @@ using System; -using System.Collections.Generic; -using System.IO; using System.Linq; -using System.Net; using System.Net.Sockets; using System.Text; -using Newtonsoft; using Newtonsoft.Json; namespace Server diff --git a/Server/Program.cs b/Server/Program.cs index bf66448..cd980a4 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -1,5 +1,4 @@ -using System; -using System.Net; +using System.Net; using System.Net.Sockets; namespace Server -- 2.47.2 From 64773ffe1c1bd07c850c7ccb3c91cff85a3999f0 Mon Sep 17 00:00:00 2001 From: Logophilist Date: Wed, 23 Sep 2020 15:33:23 +0200 Subject: [PATCH 27/54] Attempt 1 dashboard VR --- RH-Engine/Command.cs | 58 +++++++++++++++++++++++++++++++++++++++++ RH-Engine/JSONParser.cs | 10 +++++++ RH-Engine/Program.cs | 32 ++++++++++++++--------- 3 files changed, 88 insertions(+), 12 deletions(-) diff --git a/RH-Engine/Command.cs b/RH-Engine/Command.cs index 6491de4..d7de332 100644 --- a/RH-Engine/Command.cs +++ b/RH-Engine/Command.cs @@ -106,6 +106,64 @@ namespace RH_Engine } + public string addPanel() + { + dynamic payload = new + { + id = "scene/node/add", + data = new + { + name = "dashboard", + components = new + { + panel = new + { + size = new int[] { 1, 1 }, + resolution = new int[] { 256, 256 }, + background = new int[] { 1, 1, 1, 1 }, + castShadow = true + + } + } + } + }; + + return JsonConvert.SerializeObject(Payload(payload)); + } + + public string bikeSpeed(string uuidPanel) + { + dynamic payload = new + { + id = "scene/panel/drawtext", + data = new + { + id = uuidPanel, + text = "Bike speed placeholder", + position = new int[] { 0, 0 }, + size = 32.0, + color = new int[] { 0, 0, 0, 1 }, + font = "segoeui" + } + }; + + return JsonConvert.SerializeObject(Payload(payload)); + } + + public string ClearPanel(string uuid) + { + dynamic payload = new + { + id = "scene/panel/clear", + data = new + { + id = uuid + } + }; + + return JsonConvert.SerializeObject(Payload(payload)); + } + public string AddBikeModel() { return AddModel("bike", "data\\NetworkEngine\\models\\bike\\bike.fbx"); diff --git a/RH-Engine/JSONParser.cs b/RH-Engine/JSONParser.cs index 2f1f903..2309d7f 100644 --- a/RH-Engine/JSONParser.cs +++ b/RH-Engine/JSONParser.cs @@ -70,5 +70,15 @@ namespace RH_Engine return null; } + public static string getPanelID(string json) + { + dynamic jsonData = JsonConvert.DeserializeObject(json); + if (jsonData.data.data.data.name == "dashboard") + { + Console.WriteLine(jsonData.data.data.data.uuid); + return jsonData.data.data.data.uuid; + } + return null; + } } } diff --git a/RH-Engine/Program.cs b/RH-Engine/Program.cs index 5955866..8a710c3 100644 --- a/RH-Engine/Program.cs +++ b/RH-Engine/Program.cs @@ -17,10 +17,10 @@ namespace RH_Engine { private static PC[] PCs = { //new PC("DESKTOP-M2CIH87", "Fabian"), - new PC("T470S", "Shinichi"), + //new PC("T470S", "Shinichi"), //new PC("DESKTOP-DHS478C", "semme"), //new PC("DESKTOP-TV73FKO", "Wouter"), - //new PC("DESKTOP-SINMKT1", "Ralf"), + new PC("DESKTOP-SINMKT1", "Ralf van Aert"), //new PC("NA", "Bart") }; private static void Main(string[] args) @@ -104,8 +104,6 @@ namespace RH_Engine } sendCommands(stream, tunnelID); - - } /// @@ -126,18 +124,28 @@ namespace RH_Engine Console.WriteLine(ReadPrefMessage(stream)); string command; - command = mainCommand.AddBikeModel(); + //command = mainCommand.AddBikeModel(); + //WriteTextMessage(stream, command); + + //Console.WriteLine(ReadPrefMessage(stream)); + + //command = mainCommand.AddModel("car", "data\\customModels\\TeslaRoadster.fbx"); + + //WriteTextMessage(stream, command); + + //Console.WriteLine(ReadPrefMessage(stream)); + + + command = mainCommand.addPanel(); WriteTextMessage(stream, command); - + string response = ReadPrefMessage(stream); + Console.WriteLine("add Panel response: \n\r" + response); + string uuidPanel = JSONParser.getPanelID(response); + WriteTextMessage(stream, mainCommand.ClearPanel(uuidPanel)); Console.WriteLine(ReadPrefMessage(stream)); - - command = mainCommand.AddModel("car", "data\\customModels\\TeslaRoadster.fbx"); - - WriteTextMessage(stream, command); - + WriteTextMessage(stream, mainCommand.bikeSpeed(uuidPanel)); Console.WriteLine(ReadPrefMessage(stream)); - } /// -- 2.47.2 From fb57a1779c1d1fbf661f317885b1b704e41881b2 Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 23 Sep 2020 15:37:37 +0200 Subject: [PATCH 28/54] comments and some small fixes --- Client/DataParser.cs | 45 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/Client/DataParser.cs b/Client/DataParser.cs index 14014d6..e9051f3 100644 --- a/Client/DataParser.cs +++ b/Client/DataParser.cs @@ -7,6 +7,12 @@ namespace Client { class DataParser { + /// + /// makes the json object with LOGIN identifier and username and password + /// + /// username + /// password + /// json object to ASCII to bytes public static byte[] GetLoginJson(string mUsername, string mPassword) { dynamic json = new @@ -22,6 +28,12 @@ namespace Client return Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(json)); } + /// + /// get the identifier from json + /// + /// json in ASCII + /// gets the identifier + /// if it sucseeded public static bool getJsonIdentifier(byte[] bytes, out string identifier) { if (bytes.Length <= 6) @@ -43,36 +55,67 @@ namespace Client } } + /// + /// checks if the de message is raw data according to the protocol + /// + /// message + /// if message contains raw data public static bool isRawData(byte[] bytes) { if (bytes.Length <= 6) { throw new ArgumentException("bytes to short"); } - return bytes[5] == 0x02; + return bytes[4] == 0x02; } + /// + /// constructs a message with the payload, messageId and clientId + /// + /// + /// + /// + /// the message ready for sending private static byte[] getMessage(byte[] payload, byte messageId, byte clientId) { byte[] res = new byte[payload.Length + 6]; Array.Copy(BitConverter.GetBytes(payload.Length + 6), 0, res, 0, 4); res[4] = messageId; + res[5] = clientId; Array.Copy(payload, 0, res, 6, payload.Length); return res; } + /// + /// constructs a message with the payload and clientId and assumes the payload is raw data + /// + /// + /// + /// the message ready for sending public static byte[] GetRawDataMessage(byte[] payload, byte clientId) { return getMessage(payload, 0x02, clientId); } + /// + /// constructs a message with the payload and clientId and assumes the payload is json + /// + /// + /// + /// the message ready for sending public static byte[] getJsonMessage(byte[] payload, byte clientId) { return getMessage(payload, 0x01, clientId); } + /// + /// constructs a message with the message and clientId + /// + /// + /// + /// the message ready for sending public static byte[] getJsonMessage(string message, byte clientId) { return getJsonMessage(Encoding.ASCII.GetBytes(message), clientId); -- 2.47.2 From dded1a5b24b112094a69fdc2e2a1d26fa888a978 Mon Sep 17 00:00:00 2001 From: shinichi Date: Fri, 25 Sep 2020 12:43:01 +0200 Subject: [PATCH 29/54] johan's code senior meeting --- Server/Client.cs | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/Server/Client.cs b/Server/Client.cs index 9dfc946..37a6800 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -14,6 +14,7 @@ namespace Server private byte[] buffer = new byte[1024]; private byte[] totalBuffer = new byte[1024]; private int bytesReceived; + private int totalBufferReceived = 0; public string Username { get; set; } @@ -31,6 +32,33 @@ namespace Server int receivedBytes = this.stream.EndRead(ar); byte[] lengthBytes = new byte[4]; + if (totalBufferReceived + receivedBytes > 1024) + { + throw new OutOfMemoryException("buffer too small"); + } + Array.Copy(buffer, 0, totalBuffer, totalBufferReceived, receivedBytes); + totalBufferReceived += receivedBytes; + + + int expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0); + while (totalBufferReceived >= expectedMessageLength) + { + //volledig packet binnen + byte[] packetBytes = new byte[expectedMessageLength]; + Array.Copy(totalBuffer, 6, packetBytes, 0, expectedMessageLength - 6); + + + Console.WriteLine(Encoding.ASCII.GetString(packetBytes) + " " + expectedMessageLength); + + + + Array.Copy(totalBuffer, expectedMessageLength, totalBuffer, 0, (totalBufferReceived - expectedMessageLength)); + totalBufferReceived -= expectedMessageLength; + expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0); + } + + /* + Array.Copy(this.buffer, 0, lengthBytes, 0, 4); int expectedMessageLength = BitConverter.ToInt32(lengthBytes); @@ -73,7 +101,7 @@ namespace Server this.bytesReceived = 0; } - + */ this.stream.BeginRead(this.buffer, 0, this.buffer.Length, new AsyncCallback(OnRead), null); } -- 2.47.2 From 2139fcf2b2d29590aafbc39cd979fdeb12d87d01 Mon Sep 17 00:00:00 2001 From: shinichi Date: Fri, 25 Sep 2020 12:51:54 +0200 Subject: [PATCH 30/54] removed clientId --- Client/Client.cs | 7 +++--- Client/DataParser.cs | 27 +++++++++++----------- Server/Client.cs | 53 ++++---------------------------------------- 3 files changed, 20 insertions(+), 67 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index e6727d3..64b66cb 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -11,7 +11,6 @@ namespace Client private byte[] buffer = new byte[1024]; private int bytesReceived; private bool connected; - private byte clientId = 0; public Client() : this("localhost", 5555) @@ -41,7 +40,7 @@ namespace Client Console.WriteLine("enter password"); string password = Console.ReadLine(); - byte[] message = DataParser.getJsonMessage(DataParser.GetLoginJson(username, password), this.clientId); + byte[] message = DataParser.getJsonMessage(DataParser.GetLoginJson(username, password)); this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); @@ -109,7 +108,7 @@ namespace Client { throw new ArgumentNullException("no bytes"); } - byte[] message = DataParser.GetRawDataMessage(bytes, clientId); + byte[] message = DataParser.GetRawDataMessage(bytes); this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); } @@ -119,7 +118,7 @@ namespace Client { throw new ArgumentNullException("no bytes"); } - byte[] message = DataParser.GetRawDataMessage(bytes, clientId); + byte[] message = DataParser.GetRawDataMessage(bytes); this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); } diff --git a/Client/DataParser.cs b/Client/DataParser.cs index e9051f3..5300b79 100644 --- a/Client/DataParser.cs +++ b/Client/DataParser.cs @@ -36,7 +36,7 @@ namespace Client /// if it sucseeded public static bool getJsonIdentifier(byte[] bytes, out string identifier) { - if (bytes.Length <= 6) + if (bytes.Length <= 5) { throw new ArgumentException("bytes to short"); } @@ -44,7 +44,7 @@ namespace Client if (messageId == 1) { - dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(bytes.Skip(6).ToArray())); + dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(bytes.Skip(5).ToArray())); identifier = json.identifier; return true; } @@ -62,7 +62,7 @@ namespace Client /// if message contains raw data public static bool isRawData(byte[] bytes) { - if (bytes.Length <= 6) + if (bytes.Length <= 5) { throw new ArgumentException("bytes to short"); } @@ -76,14 +76,13 @@ namespace Client /// /// /// the message ready for sending - private static byte[] getMessage(byte[] payload, byte messageId, byte clientId) + private static byte[] getMessage(byte[] payload, byte messageId) { - byte[] res = new byte[payload.Length + 6]; + byte[] res = new byte[payload.Length + 5]; - Array.Copy(BitConverter.GetBytes(payload.Length + 6), 0, res, 0, 4); + Array.Copy(BitConverter.GetBytes(payload.Length + 5), 0, res, 0, 4); res[4] = messageId; - res[5] = clientId; - Array.Copy(payload, 0, res, 6, payload.Length); + Array.Copy(payload, 0, res, 5, payload.Length); return res; } @@ -94,9 +93,9 @@ namespace Client /// /// /// the message ready for sending - public static byte[] GetRawDataMessage(byte[] payload, byte clientId) + public static byte[] GetRawDataMessage(byte[] payload) { - return getMessage(payload, 0x02, clientId); + return getMessage(payload, 0x02); } /// @@ -105,9 +104,9 @@ namespace Client /// /// /// the message ready for sending - public static byte[] getJsonMessage(byte[] payload, byte clientId) + public static byte[] getJsonMessage(byte[] payload) { - return getMessage(payload, 0x01, clientId); + return getMessage(payload, 0x01); } /// @@ -116,9 +115,9 @@ namespace Client /// /// /// the message ready for sending - public static byte[] getJsonMessage(string message, byte clientId) + public static byte[] getJsonMessage(string message) { - return getJsonMessage(Encoding.ASCII.GetBytes(message), clientId); + return getJsonMessage(Encoding.ASCII.GetBytes(message)); } diff --git a/Server/Client.cs b/Server/Client.cs index 37a6800..c836f72 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -13,7 +13,6 @@ namespace Server private NetworkStream stream; private byte[] buffer = new byte[1024]; private byte[] totalBuffer = new byte[1024]; - private int bytesReceived; private int totalBufferReceived = 0; @@ -30,7 +29,6 @@ namespace Server private void OnRead(IAsyncResult ar) { int receivedBytes = this.stream.EndRead(ar); - byte[] lengthBytes = new byte[4]; if (totalBufferReceived + receivedBytes > 1024) { @@ -45,7 +43,7 @@ namespace Server { //volledig packet binnen byte[] packetBytes = new byte[expectedMessageLength]; - Array.Copy(totalBuffer, 6, packetBytes, 0, expectedMessageLength - 6); + Array.Copy(totalBuffer, 5, packetBytes, 0, expectedMessageLength - 5); Console.WriteLine(Encoding.ASCII.GetString(packetBytes) + " " + expectedMessageLength); @@ -57,59 +55,16 @@ namespace Server expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0); } - /* - Array.Copy(this.buffer, 0, lengthBytes, 0, 4); - int expectedMessageLength = BitConverter.ToInt32(lengthBytes); - - if (expectedMessageLength > this.buffer.Length) - { - throw new OutOfMemoryException("buffer to small"); - } - - if (expectedMessageLength > this.bytesReceived + receivedBytes) - { - //message hasn't completely arrived yet - this.bytesReceived += receivedBytes; - Console.WriteLine("segmented message, {0} arrived", receivedBytes); - this.stream.BeginRead(this.buffer, this.bytesReceived, this.buffer.Length - this.bytesReceived, new AsyncCallback(OnRead), null); - - } - else - { - //message completely arrived - if (expectedMessageLength != this.bytesReceived + receivedBytes) - { - Console.WriteLine("something has gone completely wrong"); - Console.WriteLine($"expected: {expectedMessageLength} bytesReceive: {bytesReceived} receivedBytes: {receivedBytes}"); - Console.WriteLine($"received WEIRD data {BitConverter.ToString(buffer.Take(receivedBytes).ToArray())} string {Encoding.ASCII.GetString(buffer.Take(receivedBytes).ToArray())}"); - - } - else if (buffer[4] == 0x02) - { - Console.WriteLine($"received raw data {BitConverter.ToString(buffer.Skip(5).Take(expectedMessageLength).ToArray())}"); - } - else if (buffer[4] == 0x01) - { - byte[] packet = new byte[expectedMessageLength]; - Console.WriteLine(Encoding.ASCII.GetString(buffer) + " " + expectedMessageLength); - Array.Copy(buffer, 5, packet, 0, expectedMessageLength - 5); - Console.WriteLine(Encoding.ASCII.GetString(packet)); - HandleData(Encoding.ASCII.GetString(packet)); - } - this.bytesReceived = 0; - - } - */ this.stream.BeginRead(this.buffer, 0, this.buffer.Length, new AsyncCallback(OnRead), null); } - private void HandleData(string packet) + private void HandleData(byte[] message) { - Console.WriteLine("Data " + packet); - JsonConvert.DeserializeObject(packet); + //Console.WriteLine("Data " + packet); + //JsonConvert.DeserializeObject(packet); } } } -- 2.47.2 From 96f2e6e9734a713185d43790efeff7306db50980 Mon Sep 17 00:00:00 2001 From: shinichi Date: Fri, 25 Sep 2020 12:58:12 +0200 Subject: [PATCH 31/54] rewrote OnRead --- Server/Client.cs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/Server/Client.cs b/Server/Client.cs index c836f72..82e2551 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -37,30 +37,28 @@ namespace Server Array.Copy(buffer, 0, totalBuffer, totalBufferReceived, receivedBytes); totalBufferReceived += receivedBytes; - int expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0); while (totalBufferReceived >= expectedMessageLength) { //volledig packet binnen - byte[] packetBytes = new byte[expectedMessageLength]; - Array.Copy(totalBuffer, 5, packetBytes, 0, expectedMessageLength - 5); + byte[] messageBytes = new byte[expectedMessageLength]; + Array.Copy(totalBuffer, 0, messageBytes, 0, expectedMessageLength); + HandleData(messageBytes); + //Array.Copy(totalBuffer, expectedMessageLength, totalBuffer, 0, (totalBufferReceived - expectedMessageLength)); //maybe unsafe idk + totalBuffer = totalBuffer.Skip(expectedMessageLength).ToArray(); - Console.WriteLine(Encoding.ASCII.GetString(packetBytes) + " " + expectedMessageLength); - - - - Array.Copy(totalBuffer, expectedMessageLength, totalBuffer, 0, (totalBufferReceived - expectedMessageLength)); totalBufferReceived -= expectedMessageLength; expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0); } - - this.stream.BeginRead(this.buffer, 0, this.buffer.Length, new AsyncCallback(OnRead), null); } - + /// + /// TODO + /// + /// including message length and messageId (can be changed) private void HandleData(byte[] message) { //Console.WriteLine("Data " + packet); -- 2.47.2 From 360ec4175f404e82b09e59f28e0dc04169eccf50 Mon Sep 17 00:00:00 2001 From: shinichi Date: Fri, 25 Sep 2020 13:04:39 +0200 Subject: [PATCH 32/54] OnRead for client updated --- Client/Client.cs | 37 +++++++++++++++---------------------- Client/DataParser.cs | 2 +- Server/Client.cs | 8 ++++++++ 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index 64b66cb..d6dc26c 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -9,8 +9,9 @@ namespace Client private TcpClient client; private NetworkStream stream; private byte[] buffer = new byte[1024]; - private int bytesReceived; private bool connected; + private byte[] totalBuffer = new byte[1024]; + private int totalBufferReceived = 0; public Client() : this("localhost", 5555) @@ -21,7 +22,6 @@ namespace Client public Client(string adress, int port) { this.client = new TcpClient(); - this.bytesReceived = 0; this.connected = false; client.BeginConnect(adress, port, new AsyncCallback(OnConnect), null); } @@ -54,31 +54,20 @@ namespace Client private void OnRead(IAsyncResult ar) { int receivedBytes = this.stream.EndRead(ar); - byte[] lengthBytes = new byte[4]; - Array.Copy(this.buffer, 0, lengthBytes, 0, 4); - - int expectedMessageLength = BitConverter.ToInt32(lengthBytes); - - if (expectedMessageLength > this.buffer.Length) + if (totalBufferReceived + receivedBytes > 1024) { - throw new OutOfMemoryException("buffer to small"); + throw new OutOfMemoryException("buffer too small"); } + Array.Copy(buffer, 0, totalBuffer, totalBufferReceived, receivedBytes); + totalBufferReceived += receivedBytes; - if (expectedMessageLength > this.bytesReceived + receivedBytes) + int expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0); + while (totalBufferReceived >= expectedMessageLength) { - //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); - - } - else - { - //message completely arrived - if (expectedMessageLength != this.bytesReceived + receivedBytes) - { - Console.WriteLine("something has gone completely wrong"); - } + //volledig packet binnen + byte[] messageBytes = new byte[expectedMessageLength]; + Array.Copy(totalBuffer, 0, messageBytes, 0, expectedMessageLength); string identifier; bool isJson = DataParser.getJsonIdentifier(this.buffer, out identifier); @@ -90,7 +79,11 @@ namespace Client { throw new NotImplementedException(); } + + totalBufferReceived -= expectedMessageLength; + expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0); } + this.stream.BeginRead(this.buffer, 0, this.buffer.Length, new AsyncCallback(OnRead), null); } diff --git a/Client/DataParser.cs b/Client/DataParser.cs index 5300b79..57a9b1c 100644 --- a/Client/DataParser.cs +++ b/Client/DataParser.cs @@ -42,7 +42,7 @@ namespace Client } byte messageId = bytes[4]; - if (messageId == 1) + if (messageId == 0x01) { dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(bytes.Skip(5).ToArray())); identifier = json.identifier; diff --git a/Server/Client.cs b/Server/Client.cs index 82e2551..27a4f49 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -63,6 +63,14 @@ namespace Server { //Console.WriteLine("Data " + packet); //JsonConvert.DeserializeObject(packet); + if (message[4] == 0x01) + { + + } + else if (message[4] == 0x02) + { + + } } } } -- 2.47.2 From 22558e3289b98f84df25937d41ac2466f0f59d6e Mon Sep 17 00:00:00 2001 From: shinichi Date: Fri, 25 Sep 2020 13:11:14 +0200 Subject: [PATCH 33/54] print received data on client side --- Client/Client.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index d6dc26c..e0248bc 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -1,5 +1,7 @@ using System; +using System.Linq; using System.Net.Sockets; +using System.Text; using ProftaakRH; namespace Client @@ -70,14 +72,14 @@ namespace Client Array.Copy(totalBuffer, 0, messageBytes, 0, expectedMessageLength); string identifier; - bool isJson = DataParser.getJsonIdentifier(this.buffer, out identifier); + bool isJson = DataParser.getJsonIdentifier(messageBytes, out identifier); if (isJson) { - throw new NotImplementedException(); + Console.WriteLine($"Received json :\n{Encoding.ASCII.GetString(messageBytes.Skip(5).ToArray())}"); } else if (DataParser.isRawData(this.buffer)) { - throw new NotImplementedException(); + Console.WriteLine($"Received data: {BitConverter.ToString(messageBytes.Skip(5).ToArray())}"); } totalBufferReceived -= expectedMessageLength; -- 2.47.2 From 8204f22fe7514af8339b5bd2ccbcb265d1f1f3a2 Mon Sep 17 00:00:00 2001 From: shinichi Date: Fri, 25 Sep 2020 13:18:19 +0200 Subject: [PATCH 34/54] better printing on client side --- Client/Client.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index e0248bc..12acd78 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -75,9 +75,9 @@ namespace Client bool isJson = DataParser.getJsonIdentifier(messageBytes, out identifier); if (isJson) { - Console.WriteLine($"Received json :\n{Encoding.ASCII.GetString(messageBytes.Skip(5).ToArray())}"); + Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(messageBytes.Skip(5).ToArray())}"); } - else if (DataParser.isRawData(this.buffer)) + else if (DataParser.isRawData(messageBytes)) { Console.WriteLine($"Received data: {BitConverter.ToString(messageBytes.Skip(5).ToArray())}"); } -- 2.47.2 From 80ee448acf617336e69f873573509c4b70788dbf Mon Sep 17 00:00:00 2001 From: fabjuuuh Date: Fri, 25 Sep 2020 13:18:41 +0200 Subject: [PATCH 35/54] Handledata --- Server/Client.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Server/Client.cs b/Server/Client.cs index 27a4f49..c8b6bf6 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -41,6 +41,7 @@ namespace Server while (totalBufferReceived >= expectedMessageLength) { //volledig packet binnen + Console.WriteLine(expectedMessageLength); byte[] messageBytes = new byte[expectedMessageLength]; Array.Copy(totalBuffer, 0, messageBytes, 0, expectedMessageLength); HandleData(messageBytes); @@ -50,6 +51,10 @@ namespace Server totalBufferReceived -= expectedMessageLength; expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0); + if (expectedMessageLength <= 5) + { + break; + } } this.stream.BeginRead(this.buffer, 0, this.buffer.Length, new AsyncCallback(OnRead), null); @@ -63,14 +68,23 @@ namespace Server { //Console.WriteLine("Data " + packet); //JsonConvert.DeserializeObject(packet); + //0x01 Json + //0x01 Raw data + if (message[4] == 0x01) { + byte[] jsonArray = new byte[message.Length - 5]; + Array.Copy(message, 5, jsonArray, 0, message.Length - 5); + dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(jsonArray)); + Console.WriteLine(json); } else if (message[4] == 0x02) { } + + } } } -- 2.47.2 From d0071bd13c469768e3326c231a6f5c764728360f Mon Sep 17 00:00:00 2001 From: shinichi Date: Fri, 25 Sep 2020 13:22:16 +0200 Subject: [PATCH 36/54] bug fix --- Server/Client.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Server/Client.cs b/Server/Client.cs index c8b6bf6..242d536 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -41,13 +41,11 @@ namespace Server while (totalBufferReceived >= expectedMessageLength) { //volledig packet binnen - Console.WriteLine(expectedMessageLength); byte[] messageBytes = new byte[expectedMessageLength]; Array.Copy(totalBuffer, 0, messageBytes, 0, expectedMessageLength); HandleData(messageBytes); - //Array.Copy(totalBuffer, expectedMessageLength, totalBuffer, 0, (totalBufferReceived - expectedMessageLength)); //maybe unsafe idk - totalBuffer = totalBuffer.Skip(expectedMessageLength).ToArray(); + Array.Copy(totalBuffer, expectedMessageLength, totalBuffer, 0, (totalBufferReceived - expectedMessageLength)); //maybe unsafe idk totalBufferReceived -= expectedMessageLength; expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0); -- 2.47.2 From fac3987678681a5c385ee5120c3e6c8528f9479b Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Fri, 25 Sep 2020 13:26:48 +0200 Subject: [PATCH 37/54] added delegate for reading server response --- RH-Engine/Command.cs | 22 +++++++++-- RH-Engine/Program.cs | 13 +++++++ RH-Engine/ServerResponseReader.cs | 64 +++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 RH-Engine/ServerResponseReader.cs diff --git a/RH-Engine/Command.cs b/RH-Engine/Command.cs index d7de332..dd3d7ff 100644 --- a/RH-Engine/Command.cs +++ b/RH-Engine/Command.cs @@ -119,9 +119,9 @@ namespace RH_Engine panel = new { size = new int[] { 1, 1 }, - resolution = new int[] { 256, 256 }, - background = new int[] { 1, 1, 1, 1 }, - castShadow = true + resolution = new int[] { 512, 512 }, + background = new int[] { 1, 0, 0, 0 }, + castShadow = false } } @@ -140,7 +140,7 @@ namespace RH_Engine { id = uuidPanel, text = "Bike speed placeholder", - position = new int[] { 0, 0 }, + position = new int[] { 100, 100 }, size = 32.0, color = new int[] { 0, 0, 0, 1 }, font = "segoeui" @@ -150,6 +150,20 @@ namespace RH_Engine return JsonConvert.SerializeObject(Payload(payload)); } + public string SwapPanelCommand(string uuid) + { + dynamic payload = new + { + id = "scene/panel/swap", + data = new + { + id = uuid + } + }; + + return JsonConvert.SerializeObject(Payload(payload)); + } + public string ClearPanel(string uuid) { dynamic payload = new diff --git a/RH-Engine/Program.cs b/RH-Engine/Program.cs index 8a710c3..3951d32 100644 --- a/RH-Engine/Program.cs +++ b/RH-Engine/Program.cs @@ -19,16 +19,27 @@ namespace RH_Engine //new PC("DESKTOP-M2CIH87", "Fabian"), //new PC("T470S", "Shinichi"), //new PC("DESKTOP-DHS478C", "semme"), + new PC("HP-ZBOOK-SEM", "Sem"), //new PC("DESKTOP-TV73FKO", "Wouter"), new PC("DESKTOP-SINMKT1", "Ralf van Aert"), //new PC("NA", "Bart") }; + + private static ServerResponseReader serverResponseReader; private static void Main(string[] args) { TcpClient client = new TcpClient("145.48.6.10", 6666); CreateConnection(client.GetStream()); + serverResponseReader = new ServerResponseReader(client.GetStream()); + serverResponseReader.callback = HandleResponse; + + + } + + public static void HandleResponse(string message) + { } @@ -146,6 +157,8 @@ namespace RH_Engine Console.WriteLine(ReadPrefMessage(stream)); WriteTextMessage(stream, mainCommand.bikeSpeed(uuidPanel)); Console.WriteLine(ReadPrefMessage(stream)); + + WriteTextMessage(stream, mainCommand.SwapPanelCommand(uuidPanel)); } /// diff --git a/RH-Engine/ServerResponseReader.cs b/RH-Engine/ServerResponseReader.cs new file mode 100644 index 0000000..811352c --- /dev/null +++ b/RH-Engine/ServerResponseReader.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using System.Net.Sockets; +using System.Text; +using System.Threading; + +namespace RH_Engine +{ + public delegate void OnResponse(string response); + class ServerResponseReader + { + public OnResponse callback + { + get;set; + } + public NetworkStream Stream { get; } + + public ServerResponseReader(NetworkStream stream) + { + this.Stream = stream; + } + + public void StartRead() + { + Thread t = new Thread(() => + { + if (this.callback == null) + { + throw new Exception("Callback not initialized!"); + } else + while (true) + { + string res = ReadPrefMessage(Stream); + this.callback(res); + } + }); + } + + public static string ReadPrefMessage(NetworkStream stream) + { + byte[] lengthBytes = new byte[4]; + + stream.Read(lengthBytes, 0, 4); + Console.WriteLine("read message.."); + + int length = BitConverter.ToInt32(lengthBytes); + + //Console.WriteLine("length is: " + length); + + byte[] buffer = new byte[length]; + int totalRead = 0; + + //read bytes until stream indicates there are no more + do + { + int read = stream.Read(buffer, totalRead, buffer.Length - totalRead); + totalRead += read; + //Console.WriteLine("ReadMessage: " + read); + } while (totalRead < length); + + return Encoding.UTF8.GetString(buffer, 0, totalRead); + } + } +} -- 2.47.2 From 23f146afdda078131a9db53697a1bac08b0d6534 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Fri, 25 Sep 2020 13:41:54 +0200 Subject: [PATCH 38/54] getting response from server through callback --- RH-Engine/Program.cs | 38 +++++++++++++++++-------------- RH-Engine/ServerResponseReader.cs | 15 ++++++++---- 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/RH-Engine/Program.cs b/RH-Engine/Program.cs index 3951d32..caac7ac 100644 --- a/RH-Engine/Program.cs +++ b/RH-Engine/Program.cs @@ -31,16 +31,18 @@ namespace RH_Engine TcpClient client = new TcpClient("145.48.6.10", 6666); CreateConnection(client.GetStream()); + } - serverResponseReader = new ServerResponseReader(client.GetStream()); + private static void initReader(NetworkStream stream) + { + serverResponseReader = new ServerResponseReader(stream); serverResponseReader.callback = HandleResponse; - - + serverResponseReader.StartRead(); } public static void HandleResponse(string message) { - + Console.WriteLine("GOT MESSAGE FROM CALLBACK!!!!! " + message); } /// @@ -58,7 +60,7 @@ namespace RH_Engine stream.Write(res); - //Console.WriteLine("sent message " + message); + Console.WriteLine("sent message " + message); } /// @@ -96,25 +98,27 @@ namespace RH_Engine /// the network stream to use private static void CreateConnection(NetworkStream stream) { + initReader(stream); + WriteTextMessage(stream, "{\r\n\"id\" : \"session/list\"\r\n}"); - string id = JSONParser.GetSessionID(ReadPrefMessage(stream), PCs); + //string id = JSONParser.GetSessionID(ReadPrefMessage(stream), PCs); - string tunnelCreate = "{\"id\" : \"tunnel/create\", \"data\" : {\"session\" : \"" + id + "\"}}"; + //string tunnelCreate = "{\"id\" : \"tunnel/create\", \"data\" : {\"session\" : \"" + id + "\"}}"; - WriteTextMessage(stream, tunnelCreate); + //WriteTextMessage(stream, tunnelCreate); - string tunnelResponse = ReadPrefMessage(stream); + //string tunnelResponse = ReadPrefMessage(stream); - Console.WriteLine(tunnelResponse); + //Console.WriteLine(tunnelResponse); - string tunnelID = JSONParser.GetTunnelID(tunnelResponse); - if (tunnelID == null) - { - Console.WriteLine("could not find a valid tunnel id!"); - return; - } + //string tunnelID = JSONParser.GetTunnelID(tunnelResponse); + //if (tunnelID == null) + //{ + // Console.WriteLine("could not find a valid tunnel id!"); + // return; + //} - sendCommands(stream, tunnelID); + //sendCommands(stream, tunnelID); } /// diff --git a/RH-Engine/ServerResponseReader.cs b/RH-Engine/ServerResponseReader.cs index 811352c..01d9a64 100644 --- a/RH-Engine/ServerResponseReader.cs +++ b/RH-Engine/ServerResponseReader.cs @@ -27,21 +27,28 @@ namespace RH_Engine if (this.callback == null) { throw new Exception("Callback not initialized!"); - } else - while (true) + } + else { + Console.WriteLine("Starting loop for reading"); + while (true) + { string res = ReadPrefMessage(Stream); + //Console.WriteLine("[SERVERRESPONSEREADER] got message from server: " + res); this.callback(res); + } } }); + + t.Start(); } public static string ReadPrefMessage(NetworkStream stream) { byte[] lengthBytes = new byte[4]; - stream.Read(lengthBytes, 0, 4); - Console.WriteLine("read message.."); + int streamread = stream.Read(lengthBytes, 0, 4); + Console.WriteLine("read message.. " + streamread); int length = BitConverter.ToInt32(lengthBytes); -- 2.47.2 From cf9341a93e3fa4cbf42114ffbac25590da2f2822 Mon Sep 17 00:00:00 2001 From: fabjuuuh Date: Fri, 25 Sep 2020 13:43:00 +0200 Subject: [PATCH 39/54] SaveData --- Server/Client.cs | 5 ++++- Server/SaveData.cs | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 Server/SaveData.cs diff --git a/Server/Client.cs b/Server/Client.cs index 242d536..9e1b2a9 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -14,12 +14,14 @@ namespace Server private byte[] buffer = new byte[1024]; private byte[] totalBuffer = new byte[1024]; private int totalBufferReceived = 0; + private SaveData saveData; public string Username { get; set; } public Client(Communication communication, TcpClient tcpClient) { + this.saveData = new SaveData(); this.communication = communication; this.tcpClient = tcpClient; this.stream = this.tcpClient.GetStream(); @@ -75,11 +77,12 @@ namespace Server Array.Copy(message, 5, jsonArray, 0, message.Length - 5); dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(jsonArray)); Console.WriteLine(json); + saveData.WriteData(Encoding.ASCII.GetString(jsonArray)); } else if (message[4] == 0x02) { - + Console.WriteLine(message); } diff --git a/Server/SaveData.cs b/Server/SaveData.cs new file mode 100644 index 0000000..eddb8a9 --- /dev/null +++ b/Server/SaveData.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace Server +{ + class SaveData + { + public SaveData() + { + } + + public void WriteData(string data) + { + using (StreamWriter sw = File.AppendText(Directory.GetCurrentDirectory() + "/data.txt")) + { + sw.WriteLine(data); + } + } + } +} -- 2.47.2 From 6ef5bbfe127c5a373163e87f55939f3d40eca95d Mon Sep 17 00:00:00 2001 From: fabjuuuh Date: Fri, 25 Sep 2020 14:06:45 +0200 Subject: [PATCH 40/54] More SaveData --- Server/Client.cs | 6 ++++-- Server/SaveData.cs | 20 +++++++++++++++++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/Server/Client.cs b/Server/Client.cs index 9e1b2a9..9ff610e 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -1,4 +1,5 @@ using System; +using System.IO; using System.Linq; using System.Net.Sockets; using System.Text; @@ -21,7 +22,7 @@ namespace Server public Client(Communication communication, TcpClient tcpClient) { - this.saveData = new SaveData(); + this.saveData = new SaveData(Directory.GetCurrentDirectory()+$"/test"); this.communication = communication; this.tcpClient = tcpClient; this.stream = this.tcpClient.GetStream(); @@ -77,12 +78,13 @@ namespace Server Array.Copy(message, 5, jsonArray, 0, message.Length - 5); dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(jsonArray)); Console.WriteLine(json); - saveData.WriteData(Encoding.ASCII.GetString(jsonArray)); + saveData.WriteDataJSON(Encoding.ASCII.GetString(jsonArray)); } else if (message[4] == 0x02) { Console.WriteLine(message); + saveData.WriteDataRAW(Encoding.ASCII.GetString(message)); } diff --git a/Server/SaveData.cs b/Server/SaveData.cs index eddb8a9..640a862 100644 --- a/Server/SaveData.cs +++ b/Server/SaveData.cs @@ -7,13 +7,27 @@ namespace Server { class SaveData { - public SaveData() + private string path; + public SaveData(string path) { + this.path = path; + if (!Directory.Exists(path)) + { + Directory.CreateDirectory(path); + } } - public void WriteData(string data) + public void WriteDataJSON(string data) { - using (StreamWriter sw = File.AppendText(Directory.GetCurrentDirectory() + "/data.txt")) + using (StreamWriter sw = File.AppendText(this.path + "/dataJSON.txt")) + { + sw.WriteLine(data); + } + } + + public void WriteDataRAW(string data) + { + using (StreamWriter sw = File.AppendText(this.path + "/dataRAW.txt")) { sw.WriteLine(data); } -- 2.47.2 From 23846b14bcd891f4b159a6f86534ad3f97e4bb46 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Fri, 25 Sep 2020 14:06:56 +0200 Subject: [PATCH 41/54] creating connection with new reading stuff works --- RH-Engine/JSONParser.cs | 13 +++++++++++ RH-Engine/Program.cs | 50 +++++++++++++++++++++++++++-------------- 2 files changed, 46 insertions(+), 17 deletions(-) diff --git a/RH-Engine/JSONParser.cs b/RH-Engine/JSONParser.cs index 2309d7f..2a8ca2b 100644 --- a/RH-Engine/JSONParser.cs +++ b/RH-Engine/JSONParser.cs @@ -50,6 +50,19 @@ namespace RH_Engine return null; } + public static string GetSerial(string json) + { + dynamic jsonData = JsonConvert.DeserializeObject(json); + return jsonData.serial; + } + + public static string GetID(string json) + { + dynamic d = JsonConvert.DeserializeObject(json); + return d.id; + + } + public static string GetTunnelID(string json) { dynamic jsonData = JsonConvert.DeserializeObject(json); diff --git a/RH-Engine/Program.cs b/RH-Engine/Program.cs index caac7ac..9aaa7f6 100644 --- a/RH-Engine/Program.cs +++ b/RH-Engine/Program.cs @@ -26,6 +26,8 @@ namespace RH_Engine }; private static ServerResponseReader serverResponseReader; + private static string sessionId = string.Empty; + private static string tunnelId = string.Empty; private static void Main(string[] args) { TcpClient client = new TcpClient("145.48.6.10", 6666); @@ -42,7 +44,24 @@ namespace RH_Engine public static void HandleResponse(string message) { - Console.WriteLine("GOT MESSAGE FROM CALLBACK!!!!! " + message); + //Console.WriteLine("GOT MESSAGE FROM CALLBACK!!!!! " + message); + + string id = JSONParser.GetID(message); + Console.WriteLine("got id : " + id); + + // because the first messages don't have a serial, we need to check on the id + if (id == "session/list") + { + sessionId = JSONParser.GetSessionID(message,PCs); + } else if (id == "tunnel/create") + { + tunnelId = JSONParser.GetTunnelID(message); + if (tunnelId == null) + { + Console.WriteLine("could not find a valid tunnel id!"); + return; + } + } } /// @@ -92,6 +111,7 @@ namespace RH_Engine return Encoding.UTF8.GetString(buffer, 0, totalRead); } + /// /// connects to the server and creates the tunnel /// @@ -100,25 +120,19 @@ namespace RH_Engine { initReader(stream); - WriteTextMessage(stream, "{\r\n\"id\" : \"session/list\"\r\n}"); - //string id = JSONParser.GetSessionID(ReadPrefMessage(stream), PCs); + WriteTextMessage(stream, "{\r\n\"id\" : \"session/list\",\r\n\"serial\" : \"list\"\r\n}"); - //string tunnelCreate = "{\"id\" : \"tunnel/create\", \"data\" : {\"session\" : \"" + id + "\"}}"; + // wait until we have got a sessionId + while (sessionId == string.Empty) { } + + string tunnelCreate = "{\"id\" : \"tunnel/create\", \"data\" : {\"session\" : \"" + sessionId + "\"}}"; - //WriteTextMessage(stream, tunnelCreate); + WriteTextMessage(stream, tunnelCreate); - //string tunnelResponse = ReadPrefMessage(stream); - - //Console.WriteLine(tunnelResponse); - - //string tunnelID = JSONParser.GetTunnelID(tunnelResponse); - //if (tunnelID == null) - //{ - // Console.WriteLine("could not find a valid tunnel id!"); - // return; - //} - - //sendCommands(stream, tunnelID); + // wait until we have a tunnel id + while (tunnelId == string.Empty) { } + Console.WriteLine("got tunnel id! sending commands..."); + //sendCommands(stream, tunnelId); } /// @@ -139,6 +153,8 @@ namespace RH_Engine Console.WriteLine(ReadPrefMessage(stream)); string command; + + //command = mainCommand.AddBikeModel(); //WriteTextMessage(stream, command); -- 2.47.2 From bb30538f000358b3b5fb7323bd1b03b0489b09ab Mon Sep 17 00:00:00 2001 From: Logophilist Date: Fri, 25 Sep 2020 14:12:16 +0200 Subject: [PATCH 42/54] added features panel --- ProftaakRH/Main.cs | 6 +++--- RH-Engine/Command.cs | 33 +++++++++++++++++++++++++++++++-- RH-Engine/Program.cs | 8 +++++--- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/ProftaakRH/Main.cs b/ProftaakRH/Main.cs index eb0c2a5..5ffcab4 100644 --- a/ProftaakRH/Main.cs +++ b/ProftaakRH/Main.cs @@ -13,9 +13,9 @@ namespace ProftaakRH { IDataReceiver dataReceiver = new DataConverter(); BLEHandler bLEHandler = new BLEHandler(dataReceiver); - //BikeSimulator bikeSimulator = new BikeSimulator(dataConverter); - //bikeSimulator.setResistance(bikeSimulator.GenerateResistance(1f)); - //bikeSimulator.StartSimulation(); + BikeSimulator bikeSimulator = new BikeSimulator(dataReceiver); + bikeSimulator.setResistance(bikeSimulator.GenerateResistance(1f)); + bikeSimulator.StartSimulation(); bool running = true; diff --git a/RH-Engine/Command.cs b/RH-Engine/Command.cs index dd3d7ff..dd8fdb4 100644 --- a/RH-Engine/Command.cs +++ b/RH-Engine/Command.cs @@ -131,7 +131,36 @@ namespace RH_Engine return JsonConvert.SerializeObject(Payload(payload)); } - public string bikeSpeed(string uuidPanel) + public string ColorPanel(string uuidPanel) + { + dynamic payload = new + { + id = "scene/panel/setclearcolor", + data = new + { + id = uuidPanel, + color = new int[] { 1, 1, 1, 1 } + } + }; + + return JsonConvert.SerializeObject(Payload(payload)); + } + + public string SwapPanel(string uuid) + { + dynamic payload = new + { + id = "scene/panel/swap", + data = new + { + id = uuid + } + }; + + return JsonConvert.SerializeObject(Payload(payload)); + } + + public string bikeSpeed(string uuidPanel, double speed) { dynamic payload = new { @@ -140,7 +169,7 @@ namespace RH_Engine { id = uuidPanel, text = "Bike speed placeholder", - position = new int[] { 100, 100 }, + position = new int[] { 0, 0 }, size = 32.0, color = new int[] { 0, 0, 0, 1 }, font = "segoeui" diff --git a/RH-Engine/Program.cs b/RH-Engine/Program.cs index 9aaa7f6..d965579 100644 --- a/RH-Engine/Program.cs +++ b/RH-Engine/Program.cs @@ -175,10 +175,12 @@ namespace RH_Engine string uuidPanel = JSONParser.getPanelID(response); WriteTextMessage(stream, mainCommand.ClearPanel(uuidPanel)); Console.WriteLine(ReadPrefMessage(stream)); - WriteTextMessage(stream, mainCommand.bikeSpeed(uuidPanel)); + WriteTextMessage(stream, mainCommand.bikeSpeed(uuidPanel, 2.42)); Console.WriteLine(ReadPrefMessage(stream)); - - WriteTextMessage(stream, mainCommand.SwapPanelCommand(uuidPanel)); + WriteTextMessage(stream, mainCommand.ColorPanel(uuidPanel)); + Console.WriteLine("Color panel: " + ReadPrefMessage(stream)); + WriteTextMessage(stream, mainCommand.SwapPanel(uuidPanel)); + Console.WriteLine("Swap panel: " + ReadPrefMessage(stream)); } /// -- 2.47.2 From d3a37d023867fb03471753108752c4ace00a12be Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Fri, 25 Sep 2020 14:30:33 +0200 Subject: [PATCH 43/54] progress on improving responses --- RH-Engine/Command.cs | 4 +- RH-Engine/JSONParser.cs | 9 ++- RH-Engine/Program.cs | 109 +++++++++++++++--------------- RH-Engine/ServerResponseReader.cs | 5 ++ 4 files changed, 68 insertions(+), 59 deletions(-) diff --git a/RH-Engine/Command.cs b/RH-Engine/Command.cs index dd3d7ff..34201a7 100644 --- a/RH-Engine/Command.cs +++ b/RH-Engine/Command.cs @@ -264,6 +264,7 @@ namespace RH_Engine dynamic payload = new { id = "route/add", + serial = "route", data = new { nodes = new dynamic[] @@ -354,7 +355,8 @@ namespace RH_Engine { dynamic payload = new { - id = "scene/reset" + id = "scene/reset", + serial = "reset" }; return JsonConvert.SerializeObject(Payload(payload)); diff --git a/RH-Engine/JSONParser.cs b/RH-Engine/JSONParser.cs index 2a8ca2b..575b1bd 100644 --- a/RH-Engine/JSONParser.cs +++ b/RH-Engine/JSONParser.cs @@ -53,7 +53,7 @@ namespace RH_Engine public static string GetSerial(string json) { dynamic jsonData = JsonConvert.DeserializeObject(json); - return jsonData.serial; + return jsonData.data.data.serial; } public static string GetID(string json) @@ -73,7 +73,12 @@ namespace RH_Engine return null; } - public static string GetRouteID(string json) + /// + /// method to get the uuid from requests for adding a node,route or road + /// + /// the json response froo the server + /// the uuid of the created object + public static string GetResponseUuid(string json) { dynamic jsonData = JsonConvert.DeserializeObject(json); if (jsonData.data.status == "ok") diff --git a/RH-Engine/Program.cs b/RH-Engine/Program.cs index 9aaa7f6..4a77c08 100644 --- a/RH-Engine/Program.cs +++ b/RH-Engine/Program.cs @@ -2,6 +2,7 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; +using System.Collections.Generic; using System.Globalization; using System.IO; using System.Net.Sockets; @@ -13,6 +14,7 @@ using System.Threading; namespace RH_Engine { + public delegate void HandleSerial(string uuid); internal class Program { private static PC[] PCs = { @@ -28,13 +30,25 @@ namespace RH_Engine private static ServerResponseReader serverResponseReader; private static string sessionId = string.Empty; private static string tunnelId = string.Empty; + + private static Dictionary serialResponses = new Dictionary(); + + private static void Main(string[] args) { + + serialResponses.Add("route",handleRouteSerial); + TcpClient client = new TcpClient("145.48.6.10", 6666); CreateConnection(client.GetStream()); } + private static void handleRouteSerial(string uuid) + { + + } + private static void initReader(NetworkStream stream) { serverResponseReader = new ServerResponseReader(stream); @@ -44,12 +58,11 @@ namespace RH_Engine public static void HandleResponse(string message) { - //Console.WriteLine("GOT MESSAGE FROM CALLBACK!!!!! " + message); string id = JSONParser.GetID(message); - Console.WriteLine("got id : " + id); - // because the first messages don't have a serial, we need to check on the id + // because the first messages doesn't have a serial, we need to check on the id + if (id == "session/list") { sessionId = JSONParser.GetSessionID(message,PCs); @@ -62,6 +75,15 @@ namespace RH_Engine return; } } + + if (message.Contains("serial")) + { + string serial = JSONParser.GetSerial(message); + Console.WriteLine("Got serial " + serial); + serialResponses[serial].Invoke(); + } + + } /// @@ -82,35 +104,7 @@ namespace RH_Engine Console.WriteLine("sent message " + message); } - /// - /// reads a response from the server - /// - /// the network stream to use - /// the returned message from the server - public static string ReadPrefMessage(NetworkStream stream) - { - byte[] lengthBytes = new byte[4]; - - stream.Read(lengthBytes, 0, 4); - Console.WriteLine("read message.."); - - int length = BitConverter.ToInt32(lengthBytes); - - //Console.WriteLine("length is: " + length); - - byte[] buffer = new byte[length]; - int totalRead = 0; - - //read bytes until stream indicates there are no more - do - { - int read = stream.Read(buffer, totalRead, buffer.Length - totalRead); - totalRead += read; - //Console.WriteLine("ReadMessage: " + read); - } while (totalRead < length); - - return Encoding.UTF8.GetString(buffer, 0, totalRead); - } + /// /// connects to the server and creates the tunnel @@ -132,7 +126,7 @@ namespace RH_Engine // wait until we have a tunnel id while (tunnelId == string.Empty) { } Console.WriteLine("got tunnel id! sending commands..."); - //sendCommands(stream, tunnelId); + sendCommands(stream, tunnelId); } /// @@ -146,12 +140,10 @@ namespace RH_Engine WriteTextMessage(stream, mainCommand.ResetScene()); - ReadPrefMessage(stream); string routeid = CreateRoute(stream, mainCommand); - WriteTextMessage(stream, mainCommand.TerrainCommand(new int[] { 256, 256 }, null)); - Console.WriteLine(ReadPrefMessage(stream)); - string command; + //WriteTextMessage(stream, mainCommand.TerrainCommand(new int[] { 256, 256 }, null)); + //string command; @@ -168,17 +160,17 @@ namespace RH_Engine //Console.WriteLine(ReadPrefMessage(stream)); - command = mainCommand.addPanel(); - WriteTextMessage(stream, command); - string response = ReadPrefMessage(stream); - Console.WriteLine("add Panel response: \n\r" + response); - string uuidPanel = JSONParser.getPanelID(response); - WriteTextMessage(stream, mainCommand.ClearPanel(uuidPanel)); - Console.WriteLine(ReadPrefMessage(stream)); - WriteTextMessage(stream, mainCommand.bikeSpeed(uuidPanel)); - Console.WriteLine(ReadPrefMessage(stream)); + //command = mainCommand.addPanel(); + //WriteTextMessage(stream, command); + //string response = ReadPrefMessage(stream); + //Console.WriteLine("add Panel response: \n\r" + response); + //string uuidPanel = JSONParser.getPanelID(response); + //WriteTextMessage(stream, mainCommand.ClearPanel(uuidPanel)); + //Console.WriteLine(ReadPrefMessage(stream)); + //WriteTextMessage(stream, mainCommand.bikeSpeed(uuidPanel)); + //Console.WriteLine(ReadPrefMessage(stream)); - WriteTextMessage(stream, mainCommand.SwapPanelCommand(uuidPanel)); + //WriteTextMessage(stream, mainCommand.SwapPanelCommand(uuidPanel)); } /// @@ -206,12 +198,14 @@ namespace RH_Engine public static string CreateRoute(NetworkStream stream, Command createGraphics) { + //=============================================================================================================TODO change WriteTextMessage(stream, createGraphics.RouteCommand()); - dynamic response = JsonConvert.DeserializeObject(ReadPrefMessage(stream)); - if (response.data.data.id == "route/add") - { - return response.data.data.data.uuid; - } + //dynamic response = JsonConvert.DeserializeObject(ReadPrefMessage(stream)); + //dynamic response = null; + //if (response.data.data.id == "route/add") + //{ + // return response.data.data.data.uuid; + //} return null; } @@ -227,11 +221,12 @@ namespace RH_Engine x += 0.001f; } + //=============================================================================================================TODO change WriteTextMessage(stream, createGraphics.TerrainCommand(new int[] { 256, 256 }, height)); - Console.WriteLine(ReadPrefMessage(stream)); + //Console.WriteLine(ReadPrefMessage(stream)); WriteTextMessage(stream, createGraphics.AddNodeCommand()); - Console.WriteLine(ReadPrefMessage(stream)); + //Console.WriteLine(ReadPrefMessage(stream)); } /// @@ -243,8 +238,10 @@ namespace RH_Engine public static JArray GetChildren(NetworkStream stream, Command createGraphics) { WriteTextMessage(stream, createGraphics.GetSceneInfoCommand()); - dynamic response = JsonConvert.DeserializeObject(ReadPrefMessage(stream)); - return response.data.data.data.children; + //dynamic response = JsonConvert.DeserializeObject(ReadPrefMessage(stream)); + //return response.data.data.data.children; + //=============================================================================================================TODO change + return null; } /// diff --git a/RH-Engine/ServerResponseReader.cs b/RH-Engine/ServerResponseReader.cs index 01d9a64..04d3312 100644 --- a/RH-Engine/ServerResponseReader.cs +++ b/RH-Engine/ServerResponseReader.cs @@ -43,6 +43,11 @@ namespace RH_Engine t.Start(); } + /// + /// reads a response from the server + /// + /// the network stream to use + /// the returned message from the server public static string ReadPrefMessage(NetworkStream stream) { byte[] lengthBytes = new byte[4]; -- 2.47.2 From 9c3b2c3f9b9855aebb7df63414949714f3c6f01c Mon Sep 17 00:00:00 2001 From: shinichi Date: Fri, 25 Sep 2020 15:00:43 +0200 Subject: [PATCH 44/54] Auto stash before merge of "client" and "origin/client" --- Client/Client.cs | 10 +++++++- Client/DataParser.cs | 40 ++++++++++++++++++++++++++++++-- Server/Client.cs | 55 ++++++++++++++++++++++++++++++++++++++++++-- Server/Server.csproj | 20 +++++++++------- 4 files changed, 112 insertions(+), 13 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index 12acd78..023b806 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -75,7 +75,15 @@ namespace Client bool isJson = DataParser.getJsonIdentifier(messageBytes, out identifier); if (isJson) { - Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(messageBytes.Skip(5).ToArray())}"); + switch (identifier) + { + case DataParser.LOGIN: + Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(messageBytes.Skip(5).ToArray())}"); + break; + default: + Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(messageBytes.Skip(5).ToArray())}"); + break; + } } else if (DataParser.isRawData(messageBytes)) { diff --git a/Client/DataParser.cs b/Client/DataParser.cs index 57a9b1c..2bc16e5 100644 --- a/Client/DataParser.cs +++ b/Client/DataParser.cs @@ -1,12 +1,16 @@ using Newtonsoft.Json; +using Newtonsoft.Json.Serialization; using System; +using System.Globalization; using System.Linq; using System.Text; namespace Client { - class DataParser + public class DataParser { + public const string LOGIN = "LOGIN"; + public const string LOGIN_RESPONSE = "LOGIN_RESPONSE"; /// /// makes the json object with LOGIN identifier and username and password /// @@ -17,7 +21,7 @@ namespace Client { dynamic json = new { - identifier = "LOGIN", + identifier = LOGIN, data = new { username = mUsername, @@ -28,6 +32,38 @@ namespace Client return Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(json)); } + public static bool GetUsernamePassword(byte[] jsonbytes, out string username, out string password) + { + dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(jsonbytes)); + try + { + username = json.data.username; + password = json.data.password; + return true; + } + catch + { + username = null; + password = null; + return false; + } + } + + private static byte[] getJsonMessage(string mIdentifier, dynamic data) + { + dynamic json = new + { + identifier = mIdentifier, + data + }; + return getMessage(Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(json)), 0x01); + } + + public static byte[] getLoginResponse(string mStatus) + { + return getJsonMessage(LOGIN_RESPONSE, new { status = mStatus }); + } + /// /// get the identifier from json /// diff --git a/Server/Client.cs b/Server/Client.cs index 9ff610e..df16fa2 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -3,6 +3,7 @@ using System.IO; using System.Linq; using System.Net.Sockets; using System.Text; +using Client; using Newtonsoft.Json; namespace Server @@ -16,6 +17,8 @@ namespace Server private byte[] totalBuffer = new byte[1024]; private int totalBufferReceived = 0; private SaveData saveData; + private string username = null; + public string Username { get; set; } @@ -61,6 +64,13 @@ namespace Server this.stream.BeginRead(this.buffer, 0, this.buffer.Length, new AsyncCallback(OnRead), null); } + + private void OnWrite(IAsyncResult ar) + { + this.stream.EndWrite(ar); + } + + /// /// TODO /// @@ -72,8 +82,44 @@ namespace Server //0x01 Json //0x01 Raw data - if (message[4] == 0x01) + byte[] payloadbytes = new byte[BitConverter.ToInt32(message, 0) - 5]; + + Array.Copy(message, 5, payloadbytes, 0, payloadbytes.Length); + + string identifier; + bool isJson = DataParser.getJsonIdentifier(message, out identifier); + if (isJson) { + Console.WriteLine($"received json with identifier {identifier} and expecting {DataParser.LOGIN}"); + switch (identifier) + { + case DataParser.LOGIN: + Console.WriteLine("LOGIN"); + string username; + string password; + bool worked = DataParser.GetUsernamePassword(payloadbytes, out username, out password); + if (worked) + { + if (verifyLogin(username, password)) + { + this.username = username; + stream.BeginWrite(DataParser.getLoginResponse("OK"), 0, 0, new AsyncCallback(OnWrite), null); + } + else + { + stream.BeginWrite(DataParser.getLoginResponse("wrong username or password"), 0, 0, new AsyncCallback(OnWrite), null); + } + } + else + { + stream.BeginWrite(DataParser.getLoginResponse("invalid json"), 0, 0, new AsyncCallback(OnWrite), null); + } + break; + default: + Console.WriteLine("default"); + Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(payloadbytes)}"); + break; + } byte[] jsonArray = new byte[message.Length - 5]; Array.Copy(message, 5, jsonArray, 0, message.Length - 5); dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(jsonArray)); @@ -81,7 +127,7 @@ namespace Server saveData.WriteDataJSON(Encoding.ASCII.GetString(jsonArray)); } - else if (message[4] == 0x02) + else if (DataParser.isRawData(message)) { Console.WriteLine(message); saveData.WriteDataRAW(Encoding.ASCII.GetString(message)); @@ -89,5 +135,10 @@ namespace Server } + + private bool verifyLogin(string username, string password) + { + return username == password; + } } } diff --git a/Server/Server.csproj b/Server/Server.csproj index 0eaee1b..a91c176 100644 --- a/Server/Server.csproj +++ b/Server/Server.csproj @@ -1,12 +1,16 @@ - + - - Exe - netcoreapp3.1 - + + Exe + netcoreapp3.1 + - - - + + + + + + + -- 2.47.2 From 21203fd3ff303e9b1ca38e739104bd29adaa0074 Mon Sep 17 00:00:00 2001 From: shinichi Date: Fri, 25 Sep 2020 15:33:39 +0200 Subject: [PATCH 45/54] login implemented --- Client/Client.cs | 48 ++++++++++++++++++++++++++++---------------- Client/DataParser.cs | 5 +++++ Server/Client.cs | 24 ++++++++++------------ 3 files changed, 47 insertions(+), 30 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index 023b806..123d222 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -36,21 +36,9 @@ namespace Client this.stream = this.client.GetStream(); - //TODO File in lezen - Console.WriteLine("enter username"); - string username = Console.ReadLine(); - Console.WriteLine("enter password"); - string password = Console.ReadLine(); - - byte[] message = DataParser.getJsonMessage(DataParser.GetLoginJson(username, password)); - - this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); + tryLogin(); this.stream.BeginRead(this.buffer, 0, this.buffer.Length, new AsyncCallback(OnRead), null); - - //TODO lees OK message - //temp moet eigenlijk een ok bericht ontvangen - this.connected = true; } private void OnRead(IAsyncResult ar) @@ -71,23 +59,37 @@ namespace Client byte[] messageBytes = new byte[expectedMessageLength]; Array.Copy(totalBuffer, 0, messageBytes, 0, expectedMessageLength); + + byte[] payloadbytes = new byte[BitConverter.ToInt32(messageBytes, 0) - 5]; + + Array.Copy(messageBytes, 5, payloadbytes, 0, payloadbytes.Length); + string identifier; bool isJson = DataParser.getJsonIdentifier(messageBytes, out identifier); if (isJson) { switch (identifier) { - case DataParser.LOGIN: - Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(messageBytes.Skip(5).ToArray())}"); + case DataParser.LOGIN_RESPONSE: + string responseStatus = DataParser.getResponseStatus(payloadbytes); + if (responseStatus == "OK") + { + this.connected = true; + } + else + { + Console.WriteLine($"login failed \"{responseStatus}\""); + tryLogin(); + } break; default: - Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(messageBytes.Skip(5).ToArray())}"); + Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(payloadbytes)}"); break; } } else if (DataParser.isRawData(messageBytes)) { - Console.WriteLine($"Received data: {BitConverter.ToString(messageBytes.Skip(5).ToArray())}"); + Console.WriteLine($"Received data: {BitConverter.ToString(payloadbytes)}"); } totalBufferReceived -= expectedMessageLength; @@ -131,5 +133,17 @@ namespace Client { return this.connected; } + private void tryLogin() + { + //TODO File in lezen + Console.WriteLine("enter username"); + string username = Console.ReadLine(); + Console.WriteLine("enter password"); + string password = Console.ReadLine(); + + byte[] message = DataParser.getJsonMessage(DataParser.GetLoginJson(username, password)); + + this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); + } } } diff --git a/Client/DataParser.cs b/Client/DataParser.cs index 2bc16e5..c83b86a 100644 --- a/Client/DataParser.cs +++ b/Client/DataParser.cs @@ -64,6 +64,11 @@ namespace Client return getJsonMessage(LOGIN_RESPONSE, new { status = mStatus }); } + public static string getResponseStatus(byte[] json) + { + return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.status; + } + /// /// get the identifier from json /// diff --git a/Server/Client.cs b/Server/Client.cs index df16fa2..43d4fd1 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -25,7 +25,7 @@ namespace Server public Client(Communication communication, TcpClient tcpClient) { - this.saveData = new SaveData(Directory.GetCurrentDirectory()+$"/test"); + this.saveData = new SaveData(Directory.GetCurrentDirectory() + $"/test"); this.communication = communication; this.tcpClient = tcpClient; this.stream = this.tcpClient.GetStream(); @@ -90,11 +90,9 @@ namespace Server bool isJson = DataParser.getJsonIdentifier(message, out identifier); if (isJson) { - Console.WriteLine($"received json with identifier {identifier} and expecting {DataParser.LOGIN}"); switch (identifier) { case DataParser.LOGIN: - Console.WriteLine("LOGIN"); string username; string password; bool worked = DataParser.GetUsernamePassword(payloadbytes, out username, out password); @@ -103,33 +101,33 @@ namespace Server if (verifyLogin(username, password)) { this.username = username; - stream.BeginWrite(DataParser.getLoginResponse("OK"), 0, 0, new AsyncCallback(OnWrite), null); + byte[] response = DataParser.getLoginResponse("OK"); + stream.BeginWrite(response, 0, response.Length, new AsyncCallback(OnWrite), null); } else { - stream.BeginWrite(DataParser.getLoginResponse("wrong username or password"), 0, 0, new AsyncCallback(OnWrite), null); + byte[] response = DataParser.getLoginResponse("wrong username or password"); + stream.BeginWrite(response, 0, response.Length, new AsyncCallback(OnWrite), null); } } else { - stream.BeginWrite(DataParser.getLoginResponse("invalid json"), 0, 0, new AsyncCallback(OnWrite), null); + byte[] response = DataParser.getLoginResponse("invalid json"); + stream.BeginWrite(response, 0, response.Length, new AsyncCallback(OnWrite), null); } break; default: - Console.WriteLine("default"); Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(payloadbytes)}"); break; } - byte[] jsonArray = new byte[message.Length - 5]; - Array.Copy(message, 5, jsonArray, 0, message.Length - 5); - dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(jsonArray)); - Console.WriteLine(json); - saveData.WriteDataJSON(Encoding.ASCII.GetString(jsonArray)); + Array.Copy(message, 5, payloadbytes, 0, message.Length - 5); + dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(payloadbytes)); + saveData.WriteDataJSON(Encoding.ASCII.GetString(payloadbytes)); } else if (DataParser.isRawData(message)) { - Console.WriteLine(message); + Console.WriteLine(BitConverter.ToString(message)); saveData.WriteDataRAW(Encoding.ASCII.GetString(message)); } -- 2.47.2 From 1057c0caabbd27d6e85872da5a003b710a04461a Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Fri, 25 Sep 2020 15:40:07 +0200 Subject: [PATCH 46/54] added comments --- RH-Engine/Program.cs | 30 +++++++++++++++++------------- RH-Engine/ServerResponseReader.cs | 2 +- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/RH-Engine/Program.cs b/RH-Engine/Program.cs index 3b7aab5..b2d4cbf 100644 --- a/RH-Engine/Program.cs +++ b/RH-Engine/Program.cs @@ -45,6 +45,10 @@ namespace RH_Engine CreateConnection(client.GetStream()); } + /// + /// initializes and starts the reading of the responses from the vr server + /// + /// the networkstream private static void initReader(NetworkStream stream) { serverResponseReader = new ServerResponseReader(stream); @@ -52,13 +56,16 @@ namespace RH_Engine serverResponseReader.StartRead(); } + /// + /// callback method that handles responses from the server + /// + /// the response message from the server public static void HandleResponse(string message) { string id = JSONParser.GetID(message); - // because the first messages doesn't have a serial, we need to check on the id - + // because the first messages don't have a serial, we need to check on the id if (id == "session/list") { sessionId = JSONParser.GetSessionID(message,PCs); @@ -80,10 +87,15 @@ namespace RH_Engine if (serialResponses.ContainsKey(serial)) serialResponses[serial].Invoke(message); } - - } + /// + /// method that sends the speciefied message with the specified serial, and executes the given action upon receivind a reply from the server with this serial. + /// + /// the networkstream to use + /// the message to send + /// the serial to check for + /// the code to be executed upon reveiving a reply from the server with the specified serial public static void SendMessageAndOnResponse(NetworkStream stream, string message, string serial, HandleSerial action) { serialResponses.Add(serial,action); @@ -145,24 +157,16 @@ namespace RH_Engine WriteTextMessage(stream, mainCommand.ResetScene()); SendMessageAndOnResponse(stream, mainCommand.RouteCommand("routeID"), "routeID", (message) => routeId = JSONParser.GetResponseUuid(message)); + //WriteTextMessage(stream, mainCommand.TerrainCommand(new int[] { 256, 256 }, null)); //string command; - //WriteTextMessage(stream, mainCommand.AddBikeModel()); - SendMessageAndOnResponse(stream, mainCommand.addPanel("panelID"), "panelID", (message) => panelId = JSONParser.GetResponseUuid(message)); - //Console.WriteLine(ReadPrefMessage(stream)); - //command = mainCommand.AddModel("car", "data\\customModels\\TeslaRoadster.fbx"); - //WriteTextMessage(stream, command); - //Console.WriteLine(ReadPrefMessage(stream)); - - - //command = mainCommand.addPanel(); // WriteTextMessage(stream, command); // string response = ReadPrefMessage(stream); diff --git a/RH-Engine/ServerResponseReader.cs b/RH-Engine/ServerResponseReader.cs index 04d3312..13192a1 100644 --- a/RH-Engine/ServerResponseReader.cs +++ b/RH-Engine/ServerResponseReader.cs @@ -53,7 +53,7 @@ namespace RH_Engine byte[] lengthBytes = new byte[4]; int streamread = stream.Read(lengthBytes, 0, 4); - Console.WriteLine("read message.. " + streamread); + //Console.WriteLine("read message.. " + streamread); int length = BitConverter.ToInt32(lengthBytes); -- 2.47.2 From b6e4842cf8206b384777d66f9a2ce970ae117d15 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Fri, 25 Sep 2020 15:48:47 +0200 Subject: [PATCH 47/54] updated method to get id based on name --- RH-Engine/Command.cs | 6 +++--- RH-Engine/Program.cs | 19 ++++++++++--------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/RH-Engine/Command.cs b/RH-Engine/Command.cs index 20b4f67..9277c99 100644 --- a/RH-Engine/Command.cs +++ b/RH-Engine/Command.cs @@ -74,7 +74,6 @@ namespace RH_Engine dynamic payload = new { id = "scene/node/add", - serial = "needResponse", data = new { name = "newNode", @@ -373,11 +372,12 @@ namespace RH_Engine return JsonConvert.SerializeObject(Payload(payload)); } - public string GetSceneInfoCommand() + public string GetSceneInfoCommand(string serialToSend) { dynamic payload = new { - id = "scene/get" + id = "scene/get", + serial = serialToSend }; return JsonConvert.SerializeObject(Payload(payload)); diff --git a/RH-Engine/Program.cs b/RH-Engine/Program.cs index b2d4cbf..51e385b 100644 --- a/RH-Engine/Program.cs +++ b/RH-Engine/Program.cs @@ -164,6 +164,8 @@ namespace RH_Engine SendMessageAndOnResponse(stream, mainCommand.addPanel("panelID"), "panelID", (message) => panelId = JSONParser.GetResponseUuid(message)); + Console.WriteLine("id of head " + GetId(Command.STANDARD_HEAD,stream,mainCommand)); + //command = mainCommand.AddModel("car", "data\\customModels\\TeslaRoadster.fbx"); //WriteTextMessage(stream, command); @@ -216,13 +218,9 @@ namespace RH_Engine height[i] = improvedPerlin.GetValue(x / 10, x / 10, x * 100) + 1; x += 0.001f; } - - //=============================================================================================================TODO change WriteTextMessage(stream, createGraphics.TerrainCommand(new int[] { 256, 256 }, height)); - //Console.WriteLine(ReadPrefMessage(stream)); WriteTextMessage(stream, createGraphics.AddNodeCommand()); - //Console.WriteLine(ReadPrefMessage(stream)); } /// @@ -233,11 +231,14 @@ namespace RH_Engine /// all the children objects in the current scene public static JArray GetChildren(NetworkStream stream, Command createGraphics) { - WriteTextMessage(stream, createGraphics.GetSceneInfoCommand()); - //dynamic response = JsonConvert.DeserializeObject(ReadPrefMessage(stream)); - //return response.data.data.data.children; - //=============================================================================================================TODO change - return null; + JArray res = null; + SendMessageAndOnResponse(stream, createGraphics.GetSceneInfoCommand("getChildren"),"getChildren",(message) => + { + dynamic response = JsonConvert.DeserializeObject(message); + res = response.data.data.data.children; + }); + while (res == null) { } + return res; } /// -- 2.47.2 From a353d6839e9125334122b2f877fc121ece9c3907 Mon Sep 17 00:00:00 2001 From: shinichi Date: Fri, 25 Sep 2020 15:50:20 +0200 Subject: [PATCH 48/54] added some flair --- Server/Communication.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Server/Communication.cs b/Server/Communication.cs index 1eed0b0..0d354c4 100644 --- a/Server/Communication.cs +++ b/Server/Communication.cs @@ -20,6 +20,9 @@ namespace Server public void Start() { listener.Start(); + Console.WriteLine($"==========================================================================\n" + + $"\tstarted accepting clients at {DateTime.Now}\n" + + $"=========================================================================="); listener.BeginAcceptTcpClient(new AsyncCallback(OnConnect), null); } -- 2.47.2 From aa5f58e7521edd9f0e2c6e77b1c6f7869bda2510 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Fri, 25 Sep 2020 15:54:41 +0200 Subject: [PATCH 49/54] cleaned program.cs --- RH-Engine/Program.cs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/RH-Engine/Program.cs b/RH-Engine/Program.cs index 51e385b..512545d 100644 --- a/RH-Engine/Program.cs +++ b/RH-Engine/Program.cs @@ -263,12 +263,6 @@ namespace RH_Engine } - public static string getUUIDFromResponse(string response) - { - dynamic JSON = JsonConvert.DeserializeObject(response); - return JSON.data.data.data.uuid; - } - } /// -- 2.47.2 From 13d99eb1072cf3acd0fc0176cdb3374fdc3075b0 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Fri, 25 Sep 2020 15:56:26 +0200 Subject: [PATCH 50/54] cleanup files --- RH-Engine/Command.cs | 45 +++++++++---------------------- RH-Engine/JSONParser.cs | 14 +++------- RH-Engine/Program.cs | 42 +++++++++-------------------- RH-Engine/ServerResponseReader.cs | 7 ++--- 4 files changed, 33 insertions(+), 75 deletions(-) diff --git a/RH-Engine/Command.cs b/RH-Engine/Command.cs index 9277c99..5c4164d 100644 --- a/RH-Engine/Command.cs +++ b/RH-Engine/Command.cs @@ -1,14 +1,10 @@ using LibNoise.Primitive; using Newtonsoft.Json; using System; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Threading; namespace RH_Engine { - class Command + internal class Command { public const string STANDARD_HEAD = "Head"; public const string STANDARD_GROUND = "GroundPlane"; @@ -16,9 +12,7 @@ namespace RH_Engine public const string STANDARD_LEFTHAND = "LeftHand"; public const string STANDARD_RIGHTHAND = "RightHand"; - - - string tunnelID; + private string tunnelID; public Command(string tunnelID) { @@ -35,10 +29,10 @@ namespace RH_Engine size = sizeArray, heights = heightsArray } - }; return JsonConvert.SerializeObject(Payload(payload)); } + public string AddLayer(string uid, string texture) { dynamic payload = new @@ -56,6 +50,7 @@ namespace RH_Engine }; return JsonConvert.SerializeObject(Payload(payload)); } + public string UpdateTerrain() { dynamic payload = new @@ -63,7 +58,6 @@ namespace RH_Engine id = "scene/terrain/update", data = new { - } }; return JsonConvert.SerializeObject(Payload(payload)); @@ -91,19 +85,15 @@ namespace RH_Engine public string DeleteNode(string uuid) { - dynamic payload = new { id = "scene/node/delete", data = new { id = uuid, - } - }; return JsonConvert.SerializeObject(Payload(payload)); - } public string addPanel(string serialToSend) @@ -123,7 +113,6 @@ namespace RH_Engine resolution = new int[] { 512, 512 }, background = new int[] { 1, 0, 0, 0 }, castShadow = false - } } } @@ -210,20 +199,20 @@ namespace RH_Engine public string AddBikeModel() { - return AddModel("bike","addbike", "data\\NetworkEngine\\models\\bike\\bike.fbx"); + return AddModel("bike", "addbike", "data\\NetworkEngine\\models\\bike\\bike.fbx"); } - public string AddModel(string nodeName,string serial, string fileLocation) + public string AddModel(string nodeName, string serial, string fileLocation) { - return AddModel(nodeName,serial, fileLocation, null, new float[] { 0, 0, 0 }, 1, new float[] { 0, 0, 0 }); + return AddModel(nodeName, serial, fileLocation, null, new float[] { 0, 0, 0 }, 1, new float[] { 0, 0, 0 }); } - public string AddModel(string nodeName,string serial, string fileLocation, float[] positionVector, float scalar, float[] rotationVector) + public string AddModel(string nodeName, string serial, string fileLocation, float[] positionVector, float scalar, float[] rotationVector) { - return AddModel(nodeName,serial, fileLocation, null, positionVector, scalar, rotationVector); + return AddModel(nodeName, serial, fileLocation, null, positionVector, scalar, rotationVector); } - public string AddModel(string nodeName,string serialToSend, string fileLocation, string animationLocation, float[] positionVector, float scalar, float[] rotationVector) + public string AddModel(string nodeName, string serialToSend, string fileLocation, string animationLocation, float[] positionVector, float scalar, float[] rotationVector) { string namename = nodeName; bool animatedBool = false; @@ -246,7 +235,6 @@ namespace RH_Engine position = positionVector, scale = scalar, rotation = rotationVector - }, model = new { @@ -257,7 +245,6 @@ namespace RH_Engine }, } } - }; return JsonConvert.SerializeObject(Payload(payload)); } @@ -287,7 +274,6 @@ namespace RH_Engine return JsonConvert.SerializeObject(Payload(payload)); } - public string RouteCommand(string serialToSend) { ImprovedPerlin improvedPerlin = new ImprovedPerlin(4325, LibNoise.NoiseQuality.Best); @@ -345,7 +331,7 @@ namespace RH_Engine private int[] GetDir() { Random rng = new Random(); - int[] dir = {rng.Next(50), 0, rng.Next(50)}; + int[] dir = { rng.Next(50), 0, rng.Next(50) }; return dir; } @@ -401,7 +387,6 @@ namespace RH_Engine throw new Exception("The time must be between 0 and 24!"); } - dynamic payload = new { id = "scene/skybox/settime", @@ -409,13 +394,10 @@ namespace RH_Engine { time = timeToSet } - }; return JsonConvert.SerializeObject(Payload(payload)); - } - private object Payload(dynamic message) { return new @@ -428,8 +410,5 @@ namespace RH_Engine } }; } - - - } -} +} \ No newline at end of file diff --git a/RH-Engine/JSONParser.cs b/RH-Engine/JSONParser.cs index a4a5d26..8621aff 100644 --- a/RH-Engine/JSONParser.cs +++ b/RH-Engine/JSONParser.cs @@ -1,9 +1,5 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Net.Sockets; -using System.Text; -using Newtonsoft.Json; +using Newtonsoft.Json; +using System; namespace RH_Engine { @@ -27,14 +23,13 @@ namespace RH_Engine } return res; - } public static string GetSessionID(string msg, PC[] PCs) { dynamic jsonData = JsonConvert.DeserializeObject(msg); Newtonsoft.Json.Linq.JArray data = jsonData.data; - for (int i = data.Count-1; i >= 0; i--) + for (int i = data.Count - 1; i >= 0; i--) { dynamic d = data[i]; foreach (PC pc in PCs) @@ -60,7 +55,6 @@ namespace RH_Engine { dynamic d = JsonConvert.DeserializeObject(json); return d.id; - } public static string GetTunnelID(string json) @@ -99,4 +93,4 @@ namespace RH_Engine return null; } } -} +} \ No newline at end of file diff --git a/RH-Engine/Program.cs b/RH-Engine/Program.cs index 512545d..a2c1f57 100644 --- a/RH-Engine/Program.cs +++ b/RH-Engine/Program.cs @@ -3,19 +3,13 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; -using System.Globalization; -using System.IO; using System.Net.Sockets; -using System.Runtime.Intrinsics.X86; -using System.Security.Cryptography; -using System.Security.Cryptography.X509Certificates; using System.Text; -using System.Threading; namespace RH_Engine { - public delegate void HandleSerial(string message); + internal class Program { private static PC[] PCs = { @@ -36,10 +30,8 @@ namespace RH_Engine private static Dictionary serialResponses = new Dictionary(); - private static void Main(string[] args) { - TcpClient client = new TcpClient("145.48.6.10", 6666); CreateConnection(client.GetStream()); @@ -62,14 +54,14 @@ namespace RH_Engine /// the response message from the server public static void HandleResponse(string message) { - string id = JSONParser.GetID(message); // because the first messages don't have a serial, we need to check on the id if (id == "session/list") { - sessionId = JSONParser.GetSessionID(message,PCs); - } else if (id == "tunnel/create") + sessionId = JSONParser.GetSessionID(message, PCs); + } + else if (id == "tunnel/create") { tunnelId = JSONParser.GetTunnelID(message); if (tunnelId == null) @@ -85,7 +77,6 @@ namespace RH_Engine string serial = JSONParser.GetSerial(message); //Console.WriteLine("Got serial " + serial); if (serialResponses.ContainsKey(serial)) serialResponses[serial].Invoke(message); - } } @@ -98,7 +89,7 @@ namespace RH_Engine /// the code to be executed upon reveiving a reply from the server with the specified serial public static void SendMessageAndOnResponse(NetworkStream stream, string message, string serial, HandleSerial action) { - serialResponses.Add(serial,action); + serialResponses.Add(serial, action); WriteTextMessage(stream, message); } @@ -120,8 +111,6 @@ namespace RH_Engine Console.WriteLine("sent message " + message); } - - /// /// connects to the server and creates the tunnel /// @@ -134,7 +123,7 @@ namespace RH_Engine // wait until we have got a sessionId while (sessionId == string.Empty) { } - + string tunnelCreate = "{\"id\" : \"tunnel/create\", \"data\" : {\"session\" : \"" + sessionId + "\"}}"; WriteTextMessage(stream, tunnelCreate); @@ -154,17 +143,15 @@ namespace RH_Engine { Command mainCommand = new Command(tunnelID); - WriteTextMessage(stream, mainCommand.ResetScene()); SendMessageAndOnResponse(stream, mainCommand.RouteCommand("routeID"), "routeID", (message) => routeId = JSONParser.GetResponseUuid(message)); - //WriteTextMessage(stream, mainCommand.TerrainCommand(new int[] { 256, 256 }, null)); //string command; SendMessageAndOnResponse(stream, mainCommand.addPanel("panelID"), "panelID", (message) => panelId = JSONParser.GetResponseUuid(message)); - Console.WriteLine("id of head " + GetId(Command.STANDARD_HEAD,stream,mainCommand)); + Console.WriteLine("id of head " + GetId(Command.STANDARD_HEAD, stream, mainCommand)); //command = mainCommand.AddModel("car", "data\\customModels\\TeslaRoadster.fbx"); //WriteTextMessage(stream, command); @@ -182,7 +169,6 @@ namespace RH_Engine // Console.WriteLine("Color panel: " + ReadPrefMessage(stream)); // WriteTextMessage(stream, mainCommand.SwapPanel(uuidPanel)); // Console.WriteLine("Swap panel: " + ReadPrefMessage(stream)); - } /// @@ -205,7 +191,6 @@ namespace RH_Engine } Console.WriteLine("Could not find id of " + name); return null; - } public static void CreateTerrain(NetworkStream stream, Command createGraphics) @@ -232,11 +217,11 @@ namespace RH_Engine public static JArray GetChildren(NetworkStream stream, Command createGraphics) { JArray res = null; - SendMessageAndOnResponse(stream, createGraphics.GetSceneInfoCommand("getChildren"),"getChildren",(message) => - { - dynamic response = JsonConvert.DeserializeObject(message); - res = response.data.data.data.children; - }); + SendMessageAndOnResponse(stream, createGraphics.GetSceneInfoCommand("getChildren"), "getChildren", (message) => + { + dynamic response = JsonConvert.DeserializeObject(message); + res = response.data.data.data.children; + }); while (res == null) { } return res; } @@ -260,9 +245,7 @@ namespace RH_Engine } return res; - } - } /// @@ -275,6 +258,7 @@ namespace RH_Engine this.host = host; this.user = user; } + public string host { get; } public string user { get; } diff --git a/RH-Engine/ServerResponseReader.cs b/RH-Engine/ServerResponseReader.cs index 13192a1..85e7f00 100644 --- a/RH-Engine/ServerResponseReader.cs +++ b/RH-Engine/ServerResponseReader.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Net.Sockets; using System.Text; using System.Threading; @@ -7,12 +6,14 @@ using System.Threading; namespace RH_Engine { public delegate void OnResponse(string response); + class ServerResponseReader { public OnResponse callback { - get;set; + get; set; } + public NetworkStream Stream { get; } public ServerResponseReader(NetworkStream stream) @@ -73,4 +74,4 @@ namespace RH_Engine return Encoding.UTF8.GetString(buffer, 0, totalRead); } } -} +} \ No newline at end of file -- 2.47.2 From dc4d3c852b15f12b38b9b5ad65bfd2a751afe85c Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Fri, 25 Sep 2020 15:56:47 +0200 Subject: [PATCH 51/54] removed internal --- RH-Engine/Command.cs | 2 +- RH-Engine/Program.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/RH-Engine/Command.cs b/RH-Engine/Command.cs index 5c4164d..e6e5dd3 100644 --- a/RH-Engine/Command.cs +++ b/RH-Engine/Command.cs @@ -4,7 +4,7 @@ using System; namespace RH_Engine { - internal class Command + class Command { public const string STANDARD_HEAD = "Head"; public const string STANDARD_GROUND = "GroundPlane"; diff --git a/RH-Engine/Program.cs b/RH-Engine/Program.cs index a2c1f57..17deddc 100644 --- a/RH-Engine/Program.cs +++ b/RH-Engine/Program.cs @@ -10,7 +10,7 @@ namespace RH_Engine { public delegate void HandleSerial(string message); - internal class Program + class Program { private static PC[] PCs = { //new PC("DESKTOP-M2CIH87", "Fabian"), -- 2.47.2 From 68a43fb9300ca160fc95f5112ba1ef7afafb06d6 Mon Sep 17 00:00:00 2001 From: fabjuuuh Date: Fri, 25 Sep 2020 16:38:16 +0200 Subject: [PATCH 52/54] Save Raw data --- Client/Program.cs | 9 +++++---- Server/Client.cs | 15 +++++++++++++-- Server/SaveData.cs | 12 +++++++++--- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/Client/Program.cs b/Client/Program.cs index e98dfc7..7b722f5 100644 --- a/Client/Program.cs +++ b/Client/Program.cs @@ -1,5 +1,6 @@ using System; using Hardware; +using Hardware.Simulators; namespace Client { @@ -18,13 +19,13 @@ namespace Client { } - BLEHandler bLEHandler = new BLEHandler(client); + //BLEHandler bLEHandler = new BLEHandler(client); - bLEHandler.Connect(); + //bLEHandler.Connect(); - //BikeSimulator bikeSimulator = new BikeSimulator(client); + BikeSimulator bikeSimulator = new BikeSimulator(client); - //bikeSimulator.StartSimulation(); + bikeSimulator.StartSimulation(); while (true) { diff --git a/Server/Client.cs b/Server/Client.cs index 43d4fd1..5e385be 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -18,6 +18,7 @@ namespace Server private int totalBufferReceived = 0; private SaveData saveData; private string username = null; + private DateTime sessionStart; @@ -25,7 +26,7 @@ namespace Server public Client(Communication communication, TcpClient tcpClient) { - this.saveData = new SaveData(Directory.GetCurrentDirectory() + $"/test"); + this.sessionStart = DateTime.Now; this.communication = communication; this.tcpClient = tcpClient; this.stream = this.tcpClient.GetStream(); @@ -100,9 +101,11 @@ namespace Server { if (verifyLogin(username, password)) { + Console.WriteLine("Log in"); this.username = username; byte[] response = DataParser.getLoginResponse("OK"); stream.BeginWrite(response, 0, response.Length, new AsyncCallback(OnWrite), null); + this.saveData = new SaveData(Directory.GetCurrentDirectory() + "/" + username, sessionStart.ToString("yyyy-MM-dd HH-mm-ss")); } else { @@ -128,7 +131,7 @@ namespace Server else if (DataParser.isRawData(message)) { Console.WriteLine(BitConverter.ToString(message)); - saveData.WriteDataRAW(Encoding.ASCII.GetString(message)); + saveData.WriteDataRAW(ByteArrayToString(message)); } @@ -138,5 +141,13 @@ namespace Server { return username == password; } + + public static string ByteArrayToString(byte[] ba) + { + StringBuilder hex = new StringBuilder(ba.Length * 2); + foreach (byte b in ba) + hex.AppendFormat("{0:x2}", b); + return hex.ToString(); + } } } diff --git a/Server/SaveData.cs b/Server/SaveData.cs index 640a862..0286bde 100644 --- a/Server/SaveData.cs +++ b/Server/SaveData.cs @@ -8,18 +8,24 @@ namespace Server class SaveData { private string path; - public SaveData(string path) + private string filename; + public SaveData(string path, string filename) { this.path = path; + this.filename = filename; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } } + /// + /// Every line is a new data entry + /// + public void WriteDataJSON(string data) { - using (StreamWriter sw = File.AppendText(this.path + "/dataJSON.txt")) + using (StreamWriter sw = File.AppendText(this.path + "/json"+filename+".txt")) { sw.WriteLine(data); } @@ -27,7 +33,7 @@ namespace Server public void WriteDataRAW(string data) { - using (StreamWriter sw = File.AppendText(this.path + "/dataRAW.txt")) + using (StreamWriter sw = File.AppendText(this.path + "/raw" + filename + ".txt")) { sw.WriteLine(data); } -- 2.47.2 From 78bb7f6a6c0e7b5776e60e4ff1816fde5ac40a01 Mon Sep 17 00:00:00 2001 From: shinichi Date: Fri, 25 Sep 2020 16:38:22 +0200 Subject: [PATCH 53/54] added possibility for multiple DataReceivers --- ProftaakRH/BLEHandler.cs | 24 ++++++++++++++++++++---- ProftaakRH/BikeSimulator.cs | 23 ++++++++++++++++++----- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/ProftaakRH/BLEHandler.cs b/ProftaakRH/BLEHandler.cs index 238bb4c..6baf843 100644 --- a/ProftaakRH/BLEHandler.cs +++ b/ProftaakRH/BLEHandler.cs @@ -13,7 +13,7 @@ namespace Hardware /// public class BLEHandler { - IDataReceiver dataReceiver; + List dataReceivers; private BLE bleBike; private BLE bleHeart; public bool Running { get; set; } @@ -24,7 +24,17 @@ namespace Hardware /// the dataconverter object public BLEHandler(IDataReceiver dataReceiver) { - this.dataReceiver = dataReceiver; + this.dataReceivers = new List { dataReceiver }; + } + + public BLEHandler(List dataReceivers) + { + this.dataReceivers = dataReceivers; + } + + public void addDataReceiver(IDataReceiver dataReceiver) + { + this.dataReceivers.Add(dataReceiver); } /// @@ -125,11 +135,17 @@ namespace Hardware { byte[] payload = new byte[8]; Array.Copy(e.Data, 4, payload, 0, 8); - this.dataReceiver.Bike(payload); + foreach (IDataReceiver dataReceiver in this.dataReceivers) + { + dataReceiver.Bike(payload); + } } else if (e.ServiceName == "00002a37-0000-1000-8000-00805f9b34fb") { - this.dataReceiver.BPM(e.Data); + foreach (IDataReceiver dataReceiver in this.dataReceivers) + { + dataReceiver.BPM(e.Data); + } } else { diff --git a/ProftaakRH/BikeSimulator.cs b/ProftaakRH/BikeSimulator.cs index 304a00d..daea355 100644 --- a/ProftaakRH/BikeSimulator.cs +++ b/ProftaakRH/BikeSimulator.cs @@ -12,7 +12,7 @@ namespace Hardware.Simulators { public class BikeSimulator : IHandler { - IDataReceiver dataReceiver; + List dataReceivers; private int elapsedTime = 0; private int eventCounter = 0; private double distanceTraveled = 0; @@ -32,7 +32,17 @@ namespace Hardware.Simulators public BikeSimulator(IDataReceiver dataReceiver) { - this.dataReceiver = dataReceiver; + this.dataReceivers = new List { dataReceiver }; + } + + public BikeSimulator(List dataReceivers) + { + this.dataReceivers = dataReceivers; + } + + public void addDataReceiver(IDataReceiver dataReceiver) + { + this.dataReceivers.Add(dataReceiver); } public void StartSimulation() @@ -50,9 +60,12 @@ namespace Hardware.Simulators CalculateVariables(improvedPerlin.GetValue(x) + 1); //Simulate sending data - dataReceiver.Bike(GenerateBike0x19()); - dataReceiver.Bike(GenerateBike0x10()); - dataReceiver.BPM(GenerateHeart()); + foreach (IDataReceiver dataReceiver in this.dataReceivers) + { + dataReceiver.Bike(GenerateBike0x19()); + dataReceiver.Bike(GenerateBike0x10()); + dataReceiver.BPM(GenerateHeart()); + } Thread.Sleep(1000); -- 2.47.2 From 38886ca7c3d9281645217fe9f700f8bc4c5e8638 Mon Sep 17 00:00:00 2001 From: Logophilist Date: Fri, 25 Sep 2020 16:48:05 +0200 Subject: [PATCH 54/54] Bike follows route --- RH-Engine/Command.cs | 42 +++++++++++++++++++++++++++++++++++++----- RH-Engine/Program.cs | 13 +++++++++++-- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/RH-Engine/Command.cs b/RH-Engine/Command.cs index e6e5dd3..3ef3bc1 100644 --- a/RH-Engine/Command.cs +++ b/RH-Engine/Command.cs @@ -96,7 +96,7 @@ namespace RH_Engine return JsonConvert.SerializeObject(Payload(payload)); } - public string addPanel(string serialToSend) + public string addPanel(string serialToSend, string uuidBike) { dynamic payload = new { @@ -105,6 +105,7 @@ namespace RH_Engine data = new { name = "dashboard", + parent = uuidBike, components = new { panel = new @@ -197,9 +198,9 @@ namespace RH_Engine return JsonConvert.SerializeObject(Payload(payload)); } - public string AddBikeModel() + public string AddBikeModel(string serial) { - return AddModel("bike", "addbike", "data\\NetworkEngine\\models\\bike\\bike.fbx"); + return AddModel("bike", serial, "data\\NetworkEngine\\models\\bike\\bike.fbx"); } public string AddModel(string nodeName, string serial, string fileLocation) @@ -335,9 +336,40 @@ namespace RH_Engine return dir; } - public string FollowRouteCommand() + public string RouteFollow(string routeID, string nodeID, float speedValue) { - return ""; + return RouteFollow(routeID, nodeID, speedValue, new float[] { 0, 0, 0 }); + } + + public string RouteFollow(string routeID, string nodeID, float speedValue, float[] rotateOffsetVector, float[] positionOffsetVector) + { + return RouteFollow(routeID, nodeID, speedValue, 0, "XYZ", 1, true, rotateOffsetVector, positionOffsetVector); + } + + public string RouteFollow(string routeID, string nodeID, float speedValue, float[] positionOffsetVector) + { + return RouteFollow(routeID, nodeID, speedValue, 0, "XYZ", 1, true, new float[] { 0, 0, 0 }, positionOffsetVector); + } + private string RouteFollow(string routeID, string nodeID, float speedValue, float offsetValue, string rotateValue, float smoothingValue, bool followHeightValue, float[] rotateOffsetVector, float[] positionOffsetVector) + { + dynamic payload = new + { + id = "route/follow", + data = new + { + route = routeID, + node = nodeID, + speed = speedValue, + offset = offsetValue, + rotate = rotateValue, + smoothing = smoothingValue, + followHeight = followHeightValue, + rotateOffset = rotateOffsetVector, + positionOffset = positionOffsetVector + + } + }; + return JsonConvert.SerializeObject(Payload(payload)); } public string RoadCommand(string uuid_route) diff --git a/RH-Engine/Program.cs b/RH-Engine/Program.cs index 17deddc..63c86fb 100644 --- a/RH-Engine/Program.cs +++ b/RH-Engine/Program.cs @@ -1,4 +1,4 @@ -using LibNoise.Primitive; +using LibNoise.Primitive; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; @@ -27,6 +27,7 @@ namespace RH_Engine private static string tunnelId = string.Empty; private static string routeId = string.Empty; private static string panelId = string.Empty; + private static string bikeId = string.Empty; private static Dictionary serialResponses = new Dictionary(); @@ -149,7 +150,15 @@ namespace RH_Engine //WriteTextMessage(stream, mainCommand.TerrainCommand(new int[] { 256, 256 }, null)); //string command; - SendMessageAndOnResponse(stream, mainCommand.addPanel("panelID"), "panelID", (message) => panelId = JSONParser.GetResponseUuid(message)); + SendMessageAndOnResponse(stream, mainCommand.AddBikeModel("bikeID"), "bikeID", (message) => bikeId = JSONParser.GetResponseUuid(message)); + + SendMessageAndOnResponse(stream, mainCommand.addPanel("panelID", bikeId), "panelID", + (message) => + { + panelId = JSONParser.GetResponseUuid(message); + while (bikeId == string.Empty) { } + WriteTextMessage(stream, mainCommand.RouteFollow(routeId, bikeId, 5, new float[] { 0, -(float)Math.PI / 2f, 0 }, new float[] { 0, 0, 0 })); + }); Console.WriteLine("id of head " + GetId(Command.STANDARD_HEAD, stream, mainCommand)); -- 2.47.2