improvements to netcode and new functionality
This commit is contained in:
12
src/json.rs
12
src/json.rs
@@ -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),
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user