pass messages to api for active requests
This commit is contained in:
parent
f9dba75462
commit
1a8156a14f
59
Cargo.lock
generated
59
Cargo.lock
generated
|
@ -95,6 +95,19 @@ version = "1.0.77"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c9d19de80eff169429ac1e9f48fffb163916b448a44e8e046186232046d9e1f9"
|
checksum = "c9d19de80eff169429ac1e9f48fffb163916b448a44e8e046186232046d9e1f9"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "async-channel"
|
||||||
|
version = "2.1.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1ca33f4bc4ed1babef42cad36cc1f51fa88be00420404e5b1e80ab1b18f7678c"
|
||||||
|
dependencies = [
|
||||||
|
"concurrent-queue",
|
||||||
|
"event-listener",
|
||||||
|
"event-listener-strategy",
|
||||||
|
"futures-core",
|
||||||
|
"pin-project-lite",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "async-stream"
|
name = "async-stream"
|
||||||
version = "0.3.5"
|
version = "0.3.5"
|
||||||
|
@ -314,6 +327,15 @@ dependencies = [
|
||||||
"yansi 0.5.1",
|
"yansi 0.5.1",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "concurrent-queue"
|
||||||
|
version = "2.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d16048cd947b08fa32c24458a22f5dc5e835264f689f4f5653210c69fd107363"
|
||||||
|
dependencies = [
|
||||||
|
"crossbeam-utils",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "convert_case"
|
name = "convert_case"
|
||||||
version = "0.4.0"
|
version = "0.4.0"
|
||||||
|
@ -384,6 +406,15 @@ dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crossbeam-utils"
|
||||||
|
version = "0.8.18"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c3a430a770ebd84726f584a90ee7f020d28db52c6d02138900f22341f866d39c"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "crypto-common"
|
name = "crypto-common"
|
||||||
version = "0.1.6"
|
version = "0.1.6"
|
||||||
|
@ -490,6 +521,27 @@ dependencies = [
|
||||||
"windows-sys 0.52.0",
|
"windows-sys 0.52.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "event-listener"
|
||||||
|
version = "4.0.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "67b215c49b2b248c855fb73579eb1f4f26c38ffdc12973e20e07b91d78d5646e"
|
||||||
|
dependencies = [
|
||||||
|
"concurrent-queue",
|
||||||
|
"parking",
|
||||||
|
"pin-project-lite",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "event-listener-strategy"
|
||||||
|
version = "0.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "958e4d70b6d5e81971bebec42271ec641e7ff4e170a6fa605f2b8a8b65cb97d3"
|
||||||
|
dependencies = [
|
||||||
|
"event-listener",
|
||||||
|
"pin-project-lite",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "fastrand"
|
name = "fastrand"
|
||||||
version = "2.0.1"
|
version = "2.0.1"
|
||||||
|
@ -1167,6 +1219,12 @@ version = "3.5.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f"
|
checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "parking"
|
||||||
|
version = "2.2.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "bb813b8af86854136c6922af0598d719255ecb2179515e6e7730d468f05c9cae"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "parking_lot"
|
name = "parking_lot"
|
||||||
version = "0.12.1"
|
version = "0.12.1"
|
||||||
|
@ -1994,6 +2052,7 @@ name = "tesla-charge-controller"
|
||||||
version = "0.1.4"
|
version = "0.1.4"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
|
"async-channel",
|
||||||
"chrono",
|
"chrono",
|
||||||
"clap",
|
"clap",
|
||||||
"include_dir",
|
"include_dir",
|
||||||
|
|
|
@ -23,3 +23,4 @@ rocket = { version = "0.5", features = ["json"] }
|
||||||
anyhow = "1.0"
|
anyhow = "1.0"
|
||||||
include_dir = "0.7"
|
include_dir = "0.7"
|
||||||
chrono = "0.4"
|
chrono = "0.4"
|
||||||
|
async-channel = "2.1"
|
||||||
|
|
|
@ -27,6 +27,11 @@ struct AuthInfo {
|
||||||
refresh_token: Option<RefreshToken>,
|
refresh_token: Option<RefreshToken>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
pub enum InterfaceRequest {
|
||||||
|
FlashLights,
|
||||||
|
}
|
||||||
|
|
||||||
const REFRESH_INTERVAL: Duration = Duration::from_secs(12 * 60 * 60);
|
const REFRESH_INTERVAL: Duration = Duration::from_secs(12 * 60 * 60);
|
||||||
|
|
||||||
impl TeslaInterface {
|
impl TeslaInterface {
|
||||||
|
@ -77,6 +82,14 @@ impl TeslaInterface {
|
||||||
self.refresh_state().await;
|
self.refresh_state().await;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn process_request(&mut self, request: InterfaceRequest) {
|
||||||
|
match request {
|
||||||
|
InterfaceRequest::FlashLights => {
|
||||||
|
let _ = self.api.flash_lights(&self.vehicle.vin).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async fn refresh_state(&mut self) {
|
async fn refresh_state(&mut self) {
|
||||||
match get_state(&self.api, self.vehicle.id.clone()).await {
|
match get_state(&self.api, self.vehicle.id.clone()).await {
|
||||||
Ok(new_state) => {
|
Ok(new_state) => {
|
||||||
|
|
12
src/main.rs
12
src/main.rs
|
@ -51,16 +51,24 @@ async fn main() {
|
||||||
let config: Config =
|
let config: Config =
|
||||||
ron::from_str(&std::fs::read_to_string(&config_path).unwrap()).unwrap();
|
ron::from_str(&std::fs::read_to_string(&config_path).unwrap()).unwrap();
|
||||||
|
|
||||||
|
let (api_requests, receiver) = async_channel::unbounded();
|
||||||
|
|
||||||
let server_handle = server::launch_server(server::ServerState {
|
let server_handle = server::launch_server(server::ServerState {
|
||||||
config,
|
config,
|
||||||
state: interface.state.clone(),
|
state: interface.state.clone(),
|
||||||
|
api_requests,
|
||||||
});
|
});
|
||||||
|
|
||||||
tokio::task::spawn(async move {
|
tokio::task::spawn(async move {
|
||||||
let mut interval = tokio::time::interval(std::time::Duration::from_secs(120));
|
let mut interval = tokio::time::interval(std::time::Duration::from_secs(120));
|
||||||
loop {
|
loop {
|
||||||
interval.tick().await;
|
tokio::select! {
|
||||||
interface.refresh().await;
|
_ = interval.tick() => interface.refresh().await,
|
||||||
|
message = receiver.recv() => match message {
|
||||||
|
Ok(message) => interface.process_request(message).await,
|
||||||
|
Err(e) => eprintln!("Error on receive channel: {e:#?}")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
use std::sync::{Arc, RwLock};
|
use std::sync::{Arc, RwLock};
|
||||||
|
|
||||||
|
use async_channel::Sender;
|
||||||
use rocket::{
|
use rocket::{
|
||||||
fairing::{Fairing, Info, Kind},
|
fairing::{Fairing, Info, Kind},
|
||||||
http::Header,
|
http::Header,
|
||||||
|
@ -8,6 +9,7 @@ use rocket::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
api_interface::InterfaceRequest,
|
||||||
config::Config,
|
config::Config,
|
||||||
types::{CarState, ChargeState},
|
types::{CarState, ChargeState},
|
||||||
};
|
};
|
||||||
|
@ -19,6 +21,7 @@ mod static_handler;
|
||||||
pub struct ServerState {
|
pub struct ServerState {
|
||||||
pub config: Config,
|
pub config: Config,
|
||||||
pub state: Arc<RwLock<CarState>>,
|
pub state: Arc<RwLock<CarState>>,
|
||||||
|
pub api_requests: Sender<InterfaceRequest>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn launch_server(state: ServerState) {
|
pub async fn launch_server(state: ServerState) {
|
||||||
|
@ -55,14 +58,7 @@ async fn charge_state(state: &State<ServerState>) -> Option<Json<ChargeState>> {
|
||||||
|
|
||||||
#[post("/flash")]
|
#[post("/flash")]
|
||||||
async fn flash(state: &State<ServerState>) {
|
async fn flash(state: &State<ServerState>) {
|
||||||
state.flash().await;
|
let _ = state.api_requests.send(InterfaceRequest::FlashLights).await;
|
||||||
}
|
|
||||||
|
|
||||||
impl ServerState {
|
|
||||||
async fn flash(&self) {
|
|
||||||
println!("stubbed flash function");
|
|
||||||
// let _ = self.auth.api().flash_lights(&self.vehicle.vin).await;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Cors;
|
pub struct Cors;
|
||||||
|
|
Loading…
Reference in a new issue