Exemplo n.º 1
0
    def create_packet(raw, operating_mode):
        """
        Override method.

        Returns:
            :class:`.RX64IOPacket`.

        Raises:
            InvalidPacketException: if the bytearray length is less than 20. (start delim. + length (2 bytes) + frame
                type + 64bit addr. + rssi + receive options + rf data (5 bytes) + checksum = 20 bytes)
            InvalidPacketException: if the length field of 'raw' is different than its real length. (length field: bytes
                2 and 3)
            InvalidPacketException: if the first byte of 'raw' is not the header byte. See :class:`.SpecialByte`.
            InvalidPacketException: if the calculated checksum is different than the checksum field value (last byte).
            InvalidPacketException: if the frame type is different than :attr:`.ApiFrameType.RX_IO_64`.
            InvalidOperatingModeException: if ``operating_mode`` is not supported.

        .. seealso::
           | :meth:`.XBeePacket.create_packet`
           | :meth:`.XBeeAPIPacket._check_api_packet`
        """
        if operating_mode != OperatingMode.ESCAPED_API_MODE and operating_mode != OperatingMode.API_MODE:
            raise InvalidOperatingModeException(op_mode=operating_mode)

        XBeeAPIPacket._check_api_packet(
            raw, min_length=RX64IOPacket.__MIN_PACKET_LENGTH)

        if raw[3] != ApiFrameType.RX_IO_64.code:
            raise InvalidPacketException(
                message="This packet is not an RX 64 IO packet.")

        return RX64IOPacket(XBee64BitAddress(raw[4:12]), raw[12], raw[13],
                            raw[14:-1])
Exemplo n.º 2
0
    def create_packet(raw, operating_mode):
        """
        Override method.
        
        Returns:
            :class:`.TX64Packet`.
            
        Raises:
            InvalidPacketException: if the bytearray length is less than 15. (start delim. + length (2 bytes) + frame
                type + frame id + 64bit addr. + transmit options + checksum = 15 bytes).
            InvalidPacketException: if the length field of 'raw' is different than its real length. (length field: bytes
                2 and 3)
            InvalidPacketException: if the first byte of 'raw' is not the header byte. See :class:`.SpecialByte`.
            InvalidPacketException: if the calculated checksum is different than the checksum field value (last byte).
            InvalidPacketException: if the frame type is different than :attr:`.ApiFrameType.TX_64`.
            InvalidOperatingModeException: if ``operating_mode`` is not supported.
            
        .. seealso::
           | :meth:`.XBeePacket.create_packet`
           | :meth:`.XBeeAPIPacket._check_api_packet`
        """
        if operating_mode != OperatingMode.ESCAPED_API_MODE and operating_mode != OperatingMode.API_MODE:
            raise InvalidOperatingModeException(operating_mode.name +
                                                " is not supported.")

        raw = XBeeAPIPacket._unescape_data(
            raw) if operating_mode == OperatingMode.ESCAPED_API_MODE else raw

        XBeeAPIPacket._check_api_packet(
            raw, min_length=TX64Packet.__MIN_PACKET_LENGTH)
        if raw[3] != ApiFrameType.TX_64.code:
            raise InvalidPacketException("This packet is not a TX 64 packet.")

        return TX64Packet(raw[4], XBee64BitAddress(raw[5:13]), raw[13],
                          raw[14:-1])
