Пример #1
0
    def save_global_led_feedback_configuration(
            global_feedbacks,
            activate=True):  # type: (List[GlobalFeedbackDTO], bool) -> None
        # Important assumption in the below code to make this strategy solvable: If any of the 4 feedbacks is
        # given, they all are assumed to be given.
        global_configuration = GlobalConfiguration()
        if global_configuration.groupaction_any_output_changed > 255:
            group_action = GroupActionController.get_unused_group_action()
            if group_action is None:
                raise ValueError(
                    'No GroupAction available to store LED feedback configuration'
                )
        else:
            group_action = GroupActionController.load_group_action(
                global_configuration.groupaction_any_output_changed)

        for global_feedback_dto in global_feedbacks:
            holds_data, holds_configuration = CANFeedbackController._available_led_data(
                global_feedback_dto)
            if not holds_data:
                continue  # No change required
            # First, delete everything related to this global_feedback_dto
            if global_feedback_dto.id in [0, 16]:
                action = 73
                extra_parametery_msb = 0 if global_feedback_dto.id == 16 else 1
            else:
                action = 71 if 0 < global_feedback_dto.id < 16 else 70
                extra_parametery_msb = global_feedback_dto.id - (
                    1 if global_feedback_dto.id < 16 else 17)
            for basic_action in group_action.actions[:]:
                if basic_action.action_type == 20 and basic_action.action == action:
                    if basic_action.extra_parameter_msb == extra_parametery_msb:
                        group_action.actions.remove(basic_action)
            # Then, add the relevant entries back again
            if holds_configuration:
                for field in CANFeedbackController.FIELDS:
                    feedback_led_dto = getattr(global_feedback_dto, field)
                    if field in global_feedback_dto.loaded_fields and feedback_led_dto.id is not None:
                        basic_action = BasicAction(
                            action_type=20,
                            action=action,
                            device_nr=feedback_led_dto.id)
                        basic_action.extra_parameter_msb = extra_parametery_msb
                        basic_action.extra_parameter_lsb = CANFeedbackController._blinking_string_to_byte(
                            feedback_led_dto.function)
                        group_action.actions.append(basic_action)
        # Save changes
        if group_action.actions:
            if group_action.name == '':
                group_action.name = 'Global feedback'
            global_configuration.groupaction_any_output_changed = group_action.id
        else:
            group_action.name = ''
            global_configuration.groupaction_any_output_changed = CANFeedbackController.WORD_MAX
        global_configuration.save(activate=False)
        GroupActionController.save_group_action(group_action,
                                                ['name', 'actions'],
                                                activate=False)
        if activate:
            MemoryActivator.activate()
Пример #2
0
 def save_output_led_feedback_configuration(output,
                                            output_dto,
                                            activate=True):
     # type: (OutputConfiguration, OutputDTO, bool) -> None
     holds_data, holds_configuration = CANFeedbackController._available_led_data(
         output_dto)
     if not holds_data:
         return  # No change required
     group_action_id = output.output_groupaction_follow
     group_action = None  # type: Optional[GroupAction]  # Needed for keeping Mypy happy...
     if group_action_id <= 255:
         group_action = GroupActionController.load_group_action(
             group_action_id)
         for basic_action in group_action.actions[:]:
             if basic_action.action_type == 20 and basic_action.action in [
                     50, 51
             ]:
                 group_action.actions.remove(basic_action)
     else:
         if not holds_configuration:
             return  # No GroupAction configured, and no configurion. Nothing to do.
         group_action = GroupActionController.get_unused_group_action()
         if group_action is None:
             raise ValueError(
                 'No GroupAction available to store LED feedback configuration'
             )
     for field in CANFeedbackController.FIELDS:
         feedback_led_dto = getattr(output_dto, field)
         if field in output_dto.loaded_fields and feedback_led_dto.id is not None:
             action = CANFeedbackController._inverted_string_to_action(
                 feedback_led_dto.function)
             basic_action = BasicAction(action_type=20,
                                        action=action,
                                        device_nr=feedback_led_dto.id)
             basic_action.extra_parameter_lsb = CANFeedbackController._blinking_string_to_byte(
                 feedback_led_dto.function)
             basic_action.extra_parameter_msb = CANFeedbackController._brightness_string_to_byte(
                 feedback_led_dto.function)
             group_action.actions.append(basic_action)
     if group_action.actions:
         if group_action.name == '':
             group_action.name = 'Output {0}'.format(output.id)
         output.output_groupaction_follow = group_action.id
     else:
         group_action.name = ''
         output.output_groupaction_follow = CANFeedbackController.WORD_MAX
     output.save(activate=False)
     GroupActionController.save_group_action(group_action,
                                             ['name', 'actions'],
                                             activate=False)
     if activate:
         MemoryActivator.activate()