Skip to content

Commit 9cd2b3f

Browse files
committed
Added UDP connection statuses
1 parent 431c079 commit 9cd2b3f

5 files changed

Lines changed: 19 additions & 6 deletions

File tree

examples/simple_upd.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import os
22
import sys
33
import logging
4-
import time
54
from typing import Any
65

76
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
@@ -19,9 +18,8 @@ def dataref_monitor(dataref: str, value: Any):
1918
# UDP API
2019
beacon = xpwebapi.beacon()
2120
beacon.start_monitor()
22-
while not beacon.receiving_beacon:
23-
print("waiting for beacon")
24-
time.sleep(2)
21+
beacon.wait_for_beacon(report=True) # blocks until beacon is detected
22+
2523
xp = xpwebapi.udp_api(beacon=beacon)
2624

2725
xp.add_callback(callback=dataref_monitor)

xpwebapi/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ def udp_api(**kwargs):
2222
return XPUDPAPI(**kwargs)
2323

2424

25-
version = "3.2.1"
25+
version = "3.2.2"

xpwebapi/api.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class CONNECTION_STATUS(IntEnum):
5151
REST_API_NOT_REACHABLE = 8
5252
WEBSOCKET_CONNNECTED = 3
5353
WEBSOCKET_DISCONNNECTED = 9
54+
UDP_LISTENER_RUNNING = 6
5455
LISTENING_FOR_DATA = 4
5556
RECEIVING_DATA = 5
5657

xpwebapi/beacon.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import struct
1717
import binascii
1818
import platform
19+
import time
1920
from typing import Callable, List, Set
2021
from enum import Enum, IntEnum
2122
from datetime import datetime
@@ -444,6 +445,12 @@ def stop_monitor(self):
444445
logger.debug("..monitor not running")
445446
self.status = BEACON_MONITOR_STATUS.NOT_RUNNING
446447

448+
def wait_for_beacon(self, report: bool = False, retry: int = 2):
449+
while not self.receiving_beacon:
450+
if report:
451+
logger.info("waiting for beacon..")
452+
time.sleep(retry)
453+
447454

448455
# ######################################
449456
# Demo and Usage

xpwebapi/udp.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from time import sleep
1515
from typing import Tuple, Dict, Callable
1616

17-
from .api import API, DatarefValueType, Dataref, Command
17+
from .api import API, CONNECTION_STATUS, DatarefValueType, Dataref, Command
1818

1919
# local logging
2020
logger = logging.getLogger(__name__)
@@ -218,9 +218,13 @@ def read_monitored_dataref_values(self):
218218
Raises:
219219
XPlaneTimeout: [description]
220220
"""
221+
if self.status not in [CONNECTION_STATUS.LISTENING_FOR_DATA, CONNECTION_STATUS.RECEIVING_DATA]:
222+
self.status = CONNECTION_STATUS.LISTENING_FOR_DATA
221223
try:
222224
# Receive packet
223225
data, addr = self.socket.recvfrom(1472) # maximum bytes of an RREF answer X-Plane will send (Ethernet MTU - IP hdr - UDP hdr)
226+
if self.status != CONNECTION_STATUS.RECEIVING_DATA:
227+
self.status = CONNECTION_STATUS.RECEIVING_DATA
224228
# Decode Packet
225229
retvalues = {}
226230
# * Read the Header "RREFO".
@@ -244,6 +248,8 @@ def read_monitored_dataref_values(self):
244248
self.execute_callbacks(dataref=self.datarefs[idx], value=value)
245249
self.xplaneValues.update(retvalues)
246250
except:
251+
if self.status != CONNECTION_STATUS.LISTENING_FOR_DATA:
252+
self.status = CONNECTION_STATUS.LISTENING_FOR_DATA
247253
raise XPlaneTimeout
248254
return self.xplaneValues
249255

@@ -254,6 +260,7 @@ def udp_listener_running(self) -> bool:
254260
def udp_listener(self):
255261
logger.info("starting udp listener..")
256262

263+
self.status = CONNECTION_STATUS.UDP_LISTENER_RUNNING
257264
while self.udp_listener_running:
258265
try:
259266
data = self.read_monitored_dataref_values()

0 commit comments

Comments
 (0)