static void Main(string[] args) { Console.WriteLine("The ProxyControlClient Console\n\nType 'command;payload'\nConnecting to proxy object ..."); ProxyControlAPI.ProxyControlClient client = new ProxyControlAPI.ProxyControlClient(); if (!client.connectToProxy(proxyIP, proxyPort)) { Console.WriteLine("Could not connect to the proxy! Terminating."); } else { Console.WriteLine("... connected!"); } string input = ""; while ((input = Console.ReadLine()) != "#") { if (input == "reconnect") { Console.WriteLine("Reconnecting ... "); if (!client.connectToProxy(proxyIP, proxyPort)) { Console.WriteLine("Could not connect to the proxy!"); continue; } else { Console.WriteLine("... connected!"); } } else if (input == "C") { ConnectionCheckCallback callback = delegate(bool isAlive) { Console.WriteLine("IS ALIVE ! " + isAlive); }; client.checkConnection(callback); } string[] split = input.Split(';'); if (split.Length == 2) { try { byte command = Byte.Parse(split[0]); float payload = float.Parse(split[1]); if (command == (byte)COMMANDS.CHECK_CURRENT_POSITION) { CurrentPositionCallback callback = delegate(float pos) { Console.WriteLine("Current Position: " + pos); }; client.sendCurrentPositionRequest(callback); } else if (command == (byte)COMMANDS.SEND_NEW_TARGET_POSITION) { NewTargetCallback callback = delegate(float pos) { Console.WriteLine("NewTarget Answer from server: " + System.Text.Encoding.UTF8.GetString(BitConverter.GetBytes(pos))); }; client.sendNewTargetRequest(callback, payload); } else if (command == (byte)COMMANDS.SEND_NEW_SPEED) { NewSpeedCallback callback = delegate(float pos) { Console.WriteLine("NewSpeed Answer from server: " + System.Text.Encoding.UTF8.GetString(BitConverter.GetBytes(pos))); }; client.sendNewSpeedRequest(callback, (int)payload); } else if (command == (byte)COMMANDS.CHECK_IS_TARGET_REACHED) { IsTargetReachedCallback callback = delegate(bool isReached) { Console.WriteLine(isReached ? "Target reached!" : "Target not yet reached!"); }; client.sendIsTargetReachedRequest(callback); } else if (command == (byte)COMMANDS.CHECK_CURRENT_SPEED) { CurrentSpeedCallback callback = delegate(int speed) { Console.WriteLine("Current Speed: " + speed); }; client.sendCurrentSpeedRequest(callback); } else if (command == (byte)COMMANDS.RECALIBRATE) { RecalibrateCallback callback = delegate(float pos) { Console.WriteLine("Recalibrate Answer from server: " + System.Text.Encoding.UTF8.GetString(BitConverter.GetBytes(pos))); }; client.sendRecalibrationRequest(callback); } else if (command == (byte)COMMANDS.CHECK_EXPECTED_TIME) { ExpectedTimeCallback callback = delegate(long time) { Console.WriteLine("Expected Time Estimation from server: " + System.Text.Encoding.UTF8.GetString(BitConverter.GetBytes(time))); }; client.sendExpectedTimeRequest(callback, payload); } else if (command == (byte)COMMANDS.SEND_SAVE_POWER) { SavePowerCallback callback = delegate(float p) { Console.WriteLine("Save Power ACK from server: " + System.Text.Encoding.UTF8.GetString(BitConverter.GetBytes(p))); }; client.sendSavePowerRequest(callback); } else if (command == (byte)COMMANDS.STEPPING_MODE) { SteppingModeCallback callback = delegate(float pos) { Console.WriteLine("SteppingMode Answer from server: " + System.Text.Encoding.UTF8.GetString(BitConverter.GetBytes(pos))); }; client.sendNewSteppingModeRequest(callback, (int)payload); } else if (command == (byte)COMMANDS.VERSION_INFO) { VersionInfoCallback callback = delegate(float version) { Console.WriteLine("VersionInfo Answer from server: " + version); }; client.sendVersionInfoRequest(callback); } else { Console.WriteLine("Unknown command input! Try again!"); } } catch (Exception e) { Console.WriteLine("Exception caught! Try again!"); Console.WriteLine(e.Message); } } client.receiveAndHandleCallbacks(); } Console.WriteLine("Disconnecting from server..."); client.disconnect(); Console.ReadLine(); }
// Update is called once per frame void Update() { if (proxy != null && connected) { proxy.receiveAndHandleCallbacks(); } if (Input.GetKeyDown(KeyCode.C)) { connect(); } if (Input.GetKeyDown(KeyCode.LeftArrow) && connected) { ProxyControlAPI.ProxyControlClient.NewSpeedCallback cb = delegate(float payload) { Debug.Log("SET SPEED TO " + speed1 + " - " + payload); }; proxy.sendNewSpeedRequest(cb, speed1); } if (Input.GetKeyDown(KeyCode.RightArrow) && connected) { ProxyControlAPI.ProxyControlClient.NewSpeedCallback cb = delegate(float payload) { Debug.Log("SET SPEED TO " + speed2 + " - " + payload); }; proxy.sendNewSpeedRequest(cb, speed2); } if (Input.GetKeyDown(KeyCode.UpArrow) && connected) { ProxyControlAPI.ProxyControlClient.NewTargetCallback cb = delegate(float payload) { Debug.Log("SET TARGET TO " + 1 + " - " + payload); }; proxy.sendNewTargetRequest(cb, 1); } if (Input.GetKeyDown(KeyCode.Insert) && connected) { ProxyControlAPI.ProxyControlClient.NewTargetCallback cb = delegate(float payload) { Debug.Log("SET TARGET TO " + .5f + " - " + payload); }; proxy.sendNewTargetRequest(cb, .5f); } if (Input.GetKeyDown(KeyCode.DownArrow) && connected) { ProxyControlAPI.ProxyControlClient.NewTargetCallback cb = delegate(float payload) { Debug.Log("SET TARGET TO " + 0 + " - " + payload); }; proxy.sendNewTargetRequest(cb, 0); } if (Input.GetKeyDown(KeyCode.R)) { ProxyControlAPI.ProxyControlClient.ReconnectionAttemptCallback cb = delegate(bool success) { Debug.Log("RECONNECT " + success); }; proxy.reconnectToProxyAsync(ip, port, cb); } if (Input.GetKeyDown(KeyCode.D)) { ProxyControlAPI.ProxyControlClient.DisconnectionCallback cb = delegate() { Debug.Log("DISCONNECTED"); }; proxy.disconnectAsync(cb); } if (Input.GetKeyDown(KeyCode.K)) { ProxyControlAPI.ProxyControlClient.RecalibrateCallback cb = delegate(float payload) { Debug.Log("RECALIBRATION " + payload); }; proxy.sendRecalibrationRequest(cb); } if (Input.GetKeyDown(KeyCode.P)) { ProxyControlAPI.ProxyControlClient.SavePowerCallback cb = delegate(float payload) { Debug.Log("SAVE POWER: " + payload); }; proxy.sendSavePowerRequest(cb); } if (Input.GetKeyDown(KeyCode.X)) { //IF CONNECTION PROBLEM: COMPLETE RESET Debug.LogWarning("DOING A COMPLETE CONNECTION RESET!"); this.proxy = new ProxyControlClient(); Start(); Debug.LogWarning("COMPLETE CONNECTION RESET FINISHED!"); } }