Implement SignalR layer decoding, run cargo fmt

This commit is contained in:
Maxime Augier 2024-08-07 19:50:37 +02:00
parent b4dedb3728
commit d1702d69a9
5 changed files with 278 additions and 76 deletions

View File

@ -1,4 +1,7 @@
use std::{io, time::{Duration, Instant}};
use std::{
io,
time::{Duration, Instant},
};
use serde::{de::DeserializeOwned, Deserialize, Deserializer, Serialize};
use serde_repr::Deserialize_repr;
@ -20,8 +23,7 @@ const REFRESH_TOKEN_DELAY: Duration = Duration::from_secs(600);
pub struct NaiveDateTime(pub chrono::NaiveDateTime);
impl<'de> Deserialize<'de> for NaiveDateTime {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error>
{
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
use serde::de::Error;
let s = <&str as Deserialize>::deserialize(d)?;
let dt = chrono::NaiveDateTime::parse_from_str(s, "%Y-%m-%dT%H:%M:%S%.f")
@ -34,8 +36,7 @@ impl<'de> Deserialize<'de> for NaiveDateTime {
pub struct UtcDateTime(pub chrono::DateTime<chrono::Utc>);
impl<'de> Deserialize<'de> for UtcDateTime {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error>
{
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
use serde::de::Error;
let s = <&str as Deserialize>::deserialize(d)?;
let dt = chrono::DateTime::parse_from_str(s, "%+")
@ -155,7 +156,6 @@ pub struct ChargerState {
pub derated_current: Option<f64>,
pub derating_active: bool,
pub connected_to_cloud: bool,
}
#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd)]
@ -176,14 +176,11 @@ pub struct ChargingSession {
pub currency_id: Option<String>,
pub cost_including_vat: Option<f64>,
pub cost_excluding_vat: Option<f64>,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Address {
}
pub struct Address {}
#[derive(Clone, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd)]
#[serde(rename_all = "camelCase")]
@ -194,7 +191,7 @@ pub struct Site {
pub name: Option<String>,
pub level_of_access: u32,
//pub address: Address,
pub installer_alias: Option<String>
pub installer_alias: Option<String>,
}
#[derive(Clone, Debug, Deserialize)]
@ -204,7 +201,15 @@ pub struct LoginResponse {
pub expires_in: u32,
pub access_claims: Vec<Option<String>>,
pub token_type: Option<String>,
pub refresh_token: String
pub refresh_token: String,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct CommandReply {
command_id: u64,
device: String,
ticks: u64,
}
#[derive(Debug, Error)]
@ -227,7 +232,7 @@ pub enum ApiError {
/// A JSON datetime field could not be parsed
#[error("format error: {0}")]
FormatError(#[from] chrono::ParseError)
FormatError(#[from] chrono::ParseError),
}
impl From<ureq::Error> for ApiError {
@ -250,12 +255,14 @@ impl JsonExplicitError for ureq::Response {
}
impl Context {
/// Build a context from provided acess tokens
pub fn from_tokens(access_token: &str, refresh_token: String, expires_in: u32) -> Self {
Self { auth_header: format!("Bearer {}", access_token),
Self {
auth_header: format!("Bearer {}", access_token),
refresh_token,
token_expiration: Instant::now() + Duration::from_secs(expires_in as u64) - REFRESH_TOKEN_DELAY }
token_expiration: Instant::now() + Duration::from_secs(expires_in as u64)
- REFRESH_TOKEN_DELAY,
}
}
fn from_login_response(resp: LoginResponse) -> Self {
@ -266,12 +273,18 @@ impl Context {
pub fn from_login(user: &str, password: &str) -> Result<Self, ApiError> {
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct Params<'t> { user_name: &'t str, password: &'t str }
struct Params<'t> {
user_name: &'t str,
password: &'t str,
}
info!("Logging into API");
let url: String = format!("{}accounts/login", API_BASE);
let resp: LoginResponse = ureq::post(&url)
.send_json(Params { user_name: user, password } )?
.send_json(Params {
user_name: user,
password,
})?
.into_json_with_error()?;
Ok(Self::from_login_response(resp))
@ -294,10 +307,14 @@ impl Context {
pub fn refresh_token(&mut self) -> Result<(), ApiError> {
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct Params<'t> { refresh_token: &'t str }
struct Params<'t> {
refresh_token: &'t str,
}
info!("Refreshing access token");
let params = Params { refresh_token: &self.refresh_token };
let params = Params {
refresh_token: &self.refresh_token,
};
let url = format!("{}accounts/refresh_token", API_BASE);
let resp: LoginResponse = ureq::post(&url)
.set("Content-type", "application/json")
@ -306,7 +323,6 @@ impl Context {
*self = Self::from_login_response(resp);
Ok(())
}
/// List all sites available to the user
@ -342,18 +358,26 @@ impl Context {
Ok(r) => Ok(Some(r)),
Err(ApiError::Ureq(e)) => match &*e {
ureq::Error::Status(404, _) => Ok(None),
_ => Err(ApiError::Ureq(e))
_ => Err(ApiError::Ureq(e)),
},
Err(other) => Err(other)
Err(other) => Err(other),
}
}
pub(crate) fn post<T: DeserializeOwned, P: Serialize>(&mut self, path: &str, params: &P) -> Result<T, ApiError> {
pub(crate) fn post<T: DeserializeOwned, P: Serialize>(
&mut self,
path: &str,
params: &P,
) -> Result<T, ApiError> {
let url: String = format!("{}{}", API_BASE, path);
self.post_raw(&url, params)
}
pub(crate) fn post_raw<T: DeserializeOwned, P: Serialize>(&mut self, url: &str, params: &P) -> Result<T, ApiError> {
pub(crate) fn post_raw<T: DeserializeOwned, P: Serialize>(
&mut self,
url: &str,
params: &P,
) -> Result<T, ApiError> {
self.check_expired()?;
let req = ureq::post(url)
.set("Accept", "application/json")
@ -368,7 +392,6 @@ impl Context {
resp.into_json_with_error()
}
}
/// Energy meter reading
@ -412,26 +435,27 @@ impl Charger {
ctx.maybe_get(&format!("chargers/{}/sessions/latest", &self.id))
}
fn command(&self, ctx: &mut Context, command: &str) -> Result<(), ApiError> {
fn command(&self, ctx: &mut Context, command: &str) -> Result<CommandReply, ApiError> {
ctx.post(&format!("chargers/{}/commands/{}", self.id, command), &())
}
pub fn start(&self, ctx: &mut Context) -> Result<(), ApiError> {
self.command(ctx, "start_charging")
self.command(ctx, "start_charging")?;
Ok(())
}
pub fn pause(&self, ctx: &mut Context) -> Result<(), ApiError> {
self.command(ctx, "pause_charging")
self.command(ctx, "pause_charging")?;
Ok(())
}
pub fn resume(&self, ctx: &mut Context) -> Result<(), ApiError> {
self.command(ctx, "resume_charging")
self.command(ctx, "resume_charging")?;
Ok(())
}
pub fn stop(&self, ctx: &mut Context) -> Result<(), ApiError> {
self.command(ctx, "stop_charging")
self.command(ctx, "stop_charging")?;
Ok(())
}
}

View File

@ -2,3 +2,8 @@ pub mod api;
#[cfg(feature = "tungstenite")]
pub mod stream;
#[cfg(feature = "tungstenite")]
pub mod signalr;
pub mod observation;

40
src/observation.rs Normal file
View File

@ -0,0 +1,40 @@
use crate::api::ChargerOpMode;
#[repr(u8)]
pub enum PilotMode {
Disconnected = b'A',
Connected = b'B',
Charging = b'C',
NeedsVentilation = b'D',
FaultDetected = b'F',
}
#[repr(u8)]
pub enum PhaseMode {
Ignore = 0,
Phase1 = 1,
Auto = 2,
Phase2 = 3,
}
pub enum ReasonForNoCurrent {}
pub enum Observation {
SelfTestResult(String),
SelfTestDetails(serde_json::Value),
WifiEvent(u64),
ChargerOfflineReason(u64),
CircuitMaxCurrent { phase: u8, amperes: u64 },
SiteID(String),
IsEnabled(bool),
Temperature(u64),
TriplePhase(bool),
DynamicChargerCurrent(f64),
ReasonForNoCurrent(ReasonForNoCurrent),
PilotMode(PilotMode),
SmartCharging(bool),
CableLocked(bool),
CableRating(f64),
UserId(String),
ChargerOpMode(ChargerOpMode),
}

125
src/signalr.rs Normal file
View File

@ -0,0 +1,125 @@
use serde_json::Value;
use thiserror::Error;
use crate::stream::RecvError;
/* This entire module can be rewritten in two lines when
https://github.com/serde-rs/serde/issues/745
is merged */
#[derive(Debug)]
pub enum Message {
Empty,
Invocation {
target: String,
arguments: Vec<Value>,
},
InvocationResult {
id: String,
result: serde_json::Value,
},
Ping,
Other(serde_json::Value),
}
#[derive(Debug, Error)]
pub enum ParseError {
#[error("Expecting object, received {0}")]
ExpectingObject(Value),
#[error("Missing `type` key")]
MissingTypeKey,
#[error("`type` is not a number")]
TypeNotANumber,
#[error("Unknown type {0}")]
UnknownType(u64),
#[error("Missing expected key {0}")]
MissingKey(&'static str),
#[error("Expecting string")]
ExpectingString,
#[error("Expecting array")]
ExpectingArray,
}
impl Message {
pub fn from_json(msg: Value) -> Result<Self, ParseError> {
let Some(obj) = msg.as_object() else {
return Err(ParseError::ExpectingObject(msg));
};
if obj.is_empty() {
return Ok(Message::Empty);
}
let typ = obj
.get("type")
.ok_or(ParseError::MissingTypeKey)?
.as_number()
.and_then(|n| n.as_u64())
.ok_or(ParseError::TypeNotANumber)?;
match typ {
1 => Ok(Message::Invocation {
target: obj
.get("target")
.ok_or(ParseError::MissingKey("target"))?
.as_str()
.ok_or(ParseError::ExpectingString)?
.to_owned(),
arguments: obj
.get("arguments")
.ok_or(ParseError::MissingKey("arguments"))?
.as_array()
.ok_or(ParseError::ExpectingArray)?
.to_owned(),
}),
3 => Ok(Message::InvocationResult {
id: obj
.get("invocationId")
.ok_or(ParseError::MissingKey("invocationId"))?
.as_str()
.ok_or(ParseError::ExpectingString)?
.to_owned(),
result: obj
.get("result")
.ok_or(ParseError::MissingKey("result"))?
.to_owned(),
}),
6 => Ok(Message::Ping),
_ => Ok(Message::Other(msg)),
}
}
}
#[derive(Debug, Error)]
pub enum StreamError {
#[error("Parse error: {0}")]
ParseError(#[from] ParseError),
#[error("Recv error: {0}")]
StreamError(#[from] RecvError),
}
pub struct Stream {
buffer: Vec<serde_json::Value>,
ws: super::stream::Stream,
}
impl Stream {
pub fn from_ws(ws: super::stream::Stream) -> Self {
Self { ws, buffer: vec![] }
}
pub fn recv(&mut self) -> Result<Message, StreamError> {
while self.buffer.is_empty() {
self.buffer = self.ws.recv()?;
self.buffer.reverse();
}
let json = self.buffer.pop().unwrap();
Ok(Message::from_json(json)?)
}
}

View File

@ -1,12 +1,13 @@
use std::net::TcpStream;
use tungstenite::{stream::MaybeTlsStream, WebSocket, Message};
use super::api::{ApiError, Context};
use serde::{Deserialize, Serialize};
use thiserror::Error;
use super::api::{Context, ApiError};
use serde_json::json;
use thiserror::Error;
use tungstenite::{stream::MaybeTlsStream, Message, WebSocket};
const STREAM_API_NEGOTIATION_URL: &str = "https://streams.easee.com/hubs/products/negotiate?negotiateVersion=1";
const STREAM_API_NEGOTIATION_URL: &str =
"https://streams.easee.com/hubs/products/negotiate?negotiateVersion=1";
const WSS_URL: &str = "wss://streams.easee.com/hubs/products";
#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd)]
@ -48,8 +49,10 @@ impl Stream {
dbg!(&r);
let token = ctx.auth_token();
let wss_url = format!("{}?id={}&access_token={}", WSS_URL,
r.connection_token, token);
let wss_url = format!(
"{}?id={}&access_token={}",
WSS_URL, r.connection_token, token
);
dbg!(&wss_url);
/*
@ -72,7 +75,10 @@ impl Stream {
let resp = tungstenite::client::connect(&wss_url);
if let Err(tungstenite::Error::Http(he)) = &resp {
eprintln!("Response: {}", std:: str::from_utf8(&he.body().as_ref().unwrap()).unwrap());
eprintln!(
"Response: {}",
std::str::from_utf8(&he.body().as_ref().unwrap()).unwrap()
);
}
let mut stream = Stream { sock: resp?.0 };
@ -89,9 +95,12 @@ impl Stream {
pub fn recv(&mut self) -> Result<Vec<serde_json::Value>, RecvError> {
let msg = self.sock.read()?;
let Message::Text(txt) = msg else { return Err(RecvError::BadMessageType) };
let Message::Text(txt) = msg else {
return Err(RecvError::BadMessageType);
};
let msgs = txt.split_terminator('\x1E')
let msgs = txt
.split_terminator('\x1E')
.filter_map(|s| serde_json::from_str(s).ok())
.collect();
@ -104,5 +113,4 @@ impl Stream {
"target": "SubscribeWithCurrentState",
"type": 1} ))
}
}