From 8ce9b1558bfba319f1405849884d823600951dfe Mon Sep 17 00:00:00 2001 From: Daniel Maslowski Date: Wed, 22 May 2024 19:15:23 +0200 Subject: [PATCH] write initial status if it doesn't exist --- src/main.rs | 42 +++++++++++++++++++++++++++++++----------- status.json | 1 - 2 files changed, 31 insertions(+), 12 deletions(-) delete mode 100644 status.json diff --git a/src/main.rs b/src/main.rs index 06f2827..40cb67d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,23 +16,40 @@ const STATUS_FILE: &str = "status.json"; #[tokio::main] async fn main() { - // initialize tracing - // tracing_subscriber::fmt::init(); - - // build our application with a route let app = Router::new() .route("/status.json", get(root)) .route("/api/update.php", post(the_doors)); - // run our app with hyper, listening globally on port 3000 let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); axum::serve(listener, app).await.unwrap(); } -// Just output the current file. It may cease to exist. +// Initialize empty status. +fn init_status() -> Status { + let new_doors = status::TheDoors { + aerie: false, + cellar: false, + }; + let (sensors, state) = status::update(new_doors); + status::status(sensors, state) +} + +// Write status to file and return JSON string. +fn write_status(s: Status) -> String { + let s = serde_json::to_string(&s).unwrap(); + let mut file = File::create(STATUS_FILE).unwrap(); + file.write_all(s.as_bytes()).unwrap(); + s +} + +// Just output the current file. We assume it to be consistent. +// It may cease to or not yet exist. Then create an initial status and persist. async fn root() -> String { - // TODO: handle error - std::fs::read_to_string(STATUS_FILE).unwrap_or(String::from("KAPOTT")) + if std::path::Path::new(STATUS_FILE).exists() { + return std::fs::read_to_string(STATUS_FILE).unwrap_or(String::from("KAPOTT")); + } + let s = init_status(); + write_status(s) } // Input type for the API: Both fields are optional. @@ -45,9 +62,12 @@ struct TheDoors { // The door can see through your soul. // https://www.youtube.com/watch?v=bDQDp00oTP4 async fn the_doors(Json(payload): Json) -> StatusCode { - // TODO: handle error - let contents = std::fs::read_to_string(STATUS_FILE).expect("FCKAFD"); - let status: Status = serde_json::from_str(&contents).unwrap(); + let status: Status = if std::path::Path::new(STATUS_FILE).exists() { + let contents = std::fs::read_to_string(STATUS_FILE).expect("FCKAFD"); + serde_json::from_str(&contents).unwrap_or_else(|_| init_status()) + } else { + init_status() + }; if let Some(sens) = status.sensors { // Get the current status as read from the file. diff --git a/status.json b/status.json deleted file mode 100644 index 421ce85..0000000 --- a/status.json +++ /dev/null @@ -1 +0,0 @@ -{"api_compatibility":["14"],"space":"Chaospott","logo":"https://chaospott.de/images/logo.png","url":"https://chaospott.de","location":{"address":"Sibyllastr. 9, 45136 Essen, Germany","lat":51.438476,"lon":7.024991},"contact":{"phone":"+49 201 85892243","irc":"irc://irc.hackint.org/#chaospott","email":"info@chaospott.de","ml":"discuss@lists.chaospott.de","issue_mail":"support@chaospott.de","mumble":"mumble://mumble.chaospott.de","matrix":"#chaospott:matrix.chaospott.de","mastodon":"https://chaos.social/@chaospott"},"state":{"open":false},"sensors":{"door_locked":[{"location":"aerie","value":false},{"location":"cellar","value":false}]}} \ No newline at end of file