Cargo fmt
This commit is contained in:
parent
67a27284b3
commit
e74f49277f
@ -1,5 +1,5 @@
|
||||
use serde::Deserialize;
|
||||
use anyhow::Result;
|
||||
use serde::Deserialize;
|
||||
use serde_json;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
@ -17,7 +17,7 @@ pub struct Mattermost {
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Prometheus {
|
||||
pub base: String
|
||||
pub base: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
|
@ -1,3 +1,4 @@
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::convert::Infallible;
|
||||
use std::sync::{Arc, Mutex};
|
||||
@ -81,45 +82,49 @@ pub fn start(mut ctx: Context, config: Config, mut chargers: Vec<api::Charger>)
|
||||
};
|
||||
|
||||
let _ctrl = thread::spawn(move || controller.adjust_power());
|
||||
|
||||
}
|
||||
|
||||
loop {
|
||||
|
||||
let evt = match stream.recv() {
|
||||
Ok(e) => e,
|
||||
Err(ObservationError::Stream(stream_error)) => Err(stream_error)?,
|
||||
Err(other) => { error!("Cannot process message: {}", other); continue },
|
||||
Err(other) => {
|
||||
error!("Cannot process message: {}", other);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
let mut chargers = chargers.lock().unwrap();
|
||||
|
||||
let Some(charger) = chargers.get_mut(&evt.charger)
|
||||
else { warn!("Received message for unknown charger {}", &evt.charger); continue };
|
||||
let Some(charger) = chargers.get_mut(&evt.charger) else {
|
||||
warn!("Received message for unknown charger {}", &evt.charger);
|
||||
continue;
|
||||
};
|
||||
|
||||
let result = handle_event(evt, charger, &mattermost, &channel);
|
||||
|
||||
if let Err(err) = result {
|
||||
error!("Error handling observation: {:?}", err);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_event(evt: Event, charger: &mut Charger, ctx: &mattermost::Context, channel: &Channel) -> Result<()> {
|
||||
|
||||
fn handle_event(
|
||||
evt: Event,
|
||||
charger: &mut Charger,
|
||||
ctx: &mattermost::Context,
|
||||
channel: &Channel,
|
||||
) -> Result<()> {
|
||||
let send = |msg: &str| ctx.send_to_channel(channel, msg);
|
||||
|
||||
match evt.observation {
|
||||
Observation::PilotMode(mode) => {
|
||||
match mode {
|
||||
Observation::PilotMode(mode) => match mode {
|
||||
PilotMode::Disconnected => send("Car Disconnected"),
|
||||
PilotMode::Connected => send("Car Connected"),
|
||||
PilotMode::Charging => send("Car Charging"),
|
||||
PilotMode::NeedsVentilation => send("Car needs ventilation"),
|
||||
PilotMode::FaultDetected => send("Fault detected"),
|
||||
PilotMode::Unknown => send("Unknown"),
|
||||
}
|
||||
},
|
||||
Observation::ChargerOpMode(mode) => {
|
||||
match mode {
|
||||
@ -150,9 +155,7 @@ struct Controller {
|
||||
}
|
||||
|
||||
impl Controller {
|
||||
|
||||
pub fn adjust_power(mut self) -> Result<()> {
|
||||
|
||||
let voltage = 240f64;
|
||||
let time_to_live = Some(self.delay.as_secs() as i32);
|
||||
|
||||
|
19
src/main.rs
19
src/main.rs
@ -8,10 +8,10 @@ use easee::observation;
|
||||
|
||||
use tracing::info;
|
||||
|
||||
mod prom;
|
||||
mod mattermost;
|
||||
mod config;
|
||||
mod control;
|
||||
mod mattermost;
|
||||
mod prom;
|
||||
|
||||
#[derive(Debug, Clone, Copy, ValueEnum)]
|
||||
enum Command {
|
||||
@ -87,8 +87,7 @@ fn login() -> Result<()> {
|
||||
fn load_context() -> Result<easee::api::Context> {
|
||||
let saved = std::fs::read_to_string(SAVED_TOKEN_PATH)
|
||||
.context("Cannot read saved token (did you log in ?)")?;
|
||||
let ctx = easee::api::Context::from_saved(&saved)?
|
||||
.on_refresh(save_context);
|
||||
let ctx = easee::api::Context::from_saved(&saved)?.on_refresh(save_context);
|
||||
Ok(ctx)
|
||||
}
|
||||
|
||||
@ -154,7 +153,8 @@ fn main() -> Result<()> {
|
||||
|
||||
// We need to do this before loading the context
|
||||
if let Mode::Login = args.mode {
|
||||
login()?; return Ok(())
|
||||
login()?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let mut ctx = load_context()?;
|
||||
@ -164,7 +164,10 @@ fn main() -> Result<()> {
|
||||
Mode::Login => login()?,
|
||||
Mode::List => {
|
||||
for site in ctx.sites_details()? {
|
||||
println!("Site {} (level {})", site.site.id, site.site.level_of_access);
|
||||
println!(
|
||||
"Site {} (level {})",
|
||||
site.site.id, site.site.level_of_access
|
||||
);
|
||||
for circuit in site.circuits {
|
||||
println!(" Circuit {} ({}A)", circuit.id, circuit.rated_current);
|
||||
for charger in circuit.chargers {
|
||||
@ -172,7 +175,7 @@ fn main() -> Result<()> {
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
Mode::Status => loop_chargers(&mut ctx, &args.charger_id, |c, ctx| {
|
||||
c.state(ctx).map(|s| status(&c.id, s))
|
||||
})?,
|
||||
@ -196,7 +199,7 @@ fn main() -> Result<()> {
|
||||
Mode::Power => {
|
||||
let pow = prom::PromClient::new(config.prometheus.base).current_power()?;
|
||||
println!("P1:{}W P2:{}W P3:{}W", pow.phase1, pow.phase2, pow.phase3);
|
||||
},
|
||||
}
|
||||
Mode::Control => {
|
||||
let chargers = load_chargers(&mut ctx, &args.charger_id)?;
|
||||
control::start(ctx, config, chargers)?;
|
||||
|
@ -1,6 +1,6 @@
|
||||
use anyhow::Result;
|
||||
use easee::api::ChargerOpMode;
|
||||
use ureq::json;
|
||||
use anyhow::Result;
|
||||
|
||||
pub struct Context {
|
||||
pub base: String,
|
||||
@ -11,9 +11,7 @@ pub struct Channel {
|
||||
channel_id: String,
|
||||
}
|
||||
|
||||
|
||||
impl Context {
|
||||
|
||||
pub fn new(base: String, token: &str) -> Result<Self> {
|
||||
Ok(Self {
|
||||
base,
|
||||
@ -27,8 +25,7 @@ impl Context {
|
||||
|
||||
pub fn set_custom_status(&self, text: &str, emoji: &str) -> Result<()> {
|
||||
let path = &self.path("users/me/status/custom");
|
||||
ureq::put(path)
|
||||
.send_json(json!( { "emoji": emoji, "text": text } ))?;
|
||||
ureq::put(path).send_json(json!( { "emoji": emoji, "text": text } ))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -42,12 +39,16 @@ impl Context {
|
||||
Finished => ("Finished", "white_check_mark"),
|
||||
Error => ("Error", "no_entry_sign"),
|
||||
Ready => ("Ready", "electric_plug"),
|
||||
AwaitingAuthentication => ("Awaiting authentication", "key"),
|
||||
Deauthenticating => ("Deauthenticating", "closed_lock_with_key"),
|
||||
};
|
||||
self.set_custom_status(text, emoji)
|
||||
}
|
||||
|
||||
pub fn channel(&self, id: &str) -> Channel {
|
||||
Channel { channel_id: id.to_owned() }
|
||||
Channel {
|
||||
channel_id: id.to_owned(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn send_to_channel(&self, channel: &Channel, msg: &str) -> Result<()> {
|
||||
@ -59,5 +60,4 @@ impl Context {
|
||||
))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -44,13 +44,16 @@ struct MatrixEntry {
|
||||
|
||||
pub struct PromClient {
|
||||
base: String,
|
||||
power_query_url: String
|
||||
power_query_url: String,
|
||||
}
|
||||
|
||||
impl PromClient {
|
||||
pub fn new(base: String) -> Self {
|
||||
let power_query_url = format!("{}{}", &base, PROM_QUERY);
|
||||
PromClient { base, power_query_url }
|
||||
PromClient {
|
||||
base,
|
||||
power_query_url,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn current_power(&self) -> Result<Triphase> {
|
||||
@ -81,5 +84,4 @@ impl PromClient {
|
||||
phase3: r.2.ok_or_else(|| anyhow!("Missing phase c"))?,
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user