Skip to content

Commit

Permalink
calculat dawn and dusk times
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffdudds committed Nov 26, 2023
1 parent 1f6636c commit 8ff8d2e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 13 deletions.
6 changes: 4 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
"program": "${workspaceFolder}/target/xtensa-esp32-espidf/debug/chicken_coop_rs",
"cwd": "${workspaceFolder}",
"MIMode": "gdb",
"miDebuggerPath": "${userHome}/.espressif/tools/xtensa-esp32-elf/esp-2021r2-patch3-8.4.0/xtensa-esp32-elf/bin/xtensa-esp32-elf-gdb",
// "miDebuggerPath": "${userHome}/.espressif/tools/xtensa-esp32-elf/esp-2021r2-patch3-8.4.0/xtensa-esp32-elf/bin/xtensa-esp32-elf-gdb",
// "miDebuggerPath": "/home/esp/.rustup/toolchains/esp/xtensa-esp-elf/esp-13.2.0_20230928/xtensa-esp-elf/share/licenses/binutils/gdb",
"miDebuggerPath": "/home/esp/.rustup/toolchains/esp/bin/rust-gdb",
"miDebuggerServerAddress": "localhost:3333"
}
]
}
}
8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@ std = ["alloc", "esp-idf-svc/binstart", "esp-idf-svc/std"]
alloc = ["esp-idf-svc/alloc"]
nightly = ["esp-idf-svc/nightly"]
experimental = ["esp-idf-svc/experimental"]
embassy = ["esp-idf-svc/embassy-sync", "esp-idf-svc/critical-section", "esp-idf-svc/embassy-time-driver"]
embassy = [
"esp-idf-svc/embassy-sync",
"esp-idf-svc/critical-section",
"esp-idf-svc/embassy-time-driver",
]

[dependencies]
log = { version = "0.4", default-features = false }
esp-idf-svc = { version = "0.47.1", default-features = false }
sunrise-next = "1.2"
chrono = "0.4.31"

[build-dependencies]
embuild = "0.31.3"
60 changes: 51 additions & 9 deletions src/chicken.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,66 @@
use std::time::SystemTime;
use chrono::prelude::*;
use chrono::LocalResult;
// use esp_idf_svc::sys::__time_t;
// use std::time::SystemTime;
use sunrise::{DawnType, SolarDay, SolarEvent};

pub struct Chicken {
wake_up_time: SystemTime,
bed_time: SystemTime,
wake_time: Option<u32>,
bed_time: Option<u32>,
}
impl Chicken {
pub fn new() -> Self {
Self {
wake_up_time: Chicken::get_bed_time(),
bed_time: Chicken::get_bed_time(),
wake_time: Option::None,
bed_time: Option::None,
}
}

pub fn is_awake(&self) -> bool {
let now = SystemTime::now();
now > self.wake_up_time && now < self.bed_time
let secs_from_midnight = Local::now().num_seconds_from_midnight();
secs_from_midnight > self.wake_time.unwrap() && secs_from_midnight < self.bed_time.unwrap()
}

pub fn get_bed_time() -> SystemTime {
SystemTime::now()
pub fn get_time() -> u32 {
Local::now().num_seconds_from_midnight()
}

pub fn get_bed_time() -> Option<u32> {
// need to offset so its relative to the start of a day
let date_time: DateTime<Local> = Local::now();
let dusk = SolarDay::new(
49.1659,
-123.9401,
date_time.year(),
date_time.month(),
date_time.day(),
)
.event_time(SolarEvent::Dusk(DawnType::Civil));

// convert unix timestamp into secs since midnight
match Local.timestamp_opt(dusk, 0) {
LocalResult::Single(dusk_time) => Some(dusk_time.num_seconds_from_midnight()),
_ => None,
}
}

pub fn get_wake_time() -> Option<u32> {
// need to offset so its relative to the start of a day
let date_time: DateTime<Local> = Local::now();
let dawn = SolarDay::new(
49.1659,
-123.9401,
date_time.year(),
date_time.month(),
date_time.day(),
)
.event_time(SolarEvent::Dawn(DawnType::Civil));

// convert unix timestamp into secs since midnight
match Local.timestamp_opt(dawn, 0) {
LocalResult::Single(dawn_time) => Some(dawn_time.num_seconds_from_midnight()),
_ => None,
}
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ fn main() {
// create a chicken object (updates at midnight)
// future - add app overrides
// create a gate controller (init based on schedule, eg isAwake(offset))
// drive gate actuator
// create gate driver

let chicken = Chicken::new();
if chicken.is_awake() {
log::info!("the chickens are awake!");
}
log::info!("wake time: {}", Chicken::get_wake_time().unwrap());
log::info!("bed time: {}", Chicken::get_bed_time().unwrap());
log::info!("now: {}", Chicken::get_time());
}

0 comments on commit 8ff8d2e

Please sign in to comment.