1786891974

This commit is contained in:
2026-08-16 16:52:54 +02:00
parent f51d4b33cb
commit bbbc8865ee
5 changed files with 41 additions and 13 deletions

20
dashboard/src/battery.rs Normal file
View File

@@ -0,0 +1,20 @@
use slint::Weak;
use tokio::sync::mpsc::UnboundedSender;
use crate::{error::AppError, messages::EventMessage, ui::AppWindow};
pub async fn battery_listener(
app_weak: Weak<AppWindow>,
mut s2c_tx: UnboundedSender<EventMessage>,
) -> Result<(), AppError> {
let manager = ::battery::Manager::new()?;
let battery = manager
.batteries()?
.next()
.ok_or_else(|| AppError::NoBattery)??;
dbg!(battery);
Ok(())
}

View File

@@ -6,12 +6,15 @@ pub enum AppError {
#[error("HASS_TOKEN is not set")]
MissingToken,
#[error("invalid HASS_URL")]
#[error("Invalid HASS_URL")]
InvalidUrl,
#[error("Unsupported HASS_URL scheme: {0}")]
InvalidScheme(String),
#[error("No battery found")]
NoBattery,
#[error(transparent)]
SlintPlatform(#[from] slint::PlatformError),

View File

@@ -1,3 +1,4 @@
mod battery;
mod error;
mod ha_ext;
mod home_assistant;
@@ -19,18 +20,6 @@ use crate::{
async fn main() -> Result<(), AppError> {
dotenvy::dotenv().ok();
let manager = battery::Manager::new()?;
for (idx, maybe_battery) in manager.batteries()?.enumerate() {
let battery = maybe_battery?;
println!("Battery #{}:", idx);
println!("Vendor: {:?}", battery.vendor());
println!("Model: {:?}", battery.model());
println!("State: {:?}", battery.state());
println!("Time to full charge: {:?}", battery.time_to_full());
println!();
}
#[cfg(not(feature = "dev"))]
slint::platform::set_platform(Box::new(trekstor::Trekstor::new())).expect("set platform");
@@ -43,6 +32,11 @@ async fn main() -> Result<(), AppError> {
ui::bind_buttons(&app, c2s_tx);
tokio::spawn({
let s2c_tx = s2c_tx.clone();
battery::battery_listener(app.as_weak(), s2c_tx)
});
// listen for data
tokio::spawn(ui::update_listener(app.as_weak(), s2c_rx));

View File

@@ -1,3 +1,4 @@
use battery::units::Ratio;
use hass_rs::HassEntity;
use crate::home_assistant::CalendarEvent;
@@ -17,4 +18,5 @@ pub enum EventMessage {
EntityUpdated(HassEntity),
EntityRemoved(HassEntity),
CalendarUpdated(Vec<CalendarEvent>),
BatteryPercentage(Ratio),
}

View File

@@ -6,6 +6,7 @@ use crate::{
messages::{CommandMessage, EventMessage},
};
use battery::units::Ratio;
use hass_rs::HassEntity;
use paste::paste;
use slint::{Model, ModelRc, VecModel, Weak, language::ColorScheme};
@@ -121,6 +122,11 @@ pub fn apply_calendars(app: &Weak<AppWindow>, entities: Vec<CalendarEvent>) {
}
}
pub fn apply_battery(app: &Weak<AppWindow>, batt: Ratio) {
// ..
dbg!(batt);
}
pub fn apply_state(app: &Weak<AppWindow>, entities: Vec<HassEntity>) {
if let Err(err) = app.upgrade_in_event_loop(move |app| {
apply_theme(&app, &entities);
@@ -189,6 +195,9 @@ pub async fn update_listener(
EventMessage::CalendarUpdated(events) => {
apply_calendars(&app_weak, events);
}
EventMessage::BatteryPercentage(batt) => {
apply_battery(&app_weak, batt);
}
}
}
}