Ver código fonte

allow follow_up_state to be dynamically set by the cmd exit code

Noah Vogt 1 mês atrás
pai
commit
ff3949b4dd
2 arquivos alterados com 45 adições e 11 exclusões
  1. 23 8
      VulcanBoard.py
  2. 22 3
      config/load.py

+ 23 - 8
VulcanBoard.py

@@ -173,8 +173,9 @@ class VulcanBoardApp(App):
     async def execute_command_async(self, button: dict, btn: AutoResizeButton):
         follow_up_state_loop = True
         states = button["states"]
+        current_state_id = btn.state_id
         while follow_up_state_loop:
-            state = get_state_from_id(states, btn.state_id)
+            state = get_state_from_id(states, current_state_id)
             follow_up_state_loop = False
 
             try:
@@ -188,15 +189,29 @@ class VulcanBoardApp(App):
                 log(f"Error executing command: {e}", color="yellow")
 
             if len(states) != 1:
-                if isinstance(
-                    follow_up_state := state.get("follow_up_state"), int
-                ):
+                follow_up_state = state.get("follow_up_state")
+                if isinstance(follow_up_state, int):
                     follow_up_state_loop = True
                     exit_code = follow_up_state
+                elif follow_up_state == "exit_code":
+                    follow_up_state_loop = True
+
+                if follow_up_state_loop:
+                    current_state_id = get_state_id_from_exit_code(
+                        states, exit_code
+                    )
+                    follow_up_execute_states = state.get(
+                        "follow_up_execute_states"
+                    )
+                    if (
+                        follow_up_execute_states is not None
+                        and current_state_id not in follow_up_execute_states
+                    ):
+                        follow_up_state_loop = False
 
                 Clock.schedule_once(
-                    lambda _: self.update_button_feedback(
-                        states, btn, exit_code
+                    lambda _, ec=exit_code: self.update_button_feedback(
+                        states, btn, ec
                     )
                 )
                 affects_buttons = button.get("affects_buttons", None)
@@ -205,10 +220,10 @@ class VulcanBoardApp(App):
                         btn_pos = (affected_btn_dims[0], affected_btn_dims[1])
                         affected_button = self.button_grid[btn_pos]
                         Clock.schedule_once(
-                            lambda _, btn_pos=btn_pos, affected_button=affected_button: self.update_button_feedback(
+                            lambda _, btn_pos=btn_pos, affected_button=affected_button, ec=exit_code: self.update_button_feedback(
                                 self.button_config_map[btn_pos]["states"],
                                 affected_button,
-                                exit_code,
+                                ec,
                             )
                         )
 

+ 22 - 3
config/load.py

@@ -165,12 +165,31 @@ class ConfigLoader:
                         )
 
                 follow_up_state = state.get("follow_up_state", 0)
-                if not isinstance(follow_up_state, int):
+                if not (
+                    isinstance(follow_up_state, int)
+                    or follow_up_state == "exit_code"
+                ):
                     raise CustomException(
                         f"invalid {btn_dims}: 'follow_up_state' subentry for"
-                        + f" state '{state_id}': must be int"
+                        + f" state '{state_id}': must be int or 'exit_code'"
                     )
-                to_follow_up_state_ids.add(follow_up_state)
+                if isinstance(follow_up_state, int):
+                    to_follow_up_state_ids.add(follow_up_state)
+
+                follow_up_execute_states = state.get("follow_up_execute_states")
+                if follow_up_execute_states is not None:
+                    if not isinstance(follow_up_execute_states, list):
+                        raise CustomException(
+                            f"invalid {btn_dims}: 'follow_up_execute_states' subentry for"
+                            + f" state '{state_id}': must be a list"
+                        )
+                    for sid in follow_up_execute_states:
+                        if not isinstance(sid, int):
+                            raise CustomException(
+                                f"invalid {btn_dims}: 'follow_up_execute_states' subentry for"
+                                + f" state '{state_id}': list elements must be ints"
+                            )
+                        to_follow_up_state_ids.add(sid)
 
             button_grid[(dimensions[0], dimensions[1])] = button