From 62a054ae1220cd67ee928d80046c022154dbccf1 Mon Sep 17 00:00:00 2001 From: Andrew Kay Date: Mon, 11 May 2020 20:19:06 -0500 Subject: [PATCH] Add terminal type to TerminalId (CUT and DFT) --- pycoax/coax/__init__.py | 1 + pycoax/coax/protocol.py | 31 ++++++++++++++++++++++--------- pycoax/tests/test_protocol.py | 23 ++++++++++++++++------- 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/pycoax/coax/__init__.py b/pycoax/coax/__init__.py index efc9602..2fe13ba 100644 --- a/pycoax/coax/__init__.py +++ b/pycoax/coax/__init__.py @@ -7,6 +7,7 @@ from .protocol import ( PollResponse, PowerOnResetCompletePollResponse, KeystrokePollResponse, + TerminalType, Control, SecondaryControl, poll, diff --git a/pycoax/coax/protocol.py b/pycoax/coax/protocol.py index f9e1317..b70a4aa 100644 --- a/pycoax/coax/protocol.py +++ b/pycoax/coax/protocol.py @@ -97,6 +97,12 @@ class Status: f'feature_error={self.feature_error}, ' f'operation_complete={self.operation_complete}>') +class TerminalType(Enum): + """Terminal type.""" + + CUT = 1 + DFT = 2 + class TerminalId: """Terminal model and keyboard.""" @@ -108,21 +114,28 @@ class TerminalId: } def __init__(self, value): - if (value & 0x1) != 0: - raise ValueError('Invalid terminal identifier') - self.value = value - model = (value & 0x0e) >> 1 + if (value & 0x1) == 0: + self.type = TerminalType.CUT - if model not in TerminalId._MODEL_MAP: - raise ValueError('Invalid model') + model = (value & 0x0e) >> 1 - self.model = TerminalId._MODEL_MAP[model] - self.keyboard = (value & 0xf0) >> 4 + if model not in TerminalId._MODEL_MAP: + raise ValueError('Invalid model') + + self.model = TerminalId._MODEL_MAP[model] + self.keyboard = (value & 0xf0) >> 4 + elif value == 1: + self.type = TerminalType.DFT + self.model = None + self.keyboard = None + else: + raise ValueError('Invalid terminal identifier') def __repr__(self): - return f'' + return (f'') class Control: """Terminal control register.""" diff --git a/pycoax/tests/test_protocol.py b/pycoax/tests/test_protocol.py index ee2088a..2cfe4c1 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, Status, TerminalId, Control, SecondaryControl, pack_command_word, unpack_command_word, pack_data_word, unpack_data_word, pack_data_words, unpack_data_words, _execute_read_command, _execute_write_command +from coax.protocol import Command, Status, TerminalType, TerminalId, Control, SecondaryControl, pack_command_word, unpack_command_word, pack_data_word, unpack_data_word, pack_data_words, unpack_data_words, _execute_read_command, _execute_write_command class PollResponseTestCase(unittest.TestCase): def test_is_power_on_reset_complete(self): @@ -35,31 +35,40 @@ class StatusTestCase(unittest.TestCase): self.assertTrue(status.operation_complete) class TerminalIdTestCase(unittest.TestCase): - def test_model_2(self): + def test_cut_model_2(self): terminal_id = TerminalId(0b00000100) + self.assertEqual(terminal_id.type, TerminalType.CUT) self.assertEqual(terminal_id.model, 2) - def test_model_3(self): + def test_cut_model_3(self): terminal_id = TerminalId(0b00000110) + self.assertEqual(terminal_id.type, TerminalType.CUT) self.assertEqual(terminal_id.model, 3) - def test_model_4(self): + def test_cut_model_4(self): terminal_id = TerminalId(0b00001110) + self.assertEqual(terminal_id.type, TerminalType.CUT) self.assertEqual(terminal_id.model, 4) - def test_model_5(self): + def test_cut_model_5(self): terminal_id = TerminalId(0b00001100) + self.assertEqual(terminal_id.type, TerminalType.CUT) self.assertEqual(terminal_id.model, 5) + def test_dft(self): + terminal_id = TerminalId(0b00000001) + + self.assertEqual(terminal_id.type, TerminalType.DFT) + def test_invalid_identifier(self): with self.assertRaisesRegex(ValueError, 'Invalid terminal identifier'): - terminal_id = TerminalId(0b00000001) + terminal_id = TerminalId(0b00000011) - def test_invalid_model(self): + def test_invalid_cut_model(self): with self.assertRaisesRegex(ValueError, 'Invalid model'): terminal_id = TerminalId(0b00000000)