Skip to content

Commit 9706b6a

Browse files
committed
relay_frames: add wrappers to send and receive data between XBee local
interfaces - The new methods allow sending data and registering a listener to receive data from the Bluetooth and MicroPython interface. - The UserDataRelayInterface class has been renamed to XBeeLocalInterface. - Updated also the documentation. Signed-off-by: Ruben Moral <ruben.moral@digi.com>
1 parent 2a044c3 commit 9706b6a

File tree

8 files changed

+1076
-718
lines changed

8 files changed

+1076
-718
lines changed

digi/xbee/devices.py

Lines changed: 119 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from digi.xbee.models.mode import OperatingMode, APIOutputMode, IPAddressingMode
2929
from digi.xbee.models.address import XBee64BitAddress, XBee16BitAddress, XBeeIMEIAddress
3030
from digi.xbee.models.message import XBeeMessage, ExplicitXBeeMessage, IPMessage
31-
from digi.xbee.models.options import TransmitOptions, RemoteATCmdOptions, DiscoveryOptions
31+
from digi.xbee.models.options import TransmitOptions, RemoteATCmdOptions, DiscoveryOptions, XBeeLocalInterface
3232
from digi.xbee.models.protocol import XBeeProtocol, IPProtocol
3333
from digi.xbee.models.status import ATCommandStatus, TransmitStatus, PowerLevel, \
3434
ModemStatus, CellularAssociationIndicationStatus, WiFiAssociationIndicationStatus, AssociationIndicationStatus,\
@@ -1295,6 +1295,28 @@ def _add_user_data_relay_received_callback(self, callback):
12951295
"""
12961296
self._packet_listener.add_user_data_relay_received_callback(callback)
12971297

1298+
def _add_bluetooth_data_received_callback(self, callback):
1299+
"""
1300+
Adds a callback for the event :class:`.BluetoothDataReceived`.
1301+
1302+
Args:
1303+
callback (Function): the callback. Receives one argument.
1304+
1305+
* The Bluetooth data as a Bytearray
1306+
"""
1307+
self._packet_listener.add_bluetooth_data_received_callback(callback)
1308+
1309+
def _add_micropython_data_received_callback(self, callback):
1310+
"""
1311+
Adds a callback for the event :class:`.MicroPythonDataReceived`.
1312+
1313+
Args:
1314+
callback (Function): the callback. Receives one argument.
1315+
1316+
* The MicroPython data as a Bytearray
1317+
"""
1318+
self._packet_listener.add_micropython_data_received_callback(callback)
1319+
12981320
def _del_packet_received_callback(self, callback):
12991321
"""
13001322
Deletes a callback for the callback list of :class:`.PacketReceived` event.
@@ -1367,6 +1389,30 @@ def _del_user_data_relay_received_callback(self, callback):
13671389
"""
13681390
self._packet_listener.del_user_data_relay_received_callback(callback)
13691391

1392+
def _del_bluetooth_data_received_callback(self, callback):
1393+
"""
1394+
Deletes a callback for the callback list of :class:`.BluetoothDataReceived` event.
1395+
1396+
Args:
1397+
callback (Function): the callback to delete.
1398+
1399+
Raises:
1400+
ValueError: if ``callback`` is not in the callback list of :class:`.BluetoothDataReceived` event.
1401+
"""
1402+
self._packet_listener.del_bluetooth_data_received_callback(callback)
1403+
1404+
def _del_micropython_data_received_callback(self, callback):
1405+
"""
1406+
Deletes a callback for the callback list of :class:`.MicroPythonDataReceived` event.
1407+
1408+
Args:
1409+
callback (Function): the callback to delete.
1410+
1411+
Raises:
1412+
ValueError: if ``callback`` is not in the callback list of :class:`.MicroPythonDataReceived` event.
1413+
"""
1414+
self._packet_listener.del_micropython_data_received_callback(callback)
1415+
13701416
def _send_packet_sync_and_get_response(self, packet_to_send):
13711417
"""
13721418
Perform all operations needed for a synchronous operation when the packet
@@ -2161,25 +2207,65 @@ def send_data_broadcast(self, data, transmit_options=TransmitOptions.NONE.value)
21612207
"""
21622208
return self._send_data_64(XBee64BitAddress.BROADCAST_ADDRESS, data, transmit_options)
21632209

2164-
def send_user_data_relay(self, relay_interface, data):
2210+
@AbstractXBeeDevice._before_send_method
2211+
def send_user_data_relay(self, local_interface, data):
21652212
"""
2166-
Sends the given data to the given relay interface.
2213+
Sends the given data to the given XBee local interface.
21672214
21682215
Args:
2169-
relay_interface (:class:`.UserDataRelayInterface`): Destination relay interface.
2216+
local_interface (:class:`.XBeeLocalInterface`): Destination XBee local interface.
21702217
data (Bytearray): Data to send.
21712218
21722219
Raises:
2173-
ValueError: if ``relay_interface`` is ``None``.
2220+
InvalidOperatingModeException: if the XBee device's operating mode is not API or ESCAPED API. This
2221+
method only checks the cached value of the operating mode.
2222+
ValueError: if ``local_interface`` is ``None``.
2223+
XBeeException: if there is any problem sending the User Data Relay.
21742224
21752225
.. seealso::
2176-
| :class:`.UserDataRelayInterface`
2226+
| :class:`.XBeeLocalInterface`
21772227
"""
2178-
if relay_interface is None:
2179-
raise ValueError("Relay interface cannot be None")
2228+
if local_interface is None:
2229+
raise ValueError("Destination interface cannot be None")
21802230

21812231
# Send the packet asynchronously since User Data Relay frames do not receive any transmit status.
2182-
self.send_packet(UserDataRelayPacket(self.get_next_frame_id(), relay_interface, data))
2232+
self.send_packet(UserDataRelayPacket(self.get_next_frame_id(), local_interface, data))
2233+
2234+
def send_bluetooth_data(self, data):
2235+
"""
2236+
Sends the given data to the Bluetooth interface using a User Data Relay frame.
2237+
2238+
Args:
2239+
data (Bytearray): Data to send.
2240+
2241+
Raises:
2242+
InvalidOperatingModeException: if the XBee device's operating mode is not API or ESCAPED API. This
2243+
method only checks the cached value of the operating mode.
2244+
XBeeException: if there is any problem sending the data.
2245+
2246+
.. seealso::
2247+
| :meth:`.XBeeDevice.send_micropython_data`
2248+
| :meth:`.XBeeDevice.send_user_data_relay`
2249+
"""
2250+
self.send_user_data_relay(XBeeLocalInterface.BLUETOOTH, data)
2251+
2252+
def send_micropython_data(self, data):
2253+
"""
2254+
Sends the given data to the MicroPython interface using a User Data Relay frame.
2255+
2256+
Args:
2257+
data (Bytearray): Data to send.
2258+
2259+
Raises:
2260+
InvalidOperatingModeException: if the XBee device's operating mode is not API or ESCAPED API. This
2261+
method only checks the cached value of the operating mode.
2262+
XBeeException: if there is any problem sending the data.
2263+
2264+
.. seealso::
2265+
| :meth:`.XBeeDevice.send_bluetooth_data`
2266+
| :meth:`.XBeeDevice.send_user_data_relay`
2267+
"""
2268+
self.send_user_data_relay(XBeeLocalInterface.MICROPYTHON, data)
21832269

21842270
def read_data(self, timeout=None):
21852271
"""
@@ -2338,6 +2424,18 @@ def add_user_data_relay_received_callback(self, callback):
23382424
"""
23392425
super()._add_user_data_relay_received_callback(callback)
23402426

2427+
def add_bluetooth_data_received_callback(self, callback):
2428+
"""
2429+
Override.
2430+
"""
2431+
super()._add_bluetooth_data_received_callback(callback)
2432+
2433+
def add_micropython_data_received_callback(self, callback):
2434+
"""
2435+
Override.
2436+
"""
2437+
super()._add_micropython_data_received_callback(callback)
2438+
23412439
def del_packet_received_callback(self, callback):
23422440
"""
23432441
Override.
@@ -2374,6 +2472,18 @@ def _del_user_data_relay_received_callback(self, callback):
23742472
"""
23752473
super()._del_user_data_relay_received_callback(callback)
23762474

2475+
def _del_bluetooth_data_received_callback(self, callback):
2476+
"""
2477+
Override.
2478+
"""
2479+
super()._del_bluetooth_data_received_callback(callback)
2480+
2481+
def _del_micropython_data_received_callback(self, callback):
2482+
"""
2483+
Override.
2484+
"""
2485+
super()._del_micropython_data_received_callback(callback)
2486+
23772487
def get_xbee_device_callbacks(self):
23782488
"""
23792489
Returns this XBee internal callbacks for process received packets.

