30
30
/* *****************************************************************************
31
31
CLASS DECLARATION
32
32
******************************************************************************/
33
- enum class MaskType : int {
34
- minute = 0 ,
35
- hour = 1 ,
36
- day = 2 ,
37
- week = 3 , /* Weekly daymask */
38
- month = 4 , /* Day of the month 1-31 */
39
- year = 5 /* Month 1-12 + Day of the month 1-31 */
40
- };
41
-
42
33
class Scheduler : public TimeService {
43
34
public:
44
- unsigned int start, end, duration;
45
- int type;
46
- unsigned int mask;
47
- Scheduler (unsigned int s, unsigned int e, unsigned int d, int t, unsigned int m): start(s), end(e), duration(d), type(t), mask(m) {}
35
+ unsigned int start, end, duration, mask;
36
+ Scheduler (unsigned int s, unsigned int e, unsigned int d, unsigned int m): start(s), end(e), duration(d), mask(m) {}
48
37
49
38
bool isActive () {
50
39
51
40
unsigned int now = getTime ();
52
- if (now >= start && (now < end || end == 0 )) {
41
+ if (checkSchedulePeriod ( now, start, end)) {
53
42
/* We are in the schedule range */
54
43
55
- if (type == 3 || type == 4 || type == 5 ) {
56
- unsigned int nowmask = timeToMask (type, now);
57
- if ( (nowmask & mask) == 0 ) {
58
- /* This is not the correct Day or Month */
59
- return false ;
44
+ if (checkScheduleMask (now, mask)) {
45
+
46
+ /* We can assume now that the schedule is always repeating with fixed delta */
47
+ unsigned int delta = getScheduleDelta (mask);
48
+ if ( ( (std::max (now , start) - std::min (now , start)) % delta ) <= duration ) {
49
+ return true ;
60
50
}
61
51
}
62
-
63
- /* We can assume now that the schedule is always repeating with fixed delta */
64
- unsigned int delta = getScheduleDelta (type, mask);
65
- if ( ( (std::max (now , start) - std::min (now , start)) % delta ) <= duration ) {
66
- return true ;
67
- }
68
52
}
69
53
return false ;
70
54
}
@@ -73,62 +57,201 @@ class Scheduler : public TimeService {
73
57
start = aScheduler.start ;
74
58
end = aScheduler.end ;
75
59
duration = aScheduler.duration ;
76
- type = aScheduler.type ;
77
60
mask = aScheduler.mask ;
78
61
return *this ;
79
62
}
80
63
81
64
bool operator ==(Scheduler & aScheduler) {
82
- return start == aScheduler.start && end == aScheduler.end && duration == aScheduler.duration && type == aScheduler. type && mask == aScheduler.mask ;
65
+ return start == aScheduler.start && end == aScheduler.end && duration == aScheduler.duration && mask == aScheduler.mask ;
83
66
}
84
67
85
68
bool operator !=(Scheduler & aScheduler) {
86
69
return !(operator ==(aScheduler));
87
70
}
88
71
private:
72
+ bool isScheduleOneShot (unsigned int mask) {
73
+ if ((mask & 0x3C000000 ) == 0x00000000 ) {
74
+ return true ;
75
+ } else {
76
+ return false ;
77
+ }
78
+ }
89
79
90
- unsigned int timeToMask (int type, time_t rawtime) {
91
- struct tm * ptm;
92
- ptm = gmtime ( &rawtime );
80
+ bool isScheduleFixed (unsigned int mask) {
81
+ if ((mask & 0x3C000000 ) == 0x04000000 ) {
82
+ return true ;
83
+ } else {
84
+ return false ;
85
+ }
86
+ }
93
87
94
- if (type == 3 ) {
95
- return 1 << ptm->tm_wday ;
88
+ bool isScheduleWeekly (unsigned int mask) {
89
+ if ((mask & 0x3C000000 ) == 0x08000000 ) {
90
+ return true ;
91
+ } else {
92
+ return false ;
96
93
}
94
+ }
97
95
98
- if (type == 4 ) {
99
- return ptm->tm_mday ;
96
+ bool isScheduleMonthly (unsigned int mask) {
97
+ if ((mask & 0x3C000000 ) == 0x0C000000 ) {
98
+ return true ;
99
+ } else {
100
+ return false ;
100
101
}
102
+ }
101
103
102
- if (type == 5 ) {
103
- return (tm->tm_mon << 16 ) | ptm->tm_mday ;
104
+ bool isScheduleYearly (unsigned int mask) {
105
+ if ((mask & 0x3C000000 ) == 0x10000000 ) {
106
+ return true ;
107
+ } else {
108
+ return false ;
104
109
}
105
- return 0 ;
106
110
}
107
111
108
- unsigned int getScheduleDelta (int type, unsigned int mask) {
109
- if (type == 0 ) {
110
- return 60 * mask;
112
+ bool isScheduleInSeconds (unsigned int mask) {
113
+ if ((mask & 0xC0000000 ) == 0x00000000 ) {
114
+ return true ;
115
+ } else {
116
+ return false ;
111
117
}
118
+ }
112
119
113
- if (type == 1 ) {
114
- return 60 * 60 * mask;
120
+ bool isScheduleInMinutes (unsigned int mask) {
121
+ if ((mask & 0xC0000000 ) == 0x40000000 ) {
122
+ return true ;
123
+ } else {
124
+ return false ;
115
125
}
126
+ }
116
127
117
- if (type == 2 ) {
118
- return 60 * 60 * 24 * mask;
128
+ bool isScheduleInHours (unsigned int mask) {
129
+ if ((mask & 0xC0000000 ) == 0x80000000 ) {
130
+ return true ;
131
+ } else {
132
+ return false ;
119
133
}
134
+ }
120
135
121
- if (type == 3 ) {
122
- return 60 * 60 * 24 ;
136
+ bool isScheduleInDays (unsigned int mask) {
137
+ if ((mask & 0xC0000000 ) == 0xC0000000 ) {
138
+ return true ;
139
+ } else {
140
+ return false ;
123
141
}
142
+ }
124
143
125
- if (type == 4 ) {
126
- return 60 * 60 * 24 ;
144
+ unsigned int timeToWeekMask (time_t rawtime) {
145
+ struct tm * ptm;
146
+ ptm = gmtime ( &rawtime );
147
+
148
+ return 1 << ptm->tm_wday ;
149
+ }
150
+
151
+ unsigned int timeToDay (time_t rawtime) {
152
+ struct tm * ptm;
153
+ ptm = gmtime ( &rawtime );
154
+
155
+ return ptm->tm_mday ;
156
+ }
157
+
158
+ unsigned int timeToMonth (time_t rawtime) {
159
+ struct tm * ptm;
160
+ ptm = gmtime ( &rawtime );
161
+
162
+ return ptm->tm_mon ;
163
+ }
164
+
165
+ unsigned int getScheduleRawMask (unsigned int mask) {
166
+ return mask & 0x03FFFFFF ;
167
+ }
168
+
169
+ unsigned int getScheduleWeekMask (unsigned int mask) {
170
+ return mask & 0x000000FF ;
171
+ }
172
+
173
+ unsigned int getScheduleDay (unsigned int mask) {
174
+ return mask & 0x000000FF ;
175
+ }
176
+
177
+ unsigned int getScheduleMonth (unsigned int mask) {
178
+ return (mask & 0x0000FF00 ) >> 8 ;
179
+ }
180
+
181
+ bool checkSchedulePeriod (unsigned int now, unsigned int start, unsigned int end) {
182
+ if (now >= start && (now < end || end == 0 )) {
183
+ return true ;
184
+ } else {
185
+ return false ;
186
+ }
187
+ }
188
+
189
+ bool checkScheduleMask (unsigned int now, unsigned int mask) {
190
+ if (isScheduleFixed (mask) || isScheduleOneShot (mask)) {
191
+ return true ;
192
+ }
193
+
194
+ if (isScheduleWeekly (mask)) {
195
+ unsigned int nowMask = timeToWeekMask (now);
196
+ unsigned int scheduleMask = getScheduleWeekMask (mask);
197
+
198
+ if ((nowMask & scheduleMask) == 0 ) {
199
+ return false ;
200
+ } else {
201
+ return true ;
202
+ }
203
+ }
204
+
205
+ if (isScheduleMonthly (mask)) {
206
+ unsigned int nowDay = timeToDay (now);
207
+ unsigned int scheduleDay = getScheduleDay (mask);
208
+
209
+ if (nowDay != scheduleDay) {
210
+ return false ;
211
+ } else {
212
+ return true ;
213
+ }
214
+ }
215
+
216
+ if (isScheduleYearly (mask)) {
217
+ unsigned int nowDay = timeToDay (now);
218
+ unsigned int scheduleDay = getScheduleDay (mask);
219
+ unsigned int nowMonth = timeToMonth (now);
220
+ unsigned int scheduleMonth = getScheduleMonth (mask);
221
+
222
+ if ((nowDay != scheduleDay) || (nowMonth != scheduleMonth)) {
223
+ return false ;
224
+ } else {
225
+ return true ;
226
+ }
227
+ }
228
+
229
+ return false ;
230
+ }
231
+
232
+ unsigned int getScheduleDelta (unsigned int mask) {
233
+ if (isScheduleOneShot (mask)) {
234
+ return 0xFFFFFFFF ;
127
235
}
236
+
237
+ if (isScheduleFixed (mask)) {
238
+ if (isScheduleInSeconds (mask)) {
239
+ return getScheduleRawMask (mask);
240
+ }
241
+
242
+ if (isScheduleInMinutes (mask)) {
243
+ return 60 * getScheduleRawMask (mask);
244
+ }
128
245
129
- if (type == 5 ) {
246
+ if (isScheduleInHours (mask)) {
247
+ return 60 * 60 * getScheduleRawMask (mask);
248
+ }
249
+ }
250
+
251
+ if (isScheduleWeekly (mask) || isScheduleMonthly (mask) || isScheduleYearly (mask)) {
130
252
return 60 * 60 * 24 ;
131
253
}
254
+
132
255
return 0 ;
133
256
}
134
257
};
@@ -138,8 +261,8 @@ class CloudScheduler : public Property {
138
261
Scheduler _value,
139
262
_cloud_value;
140
263
public:
141
- CloudScheduler () : _value(0 , 0 , 0 , 0 , 0 ), _cloud_value(0 , 0 , 0 , 0 , 0 ) {}
142
- CloudScheduler (unsigned int start, unsigned int end, unsigned int duration, int type, unsigned int mask) : _value(start, end, duration, type, mask), _cloud_value(start, end, duration, type , mask) {}
264
+ CloudScheduler () : _value(0 , 0 , 0 , 0 ), _cloud_value(0 , 0 , 0 , 0 ) {}
265
+ CloudScheduler (unsigned int start, unsigned int end, unsigned int duration, unsigned int mask) : _value(start, end, duration, mask), _cloud_value(start, end, duration, mask) {}
143
266
144
267
virtual bool isDifferentFromCloud () {
145
268
@@ -150,7 +273,6 @@ class CloudScheduler : public Property {
150
273
_value.start = aScheduler.start ;
151
274
_value.end = aScheduler.end ;
152
275
_value.duration = aScheduler.duration ;
153
- _value.type = aScheduler.type ;
154
276
_value.mask = aScheduler.mask ;
155
277
updateLocalTimestamp ();
156
278
return *this ;
@@ -174,15 +296,13 @@ class CloudScheduler : public Property {
174
296
CHECK_CBOR (appendAttribute (_value.start ));
175
297
CHECK_CBOR (appendAttribute (_value.end ));
176
298
CHECK_CBOR (appendAttribute (_value.duration ));
177
- CHECK_CBOR (appendAttribute (_value.type ));
178
299
CHECK_CBOR (appendAttribute (_value.mask ));
179
300
return CborNoError;
180
301
}
181
302
virtual void setAttributesFromCloud () {
182
303
setAttribute (_cloud_value.start );
183
304
setAttribute (_cloud_value.end );
184
305
setAttribute (_cloud_value.duration );
185
- setAttribute (_cloud_value.type );
186
306
setAttribute (_cloud_value.mask );
187
307
}
188
308
};
0 commit comments