Add relay pulse functionality
This commit is contained in:
parent
e2f3d7b82a
commit
61a3c6093b
14 changed files with 201 additions and 13 deletions
emgauwa-core/src/handlers/v1
|
@ -1,9 +1,11 @@
|
|||
use actix::Addr;
|
||||
use actix_web::{get, put, web, HttpResponse};
|
||||
use actix_web::{get, post, put, web, HttpResponse};
|
||||
use emgauwa_lib::db::{DbController, DbJunctionRelaySchedule, DbRelay, DbTag};
|
||||
use emgauwa_lib::errors::{DatabaseError, EmgauwaError};
|
||||
use emgauwa_lib::models::{convert_db_list, FromDbModel, Relay};
|
||||
use emgauwa_lib::types::{ControllerUid, ControllerWsAction, RequestRelayUpdate};
|
||||
use emgauwa_lib::types::{
|
||||
ControllerUid, ControllerWsAction, RequestRelayPulse, RequestRelayUpdate,
|
||||
};
|
||||
use emgauwa_lib::utils;
|
||||
use sqlx::{Pool, Sqlite};
|
||||
|
||||
|
@ -149,3 +151,35 @@ pub async fn update_for_controller(
|
|||
|
||||
Ok(HttpResponse::Ok().json(return_relay))
|
||||
}
|
||||
|
||||
#[post("/controllers/{controller_id}/relays/{relay_num}/pulse")]
|
||||
pub async fn pulse(
|
||||
pool: web::Data<Pool<Sqlite>>,
|
||||
app_state: web::Data<Addr<AppState>>,
|
||||
path: web::Path<(String, i64)>,
|
||||
data: web::Json<RequestRelayPulse>,
|
||||
) -> Result<HttpResponse, EmgauwaError> {
|
||||
let mut pool_conn = pool.acquire().await?;
|
||||
|
||||
let (controller_uid, relay_num) = path.into_inner();
|
||||
let uid = ControllerUid::try_from(controller_uid.as_str())?;
|
||||
|
||||
let controller = DbController::get_by_uid(&mut pool_conn, &uid)
|
||||
.await?
|
||||
.ok_or(DatabaseError::NotFound)?;
|
||||
|
||||
let relay = DbRelay::get_by_controller_and_num(&mut pool_conn, &controller, relay_num)
|
||||
.await?
|
||||
.ok_or(DatabaseError::NotFound)?;
|
||||
|
||||
let duration = data.duration.filter(|&d| d > 0);
|
||||
|
||||
app_state
|
||||
.send(app_state::Action {
|
||||
controller_uid: uid,
|
||||
action: ControllerWsAction::RelayPulse((relay.number, duration)),
|
||||
})
|
||||
.await??;
|
||||
|
||||
Ok(HttpResponse::Ok().finish()) // TODO add a message?
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue