]> git.mxchange.org Git - flightgear.git/blob - scripts/python/FlightGear.py
Replace cout statement with SG_LOG.
[flightgear.git] / scripts / python / FlightGear.py
1 from telnetlib import Telnet
2 import sys
3 import socket
4 import re
5 from string import split, join
6 import time
7
8 __all__ = ["FlightGear"]
9
10 CRLF = '\r\n'
11
12 class FGTelnet(Telnet):
13     def __init__(self,host,port):
14         Telnet.__init__(self,host,port)
15         self.prompt = []
16         self.prompt.append( re.compile('/[^>]*> ') )
17         self.timeout = 5
18         #Telnet.set_debuglevel(self,2)
19
20     def help(self):
21         return
22
23     def ls(self,dir=None):
24         """
25         Returns a list of properties.
26         """
27         if dir == None:
28             self._putcmd('ls')
29         else:
30             self._putcmd('ls %s' % dir )
31         return self._getresp()
32
33     def dump(self):
34         """Dump current state as XML."""
35         self._putcmd('dump')
36         return self._getresp()
37
38     def cd(self, dir):
39         """Change directory."""
40         self._putcmd('cd ' + dir)
41         self._getresp()
42         return
43
44     def pwd(self):
45         """Display current path."""
46         self._putcmd('pwd')
47         return self._getresp()
48
49     def get(self,var):
50         """Retrieve the value of a parameter."""
51         self._putcmd('get %s' % var )
52         return self._getresp()
53
54     def set(self,var,value):
55         """Set variable to a new value"""
56         self._putcmd('set %s %s' % (var,value))
57         self._getresp() # Discard response
58
59     def quit(self):
60         """Terminate connection"""
61         self._putcmd('quit')
62         self.close()
63         return
64
65     # Internal: send one command to FlightGear
66     def _putcmd(self,cmd):
67         cmd = cmd + CRLF;
68         Telnet.write(self, cmd)
69         return
70
71     # Internal: get a response from FlightGear
72     def _getresp(self):
73         (i,match,resp) = Telnet.expect(self, self.prompt, self.timeout)
74         # Remove the terminating prompt.
75         # Everything preceding it is the response.
76         return split(resp, '\n')[:-1]
77
78 class FlightGear:
79     """FlightGear interface class.
80
81     An instance of this class represents a connection to a FlightGear telnet
82     server.
83
84     Properties are accessed using a dictionary style interface:
85     For example:
86
87     # Connect to flightgear telnet server.
88     fg = FlightGear('myhost', 5500)
89     # parking brake on
90     fg['/controls/parking-brake'] = 1
91     # Get current heading
92     heading = fg['/orientation/heading-deg']
93
94     Other non-property related methods
95     """
96
97     def __init__( self, host = 'localhost', port = 5500 ):
98         try:
99             self.telnet = FGTelnet(host,port)
100         except socket.error, msg:
101             self.telnet = None
102             raise socket.error, msg
103         
104     def __del__(self):
105         # Ensure telnet connection is closed cleanly.
106         self.quit();
107
108     def __getitem__(self,key):
109         """Get a FlightGear property value.
110         Where possible the value is converted to the equivalent Python type.
111         """
112         s = self.telnet.get(key)[0]
113         (name,x,value,type) = s.split()
114         if value == '':
115             return None
116         else:
117             value = value[1:-1] # strip quote characters
118             #print "name=%s,value=%s,type=%s" % (name,value,type)
119             if type == '(double)':
120                 return float(value)
121             elif type == '(int)':
122                 return int(value)
123             elif type == '(bool)':
124                 if value == 'true':
125                     return 1
126                 else:
127                     return 0
128             else:
129                 return value
130         
131     def __setitem__(self, key, value):
132         """Set a FlightGear property value.
133         """
134         self.telnet.set( key, value )
135
136     def quit(self):
137         """Close the telnet connection to FlightGear.
138         """
139         if self.telnet:
140             self.telnet.quit()
141             self.telnet = None
142
143     def view_next(self):
144         """
145         """
146         self.telnet._putcmd('view next')
147         self.telnet._getresp()
148
149     def view_prev(self):
150         """
151         """
152         self.telnet._putcmd('view prev')
153         self.telnet._getresp()
154
155     def wait_for_prop_eq(self, prop, value, interval = 0.5):
156         while self.__getitem__(prop) != value:
157             time.sleep(interval)