|
12 | 12 | from robot_interface.models.mission.status import MissionStatus |
13 | 13 |
|
14 | 14 |
|
15 | | -class GoingToRechargingWithMission(State): |
| 15 | +def GoingToRechargingWithMission(events: Events, mission: AbortedMission) -> State: |
16 | 16 |
|
17 | | - def __init__(self, events: Events, mission: AbortedMission): |
| 17 | + def _mission_failed_event_handler( |
| 18 | + error_message: ErrorMessage, |
| 19 | + ) -> Transition: |
| 20 | + publish_mission_status( |
| 21 | + events.mqtt_queue, |
| 22 | + mission.id, |
| 23 | + MissionStatus.Failed, |
| 24 | + error_message, |
| 25 | + ) |
| 26 | + return InterventionNeeded.transition("Return home to recharge failed") |
18 | 27 |
|
19 | | - def _mission_failed_event_handler( |
20 | | - error_message: ErrorMessage, |
21 | | - ) -> Transition[InterventionNeeded.InterventionNeeded]: |
22 | | - publish_mission_status( |
23 | | - events.mqtt_queue, |
24 | | - mission.id, |
25 | | - MissionStatus.Failed, |
26 | | - error_message, |
| 28 | + def _stop_mission_event_handler( |
| 29 | + stop_mission_id: str, |
| 30 | + ) -> Transition | None: |
| 31 | + if mission.id == stop_mission_id or stop_mission_id == "": |
| 32 | + events.api_requests.stop_mission.response.trigger_event( |
| 33 | + ControlMissionResponse(success=True) |
27 | 34 | ) |
28 | | - return InterventionNeeded.transition("Return home to recharge failed") |
29 | | - |
30 | | - def _stop_mission_event_handler( |
31 | | - stop_mission_id: str, |
32 | | - ) -> Transition[GoingToRecharging.GoingToRecharging] | None: |
33 | | - if mission.id == stop_mission_id or stop_mission_id == "": |
34 | | - events.api_requests.stop_mission.response.trigger_event( |
35 | | - ControlMissionResponse(success=True) |
36 | | - ) |
37 | | - return GoingToRecharging.transition_to_existing_mission() |
38 | | - else: |
39 | | - events.api_requests.stop_mission.response.trigger_event( |
40 | | - ControlMissionResponse( |
41 | | - success=False, failure_reason="Mission not found" |
42 | | - ) |
| 35 | + return GoingToRecharging.transition_to_existing_mission() |
| 36 | + else: |
| 37 | + events.api_requests.stop_mission.response.trigger_event( |
| 38 | + ControlMissionResponse( |
| 39 | + success=False, failure_reason="Mission not found" |
43 | 40 | ) |
44 | | - return None |
| 41 | + ) |
| 42 | + return None |
45 | 43 |
|
46 | | - event_handlers: list[EventHandlerMapping] = [ |
47 | | - EventHandlerMapping[ErrorMessage]( |
48 | | - event=events.robot_service_events.mission_failed, |
49 | | - handler=_mission_failed_event_handler, |
50 | | - ), |
51 | | - EventHandlerMapping[EmptyMessage]( |
52 | | - event=events.robot_service_events.mission_succeeded, |
53 | | - handler=lambda _: RechargingWithMission.transition(mission), |
54 | | - ), |
55 | | - EventHandlerMapping[EmptyMessage]( |
56 | | - event=events.api_requests.send_to_lockdown.request, |
57 | | - handler=lambda _: GoingToLockdown.transition_to_existing_mission_and_report_to_api(), |
58 | | - ), |
59 | | - EventHandlerMapping[str]( |
60 | | - event=events.api_requests.stop_mission.request, |
61 | | - handler=_stop_mission_event_handler, |
62 | | - ), |
63 | | - ] |
64 | | - super().__init__( |
65 | | - state_name=States.GoingToRechargingWithMission, |
66 | | - signal_exit_event=events.signal_state_machine_exit, |
67 | | - event_handler_mappings=event_handlers, |
68 | | - ) |
| 44 | + event_handlers: list[EventHandlerMapping] = [ |
| 45 | + EventHandlerMapping[ErrorMessage]( |
| 46 | + event=events.robot_service_events.mission_failed, |
| 47 | + handler=_mission_failed_event_handler, |
| 48 | + ), |
| 49 | + EventHandlerMapping[EmptyMessage]( |
| 50 | + event=events.robot_service_events.mission_succeeded, |
| 51 | + handler=lambda _: RechargingWithMission.transition(mission), |
| 52 | + ), |
| 53 | + EventHandlerMapping[EmptyMessage]( |
| 54 | + event=events.api_requests.send_to_lockdown.request, |
| 55 | + handler=lambda _: GoingToLockdown.transition_to_existing_mission_and_report_to_api(), |
| 56 | + ), |
| 57 | + EventHandlerMapping[str]( |
| 58 | + event=events.api_requests.stop_mission.request, |
| 59 | + handler=_stop_mission_event_handler, |
| 60 | + ), |
| 61 | + ] |
| 62 | + return State( |
| 63 | + state_name=States.GoingToRechargingWithMission, |
| 64 | + signal_exit_event=events.signal_state_machine_exit, |
| 65 | + event_handler_mappings=event_handlers, |
| 66 | + ) |
69 | 67 |
|
70 | 68 |
|
71 | 69 | def transition_and_start_return_home( |
72 | 70 | mission: AbortedMission, |
73 | | -) -> Transition[GoingToRechargingWithMission]: |
74 | | - def _transition(events: Events) -> GoingToRechargingWithMission: |
| 71 | +) -> Transition: |
| 72 | + def _transition(events: Events) -> State: |
75 | 73 | events.robot_service_events.mission_failed.clear_event() |
76 | 74 | events.robot_service_events.mission_succeeded.clear_event() |
77 | 75 |
|
|
0 commit comments