Skip to content

Commit

Permalink
Merge pull request #16 from torfmaster/feature/fast-switching
Browse files Browse the repository at this point in the history
Add feature to switch in seconds rather than minutes
  • Loading branch information
torfmaster authored Aug 29, 2024
2 parents 24281fb + e993687 commit 7542878
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions server/config-dummy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ actors:
url: http://192.168.178.94
disable_threshold: 100
enable_threshold: -100
duration_minutes: 60
duration_seconds: 360
actor_mode: !Discharge
- actor: !HS100
address: 192.168.178.12:9999
disable_threshold: 100
enable_threshold: -100
duration_minutes: 60
duration_seconds: 360
actor_mode:
!Charge # Start loop via socat -d -d pty,raw,echo=0 pty,raw,echo=0

Expand Down
4 changes: 2 additions & 2 deletions server/config-sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ actors:
url: http://192.168.178.94
disable_threshold: 100
enable_threshold: -100
duration_minutes: 60
duration_seconds: 360
actor_mode: !Discharge
- actor: !HS100
address: 192.168.178.12:9999
disable_threshold: 100
enable_threshold: -100
duration_minutes: 60
duration_seconds: 360
actor_mode: !Charge
ttys_location: /dev/ttyS0
gpio_location: /dev/gpiochip0
Expand Down
12 changes: 6 additions & 6 deletions server/src/actors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mod tasmota;
struct ActorState {
disable_threshold: isize,
enable_threshold: isize,
duration_minutes: usize,
duration_seconds: usize,
last_set: Option<DateTime<Utc>>,
switch: Box<dyn PowerSwitch + Send>,
on: bool,
Expand Down Expand Up @@ -56,7 +56,7 @@ impl ActorState {
Self {
disable_threshold: config.disable_threshold,
enable_threshold: config.enable_threshold,
duration_minutes: config.duration_minutes,
duration_seconds: config.duration_seconds,
last_set: None,
last_updated: None,
switch,
Expand Down Expand Up @@ -87,7 +87,7 @@ pub(crate) async fn control_actors(rx: &mut Receiver<i32>, config: &Configuratio
let ActorState {
disable_threshold,
enable_threshold,
duration_minutes,
duration_seconds,
last_set,
switch,
actor_mode,
Expand All @@ -107,7 +107,7 @@ pub(crate) async fn control_actors(rx: &mut Receiver<i32>, config: &Configuratio
if should_be_on != *on {
if let Some(last_set_inner) = last_set {
let diff = now - *last_set_inner;
if diff > Duration::minutes(*duration_minutes as i64) {
if diff > Duration::seconds(*duration_seconds as i64) {
*on = should_be_on;
*last_set = Some(now.clone());
if *on {
Expand All @@ -129,7 +129,7 @@ pub(crate) async fn control_actors(rx: &mut Receiver<i32>, config: &Configuratio

if let Some(last_updated_inner) = last_updated {
let diff = now - *last_updated_inner;
if diff > Duration::minutes(*duration_minutes as i64) {
if diff > Duration::seconds(*duration_seconds as i64) {
*last_updated = Some(now);
let _ = switch.set_power(received as isize).await;
} else {
Expand Down Expand Up @@ -310,7 +310,7 @@ mod test {
ActorState {
disable_threshold: 100,
enable_threshold: -100,
duration_minutes: 60,
duration_seconds: 360,
last_set: None,
switch: Box::new(DummyActor),
on,
Expand Down
2 changes: 1 addition & 1 deletion server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct ActorConfiguration {
actor: ActorType,
disable_threshold: isize,
enable_threshold: isize,
duration_minutes: usize,
duration_seconds: usize,
actor_mode: ActorMode,
}

Expand Down

0 comments on commit 7542878

Please sign in to comment.