From 4d6e9a2f00a6e83cfbe8cf313198ab921d622995 Mon Sep 17 00:00:00 2001 From: soweliniko Date: Tue, 16 Apr 2024 23:20:12 +0100 Subject: [PATCH] feat: set sign date on boot and add reset endpoint --- Cargo.lock | 11 +++++++++++ Cargo.toml | 2 +- src/main.rs | 31 +++++++++++++++++++++++++++++++ src/web_server.rs | 8 ++++++++ 4 files changed, 51 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index ffec2d7..97611c9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -786,6 +786,15 @@ dependencies = [ "libc", ] +[[package]] +name = "num_threads" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c7398b9c8b70908f6371f47ed36737907c87c52af34c268fed0bf0ceb92ead9" +dependencies = [ + "libc", +] + [[package]] name = "object" version = "0.32.2" @@ -1242,7 +1251,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5dfd88e563464686c916c7e46e623e520ddc6d79fa6641390f2e3fa86e83e885" dependencies = [ "deranged", + "libc", "num-conv", + "num_threads", "powerfmt", "serde", "time-core", diff --git a/Cargo.toml b/Cargo.toml index 92692c2..b4cc9c6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,4 +21,4 @@ tower-http = { version = "0.4.0", features = ["full"] } tracing = "0.1.37" tracing-subscriber = { version = "0.3.16", features = ["env-filter"] } alpha_sign = { path = "./alpha_sign" } -time = "0.3.36" +time = {version = "0.3.36", features = ["local-offset"]} diff --git a/src/main.rs b/src/main.rs index 0662bca..685bc64 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,9 @@ mod web_server; use crate::web_server::{app, AppState}; use alpha_sign::text::WriteText; +use alpha_sign::write_special::SetTime; +use alpha_sign::write_special::SoftReset; +use alpha_sign::write_special::WriteSpecial; use alpha_sign::Command; use alpha_sign::Packet; use alpha_sign::SignSelector; @@ -15,6 +18,8 @@ use std::{ // thread, time::Duration, }; +use time::OffsetDateTime; +use time::UtcOffset; use tokio::select; use tokio_util::sync::CancellationToken; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Layer}; @@ -51,6 +56,20 @@ async fn main() { let yhs_selector = SignSelector::default(); // yhs_selector.checksum = false; + + // TODO this is not friendly to clocks changing! + let now = OffsetDateTime::now_utc().to_offset(UtcOffset::from_hms(1, 0, 0).unwrap()); + + // sync time with sign on startup + let set_time_command = Command::WriteSpecial(WriteSpecial::SetTime(SetTime::new(now.time()))); + + port.write( + Packet::new(vec![yhs_selector], vec![set_time_command]) + .encode() + .expect("could not create time sync packet") + .as_slice(), + ) + .expect("Could not write date/time sync to sign"); let (sign_command_tx, sign_command_rx) = tokio::sync::mpsc::unbounded_channel(); @@ -135,6 +154,18 @@ async fn handle_command(sign: SignSelector, port: &mut Box, comm port.write(write_text_command.as_slice()).ok(); // TODO handle errors } + APICommand::SoftReset => { + port.write( + Packet::new( + vec![sign], + vec![Command::WriteSpecial(WriteSpecial::SoftReset(SoftReset {}))], + ) + .encode() + .unwrap() + .as_slice(), + ) + .ok(); + } APICommand::ReadText(command, tx) => { let read_text_command = Packet::new(vec![sign], vec![Command::ReadText(command)]) .encode() diff --git a/src/web_server.rs b/src/web_server.rs index c89ed3c..8419105 100644 --- a/src/web_server.rs +++ b/src/web_server.rs @@ -39,6 +39,7 @@ pub enum APIResponse { pub enum APICommand { WriteText(WriteText), ReadText(ReadText, Sender), + SoftReset, } impl AppState { @@ -92,6 +93,7 @@ pub fn app(state: AppState) -> Router { //.route("/script", post(post_script_handler)) .route("/text/:textKey", put(put_text_handler)) .route("/text/get/:label", get(get_text_handler)) + .route("/reset", get(get_soft_reset_handler)) .layer(middleware) .with_state(state) .fallback_service(ServeDir::new("static")) @@ -168,3 +170,9 @@ async fn get_text_handler( Err(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(), } } + +#[axum::debug_handler] +async fn get_soft_reset_handler(state: State) -> impl IntoResponse { + state.command_tx.send(APICommand::SoftReset).ok(); + StatusCode::OK +}