digi/xbee/models/message.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -401,38 +401,38 @@ class UserDataRelayMessage(object):
401401
interface and the content (data) of the message.
402402
403403
.. seealso::
404-
| :class:`.UserDataRelayInterface`
404+
| :class:`.XBeeLocalInterface`
405405
"""
406406

407-
def __init__(self, relay_interface, data):
407+
def __init__(self, local_interface, data):
408408
"""
409409
Class constructor. Instantiates a new :class:`.UserDataRelayMessage` object with
410410
the provided parameters.
411411
412412
Args:
413-
relay_interface (:class:`.UserDataRelayInterface`): The source relay interface.
413+
local_interface (:class:`.XBeeLocalInterface`): The source XBee local interface.
414414
data (Bytearray): Byte array containing the data of the message.
415415
416416
Raises:
417417
ValueError: if ``relay_interface`` is ``None``.
418418
419419
.. seealso::
420-
| :class:`.UserDataRelayInterface`
420+
| :class:`.XBeeLocalInterface`
421421
"""
422-
if relay_interface is None:
423-
raise ValueError("Relay interface cannot be None")
422+
if local_interface is None:
423+
raise ValueError("XBee local interface cannot be None")
424424

425-
self.__relay_interface = relay_interface
425+
self.__local_interface = local_interface
426426
self.__data = data
427427

428-
def __get_relay_interface(self):
428+
def __get_src_interface(self):
429429
"""
430430
Returns the source interface that sent the message.
431431
432432
Returns:
433-
:class:`.UserDataRelayInterface`: The source interface that sent the message.
433+
:class:`.XBeeLocalInterface`: The source interface that sent the message.
434434
"""
435-
return self.__relay_interface
435+
return self.__local_interface
436436

437437
def __get_data(self):
438438
"""
@@ -447,11 +447,11 @@ def to_dict(self):
447447
"""
448448
Returns the message information as a dictionary.
449449
"""
450-
return {"Relay interface: ": self.__relay_interface,
451-
"Data: ": self.__data}
450+
return {"XBee local interface: ": self.__local_interface,
451+
"Data: ": self.__data}
452452

