64 lines
1.7 KiB
Rust
64 lines
1.7 KiB
Rust
use easee::api::ChargerOpMode;
|
|
use ureq::json;
|
|
use anyhow::Result;
|
|
|
|
pub struct Context {
|
|
pub base: String,
|
|
pub auth_header: String,
|
|
}
|
|
|
|
pub struct Channel {
|
|
channel_id: String,
|
|
}
|
|
|
|
|
|
impl Context {
|
|
|
|
pub fn new(base: String, token: &str) -> Result<Self> {
|
|
Ok(Self {
|
|
base,
|
|
auth_header: format!("Bearer {token}"),
|
|
})
|
|
}
|
|
|
|
fn path(&self, rel: &str) -> String {
|
|
format!("{}/api/v4/{}", self.base, rel)
|
|
}
|
|
|
|
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 } ))?;
|
|
Ok(())
|
|
}
|
|
|
|
pub fn set_status(&self, mode: ChargerOpMode) -> Result<()> {
|
|
use ChargerOpMode::*;
|
|
let (text, emoji) = match mode {
|
|
Unknown => ("Unknown", "interrobang"),
|
|
Disconnected => ("Disconnected", "zzz"),
|
|
Paused => ("Paused", "double_vertical_bar"),
|
|
Charging => ("Charging", "zap"),
|
|
Finished => ("Finished", "white_check_mark"),
|
|
Error => ("Error", "no_entry_sign"),
|
|
Ready => ("Ready", "electric_plug"),
|
|
};
|
|
self.set_custom_status(text, emoji)
|
|
}
|
|
|
|
pub fn channel(&self, id: &str) -> Channel {
|
|
Channel { channel_id: id.to_owned() }
|
|
}
|
|
|
|
pub fn send_to_channel(&self, channel: &Channel, msg: &str) -> Result<()> {
|
|
let path = self.path("posts");
|
|
ureq::post(&path)
|
|
.set("Authorization", &self.auth_header)
|
|
.send_json(json!(
|
|
{ "channel_id": channel.channel_id, "message": msg }
|
|
))?;
|
|
Ok(())
|
|
}
|
|
|
|
}
|