Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- Run `cargo fmt` after each change.
- After implementing a task or fixing a bug, run `cargo test` to ensure no regressions.
- Do not commit until `cargo fmt`, `cargo check`, and `cargo test` have been run successfully.
- Before marking a todo list step complete, run `cargo fmt`, `cargo check`, and `cargo test`, then stop for review and commit the changes to the branch.
- Run all tests with `cargo test`; for Docker build validation use `make test`.
- Add tests alongside the module you are changing to keep coverage close to the code.
- Manual `elisa` MQTT checks: publish to `action/request` and `state/request` with MQTT v5 `ResponseTopic` set; responses arrive on `action/response/<uuid>` or `state/response/<uuid>`.
Expand All @@ -38,9 +39,13 @@
- Only commit after `cargo fmt`, `cargo check`, and `cargo test` have completed successfully.
- PRs should explain the change, link related issues, and include notes on config/env changes.
- If the change affects runtime behavior, add a brief manual test note or log snippet.
- Testing sections should include only manual test commands that were actually run; omit `cargo fmt/check/test`.
- If no manual testing was performed, omit the Testing section entirely.
- Example testing section:
- `Testing: mosquitto_pub -h localhost -p 1883 -u elisa -P 123mqtt -t action/request -V mqttv5 -D publish response-topic action/response/TEST-UUID -m '{"actions":[{"elisa":[{"set_cleanup_mode":"dry_cleaning"},"ID-1"]}]}'`
- Use `gt` (Graphite) for managing PRs in this project.
- For multi-step plans, create a draft PR when starting implementation and update the PR after each completed step.
- After plan completion let's update the PR description (you can use `gh` for that) and publish it.
- After plan completion update the PR description using `gh`, then publish it.

## Security & Configuration Tips
- Several run targets expect secrets from 1Password (`op read ...`) and MQTT credentials; avoid hard-coding secrets.
Expand Down
8 changes: 8 additions & 0 deletions GEMINI.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ This document provides a concise summary of project-specific guidelines for Gemi
- Run `cargo fmt` before submitting.
- Add/run unit tests using `cargo test`.
- Do not commit until `cargo fmt`, `cargo check`, and `cargo test` have succeeded.
- Before completing a todo list step, run validations, stop for review, and commit the changes to the branch.
4. **Hardware Specifics:** Refer to `AGENTS.md` for Roborock, Inspinia, or Sonoff specific protocol details.

## Testing
Expand All @@ -33,3 +34,10 @@ This document provides a concise summary of project-specific guidelines for Gemi
- Use short, imperative sentences (e.g., "Add timeout to Roborock discovery").
- Style should match existing commit history (`git log -n 3`).
- Only commit after `cargo fmt`, `cargo check`, and `cargo test` have completed successfully.

## Pull Requests
- Update PR descriptions using `gh`.
- Testing sections should include only manual test commands that were actually run; omit `cargo fmt/check/test`.
- If no manual testing was performed, omit the Testing section entirely.
- Example testing section:
- `Testing: mosquitto_pub -h localhost -p 1883 -u elisa -P 123mqtt -t action/request -V mqttv5 -D publish response-topic action/response/TEST-UUID -m '{"actions":[{"elisa":[{"set_cleanup_mode":"dry_cleaning"},"ID-1"]}]}'`
54 changes: 54 additions & 0 deletions bin/alisa/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use std::fmt;

#[derive(Debug)]
pub enum Error {
Mqtt(paho_mqtt::Error),
Json(serde_json::Error),
Io(std::io::Error),
Join(tokio::task::JoinError),
Http(chipp_http::Error),
}

impl From<paho_mqtt::Error> for Error {
fn from(err: paho_mqtt::Error) -> Self {
Self::Mqtt(err)
}
}

impl From<serde_json::Error> for Error {
fn from(err: serde_json::Error) -> Self {
Self::Json(err)
}
}

impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Self::Io(err)
}
}

impl From<tokio::task::JoinError> for Error {
fn from(err: tokio::task::JoinError) -> Self {
Self::Join(err)
}
}

impl From<chipp_http::Error> for Error {
fn from(err: chipp_http::Error) -> Self {
Self::Http(err)
}
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Mqtt(err) => write!(f, "mqtt error: {err}"),
Self::Json(err) => write!(f, "json error: {err}"),
Self::Io(err) => write!(f, "io error: {err}"),
Self::Join(err) => write!(f, "join error: {err}"),
Self::Http(err) => write!(f, "http error: {err}"),
}
}
}

