-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScheduledTimers.h
131 lines (112 loc) · 3.75 KB
/
ScheduledTimers.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <ezTime.h>
// Structure to hold schedule settings
struct Schedule {
int startHour;
int startMinute;
int stopHour;
int stopMinute;
int days[7];
bool isScheduleDay;
bool isActive;
void (*startFunction)();
void (*stopFunction)();
int blynkVPin; // Virtual pin number for this schedule
};
Timezone local;
// Define your schedule instances
Schedule genSetSchedule = {0, 0, 0, 0, {0}, false, false, nullptr, nullptr, V1};
Schedule transmitterSchedule = {0, 0, 0, 0, {0}, false, false, nullptr, nullptr, V2};
Schedule cameraSchedule = {0, 0, 0, 0, {0}, false, false, nullptr, nullptr, V3};
// Function declarations
void startGenSet();
void stopGenSet();
void startTransmitter();
void stopTransmitter();
void startCamera();
void stopCamera();
void setupSchedules() {
// Initialize schedule functions
genSetSchedule.startFunction = startGenSet;
genSetSchedule.stopFunction = stopGenSet;
transmitterSchedule.startFunction = startTransmitter;
transmitterSchedule.stopFunction = stopTransmitter;
cameraSchedule.startFunction = startCamera;
cameraSchedule.stopFunction = stopCamera;
}
// Generic function to process schedule settings
void processScheduleSettings(Schedule& schedule, TimeInputParam t) {
if (t.hasStartTime()) {
schedule.startHour = t.getStartHour();
schedule.startMinute = t.getStartMinute();
Terminal.println(String("Start: ") + schedule.startHour + ":" + schedule.startMinute);
}
if (t.hasStopTime()) {
schedule.stopHour = t.getStopHour();
schedule.stopMinute = t.getStopMinute();
Terminal.println(String("Stop: ") + schedule.stopHour + ":" + schedule.stopMinute);
}
// Process weekdays
for (int i = 1; i <= 7; i++) {
schedule.days[i-1] = t.isWeekdaySelected(i) ? i : 0;
if (t.isWeekdaySelected(i)) {
Terminal.println(String("Day ") + i + " is selected");
}
}
}
// Blynk write handlers for each schedule
BLYNK_WRITE(V1) { // GenSet Schedule
TimeInputParam t(param);
processScheduleSettings(genSetSchedule, t);
}
BLYNK_WRITE(V2) { // Pump Schedule
TimeInputParam t(param);
processScheduleSettings(transmitterSchedule, t);
}
BLYNK_WRITE(V3) { // Light Schedule
TimeInputParam t(param);
processScheduleSettings(cameraSchedule, t);
}
// Generic function to check schedule
void checkSchedule(Schedule& schedule) {
schedule.isScheduleDay = false;
// Check if today is a scheduled day
for (int i = 0; i < 7; i++) {
if (schedule.days[i] == local.weekday()) {
schedule.isScheduleDay = true;
break;
}
}
// Check start time
if (!schedule.isActive && schedule.isScheduleDay &&
local.hour() == schedule.startHour && local.minute() == schedule.startMinute) {
schedule.isActive = true;
if (schedule.startFunction != nullptr) {
schedule.startFunction();
}
}
// Check stop time
if (schedule.isActive && schedule.isScheduleDay &&
local.hour() == schedule.stopHour && local.minute() == schedule.stopMinute) {
schedule.isActive = false;
if (schedule.stopFunction != nullptr) {
schedule.stopFunction();
}
}
}
// Function to check all schedules
void checkAllSchedules() {
checkSchedule(genSetSchedule);
checkSchedule(transmitterSchedule);
checkSchedule(cameraSchedule);
}
// UTC time handler (unchanged)
BLYNK_WRITE(InternalPinUTC) {
String cmd = param[0].asStr();
if (cmd == "time") {
const uint64_t utc_time = param[1].asLongLong();
UTC.setTime(utc_time / 1000, utc_time % 1000);
} else if (cmd == "tz_rule") {
String tz_rule = param[1].asStr();
local.setPosix(tz_rule);
}
}