-
Notifications
You must be signed in to change notification settings - Fork 0
/
Clock.cpp
273 lines (238 loc) · 5.76 KB
/
Clock.cpp
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include "Clock.hpp"
#include <iomanip>
#include <ostream>
#include <TimeLib.h>
#include <SoftwareSerial.h>
#include "Hour.hpp"
#include "Minute.hpp"
#include "Interval.hpp"
namespace sleep
{
CompactTime NightWeek{18, 0};
CompactTime DayWeek{4, 45};
Minute const DurationBeforeNight(30);
Minute const DurationAfterDay(60);
CompactTime NightWeekend{18, 0};
CompactTime DayWeekend{5, 15};
// Default values for the configuration are set in Clock::GetConfig
WeekClockConfig Clock::m_weekClockConfig = {};
Clock::Clock(WallClock const & clock, Day const day)
: WallClock(clock)
, m_day(day)
{
}
Clock::Clock(CompactTime const & compactTime, Day const day)
: WallClock(compactTime)
, m_day(day)
{
}
Clock::Clock(StrictHour const & hours, StrictMinute const & minutes, Day const day)
: WallClock(hours, minutes)
, m_day(day)
{
}
Clock::Clock(Minute const & totalMinutes, Day const day)
: WallClock(totalMinutes)
, m_day(day)
{
}
Clock::Clock(unsigned long const epoch)
: WallClock(epoch)
, m_day(EpochToDay(epoch))
{
}
Clock::Clock(Clock const & other)
: WallClock(other)
, m_day(other.m_day)
{
}
Clock & Clock::operator =(Clock const & other)
{
WallClock::operator =(other);
if (this != &other)
{
m_day = other.m_day;
}
return *this;
}
Period Clock::getPeriod() const
{
for (auto const period: Period_All)
{
Interval interval = Interval::Get(period, m_day);
if (interval.contains(*this))
{
return period;
}
}
Serial.print("Could not get period for clock: ");
print();
Serial.println("");
// there is always a matching period
assert(false);
// return WallClock::getPeriod(m_day);
}
Day Clock::day() const
{
return m_day;
}
Clock Clock::operator +(Hour const & hour) const
{
Minute const minute(totalMinutes() + hour.minutes());
Day const day = (minute < StrictMinute::DayDuration)
? m_day
: NextDay(m_day);
return Clock(minute, day);
}
Clock Clock::operator +(Minute const & minute) const
{
Minute const newMinute(totalMinutes() + minute);
Day const day = (newMinute < StrictMinute::DayDuration)
? m_day
: NextDay(m_day);
return Clock(newMinute, day);
}
Clock Clock::operator -(Hour const & hour) const
{
Minute const minute(totalMinutes() - hour.minutes());
Day const day = (minute.get() >= 0) ? m_day : PreviousDay(m_day);
return Clock(minute, day);
}
Clock Clock::operator -(Minute const & minute) const
{
Minute const newMinute(totalMinutes() - minute);
Day const day = (newMinute.get() >= 0) ? m_day : PreviousDay(m_day);
return Clock(newMinute, day);
}
Minute Clock::operator -(Clock const & rhs) const
{
return WallClock::operator -(rhs);
}
void Clock::print() const
{
PrintDay(m_day);
Serial.print(", ");
WallClock::print();
}
Clock Clock::Get(Period const period, Day const day)
{
switch (period)
{
case Period::Day:
{
Clock clock(Clock::AccessConfig().m_week.at(day).m_nightEnd, day);
return clock + Clock::AccessConfig().m_week.at(day).m_delayAfterNight;
}
case Period::BeforeNight:
{
Clock clock(Clock::AccessConfig().m_week.at(day).m_nightStart, day);
return clock - Clock::AccessConfig().m_week.at(day).m_delayBeforeNight;
}
case Period::Night:
{
Clock clockNight(
Clock::AccessConfig().m_week.at(day).m_nightStart,
day);
return clockNight;
}
case Period::AfterNight:
{
Clock clockAfterNight(
Clock::AccessConfig().m_week.at(day).m_nightEnd,
day);
return clockAfterNight;
}
}
}
void Clock::SetNightStart(int const hours, int const minutes)
{
for (auto const & day: Week())
{
Clock::SetNightStart(hours, minutes, day);
}
}
void Clock::SetNightEnd(int const hours, int const minutes)
{
for (auto const & day: Week())
{
Clock::SetNightEnd(hours, minutes, day);
}
}
void Clock::SetDelayBeforeNight(int const minutes)
{
for (auto const & day: Week())
{
Clock::SetDelayBeforeNight(minutes, day);
}
}
void Clock::SetDelayAfterNight(int const minutes)
{
for (auto const & day: Week())
{
Clock::SetDelayAfterNight(minutes, day);
}
}
void Clock::SetNightStart(int const hours, int const minutes, Day const day)
{
Clock::AccessConfig().m_week.at(day).m_nightStart = CompactTime
{ StrictHour(hours), StrictMinute(minutes) };
}
void Clock::SetNightEnd(int const hours, int const minutes, Day const day)
{
Clock::AccessConfig().m_week.at(day).m_nightEnd = CompactTime
{ StrictHour(hours), StrictMinute(minutes) };
}
void Clock::SetDelayBeforeNight(int const minutes, Day const day)
{
Clock::AccessConfig().m_week.at(day).m_delayBeforeNight = Minute(minutes);
}
void Clock::SetDelayAfterNight(int const minutes, Day const day)
{
Clock::AccessConfig().m_week.at(day).m_delayAfterNight = Minute(minutes);
}
WeekClockConfig const & Clock::GetConfig()
{
return Clock::AccessConfig();
}
void Clock::SetConfig(WeekClockConfig const & clockConfig)
{
Clock::m_weekClockConfig = clockConfig;
}
WeekClockConfig & Clock::AccessConfig()
{
if (Clock::m_weekClockConfig.m_week.empty())
{
ClockConfig config
{
NightWeek,
DayWeek,
Minute(DurationBeforeNight),
Minute(DurationAfterDay)
};
ClockConfig weekendConfig
{
NightWeekend,
DayWeekend,
Minute(DurationBeforeNight),
Minute(DurationAfterDay)
};
for (Day const & day: Week())
{
if (IsWeekDay(day))
{
Clock::m_weekClockConfig.m_week.insert(std::make_pair<>(day, config));
}
else
{
Clock::m_weekClockConfig.m_week.insert(std::make_pair<>(day, weekendConfig));
}
}
}
return Clock::m_weekClockConfig;
}
std::ostream & operator <<(std::ostream & stream, Clock const & clock)
{
stream << dynamic_cast< WallClock const & >(clock) << " " << clock.day();
return stream;
}
}