diff --git a/examples/simple_http.py b/examples/simple_http.py index f599a51..ff2aae4 100755 --- a/examples/simple_http.py +++ b/examples/simple_http.py @@ -10,10 +10,10 @@ """ Start FlightGear with `--httpd=5050` """ -props_conn = HTTPConnection('localhost', 5050) -pprint(props_conn.list_props('/', recurse_limit=0)) # List the top-level properties, no recursion +http_conn = HTTPConnection('localhost', 5050) +pprint(http_conn.list_props('/', recurse_limit=0)) # List the top-level properties, no recursion while True: - alt_ft = props_conn.get_prop('/position/altitude-ft') + alt_ft = http_conn.get_prop('/position/altitude-ft') print(f'Altitude: {alt_ft:.1f}ft') - props_conn.set_prop('/position/altitude-ft', alt_ft + 20.0) + http_conn.set_prop('/position/altitude-ft', alt_ft + 20.0) time.sleep(0.1) diff --git a/flightgear_python/fg_if.py b/flightgear_python/fg_if.py index e94d2ff..c01d4c7 100755 --- a/flightgear_python/fg_if.py +++ b/flightgear_python/fg_if.py @@ -429,9 +429,7 @@ class HTTPConnection(PropsConnectionBase): """ def __init__(self, host: str, tcp_port: int, timeout_s: float = 2.0): - self.host = host - self.port = tcp_port - self.url = f'http://{self.host}:{self.port}/json' + self.url = f'http://{host}:{tcp_port}/json' self.session = requests.Session() self.timeout_s = timeout_s @@ -453,7 +451,7 @@ def get_prop(self, prop_str: str) -> Any: """ prop_str = self.check_and_normalize_prop_path(prop_str) resp_json = self.request_shim('GET', self.url + prop_str).json() - converted_value = self._auto_convert_fg_prop(resp_json["value"], resp_json["type"]) + converted_value = self._auto_convert_fg_prop(resp_json['value'], resp_json['type']) return converted_value def set_prop(self, prop_str: str, value: Any): @@ -468,15 +466,19 @@ def set_prop(self, prop_str: str, value: Any): # Fetch the type of the property resp_json = self.request_shim('GET', self.url + prop_str).json() # Set the property - data = {"path": prop_str, "value": str(value), "type": resp_json["type"]} + data = { + 'path': prop_str, + 'value': str(value), + 'type': resp_json['type'], + } self.request_shim('POST', self.url + prop_str, json=data) # We don't care about the response def get_values_and_dirs(self, path: str) -> Tuple[List[PropertyTreeValue], List[str]]: resp_json = self.request_shim('GET', self.url + path).json() - if resp_json["nChildren"] == 0: + if resp_json['nChildren'] == 0: return [], [] - resp_list = resp_json["children"] + resp_list = resp_json['children'] # extract of directories, absolute path val_list: List[PropertyTreeValue] = [] dir_list: List[str] = []