1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
use std::{
    path::PathBuf,
    sync::{Arc, Mutex},
};

use arci::{
    gamepad::{Button, GamepadEvent},
    Speaker,
};
use async_trait::async_trait;
use clap::Parser;
use openrr_client::{resolve_relative_path, ArcRobotClient};
use openrr_command::{load_command_file_and_filter, RobotCommand};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use tracing::{error, info, warn};

use crate::ControlMode;

const MODE: &str = "command";

struct RobotCommandExecutorInner {
    commands: Vec<RobotCommandConfig>,
    submode: String,
    command_index: usize,
    is_trigger_holding: bool,
    is_sending: bool,
}

impl RobotCommandExecutorInner {
    fn new(commands: Vec<RobotCommandConfig>) -> Self {
        let submode = commands[0].name.clone();
        Self {
            commands,
            submode,
            command_index: 0,
            is_trigger_holding: false,
            is_sending: false,
        }
    }

    fn handle_event(&mut self, event: arci::gamepad::GamepadEvent) -> Option<&str> {
        match event {
            GamepadEvent::ButtonPressed(Button::East) => {
                self.command_index = (self.command_index + 1) % self.commands.len();
                let command = &self.commands[self.command_index];
                self.submode.clone_from(&command.name);
                return Some(&self.submode);
            }
            GamepadEvent::ButtonPressed(Button::RightTrigger2) => {
                self.is_trigger_holding = true;
            }
            GamepadEvent::ButtonReleased(Button::RightTrigger2) => {
                self.is_trigger_holding = false;
                self.is_sending = false;
            }
            GamepadEvent::ButtonPressed(Button::West) => {
                self.is_sending = true;
            }
            GamepadEvent::ButtonReleased(Button::West) => {
                self.is_sending = false;
            }
            GamepadEvent::Disconnected => {
                self.is_trigger_holding = false;
                self.is_sending = false;
            }
            _ => {}
        }
        None
    }

    fn get_command(&self) -> &RobotCommandConfig {
        &self.commands[self.command_index]
    }
}

pub struct RobotCommandExecutor<S>
where
    S: Speaker,
{
    base_path: PathBuf,
    robot_client: Arc<ArcRobotClient>,
    speaker: S,
    inner: Mutex<RobotCommandExecutorInner>,
}

impl<S> RobotCommandExecutor<S>
where
    S: Speaker,
{
    pub fn new(
        base_path: PathBuf,
        commands: Vec<RobotCommandConfig>,
        robot_client: Arc<ArcRobotClient>,
        speaker: S,
    ) -> Option<Self> {
        if commands.is_empty() {
            None
        } else {
            Some(Self {
                base_path,
                robot_client,
                speaker,
                inner: Mutex::new(RobotCommandExecutorInner::new(commands)),
            })
        }
    }
}

#[async_trait]
impl<S> ControlMode for RobotCommandExecutor<S>
where
    S: Speaker,
{
    fn handle_event(&self, event: arci::gamepad::GamepadEvent) {
        if let Some(submode) = self.inner.lock().unwrap().handle_event(event) {
            // do not wait
            drop(self.speaker.speak(&format!("{MODE} {submode}")).unwrap());
        }
    }

    async fn proc(&self) {
        let command = {
            let inner = self.inner.lock().unwrap();
            if inner.is_trigger_holding && inner.is_sending {
                inner.get_command().clone()
            } else {
                return;
            }
        };
        let executor = openrr_command::RobotCommandExecutor {};
        match resolve_relative_path(&self.base_path, command.file_path.clone()) {
            Ok(path) => {
                match load_command_file_and_filter(path) {
                    Ok(commands) => {
                        let commands_len = commands.len() as f64;
                        for (i, command) in commands.iter().enumerate() {
                            if !self.inner.lock().unwrap().is_trigger_holding {
                                warn!("Remaining commands are canceled.");
                                return;
                            }
                            let command_parsed_iter = command.split_whitespace();
                            // Parse the command
                            let read_opt = RobotCommand::parse_from(command_parsed_iter);
                            // Execute the parsed command
                            info!("Executing {command} {i}/{commands_len}");

                            match executor.execute(&self.robot_client, &read_opt).await {
                                Ok(_) => {
                                    info!("finished command {command}");
                                }
                                Err(e) => {
                                    error!("failed command {command} {e:?}");
                                    break;
                                }
                            }
                        }
                    }
                    Err(e) => {
                        error!("failed to load file {e:?}");
                    }
                }
            }
            Err(e) => {
                error!("failed to resolve_relative_path {e:?}");
            }
        }
    }

    fn mode(&self) -> &str {
        MODE
    }

    fn submode(&self) -> String {
        self.inner.lock().unwrap().submode.to_owned()
    }
}

