write initial status if it doesn't exist
This commit is contained in:
parent
66942e614c
commit
8ce9b1558b
40
src/main.rs
40
src/main.rs
@ -16,23 +16,40 @@ const STATUS_FILE: &str = "status.json";
|
|||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
// initialize tracing
|
|
||||||
// tracing_subscriber::fmt::init();
|
|
||||||
|
|
||||||
// build our application with a route
|
|
||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
.route("/status.json", get(root))
|
.route("/status.json", get(root))
|
||||||
.route("/api/update.php", post(the_doors));
|
.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();
|
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
|
||||||
axum::serve(listener, app).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 {
|
async fn root() -> String {
|
||||||
// TODO: handle error
|
if std::path::Path::new(STATUS_FILE).exists() {
|
||||||
std::fs::read_to_string(STATUS_FILE).unwrap_or(String::from("KAPOTT"))
|
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.
|
// Input type for the API: Both fields are optional.
|
||||||
@ -45,9 +62,12 @@ struct TheDoors {
|
|||||||
// The door can see through your soul.
|
// The door can see through your soul.
|
||||||
// https://www.youtube.com/watch?v=bDQDp00oTP4
|
// https://www.youtube.com/watch?v=bDQDp00oTP4
|
||||||
async fn the_doors(Json(payload): Json<TheDoors>) -> StatusCode {
|
async fn the_doors(Json(payload): Json<TheDoors>) -> StatusCode {
|
||||||
// TODO: handle error
|
let status: Status = if std::path::Path::new(STATUS_FILE).exists() {
|
||||||
let contents = std::fs::read_to_string(STATUS_FILE).expect("FCKAFD");
|
let contents = std::fs::read_to_string(STATUS_FILE).expect("FCKAFD");
|
||||||
let status: Status = serde_json::from_str(&contents).unwrap();
|
serde_json::from_str(&contents).unwrap_or_else(|_| init_status())
|
||||||
|
} else {
|
||||||
|
init_status()
|
||||||
|
};
|
||||||
|
|
||||||
if let Some(sens) = status.sensors {
|
if let Some(sens) = status.sensors {
|
||||||
// Get the current status as read from the file.
|
// Get the current status as read from the file.
|
||||||
|
@ -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}]}}
|
|
Loading…
Reference in New Issue
Block a user