Python wrapper for X-Plane Web API
Usage of REST API
import xpwebapi
# REST API
api = xpwebapi.rest_api(host="192.168.1.140", port=8080, api_version="v2") # defaults: host="127.0.0.1", port=8086, api="/api", use_cache=False
# options: no_cache, version
print(api.capabilities)
api.set_api_version(api_version="v2")
dataref = api.dataref("sim/cockpit2/clock_timer/local_time_seconds")
print(dataref)
# sim/cockpit2/clock_timer/local_time_seconds=42
mapview = api.command("sim/map/show_current")
mapview.execute()
Usage of Websocket API
from xpwebapi import ws_api, CALLBACK_TYPE
ws = ws_api(host="192.168.1.140", port=8080) # defaults to v2 for Websocket
def dataref_monitor(dataref: str, value: Any):
print(f"dataref updated: {dataref}={value}")
def command_active_monitor(command: str, active: bool):
print(f"command activated: {command}={active}")
ws.add_callback(cbtype=CALLBACK_TYPE.DATAREF_UPDATE, callback=dataref_monitor)
ws.add_callback(cbtype=CALLBACK_TYPE.COMMAND_ACTIVE, callback=command_active_monitor)
ws.connect()
ws.wait_connection() # blocks until X-Plane is reachable
dataref = ws.dataref("sim/cockpit2/clock_timer/local_time_seconds")
ws.monitor_dataref(dataref)
# alternative:
# dataref.monitor()
ws.monitor_command_active(ws.command("sim/map/show_current"))
# alternative:
# command = ws.command("sim/map/show_current")
# command.monitor()
ws.start(release=True)
time.sleep(10)
print("terminating..")
ws.stop()
print("..disconnecting..")
ws.disconnect()
print("..terminated")
Integrated! Usage of UDP API
import time
from typing import Any
import xpwebapi
def dataref_monitor(dataref: str, value: Any):
print(f"{dataref}={value}")
# UDP API
beacon = xpwebapi.beacon()
beacon.start_monitor()
while not beacon.receiving_beacon:
print("waiting for beacon..")
time.sleep(2)
xp = xpwebapi.udp_api(beacon=beacon)
xp.add_callback(callback=dataref_monitor)
xp.monitor_dataref(xp.dataref(path="sim/flightmodel/position/indicated_airspeed"))
xp.monitor_dataref(xp.dataref(path="sim/flightmodel/position/latitude"))
xp.start()