#![allow(clippy::too_many_arguments)]
use anyhow;
use serde_json;
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[repr(u8)]
pub enum ErrorState {
Noerror = 0,
Unabletostartorresume = 1,
Unabletocompleteoperation = 2,
Commandinvalidinstate = 3,
Failedtofindchargingdock = 64,
Stuck = 65,
Dustbinmissing = 66,
Dustbinfull = 67,
Watertankempty = 68,
Watertankmissing = 69,
Watertanklidopen = 70,
Mopcleaningpadmissing = 71,
Lowbattery = 72,
Cannotreachtargetarea = 73,
Dirtywatertankfull = 74,
Dirtywatertankmissing = 75,
Wheelsjammed = 76,
Brushjammed = 77,
Navigationsensorobscured = 78,
}
impl ErrorState {
pub fn from_u8(value: u8) -> Option<Self> {
match value {
0 => Some(ErrorState::Noerror),
1 => Some(ErrorState::Unabletostartorresume),
2 => Some(ErrorState::Unabletocompleteoperation),
3 => Some(ErrorState::Commandinvalidinstate),
64 => Some(ErrorState::Failedtofindchargingdock),
65 => Some(ErrorState::Stuck),
66 => Some(ErrorState::Dustbinmissing),
67 => Some(ErrorState::Dustbinfull),
68 => Some(ErrorState::Watertankempty),
69 => Some(ErrorState::Watertankmissing),
70 => Some(ErrorState::Watertanklidopen),
71 => Some(ErrorState::Mopcleaningpadmissing),
72 => Some(ErrorState::Lowbattery),
73 => Some(ErrorState::Cannotreachtargetarea),
74 => Some(ErrorState::Dirtywatertankfull),
75 => Some(ErrorState::Dirtywatertankmissing),
76 => Some(ErrorState::Wheelsjammed),
77 => Some(ErrorState::Brushjammed),
78 => Some(ErrorState::Navigationsensorobscured),
_ => None,
}
}
pub fn to_u8(self) -> u8 {
self as u8
}
}
impl From<ErrorState> for u8 {
fn from(val: ErrorState) -> Self {
val as u8
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[repr(u8)]
pub enum OperationalState {
Stopped = 0,
Running = 1,
Paused = 2,
Error = 3,
Seekingcharger = 64,
Charging = 65,
Docked = 66,
Emptyingdustbin = 67,
Cleaningmop = 68,
Fillingwatertank = 69,
Updatingmaps = 70,
}
impl OperationalState {
pub fn from_u8(value: u8) -> Option<Self> {
match value {
0 => Some(OperationalState::Stopped),
1 => Some(OperationalState::Running),
2 => Some(OperationalState::Paused),
3 => Some(OperationalState::Error),
64 => Some(OperationalState::Seekingcharger),
65 => Some(OperationalState::Charging),
66 => Some(OperationalState::Docked),
67 => Some(OperationalState::Emptyingdustbin),
68 => Some(OperationalState::Cleaningmop),
69 => Some(OperationalState::Fillingwatertank),
70 => Some(OperationalState::Updatingmaps),
_ => None,
}
}
pub fn to_u8(self) -> u8 {
self as u8
}
}
impl From<OperationalState> for u8 {
fn from(val: OperationalState) -> Self {
val as u8
}
}
pub fn get_command_list() -> Vec<(u32, &'static str)> {
vec![
(0x00, "Pause"),
(0x01, "Stop"),
(0x02, "Start"),
(0x03, "Resume"),
(0x04, "OperationalCommandResponse"),
(0x80, "GoHome"),
]
}
pub fn get_command_name(cmd_id: u32) -> Option<&'static str> {
match cmd_id {
0x00 => Some("Pause"),
0x01 => Some("Stop"),
0x02 => Some("Start"),
0x03 => Some("Resume"),
0x04 => Some("OperationalCommandResponse"),
0x80 => Some("GoHome"),
_ => None,
}
}
pub fn get_command_schema(cmd_id: u32) -> Option<Vec<crate::clusters::codec::CommandField>> {
match cmd_id {
0x00 => Some(vec![]),
0x01 => Some(vec![]),
0x02 => Some(vec![]),
0x03 => Some(vec![]),
0x04 => Some(vec![]),
0x80 => Some(vec![]),
_ => None,
}
}
pub fn encode_command_json(cmd_id: u32, _args: &serde_json::Value) -> anyhow::Result<Vec<u8>> {
match cmd_id {
0x00 => Ok(vec![]),
0x01 => Ok(vec![]),
0x02 => Ok(vec![]),
0x03 => Ok(vec![]),
0x04 => Ok(vec![]),
0x80 => Ok(vec![]),
_ => Err(anyhow::anyhow!("unknown command ID: 0x{:02X}", cmd_id)),
}
}
pub async fn pause(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<()> {
conn.invoke_request(endpoint, crate::clusters::defs::CLUSTER_ID_RVC_OPERATIONAL_STATE, crate::clusters::defs::CLUSTER_RVC_OPERATIONAL_STATE_CMD_ID_PAUSE, &[]).await?;
Ok(())
}
pub async fn stop(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<()> {
conn.invoke_request(endpoint, crate::clusters::defs::CLUSTER_ID_RVC_OPERATIONAL_STATE, crate::clusters::defs::CLUSTER_RVC_OPERATIONAL_STATE_CMD_ID_STOP, &[]).await?;
Ok(())
}
pub async fn start(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<()> {
conn.invoke_request(endpoint, crate::clusters::defs::CLUSTER_ID_RVC_OPERATIONAL_STATE, crate::clusters::defs::CLUSTER_RVC_OPERATIONAL_STATE_CMD_ID_START, &[]).await?;
Ok(())
}
pub async fn resume(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<()> {
conn.invoke_request(endpoint, crate::clusters::defs::CLUSTER_ID_RVC_OPERATIONAL_STATE, crate::clusters::defs::CLUSTER_RVC_OPERATIONAL_STATE_CMD_ID_RESUME, &[]).await?;
Ok(())
}
pub async fn operational_command_response(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<()> {
conn.invoke_request(endpoint, crate::clusters::defs::CLUSTER_ID_RVC_OPERATIONAL_STATE, crate::clusters::defs::CLUSTER_RVC_OPERATIONAL_STATE_CMD_ID_OPERATIONALCOMMANDRESPONSE, &[]).await?;
Ok(())
}
pub async fn go_home(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<()> {
conn.invoke_request(endpoint, crate::clusters::defs::CLUSTER_ID_RVC_OPERATIONAL_STATE, crate::clusters::defs::CLUSTER_RVC_OPERATIONAL_STATE_CMD_ID_GOHOME, &[]).await?;
Ok(())
}