Skip to content

Commit 4b24351

Browse files
committed
CloudSchedule refactor
1 parent 4c499aa commit 4b24351

File tree

1 file changed

+100
-84
lines changed

1 file changed

+100
-84
lines changed

src/property/types/CloudSchedule.h

+100-84
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,46 @@
2424

2525
#include <Arduino.h>
2626
#include "../Property.h"
27+
#include "../../AIoTC_Const.h"
2728
#include "utility/time/TimeService.h"
2829
#include <time.h>
2930

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+
3067
/******************************************************************************
3168
CLASS DECLARATION
3269
******************************************************************************/
@@ -69,116 +106,107 @@ class Schedule : public TimeService {
69106
return !(operator==(aSchedule));
70107
}
71108
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+
72133
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 ;
78135
}
79136

80137
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 ;
86139
}
87140

88141
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 ;
94143
}
95144

96145
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 ;
102147
}
103148

104149
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 ;
110151
}
111152

112153
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 ;
115156
} else {
116157
return false;
117158
}
118159
}
119160

120161
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 ;
123164
} else {
124165
return false;
125166
}
126167
}
127168

128169
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 ;
131172
} else {
132173
return false;
133174
}
134175
}
135176

136177
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 ;
139180
} else {
140181
return false;
141182
}
142183
}
143184

144-
unsigned int timeToWeekMask(time_t rawtime) {
185+
unsigned int getCurrentDayMask(time_t time) {
145186
struct tm * ptm;
146-
ptm = gmtime ( &rawtime );
187+
ptm = gmtime (&time);
147188

148189
return 1 << ptm->tm_wday;
149190
}
150191

151-
unsigned int timeToDay(time_t rawtime) {
192+
unsigned int getCurrentDay(time_t time) {
152193
struct tm * ptm;
153-
ptm = gmtime ( &rawtime );
194+
ptm = gmtime (&time);
154195

155196
return ptm->tm_mday;
156197
}
157198

158-
unsigned int timeToMonth(time_t rawtime) {
199+
unsigned int getCurrentMonth(time_t time) {
159200
struct tm * ptm;
160-
ptm = gmtime ( &rawtime );
201+
ptm = gmtime (&time);
161202

162203
return ptm->tm_mon;
163204
}
164205

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-
181206
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+
*/
182210
if(now >= frm && (now < to || to == 0)) {
183211
return true;
184212
} else {
@@ -189,39 +217,33 @@ class Schedule : public TimeService {
189217
bool checkScheduleMask(unsigned int now, unsigned int msk) {
190218
if(isScheduleFixed(msk) || isScheduleOneShot(msk)) {
191219
return true;
192-
}
220+
}
193221

194222
if(isScheduleWeekly(msk)) {
195-
unsigned int nowMask = timeToWeekMask(now);
223+
unsigned int currentDayMask = getCurrentDayMask(now);
196224
unsigned int scheduleMask = getScheduleWeekMask(msk);
197225

198-
if((nowMask & scheduleMask) == 0) {
199-
return false;
200-
} else {
226+
if((currentDayMask & scheduleMask) != 0) {
201227
return true;
202228
}
203229
}
204230

205231
if(isScheduleMonthly(msk)) {
206-
unsigned int nowDay = timeToDay(now);
232+
unsigned int currentDay = getCurrentDay(now);
207233
unsigned int scheduleDay = getScheduleDay(msk);
208234

209-
if(nowDay != scheduleDay) {
210-
return false;
211-
} else {
235+
if(currentDay == scheduleDay) {
212236
return true;
213237
}
214238
}
215239

216240
if(isScheduleYearly(msk)) {
217-
unsigned int nowDay = timeToDay(now);
241+
unsigned int currentDay = getCurrentDay(now);
218242
unsigned int scheduleDay = getScheduleDay(msk);
219-
unsigned int nowMonth = timeToMonth(now);
243+
unsigned int currentMonth = getCurrentMonth(now);
220244
unsigned int scheduleMonth = getScheduleMonth(msk);
221245

222-
if((nowDay != scheduleDay) || (nowMonth != scheduleMonth)) {
223-
return false;
224-
} else {
246+
if((currentDay == scheduleDay) && (currentMonth == scheduleMonth)) {
225247
return true;
226248
}
227249
}
@@ -230,29 +252,23 @@ class Schedule : public TimeService {
230252
}
231253

232254
unsigned int getScheduleDelta(unsigned int msk) {
233-
if(isScheduleOneShot(msk)) {
234-
return 0xFFFFFFFF;
255+
if(isScheduleInSeconds(msk)) {
256+
return SECONDS * getScheduleRepetition(msk);
235257
}
236-
237-
if(isScheduleFixed(msk)) {
238-
if(isScheduleInSeconds(msk)) {
239-
return getScheduleRawMask(msk);
240-
}
241258

242-
if(isScheduleInMinutes(msk)) {
243-
return 60 * getScheduleRawMask(msk);
244-
}
259+
if(isScheduleInMinutes(msk)) {
260+
return MINUTES * getScheduleRepetition(msk);
261+
}
245262

246-
if(isScheduleInHours(msk)) {
247-
return 60 * 60 * getScheduleRawMask(msk);
248-
}
263+
if(isScheduleInHours(msk)) {
264+
return HOURS * getScheduleRepetition(msk);
249265
}
250266

251267
if(isScheduleWeekly(msk) || isScheduleMonthly(msk) || isScheduleYearly(msk)) {
252-
return 60 * 60 * 24;
268+
return DAYS;
253269
}
254270

255-
return 0;
271+
return SCHEDULE_ONE_SHOT;
256272
}
257273
};
258274

0 commit comments

Comments
 (0)