def rx_msg(msg_code: str, msg: str, notifier: Notifier, zeros="00"): """ Create a Elk received message, pass it to decode, and invoke the notifiers which will update the base element. """ data = f"{len(msg)+len(zeros)+4:02X}{msg_code}{msg}{zeros}" cksum = (256 - reduce(lambda x, y: x + y, map(ord, data))) % 256 decoded = decode(f"{data}{cksum:02X}") if decoded: notifier.notify(decoded[0], decoded[1])
def test_decode_login_success(): decoded = m.decode("Login successful") assert decoded assert decoded[0] == "login" assert decoded[1] == {"succeeded": True}
def test_decode_login_failed(): decoded = m.decode("Username/Password not found") assert decoded assert decoded[0] == "login" assert decoded[1] == {"succeeded": False}
def test_decode_raises_value_error_on_short_message(): with pytest.raises(ValueError) as excinfo: m.decode("X") assert str(excinfo.value) == "Message invalid"
def test_decode_raises_value_error_on_bad_checksum(): with pytest.raises(ValueError) as excinfo: m.decode("0DCV01000990042") assert str(excinfo.value).startswith("Bad checksum")
def test_decode_raises_value_error_on_length_too_short(): with pytest.raises(ValueError) as excinfo: m.decode("02CV01000990030") assert str(excinfo.value).startswith("Incorrect message length")
def test_decode_calls_unknown_handler_on_bad_command_or_not_implemented(): decoded = m.decode("08XXtest28") assert decoded assert decoded[0] == "unknown" assert decoded[1] == {"msg_code": "XX", "data": "test"}
def test_decode_raises_value_error_on_bad_message(): with pytest.raises(ValueError): m.decode("a really really bad message")