nbus_generic_port.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. from abc import ABCMeta, abstractmethod
  2. from typing import Annotated
  3. from beartype import beartype
  4. from beartype.vale import Is
  5. from nbus_types.nbus_address_type import NBusModuleAddress, NBusSensorAddress
  6. from nbus_types.nbus_command_type import NBusCommand
  7. """
  8. NBus delay typedef
  9. """
  10. NBusDelay = Annotated[float, Is[lambda s: s >= 0]]
  11. @beartype
  12. class NBusPort(metaclass=ABCMeta):
  13. """
  14. Class for generic NBus communication port.
  15. """
  16. @abstractmethod
  17. def open(self) -> None:
  18. """
  19. Open communication port.
  20. """
  21. pass
  22. @abstractmethod
  23. def close(self) -> None:
  24. """
  25. Close communication port.
  26. """
  27. pass
  28. @abstractmethod
  29. def is_connected(self) -> bool:
  30. """
  31. Return connection status.
  32. :return: status (1 = connected, 0 = not connected)
  33. """
  34. pass
  35. @abstractmethod
  36. def request_broadcast(self, command: NBusCommand, data: bytearray) -> None:
  37. """
  38. Make broadcast request to nbus network.
  39. :param command: command id
  40. :param data: command data to send
  41. """
  42. pass
  43. @abstractmethod
  44. def request_module(self, module_addr: NBusModuleAddress, command: NBusCommand, data: bytearray,
  45. long_answer: NBusDelay = 0.0) -> bytearray:
  46. """
  47. Make module request to nbus network.
  48. :param module_addr: address of module
  49. :param command: command id
  50. :param data: command data to send
  51. :param long_answer: delay in s for longer answer
  52. :return: | payload length | payload |
  53. """
  54. pass
  55. @abstractmethod
  56. def request_sensor(self, module_addr: NBusModuleAddress, sensor_address: NBusSensorAddress, command: NBusCommand,
  57. data: bytearray, long_answer: NBusDelay = 0.0) -> bytearray:
  58. """
  59. Make sensor request to nbus network.
  60. :param module_addr: address of module
  61. :param sensor_address: address of sensor
  62. :param command: command id
  63. :param data: command data to send
  64. :param long_answer: delay in s for longer answer
  65. :return: | payload length | payload |
  66. """
  67. pass