impl std::error::Error for Error {}
6 changes: 4 additions & 2 deletions bin/alisa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ mod web_service;
pub use reporter::Reporter;
pub use web_service::router;

pub type ErasedError = Box<dyn std::error::Error + Send + Sync>;
pub type Result<T> = std::result::Result<T, ErasedError>;
mod error;
pub use error::Error;

pub type Result<T> = std::result::Result<T, Error>;
22 changes: 18 additions & 4 deletions bin/alisa/src/web_service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ use axum::routing::{get, head, post};
use axum::Router;
use log::error;

pub struct ServiceError(Box<dyn std::error::Error + Send + Sync>, uuid::Uuid);
use crate::Error;

pub struct ServiceError(Error, uuid::Uuid);

impl IntoResponse for ServiceError {
fn into_response(self) -> Response<Body> {
Expand All @@ -42,9 +44,21 @@ impl IntoResponse for ServiceError {
}
}

impl<T: std::error::Error + Sync + Send + 'static> From<T> for ServiceError {
fn from(value: T) -> Self {
ServiceError(Box::new(value), uuid::Uuid::new_v4())
impl From<Error> for ServiceError {
fn from(value: Error) -> Self {
ServiceError(value, uuid::Uuid::new_v4())
}
}

impl From<paho_mqtt::Error> for ServiceError {
fn from(value: paho_mqtt::Error) -> Self {
ServiceError(Error::Mqtt(value), uuid::Uuid::new_v4())
}
}

impl From<serde_json::Error> for ServiceError {
fn from(value: serde_json::Error) -> Self {
ServiceError(Error::Json(value), uuid::Uuid::new_v4())
}
}

Expand Down
2 changes: 1 addition & 1 deletion bin/alisa/src/web_service/user/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub async fn query(

let mut mqtt_client = connect_mqtt(mqtt_address, mqtt_username, mqtt_password, "alisa_query")
.await
.expect("failed to connect mqtt");
.map_err(ServiceError::from)?;

let request_id = headers.get("X-Request-Id").unwrap().to_str().unwrap();

Expand Down
56 changes: 56 additions & 0 deletions bin/elisa/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use std::fmt;

#[derive(Debug)]
pub enum Error {
Json(serde_json::Error),
Mqtt(paho_mqtt::Error),
Vacuum(roborock::Error),
QueueClosed,
Join(tokio::task::JoinError),
AddrParse(std::net::AddrParseError),
}

impl From<serde_json::Error> for Error {
fn from(err: serde_json::Error) -> Self {
Self::Json(err)
}
}

impl From<paho_mqtt::Error> for Error {
fn from(err: paho_mqtt::Error) -> Self {
Self::Mqtt(err)
}
}

impl From<roborock::Error> for Error {
fn from(err: roborock::Error) -> Self {
Self::Vacuum(err)
}
}

impl From<tokio::task::JoinError> for Error {
fn from(err: tokio::task::JoinError) -> Self {
Self::Join(err)
}
}

impl From<std::net::AddrParseError> for Error {
fn from(err: std::net::AddrParseError) -> Self {
Self::AddrParse(err)
}
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Json(err) => write!(f, "json error: {err}"),
Self::Mqtt(err) => write!(f, "mqtt error: {err}"),
Self::Vacuum(err) => write!(f, "vacuum error: {err}"),
Self::QueueClosed => write!(f, "vacuum queue closed"),
Self::Join(err) => write!(f, "join error: {err}"),
Self::AddrParse(err) => write!(f, "address parse error: {err}"),
}
}
}

impl std::error::Error for Error {}
39 changes: 22 additions & 17 deletions bin/elisa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ use transport::{
use log::{debug, error, info};
use paho_mqtt::{AsyncClient as MqClient, Message, MessageBuilder, PropertyCode};

pub type ErasedError = Box<dyn std::error::Error + Send + Sync>;
pub type Result<T> = std::result::Result<T, ErasedError>;
mod error;
pub use error::Error;

pub type Result<T> = std::result::Result<T, Error>;

enum VacuumRequest {
Action(Action, oneshot::Sender<Result<()>>),
Expand All @@ -39,7 +41,8 @@ impl VacuumQueue {
let result = vacuum
.status()
.await
.map(|status| (status, vacuum.last_cleaning_rooms().to_vec()));
.map(|status| (status, vacuum.last_cleaning_rooms().to_vec()))
.map_err(Error::from);
let _ = responder.send(result);
}
}
Expand All @@ -54,24 +57,20 @@ impl VacuumQueue {
self.tx
.send(VacuumRequest::Action(action, tx))
.await
.map_err(|_| queue_closed())?;
rx.await.map_err(|_| queue_closed())?
.map_err(|_| Error::QueueClosed)?;
rx.await.map_err(|_| Error::QueueClosed)?
}

pub async fn get_status(&self) -> Result<(Status, Vec<u8>)> {
let (tx, rx) = oneshot::channel();
self.tx
.send(VacuumRequest::Status(tx))
.await
.map_err(|_| queue_closed())?;
rx.await.map_err(|_| queue_closed())?
.map_err(|_| Error::QueueClosed)?;
rx.await.map_err(|_| Error::QueueClosed)?
}
}

fn queue_closed() -> ErasedError {
"vacuum queue closed".into()
}

pub async fn handle_action_request(msg: Message, mqtt: &mut MqClient, vacuum: Arc<VacuumQueue>) {
let request: ActionRequest = match serde_json::from_slice(msg.payload()) {
Ok(ids) => ids,
Expand Down Expand Up @@ -179,32 +178,38 @@ async fn perform_action(action: Action, vacuum: &mut Vacuum) -> Result<()> {
let room_ids = rooms.iter().filter_map(room_id_for_room).collect();

info!("wants to start cleaning in rooms: {:?}", rooms);
vacuum.start(room_ids).await
vacuum.start(room_ids).await?;
Ok(())
}
Action::Stop => {
info!("wants to stop cleaning");
vacuum.stop().await?;
vacuum.go_home().await
vacuum.go_home().await?;
Ok(())
}
Action::SetWorkSpeed(work_speed) => {
let mode = from_elisa_speed(work_speed);

info!("wants to set mode {:?}", mode);
vacuum.set_fan_speed(mode).await
vacuum.set_fan_speed(mode).await?;
Ok(())
}
Action::SetCleanupMode(cleanup_mode) => {
let mode = from_elisa_cleanup(cleanup_mode);

info!("wants to set cleanup mode {:?}", mode);
vacuum.set_cleanup_mode(mode).await
vacuum.set_cleanup_mode(mode).await?;
Ok(())
}
Action::Pause => {
info!("wants to pause");
vacuum.pause().await
vacuum.pause().await?;
Ok(())
}
Action::Resume => {
info!("wants to resume");
vacuum.resume().await
vacuum.resume().await?;
Ok(())
}
}
}
Expand Down
64 changes: 64 additions & 0 deletions bin/elisheba/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use std::fmt;

#[derive(Debug)]
pub enum Error {
Sonoff(sonoff::Error),
Mqtt(paho_mqtt::Error),
Json(serde_json::Error),
Join(tokio::task::JoinError),
Timeout(tokio::time::error::Elapsed),
Io(std::io::Error),
UnknownDevice,
}

impl From<sonoff::Error> for Error {
fn from(err: sonoff::Error) -> Self {
Self::Sonoff(err)
}
}

impl From<paho_mqtt::Error> for Error {
fn from(err: paho_mqtt::Error) -> Self {
Self::Mqtt(err)
}
}

impl From<serde_json::Error> for Error {
fn from(err: serde_json::Error) -> Self {
Self::Json(err)
}
}

impl From<tokio::task::JoinError> for Error {
fn from(err: tokio::task::JoinError) -> Self {
Self::Join(err)
}
}

impl From<tokio::time::error::Elapsed> for Error {
fn from(err: tokio::time::error::Elapsed) -> Self {
Self::Timeout(err)
}
}

impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Self::Io(err)
}
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Sonoff(err) => write!(f, "sonoff error: {err}"),
Self::Mqtt(err) => write!(f, "mqtt error: {err}"),
Self::Json(err) => write!(f, "json error: {err}"),
Self::Join(err) => write!(f, "join error: {err}"),
Self::Timeout(err) => write!(f, "timeout error: {err}"),
Self::Io(err) => write!(f, "io error: {err}"),
Self::UnknownDevice => write!(f, "unknown device"),
}
}
}

impl std::error::Error for Error {}
Loading