feat: 增加充电控制倒计时上报到 MQTT 能力。

This commit is contained in:
Ivan Li 2022-09-12 11:50:26 +08:00
parent 09ae17fa31
commit daf6effe59

View File

@ -1,4 +1,7 @@
use std::sync::{Arc, Mutex, MutexGuard};
use std::{
sync::{Arc, Mutex, MutexGuard},
time::Duration,
};
use embedded_hal::digital::v2::{OutputPin, PinState};
use embedded_svc::event_bus::{EventBus, Postbox};
@ -8,10 +11,13 @@ use esp_idf_svc::eventloop::{
EspSubscription, EspTypedEventDeserializer, EspTypedEventSerializer, EspTypedEventSource, User,
};
use esp_idf_sys::c_types;
use log::{warn};
use log::warn;
use serde_json::json;
use crate::voltage_detection::{VoltageDetectionWorker, VOLTAGE_EVENTLOOP};
use crate::{
time::Time,
voltage_detection::{VoltageDetectionWorker, VOLTAGE_EVENTLOOP},
};
pub static mut CHARGE_STATE_EVENT_LOOP: Option<
EspEventLoop<esp_idf_svc::eventloop::User<Background>>,
@ -27,6 +33,7 @@ pub enum ChargeStatus {
pub struct ChargeControllerState {
pub status: ChargeStatus,
pub pin_state: PinState,
pub charge_deadline_at: Duration,
}
impl ChargeControllerState {
@ -34,6 +41,7 @@ impl ChargeControllerState {
Self {
status: ChargeStatus::Charging,
pin_state: PinState::Low,
charge_deadline_at: Duration::ZERO,
}
}
@ -46,7 +54,13 @@ impl ChargeControllerState {
PinState::Low => "Low",
PinState::High => "High",
};
json!({ "status": status, "pin_state": pin_state}).to_string()
let now = Time::new().get_time();
let charging_count_down = if now > self.charge_deadline_at {
-1i64
} else {
(self.charge_deadline_at - now).as_secs() as i64
};
json!({ "status": status, "pin_state": pin_state, "charging_count_down": charging_count_down}).to_string()
}
}
@ -112,7 +126,14 @@ impl ChargeController {
if obj.battery_voltage < 12600 {
state.status = ChargeStatus::Charging;
} else {
let now = Time::new().get_time();
if state.charge_deadline_at == Duration::ZERO {
state.charge_deadline_at = now + Duration::from_secs(600);
} else if now > state.charge_deadline_at {
state.status = ChargeStatus::Charged;
} else {
state.status = ChargeStatus::Charging;
}
}
}
ChargeStatus::Charged => {