3030/* *****************************************************************************
3131 CLASS DECLARATION
3232 ******************************************************************************/
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-
4233class Scheduler : public TimeService {
4334 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) {}
4837
4938 bool isActive () {
5039
5140 unsigned int now = getTime ();
52- if (now >= start && (now < end || end == 0 )) {
41+ if (checkSchedulePeriod ( now, start, end)) {
5342 /* We are in the schedule range */
5443
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 ;
6050 }
6151 }
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- }
6852 }
6953 return false ;
7054 }
@@ -73,62 +57,201 @@ class Scheduler : public TimeService {
7357 start = aScheduler.start ;
7458 end = aScheduler.end ;
7559 duration = aScheduler.duration ;
76- type = aScheduler.type ;
7760 mask = aScheduler.mask ;
7861 return *this ;
7962 }
8063
8164 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 ;
8366 }
8467
8568 bool operator !=(Scheduler & aScheduler) {
8669 return !(operator ==(aScheduler));
8770 }
8871 private:
72+ bool isScheduleOneShot (unsigned int mask) {
73+ if ((mask & 0x3C000000 ) == 0x00000000 ) {
74+ return true ;
75+ } else {
76+ return false ;
77+ }
78+ }
8979
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+ }
9387
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 ;
9693 }
94+ }
9795
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 ;
100101 }
102+ }
101103
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 ;
104109 }
105- return 0 ;
106110 }
107111
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 ;
111117 }
118+ }
112119
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 ;
115125 }
126+ }
116127
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 ;
119133 }
134+ }
120135
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 ;
123141 }
142+ }
124143
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 ;
127235 }
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+ }
128245
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)) {
130252 return 60 * 60 * 24 ;
131253 }
254+
132255 return 0 ;
133256 }
134257};
@@ -138,8 +261,8 @@ class CloudScheduler : public Property {
138261 Scheduler _value,
139262 _cloud_value;
140263 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) {}
143266
144267 virtual bool isDifferentFromCloud () {
145268
@@ -150,7 +273,6 @@ class CloudScheduler : public Property {
150273 _value.start = aScheduler.start ;
151274 _value.end = aScheduler.end ;
152275 _value.duration = aScheduler.duration ;
153- _value.type = aScheduler.type ;
154276 _value.mask = aScheduler.mask ;
155277 updateLocalTimestamp ();
156278 return *this ;
@@ -174,15 +296,13 @@ class CloudScheduler : public Property {
174296 CHECK_CBOR (appendAttribute (_value.start ));
175297 CHECK_CBOR (appendAttribute (_value.end ));
176298 CHECK_CBOR (appendAttribute (_value.duration ));
177- CHECK_CBOR (appendAttribute (_value.type ));
178299 CHECK_CBOR (appendAttribute (_value.mask ));
179300 return CborNoError;
180301 }
181302 virtual void setAttributesFromCloud () {
182303 setAttribute (_cloud_value.start );
183304 setAttribute (_cloud_value.end );
184305 setAttribute (_cloud_value.duration );
185- setAttribute (_cloud_value.type );
186306 setAttribute (_cloud_value.mask );
187307 }
188308};
0 commit comments