Skip to content

Commit

Permalink
Add a TaskCommand to OneWireTask struct
Browse files Browse the repository at this point in the history
By this it will be possible to use a relay-specific
turn-on/prolong duration from a database.

Durations are considered in the following order:
- duration passed in OneWireTask
- switch_hold_secs (if set)
- pir_hold_secs
  • Loading branch information
manio committed Jun 16, 2020
1 parent de5f0f1 commit 53c5587
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
38 changes: 33 additions & 5 deletions src/onewire.rs
Expand Up @@ -44,8 +44,14 @@ pub const YEELIGHT_DURATION_MS: u32 = 500; //duration of above effect
pub const DAYLIGHT_SUN_DEGREE: f64 = 3.0; //sun elevation for day/night switching
pub const SUN_POS_CHECK_INTERVAL_SECS: f32 = 60.0; //secs between calculating sun position

#[derive(Clone, Debug)]
pub enum TaskCommand {
TurnOnProlong,
TurnOff,
}
#[derive(Clone)]
pub struct OneWireTask {
pub command: TaskCommand,
pub id_relay: i32,
pub duration: Option<Duration>,
}
Expand Down Expand Up @@ -1237,8 +1243,8 @@ impl OneWire {
.collect();
for t in &relay_tasks {
debug!(
"Processing OneWireTask: id_relay={}, duration={:?}",
t.id_relay, t.duration
"Processing OneWireTask: command={:?} id_relay={}, duration={:?}",
t.command, t.id_relay, t.duration
);

//flip-flop protection for too fast state changes
Expand All @@ -1254,9 +1260,31 @@ impl OneWire {
_ => {}
}

match t.duration {
Some(d) => {
match t.command {
TaskCommand::TurnOnProlong => {
//turn on or prolong

let d = match t.duration {
Some(d) => {
//if we have a duration passed, use it
d
}
None => {
//otherwise take a switch_hold_secs or pir_hold_secs
if relay.switch_hold_secs
!= DEFAULT_SWITCH_HOLD_SECS
{
Duration::from_secs_f32(
relay.switch_hold_secs,
)
} else {
Duration::from_secs_f32(
relay.pir_hold_secs,
)
}
}
};

//checking if bit is set (relay is off)
if !relay.override_mode
&& new_state & (1 << i as u8) != 0
Expand Down Expand Up @@ -1321,7 +1349,7 @@ impl OneWire {
}
}
}
None => {
TaskCommand::TurnOff => {
let on: bool = new_state & (1 << i as u8) == 0;
if on {
if flipflop_block {
Expand Down
5 changes: 3 additions & 2 deletions src/onewire_env.rs
@@ -1,6 +1,6 @@
use crate::onewire::{
get_w1_device_name, OneWireTask, FAMILY_CODE_DS18B20, FAMILY_CODE_DS18S20, FAMILY_CODE_DS2438,
W1_ROOT_PATH,
get_w1_device_name, OneWireTask, TaskCommand, FAMILY_CODE_DS18B20, FAMILY_CODE_DS18S20,
FAMILY_CODE_DS2438, W1_ROOT_PATH,
};
use std::collections::HashMap;
use std::fs::File;
Expand Down Expand Up @@ -334,6 +334,7 @@ impl OneWireEnv {
&sensor.associated_relays
{
let task = OneWireTask {
command: TaskCommand::TurnOnProlong,
id_relay: *id_relay,
duration: Some(Duration::from_secs_f32(HUMID_FAN_PROLONG_SECS)),
};
Expand Down
4 changes: 3 additions & 1 deletion src/webserver.rs
Expand Up @@ -3,7 +3,7 @@ use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;

use crate::onewire::OneWireTask;
use crate::onewire::{OneWireTask, TaskCommand};
use rocket::*;
use std::sync::mpsc::Sender;

Expand All @@ -20,6 +20,7 @@ pub fn hello() -> &'static str {
#[get("/fan-on")]
pub fn fan_on(ow_transmitter: State<Arc<Mutex<Sender<OneWireTask>>>>) -> String {
let task = OneWireTask {
command: TaskCommand::TurnOnProlong,
id_relay: 14,
duration: Some(Duration::from_secs(60 * 5)),
};
Expand All @@ -32,6 +33,7 @@ pub fn fan_on(ow_transmitter: State<Arc<Mutex<Sender<OneWireTask>>>>) -> String
#[get("/fan-off")]
pub fn fan_off(ow_transmitter: State<Arc<Mutex<Sender<OneWireTask>>>>) -> String {
let task = OneWireTask {
command: TaskCommand::TurnOff,
id_relay: 14,
duration: None,
};
Expand Down

0 comments on commit 53c5587

Please sign in to comment.