-
Notifications
You must be signed in to change notification settings - Fork 20
/
irrigation.h
75 lines (62 loc) · 2.34 KB
/
irrigation.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "esphome.h"
// Declare functions before calling them.
bool scheduled_runtime(std::string);
std::string update_next_runtime(std::string);
bool scheduled_runtime(std::string time) {
// Retrieve the current time.
auto time_now = id(homeassistant_time).now();
int time_hour = time_now.hour;
int time_minute = time_now.minute;
// Split the hour and minutes.
int next_hour = atoi(time.substr(0,2).c_str());
int next_minute = atoi(time.substr(3,2).c_str());
//ESP_LOGD("scheduled_runtime()", "now: %i:%i", next_hour, next_minute);
return (time_hour == next_hour && time_minute == next_minute);
}
std::string update_next_runtime(std::string time_list) {
// Initialize variables.
std::vector<std::string> times;
std::vector<std::string> next_time;
char * token;
// Split the list of run times into an array.
token = strtok(&time_list[0], ",");
while (token != NULL) {
times.push_back(token);
token = strtok(NULL, ",");
}
// Stop now if the list does not contain more than one time.
if (times.size() <= 1) {
return time_list;
}
// Retrieve the current time.
auto time_now = id(homeassistant_time).now();
int time_hour = time_now.hour;
int time_minute = time_now.minute;
// Initialize variables.
int next_hour = 0;
int next_minute = 0;
int index = 0;
int loop_count = 0;
int time_count = times.size()-1;
// Compare the list of times with the current time, and return the next in the list.
//ESP_LOGD("update_next_runtime", "now: %i:%i", hour, minute);
for (std::string time : times) {
// Retrieve the next scheduled time from the list.
next_hour = atoi(time.substr(0,2).c_str());
next_minute = atoi(time.substr(3,2).c_str());
//ESP_LOGD("update_next_runtime", "next_hour: %s", time.c_str());
if (time_hour < next_hour || (time_hour == next_hour && time_minute < next_minute)) {
// Return this time if the next hour is greater than the current hour.
return times[loop_count].c_str();
break;
// When we reach the end of our schedule for the day, return the first time of tomorrow.
} else if (time_count == loop_count) {
return times[0].c_str();
break;
}
// Increment the loop counter and array index.
loop_count += 1;
index += 2;
}
return "unknown";
}