########################################################################### # # Copyright (c) 2021-2024 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 hd_events.py # # @author (last) Micahel Garthwaite # @date (last) 29-Aug-2023 # @author (original) Dara Navaei # @date (original) 12-Nov-2021 # ############################################################################ import struct from logging import Logger from ..common import * from ..common.msg_defs import MsgIds, MsgFieldPositions from ..protocols.CAN import DenaliChannels from ..utils.base import AbstractSubSystem, publish from datetime import datetime from time import time class HDEvents(AbstractSubSystem): """ Hemodialysis (HD) Dialin API sub-class for events related commands. """ UNKNOWN_STATE = "UNKNOWN_PREVIOUS_STATE" 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.hd_to_ui_ch_id msg_id = MsgIds.MSG_ID_HD_EVENT.value self.can_interface.register_receiving_publication_function(channel_id, msg_id, self._handler_events_sync) channel_id = DenaliChannels.hd_sync_broadcast_ch_id msg_id = MsgIds.MSG_ID_HD_OP_MODE_DATA.value self.can_interface.register_receiving_publication_function(channel_id, msg_id, self._handler_hd_op_mode_sync) # Define the dictionaries self._hd_event_dictionary = dict() self._hd_event_data_type = dict() self.hd_event_timestamp = 0.0 self.hd_event_op_mode_timestamp = 0.0 # Dictionary of the mode as key and the sub mode states enum class as the value self._hd_op_mode_2_sub_mode = {HDOpModes.MODE_FAUL.name: HDFaultStates, HDOpModes.MODE_INIT.name: HDInitStates, HDOpModes.MODE_STAN.name: HDStandbyStates, HDOpModes.MODE_TPAR.name: TreatmentParametersStates, HDOpModes.MODE_PRET.name: PreTreatmentSubModes, HDOpModes.MODE_TREA.name: TreatmentStates, HDOpModes.MODE_POST.name: PostTreatmentStates} # Loop through the list of the HD events enums and initial the event dictionary. Each event is a key in the # dictionary and the value is a list. for event in HDEventList: self._hd_event_dictionary[HDEventList(event).name] = [] # Loop through the list of the event data type enum and update the dictionary for data_type in HDEventDataType: event_data_type = HDEventDataType(data_type).name struct_unpack_type = None # If U32 is in the data type enum (i.e. EVENT_DATA_TYPE_U32), then the key is the enum and the value is # the corresponding format in the python struct if 'U32' in event_data_type or 'BOOL' in event_data_type: struct_unpack_type = 'I' elif 'S32' in event_data_type: struct_unpack_type = 'i' elif 'F32' in event_data_type: struct_unpack_type = 'f' self._hd_event_data_type[event_data_type] = struct_unpack_type def get_hd_nth_event(self, event_id, event_number=0): """ Returns the nth requested HD event @param event_id the ID of the HD event types (i.e. HD_EVENT_STARTUP) @param event_number the event number that is requested. The default is 0 meaning the last occurred event @returns the requested HD event number """ list_length = len(self._hd_event_dictionary[HDEventList(event_id).name]) if list_length == 0: event = [] elif event_number > list_length: event = self._hd_event_dictionary[HDEventList(event_id).name][list_length - 1] else: event = self._hd_event_dictionary[HDEventList(event_id).name][list_length - event_number - 1] return event def clear_hd_event_list(self): """ Clears the HD event list @returns none """ for key in self._hd_event_dictionary: self._hd_event_dictionary[key].clear() def get_hd_events(self, event_id, number_of_events=1): """ Returns the requested number of a certain HD event ID @param event_id the ID of the HD event types (i.e. HD_EVENT_STARTUP) @param number_of_events the last number of messages of a certain event type @returns a list of the requested HD event type """ list_of_events = [] # If there are not enough event lists send all the events that are available if len(self._hd_event_dictionary[HDEventList(event_id).name]) <= number_of_events: list_of_events = self._hd_event_dictionary[HDEventList(event_id).name] else: # Get the all the events complete_list = self._hd_event_dictionary[HDEventList(event_id).name] # Since the last are located at the end of the list, iterate backwards for the defined # event messages for i in range(len(complete_list) - 1, len(complete_list) - number_of_events - 1, -1): list_of_events.append(complete_list[i]) if number_of_events == 0: list_of_events = self._hd_event_dictionary[HDEventList(event_id).name] return list_of_events @publish(["hd_event_timestamp", '_hd_event_dictionary']) def _handler_events_sync(self, message, timestamp=0.0): """ Handles published events message @param message: published HD events data message @returns none """ event_data_1 = 0 event_data_2 = 0 op_mode = 0 sub_mode = 0 sub_state = 0 fourth_state = 0 current_sub_tuple = [] event_id = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_1:MsgFieldPositions.END_POS_FIELD_1]))[0] if event_id == HDEventList.HD_EVENT_OPERATION_STATUS.value: # Get the data type event_data_type_1 = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_2:MsgFieldPositions.END_POS_FIELD_2]))[0] struct_data_type = self._hd_event_data_type[HDEventDataType(event_data_type_1).name] op_mode = struct.unpack(' current_sub_mode_timestamp: # If the previous and current of the last two tuples do not match, then an operation mode transition # has occurred and the previous state is converted from the previous class and the current op mode # is converted from current operation states enum class. # i.e last = (timestamp, event type, 3, 8) and one before = (timestamp, event type, 8, 3) # previous and current do not match so in the last type (timestamp, event type, 8, 3) the prev state # should be from op mode 8 and the current state should be from op mode 3 previous_op_mode = last_op_tuple[len(last_op_tuple) - 2] if previous_op_mode != HDEvents.UNKNOWN_STATE: previous_sub_mode_enum_class = self._hd_op_mode_2_sub_mode[previous_op_mode] event_data_1 = previous_sub_mode_enum_class(event_data_1).name # Unknown previous state. Display value instead of name. else: event_data_1 = str(event_data_1) event_data_2 = current_sub_mode_enum_class(event_data_2).name else: if event_data_2 != 0: event_data_1 = current_sub_mode_enum_class(event_data_1).name event_data_2 = current_sub_mode_enum_class(event_data_2).name else: previous_sub_mode = current_sub_tuple[len(current_sub_tuple) - 2] previous_sub_mode_enum_class = self._hd_op_mode_2_sub_mode[previous_sub_mode] event_data_1 = previous_sub_mode_enum_class(event_data_1).name event_data_2 = current_sub_mode_enum_class(event_data_2).name event_tuple = (datetime.now().astimezone().strftime('%Y-%m-%d %H:%M:%S.%f'), event_state_name, event_data_1, event_data_2) elif event_state_name == HDEventList.HD_EVENT_OPERATION_STATUS.name: event_tuple = (time(), op_mode, sub_mode, sub_state, fourth_state) # Update event dictionary self._hd_event_dictionary[event_state_name].append(event_tuple) self.hd_event_timestamp = timestamp @publish(["hd_event_op_mode_timestamp", "hd_event_op_mode", "hd_event_sub_mode"]) def _handler_hd_op_mode_sync(self, message, timestamp=0.0): """ Handles published HD operation mode messages. Current HD operation mode is captured for reference. @param message: published HD operation mode broadcast message @return: None """ mode = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_1:MsgFieldPositions.END_POS_FIELD_1])) smode = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_2:MsgFieldPositions.END_POS_FIELD_2])) self.hd_event_op_mode = mode[0] self.hd_event_sub_mode = smode[0] self.hd_event_op_mode_timestamp = timestamp