From 39b935ecbc5f59b132010c379e10570e10365dee Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Fri, 9 Oct 2020 12:02:23 +0200 Subject: [PATCH] o p t i m i z e --- Client/Client.cs | 14 +++++++------- Client/EngineConnection.cs | 15 ++++++++++++--- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index 5696071..b24de1e 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -123,7 +123,7 @@ namespace Client { Console.WriteLine("Username and password correct!"); this.connected = true; - //initEngine(); + initEngine(); } else { @@ -212,7 +212,7 @@ namespace Client if (engineConnection.Connected && engineConnection.FollowingRoute) { engineConnection.BikeBPM = bytes[1]; - engineConnection.UpdateInfoPanel(); + } @@ -225,6 +225,7 @@ namespace Client /// the message public void Bike(byte[] bytes) { + bool canSendToEngine = engineConnection.Connected && engineConnection.FollowingRoute; if (!sessionRunning) { return; @@ -237,15 +238,14 @@ namespace Client switch (bytes[0]) { case 0x10: - - engineConnection.BikeSpeed = (bytes[4] | (bytes[5] << 8)) * 0.01f; + + if (canSendToEngine) engineConnection.BikeSpeed = (bytes[4] | (bytes[5] << 8)) * 0.01f; break; case 0x19: - engineConnection.BikePower = (bytes[5]) | (bytes[6] & 0b00001111) << 8; + if (canSendToEngine) engineConnection.BikePower = (bytes[5]) | (bytes[6] & 0b00001111) << 8; break; } - if (engineConnection.Connected && engineConnection.FollowingRoute) - engineConnection.UpdateInfoPanel(); + this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); } diff --git a/Client/EngineConnection.cs b/Client/EngineConnection.cs index 9d0d4e5..fe3224e 100644 --- a/Client/EngineConnection.cs +++ b/Client/EngineConnection.cs @@ -14,6 +14,7 @@ namespace Client { private static EngineConnection instance = null; private static readonly object padlock = new object(); + private static System.Timers.Timer updateTimer; public HandleNoTunnelId OnNoTunnelId; public OnSuccessfullConnection OnSuccessFullConnection; @@ -57,6 +58,15 @@ namespace Client BikePower = 0; BikeBPM = 0; BikeResistance = 50; + updateTimer = new System.Timers.Timer(1000); + updateTimer.Elapsed += UpdateTimer_Elapsed; + updateTimer.AutoReset = true; + updateTimer.Enabled = false; + } + + private void UpdateTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) + { + UpdateInfoPanel(); } /// @@ -164,6 +174,7 @@ namespace Client public void initScene() { + Write("initializing scene..."); mainCommand = new Command(tunnelId); @@ -179,9 +190,6 @@ namespace Client string headId = JSONParser.GetIdSceneInfoChild(message, "Head"); string handLeftId = JSONParser.GetIdSceneInfoChild(message, "LeftHand"); string handRightId = JSONParser.GetIdSceneInfoChild(message, "RightHand"); - - //Force(stream, mainCommand.DeleteNode(handLeftId, "deleteHandL"), "deleteHandL", (message) => Console.WriteLine("Left hand deleted")); - //Force(stream, mainCommand.DeleteNode(handRightId, "deleteHandR"), "deleteHandR", (message) => Console.WriteLine("Right hand deleted")); }); // add the route and set the route id SendMessageAndOnResponse(mainCommand.RouteCommand("routeID"), "routeID", (message) => routeId = JSONParser.GetResponseUuid(message)); @@ -204,6 +212,7 @@ namespace Client WriteTextMessage(mainCommand.ColorPanel(panelId)); UpdateInfoPanel(); + updateTimer.Enabled = true; while (cameraId == string.Empty) { } SetFollowSpeed(5.0f);