Exemplo n.º 3
0
    def create_packet(raw, operating_mode):
        """
        Override method.

        Returns:
            :class:`.OTAFirmwareUpdateStatusPacket`.

        Raises:
            InvalidPacketException: if the bytearray length is less than 17.
                (start delim. + length (2 bytes) + frame type
                + source 64bit addr. (8 bytes) + updater 16bit addr. (2 bytes)
                + receive options + bootloader message type + block number
                + source 64bit addr. (8 bytes) + checksum = 27 bytes).
            InvalidPacketException: if the length field of 'raw' is different
                from its real length. (length field: bytes 1 and 3)
            InvalidPacketException: if the first byte of 'raw' is not the
                header byte. See :class:`.SpecialByte`.
            InvalidPacketException: if the calculated checksum is different
                from the checksum field value (last byte).
            InvalidPacketException: if the frame type is not
                :attr:`.ApiFrameType.OTA_FIRMWARE_UPDATE_STATUS`.
            InvalidOperatingModeException: if `operating_mode` is not supported.

        .. seealso::
           | :meth:`.XBeePacket.create_packet`
           | :meth:`.XBeeAPIPacket._check_api_packet`
        """
        if operating_mode not in (OperatingMode.ESCAPED_API_MODE,
                                  OperatingMode.API_MODE):
            raise InvalidOperatingModeException(operating_mode.name +
                                                " is not supported.")

        XBeeAPIPacket._check_api_packet(
            raw, min_length=OTAFirmwareUpdateStatusPacket.__MIN_PACKET_LENGTH)

        if raw[3] != ApiFrameType.OTA_FIRMWARE_UPDATE_STATUS.code:
            raise InvalidPacketException(
                "This packet is not an OTA Firmware Update Status packet.")

        return OTAFirmwareUpdateStatusPacket(XBee64BitAddress(raw[4:12]),
                                             XBee16BitAddress(raw[12:14]),
                                             raw[14],
                                             EmberBootloaderMessageType.get(
                                                 raw[15]),
                                             raw[16],
                                             XBee64BitAddress(raw[17:25]),
                                             op_mode=operating_mode)
Exemplo n.º 4
0
    def create_packet(raw, operating_mode):
        """
        Override method.

        Returns:
            :class:`.CreateSourceRoutePacket`.

        Raises:
            InvalidPacketException: If the bytearray length is less than 18.
                (start delim. + length (2 bytes) + frame type + frame id +
                64-bit addr. + 16-bit addr. + Route command options
                + num of addrs + hops 16-bit addrs + checksum = 18 bytes).
            InvalidPacketException: If the length field of `raw` is different
                from its real length. (length field: bytes 1 and 3)
            InvalidPacketException: If the first byte of 'raw' is not the
                header byte. See :class:`.SpecialByte`.
            InvalidPacketException: If the calculated checksum is different
                from the checksum field value (last byte).
            InvalidPacketException: If the frame type is not
                :attr:`.ApiFrameType.CREATE_SOURCE_ROUTE`.
            InvalidPacketException: If the number of hops does not match with
                the number of 16-bit addresses.
            InvalidOperatingModeException: If `operating_mode` is not
                supported.

        .. seealso::
           | :meth:`.XBeePacket.create_packet`
           | :meth:`.XBeeAPIPacket._check_api_packet`
        """
        if operating_mode not in (OperatingMode.ESCAPED_API_MODE,
                                  OperatingMode.API_MODE):
            raise InvalidOperatingModeException(operating_mode.name +
                                                " is not supported.")

        XBeeAPIPacket._check_api_packet(
            raw, min_length=CreateSourceRoutePacket.__MIN_PACKET_LENGTH)

        if raw[3] != ApiFrameType.CREATE_SOURCE_ROUTE.code:
            raise InvalidPacketException(
                "This packet is not a Create Source Route packet.")

        hops = [
            XBee16BitAddress(raw[i:i + 2]) for i in range(17,
                                                          len(raw) - 1, 2)
        ]

        if raw[16] != len(hops):
            raise InvalidPacketException("Specified number of hops does not"
                                         "match with the length of addresses.")

        return CreateSourceRoutePacket(raw[4],
                                       XBee64BitAddress(raw[5:13]),
                                       XBee16BitAddress(raw[13:15]),
                                       raw[15],
                                       hops,
                                       op_mode=operating_mode)
