Make CommandGroup a GenericCommand
This commit is contained in:
parent
e3dbd8bdee
commit
1a40f2e661
@ -164,22 +164,25 @@ macro_rules! command_impl {
|
|||||||
|
|
||||||
pub type CommandRef = Rc<RefCell<dyn Command>>;
|
pub type CommandRef = Rc<RefCell<dyn Command>>;
|
||||||
|
|
||||||
pub struct CommandGroup {
|
pub struct CommandGroupConfig {
|
||||||
commands: Vec<CommandRef>,
|
pub commands: Vec<CommandRef>,
|
||||||
command_data: CommandData,
|
}
|
||||||
|
|
||||||
|
impl CommandGroupConfig {
|
||||||
|
pub fn new(commands: Vec<CommandRef>) -> Self {
|
||||||
|
Self { commands }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct CommandGroupState {
|
||||||
current_idx: Option<usize>,
|
current_idx: Option<usize>,
|
||||||
has_initialized: bool,
|
has_initialized: bool,
|
||||||
has_cancelled: bool,
|
has_cancelled: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CommandGroup {
|
impl Default for CommandGroupState {
|
||||||
pub fn new<TCommands>(data: CommandData, commands: TCommands) -> Self
|
fn default() -> Self {
|
||||||
where
|
CommandGroupState {
|
||||||
TCommands: IntoIterator<Item = CommandRef>,
|
|
||||||
{
|
|
||||||
CommandGroup {
|
|
||||||
commands: commands.into_iter().collect(),
|
|
||||||
command_data: data,
|
|
||||||
current_idx: None,
|
current_idx: None,
|
||||||
has_initialized: false,
|
has_initialized: false,
|
||||||
has_cancelled: false,
|
has_cancelled: false,
|
||||||
@ -187,51 +190,49 @@ impl CommandGroup {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WithCommandData for CommandGroup {
|
pub type CommandGroup = GenericCommand<CommandGroupConfig, CommandGroupState>;
|
||||||
fn command_data(&self) -> &CommandData {
|
|
||||||
&self.command_data
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Command for CommandGroup {
|
impl Command for CommandGroup {
|
||||||
fn init(&mut self) {
|
fn init(&mut self) {
|
||||||
if self.current_idx.is_some() {
|
if self.state.current_idx.is_some() {
|
||||||
error!("CommandGroup init called twice");
|
error!("CommandGroup init called twice");
|
||||||
}
|
}
|
||||||
self.current_idx = Some(0);
|
self.state.current_idx = Some(0);
|
||||||
self.has_initialized = false;
|
self.state.has_initialized = false;
|
||||||
self.has_cancelled = false;
|
self.state.has_cancelled = false;
|
||||||
debug!("CommandGroup {} init", self.command_data().name());
|
debug!("CommandGroup {} init", self.command_data().name());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn step(&mut self) -> CommandResult {
|
fn step(&mut self) -> CommandResult {
|
||||||
use self::CommandResult::*;
|
use self::CommandResult::*;
|
||||||
if self.has_cancelled {
|
let state = &mut self.state;
|
||||||
|
let commands = &self.config.commands;
|
||||||
|
if state.has_cancelled {
|
||||||
debug!("CommandGroup step called after cancelled");
|
debug!("CommandGroup step called after cancelled");
|
||||||
return CommandResult::Cancel;
|
return CommandResult::Cancel;
|
||||||
}
|
}
|
||||||
let mut current_idx = if let Some(c) = self.current_idx {
|
let mut current_idx = if let Some(c) = state.current_idx {
|
||||||
c
|
c
|
||||||
} else {
|
} else {
|
||||||
error!("CommandGroup step called before init");
|
error!("CommandGroup step called before init");
|
||||||
self.has_cancelled = true;
|
state.has_cancelled = true;
|
||||||
return CommandResult::Cancel;
|
return CommandResult::Cancel;
|
||||||
};
|
};
|
||||||
let mut result: CommandResult;
|
let mut result: CommandResult;
|
||||||
loop {
|
loop {
|
||||||
if current_idx >= self.commands.len() {
|
if current_idx >= commands.len() {
|
||||||
debug!("CommandGroup out of commands");
|
debug!("CommandGroup out of commands");
|
||||||
return CommandResult::Done;
|
return CommandResult::Done;
|
||||||
}
|
}
|
||||||
let mut command = self.commands[current_idx].borrow_mut();
|
let mut command = commands[current_idx].borrow_mut();
|
||||||
if !self.has_initialized {
|
if !state.has_initialized {
|
||||||
debug!(
|
debug!(
|
||||||
"CommandGroup init {0} ({1})",
|
"CommandGroup init {0} ({1})",
|
||||||
current_idx,
|
current_idx,
|
||||||
command.command_data().name()
|
command.command_data().name()
|
||||||
);
|
);
|
||||||
command.init();
|
command.init();
|
||||||
self.has_initialized = true;
|
state.has_initialized = true;
|
||||||
}
|
}
|
||||||
result = command.step();
|
result = command.step();
|
||||||
if result != Continue {
|
if result != Continue {
|
||||||
@ -246,29 +247,31 @@ impl Command for CommandGroup {
|
|||||||
match result {
|
match result {
|
||||||
Done => {
|
Done => {
|
||||||
current_idx += 1;
|
current_idx += 1;
|
||||||
self.has_initialized = false;
|
state.has_initialized = false;
|
||||||
}
|
}
|
||||||
Cancel => {
|
Cancel => {
|
||||||
self.has_cancelled = true;
|
state.has_cancelled = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
_ => break,
|
_ => break,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.current_idx = Some(current_idx);
|
state.current_idx = Some(current_idx);
|
||||||
CommandResult::Continue
|
CommandResult::Continue
|
||||||
}
|
}
|
||||||
|
|
||||||
fn finish(&mut self, result: CommandResult) {
|
fn finish(&mut self, result: CommandResult) {
|
||||||
|
let state = &mut self.state;
|
||||||
|
let commands = &mut self.config.commands;
|
||||||
if result == CommandResult::Cancel {
|
if result == CommandResult::Cancel {
|
||||||
if let Some(command) = self
|
if let Some(command) = state
|
||||||
.current_idx
|
.current_idx
|
||||||
.and_then(|current_idx| self.commands.get(current_idx))
|
.and_then(|current_idx| commands.get(current_idx))
|
||||||
{
|
{
|
||||||
command.borrow_mut().finish(result);
|
command.borrow_mut().finish(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.current_idx = None;
|
state.current_idx = None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@ use std::rc::Rc;
|
|||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use command::{CommandData, CommandGroup, CommandRef, CommandScheduler};
|
use command::{CommandData, CommandGroup, CommandGroupConfig, CommandRef, CommandScheduler};
|
||||||
use drive::{Drive, DriveRef};
|
use drive::{Drive, DriveRef};
|
||||||
|
|
||||||
mod cmds {
|
mod cmds {
|
||||||
@ -77,7 +77,7 @@ impl Robot {
|
|||||||
let log_command = cmds::LogCmd::new_ref(CommandData::new(2, "LogCommand"), ());
|
let log_command = cmds::LogCmd::new_ref(CommandData::new(2, "LogCommand"), ());
|
||||||
let command_group = CommandGroup::new(
|
let command_group = CommandGroup::new(
|
||||||
CommandData::new(1, "Group"),
|
CommandData::new(1, "Group"),
|
||||||
vec![command, log_command],
|
CommandGroupConfig::new(vec![command, log_command]),
|
||||||
);
|
);
|
||||||
let command_ref: CommandRef = Rc::new(RefCell::new(command_group));
|
let command_ref: CommandRef = Rc::new(RefCell::new(command_group));
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user