import pathlib test_file = pathlib.Path(__file__) test_dir = test_file.parent project_dir = test_dir.parent import sys sys.path.append(str(project_dir.absolute())) import source as communication class fail_message(communication.message): _type = 0x0012 def __init__(self) -> None: super().__init__() self._add_field(communication.field("test", int, 1)) def main(): print("Starting test.") print("Registering base messages.") receiver = communication.message_receiver() receiver.register(communication.position_message) receiver.register(communication.state_message) receiver.register(communication.control_message) print("Trying to register bad message.") try: receiver.register(fail_message) except Exception as error: print("Work, raised: " + str(error)) print("Creating sample message to receive.") sample = communication.state_message() sample.set_ignition(True) sample.set_leds(0x20) coded = sample.encoder().code() print("Encoded.") print("Trying to receive it.") received = receiver.receive(coded) print("Type of received message: " + str(type(received))) print("Checking fields:") try: if received.get_ignition() != True: raise RuntimeError("Bad decoded ignition field.") if received.get_leds() != 0x20: raise RuntimeError("Bad decoded leds field.") print("All fields are good.") except Exception as error: print("Can not check, error: " + str(error)) if __name__ == "__main__": main()