Compare commits
18 Commits
9643d8e592
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 21cb416f41 | |||
| 3ccee9f897 | |||
| 2266d7ecef | |||
| 0a5891b5be | |||
| d1a5666d18 | |||
| 4a4dea12ce | |||
| 7cdc180a6c | |||
| 44d9f9bb41 | |||
| 7881799e84 | |||
| bc1a15f27c | |||
| f2c5b86909 | |||
| dfd87f2af1 | |||
| 7f93f2d0b7 | |||
| 6b6df9eaca | |||
| 4470f281fc | |||
| 28fdd10e0a | |||
| dbbfbadf7f | |||
| 5dd8761bc0 |
+4
-1
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "easee"
|
name = "easee"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
authors = ["Maxime Augier <max@xolus.net>"]
|
authors = ["Maxime Augier <max@xolus.net>"]
|
||||||
description = "Rust bindings for the Easee cloud API for EV charging devices"
|
description = "Rust bindings for the Easee cloud API for EV charging devices"
|
||||||
@@ -22,3 +22,6 @@ thiserror = "1.0.63"
|
|||||||
tracing = "0.1.40"
|
tracing = "0.1.40"
|
||||||
tungstenite = { version = "0.23.0", optional = true, features = ["rustls-tls-native-roots"] }
|
tungstenite = { version = "0.23.0", optional = true, features = ["rustls-tls-native-roots"] }
|
||||||
ureq = { version = "2.10.0", features = ["json"] }
|
ureq = { version = "2.10.0", features = ["json"] }
|
||||||
|
|
||||||
|
[features]
|
||||||
|
default = ["tungstenite"]
|
||||||
|
|||||||
+181
-17
@@ -1,5 +1,6 @@
|
|||||||
use std::{
|
use std::{
|
||||||
io,
|
io,
|
||||||
|
ops::{Add, Mul, Sub},
|
||||||
time::{Duration, Instant, SystemTime, UNIX_EPOCH},
|
time::{Duration, Instant, SystemTime, UNIX_EPOCH},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -8,12 +9,22 @@ use serde_repr::Deserialize_repr;
|
|||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
use tracing::{debug, info, instrument};
|
use tracing::{debug, info, instrument};
|
||||||
|
|
||||||
/// API Authentication context
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Context {
|
pub struct Context {
|
||||||
auth_header: String, //TODO mark as secret to hide in tracing
|
auth_header: String,
|
||||||
refresh_token: String,
|
refresh_token: String,
|
||||||
token_expiration: Instant,
|
token_expiration: Instant,
|
||||||
|
on_refresh: Option<Box<dyn FnMut(&mut Self) + Send>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Debug for Context {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
f.debug_struct("Context")
|
||||||
|
.field("auth_header", &"<secret>")
|
||||||
|
.field("refresh_token", &"<secret>")
|
||||||
|
.field("token_expiration", &self.token_expiration)
|
||||||
|
.field("on_refresh", &"[closure]")
|
||||||
|
.finish()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const API_BASE: &str = "https://api.easee.com/api/";
|
const API_BASE: &str = "https://api.easee.com/api/";
|
||||||
@@ -45,6 +56,66 @@ impl<'de> Deserialize<'de> for UtcDateTime {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize)]
|
||||||
|
pub struct Triphase {
|
||||||
|
pub phase1: f64,
|
||||||
|
pub phase2: f64,
|
||||||
|
pub phase3: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Add<Triphase> for Triphase {
|
||||||
|
type Output = Triphase;
|
||||||
|
|
||||||
|
fn add(self, rhs: Triphase) -> Self::Output {
|
||||||
|
Triphase {
|
||||||
|
phase1: self.phase1 + rhs.phase1,
|
||||||
|
phase2: self.phase2 + rhs.phase2,
|
||||||
|
phase3: self.phase3 + rhs.phase3,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Sub<Triphase> for Triphase {
|
||||||
|
type Output = Triphase;
|
||||||
|
|
||||||
|
fn sub(self, rhs: Triphase) -> Self::Output {
|
||||||
|
Triphase {
|
||||||
|
phase1: self.phase1 + rhs.phase1,
|
||||||
|
phase2: self.phase2 + rhs.phase2,
|
||||||
|
phase3: self.phase3 + rhs.phase3,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Mul<f64> for Triphase {
|
||||||
|
type Output = Triphase;
|
||||||
|
|
||||||
|
fn mul(self, rhs: f64) -> Self::Output {
|
||||||
|
Triphase {
|
||||||
|
phase1: self.phase1 * rhs,
|
||||||
|
phase2: self.phase2 * rhs,
|
||||||
|
phase3: self.phase3 * rhs,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<f64> for Triphase {
|
||||||
|
fn from(value: f64) -> Self {
|
||||||
|
Triphase {
|
||||||
|
phase1: value,
|
||||||
|
phase2: value,
|
||||||
|
phase3: value,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Serialize)]
|
||||||
|
pub struct SetCurrent {
|
||||||
|
pub time_to_live: Option<i32>,
|
||||||
|
#[serde(flatten)]
|
||||||
|
pub current: Triphase,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd)]
|
#[derive(Clone, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct Charger {
|
pub struct Charger {
|
||||||
@@ -60,12 +131,15 @@ pub struct Charger {
|
|||||||
#[derive(Clone, Copy, Debug, Deserialize_repr, Eq, Ord, PartialEq, PartialOrd)]
|
#[derive(Clone, Copy, Debug, Deserialize_repr, Eq, Ord, PartialEq, PartialOrd)]
|
||||||
#[repr(u8)]
|
#[repr(u8)]
|
||||||
pub enum ChargerOpMode {
|
pub enum ChargerOpMode {
|
||||||
|
Unknown = 0,
|
||||||
Disconnected = 1,
|
Disconnected = 1,
|
||||||
Paused = 2,
|
Paused = 2,
|
||||||
Charging = 3,
|
Charging = 3,
|
||||||
Finished = 4,
|
Finished = 4,
|
||||||
Error = 5,
|
Error = 5,
|
||||||
Ready = 6,
|
Ready = 6,
|
||||||
|
AwaitingAuthentication = 7,
|
||||||
|
Deauthenticating = 8,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, Deserialize_repr, Eq, Ord, PartialEq, PartialOrd)]
|
#[derive(Clone, Copy, Debug, Deserialize_repr, Eq, Ord, PartialEq, PartialOrd)]
|
||||||
@@ -193,6 +267,28 @@ pub struct Site {
|
|||||||
pub installer_alias: Option<String>,
|
pub installer_alias: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Deserialize)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
pub struct SiteDetails {
|
||||||
|
#[serde(flatten)]
|
||||||
|
pub site: Site,
|
||||||
|
pub circuits: Vec<Circuit>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Deserialize)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
pub struct Circuit {
|
||||||
|
pub id: u32,
|
||||||
|
pub uuid: String,
|
||||||
|
pub site_id: u32,
|
||||||
|
pub circuit_panel_id: i64,
|
||||||
|
pub panel_name: String,
|
||||||
|
pub rated_current: f64,
|
||||||
|
pub fuse: f64,
|
||||||
|
pub chargers: Vec<Charger>,
|
||||||
|
pub use_dynamic_master: bool,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Deserialize)]
|
#[derive(Clone, Debug, Deserialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct LoginResponse {
|
pub struct LoginResponse {
|
||||||
@@ -235,7 +331,7 @@ pub enum ApiError {
|
|||||||
FormatError(#[from] chrono::ParseError),
|
FormatError(#[from] chrono::ParseError),
|
||||||
|
|
||||||
#[error("Invalid ID: {0:?}")]
|
#[error("Invalid ID: {0:?}")]
|
||||||
InvalidID(String)
|
InvalidID(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<ureq::Error> for ApiError {
|
impl From<ureq::Error> for ApiError {
|
||||||
@@ -267,34 +363,50 @@ pub enum TokenParseError {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Context {
|
impl Context {
|
||||||
|
|
||||||
fn from_login_response(resp: LoginResponse) -> Self {
|
fn from_login_response(resp: LoginResponse) -> Self {
|
||||||
Self {
|
Self {
|
||||||
auth_header: format!("Bearer {}", &resp.access_token),
|
auth_header: format!("Bearer {}", &resp.access_token),
|
||||||
refresh_token: resp.refresh_token,
|
refresh_token: resp.refresh_token,
|
||||||
token_expiration: (Instant::now() + Duration::from_secs(resp.expires_in as u64))
|
token_expiration: (Instant::now() + Duration::from_secs(resp.expires_in as u64)),
|
||||||
|
on_refresh: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_saved(saved: &str) -> Result<Self, TokenParseError> {
|
pub fn from_saved(saved: &str) -> Result<Self, TokenParseError> {
|
||||||
let lines: Vec<&str> = saved.lines().collect();
|
let lines: Vec<&str> = saved.lines().collect();
|
||||||
let &[token, refresh, expire] = &*lines else { return Err(TokenParseError::IncorrectLineCount) };
|
let &[token, refresh, expire] = &*lines else {
|
||||||
|
return Err(TokenParseError::IncorrectLineCount);
|
||||||
|
};
|
||||||
|
|
||||||
let expire: u64 = expire.parse()?;
|
let expire: u64 = expire.parse()?;
|
||||||
let token_expiration = Instant::now() + (UNIX_EPOCH + Duration::from_secs(expire)).duration_since(SystemTime::now()).unwrap_or_default();
|
let token_expiration = Instant::now()
|
||||||
|
+ (UNIX_EPOCH + Duration::from_secs(expire))
|
||||||
|
.duration_since(SystemTime::now())
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
auth_header: format!("Bearer {}", token),
|
auth_header: format!("Bearer {}", token),
|
||||||
refresh_token: refresh.to_owned(),
|
refresh_token: refresh.to_owned(),
|
||||||
token_expiration,
|
token_expiration,
|
||||||
|
on_refresh: None,
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn on_refresh<F: FnMut(&mut Self) + Send + 'static>(mut self, on_refresh: F) -> Self {
|
||||||
|
self.on_refresh = Some(Box::new(on_refresh));
|
||||||
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn save(&self) -> String {
|
pub fn save(&self) -> String {
|
||||||
let expiration = (SystemTime::now() + (self.token_expiration - Instant::now())).duration_since(UNIX_EPOCH)
|
let expiration = (SystemTime::now() + (self.token_expiration - Instant::now()))
|
||||||
|
.duration_since(UNIX_EPOCH)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
format!("{}\n{}\n{}\n", self.auth_token(), self.refresh_token, expiration.as_secs())
|
format!(
|
||||||
|
"{}\n{}\n{}\n",
|
||||||
|
self.auth_token(),
|
||||||
|
self.refresh_token,
|
||||||
|
expiration.as_secs()
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Retrieve access tokens online, by logging in with the provided credentials
|
/// Retrieve access tokens online, by logging in with the provided credentials
|
||||||
@@ -358,6 +470,10 @@ impl Context {
|
|||||||
self.get("sites")
|
self.get("sites")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn site(&mut self, id: i32) -> Result<SiteDetails, ApiError> {
|
||||||
|
self.get(&format!("sites/{id}"))
|
||||||
|
}
|
||||||
|
|
||||||
/// List all chargers available to the user
|
/// List all chargers available to the user
|
||||||
pub fn chargers(&mut self) -> Result<Vec<Charger>, ApiError> {
|
pub fn chargers(&mut self) -> Result<Vec<Charger>, ApiError> {
|
||||||
self.get("chargers")
|
self.get("chargers")
|
||||||
@@ -365,11 +481,37 @@ impl Context {
|
|||||||
|
|
||||||
pub fn charger(&mut self, id: &str) -> Result<Charger, ApiError> {
|
pub fn charger(&mut self, id: &str) -> Result<Charger, ApiError> {
|
||||||
if !id.chars().all(char::is_alphanumeric) {
|
if !id.chars().all(char::is_alphanumeric) {
|
||||||
return Err(ApiError::InvalidID(id.to_owned()))
|
return Err(ApiError::InvalidID(id.to_owned()));
|
||||||
}
|
}
|
||||||
self.get(&format!("chargers/{}", id))
|
self.get(&format!("chargers/{}", id))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn circuit(&mut self, site_id: u32, circuit_id: u32) -> Result<Circuit, ApiError> {
|
||||||
|
self.get(&format!("site/{site_id}/circuit/{circuit_id}"))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn circuit_dynamic_current(
|
||||||
|
&mut self,
|
||||||
|
site_id: u32,
|
||||||
|
circuit_id: u32,
|
||||||
|
) -> Result<Triphase, ApiError> {
|
||||||
|
self.get(&format!(
|
||||||
|
"sites/{site_id}/circuits/{circuit_id}/dynamicCurrent"
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_circuit_dynamic_current(
|
||||||
|
&mut self,
|
||||||
|
site_id: u32,
|
||||||
|
circuit_id: u32,
|
||||||
|
current: SetCurrent,
|
||||||
|
) -> Result<(), ApiError> {
|
||||||
|
self.post(
|
||||||
|
&format!("sites/{site_id}/circuits/{circuit_id}/dynamicCurrent"),
|
||||||
|
¤t,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
#[instrument]
|
#[instrument]
|
||||||
fn get<T: DeserializeOwned>(&mut self, path: &str) -> Result<T, ApiError> {
|
fn get<T: DeserializeOwned>(&mut self, path: &str) -> Result<T, ApiError> {
|
||||||
self.check_expired()?;
|
self.check_expired()?;
|
||||||
@@ -445,6 +587,28 @@ impl Site {
|
|||||||
pub fn lifetime_energy(&self, ctx: &mut Context) -> Result<Vec<MeterReading>, ApiError> {
|
pub fn lifetime_energy(&self, ctx: &mut Context) -> Result<Vec<MeterReading>, ApiError> {
|
||||||
ctx.get(&format!("sites/{}/energy", self.id))
|
ctx.get(&format!("sites/{}/energy", self.id))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn details(&self, ctx: &mut Context) -> Result<SiteDetails, ApiError> {
|
||||||
|
ctx.get(&format!("sites/{}", self.id))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Circuit {
|
||||||
|
fn dynamic_current_path(&self) -> String {
|
||||||
|
format!("sites/{}/circuits/{}/dynamicCurrent", self.site_id, self.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn dynamic_current(&self, ctx: &mut Context) -> Result<Triphase, ApiError> {
|
||||||
|
ctx.circuit_dynamic_current(self.site_id, self.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_dynamic_current(
|
||||||
|
&self,
|
||||||
|
ctx: &mut Context,
|
||||||
|
current: SetCurrent,
|
||||||
|
) -> Result<(), ApiError> {
|
||||||
|
ctx.post(&self.dynamic_current_path(), ¤t)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Charger {
|
impl Charger {
|
||||||
@@ -495,7 +659,6 @@ impl Charger {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
@@ -503,10 +666,12 @@ mod test {
|
|||||||
use super::Context;
|
use super::Context;
|
||||||
#[test]
|
#[test]
|
||||||
fn token_save() {
|
fn token_save() {
|
||||||
|
let ctx = Context {
|
||||||
let ctx = Context { auth_header: "Bearer aaaaaaa0".to_owned()
|
auth_header: "Bearer aaaaaaa0".to_owned(),
|
||||||
, refresh_token: "abcdef".to_owned()
|
refresh_token: "abcdef".to_owned(),
|
||||||
, token_expiration: Instant::now() + Duration::from_secs(1234) };
|
token_expiration: Instant::now() + Duration::from_secs(1234),
|
||||||
|
on_refresh: None,
|
||||||
|
};
|
||||||
|
|
||||||
let saved = ctx.save();
|
let saved = ctx.save();
|
||||||
let ctx2 = Context::from_saved(&saved).unwrap();
|
let ctx2 = Context::from_saved(&saved).unwrap();
|
||||||
@@ -514,6 +679,5 @@ mod test {
|
|||||||
assert_eq!(&ctx.auth_header, &ctx2.auth_header);
|
assert_eq!(&ctx.auth_header, &ctx2.auth_header);
|
||||||
assert_eq!(&ctx.refresh_token, &ctx2.refresh_token);
|
assert_eq!(&ctx.refresh_token, &ctx2.refresh_token);
|
||||||
assert!((ctx.token_expiration - ctx2.token_expiration) < Duration::from_secs(5))
|
assert!((ctx.token_expiration - ctx2.token_expiration) < Duration::from_secs(5))
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,4 +6,5 @@ pub mod stream;
|
|||||||
#[cfg(feature = "tungstenite")]
|
#[cfg(feature = "tungstenite")]
|
||||||
pub mod signalr;
|
pub mod signalr;
|
||||||
|
|
||||||
|
#[cfg(feature = "tungstenite")]
|
||||||
pub mod observation;
|
pub mod observation;
|
||||||
|
|||||||
+346
-7
@@ -1,5 +1,20 @@
|
|||||||
use crate::api::ChargerOpMode;
|
use serde::{
|
||||||
|
de::{DeserializeOwned, IntoDeserializer},
|
||||||
|
Deserialize,
|
||||||
|
};
|
||||||
|
use serde_repr::Deserialize_repr;
|
||||||
|
use std::num::{ParseFloatError, ParseIntError};
|
||||||
|
use thiserror::Error;
|
||||||
|
use tracing::info;
|
||||||
|
use ureq::json;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
api::{ChargerOpMode, Context, OutputPhase, UtcDateTime},
|
||||||
|
signalr::{self, StreamError},
|
||||||
|
stream::NegotiateError,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, Deserialize_repr)]
|
||||||
#[repr(u8)]
|
#[repr(u8)]
|
||||||
pub enum PilotMode {
|
pub enum PilotMode {
|
||||||
Disconnected = b'A',
|
Disconnected = b'A',
|
||||||
@@ -7,8 +22,24 @@ pub enum PilotMode {
|
|||||||
Charging = b'C',
|
Charging = b'C',
|
||||||
NeedsVentilation = b'D',
|
NeedsVentilation = b'D',
|
||||||
FaultDetected = b'F',
|
FaultDetected = b'F',
|
||||||
|
Unknown = b'\x00',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<&str> for PilotMode {
|
||||||
|
fn from(value: &str) -> Self {
|
||||||
|
use PilotMode::*;
|
||||||
|
match value {
|
||||||
|
"A" => Disconnected,
|
||||||
|
"B" => Connected,
|
||||||
|
"C" => Charging,
|
||||||
|
"D" => NeedsVentilation,
|
||||||
|
"F" => FaultDetected,
|
||||||
|
_ => Unknown,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, Deserialize_repr)]
|
||||||
#[repr(u8)]
|
#[repr(u8)]
|
||||||
pub enum PhaseMode {
|
pub enum PhaseMode {
|
||||||
Ignore = 0,
|
Ignore = 0,
|
||||||
@@ -17,24 +48,332 @@ pub enum PhaseMode {
|
|||||||
Phase2 = 3,
|
Phase2 = 3,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum ReasonForNoCurrent {}
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
pub enum InputPin {
|
||||||
|
T1,
|
||||||
|
T2,
|
||||||
|
T3,
|
||||||
|
T4,
|
||||||
|
T5,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, Deserialize_repr)]
|
||||||
|
#[repr(u8)]
|
||||||
|
enum DataType {
|
||||||
|
Boolean = 2,
|
||||||
|
Double = 3,
|
||||||
|
Integer = 4,
|
||||||
|
String = 6,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub enum ObservationData {
|
||||||
|
Boolean(bool),
|
||||||
|
Double(f64),
|
||||||
|
Integer(i64),
|
||||||
|
String(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Error, Debug)]
|
||||||
|
pub enum ParseError {
|
||||||
|
#[error("integer `{0}`: {1}")]
|
||||||
|
Integer(String, ParseIntError),
|
||||||
|
|
||||||
|
#[error("double `{0}: {1}")]
|
||||||
|
Double(String, ParseFloatError),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ObservationData {
|
||||||
|
fn from_dynamic(value: String, data_type: DataType) -> Result<ObservationData, ParseError> {
|
||||||
|
Ok(match data_type {
|
||||||
|
DataType::Boolean => ObservationData::Boolean(match &*value {
|
||||||
|
"False" | "false" => false,
|
||||||
|
"True" | "true" => true,
|
||||||
|
other => {
|
||||||
|
other
|
||||||
|
.parse::<i64>()
|
||||||
|
.map_err(move |e| ParseError::Integer(value, e))?
|
||||||
|
!= 0
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
DataType::Double => ObservationData::Double(
|
||||||
|
value
|
||||||
|
.parse()
|
||||||
|
.map_err(move |e| ParseError::Double(value, e))?,
|
||||||
|
),
|
||||||
|
DataType::Integer => ObservationData::Integer(
|
||||||
|
value
|
||||||
|
.parse()
|
||||||
|
.map_err(move |e| ParseError::Integer(value, e))?,
|
||||||
|
),
|
||||||
|
DataType::String => ObservationData::String(value),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
fn dynamic_type(&self) -> DataType {
|
||||||
|
match self {
|
||||||
|
ObservationData::Boolean(_) => DataType::Boolean,
|
||||||
|
ObservationData::Double(_) => DataType::Double,
|
||||||
|
ObservationData::Integer(_) => DataType::Integer,
|
||||||
|
ObservationData::String(_) => DataType::String,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
|
pub struct ReasonForNoCurrent(u16);
|
||||||
|
|
||||||
|
impl std::fmt::Display for ReasonForNoCurrent {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"{}",
|
||||||
|
match self.0 {
|
||||||
|
0 => "OK",
|
||||||
|
1 => "LoadBalance: circuit too low",
|
||||||
|
2 => "LoadBalance: dynamic circuit too low",
|
||||||
|
3 => "LoadBalance: max dynamic offline",
|
||||||
|
4 => "LoadBalance: circuit fuse too low",
|
||||||
|
5 => "LoadBalance: waiting in queue",
|
||||||
|
6 => "LoadBalance: waiting in charged queue",
|
||||||
|
7 => "Error: illegal grid type",
|
||||||
|
8 => "Error: not received request from car",
|
||||||
|
9 => "Error: master communication lost",
|
||||||
|
10 => "Error: no current from equalizer",
|
||||||
|
11 => "Error: no current, phase disconnected",
|
||||||
|
25 => "Error: limited by circuit fuse",
|
||||||
|
26 => "Error: limited by circuit max current",
|
||||||
|
27 => "Error: limited by dynamic circuit current",
|
||||||
|
28 => "Error: limited by equalizer",
|
||||||
|
29 => "Error: limited by circuit load balancing",
|
||||||
|
30 => "Error: limited by offline settings",
|
||||||
|
53 => "Info: charger disabled",
|
||||||
|
54 => "Waiting: pending schedule",
|
||||||
|
55 => "Waiting: pending authorization",
|
||||||
|
56 => "Error: charger in error state",
|
||||||
|
57 => "Error: Erratic EV",
|
||||||
|
75 => "Cable: limited by cable rating",
|
||||||
|
76 => "Schedule: limited by schedule",
|
||||||
|
77 => "Charger limit: limited by charger max current",
|
||||||
|
78 => "Charger Limit: limited by dynamic charger current",
|
||||||
|
79 => "Car limit: limited by car not charging",
|
||||||
|
80 => "Local: limited by local adjustment",
|
||||||
|
81 => "Car limit: limited by car",
|
||||||
|
100 => "Error: undefined",
|
||||||
|
other => return write!(f, "Code {other}"),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub enum Observation {
|
pub enum Observation {
|
||||||
SelfTestResult(String),
|
SelfTestResult(String),
|
||||||
SelfTestDetails(serde_json::Value),
|
SelfTestDetails(String),
|
||||||
WifiEvent(u64),
|
WifiEvent(i64),
|
||||||
ChargerOfflineReason(u64),
|
ChargerOfflineReason(i64),
|
||||||
CircuitMaxCurrent { phase: u8, amperes: u64 },
|
CircuitMaxCurrent { phase: u8, amperes: f64 },
|
||||||
SiteID(String),
|
SiteID(String),
|
||||||
IsEnabled(bool),
|
IsEnabled(bool),
|
||||||
Temperature(u64),
|
Temperature(i64),
|
||||||
TriplePhase(bool),
|
TriplePhase(bool),
|
||||||
DynamicChargerCurrent(f64),
|
DynamicChargerCurrent(f64),
|
||||||
|
CircuitTotalCurrent { phase: u8, amperes: f64 },
|
||||||
|
|
||||||
|
ICCID(String),
|
||||||
|
MobileNetworkOperator(String),
|
||||||
|
|
||||||
ReasonForNoCurrent(ReasonForNoCurrent),
|
ReasonForNoCurrent(ReasonForNoCurrent),
|
||||||
|
|
||||||
|
LocalPreauthEnabled(bool),
|
||||||
|
LocalOfflineAuthEnabled(bool),
|
||||||
|
AllowOfflineTxUnknownId(bool),
|
||||||
|
|
||||||
PilotMode(PilotMode),
|
PilotMode(PilotMode),
|
||||||
SmartCharging(bool),
|
SmartCharging(bool),
|
||||||
CableLocked(bool),
|
CableLocked(bool),
|
||||||
CableRating(f64),
|
CableRating(f64),
|
||||||
|
|
||||||
|
LedMode(i64),
|
||||||
|
|
||||||
UserId(String),
|
UserId(String),
|
||||||
ChargerOpMode(ChargerOpMode),
|
ChargerOpMode(ChargerOpMode),
|
||||||
|
IntCurrent { pin: InputPin, current: f64 },
|
||||||
|
IntVoltage { pins: (InputPin, InputPin), voltage: f64 },
|
||||||
|
|
||||||
|
ActiveOutputPhase(OutputPhase),
|
||||||
|
MaximumTemperature(i64),
|
||||||
|
|
||||||
|
TotalPower(f64),
|
||||||
|
EnergyPerHour(f64),
|
||||||
|
LifetimeEnergy(f64),
|
||||||
|
LifetimeRelaySwitches(i64),
|
||||||
|
LifetimeHours(i64),
|
||||||
|
|
||||||
|
Unknown { code: u16, value: ObservationData },
|
||||||
|
}
|
||||||
|
|
||||||
|
fn op_mode_from_int(mode: i64) -> ChargerOpMode {
|
||||||
|
use ChargerOpMode::*;
|
||||||
|
match mode {
|
||||||
|
1 => Disconnected,
|
||||||
|
2 => Paused,
|
||||||
|
3 => Charging,
|
||||||
|
4 => Finished,
|
||||||
|
5 => Error,
|
||||||
|
6 => Ready,
|
||||||
|
_ => Unknown,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_i64<T: DeserializeOwned>(value: i64) -> Option<T> {
|
||||||
|
T::deserialize(<i64 as IntoDeserializer<serde::de::value::Error>>::into_deserializer(value))
|
||||||
|
.ok()
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Observation {
|
||||||
|
fn try_from_data(code: u16, data: ObservationData) -> Observation {
|
||||||
|
use InputPin::*;
|
||||||
|
use Observation::*;
|
||||||
|
use ObservationData::*;
|
||||||
|
match (code, data) {
|
||||||
|
(1, String(result)) => SelfTestResult(result),
|
||||||
|
(2, String(details)) => SelfTestDetails(details),
|
||||||
|
(10, Integer(wifi)) => WifiEvent(wifi),
|
||||||
|
(11, Integer(reason)) => ChargerOfflineReason(reason),
|
||||||
|
(22, Double(amperes)) => CircuitMaxCurrent { phase: 1, amperes },
|
||||||
|
(23, Double(amperes)) => CircuitMaxCurrent { phase: 2, amperes },
|
||||||
|
(24, Double(amperes)) => CircuitMaxCurrent { phase: 3, amperes },
|
||||||
|
(26, String(site)) => SiteID(site),
|
||||||
|
(31, Boolean(enabled)) => IsEnabled(enabled),
|
||||||
|
(32, Integer(temperature)) => Temperature(temperature),
|
||||||
|
(38, Integer(1)) => TriplePhase(false),
|
||||||
|
(38, Integer(3)) => TriplePhase(true),
|
||||||
|
(46, Integer(mode)) => LedMode(mode),
|
||||||
|
(48, Double(current)) => DynamicChargerCurrent(current),
|
||||||
|
(73, Double(amperes)) => CircuitTotalCurrent { phase: 1, amperes },
|
||||||
|
(74, Double(amperes)) => CircuitTotalCurrent { phase: 2, amperes },
|
||||||
|
(75, Double(amperes)) => CircuitTotalCurrent { phase: 3, amperes },
|
||||||
|
(81, String(iccid)) => ICCID(iccid),
|
||||||
|
(84, String(operator)) => MobileNetworkOperator(operator),
|
||||||
|
(96, Integer(reason)) => ReasonForNoCurrent(self::ReasonForNoCurrent(reason as u16)),
|
||||||
|
(100, String(l)) => PilotMode(super::observation::PilotMode::from(&*l)),
|
||||||
|
(102, Boolean(enabled)) => SmartCharging(enabled),
|
||||||
|
(103, Boolean(locked)) => CableLocked(locked),
|
||||||
|
(104, Double(amps)) => CableRating(amps),
|
||||||
|
(107, String(tok_rev)) => UserId(tok_rev.chars().rev().collect()),
|
||||||
|
(109, Integer(mode)) => ChargerOpMode(op_mode_from_int(mode)),
|
||||||
|
(110, Integer(mode)) => ActiveOutputPhase(deserialize_i64(mode).unwrap_or(OutputPhase::Unknown)),
|
||||||
|
(120, Double(power)) => TotalPower(power),
|
||||||
|
(122, Double(energy)) => EnergyPerHour(energy),
|
||||||
|
(124, Double(energy)) => LifetimeEnergy(energy),
|
||||||
|
(125, Integer(count)) => LifetimeRelaySwitches(count),
|
||||||
|
(126, Integer(hours)) => LifetimeHours(hours),
|
||||||
|
(150, Integer(degrees)) => MaximumTemperature(degrees),
|
||||||
|
(182, Double(current)) => IntCurrent { pin: T2, current },
|
||||||
|
(183, Double(current)) => IntCurrent { pin: T3, current },
|
||||||
|
(184, Double(current)) => IntCurrent { pin: T4, current },
|
||||||
|
(185, Double(current)) => IntCurrent { pin: T5, current },
|
||||||
|
(190, Double(voltage)) => IntVoltage { pins: (T1, T2), voltage },
|
||||||
|
(191, Double(voltage)) => IntVoltage { pins: (T1, T3), voltage },
|
||||||
|
(192, Double(voltage)) => IntVoltage { pins: (T1, T4), voltage },
|
||||||
|
(193, Double(voltage)) => IntVoltage { pins: (T1, T5), voltage },
|
||||||
|
(194, Double(voltage)) => IntVoltage { pins: (T2, T3), voltage },
|
||||||
|
(195, Double(voltage)) => IntVoltage { pins: (T2, T4), voltage },
|
||||||
|
(196, Double(voltage)) => IntVoltage { pins: (T2, T5), voltage },
|
||||||
|
(197, Double(voltage)) => IntVoltage { pins: (T3, T4), voltage },
|
||||||
|
(198, Double(voltage)) => IntVoltage { pins: (T3, T5), voltage },
|
||||||
|
(199, Double(voltage)) => IntVoltage { pins: (T4, T5), voltage },
|
||||||
|
|
||||||
|
|
||||||
|
(code, value) => Unknown { code, value },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Event {
|
||||||
|
pub charger: String,
|
||||||
|
pub observation: Observation,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Stream {
|
||||||
|
inner: signalr::Stream,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Error)]
|
||||||
|
pub enum ObservationError {
|
||||||
|
#[error("stream: {0}")]
|
||||||
|
Stream(#[from] StreamError),
|
||||||
|
|
||||||
|
#[error("Protocol error")]
|
||||||
|
Protocol(signalr::Message),
|
||||||
|
|
||||||
|
#[error("JSON: {0}")]
|
||||||
|
Deserialize(#[from] serde_json::Error),
|
||||||
|
|
||||||
|
#[error("Parsing: {0}")]
|
||||||
|
Parsing(#[from] ParseError),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
struct ProductUpdate {
|
||||||
|
data_type: DataType,
|
||||||
|
id: u16,
|
||||||
|
mid: String,
|
||||||
|
timestamp: UtcDateTime,
|
||||||
|
value: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Stream {
|
||||||
|
pub fn from_context(ctx: &mut Context) -> Result<Self, NegotiateError> {
|
||||||
|
Ok(Self {
|
||||||
|
inner: signalr::Stream::from_ws(crate::stream::Stream::open(ctx)?),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn recv(&mut self) -> Result<Event, ObservationError> {
|
||||||
|
use signalr::Message::*;
|
||||||
|
let de = |msg| -> Result<Event, ObservationError> { Err(ObservationError::Protocol(msg)) };
|
||||||
|
loop {
|
||||||
|
let msg = self.inner.recv()?;
|
||||||
|
match &msg {
|
||||||
|
Ping => continue,
|
||||||
|
Empty | InvocationResult { .. } => info!("Skipped message: {msg:?}"),
|
||||||
|
Invocation { target, arguments } if target == "ProductUpdate" => {
|
||||||
|
if arguments.len() != 1 {
|
||||||
|
return de(msg);
|
||||||
|
};
|
||||||
|
let evt = ProductUpdate::deserialize(&arguments[0])?;
|
||||||
|
return decode_update(evt);
|
||||||
|
}
|
||||||
|
Invocation { .. } => continue,
|
||||||
|
_other => return de(msg),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn subscribe(&mut self, id: &str) -> Result<(), tungstenite::Error> {
|
||||||
|
self.inner
|
||||||
|
.invoke("SubscribeWithCurrentState", json!([id, true]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn decode_update(update: ProductUpdate) -> Result<Event, ObservationError> {
|
||||||
|
let ProductUpdate {
|
||||||
|
data_type,
|
||||||
|
id,
|
||||||
|
mid,
|
||||||
|
timestamp,
|
||||||
|
value,
|
||||||
|
} = update;
|
||||||
|
let data = ObservationData::from_dynamic(value, data_type)?;
|
||||||
|
let obs = Observation::try_from_data(id, data);
|
||||||
|
let _ = timestamp;
|
||||||
|
Ok(Event {
|
||||||
|
charger: mid,
|
||||||
|
observation: obs,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-1
@@ -1,4 +1,4 @@
|
|||||||
use serde_json::Value;
|
use serde_json::{json, Value};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
use crate::stream::RecvError;
|
use crate::stream::RecvError;
|
||||||
@@ -122,4 +122,15 @@ impl Stream {
|
|||||||
let json = self.buffer.pop().unwrap();
|
let json = self.buffer.pop().unwrap();
|
||||||
Ok(Message::from_json(json)?)
|
Ok(Message::from_json(json)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn invoke(
|
||||||
|
&mut self,
|
||||||
|
target: &str,
|
||||||
|
args: serde_json::Value,
|
||||||
|
) -> Result<(), tungstenite::Error> {
|
||||||
|
self.ws.send(json!( { "arguments": args,
|
||||||
|
"invocationId": "0",
|
||||||
|
"target": target,
|
||||||
|
"type": 1} ))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-29
@@ -1,8 +1,7 @@
|
|||||||
use std::net::TcpStream;
|
|
||||||
|
|
||||||
use super::api::{ApiError, Context};
|
use super::api::{ApiError, Context};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
|
use std::net::TcpStream;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
use tungstenite::{stream::MaybeTlsStream, Message, WebSocket};
|
use tungstenite::{stream::MaybeTlsStream, Message, WebSocket};
|
||||||
|
|
||||||
@@ -46,31 +45,12 @@ pub struct Stream {
|
|||||||
impl Stream {
|
impl Stream {
|
||||||
pub fn open(ctx: &mut Context) -> Result<Stream, NegotiateError> {
|
pub fn open(ctx: &mut Context) -> Result<Stream, NegotiateError> {
|
||||||
let r: NegotiateResponse = ctx.post_raw(STREAM_API_NEGOTIATION_URL, &())?;
|
let r: NegotiateResponse = ctx.post_raw(STREAM_API_NEGOTIATION_URL, &())?;
|
||||||
dbg!(&r);
|
|
||||||
|
|
||||||
let token = ctx.auth_token();
|
let token = ctx.auth_token();
|
||||||
let wss_url = format!(
|
let wss_url = format!(
|
||||||
"{}?id={}&access_token={}",
|
"{}?id={}&access_token={}",
|
||||||
WSS_URL, r.connection_token, token
|
WSS_URL, r.connection_token, token
|
||||||
);
|
);
|
||||||
dbg!(&wss_url);
|
|
||||||
|
|
||||||
/*
|
|
||||||
let req = tungstenite::http::Request::builder()
|
|
||||||
.uri(WSS_URL)
|
|
||||||
.header("Accept", "* / *")
|
|
||||||
.header("Host", "streams.easee.com")
|
|
||||||
.header("Origin", "https://portal.easee.com")
|
|
||||||
.header("Connection", "keep-alive, Upgrade")
|
|
||||||
.header("Upgrade", "websocket")
|
|
||||||
.header("Sec-WebSocket-Version", "13")
|
|
||||||
.header("Sec-WebSocket-Key", tungstenite::handshake::client::generate_key())
|
|
||||||
.header("Sec-Fetch-Dest", "websocket")
|
|
||||||
.header("Sec-Fetch-Mode", "websocket")
|
|
||||||
.header("Sec-Fetch-Site", "same-site")
|
|
||||||
.header("Cookie", format!("easee_skaat=\"{}\"", token))
|
|
||||||
.body(()).unwrap();
|
|
||||||
*/
|
|
||||||
|
|
||||||
let resp = tungstenite::client::connect(&wss_url);
|
let resp = tungstenite::client::connect(&wss_url);
|
||||||
|
|
||||||
@@ -87,7 +67,7 @@ impl Stream {
|
|||||||
Ok(stream)
|
Ok(stream)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn send<T: Serialize>(&mut self, msg: T) -> Result<(), tungstenite::Error> {
|
pub fn send<T: Serialize>(&mut self, msg: T) -> Result<(), tungstenite::Error> {
|
||||||
let mut msg = serde_json::to_string(&msg).unwrap();
|
let mut msg = serde_json::to_string(&msg).unwrap();
|
||||||
msg.push('\x1E');
|
msg.push('\x1E');
|
||||||
self.sock.send(Message::Text(msg))
|
self.sock.send(Message::Text(msg))
|
||||||
@@ -106,11 +86,4 @@ impl Stream {
|
|||||||
|
|
||||||
Ok(msgs)
|
Ok(msgs)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn subscribe(&mut self, id: &str) -> Result<(), tungstenite::Error> {
|
|
||||||
self.send(json!( { "arguments": [id, true],
|
|
||||||
"invocationId": "0",
|
|
||||||
"target": "SubscribeWithCurrentState",
|
|
||||||
"type": 1} ))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user