004-message_receiver.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import pathlib
  2. test_file = pathlib.Path(__file__)
  3. test_dir = test_file.parent
  4. project_dir = test_dir.parent
  5. import sys
  6. sys.path.append(str(project_dir.absolute()))
  7. import source as communication
  8. class fail_message(communication.message):
  9. _type = 0x0012
  10. def __init__(self) -> None:
  11. super().__init__()
  12. self._add_field(communication.field("test", int, 1))
  13. def main():
  14. print("Starting test.")
  15. print("Registering base messages.")
  16. receiver = communication.message_receiver()
  17. receiver.register(communication.position_message)
  18. receiver.register(communication.state_message)
  19. receiver.register(communication.control_message)
  20. print("Trying to register bad message.")
  21. try:
  22. receiver.register(fail_message)
  23. except Exception as error:
  24. print("Work, raised: " + str(error))
  25. print("Creating sample message to receive.")
  26. sample = communication.state_message()
  27. sample.set_ignition(True)
  28. sample.set_leds(0x20)
  29. coded = sample.encoder().code()
  30. print("Encoded.")
  31. print("Trying to receive it.")
  32. received = receiver.receive(coded)
  33. print("Type of received message: " + str(type(received)))
  34. print("Checking fields:")
  35. try:
  36. if received.get_ignition() != True:
  37. raise RuntimeError("Bad decoded ignition field.")
  38. if received.get_leds() != 0x20:
  39. raise RuntimeError("Bad decoded leds field.")
  40. print("All fields are good.")
  41. except Exception as error:
  42. print("Can not check, error: " + str(error))
  43. if __name__ == "__main__":
  44. main()