private void SetFGVarValue(string varName, double value) { if (!stop) { try { // Lock the simulator to prevent other threads contacting it at the same time. socketMutex.WaitOne(); // Write the new value to the simulator. telnetClient.Write("set " + varName + " " + value.ToString() + "\n"); socketMutex.ReleaseMutex(); if (this.vars.ContainsKey(varName)) { // Recieve the accepted simulator value (in case the value we sent is out of bound) this.vars[varName].VarValue = HandleSimulatorReturn(varName); } else { telnetClient.Read(); } } catch (Exception e) { NotifyError(ErrorMessages.errorsEnum.Other, e.Message); } } }
private Task LoopProcessFrontEnd() { var stringBuilder = new StringBuilder(); return(_frontend.StartReading(text => { if (text.Contains(BACKSPACE) && stringBuilder.Length > 0) { stringBuilder.Remove(stringBuilder.Length - 1, 1); } else { stringBuilder.Append(text); } Console.WriteLine($"frontend] {text}"); //Console.WriteLine($"frontend input] {input}"); if (text.Contains("\n")) { _backend.Write(stringBuilder.ToString()); stringBuilder.Clear(); } })); }
//a method to execute the string commands. public void Execute(string command) { if (connectionClient) { tc.Write(command); } }
public ActionResult Post([FromBody] Command value) { try { client.Write(value); return(Ok()); } catch (Exception e) { return(StatusCode(421)); } }
public void start() { new Thread(delegate() { while (!stop) { telnetClient.Write("get left sonar"); Rudder = Double.Parse(telnetClient.Read()); // the same for the other sensors properties Thread.Sleep(250);// read the data in 4Hz } }).Start(); }
// Write a command to the server and read its response. public string WriteAndRead(string command) { // Prevent both threads from performing the method at the same time. lock (syncLock) { string message; client.Write(command); // Measure the server response time. stopWatch.Restart(); message = client.Read(); stopWatch.Reset(); return(message); } }
public string Write(string command) { string str = null; if (connected) { str = client.Write(command); if (str == "ERR") { this.Message = "Server returned 'ERR'"; } } return(str); }
// We maintain a queue which collects the set commands, and send them to the server in the proper order private void StartWriting() { new Thread(delegate() { while (true) { // sending the data until we disconnected or the queue is empty if (String.Equals(tc.IsConnected, "Connected") && this.writeQueue.Count != 0) { string writeCommand = writeQueue.Peek(); writeQueue.Dequeue(); Console.WriteLine(writeCommand); tc.Write(writeCommand); tc.Read(); } else if (String.Equals(tc.IsConnected, "Disconnected") && this.writeQueue.Count != 0) { writeQueue.Clear(); } } }).Start(); }
// Send a command (of get or set) to the simulator. private string SendCommand(string command, string currVal) { string returnValWrite = telnetClient.Write(command); if (returnValWrite == "Status: Server has disconnected") { ConnectionStatus = returnValWrite; return(currVal); } string returnValRead = telnetClient.Read(); if (returnValRead == "Status: Server timeout") { if (connectionStatus == "Status: Connected to server") { ConnectionStatus = returnValRead; } return(currVal); } return(returnValRead); }
// function running a thread that asks for information about the plane's location 4 times a second public void StartReadingFlightData() { new Thread(delegate() { while (true) { // getting the plane's current latitude tc.Write("get /position/latitude-deg \n"); double recivedLatitude; string serverInput = tc.Read(); bool result = double.TryParse(serverInput, out recivedLatitude); //checking if the recieved input can be parsed to double if (result) { //checking if recieved value is in valid range if (((recivedLatitude <= 90) && (recivedLatitude >= -90)) && (Math.Abs(Convert.ToDouble(recivedLatitude) - Math.Abs(Convert.ToDouble(oldLatitude))) < 2)) { Latitude = serverInput; oldLatitude = latitude; LatitudeError = ""; } // if not, showing an appropriate message else { latitude = oldLatitude; LatitudeError = "Bad latitude recieved, showing last correct atitude"; } } // if not, writing to console else { Console.WriteLine("Map model couldn't parse Latitude from server"); } // getting the plane's current longitude double recievedLongitude; tc.Write("get /position/longitude-deg \n"); serverInput = tc.Read(); result = double.TryParse(serverInput, out recievedLongitude); //checking if the recieved input can be parsed to double if (result) { //checking if recieved value is in valid range if (((recievedLongitude <= 180) && (recievedLongitude >= -180)) && (Math.Abs(Convert.ToDouble(recievedLongitude)) - (Math.Abs(Convert.ToDouble(oldLongtitude))) < 2)) { Longitude = serverInput; oldLongtitude = Longitude; LongitudeError = ""; } // if not, showing an appropriate message else { Longitude = oldLongtitude; LongitudeError = "Bad longitude recieved, showing last correct longitude"; } } // if not, writing to console else { Console.WriteLine("Map model couldn't parse Longitude from server"); } //bringing the plane back to starting position, if the connection has terminated and the plane's location values were not set to default yet if (String.Equals(tc.IsConnected, "Disconnected") && (this.Latitude != System.Configuration.ConfigurationManager.AppSettings["startLatitude"] || this.Longitude != System.Configuration.ConfigurationManager.AppSettings["startLongitude"])) { Latitude = System.Configuration.ConfigurationManager.AppSettings["startLatitude"]; Longitude = System.Configuration.ConfigurationManager.AppSettings["startLongitude"]; oldLatitude = System.Configuration.ConfigurationManager.AppSettings["startLatitude"]; oldLongtitude = System.Configuration.ConfigurationManager.AppSettings["startLongitude"]; latitudeError = ""; LongitudeError = ""; } FlightData = Latitude + "," + Longitude; Thread.Sleep(250); } }).Start(); }
public void start() { this.thread = new Thread(delegate() { String msg; while (!stop) { try { if (timeout) { msg = telnetClient.Read(); timeout = false; } // 1 telnetClient.Write("get /instrumentation/heading-indicator/indicated-heading-deg\n"); msg = telnetClient.Read(); if (!msg.Contains("ERR")) { IndicatedHeadingDeg = Double.Parse(msg); } // 2 telnetClient.Write("get /instrumentation/gps/indicated-vertical-speed\n"); msg = telnetClient.Read(); if (!msg.Contains("ERR")) { GpsIndicatedVerticalSpeed = Double.Parse(msg); } // 3 telnetClient.Write("get /instrumentation/gps/indicated-ground-speed-kt\n"); msg = telnetClient.Read(); if (!msg.Contains("ERR")) { GpsIndicatedGroundSpeedKt = Double.Parse(msg); } // 4 telnetClient.Write("get /instrumentation/airspeed-indicator/indicated-speed-kt\n"); msg = telnetClient.Read(); if (!msg.Contains("ERR")) { AirspeedIndicatorIndicatedSpeedKt = Double.Parse(msg); } // 5 telnetClient.Write("get /instrumentation/gps/indicated-altitude-ft\n"); msg = telnetClient.Read(); if (!msg.Contains("ERR")) { GpsIndicatedAltitudeFt = Double.Parse(msg); } // 6 telnetClient.Write("get /instrumentation/attitude-indicator/internal-roll-deg\n"); msg = telnetClient.Read(); if (!msg.Contains("ERR")) { AttitudeIndicatorInternalRollDeg = Double.Parse(msg); } // 7 telnetClient.Write("get /instrumentation/attitude-indicator/internal-pitch-deg\n"); msg = telnetClient.Read(); if (!msg.Contains("ERR")) { AttitudeIndicatorInternalPitchDeg = Double.Parse(msg); } // 8 telnetClient.Write("get /instrumentation/altimeter/indicated-altitude-ft\n"); msg = telnetClient.Read(); if (!msg.Contains("ERR")) { AltimeterIndicatedAltitudeFt = Double.Parse(msg); } // Longitude. telnetClient.Write("get /position/longitude-deg\n"); msg = telnetClient.Read(); if (!msg.Contains("ERR")) { Longitude = Double.Parse(msg); } // Latitude. telnetClient.Write("get /position/latitude-deg\n"); msg = telnetClient.Read(); if (!msg.Contains("ERR")) { Latitude = Double.Parse(msg); } // Set the variables in the queue. while (this.update.Count != 0) { string s = "set " + update.Dequeue() + "\n"; telnetClient.Write(s); telnetClient.Read(); } // The same for the other sensors properties. Thread.Sleep(250);// read the data in 4Hz } catch (IOException e) { if (e.ToString().Contains("A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.")) { // Problem with reading values. ReadError = true; timeout = true; } else { stop = true; Connecting = "disconnected"; ServerError = true; update.Clear(); } } catch (FormatException) { // Error with values. InValidError = true; } catch (Exception) { // Problem with connecting to the server. update.Clear(); stop = true; if (!telnetClient.IsConnect()) { Connecting = "disconnected"; } ServerError = true; } } }); thread.Start(); }
// function running a thread that asks for information about the plane's different values, 4 times a second public void StartReadingFlightData() { new Thread(delegate() { while (true) { tc.Write("get /instrumentation/gps/indicated-vertical-speed \n"); double i; string serverInput = tc.Read(); bool result = double.TryParse(serverInput, out i); if (result) { VerticalSpeed = serverInput; VerticalSpeedColor = "Green"; } else { VerticalSpeedColor = "Red"; } tc.Write("get /instrumentation/gps/indicated-ground-speed-kt \n"); serverInput = tc.Read(); result = double.TryParse(serverInput, out i); if (result) { GroundSpeed = serverInput; GroundSpeedColor = "Green"; } else { GroundSpeedColor = "Red"; } tc.Write("get /instrumentation/heading-indicator/indicated-heading-deg \n"); serverInput = tc.Read(); result = double.TryParse(serverInput, out i); if (result) { Heading = serverInput; HeadingColor = "Green"; } else { HeadingColor = "Red"; } tc.Write("get /instrumentation/altimeter/indicated-altitude-ft \n"); serverInput = tc.Read(); result = double.TryParse(serverInput, out i); if (result) { Altimeter = serverInput; AltimeterColor = "Green"; } else { AltimeterColor = "Red"; } tc.Write("get /instrumentation/attitude-indicator/internal-pitch-deg \n"); serverInput = tc.Read(); result = double.TryParse(serverInput, out i); if (result) { Pitch = serverInput; PitchColor = "Green"; } else { PitchColor = "Red"; } tc.Write("get /instrumentation/attitude-indicator/internal-roll-deg \n"); serverInput = tc.Read(); result = double.TryParse(serverInput, out i); if (result) { Roll = serverInput; RollColor = "Green"; } else { RollColor = "Red"; } tc.Write("get /instrumentation/gps/indicated-altitude-ft \n"); serverInput = tc.Read(); result = double.TryParse(serverInput, out i); if (result) { Altitude = serverInput; AltitudeColor = "Green"; } else { AltitudeColor = "Red"; } tc.Write("get /instrumentation/airspeed-indicator/indicated-speed-kt \n"); serverInput = tc.Read(); result = double.TryParse(serverInput, out i); if (result) { AirSpeed = serverInput; AirSpeedColor = "Green"; } else { AirSpeedColor = "Red"; } Thread.Sleep(250); } }).Start(); }
//Starting sending and receiving data with server. public void Start() { //Send and receive data in new threads. new Thread(delegate() { //Set commands StartClient(); try { while (Connected) { mtx.WaitOne(); client.Write("get /instrumentation/heading-indicator/indicated-heading-deg"); Heading = (client.Read()); client.Write("get /instrumentation/gps/indicated-vertical-speed"); VerticalSpeed = (client.Read()); client.Write("get /instrumentation/gps/indicated-ground-speed-kt"); GroundSpeed = (client.Read()); client.Write("get /instrumentation/airspeed-indicator/indicated-speed-kt"); AirSpeed = (client.Read()); client.Write("get /instrumentation/gps/indicated-altitude-ft"); GPSAlt = (client.Read()); client.Write("get /instrumentation/attitude-indicator/internal-roll-deg"); Roll = (client.Read()); client.Write("get /instrumentation/attitude-indicator/internal-pitch-deg"); Pitch = (client.Read()); client.Write("get /instrumentation/altimeter/indicated-altitude-ft"); AltimeterAlt = (client.Read()); client.Write("get /position/latitude-deg"); Latitude = (client.Read()); client.Write("get /position/longitude-deg"); Longitude = (client.Read()); NotifyPropertyChanged("Location"); mtx.ReleaseMutex(); Thread.Sleep(250); } } catch (InvalidOperationException) { if (Connected) { Disconnect(); Message = "Server terminated unexpectedly."; MessageInd = true; } mtx.ReleaseMutex(); } catch (ArgumentNullException) { Disconnect(); Message = "Server terminated unexpectedly."; MessageInd = true; mtx.ReleaseMutex(); } catch (SocketException) { Disconnect(); Message = "Server terminated unexpectedly."; MessageInd = true; mtx.ReleaseMutex(); } catch (IOException e) { Disconnect(); if (e.Message.Contains("connected party did not properly respond after a period of time")) { Message = "Server timed out. Disconnected"; } else { Message = "Server terminated unexpectedly."; } MessageInd = true; mtx.ReleaseMutex(); } }).Start(); }
public async Task StartWriting(string command) { await Task.Run(() => telnetClient.Write(command)); }
public void Start() { double val; string serverStr; while (!stop) { try { var builder = new StringBuilder(); for (int i = 0; i < readFlightObjects.Length; i++) { Reset(timer); builder.Append("get ").Append(readFlightObjects[i].Sim); myTelnetClient.Write(builder.ToString()); serverStr = myTelnetClient.Read(); val = Double.Parse(serverStr); //8 and 9 are the indexs of the longitude and latitude thus we check their values try { if (i == 8 && ((val > 85) || (val < -85))) { throw new InvalidOperationException(); } if (i == 9 && ((val > 180) || (val < -180))) { throw new InvalidOperationException(); } } catch (InvalidOperationException) { CallErrorAsync("ERR: Invalid map values"); builder.Clear(); continue; } readFlightObjects[i].Value = val; NotifyPropertyChanged(readFlightObjects[i].Name); builder.Clear(); } Reset(timer); //send all the commands from the queue to the simulator and remove the item from the queue //resetting timer to check dealay, with the server, in the set commands while (queueCommands.Count > 0) { myTelnetClient.Write(queueCommands.Dequeue()); serverStr = myTelnetClient.Read(); val = Double.Parse(serverStr); } } catch (FormatException) { CallErrorAsync("ERR: Invalid value"); } catch (IOException) { CallErrorAsync("ERR: Server is not available"); } //general exception catch (Exception) { CallErrorAsync("Err"); } Thread.Sleep(250); } }