Skip to content
Open
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
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]}
31 changes: 31 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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};
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -135,6 +154,18 @@ async fn handle_command(sign: SignSelector, port: &mut Box<dyn SerialPort>, 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()
Expand Down
8 changes: 8 additions & 0 deletions src/web_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub enum APIResponse {
pub enum APICommand {
WriteText(WriteText),
ReadText(ReadText, Sender<APIResponse>),
SoftReset,
}

impl AppState {
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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<AppState>) -> impl IntoResponse {
state.command_tx.send(APICommand::SoftReset).ok();
StatusCode::OK
}