Index: dialin/dg/events.py =================================================================== diff -u -rff9ca2cc07737e0bae9d05d97a88838de5863f3e -rd430e49fa1d7a0f4ff1dfe24733fecded6d5ee70 --- dialin/dg/events.py (.../events.py) (revision ff9ca2cc07737e0bae9d05d97a88838de5863f3e) +++ dialin/dg/events.py (.../events.py) (revision d430e49fa1d7a0f4ff1dfe24733fecded6d5ee70) @@ -28,7 +28,6 @@ msg_id = MsgIds.MSG_ID_DG_EVENT.value self.can_interface.register_receiving_publication_function(channel_id, msg_id, self._handler_events_sync) - self._signal_new_op_mode_data = False # Define the dictionaries self._dg_event_dictionary = dict() self._dg_event_data_type = dict() @@ -138,57 +137,75 @@ # Convert the event ID to enum event_state_name = DGEventList(event_id).name + #print(event_data_1, event_data_2) + # Check if the event state name is operation mode change. If it is, get the name of the operation modes # from the op modes enum class if event_state_name == DGEventList.DG_EVENT_OP_MODE_CHANGE.name: event_data_1 = DGOpModes(event_data_1).name event_data_2 = DGOpModes(event_data_2).name - self._signal_new_op_mode_data = True # Check if the event state name is sub mode change. elif event_state_name == DGEventList.DG_EVENT_SUB_MODE_CHANGE.name: - self._signal_new_op_mode_data = False - # Get the length of the list of the sub mode list - event_list_len = len(self._dg_event_dictionary[DGEventList.DG_EVENT_OP_MODE_CHANGE.name]) + op_list_len = len(self._dg_event_dictionary[DGEventList.DG_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_mode = self._dg_event_dictionary[DGEventList.DG_EVENT_OP_MODE_CHANGE.name][event_list_len - 1] + last_op_tuple = self._dg_event_dictionary[DGEventList.DG_EVENT_OP_MODE_CHANGE.name][op_list_len - 1] + # 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) - last_current = last_op_mode[len(last_op_mode) - 1] - last_previous = last_op_mode[len(last_op_mode) - 2] + current_op_mode = last_op_tuple[len(last_op_tuple) - 1] + current_op_mode_timestamp = datetime.strptime(last_op_tuple[0], '%Y-%m-%d %H:%M:%S.%f') - # Get the tuple prior to the last tuple and get its previous and current operation modes - prev_to_last_op = self._dg_event_dictionary[DGEventList.DG_EVENT_OP_MODE_CHANGE.name][event_list_len - 2] - prev_current = prev_to_last_op[len(prev_to_last_op) - 1] - prev_previous = prev_to_last_op[len(prev_to_last_op) - 2] + sub_mode_list_len = len(self._dg_event_dictionary[DGEventList.DG_EVENT_SUB_MODE_CHANGE.name]) + if sub_mode_list_len != 0: + # Get the tuple prior to the last tuple and get its previous and current operation modes + current_sub_tuple = self._dg_event_dictionary[DGEventList.DG_EVENT_SUB_MODE_CHANGE.name][ + sub_mode_list_len - 1] + + current_sub_mode_timestamp = datetime.strptime(current_sub_tuple[0], '%Y-%m-%d %H:%M:%S.%f') + else: + current_sub_mode_timestamp = 0 + # Get the class of the states enums of the current operation mode that is running - current_sub_mode_enum_class = self._dg_op_mode_2_sub_mode[last_current] + current_sub_mode_enum_class = self._dg_op_mode_2_sub_mode[current_op_mode] # Check if the operation modes of the two tuples match # i.e. last = (timestamp, event type, prev, current) and one before = (timestamp, event type, prev, current) # If the prev and current match respectively, it means the current operation mode has not changed so the # operation mode states can be converted from the current sub mode enum class - #if last_current == prev_current and last_previous == prev_previous: - if self._signal_new_op_mode_data is False: + if current_sub_mode_timestamp != 0: + if current_op_mode_timestamp <= current_sub_mode_timestamp: - event_data_1 = current_sub_mode_enum_class(event_data_1).name - event_data_2 = current_sub_mode_enum_class(event_data_2).name + event_data_1 = current_sub_mode_enum_class(event_data_1).name + event_data_2 = current_sub_mode_enum_class(event_data_2).name + elif current_op_mode_timestamp > current_sub_mode_timestamp: + # If the previous and current of the last two tuples do not match, then an operation modes transition + # has occurred and 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_sub_mode = current_sub_tuple[len(current_sub_tuple) - 2] + previous_sub_mode_enum_class = self._dg_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 else: - # If the previous and current of the last two tuples do not match, then an operation modes transition - # has occurred and 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_sub_mode_enum_class = self._dg_op_mode_2_sub_mode[last_previous] - event_data_1 = previous_sub_mode_enum_class(event_data_1).name - event_data_2 = current_sub_mode_enum_class(event_data_2).name + #print(event_data_1, event_data_2) + 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._dg_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 + # Get the current timestamp and create a tuple of the current events - event_tuple = (str(datetime.now()), event_state_name, event_data_1, event_data_2) + event_tuple = (datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f'), event_state_name, event_data_1, event_data_2) # Update event dictionary self._dg_event_dictionary[event_state_name].append(event_tuple) Index: tests/test_flush.py =================================================================== diff -u -r1936d2c112d7629ddef976e2bd4cba60664c621f -rd430e49fa1d7a0f4ff1dfe24733fecded6d5ee70 --- tests/test_flush.py (.../test_flush.py) (revision 1936d2c112d7629ddef976e2bd4cba60664c621f) +++ tests/test_flush.py (.../test_flush.py) (revision d430e49fa1d7a0f4ff1dfe24733fecded6d5ee70) @@ -104,7 +104,9 @@ complete_counter = 1 f = open("/home/fw/projects/dialin/tests/flush_mode.log", "w") - dg.hd_proxy.cmd_start_stop_dg_flush() + #dg.hd_proxy.cmd_start_stop_dg_flush() + dg.cmd_dg_software_reset_request() + #sleep(0.1) try: while True: flush = get_flush_mode_info() @@ -123,11 +125,11 @@ # If the mode came back to standby or standby solo if dg.dg_operation_mode == 3 or dg.dg_operation_mode == 4: - + dg.hd_proxy.cmd_start_stop_dg_flush() # Write a few more complete states to make sure the complete state items are recorded - if complete_counter == 3: - f.close() - break + #if complete_counter == 3: + # f.close() + # break complete_counter += 1