MistyGro API
C++ API for MistyGro's ESP32 controller based on the Arduino framework
light_scheduler.cpp
Go to the documentation of this file.
1 // Copyright 2023 Myron Rodrigues
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "light_scheduler.h"
16 
18  Relay * light, int start_hour, int start_min, uint64_t duration_sec)
19 : light_(light),
20  start_hour_(start_hour),
21  start_min_(start_min),
22  light_duration_(duration_sec),
23  start_time_(0),
24  prev_wday_(0) // sunday
25 {
26 }
27 
28 void LightScheduler::reset(tm * time)
29 {
30  tm start = *time;
31  start.tm_hour = start_hour_;
32  start.tm_min = start_min_;
33  start.tm_sec = 0;
34  start_time_ = mktime(&start);
35  Serial.printf("Light start time: %ld\n", start_time_);
36 }
37 
39  tm * time, bool is_bright) // needs a daylight cycle between runs
40 {
41  time_t epoch_time = mktime(time);
42 
43  if (
44  time->tm_wday != prev_wday_ ||
45  start_time_ == 0) // day changed or first init
46  {
47  reset(time);
48  prev_wday_ = time->tm_wday;
49  }
50 
51  if (
52  epoch_time > start_time_ && epoch_time <= start_time_ + light_duration_ &&
53  !is_bright) { // when end set and not end
54  light_->set(Switch::ON);
55  Serial.println("Light on");
56  } else {
57  light_->set(Switch::OFF);
58  Serial.println("Light off");
59  }
60 }
RelayAL light(14)
Definition: main.cpp:33
void reset(tm *time)
reset start time to given time
void run(tm *time, bool is_bright)
Run this in a loop and pass the current time and a boolean indicating if its bright.
LightScheduler(Relay *light, int start_hour, int start_min, uint64_t duration_sec)
Construct a new Light Scheduler object.
Abstract relay class.
Definition: relay.h:29
virtual void set(Switch state)=0