Index: dialin/dg/dialysate_generator.py =================================================================== diff -u -rceb2d67297d31ac7c816f01cd4f836f071c1d39a -rd223289863a5bbb7d9f82cacca2d65b7ac3b2281 --- dialin/dg/dialysate_generator.py (.../dialysate_generator.py) (revision ceb2d67297d31ac7c816f01cd4f836f071c1d39a) +++ dialin/dg/dialysate_generator.py (.../dialysate_generator.py) (revision d223289863a5bbb7d9f82cacca2d65b7ac3b2281) @@ -138,8 +138,7 @@ # Create listener self.can_interface = DenaliCanMessenger(can_interface=can_interface, - logger=self.logger, - log_can=self._log_manager.log_level == "CAN_ONLY") + logger=self.logger) self.can_interface.start() # register handler for HD operation mode broadcast messages Index: dialin/dg/events.py =================================================================== diff -u -rceb2d67297d31ac7c816f01cd4f836f071c1d39a -rd223289863a5bbb7d9f82cacca2d65b7ac3b2281 --- dialin/dg/events.py (.../events.py) (revision ceb2d67297d31ac7c816f01cd4f836f071c1d39a) +++ dialin/dg/events.py (.../events.py) (revision d223289863a5bbb7d9f82cacca2d65b7ac3b2281) @@ -21,13 +21,14 @@ from ..protocols.CAN import DenaliChannels from ..utils.base import AbstractSubSystem, publish from datetime import datetime +from time import strftime, localtime class DGEvents(AbstractSubSystem): """ Dialysate Generator (DG) Dialin API sub-class for events related commands. """ - + UNKNOWN_STATE = "UNKNOWN_PREVIOUS_STATE" def __init__(self, can_interface, logger: Logger): """ @@ -44,11 +45,17 @@ msg_id = MsgIds.MSG_ID_DG_EVENT.value self.can_interface.register_receiving_publication_function(channel_id, msg_id, self._handler_events_sync) + channel_id = DenaliChannels.dg_sync_broadcast_ch_id + msg_id = MsgIds.MSG_ID_DG_OP_MODE.value + self.can_interface.register_receiving_publication_function(channel_id, msg_id, + self._handler_dg_op_mode_sync) + + # Define the dictionaries self._dg_event_dictionary = dict() self._dg_event_data_type = dict() - # Dictionary of the the mode as key and the sub mode states enum class as the value + # Dictionary of the mode as key and the sub mode states enum class as the value self._dg_op_mode_2_sub_mode = {DGOpModes.DG_MODE_FAUL.name: DGFaultStates, DGOpModes.DG_MODE_INIT.name: DGInitStates, DGOpModes.DG_MODE_STAN.name: DGStandByModeStates, @@ -175,12 +182,19 @@ event_data_2 = DGOpModes(event_data_2).name # Check if the event state name is sub mode change. elif event_state_name == DGEventList.DG_EVENT_SUB_MODE_CHANGE.name: - # Get the length of the list of the op mode list + # Get the length of the list of the sub mode list op_list_len = len(self._dg_event_dictionary[DGEventList.DG_EVENT_OP_MODE_CHANGE.name]) - # Get the last tuple of the op mode + # Get the last tuple of the sub mode # It is a list of tuples that each tuple is (timestamp, event type, prev op mode, current op mode) - last_op_tuple = self._dg_event_dictionary[DGEventList.DG_EVENT_OP_MODE_CHANGE.name][op_list_len - 1] + if op_list_len != 0: + last_op_tuple = self._dg_event_dictionary[DGEventList.DG_EVENT_OP_MODE_CHANGE.name][op_list_len - 1] + else: + # No op mode event has been recieved before the submode event was recieved. Use broadcast messages to + # determine current op mode. Previous mode can not be known + last_op_tuple = (strftime('%Y-%m-%d %H:%M:%S.%f', localtime(self.dg_event_op_mode_timestamp)), + DGEventList.DG_EVENT_OP_MODE_CHANGE.name, DGEvents.UNKNOWN_STATE, + DGOpModes(self.dg_event_op_mode).name) # Get the current and previous operation modes of the last tuple in the list of the sub modes # i.e. (timestamp, event type, prev, current) current_op_mode = last_op_tuple[len(last_op_tuple) - 1] @@ -238,3 +252,22 @@ # Update event dictionary self._dg_event_dictionary[event_state_name].append(event_tuple) self.dg_events_timestamp = timestamp + + @publish(["dg_event_op_mode_timestamp","dg_event_op_mode", "dg_event_sub_mode"]) + def _handler_dg_op_mode_sync(self, message, timestamp=0.0): + """ + Handles published DG operation mode messages. Current DG operation mode + is captured for reference. + + @param message: published DG 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.dg_event_op_mode = mode[0] + self.dg_event_sub_mode = smode[0] + self.dg_event_op_mode_timestamp = timestamp \ No newline at end of file Index: dialin/hd/hd_events.py =================================================================== diff -u -r36a7662dc9d72a7aa3eba334b5194cb1415fcd81 -rd223289863a5bbb7d9f82cacca2d65b7ac3b2281 --- dialin/hd/hd_events.py (.../hd_events.py) (revision 36a7662dc9d72a7aa3eba334b5194cb1415fcd81) +++ dialin/hd/hd_events.py (.../hd_events.py) (revision d223289863a5bbb7d9f82cacca2d65b7ac3b2281) @@ -21,13 +21,14 @@ from ..protocols.CAN import DenaliChannels from ..utils.base import AbstractSubSystem, publish from datetime import datetime +from time import strftime, localtime 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): """ @@ -43,10 +44,16 @@ 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.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, @@ -176,8 +183,16 @@ op_list_len = len(self._hd_event_dictionary[HDEventList.HD_EVENT_OP_MODE_CHANGE.name]) # Get the last tuple of the sub mode # It is a list of tuples that each tuple is (timestamp, event type, prev op mode, current op mode) - last_op_tuple = self._hd_event_dictionary[HDEventList.HD_EVENT_OP_MODE_CHANGE.name][op_list_len - 1] + if op_list_len != 0: + last_op_tuple = self._hd_event_dictionary[HDEventList.HD_EVENT_OP_MODE_CHANGE.name][op_list_len - 1] + else: + # No op mode event has been recieved before the submode event was recieved. Use broadcast messages to + # determine current op mode. Previous mode can not be known + last_op_tuple = (strftime('%Y-%m-%d %H:%M:%S.%f', localtime(self.hd_event_op_mode_timestamp)), + HDEventList.HD_EVENT_OP_MODE_CHANGE.name, HDEvents.UNKNOWN_STATE, + HDOpModes(self.hd_event_op_mode).name) + # Get the current and previous operation modes of the last tuple in the list of the sub modes # i.e. (timestamp, event type, prev, current) current_op_mode = last_op_tuple[len(last_op_tuple) - 1] @@ -235,3 +250,21 @@ # 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 \ No newline at end of file Index: dialin/hd/hemodialysis_device.py =================================================================== diff -u -r21cc5bfbf3079f194f7bad44e87146da5a986226 -rd223289863a5bbb7d9f82cacca2d65b7ac3b2281 --- dialin/hd/hemodialysis_device.py (.../hemodialysis_device.py) (revision 21cc5bfbf3079f194f7bad44e87146da5a986226) +++ dialin/hd/hemodialysis_device.py (.../hemodialysis_device.py) (revision d223289863a5bbb7d9f82cacca2d65b7ac3b2281) @@ -101,8 +101,7 @@ # Create listener self.can_interface = DenaliCanMessenger(can_interface=can_interface, - logger=self.logger, - log_can=self._log_manager.log_level == "CAN_ONLY") + logger=self.logger) self.can_interface.start() # register handler for HD operation mode broadcast messages