Exemplo n.º 5
0
    def create_packet(raw, operating_mode):
        """
        Override method.

        Returns:
            :class:`.RouteRecordIndicatorPacket`.

        Raises:
            InvalidPacketException: If the bytearray length is less than 17.
                (start delim. + length (2 bytes) + frame type + 64bit addr. +
                16bit addr. + Receive options + num of addrs + checksum
                = 17 bytes).
            InvalidPacketException: If the length field of `raw` is different
                from its real length. (length field: bytes 1 and 3)
            InvalidPacketException: If the first byte of 'raw' is not the
                header byte. See :class:`.SpecialByte`.
            InvalidPacketException: If the calculated checksum is different
                from the checksum field value (last byte).
            InvalidPacketException: If the frame type is not
                :attr:`.ApiFrameType.ROUTE_RECORD_INDICATOR`.
            InvalidPacketException: If the number of hops does not match with
                the number of 16-bit addresses.
            InvalidOperatingModeException: If `operating_mode` is not
                supported.

        .. seealso::
           | :meth:`.XBeePacket.create_packet`
           | :meth:`.XBeeAPIPacket._check_api_packet`
        """
        if operating_mode not in (OperatingMode.ESCAPED_API_MODE,
                                  OperatingMode.API_MODE):
            raise InvalidOperatingModeException(operating_mode.name +
                                                " is not supported.")

        XBeeAPIPacket._check_api_packet(
            raw, min_length=RouteRecordIndicatorPacket.__MIN_PACKET_LENGTH)

        if raw[3] != ApiFrameType.ROUTE_RECORD_INDICATOR.code:
            raise InvalidPacketException(
                "This packet is not a Route Record Indicator packet.")

        hops = [
            XBee16BitAddress(raw[i:i + 2]) for i in range(16,
                                                          len(raw) - 1, 2)
        ]

        if raw[15] != len(hops):
            raise InvalidPacketException("Specified number of hops does not"
                                         "match with the length of addresses.")

        return RouteRecordIndicatorPacket(XBee64BitAddress(raw[4:12]),
                                          XBee16BitAddress(raw[12:14]),
                                          raw[14],
                                          hops,
                                          op_mode=operating_mode)
Exemplo n.º 6
0
    def send(self, remote_address: bytearray, data: typing.Union[bytearray, bytes]):
        data = XBee._normalize_data(data)
        remote_device = RemoteXBeeDevice(self._device, x64bit_addr=XBee64BitAddress(remote_address))

        id = random.randint(0, 4000000)
        init_frame = InitFrame(id=id, size=len(data))

        self._device.send_data(remote_device, init_frame.serialize())

        sent = 0
        while sent < len(data):
            s = data[sent:sent + 50]
            frame = Frame(id=id, data=s)

            self._device.send_data(remote_device, frame.serialize())
            sent += len(s)
Exemplo n.º 7
0
    def create_packet(raw, operating_mode):
        """
        Override method.

        Returns:
            :class:`.RegisterJoiningDevicePacket`.

        Raises:
            InvalidPacketException: if the bytearray length is less than 17.
                (start delim. + length (2 bytes) + frame type + frame id
                + 64-bit registrant addr. (8 bytes)
                + 16-bit registrant addr. (2 bytes) + options
                + checksum = 17 bytes).
            InvalidPacketException: if the length field of 'raw' is different
                from its real length. (length field: bytes 2 and 3)
            InvalidPacketException: if the first byte of 'raw' is not the
                header byte. See :class:`.SpecialByte`.
            InvalidPacketException: if the calculated checksum is different
                from the checksum field value (last byte).
            InvalidPacketException: if the frame type is not
                :attr:`.ApiFrameType.REGISTER_JOINING_DEVICE`.
            InvalidOperatingModeException: if `operating_mode` is not supported.

        .. seealso::
           | :meth:`.XBeePacket.create_packet`
           | :meth:`.XBeeAPIPacket._check_api_packet`
        """
        if operating_mode not in (OperatingMode.ESCAPED_API_MODE,
                                  OperatingMode.API_MODE):
            raise InvalidOperatingModeException(operating_mode.name +
                                                " is not supported.")

        XBeeAPIPacket._check_api_packet(
            raw, min_length=RegisterJoiningDevicePacket.__MIN_PACKET_LENGTH)

        if raw[3] != ApiFrameType.REGISTER_JOINING_DEVICE.code:
            raise InvalidPacketException(
                "This packet is not a Register Joining Device packet.")

        return RegisterJoiningDevicePacket(raw[4],
                                           XBee64BitAddress(raw[5:13]),
                                           RegisterKeyOptions.get(raw[15]),
                                           raw[16:-1],
                                           op_mode=operating_mode)
