Commit 64cbb4a3 authored by Rick Gruber-Riemer's avatar Rick Gruber-Riemer

Getting telnet to work again.

parent 3c55c3bf
......@@ -2,8 +2,13 @@
Created on 27.04.2014
@author: keith.paterson
"""
See https://docs.python.org/3.5/library/telnetlib.html
See http://wiki.flightgear.org/Telnet_usage
Used in tools.py -> _raster_telnet(..)
"""
import logging
import re
import telnetlib
import time
......@@ -19,28 +24,28 @@ class FG_Telnet(object):
self.sock.write(bytes("data", encoding="ascii"))
def get_prop(self, property_name):
self.sock.write(bytes("get " + property_name + "\n", encoding="ascii"))
self.sock.write(bytes("get " + property_name + "\r\n", encoding="ascii"))
result = self.sock.read_until(self.PROMPT, 60)
return result
def set_prop(self, property_name, value):
send_buffer = bytes("set %s %s\n" % (property_name, value), encoding="ascii")
send_buffer = bytes("set %s %s\r\n" % (property_name, value), encoding="ascii")
self.sock.write(send_buffer)
result = self.sock.read_until(self.PROMPT, 60)
return result
def run_command(self, command):
try:
send_buffer = bytes("run %s\n" % command, encoding="ascii")
send_buffer = bytes("run %s\r\n" % command, encoding="ascii")
self.sock.write(send_buffer)
result = self.sock.read_until(self.PROMPT, 12000)
result_str = result.decode("ascii")
print(result)
logging.debug("Result from running command %s: %s", command, result)
if result_str.find("<completed>") >= 0:
return True
return False
except Exception as e:
print(e)
logging.exception("Running command %s resulted in exception", command)
return False
def get_elevation(self, lon, lat):
......@@ -54,6 +59,27 @@ class FG_Telnet(object):
elev = re.search('[^\']*\'([-e0-9.]*)\'[.]*', prop).group(1)
return float(elev)
def wait_loop(self):
"""Waits for FlightGear to signal, that the elevation processing has finished."""
for count in range(0, 1000):
semaphore = self.get_prop("/osm2city/tiles").decode("ascii")
semaphore = semaphore.split('=')[1]
m = re.search("([0-9.]+)", semaphore)
# We don't care if we get 0.0000 (String) or 0 (Int)
record = self.get_prop("/osm2city/record").decode("ascii")
record = record.split('=')[1]
m2 = re.search("([0-9.]+)", record)
if m is not None and float(m.groups()[0]) > 0:
try:
return True
except:
# perform an action#
pass
time.sleep(1)
if m2 is not None:
logging.debug("Waiting for Semaphore " + m2.groups()[0])
return False
def close(self):
self.sock.close()
......@@ -75,3 +101,4 @@ if __name__ == '__main__':
time.sleep(1)
fg.set_prop("/position/latitude-deg", 56)
fg.get_prop("/position/ground-elev-m")
fg.close()
......@@ -15,7 +15,6 @@ import math
import os
import os.path as osp
import queue
import re
import shutil
import subprocess
import sys
......@@ -420,28 +419,6 @@ def raster_glob(prevent_overwrite=False):
sys.exit(1)
def wait_for_fg(fg):
"""Waits for FlightGear to signal, that the elevation processing has finished."""
for count in range(0, 1000):
semaphore = fg.get_prop("/osm2city/tiles")
semaphore = semaphore.split('=')[1]
m = re.search("([0-9.]+)", semaphore)
# We don't care if we get 0.0000 (String) or 0 (Int)
record = fg.get_prop("/osm2city/record")
record = record.split('=')[1]
m2 = re.search("([0-9.]+)", record)
if m is not None and float(m.groups()[0]) > 0:
try:
return True
except:
# perform an action#
pass
time.sleep(1)
if m2 is not None:
logging.debug("Waiting for Semaphore " + m2.groups()[0])
return False
def _raster_telnet(transform, fname, x0, y0, size_x=1000, size_y=1000, step_x=5, step_y=5):
"""Writes elev.in and elev.out using Telnet to a running FlightGear instance."""
fg_home_path = setup.getFGHome()
......@@ -469,7 +446,7 @@ def _raster_telnet(transform, fname, x0, y0, size_x=1000, size_y=1000, step_x=5,
logging.info("Running FG Command")
fg.set_prop("/osm2city/tiles", 0)
if fg.run_command("get-elevation"):
if not wait_for_fg(fg):
if not fg.wait_loop():
logging.error("Process in FG timed out")
else:
logging.info("Success")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment