24
24
25
25
#include < Arduino.h>
26
26
#include " ../Property.h"
27
+ #include " ../../AIoTC_Const.h"
27
28
#include " utility/time/TimeService.h"
28
29
#include < time.h>
29
30
31
+ /* *****************************************************************************
32
+ * DEFINE
33
+ ******************************************************************************/
34
+ #define SCHEDULE_UNIT_MASK 0xC0000000
35
+ #define SCHEDULE_UNIT_SHIFT 30
36
+
37
+ #define SCHEDULE_TYPE_MASK 0x3C000000
38
+ #define SCHEDULE_TYPE_SHIFT 26
39
+
40
+ #define SCHEDULE_MONTH_MASK 0x0000FF00
41
+ #define SCHEDULE_MONTH_SHIFT 8
42
+
43
+ #define SCHEDULE_REP_MASK 0x03FFFFFF
44
+ #define SCHEDULE_WEEK_MASK 0x000000FF
45
+ #define SCHEDULE_DAY_MASK 0x000000FF
46
+
47
+ #define SCHEDULE_ONE_SHOT 0xFFFFFFFF
48
+
49
+ /* *****************************************************************************
50
+ ENUM
51
+ ******************************************************************************/
52
+ enum class ScheduleUnit : int {
53
+ Seconds = 0 ,
54
+ Minutes = 1 ,
55
+ Hours = 2 ,
56
+ Days = 3
57
+ };
58
+
59
+ enum class ScheduleType : int {
60
+ OneShot = 0 ,
61
+ FixedDelta = 1 ,
62
+ Weekly = 2 ,
63
+ Monthly = 3 ,
64
+ Yearly = 4
65
+ };
66
+
30
67
/* *****************************************************************************
31
68
CLASS DECLARATION
32
69
******************************************************************************/
@@ -69,116 +106,107 @@ class Schedule : public TimeService {
69
106
return !(operator ==(aSchedule));
70
107
}
71
108
private:
109
+ ScheduleUnit getScheduleUnit (unsigned int msk) {
110
+ return static_cast <ScheduleUnit>((msk & SCHEDULE_UNIT_MASK) >> SCHEDULE_UNIT_SHIFT);
111
+ }
112
+
113
+ ScheduleType getScheduleType (unsigned int msk) {
114
+ return static_cast <ScheduleType>((msk & SCHEDULE_TYPE_MASK) >> SCHEDULE_TYPE_SHIFT);
115
+ }
116
+
117
+ unsigned int getScheduleRepetition (unsigned int msk) {
118
+ return (msk & SCHEDULE_REP_MASK);
119
+ }
120
+
121
+ unsigned int getScheduleWeekMask (unsigned int msk) {
122
+ return (msk & SCHEDULE_WEEK_MASK);
123
+ }
124
+
125
+ unsigned int getScheduleDay (unsigned int msk) {
126
+ return (msk & SCHEDULE_DAY_MASK);
127
+ }
128
+
129
+ unsigned int getScheduleMonth (unsigned int msk) {
130
+ return ((msk & SCHEDULE_MONTH_MASK) >> SCHEDULE_MONTH_SHIFT);
131
+ }
132
+
72
133
bool isScheduleOneShot (unsigned int msk) {
73
- if ((msk & 0x3C000000 ) == 0x00000000 ) {
74
- return true ;
75
- } else {
76
- return false ;
77
- }
134
+ return (getScheduleType (msk) == ScheduleType::OneShot) ? true : false ;
78
135
}
79
136
80
137
bool isScheduleFixed (unsigned int msk) {
81
- if ((msk & 0x3C000000 ) == 0x04000000 ) {
82
- return true ;
83
- } else {
84
- return false ;
85
- }
138
+ return (getScheduleType (msk) == ScheduleType::FixedDelta) ? true : false ;
86
139
}
87
140
88
141
bool isScheduleWeekly (unsigned int msk) {
89
- if ((msk & 0x3C000000 ) == 0x08000000 ) {
90
- return true ;
91
- } else {
92
- return false ;
93
- }
142
+ return (getScheduleType (msk) == ScheduleType::Weekly) ? true : false ;
94
143
}
95
144
96
145
bool isScheduleMonthly (unsigned int msk) {
97
- if ((msk & 0x3C000000 ) == 0x0C000000 ) {
98
- return true ;
99
- } else {
100
- return false ;
101
- }
146
+ return (getScheduleType (msk) == ScheduleType::Monthly) ? true : false ;
102
147
}
103
148
104
149
bool isScheduleYearly (unsigned int msk) {
105
- if ((msk & 0x3C000000 ) == 0x10000000 ) {
106
- return true ;
107
- } else {
108
- return false ;
109
- }
150
+ return (getScheduleType (msk) == ScheduleType::Yearly) ? true : false ;
110
151
}
111
152
112
153
bool isScheduleInSeconds (unsigned int msk) {
113
- if ((msk & 0xC0000000 ) == 0x00000000 ) {
114
- return true ;
154
+ if (isScheduleFixed (msk) ) {
155
+ return ( getScheduleUnit (msk) == ScheduleUnit::Seconds) ? true : false ;
115
156
} else {
116
157
return false ;
117
158
}
118
159
}
119
160
120
161
bool isScheduleInMinutes (unsigned int msk) {
121
- if ((msk & 0xC0000000 ) == 0x40000000 ) {
122
- return true ;
162
+ if (isScheduleFixed (msk) ) {
163
+ return ( getScheduleUnit (msk) == ScheduleUnit::Minutes) ? true : false ;
123
164
} else {
124
165
return false ;
125
166
}
126
167
}
127
168
128
169
bool isScheduleInHours (unsigned int msk) {
129
- if ((msk & 0xC0000000 ) == 0x80000000 ) {
130
- return true ;
170
+ if (isScheduleFixed (msk) ) {
171
+ return ( getScheduleUnit (msk) == ScheduleUnit::Hours) ? true : false ;
131
172
} else {
132
173
return false ;
133
174
}
134
175
}
135
176
136
177
bool isScheduleInDays (unsigned int msk) {
137
- if ((msk & 0xC0000000 ) == 0xC0000000 ) {
138
- return true ;
178
+ if (isScheduleFixed (msk) ) {
179
+ return ( getScheduleUnit (msk) == ScheduleUnit::Days) ? true : false ;
139
180
} else {
140
181
return false ;
141
182
}
142
183
}
143
184
144
- unsigned int timeToWeekMask (time_t rawtime ) {
185
+ unsigned int getCurrentDayMask (time_t time ) {
145
186
struct tm * ptm;
146
- ptm = gmtime ( &rawtime );
187
+ ptm = gmtime (& time );
147
188
148
189
return 1 << ptm->tm_wday ;
149
190
}
150
191
151
- unsigned int timeToDay (time_t rawtime ) {
192
+ unsigned int getCurrentDay (time_t time ) {
152
193
struct tm * ptm;
153
- ptm = gmtime ( &rawtime );
194
+ ptm = gmtime (& time );
154
195
155
196
return ptm->tm_mday ;
156
197
}
157
198
158
- unsigned int timeToMonth (time_t rawtime ) {
199
+ unsigned int getCurrentMonth (time_t time ) {
159
200
struct tm * ptm;
160
- ptm = gmtime ( &rawtime );
201
+ ptm = gmtime (& time );
161
202
162
203
return ptm->tm_mon ;
163
204
}
164
205
165
- unsigned int getScheduleRawMask (unsigned int msk) {
166
- return msk & 0x03FFFFFF ;
167
- }
168
-
169
- unsigned int getScheduleWeekMask (unsigned int msk) {
170
- return msk & 0x000000FF ;
171
- }
172
-
173
- unsigned int getScheduleDay (unsigned int msk) {
174
- return msk & 0x000000FF ;
175
- }
176
-
177
- unsigned int getScheduleMonth (unsigned int msk) {
178
- return (msk & 0x0000FF00 ) >> 8 ;
179
- }
180
-
181
206
bool checkSchedulePeriod (unsigned int now, unsigned int frm, unsigned int to) {
207
+ /* Check if current time is inside the schedule period. If 'to' is equal to
208
+ * 0 the schedule has no end.
209
+ */
182
210
if (now >= frm && (now < to || to == 0 )) {
183
211
return true ;
184
212
} else {
@@ -189,39 +217,33 @@ class Schedule : public TimeService {
189
217
bool checkScheduleMask (unsigned int now, unsigned int msk) {
190
218
if (isScheduleFixed (msk) || isScheduleOneShot (msk)) {
191
219
return true ;
192
- }
220
+ }
193
221
194
222
if (isScheduleWeekly (msk)) {
195
- unsigned int nowMask = timeToWeekMask (now);
223
+ unsigned int currentDayMask = getCurrentDayMask (now);
196
224
unsigned int scheduleMask = getScheduleWeekMask (msk);
197
225
198
- if ((nowMask & scheduleMask) == 0 ) {
199
- return false ;
200
- } else {
226
+ if ((currentDayMask & scheduleMask) != 0 ) {
201
227
return true ;
202
228
}
203
229
}
204
230
205
231
if (isScheduleMonthly (msk)) {
206
- unsigned int nowDay = timeToDay (now);
232
+ unsigned int currentDay = getCurrentDay (now);
207
233
unsigned int scheduleDay = getScheduleDay (msk);
208
234
209
- if (nowDay != scheduleDay) {
210
- return false ;
211
- } else {
235
+ if (currentDay == scheduleDay) {
212
236
return true ;
213
237
}
214
238
}
215
239
216
240
if (isScheduleYearly (msk)) {
217
- unsigned int nowDay = timeToDay (now);
241
+ unsigned int currentDay = getCurrentDay (now);
218
242
unsigned int scheduleDay = getScheduleDay (msk);
219
- unsigned int nowMonth = timeToMonth (now);
243
+ unsigned int currentMonth = getCurrentMonth (now);
220
244
unsigned int scheduleMonth = getScheduleMonth (msk);
221
245
222
- if ((nowDay != scheduleDay) || (nowMonth != scheduleMonth)) {
223
- return false ;
224
- } else {
246
+ if ((currentDay == scheduleDay) && (currentMonth == scheduleMonth)) {
225
247
return true ;
226
248
}
227
249
}
@@ -230,29 +252,23 @@ class Schedule : public TimeService {
230
252
}
231
253
232
254
unsigned int getScheduleDelta (unsigned int msk) {
233
- if (isScheduleOneShot (msk)) {
234
- return 0xFFFFFFFF ;
255
+ if (isScheduleInSeconds (msk)) {
256
+ return SECONDS * getScheduleRepetition (msk) ;
235
257
}
236
-
237
- if (isScheduleFixed (msk)) {
238
- if (isScheduleInSeconds (msk)) {
239
- return getScheduleRawMask (msk);
240
- }
241
258
242
- if (isScheduleInMinutes (msk)) {
243
- return 60 * getScheduleRawMask (msk);
244
- }
259
+ if (isScheduleInMinutes (msk)) {
260
+ return MINUTES * getScheduleRepetition (msk);
261
+ }
245
262
246
- if (isScheduleInHours (msk)) {
247
- return 60 * 60 * getScheduleRawMask (msk);
248
- }
263
+ if (isScheduleInHours (msk)) {
264
+ return HOURS * getScheduleRepetition (msk);
249
265
}
250
266
251
267
if (isScheduleWeekly (msk) || isScheduleMonthly (msk) || isScheduleYearly (msk)) {
252
- return 60 * 60 * 24 ;
268
+ return DAYS ;
253
269
}
254
270
255
- return 0 ;
271
+ return SCHEDULE_ONE_SHOT ;
256
272
}
257
273
};
258
274
0 commit comments