Exemplo n.º 8
0
    def create_packet(raw, operating_mode):
        """
        Override method.

        Returns:
            :class:`.RemoteFSResponsePacket`

        Raises:
            InvalidPacketException: If the bytearray length is less than 8 +
                the minimum length of the command.
                (start delim. + length (2 bytes) + frame type + frame id
                + 64bit addr. + receive options + fs cmd id + status
                + checksum + cmd data = 17 bytes + cmd data).
            InvalidPacketException: If the length field of 'raw' is different
                from its real length. (length field: bytes 2 and 3)
            InvalidPacketException: If the first byte of 'raw' is not the
                header byte. See :class:`.SpecialByte`.
            InvalidPacketException: If the calculated checksum is different
                from the checksum field value (last byte).
            InvalidPacketException: If the frame type is different from
                :attr:`.ApiFrameType.REMOTE_FILE_SYSTEM_RESPONSE`.
            InvalidOperatingModeException: if `operating_mode` is not supported.

        .. seealso::
           | :meth:`.XBeePacket.create_packet`
           | :meth:`.XBeeAPIPacket._check_api_packet`
        """
        if operating_mode not in (OperatingMode.ESCAPED_API_MODE,
                                  OperatingMode.API_MODE):
            raise InvalidOperatingModeException(op_mode=operating_mode)

        XBeeAPIPacket._check_api_packet(
            raw, min_length=RemoteFSResponsePacket.__MIN_PACKET_LENGTH)

        if raw[3] != ApiFrameType.REMOTE_FILE_SYSTEM_RESPONSE.code:
            raise InvalidPacketException(
                message="This packet is not a Remote File System response packet.")

        return RemoteFSResponsePacket(raw[4], XBee64BitAddress(raw[5:13]),
                                      raw[14:-1], raw[13])
Exemplo n.º 9
0
 def send(self, remote_address: bytearray, data: typing.Union[bytearray,
                                                              bytes]):
     data = self._normalize_data(data)
     remote_device = RemoteXBeeDevice(
         self._device, x64bit_addr=XBee64BitAddress(remote_address))
     self._device.send_data(remote_device, data)
