From 09ae17fa31d72b88759157fe3edd7b2e9e16bb6a Mon Sep 17 00:00:00 2001 From: Ivan Li Date: Mon, 12 Sep 2022 11:49:52 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E7=AD=89=E5=BE=85?= =?UTF-8?q?=E8=BE=93=E5=87=BA=E5=BC=80=E5=90=AF=E5=92=8C=E5=85=B3=E9=97=AD?= =?UTF-8?q?=E7=9A=84=E5=80=92=E8=AE=A1=E6=97=B6=E9=94=99=E8=AF=AF=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/dc_out_controller.rs | 74 +++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 35 deletions(-) diff --git a/src/dc_out_controller.rs b/src/dc_out_controller.rs index 984e5e4..86a0200 100644 --- a/src/dc_out_controller.rs +++ b/src/dc_out_controller.rs @@ -1,7 +1,6 @@ use std::{ - sync::{ - Arc, Mutex, MutexGuard, - }, + sync::{Arc, Mutex, MutexGuard}, + time::Duration, }; use embedded_hal::digital::v2::{OutputPin, PinState}; @@ -12,12 +11,16 @@ 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}, +}; -const WAITING_OFF_SECONDS: u8 = 60; +const WAITING_OFF_DURATION: u64 = 60; +const WAITING_ON_DURATION: u64 = 60; pub static mut DC_OUT_STATE_EVENT_LOOP: Option< EspEventLoop>, @@ -25,11 +28,11 @@ pub static mut DC_OUT_STATE_EVENT_LOOP: Option< #[derive(Debug, Clone, Copy, PartialEq)] pub enum DcOutStatus { - WaitingOn(u8), + WaitingOn(Duration), On, Off, WaitingOff, - TurningOff(u8), + TurningOff(Duration), } #[derive(Debug, Clone, Copy)] @@ -54,11 +57,9 @@ impl DcOutControllerState { DcOutStatus::WaitingOn(_) => { self.status = DcOutStatus::Off; } - DcOutStatus::TurningOff(seconds) => { - let seconds = seconds - 1; - if seconds > 0 { - self.status = DcOutStatus::TurningOff(seconds); - } else { + DcOutStatus::TurningOff(target) => { + let now = Time::new().get_time(); + if now > target { self.status = DcOutStatus::Off; } } @@ -67,21 +68,21 @@ impl DcOutControllerState { } fn turn_off(&mut self) { + let now = Time::new().get_time(); match self.status { DcOutStatus::On => { - self.status = DcOutStatus::TurningOff(WAITING_OFF_SECONDS); + self.status = + DcOutStatus::TurningOff(now + Duration::from_secs(WAITING_OFF_DURATION)); } DcOutStatus::WaitingOff => { - self.status = DcOutStatus::TurningOff(WAITING_OFF_SECONDS); + self.status = + DcOutStatus::TurningOff(now + Duration::from_secs(WAITING_OFF_DURATION)); } DcOutStatus::WaitingOn(_) => { self.status = DcOutStatus::Off; } - DcOutStatus::TurningOff(seconds) => { - let seconds = seconds - 1; - if seconds > 0 { - self.status = DcOutStatus::TurningOff(seconds); - } else { + DcOutStatus::TurningOff(target) => { + if target < now { self.status = DcOutStatus::Off; } } @@ -90,26 +91,22 @@ impl DcOutControllerState { } fn turn_on(&mut self) { + let now = Time::new().get_time(); match self.status { DcOutStatus::WaitingOff => { self.status = DcOutStatus::On; } DcOutStatus::Off => { - self.status = DcOutStatus::WaitingOn(WAITING_OFF_SECONDS); + self.status = + DcOutStatus::WaitingOn(now + Duration::from_secs(WAITING_ON_DURATION)); } - DcOutStatus::WaitingOn(seconds) => { - let seconds = seconds - 1; - if seconds > 0 { - self.status = DcOutStatus::WaitingOn(seconds); - } else { + DcOutStatus::WaitingOn(target) => { + if target <= now { self.status = DcOutStatus::On; } } - DcOutStatus::TurningOff(seconds) => { - let seconds = seconds - 1; - if seconds > 0 { - self.status = DcOutStatus::TurningOff(seconds); - } else { + DcOutStatus::TurningOff(target) => { + if target <= now { self.status = DcOutStatus::On; } } @@ -129,11 +126,18 @@ impl DcOutControllerState { PinState::Low => "Low", PinState::High => "High", }; - let seconds: i16 = match self.status { - DcOutStatus::WaitingOn(seconds) => seconds.into(), - DcOutStatus::TurningOff(seconds) => seconds.into(), - _ => -1, + let now = Time::new().get_time(); + let target = match self.status { + DcOutStatus::WaitingOn(target) => target, + DcOutStatus::TurningOff(target) => target, + _ => Duration::ZERO, }; + let seconds = if now < target { + target - now + } else { + Duration::ZERO + } + .as_secs(); json!({ "status": status, "pin_state": pin_state, "seconds": seconds }).to_string() } }