improvements to netcode and new functionality

This commit is contained in:
2025-01-19 15:57:19 +01:00
parent 765fca635a
commit 24f8a35162
4 changed files with 65 additions and 46 deletions

View File

@@ -1,6 +1,7 @@
use std::path::Path;
use std::fs::File;
use std::io::{ErrorKind, Read};
use json::Error as JsonError;
pub struct JsonStruct {
pub ha_url: String,
@@ -50,4 +51,15 @@ fn read_file(path: &Path) -> String {
_ => panic!("[ERROR] Unexpected error reading the JSON"),
}
}
}
pub fn get_state(response: String) -> Result<Option<bool>, JsonError> {
let json = json::parse(response.as_str())?;
let state = json::stringify(json[0]["state"].as_str());
match &state[1..&state.len()-1] {
"on" => Ok(Some(true)),
"off" => Ok(Some(false)),
_ => Ok(None),
}
}