Exemplo n.º 10
0
    def create_packet(raw, operating_mode):
        """
        Override method.

        Returns:
            :class:`.RouteInformationPacket`.

        Raises:
            InvalidPacketException: If the bytearray length is less than 46.
                (start delim. + length (2 bytes) + frame type + src_event
                + length + timestamp (4 bytes) + ack timeout count
                + tx blocked count + reserved + dest addr (8 bytes)
                + src addr (8 bytes) + responder addr (8 bytes)
                + successor addr (8 bytes) + checksum = 46 bytes).
            InvalidPacketException: If the length field of `raw` is different
                from its real length. (length field: bytes 1 and 3)
            InvalidPacketException: If the first byte of 'raw' is not the
                header byte. See :class:`.SpecialByte`.
            InvalidPacketException: If the calculated checksum is different
                from the checksum field value (last byte).
            InvalidPacketException: If the frame type is not
                :attr:`.ApiFrameType.DIGIMESH_ROUTE_INFORMATION`.
            InvalidPacketException: If the internal length byte of the rest
                of the frame (without the checksum) is different from its real
                length.
            InvalidOperatingModeException: If `operating_mode` is not
                supported.

        .. seealso::
           | :meth:`.XBeePacket.create_packet`
           | :meth:`.XBeeAPIPacket._check_api_packet`
        """
        if operating_mode not in (OperatingMode.ESCAPED_API_MODE,
                                  OperatingMode.API_MODE):
            raise InvalidOperatingModeException(operating_mode.name +
                                                " is not supported.")

        XBeeAPIPacket._check_api_packet(
            raw, min_length=RouteInformationPacket.__MIN_PACKET_LENGTH)

        if raw[3] != ApiFrameType.DIGIMESH_ROUTE_INFORMATION.code:
            raise InvalidPacketException(
                "This packet is not a Route Information packet.")

        # 7: frame len starting from this byte (index 5) and without the checksum
        if raw[5] != len(raw) - 7:
            raise InvalidPacketException(
                "Length does not match with the data length")

        additional_data = []
        if len(raw) > RouteInformationPacket.__MIN_PACKET_LENGTH:
            additional_data = raw[45:]
        packet = RouteInformationPacket(raw[4],
                                        utils.bytes_to_int(raw[6:10]),
                                        raw[10],
                                        raw[11],
                                        XBee64BitAddress(raw[13:21]),
                                        XBee64BitAddress(raw[21:29]),
                                        XBee64BitAddress(raw[29:37]),
                                        XBee64BitAddress(raw[37:45]),
                                        additional_data,
                                        op_mode=operating_mode)
        packet._reserved = raw[12]

        return packet
Exemplo n.º 11
0
#!./.venv/bin/python

import json
from digi.xbee.models.address import XBee16BitAddress, XBee64BitAddress
from digi.xbee.models.options import TransmitOptions, ReceiveOptions
import serial
from digi.xbee.packets.common import TransmitPacket, ReceivePacket
from digi.xbee.devices import DigiMeshDevice, RemoteDigiMeshDevice

DUMMY_ADDR = b"\x01\x02\x03\x04\x05\x06\x07\x08"
PAYLOAD = {'cmd': 0x1, 'value': ''}

if __name__ == '__main__':
    # dummy_xbee = DigiMeshDevice('/dev/ttyUSB0', 9600)
    addr = XBee64BitAddress(DUMMY_ADDR)
    # remote = RemoteDigiMeshDevice(dummy_xbee, addr, node_id="DUMMY")
    tx_options = TransmitOptions.NONE.value
    # packet = TransmitPacket(dummy_xbee._get_next_frame_id(),
    #                         addr,
    #                         XBee16BitAddress.UNKNOWN_ADDRESS,
    #                         0,
    #                         tx_options,
    #                         rf_data=b"HELLO, WORLD")

    data = json.dumps(PAYLOAD).encode('utf-8')
    packet = ReceivePacket(addr,
                           XBee16BitAddress.UNKNOWN_ADDRESS,
                           ReceiveOptions.NONE.value,
                           rf_data=data)

    with serial.Serial('/dev/ttyUSB0', 9600) as ser:
Exemplo n.º 12
0
import re
import datetime
import socket
import requests
import struct

from digi.xbee.devices import XBeeDevice
from digi.xbee.util import utils
from digi.xbee.models.address import XBee64BitAddress
from digi.xbee.models.status import PowerLevel
from digi.xbee.exception import InvalidPacketException

SERIAL_PORT = '/dev/ttyUSB0'
SERIAL_BPS = 9600

THIGH_SOURCE = XBee64BitAddress(bytearray(b'\x00\x13\xA2\x00\x41\x80\xAA\xA5'))
ECG_SOURCE = XBee64BitAddress(bytearray(b'\x00\x13\xA2\x00\x41\x80\xAA\xA6'))

REST_PORT = 3000
REST_ADDR = "18.222.128.16"

POSITION_STANDING = 0
POSITION_SQUAT = 1
POSITION_PLANK_HIGH = 2
POSITION_PLANK_LOW = 3
POSITION_UNKNOWN = 4

POSITION_LABELS = ("STANDING", "SQUATTING", "PLANK_HIGH", "PLANK_LOW",
                   "UNKOWN")