#[derive(Debug, Serialize, Deserialize, Clone, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct RobotCommandConfig {
    pub name: String,
    pub file_path: String,
}

#[cfg(test)]
mod test {
    use std::collections::HashMap;

    use arci::{
        DummyJointTrajectoryClient, DummyLocalization, DummyMoveBase, DummyNavigation,
        DummySpeaker, JointTrajectoryClient, Localization, MoveBase, Navigation,
    };
    use openrr_client::RobotClient;

    use super::*;

    #[test]
    fn test_robot_command_executor_inner() {
        let commands = vec![
            RobotCommandConfig {
                name: String::from("name0"),
                file_path: String::from("path0"),
            },
            RobotCommandConfig {
                name: String::from("name1"),
                file_path: String::from("path1"),
            },
        ];
        let mut inner = RobotCommandExecutorInner::new(commands);

        assert_eq!(inner.get_command().name, "name0");
        assert_eq!(inner.get_command().file_path, "path0");

        // Changed submode
        inner.handle_event(GamepadEvent::ButtonPressed(Button::East));
        assert_eq!(inner.submode, "name1");
        assert_eq!(inner.command_index, 1);

        // Case that enable switch is on
        inner.handle_event(GamepadEvent::ButtonPressed(Button::RightTrigger2));
        assert!(inner.is_trigger_holding);
        assert!(!inner.is_sending);

        // Case that enable switch becomes off
        inner.handle_event(GamepadEvent::ButtonReleased(Button::RightTrigger2));
        assert!(!inner.is_trigger_holding);
        assert!(!inner.is_sending);

        // Send command
        inner.handle_event(GamepadEvent::ButtonPressed(Button::West));
        assert!(inner.is_sending);

        // Stop send command
        inner.handle_event(GamepadEvent::ButtonReleased(Button::West));
        assert!(!inner.is_sending);

        // Disconnected
        inner.handle_event(GamepadEvent::Disconnected);
        assert!(!inner.is_trigger_holding);
        assert!(!inner.is_sending);
    }

    #[test]
    fn test_robot_command_executor_new() {
        let robot_client = Arc::new(
            RobotClient::new(
                toml::from_str("urdf_path = \"path\"").unwrap(),
                {
                    HashMap::from([(
                        String::from("arm"),
                        Arc::new(DummyJointTrajectoryClient::new(vec![String::from(
                            "test_joint1",
                        )])) as Arc<dyn JointTrajectoryClient>,
                    )])
                },
                {
                    HashMap::from([(
                        String::from("speaker"),
                        Arc::new(DummySpeaker::new()) as Arc<dyn Speaker>,
                    )])
                },
                Some(Arc::new(DummyLocalization::new()) as Arc<dyn Localization>),
                Some(Arc::new(DummyMoveBase::new()) as Arc<dyn MoveBase>),
                Some(Arc::new(DummyNavigation::new()) as Arc<dyn Navigation>),
            )
            .unwrap(),
        );

        let robot_command_executor = RobotCommandExecutor::new(
            PathBuf::from("path"),
            vec![RobotCommandConfig {
                name: String::from("name"),
                file_path: String::from("file_path"),
            }],
            robot_client.clone(),
            DummySpeaker::new(),
        )
        .unwrap();

        assert_eq!(robot_command_executor.base_path, PathBuf::from("path"));
        assert_eq!(
            robot_command_executor.inner.lock().unwrap().commands[0].name,
            String::from("name")
        );
        assert_eq!(
            robot_command_executor.inner.lock().unwrap().commands[0].file_path,
            String::from("file_path")
        );

        assert!(RobotCommandExecutor::new(
            PathBuf::from("path"),
            vec![],
            robot_client,
            DummySpeaker::new()
        )
        .is_none());
    }
}