From 0304a6a932f76857a706deea9c737540470de624 Mon Sep 17 00:00:00 2001 From: Andrew Kay Date: Tue, 17 Dec 2019 22:34:09 -0600 Subject: [PATCH] Fix terminal model mapping --- pycoax/coax/protocol.py | 17 ++++++++++++++++- pycoax/tests/test_protocol.py | 31 ++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/pycoax/coax/protocol.py b/pycoax/coax/protocol.py index c488027..0463e45 100644 --- a/pycoax/coax/protocol.py +++ b/pycoax/coax/protocol.py @@ -76,15 +76,30 @@ class KeystrokePollResponse(PollResponse): class TerminalId: """Terminal model and keyboard.""" + _MODEL_MAP = { + 0b010: 2, + 0b011: 3, + 0b111: 4, + 0b110: 5 + } + def __init__(self, value): if (value & 0x1) != 0: raise ValueError('Invalid terminal identifier') self.value = value - self.model = (value & 0x0e) >> 1 + model = (value & 0x0e) >> 1 + + if model not in TerminalId._MODEL_MAP: + raise ValueError('Invalid model') + + self.model = TerminalId._MODEL_MAP[model] self.keyboard = (value & 0xf0) >> 4 + def __repr__(self): + return f'' + def poll(interface, **kwargs): """Execute a POLL command.""" response = _execute_read_command(interface, Command.POLL, allow_trta_response=True, diff --git a/pycoax/tests/test_protocol.py b/pycoax/tests/test_protocol.py index b15d72a..f8c6b9e 100644 --- a/pycoax/tests/test_protocol.py +++ b/pycoax/tests/test_protocol.py @@ -4,7 +4,7 @@ from unittest.mock import Mock import context from coax import PollResponse, KeystrokePollResponse, ProtocolError -from coax.protocol import Command, _execute_read_command, _execute_write_command, _pack_command_word, _unpack_data_words, _unpack_data_word +from coax.protocol import Command, TerminalId, _execute_read_command, _execute_write_command, _pack_command_word, _unpack_data_words, _unpack_data_word class PollResponseTestCase(unittest.TestCase): def test_is_power_on_reset_complete(self): @@ -25,6 +25,35 @@ class KeystrokePollResponseTestCase(unittest.TestCase): with self.assertRaisesRegex(ValueError, 'Invalid keystroke poll response'): response = KeystrokePollResponse(0b0000001000) +class TerminalIdTestCase(unittest.TestCase): + def test_model_2(self): + terminal_id = TerminalId(0b00000100) + + self.assertEqual(terminal_id.model, 2) + + def test_model_3(self): + terminal_id = TerminalId(0b00000110) + + self.assertEqual(terminal_id.model, 3) + + def test_model_4(self): + terminal_id = TerminalId(0b00001110) + + self.assertEqual(terminal_id.model, 4) + + def test_model_5(self): + terminal_id = TerminalId(0b00001100) + + self.assertEqual(terminal_id.model, 5) + + def test_invalid_identifier(self): + with self.assertRaisesRegex(ValueError, 'Invalid terminal identifier'): + terminal_id = TerminalId(0b00000001) + + def test_invalid_model(self): + with self.assertRaisesRegex(ValueError, 'Invalid model'): + terminal_id = TerminalId(0b00000000) + class ExecuteReadCommandTestCase(unittest.TestCase): def setUp(self): self.interface = Mock()