1786893454
This commit is contained in:
@@ -1,9 +1,47 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use battery::{Battery, State, Technology, units::*};
|
||||
use tokio::{sync::mpsc::UnboundedSender, time::sleep};
|
||||
|
||||
use crate::{error::AppError, messages::EventMessage};
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub struct BatteryData {
|
||||
state_of_health: Ratio,
|
||||
state_of_charge: Ratio,
|
||||
energy: Energy,
|
||||
energy_full: Energy,
|
||||
energy_full_design: Energy,
|
||||
energy_rate: Power,
|
||||
state: State,
|
||||
voltage: ElectricPotential,
|
||||
temperature: Option<ThermodynamicTemperature>,
|
||||
technology: Technology,
|
||||
cycle_count: Option<u32>,
|
||||
time_to_full: Option<Time>,
|
||||
time_to_empty: Option<Time>,
|
||||
}
|
||||
|
||||
impl From<&Battery> for BatteryData {
|
||||
fn from(value: &Battery) -> Self {
|
||||
Self {
|
||||
state_of_health: value.state_of_health(),
|
||||
state_of_charge: value.state_of_charge(),
|
||||
energy: value.energy(),
|
||||
energy_full: value.energy_full(),
|
||||
energy_full_design: value.energy_full_design(),
|
||||
energy_rate: value.energy_rate(),
|
||||
state: value.state(),
|
||||
voltage: value.voltage(),
|
||||
temperature: value.temperature(),
|
||||
technology: value.technology(),
|
||||
cycle_count: value.cycle_count(),
|
||||
time_to_full: value.time_to_full(),
|
||||
time_to_empty: value.time_to_empty(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn battery_listener(s2c_tx: UnboundedSender<EventMessage>) -> Result<(), AppError> {
|
||||
let manager = ::battery::Manager::new()?;
|
||||
|
||||
@@ -16,9 +54,7 @@ pub async fn battery_listener(s2c_tx: UnboundedSender<EventMessage>) -> Result<(
|
||||
let s2c_tx = s2c_tx.clone();
|
||||
async move {
|
||||
loop {
|
||||
s2c_tx
|
||||
.send(EventMessage::BatteryPercentage(battery.state_of_charge()))
|
||||
.ok();
|
||||
s2c_tx.send(EventMessage::Battery((&battery).into())).ok();
|
||||
|
||||
sleep(Duration::from_secs(30)).await;
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
use battery::units::Ratio;
|
||||
use hass_rs::HassEntity;
|
||||
|
||||
use crate::home_assistant::CalendarEvent;
|
||||
use crate::{battery::BatteryData, home_assistant::CalendarEvent};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum CommandMessage {
|
||||
@@ -18,5 +17,5 @@ pub enum EventMessage {
|
||||
EntityUpdated(HassEntity),
|
||||
EntityRemoved(HassEntity),
|
||||
CalendarUpdated(Vec<CalendarEvent>),
|
||||
BatteryPercentage(Ratio),
|
||||
Battery(BatteryData),
|
||||
}
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
slint::include_modules!();
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::{
|
||||
battery::BatteryData,
|
||||
ha_ext::*,
|
||||
home_assistant::CalendarEvent,
|
||||
messages::{CommandMessage, EventMessage},
|
||||
};
|
||||
|
||||
use battery::units::Ratio;
|
||||
use battery::Battery;
|
||||
use hass_rs::HassEntity;
|
||||
use paste::paste;
|
||||
use slint::{Model, ModelRc, VecModel, Weak, language::ColorScheme};
|
||||
@@ -122,7 +125,7 @@ pub fn apply_calendars(app: &Weak<AppWindow>, entities: Vec<CalendarEvent>) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn apply_battery(app: &Weak<AppWindow>, batt: Ratio) {
|
||||
pub fn apply_battery(app: &Weak<AppWindow>, batt: BatteryData) {
|
||||
// ..
|
||||
dbg!(batt);
|
||||
}
|
||||
@@ -195,7 +198,7 @@ pub async fn update_listener(
|
||||
EventMessage::CalendarUpdated(events) => {
|
||||
apply_calendars(&app_weak, events);
|
||||
}
|
||||
EventMessage::BatteryPercentage(batt) => {
|
||||
EventMessage::Battery(batt) => {
|
||||
apply_battery(&app_weak, batt);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user