1- # Copyright 2017, Digi International Inc.
1+ # Copyright 2017, 2018, Digi International Inc.
22#
33# This Source Code Form is subject to the terms of the Mozilla Public
44# License, v. 2.0. If a copy of the MPL was not distributed with this
2424
2525from digi .xbee .packets .cellular import TXSMSPacket
2626from digi .xbee .models .accesspoint import AccessPoint , WiFiEncryptionType
27- from digi .xbee .models .atcomm import SpecialByte
2827from digi .xbee .models .hw import HardwareVersion
2928from digi .xbee .models .mode import OperatingMode , APIOutputMode , IPAddressingMode
3029from digi .xbee .models .address import XBee64BitAddress , XBee16BitAddress , XBeeIMEIAddress
3433from digi .xbee .models .status import ATCommandStatus , TransmitStatus , PowerLevel , \
3534 ModemStatus , CellularAssociationIndicationStatus , WiFiAssociationIndicationStatus , AssociationIndicationStatus ,\
3635 NetworkDiscoveryStatus
37- from digi .xbee .packets import factory
3836from digi .xbee .packets .aft import ApiFrameType
3937from digi .xbee .packets .common import ATCommPacket , TransmitPacket , RemoteATCommandPacket , ExplicitAddressingPacket
4038from digi .xbee .packets .network import TXIPv4Packet
@@ -108,6 +106,8 @@ def __init__(self, local_xbee_device=None, serial_port=None, sync_ops_timeout=_D
108106 self ._protocol = None
109107 self ._node_id = None
110108
109+ self ._packet_listener = None
110+
111111 self ._log .addHandler (logging .NullHandler ())
112112
113113 self .__generic_lock = threading .Lock ()
@@ -1055,13 +1055,14 @@ def _get_packet_by_id(self, frame_id):
10551055 ValueError: if ``frame_id`` is less than 0 or greater than 255.
10561056 TimeoutException: if there was not any XBee packet matching the provided frame ID that could be read.
10571057 """
1058- operating_mode = OperatingMode .API_MODE if self .is_remote () else self ._operating_mode
10591058 if not (0 <= frame_id <= 255 ):
10601059 raise ValueError ("Frame ID must be between 0 and 255." )
1061- packet_read = factory .build_frame (self .__read_next_packet (), operating_mode )
1062- while not packet_read .needs_id () or packet_read .frame_id != frame_id :
1063- packet_read = factory .build_frame (self .__read_next_packet (), operating_mode )
1064- return packet_read
1060+
1061+ queue = self ._packet_listener .get_queue ()
1062+
1063+ packet = queue .get_by_id (frame_id , XBeeDevice .TIMEOUT_READ_PACKET )
1064+
1065+ return packet
10651066
10661067 @staticmethod
10671068 def __is_api_packet (xbee_packet ):
@@ -1079,31 +1080,6 @@ def __is_api_packet(xbee_packet):
10791080 return False
10801081 return True
10811082
1082- def __read_next_packet (self ):
1083- """
1084- Reads the next XBee packet. Starts to read when finds the start delimiter.
1085- The last byte read is the checksum.
1086-
1087- If there is something in the COM port buffer before the
1088- start delimiter, this method discards it.
1089-
1090- Returns:
1091- :class:XBeePacket: the next XBee packet read.
1092-
1093- Raises:
1094- TimeoutException: if it could not read any new XBee packet.
1095- """
1096- xbee_packet = bytearray (1 )
1097- xbee_packet [0 ] = self ._serial_port .read_byte ()
1098- while xbee_packet [0 ] != SpecialByte .HEADER_BYTE .code :
1099- xbee_packet [0 ] = self ._serial_port .read_byte ()
1100- packet_length = self ._serial_port .read_bytes (2 )
1101- xbee_packet += packet_length
1102- length = utils .length_to_int (packet_length )
1103- xbee_packet += self ._serial_port .read_bytes (length )
1104- xbee_packet .append (self ._serial_port .read_byte ())
1105- return xbee_packet
1106-
11071083 def __get_log (self ):
11081084 """
11091085 Returns the XBee device log.
@@ -1147,7 +1123,7 @@ class XBeeDevice(AbstractXBeeDevice):
11471123 Timeout to wait when resetting the module.
11481124 """
11491125
1150- _TIMEOUT_READ_PACKET = 3 # seconds
1126+ TIMEOUT_READ_PACKET = 3 # seconds
11511127 """
11521128 Timeout to read packets.
11531129 """
@@ -1203,7 +1179,6 @@ def __init__(self, port, baud_rate, data_bits=serial.EIGHTBITS, stop_bits=serial
12031179
12041180 self ._network = XBeeNetwork (self )
12051181
1206- self ._packet_listener = None
12071182 self .__packet_queue = None
12081183 self .__data_queue = None
12091184 self .__explicit_queue = None
@@ -2463,7 +2438,7 @@ def send_packet(self, packet, sync=False):
24632438
24642439 This method can be synchronous or asynchronous.
24652440
2466- If is synchronous, this method will discards all response
2441+ If is synchronous, this method will discard all response
24672442 packets until it finds the one that has the appropriate frame ID,
24682443 that is, the sent packet's frame ID.
24692444
@@ -3481,7 +3456,7 @@ def send_ip_data_broadcast(self, dest_port, data):
34813456 return self .send_ip_data (IPv4Address (self .BROADCAST_IP ), dest_port , IPProtocol .UDP , data )
34823457
34833458 @AbstractXBeeDevice ._before_send_method
3484- def read_ip_data (self , timeout = XBeeDevice ._TIMEOUT_READ_PACKET ):
3459+ def read_ip_data (self , timeout = XBeeDevice .TIMEOUT_READ_PACKET ):
34853460 """
34863461 Reads new IP data received by this XBee device during the
34873462 provided timeout.
@@ -3512,7 +3487,7 @@ def read_ip_data(self, timeout=XBeeDevice._TIMEOUT_READ_PACKET):
35123487 return self .__read_ip_data_packet (timeout )
35133488
35143489 @AbstractXBeeDevice ._before_send_method
3515- def read_ip_data_from (self , ip_addr , timeout = XBeeDevice ._TIMEOUT_READ_PACKET ):
3490+ def read_ip_data_from (self , ip_addr , timeout = XBeeDevice .TIMEOUT_READ_PACKET ):
35163491 """
35173492 Reads new IP data received from the given IP address during the
35183493 provided timeout.
0 commit comments