nbus_sensor.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. import struct
  2. from nbus_hal.nbus_generic_port import NBusPort
  3. from nbus_types.nbus_command_type import NBusCommand
  4. from nbus_types.nbus_data_fomat import *
  5. from nbus_types.nbus_exceptions.nbus_api_exception import NBusErrorAPI, NBusErrorAPIType
  6. from nbus_types.nbus_parameter_type import *
  7. from nbus_types.nbus_address_type import NBusSensorAddress, NBusModuleAddress
  8. from nbus_types.nbus_sensor_type import NBusSensorType
  9. from nbus_api.nbus_common_parser import NbusCommonParser
  10. from nbus_types.nbus_status_type import NBusStatusType
  11. @beartype
  12. class NBusSensor:
  13. """
  14. Class representing nBus sensor type.
  15. """
  16. def __init__(self, address: NBusSensorAddress):
  17. """
  18. Constructor.
  19. :param address: device address
  20. """
  21. self.__address = address
  22. self.__type = None
  23. self.__module_address = None
  24. self.__port = None
  25. self.__data_format = None
  26. self.__params = {}
  27. """
  28. ================================================================================================================
  29. Public Fields
  30. ================================================================================================================
  31. """
  32. @property
  33. def address(self):
  34. """
  35. Get sensor address.
  36. :return: sensor address
  37. """
  38. return self.__address
  39. @property
  40. def data_format(self):
  41. """
  42. Get data format.
  43. :return: data format
  44. """
  45. return self.__data_format
  46. @data_format.setter
  47. def data_format(self, data_format: NBusDataFormat):
  48. """
  49. Set data format.
  50. :param data_format: format of data
  51. """
  52. self.__data_format = data_format
  53. @property
  54. def parameters(self) -> dict[NBusParameterID, NBusParameterValue]:
  55. """
  56. Get parameters.
  57. :return: parameters
  58. """
  59. return self.__params
  60. @parameters.setter
  61. def parameters(self, values: dict[NBusParameterID, NBusParameterValue]):
  62. """
  63. Set parameters.
  64. :param values: values to set
  65. """
  66. self.__params = values
  67. """
  68. ================================================================================================================
  69. Module-only Methods
  70. ================================================================================================================
  71. """
  72. def set_parent_module_address(self, address: NBusModuleAddress) -> None:
  73. """
  74. Set address of parent module.
  75. :param address: module address
  76. """
  77. self.__module_address = address
  78. def set_device_port(self, port: NBusPort) -> None:
  79. """
  80. Set device communication port.
  81. :param port: communicaiton port
  82. """
  83. self.__port = port
  84. """
  85. ================================================================================================================
  86. Device Get Commands
  87. ================================================================================================================
  88. """
  89. def cmd_get_param(self, parameter: NBusParameterID) -> NBusParameterValue:
  90. """
  91. Get single sensor parameter.
  92. :param parameter: parameter id
  93. :return: parameter value
  94. """
  95. # get response
  96. resp_len, *response = self.__port.request_sensor(self.__module_address, self.__address,
  97. NBusCommand.CMD_GET_PARAM, bytearray([parameter.value]))
  98. # parse parameter
  99. param_id, param_val = NbusCommonParser.parameters_from_response(resp_len, response)[0]
  100. # store parameter value
  101. self.__params[param_id] = param_val
  102. return param_val
  103. def cmd_get_all_params(self) -> dict[NBusParameterID, NBusParameterValue]:
  104. """
  105. Get all sensor parameters.
  106. :return: dict of parameter id and parameter value
  107. """
  108. # get response
  109. resp_len, *response = self.__port.request_sensor(self.__module_address, self.__address,
  110. NBusCommand.CMD_GET_PARAM, bytearray([]))
  111. # parse parameters
  112. params = NbusCommonParser.parameters_from_response(resp_len, response)
  113. for param_id, param_val in params:
  114. # store parameters
  115. self.__params[param_id] = param_val
  116. return self.__params.copy()
  117. def cmd_get_data(self) -> list[NBusDataValue]:
  118. """
  119. Get data from sensor.
  120. :raises
  121. :return: dict of device addresses and data values
  122. """
  123. if self.__data_format is None: # check for format and params
  124. raise NBusErrorAPI(NBusErrorAPIType.FORMAT_NOT_LOADED)
  125. _, *resp = self.__port.request_sensor(self.__module_address, self.__address, NBusCommand.CMD_GET_DATA,
  126. bytearray([]))
  127. values, _ = NbusCommonParser.data_from_response(self.data_format, resp)
  128. return values
  129. def cmd_get_sensor_type(self) -> NBusSensorType:
  130. """
  131. Get sensor type.
  132. :return: sensor type
  133. """
  134. _, *response = self.__port.request_sensor(self.__module_address, self.__address,
  135. NBusCommand.CMD_GET_SENSOR_TYPE, bytearray([]))
  136. self.__type = response[0]
  137. return NBusSensorType(self.__type)
  138. def cmd_get_format(self):
  139. """
  140. Get sensor format.
  141. :return: sensor format
  142. """
  143. _, *response = self.__port.request_sensor(self.__module_address, self.__address, NBusCommand.CMD_GET_FORMAT,
  144. bytearray([]))
  145. self.data_format = NbusCommonParser.format_from_response(response)
  146. return self.data_format
  147. """
  148. ================================================================================================================
  149. Device Set Commands
  150. ================================================================================================================
  151. """
  152. def cmd_set_find(self, enable: bool) -> NBusStatusType:
  153. """
  154. Turn on/off physical indicator of device.
  155. :param enable: to start (True) / stop (False) finding
  156. :return: status
  157. """
  158. _, *response = self.__port.request_sensor(self.__module_address, self.__address,
  159. NBusCommand.CMD_SET_FIND, bytearray([enable]))
  160. return NBusStatusType(response[0])
  161. def cmd_set_param(self, param: NBusParameterID, value: NBusParameterValue) -> NBusStatusType:
  162. """
  163. Set sensor parameter.
  164. :param param: parameter ID
  165. :param value: parameter value
  166. :return: status
  167. """
  168. # create request packet
  169. param_id_raw = struct.pack("B", param.value)
  170. param_val_raw = struct.pack("<I", value)
  171. param_bytes = bytearray(param_id_raw) + bytearray(param_val_raw)
  172. # proceed request
  173. _, *response = self.__port.request_sensor(self.__module_address, self.__address,
  174. NBusCommand.CMD_SET_PARAM, param_bytes)
  175. # if response is valid, store parameter
  176. if response[0] == param.value and response[1] == NBusStatusType.STATUS_SUCCESS:
  177. self.__params[param] = value
  178. return NBusStatusType(response[1])
  179. def cmd_set_multi_params(self, params: dict[NBusParameterID, NBusParameterValue]) \
  180. -> dict[NBusParameterID, NBusStatusType]:
  181. """
  182. Set multiple sensor parameters.
  183. :param params: parameters
  184. :return: dict od statuses
  185. """
  186. # create request packet
  187. param_bytes = NbusCommonParser.parameters_to_request(params)
  188. # send request
  189. resp_length, *response = self.__port.request_sensor(self.__module_address, self.__address,
  190. NBusCommand.CMD_SET_PARAM, bytearray(param_bytes))
  191. # parse statuses
  192. statuses = {}
  193. for i in range(0, resp_length - 1, 2):
  194. p_id = NBusParameterID(response[i])
  195. if response[i + 1] == NBusStatusType.STATUS_SUCCESS:
  196. self.__params[p_id] = params[p_id] # if success, store param
  197. statuses[p_id] = NBusStatusType(response[i + 1])
  198. return statuses
  199. def cmd_set_calibrate(self) -> NBusStatusType:
  200. """
  201. Send calibration command.
  202. :return: calibration status
  203. """
  204. resp_length, *response = self.__port.request_sensor(self.__module_address, self.__address,
  205. NBusCommand.CMD_SET_CALIBRATE, bytearray([]))
  206. return NBusStatusType(response[0])
  207. def cmd_set_data(self, data: list[NBusDataValue]) -> NBusStatusType:
  208. """
  209. Set data to read-write sensor.
  210. :param data: data to set
  211. :raises: NBusErrorAPIType
  212. :return: operation status
  213. """
  214. if self.__data_format is None: # check for format and params
  215. raise NBusErrorAPI(NBusErrorAPIType.FORMAT_NOT_LOADED)
  216. # create request packet
  217. request = [self.__address]
  218. # transform data
  219. request.extend(NbusCommonParser.data_to_request(self.data_format, data))
  220. # send request
  221. resp_length, *response = self.__port.request_sensor(self.__module_address, self.__address,
  222. NBusCommand.CMD_SET_DATA, bytearray(request))
  223. # return response status
  224. return NBusStatusType(response[1])