def test_is_response_parsed_as_io(self): """ I/O data in a AT response for an IS command is parsed. """ # Build IO data # One sample, ADC 0 enabled # DIO 1,3,5,7 enabled header = b'\x01\x02\xAA' # First 7 bits ignored, DIO8 low, DIO 0-7 alternating # ADC0 value of 255 sample = b'\x00\xAA\x00\xFF' data = header + sample device = Serial() device.set_read_data(APIFrame(data=b'\x88DIS\x00' + data).output()) xbee = XBee(device) info = xbee.wait_read_frame() expected_info = {'id': 'at_response', 'frame_id': b'D', 'command': b'IS', 'status': b'\x00', 'parameter': [{'dio-1': True, 'dio-3': True, 'dio-5': True, 'dio-7': True, 'adc-0': 255}]} self.assertEqual(info, expected_info)
def test_is_remote_response_parsed_as_io(self): """ I/O data in a Remote AT response for an IS command is parsed. """ # Build IO data # One sample, ADC 0 enabled # DIO 1,3,5,7 enabled header = b'\x01\x02\xAA' # First 7 bits ignored, DIO8 low, DIO 0-7 alternating # ADC0 value of 255 sample = b'\x00\xAA\x00\xFF' data = header + sample device = Serial() device.set_read_data(APIFrame( data=b'\x97D\x00\x13\xa2\x00@oG\xe4v\x1aIS\x00' + data).output() ) xbee = XBee(device, io_loop=self._patch_io) xbee._process_input(None, None) info = yield xbee.wait_read_frame() expected_info = {'id': 'remote_at_response', 'frame_id': b'D', 'source_addr_long': b'\x00\x13\xa2\x00@oG\xe4', 'source_addr': b'v\x1a', 'command': b'IS', 'status': b'\x00', 'parameter': [{'dio-1': True, 'dio-3': True, 'dio-5': True, 'dio-7': True, 'adc-0': 255}]} self.assertEqual(info, expected_info)
def setUp(self): """ Prepare a fake device to read from """ super(TestSendShorthand, self).setUp() self.ser = Serial() self.xbee = XBee(self.ser)
def test_read_io_data(self): """ XBee class should properly read and parse incoming IO data """ # Build IO data # One sample, ADC 0 enabled # DIO 1,3,5,7 enabled header = b'\x01\x02\xAA' # First 7 bits ignored, DIO8 low, DIO 0-7 alternating # ADC0 value of 255 sample = b'\x00\xAA\x00\xFF' data = header + sample # Wrap data in frame # RX frame data rx_io_resp = b'\x83\x00\x01\x28\x00' device = Serial() device.set_read_data(b'\x7E\x00\x0C' + rx_io_resp + data + b'\xfd') xbee = XBee(device) info = xbee.wait_read_frame() expected_info = {'id': 'rx_io_data', 'source_addr': b'\x00\x01', 'rssi': b'\x28', 'options': b'\x00', 'samples': [{'dio-1': True, 'dio-3': True, 'dio-5': True, 'dio-7': True, 'adc-0': 255}] } self.assertEqual(info, expected_info)
def test_send(self): """ Test send() with AT command. """ device = Serial() xbee = ZigBee(device) xbee.send('at', command='MY') result = device.get_data_written() expected = b'~\x00\x04\x08\x01MYP' self.assertEqual(result, expected)
def test_read_invalid_followed_by_valid(self): """ _wait_for_frame should skip invalid data """ device = Serial() device.set_read_data(b'\x7E\x00\x01\x00\xFA' + b'\x7E\x00\x01\x05\xFA') xbee = XBeeBase(device) frame = xbee._wait_for_frame() self.assertEqual(frame.data, b'\x05')
def test_read(self): """ _wait_for_frame should properly read a frame of data """ device = Serial() device.set_read_data(b'\x7E\x00\x01\x00\xFF') xbee = XBeeBase(device) frame = xbee._wait_for_frame() self.assertEqual(frame.data, b'\x00')
class TestFakeSerialRead(unittest.TestCase): """ Fake Serial class should work as intended to emluate reading from a serial port. """ def setUp(self): """ Create a fake read device for each test. """ self.device = Serial() self.device.set_read_data("test") def test_read_single_byte(self): """ Reading one byte at a time should work as expected. """ self.assertEqual(self.device.read(), 't') self.assertEqual(self.device.read(), 'e') self.assertEqual(self.device.read(), 's') self.assertEqual(self.device.read(), 't') def test_read_multiple_bytes(self): """ Reading multiple bytes at a time should work as expected. """ self.assertEqual(self.device.read(3), 'tes') self.assertEqual(self.device.read(), 't') def test_write(self): """ Test serial write function. """ self.device.write("Hello World") self.assertEqual(self.device.get_data_written(), "Hello World")
def test_read(self): """ _wait_for_frame should properly read a frame of data """ device = Serial() device.set_read_data(b'\x7E\x00\x01\x00\xFF') xbee = XBeeBase(device, io_loop=self._patch_io) xbee._process_input(None, None) frame = yield xbee._get_frame() self.assertEqual(frame.data, b'\x00')
def test_read_escaped(self): """ _wait_for_frame should properly read a frame of data Verify that API mode 2 escaped bytes are read correctly """ device = Serial() device.set_read_data(b'\x7E\x00\x04\x7D\x5E\x7D\x5D\x7D\x31\x7D\x33\xE0') xbee = XBeeBase(device,escaped=True) frame = xbee._wait_for_frame() self.assertEqual(frame.data, b'\x7E\x7D\x11\x13')
def test_read_invalid_followed_by_valid(self): """ _wait_for_frame should skip invalid data """ device = Serial() device.set_read_data(b'\x7E\x00\x01\x00\xFA' + b'\x7E\x00\x01\x05\xFA') xbee = XBeeBase(device, io_loop=self._patch_io) xbee._process_input(None, None) # First process ends with no good frame, process next xbee._process_input(None, None) frame = yield xbee._get_frame() self.assertEqual(frame.data, b'\x05')
def test_read_escaped(self): """ _wait_for_frame should properly read a frame of data Verify that API mode 2 escaped bytes are read correctly """ device = Serial() device.set_read_data( b'\x7E\x00\x04\x7D\x5E\x7D\x5D\x7D\x31\x7D\x33\xE0') xbee = XBeeBase(device, escaped=True) frame = xbee._wait_for_frame() self.assertEqual(frame.data, b'\x7E\x7D\x11\x13')
def test_read_at(self): """ read and parse a parameterless AT command """ device = Serial() device.set_read_data(b'\x7E\x00\x05\x88DMY\x01\x8c') xbee = XBee(device) info = xbee.wait_read_frame() expected_info = {'id': 'at_response', 'frame_id': b'D', 'command': b'MY', 'status': b'\x01'} self.assertEqual(info, expected_info)
def test_empty_frame_ignored(self): """ If an empty frame is received from a device, it must be ignored. """ device = Serial() device.set_read_data(b'\x7E\x00\x00\xFF\x7E\x00\x05\x88DMY\x01\x8c') xbee = XBee(device) info = xbee.wait_read_frame() expected_info = {'id': 'at_response', 'frame_id': b'D', 'command': b'MY', 'status': b'\x01'} self.assertEqual(info, expected_info)
def test_write_escaped(self): """ _write method should write the expected data to the serial device """ device = Serial() xbee = XBeeBase(device,escaped=True) xbee._write(b'\x7E\x01\x7D\x11\x13') # Check resuting state of fake device expected_frame = b'\x7E\x00\x05\x7D\x5E\x01\x7D\x5D\x7D\x31\x7D\x33\xDF' result_frame = device.get_data_written() self.assertEqual(result_frame, expected_frame)
def test_write_again(self): """ _write method should write the expected data to the serial device """ device = Serial() xbee = XBeeBase(device) xbee._write(b'\x00\x01\x02') # Check resuting state of fake device expected_frame = b'\x7E\x00\x03\x00\x01\x02\xFC' result_frame = device.get_data_written() self.assertEqual(result_frame, expected_frame)
def test_read_at_params_in_escaped_mode(self): """ read and parse an AT command with a parameter in escaped API mode """ device = Serial() device.set_read_data(b'~\x00\t\x88DMY\x01}^}]}1}3m') xbee = XBee(device, escaped=True) info = xbee.wait_read_frame() expected_info = {'id': 'at_response', 'frame_id': b'D', 'command': b'MY', 'status': b'\x01', 'parameter': b'\x7E\x7D\x11\x13'} self.assertEqual(info, expected_info)
def test_read_rx_with_close_brace(self): """ An rx data frame including a close brace must be read properly. """ device = Serial() device.set_read_data(APIFrame(b'\x81\x01\x02\x55\x00{test=1}').output()) xbee = XBee(device) info = xbee.wait_read_frame() expected_info = {'id': 'rx', 'source_addr': b'\x01\x02', 'rssi': b'\x55', 'options': b'\x00', 'rf_data': b'{test=1}'} self.assertEqual(info, expected_info)
def setUp(self): """ Prepare a fake device to read from """ super(TestSendShorthand, self).setUp() self.ser = Serial() self.xbee = XBee(self.ser, io_loop=self._patch_io)
def test_read_at_params(self): """ read and parse an AT command with a parameter """ device = Serial() device.set_read_data(b'\x7E\x00\x08\x88DMY\x01\x00\x00\x00\x8c') xbee = XBee(device, io_loop=self._patch_io) xbee._process_input(None, None) info = yield xbee.wait_read_frame() expected_info = {'id': 'at_response', 'frame_id': b'D', 'command': b'MY', 'status': b'\x01', 'parameter': b'\x00\x00\x00'} self.assertEqual(info, expected_info)
def test_read_at(self): """ read and parse a parameterless AT command """ device = Serial() device.set_read_data(b'\x7E\x00\x05\x88DMY\x01\x8c') xbee = XBee(device) info = xbee.wait_read_frame() expected_info = { 'id': 'at_response', 'frame_id': b'D', 'command': b'MY', 'status': b'\x01' } self.assertEqual(info, expected_info)
def test_send_at_command(self): """ calling send should write a full API frame containing the API AT command packet to the serial device. """ serial_port = Serial() xbee = XBee(serial_port) # Send an AT command xbee.send('at', frame_id=stringToBytes('A'), command=stringToBytes('MY')) # Expect a full packet to be written to the device expected_data = b'\x7E\x00\x04\x08AMY\x10' result_data = serial_port.get_data_written() self.assertEqual(result_data, expected_data)
def test_read_at_params_in_escaped_mode(self): """ read and parse an AT command with a parameter in escaped API mode """ device = Serial() device.set_read_data(b'~\x00\t\x88DMY\x01}^}]}1}3m') xbee = XBee(device, escaped=True) info = xbee.wait_read_frame() expected_info = { 'id': 'at_response', 'frame_id': b'D', 'command': b'MY', 'status': b'\x01', 'parameter': b'\x7E\x7D\x11\x13' } self.assertEqual(info, expected_info)
def test_read_rx_with_close_brace_escaped(self): """ An escaped rx data frame including a close brace must be read properly. """ device = Serial() device.set_read_data(APIFrame(b'\x81\x01\x02\x55\x00{test=1}', escaped=True).output()) xbee = XBee(device, escaped=True, io_loop=self._patch_io) xbee._process_input(None, None) info = yield xbee.wait_read_frame() expected_info = {'id': 'rx', 'source_addr': b'\x01\x02', 'rssi': b'\x55', 'options': b'\x00', 'rf_data': b'{test=1}'} self.assertEqual(info, expected_info)
def test_is_remote_response_parsed_as_io(self): """ I/O data in a Remote AT response for an IS command is parsed. """ # Build IO data # One sample, ADC 0 enabled # DIO 1,3,5,7 enabled header = b'\x01\x02\xAA' # First 7 bits ignored, DIO8 low, DIO 0-7 alternating # ADC0 value of 255 sample = b'\x00\xAA\x00\xFF' data = header + sample device = Serial() device.set_read_data( APIFrame(data=b'\x97D\x00\x13\xa2\x00@oG\xe4v\x1aIS\x00' + data).output()) xbee = XBee(device) xbee._process_input(None, None) info = yield xbee.wait_read_frame() expected_info = { 'id': 'remote_at_response', 'frame_id': b'D', 'source_addr_long': b'\x00\x13\xa2\x00@oG\xe4', 'source_addr': b'v\x1a', 'command': b'IS', 'status': b'\x00', 'parameter': [{ 'dio-1': True, 'dio-3': True, 'dio-5': True, 'dio-7': True, 'adc-0': 255 }] } self.assertEqual(info, expected_info)
def setUp(self): """ Initialize XBee object """ super(InitXBee, self).setUp() self._patch_io = ioloop.IOLoop.current() self._patch_io.add_handler = Mock() serial_port = Serial() self.xbee = XBee(serial_port, io_loop=self._patch_io)
def test_empty_frame_ignored(self): """ If an empty frame is received from a device, it must be ignored. """ device = Serial() device.set_read_data(b'\x7E\x00\x00\xFF\x7E\x00\x05\x88DMY\x01\x8c') xbee = XBee(device) xbee._process_input(None, None) xbee._process_input(None, None) info = yield xbee.wait_read_frame() expected_info = { 'id': 'at_response', 'frame_id': b'D', 'command': b'MY', 'status': b'\x01' } self.assertEqual(info, expected_info)
def test_read_at_params(self): """ read and parse an AT command with a parameter """ device = Serial() device.set_read_data(b'\x7E\x00\x08\x88DMY\x01\x00\x00\x00\x8c') xbee = XBee(device) xbee._process_input(None, None) info = yield xbee.wait_read_frame() expected_info = { 'id': 'at_response', 'frame_id': b'D', 'command': b'MY', 'status': b'\x01', 'parameter': b'\x00\x00\x00' } self.assertEqual(info, expected_info)
def test_read_rx_with_close_brace_escaped(self): """ An escaped rx data frame including a close brace must be read properly. """ device = Serial() device.set_read_data( APIFrame(b'\x81\x01\x02\x55\x00{test=1}', escaped=True).output()) xbee = XBee(device, escaped=True) info = xbee.wait_read_frame() expected_info = { 'id': 'rx', 'source_addr': b'\x01\x02', 'rssi': b'\x55', 'options': b'\x00', 'rf_data': b'{test=1}' } self.assertEqual(info, expected_info)
def test_read_rx_with_close_brace(self): """ An rx data frame including a close brace must be read properly. """ device = Serial() device.set_read_data( APIFrame(b'\x81\x01\x02\x55\x00{test=1}').output()) xbee = XBee(device, io_loop=self._patch_io) xbee._process_input(None, None) info = yield xbee.wait_read_frame() expected_info = { 'id': 'rx', 'source_addr': b'\x01\x02', 'rssi': b'\x55', 'options': b'\x00', 'rf_data': b'{test=1}' } self.assertEqual(info, expected_info)
def test_read_io_data(self): """ XBee class should properly read and parse incoming IO data """ # Build IO data # One sample, ADC 0 enabled # DIO 1,3,5,7 enabled header = b'\x01\x02\xAA' # First 7 bits ignored, DIO8 low, DIO 0-7 alternating # ADC0 value of 255 sample = b'\x00\xAA\x00\xFF' data = header + sample # Wrap data in frame # RX frame data rx_io_resp = b'\x83\x00\x01\x28\x00' device = Serial() device.set_read_data(b'\x7E\x00\x0C' + rx_io_resp + data + b'\xfd') xbee = XBee(device) xbee._process_input(None, None) info = yield xbee.wait_read_frame() expected_info = { 'id': 'rx_io_data', 'source_addr': b'\x00\x01', 'rssi': b'\x28', 'options': b'\x00', 'samples': [{ 'dio-1': True, 'dio-3': True, 'dio-5': True, 'dio-7': True, 'adc-0': 255 }] } self.assertEqual(info, expected_info)
def test_is_response_parsed_as_io(self): """ I/O data in a AT response for an IS command is parsed. """ ## Build IO data # One sample, ADC 0 enabled # DIO 1,3,5,7 enabled header = b'\x01\x02\xAA' # First 7 bits ignored, DIO8 low, DIO 0-7 alternating # ADC0 value of 255 sample = b'\x00\xAA\x00\xFF' data = header + sample device = Serial() device.set_read_data(APIFrame(data=b'\x88DIS\x00' + data).output()) xbee = XBee(device) info = xbee.wait_read_frame() expected_info = { 'id': 'at_response', 'frame_id': b'D', 'command': b'IS', 'status': b'\x00', 'parameter': [{ 'dio-1': True, 'dio-3': True, 'dio-5': True, 'dio-7': True, 'adc-0': 255 }] } self.assertEqual(info, expected_info)
class TestSendShorthand(unittest.TestCase): """ Tests shorthand for sending commands to an XBee provided by XBee.__getattr__ """ def setUp(self): """ Prepare a fake device to read from """ self.ser = Serial() self.xbee = XBee(self.ser) def test_send_at_command(self): """ Send an AT command with a shorthand call """ # Send an AT command self.xbee.at(frame_id=stringToBytes('A'), command=stringToBytes('MY')) # Expect a full packet to be written to the device result_data = self.ser.get_data_written() expected_data = b'\x7E\x00\x04\x08AMY\x10' self.assertEqual(result_data, expected_data) def test_send_at_command_with_param(self): """ calling send should write a full API frame containing the API AT command packet to the serial device. """ # Send an AT command self.xbee.at(frame_id=stringToBytes('A'), command=stringToBytes('MY'), parameter=b'\x00\x00') # Expect a full packet to be written to the device result_data = self.ser.get_data_written() expected_data = b'\x7E\x00\x06\x08AMY\x00\x00\x10' self.assertEqual(result_data, expected_data) def test_send_tx_with_close_brace(self): """ Calling tx where the given data string includes a close brace '}' must write correctly. """ self.xbee.tx(dest_addr=b'\x01\x02', data=b'{test=1}') result_data = self.ser.get_data_written() expected_data = b'\x7E\x00\x0D\x01\x00\x01\x02\x00{test=1}\xD5' self.assertEqual(result_data, expected_data) def test_shorthand_disabled(self): """ When shorthand is disabled, any attempt at calling a non-existant attribute should raise AttributeError """ self.xbee = XBee(self.ser, shorthand=False) try: self.xbee.at except AttributeError: pass else: self.fail("Specified shorthand command should not exist")
def setUp(self): """ Create a fake read device for each test. """ self.device = Serial() self.device.set_read_data("test")
def setUp(self): self.xbee = None self.serial = Serial() self.callback = lambda data: None self.error_callback = lambda data: None
def setUp(self): super(TestDigiMesh, self).setUp() patch_io = ioloop.IOLoop.current() patch_io.add_handler = Mock() serial_port = Serial() self.digimesh = DigiMesh(serial_port, io_loop=patch_io)
class TestSendShorthand(InitXBee): """ Tests shorthand for sending commands to an XBee provided by XBee.__getattr__ """ def setUp(self): """ Prepare a fake device to read from """ super(TestSendShorthand, self).setUp() self.ser = Serial() self.xbee = XBee(self.ser) def test_send_at_command(self): """ Send an AT command with a shorthand call """ # Send an AT command self.xbee.at(frame_id=stringToBytes('A'), command=stringToBytes('MY')) # Expect a full packet to be written to the device result_data = self.ser.get_data_written() expected_data = b'\x7E\x00\x04\x08AMY\x10' self.assertEqual(result_data, expected_data) def test_send_at_command_with_param(self): """ calling send should write a full API frame containing the API AT command packet to the serial device. """ # Send an AT command self.xbee.at(frame_id=stringToBytes('A'), command=stringToBytes('MY'), parameter=b'\x00\x00') # Expect a full packet to be written to the device result_data = self.ser.get_data_written() expected_data = b'\x7E\x00\x06\x08AMY\x00\x00\x10' self.assertEqual(result_data, expected_data) def test_send_tx_with_close_brace(self): """ Calling tx where the given data string includes a close brace '}' must write correctly. """ self.xbee.tx(dest_addr=b'\x01\x02', data=b'{test=1}') result_data = self.ser.get_data_written() expected_data = b'\x7E\x00\x0D\x01\x00\x01\x02\x00{test=1}\xD5' self.assertEqual(result_data, expected_data) def test_shorthand_disabled(self): """ When shorthand is disabled, any attempt at calling a non-existant attribute should raise AttributeError """ self.xbee = XBee(self.ser, shorthand=False) try: self.xbee.at except AttributeError: pass else: self.fail("Specified shorthand command should not exist")
def setUp(self): super(TestZigBee, self).setUp() self._patch_io = ioloop.IOLoop.current() self._patch_io.add_handler = Mock() serial_port = Serial() self.zigbee = ZigBee(serial_port, io_loop=self._patch_io)
def setUp(self): """ Prepare a fake device to read from """ self.ser = Serial() self.xbee = XBee(self.ser)