453-
relay_interface = property(__get_relay_interface)
454-
""":class:`.UserDataRelayInterface`. Source interface that sent the message."""
453+
local_interface = property(__get_src_interface)
454+
""":class:`.XBeeLocalInterface`. Source interface that sent the message."""
455455

456456
data = property(__get_data)
457457
"""Bytearray. The data of the message."""

digi/xbee/models/options.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -406,14 +406,14 @@ def calculate_discovery_value(protocol, options):
406406

407407

408408
@unique
409-
class UserDataRelayInterface(Enum):
409+
class XBeeLocalInterface(Enum):
410410
"""
411411
Enumerates the different interfaces for the :class:`.UserDataRelayPacket`
412412
and :class:`.UserDataRelayOutputPacket`.
413413
414414
| Inherited properties:
415-
| **name** (String): the name (id) of the relay interface.
416-
| **value** (String): the value of the relay interface.
415+
| **name** (String): the name (id) of the XBee local interface.
416+
| **value** (String): the value of the XBee local interface.
417417
"""
418418
SERIAL = (0, "Serial port (UART when in API mode, or SPI interface)")
419419
BLUETOOTH = (1, "BLE API interface (on XBee devices which support BLE)")
@@ -426,45 +426,45 @@ def __init__(self, code, description):
426426

427427
def __get_code(self):
428428
"""
429-
Returns the code of the UserDataRelayInterface element.
429+
Returns the code of the ``XBeeLocalInterface`` element.
430430
431431
Returns:
432-
Integer: the code of the UserDataRelayInterface element.
432+
Integer: the code of the ``XBeeLocalInterface`` element.
433433
"""
434434
return self.__code
435435

436436
def __get_description(self):
437437
"""
438-
Returns the description of the UserDataRelayInterface element.
438+
Returns the description of the ``XBeeLocalInterface`` element.
439439
440440
Returns:
441-
String: the description of the UserDataRelayInterface element.
441+
String: the description of the ``XBeeLocalInterface`` element.
442442
"""
443443
return self.__description
444444

445445
@classmethod
446446
def get(cls, code):
447447
"""
448-
Returns the User Data Relay interface option for the given code.
448+
Returns the XBee local interface option for the given code.
449449
450450
Args:
451-
code (Integer): the code of the User Data Relay interface to get.
451+
code (Integer): the code of the XBee local interface to get.
452452
453453
Returns:
454-
:class:`.UserDataRelayInterface`: the UserDataRelayInterface with the given code,
455-
``UNKNOWN`` if there is not any UserDataRelayInterface with the provided code.
454+
:class:`.XBeeLocalInterface`: the ``XBeeLocalInterface`` with the given code,
455+
``UNKNOWN`` if there is not any ``XBeeLocalInterface`` with the provided code.
456456
"""
457457
try:
458458
return cls.lookupTable[code]
459459
except KeyError:
460-
return UserDataRelayInterface.UNKNOWN
460+
return XBeeLocalInterface.UNKNOWN
461461

462462
code = property(__get_code)
463-
"""Integer. The User Data Relay interface code."""
463+
"""Integer. The XBee local interface code."""
464464

465465
description = property(__get_description)
466-
"""String. The User Data Relay interface description."""
466+
"""String. The XBee local interface description."""
467467

468468

469-
UserDataRelayInterface.lookupTable = {x.code: x for x in UserDataRelayInterface}
470-
UserDataRelayInterface.__doc__ += utils.doc_enum(UserDataRelayInterface)
469+
XBeeLocalInterface.lookupTable = {x.code: x for x in XBeeLocalInterface}
470+
XBeeLocalInterface.__doc__ += utils.doc_enum(XBeeLocalInterface)

0 commit comments

Comments
 (0)