1786899994
This commit is contained in:
@@ -5,24 +5,25 @@ use tokio::{sync::mpsc::UnboundedSender, time::sleep};
|
|||||||
|
|
||||||
use crate::{error::AppError, messages::EventMessage};
|
use crate::{error::AppError, messages::EventMessage};
|
||||||
|
|
||||||
// #[derive(Copy, Clone, Debug)]
|
#[allow(unused)]
|
||||||
// pub struct BatteryData {
|
#[derive(Copy, Clone, Debug)]
|
||||||
// state_of_health: Ratio,
|
pub struct RawBatteryData {
|
||||||
// state_of_charge: Ratio,
|
pub state_of_health: Ratio,
|
||||||
// energy: Energy,
|
pub state_of_charge: Ratio,
|
||||||
// energy_full: Energy,
|
pub energy: Energy,
|
||||||
// energy_full_design: Energy,
|
pub energy_full: Energy,
|
||||||
// energy_rate: Power,
|
pub energy_full_design: Energy,
|
||||||
// state: State,
|
pub energy_rate: Power,
|
||||||
// voltage: ElectricPotential,
|
pub state: State,
|
||||||
// temperature: Option<ThermodynamicTemperature>,
|
pub voltage: ElectricPotential,
|
||||||
// technology: Technology,
|
pub temperature: Option<ThermodynamicTemperature>,
|
||||||
// cycle_count: Option<u32>,
|
pub technology: Technology,
|
||||||
// time_to_full: Option<Time>,
|
pub cycle_count: Option<u32>,
|
||||||
// time_to_empty: Option<Time>,
|
pub time_to_full: Option<Time>,
|
||||||
// }
|
pub time_to_empty: Option<Time>,
|
||||||
|
}
|
||||||
|
|
||||||
impl From<&Battery> for BatteryData {
|
impl From<&Battery> for RawBatteryData {
|
||||||
fn from(value: &Battery) -> Self {
|
fn from(value: &Battery) -> Self {
|
||||||
Self {
|
Self {
|
||||||
state_of_health: value.state_of_health(),
|
state_of_health: value.state_of_health(),
|
||||||
@@ -56,8 +57,7 @@ pub async fn battery_listener(s2c_tx: UnboundedSender<EventMessage>) -> Result<(
|
|||||||
loop {
|
loop {
|
||||||
s2c_tx.send(EventMessage::Battery((&battery).into())).ok();
|
s2c_tx.send(EventMessage::Battery((&battery).into())).ok();
|
||||||
|
|
||||||
sleep(Duration::from_secs(30)).await;
|
sleep(Duration::from_secs(5)).await;
|
||||||
|
|
||||||
battery.refresh().ok();
|
battery.refresh().ok();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use crate::ui::*;
|
use crate::{battery::RawBatteryData, ui::*};
|
||||||
use hass_rs::HassEntity;
|
use hass_rs::HassEntity;
|
||||||
use slint::ToSharedString;
|
use slint::ToSharedString;
|
||||||
|
|
||||||
@@ -172,6 +172,16 @@ impl From<&crate::home_assistant::CalendarEvent> for CalendarEventData {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<RawBatteryData> for BatteryData {
|
||||||
|
fn from(value: RawBatteryData) -> Self {
|
||||||
|
let state = value.state.to_shared_string();
|
||||||
|
let percent: f32 = (value.state_of_charge * 100.).into();
|
||||||
|
let percent = percent.round();
|
||||||
|
|
||||||
|
Self { state, percent }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn pretty_label(value: &str) -> String {
|
pub fn pretty_label(value: &str) -> String {
|
||||||
let value = match value {
|
let value = match value {
|
||||||
"partlycloudy" => "partly cloudy",
|
"partlycloudy" => "partly cloudy",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use hass_rs::HassEntity;
|
use hass_rs::HassEntity;
|
||||||
|
|
||||||
use crate::{battery::BatteryData, home_assistant::CalendarEvent};
|
use crate::{battery::RawBatteryData, home_assistant::CalendarEvent};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub enum CommandMessage {
|
pub enum CommandMessage {
|
||||||
@@ -17,5 +17,5 @@ pub enum EventMessage {
|
|||||||
EntityUpdated(HassEntity),
|
EntityUpdated(HassEntity),
|
||||||
EntityRemoved(HassEntity),
|
EntityRemoved(HassEntity),
|
||||||
CalendarUpdated(Vec<CalendarEvent>),
|
CalendarUpdated(Vec<CalendarEvent>),
|
||||||
Battery(BatteryData),
|
Battery(RawBatteryData),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,12 @@
|
|||||||
slint::include_modules!();
|
slint::include_modules!();
|
||||||
|
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
battery::BatteryData,
|
battery::RawBatteryData,
|
||||||
ha_ext::*,
|
ha_ext::*,
|
||||||
home_assistant::CalendarEvent,
|
home_assistant::CalendarEvent,
|
||||||
messages::{CommandMessage, EventMessage},
|
messages::{CommandMessage, EventMessage},
|
||||||
};
|
};
|
||||||
|
|
||||||
use battery::Battery;
|
|
||||||
use hass_rs::HassEntity;
|
use hass_rs::HassEntity;
|
||||||
use paste::paste;
|
use paste::paste;
|
||||||
use slint::{Model, ModelRc, VecModel, Weak, language::ColorScheme};
|
use slint::{Model, ModelRc, VecModel, Weak, language::ColorScheme};
|
||||||
@@ -125,9 +122,12 @@ pub fn apply_calendars(app: &Weak<AppWindow>, entities: Vec<CalendarEvent>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn apply_battery(app: &Weak<AppWindow>, batt: BatteryData) {
|
pub fn apply_battery(app: &Weak<AppWindow>, batt: RawBatteryData) {
|
||||||
// ..
|
if let Err(err) = app.upgrade_in_event_loop(move |app| {
|
||||||
dbg!(batt);
|
app.set_battery(batt.into());
|
||||||
|
}) {
|
||||||
|
eprintln!("Failed to upgrade in event loop.\n\t{:?}", err);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn apply_state(app: &Weak<AppWindow>, entities: Vec<HassEntity>) {
|
pub fn apply_state(app: &Weak<AppWindow>, entities: Vec<HassEntity>) {
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ export struct DateTimeData {
|
|||||||
|
|
||||||
export struct BatteryData {
|
export struct BatteryData {
|
||||||
state: string,
|
state: string,
|
||||||
percent: string,
|
percent: float,
|
||||||
}
|
}
|
||||||
|
|
||||||
component Clock {
|
component Clock {
|
||||||
|
|||||||
Reference in New Issue
Block a user