########################################################################### # # Copyright (c) 2019-2020 Diality Inc. - All Rights Reserved. # # THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN # WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. # # @file conductivity_sensors.py # # @author (last) Quang Nguyen # @date (last) 29-Oct-2020 # @author (original) Quang Nguyen # @date (original) 20-Jul-2020 # ############################################################################ import struct from .constants import RESET,NO_RESET from ..utils.conversions import integer_to_bytearray, float_to_bytearray from ..protocols.CAN import (DenaliMessage, DenaliChannels) from ..utils.base import _AbstractSubSystem, _publish from ..common.msg_defs import MsgIds, MsgFldPositions from logging import Logger class ConductivitySensors(_AbstractSubSystem): """ ConductivitySensors Dialysate Generator (DG) Dialin API sub-class for conductivity sensors related commands. """ def __init__(self, can_interface, logger: Logger): """ @param can_interface: Denali Can Messenger object """ super().__init__() self.can_interface = can_interface self.logger = logger if self.can_interface is not None: channel_id = DenaliChannels.dg_sync_broadcast_ch_id msg_id = MsgIds.MSG_ID_DG_CONDUCTIVITY_DATA.value self.can_interface.register_receiving_publication_function(channel_id, msg_id, self._handler_conductivity_sensors_sync) self.ro_rejection_ratio = 0.0 self.conductivity_sensor_cpi = 0.0 self.conductivity_sensor_cpo = 0.0 self.conductivity_sensor_cd1 = 0.0 self.conductivity_sensor_cd2 = 0.0 def get_conductivity_sensors(self): """ Gets the current conductivity value @return: List containing conductivity values: [ conductivity_sensor_cpi, conductivity_sensor_cpo, conductivity_sensor_cd1, conductivity_sensor_cd2 ] """ return [self.conductivity_sensor_cpi, self.conductivity_sensor_cpo, self.conductivity_sensor_cd1, self.conductivity_sensor_cd2] def get_RO_rejection_ratio(self): """ Gets the current RO rejection ratio value @return: ro_rejection_ratio """ return self.ro_rejection_ratio @_publish(["ro_rejection_ratio", "conductivity_sensor_cpi", "conductivity_sensor_cpo", "conductivity_sensor_cd1", "conductivity_sensor_cd2"]) def _handler_conductivity_sensors_sync(self, message): """ Handles published conductivity sensor data messages. Conductivity sensor data are captured for reference. @param message: published conductivity sensor data message @return: None """ ro_rejection_ratio = struct.unpack('f', bytearray(message['message'][MsgFldPositions.START_POS_FIELD_1:MsgFldPositions.END_POS_FIELD_1])) cpi = struct.unpack('f', bytearray(message['message'][MsgFldPositions.START_POS_FIELD_2:MsgFldPositions.END_POS_FIELD_2])) cpo = struct.unpack('f', bytearray(message['message'][MsgFldPositions.START_POS_FIELD_3:MsgFldPositions.END_POS_FIELD_3])) cd1 = struct.unpack('f', bytearray(message['message'][MsgFldPositions.START_POS_FIELD_4:MsgFldPositions.END_POS_FIELD_4])) cd2 = struct.unpack('f', bytearray(message['message'][MsgFldPositions.START_POS_FIELD_5:MsgFldPositions.END_POS_FIELD_5])) self.ro_rejection_ratio = ro_rejection_ratio[0] self.conductivity_sensor_cpi = cpi[0] self.conductivity_sensor_cpo = cpo[0] self.conductivity_sensor_cd1 = cd1[0] self.conductivity_sensor_cd2 = cd2[0] def cmd_conductivity_sensor_override(self, conductivity, sensor, reset=NO_RESET): """ Constructs and sends the conductivity value override command @param conductivity: float - conductivity value to override sensor with @param sensor: unsigned int - sensor ID @param reset: integer - 1 to reset a previous override, 0 to override @return: 1 if successful, zero otherwise Conductivity sensor IDs: \n 0 = CPI \n 1 = CPO \n """ reset_byte_array = integer_to_bytearray(reset) cond_byte_array = float_to_bytearray(conductivity) sensor_byte_array = integer_to_bytearray(sensor) payload = reset_byte_array + cond_byte_array + sensor_byte_array message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dg_ch_id, message_id=MsgIds.MSG_ID_CONDUCTIVITY_OVERRIDE.value, payload=payload) if reset == RESET: str_res = "reset back to normal" else: str_res = str(conductivity) + " microsiemens/cm" self.logger.debug("override conductivity sensor value for sensor " + str(sensor) + ": " + str_res) # Send message received_message = self.can_interface.send(message) # If there is content... if received_message is not None: # response payload is OK or not OK return received_message['message'][DenaliMessage.PAYLOAD_START_INDEX] else: self.logger.error("Timeout!!!!") return False def cmd_conductivity_sensor_data_broadcast_interval_override(self, ms, reset=NO_RESET): """ Constructs and sends the conductivity sensor data broadcast interval override command @param ms: integer - interval (in ms) to override with @param reset: integer - 1 to reset a previous override, 0 to override @return: 1 if successful, zero otherwise """ reset_byte_array = integer_to_bytearray(reset) ms_byte_array = integer_to_bytearray(ms) payload = reset_byte_array + ms_byte_array message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dg_ch_id, message_id=MsgIds.MSG_ID_CONDUCTIVITY_PUBLISH_INTERVAL_OVERRIDE.value, payload=payload) self.logger.debug("override DG conductivity sensor broadcast interval") # Send message received_message = self.can_interface.send(message) # If there is content... if received_message is not None: if reset == RESET: str_res = "reset back to normal: " else: str_res = str(ms) + " ms: " self.logger.debug("Conductivity sensor data broadcast interval overridden to " + str_res + str(received_message['message'][DenaliMessage.PAYLOAD_START_INDEX])) # response payload is OK or not OK return received_message['message'][DenaliMessage.PAYLOAD_START_INDEX] else: self.logger.error("Timeout!!!!") return False