2002-05-15 21:45:39 +00:00
|
|
|
from telnetlib import Telnet
|
|
|
|
import sys
|
|
|
|
import socket
|
|
|
|
import re
|
|
|
|
import time
|
|
|
|
|
|
|
|
__all__ = ["FlightGear"]
|
|
|
|
|
|
|
|
CRLF = '\r\n'
|
|
|
|
|
|
|
|
class FGTelnet(Telnet):
|
|
|
|
def __init__(self,host,port):
|
|
|
|
Telnet.__init__(self,host,port)
|
2020-08-03 18:13:35 +00:00
|
|
|
self.prompt = [re.compile('/[^>]*> '.encode('utf-8'))]
|
2002-05-15 21:45:39 +00:00
|
|
|
self.timeout = 5
|
|
|
|
#Telnet.set_debuglevel(self,2)
|
|
|
|
|
|
|
|
def help(self):
|
|
|
|
return
|
|
|
|
|
|
|
|
def ls(self,dir=None):
|
|
|
|
"""
|
|
|
|
Returns a list of properties.
|
|
|
|
"""
|
2020-08-29 17:07:15 +00:00
|
|
|
if dir is None:
|
2002-05-15 21:45:39 +00:00
|
|
|
self._putcmd('ls')
|
|
|
|
else:
|
|
|
|
self._putcmd('ls %s' % dir )
|
|
|
|
return self._getresp()
|
|
|
|
|
|
|
|
def dump(self):
|
|
|
|
"""Dump current state as XML."""
|
|
|
|
self._putcmd('dump')
|
|
|
|
return self._getresp()
|
|
|
|
|
|
|
|
def cd(self, dir):
|
|
|
|
"""Change directory."""
|
|
|
|
self._putcmd('cd ' + dir)
|
|
|
|
self._getresp()
|
|
|
|
return
|
|
|
|
|
|
|
|
def pwd(self):
|
|
|
|
"""Display current path."""
|
|
|
|
self._putcmd('pwd')
|
|
|
|
return self._getresp()
|
|
|
|
|
|
|
|
def get(self,var):
|
|
|
|
"""Retrieve the value of a parameter."""
|
|
|
|
self._putcmd('get %s' % var )
|
|
|
|
return self._getresp()
|
|
|
|
|
|
|
|
def set(self,var,value):
|
|
|
|
"""Set variable to a new value"""
|
|
|
|
self._putcmd('set %s %s' % (var,value))
|
|
|
|
self._getresp() # Discard response
|
|
|
|
|
|
|
|
def quit(self):
|
|
|
|
"""Terminate connection"""
|
|
|
|
self._putcmd('quit')
|
|
|
|
self.close()
|
|
|
|
return
|
|
|
|
|
|
|
|
# Internal: send one command to FlightGear
|
|
|
|
def _putcmd(self,cmd):
|
2020-08-03 18:13:35 +00:00
|
|
|
cmd = cmd + CRLF
|
|
|
|
Telnet.write(self, cmd.encode('utf-8'))
|
2002-05-15 21:45:39 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
# Internal: get a response from FlightGear
|
|
|
|
def _getresp(self):
|
2020-08-03 18:13:35 +00:00
|
|
|
(_i,_match,resp) = Telnet.expect(self, self.prompt, self.timeout)
|
2002-05-15 21:45:39 +00:00
|
|
|
# Remove the terminating prompt.
|
|
|
|
# Everything preceding it is the response.
|
2020-08-03 18:13:35 +00:00
|
|
|
return resp.decode('utf-8').split('\n')[:-1]
|
2002-05-15 21:45:39 +00:00
|
|
|
|
|
|
|
class FlightGear:
|
|
|
|
"""FlightGear interface class.
|
|
|
|
|
|
|
|
An instance of this class represents a connection to a FlightGear telnet
|
|
|
|
server.
|
|
|
|
|
|
|
|
Properties are accessed using a dictionary style interface:
|
|
|
|
For example:
|
|
|
|
|
|
|
|
# Connect to flightgear telnet server.
|
|
|
|
fg = FlightGear('myhost', 5500)
|
|
|
|
# parking brake on
|
2004-01-12 17:39:41 +00:00
|
|
|
fg['/controls/gear/brake-parking'] = 1
|
2002-05-15 21:45:39 +00:00
|
|
|
# Get current heading
|
|
|
|
heading = fg['/orientation/heading-deg']
|
|
|
|
|
|
|
|
Other non-property related methods
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__( self, host = 'localhost', port = 5500 ):
|
|
|
|
try:
|
|
|
|
self.telnet = FGTelnet(host,port)
|
2020-08-03 18:13:35 +00:00
|
|
|
except socket.error as msg:
|
2002-05-15 21:45:39 +00:00
|
|
|
self.telnet = None
|
2020-08-03 18:13:35 +00:00
|
|
|
raise msg
|
2006-10-29 12:34:26 +00:00
|
|
|
|
2002-05-15 21:45:39 +00:00
|
|
|
def __del__(self):
|
|
|
|
# Ensure telnet connection is closed cleanly.
|
2020-08-03 18:13:35 +00:00
|
|
|
self.quit()
|
2002-05-15 21:45:39 +00:00
|
|
|
|
|
|
|
def __getitem__(self,key):
|
|
|
|
"""Get a FlightGear property value.
|
|
|
|
Where possible the value is converted to the equivalent Python type.
|
|
|
|
"""
|
|
|
|
s = self.telnet.get(key)[0]
|
2020-08-03 18:13:35 +00:00
|
|
|
match = re.compile( r'[^=]*=\s*\'([^\']*)\'\s*([^\r]*)\r').match( s )
|
2002-07-22 23:25:28 +00:00
|
|
|
if not match:
|
|
|
|
return None
|
|
|
|
value,type = match.groups()
|
|
|
|
#value = match.group(1)
|
|
|
|
#type = match.group(2)
|
2002-05-15 21:45:39 +00:00
|
|
|
if value == '':
|
|
|
|
return None
|
2002-07-22 23:25:28 +00:00
|
|
|
|
|
|
|
if type == '(double)':
|
|
|
|
return float(value)
|
|
|
|
elif type == '(int)':
|
|
|
|
return int(value)
|
|
|
|
elif type == '(bool)':
|
|
|
|
if value == 'true':
|
|
|
|
return 1
|
2002-05-15 21:45:39 +00:00
|
|
|
else:
|
2002-07-22 23:25:28 +00:00
|
|
|
return 0
|
|
|
|
else:
|
|
|
|
return value
|
2006-10-29 12:34:26 +00:00
|
|
|
|
2002-05-15 21:45:39 +00:00
|
|
|
def __setitem__(self, key, value):
|
2002-07-22 23:25:28 +00:00
|
|
|
"""Set a FlightGear property value."""
|
2002-05-15 21:45:39 +00:00
|
|
|
self.telnet.set( key, value )
|
|
|
|
|
|
|
|
def quit(self):
|
2002-07-22 23:25:28 +00:00
|
|
|
"""Close the telnet connection to FlightGear."""
|
2002-05-15 21:45:39 +00:00
|
|
|
if self.telnet:
|
|
|
|
self.telnet.quit()
|
|
|
|
self.telnet = None
|
2006-10-29 12:34:26 +00:00
|
|
|
|
|
|
|
def view_next(self):
|
2020-08-03 18:13:35 +00:00
|
|
|
"""Move to next view."""
|
2006-10-29 12:34:26 +00:00
|
|
|
self.telnet.set( "/command/view/next", "true")
|
|
|
|
|
|
|
|
def view_prev(self):
|
2020-08-03 18:13:35 +00:00
|
|
|
"""Move to next view."""
|
2006-10-29 12:34:26 +00:00
|
|
|
self.telnet.set( "/command/view/prev", "true")
|
|
|
|
|