Skip to content

Commit 30cfea3

Browse files
committed
Initial scheduler implementation
1 parent 9ed5a57 commit 30cfea3

File tree

2 files changed

+191
-0
lines changed

2 files changed

+191
-0
lines changed

src/property/PropertyContainer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "types/CloudUnsignedInt.h"
4141
#include "types/CloudString.h"
4242
#include "types/CloudLocation.h"
43+
#include "types/CloudScheduler.h"
4344
#include "types/CloudColor.h"
4445
#include "types/CloudWrapperBase.h"
4546

src/property/types/CloudScheduler.h

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
//
2+
// This file is part of ArduinoCloudThing
3+
//
4+
// Copyright 2021 ARDUINO SA (http://www.arduino.cc/)
5+
//
6+
// This software is released under the GNU General Public License version 3,
7+
// which covers the main part of ArduinoCloudThing.
8+
// The terms of this license can be found at:
9+
// https://www.gnu.org/licenses/gpl-3.0.en.html
10+
//
11+
// You can be released from the requirements of the above licenses by purchasing
12+
// a commercial license. Buying such a license is mandatory if you want to modify or
13+
// otherwise use the software for commercial activities involving the Arduino
14+
// software without disclosing the source code of your own applications. To purchase
15+
// a commercial license, send an email to [email protected].
16+
//
17+
18+
#ifndef CLOUDSCHEDULER_H_
19+
#define CLOUDSCHEDULER_H_
20+
21+
/******************************************************************************
22+
INCLUDE
23+
******************************************************************************/
24+
25+
#include <Arduino.h>
26+
#include "../Property.h"
27+
#include "utility/time/TimeService.h"
28+
#include <time.h>
29+
30+
/******************************************************************************
31+
CLASS DECLARATION
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+
class Scheduler : public TimeService {
43+
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) {}
48+
49+
bool isActive() {
50+
51+
unsigned int now = getTime();
52+
if(now >= start && (now < end || end == 0)) {
53+
/* We are in the schedule range */
54+
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;
60+
}
61+
}
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+
}
69+
return false;
70+
}
71+
72+
Scheduler& operator=(Scheduler & aScheduler) {
73+
start = aScheduler.start;
74+
end = aScheduler.end;
75+
duration = aScheduler.duration;
76+
type = aScheduler.type;
77+
mask = aScheduler.mask;
78+
return *this;
79+
}
80+
81+
bool operator==(Scheduler & aScheduler) {
82+
return start == aScheduler.start && end == aScheduler.end && duration == aScheduler.duration && type == aScheduler.type && mask == aScheduler.mask;
83+
}
84+
85+
bool operator!=(Scheduler & aScheduler) {
86+
return !(operator==(aScheduler));
87+
}
88+
private:
89+
90+
unsigned int timeToMask(int type, time_t rawtime) {
91+
struct tm * ptm;
92+
ptm = gmtime ( &rawtime );
93+
94+
if (type == 3) {
95+
return 1 << ptm->tm_wday;
96+
}
97+
98+
if (type == 4) {
99+
return ptm->tm_mday;
100+
}
101+
102+
if (type == 5) {
103+
return (tm->tm_mon << 16) | ptm->tm_mday;
104+
}
105+
return 0;
106+
}
107+
108+
unsigned int getScheduleDelta(int type, unsigned int mask) {
109+
if (type == 0) {
110+
return 60 * mask;
111+
}
112+
113+
if (type == 1) {
114+
return 60 * 60 * mask;
115+
}
116+
117+
if (type == 2) {
118+
return 60 * 60 * 24 * mask;
119+
}
120+
121+
if (type == 3) {
122+
return 60 * 60 * 24;
123+
}
124+
125+
if (type == 4) {
126+
return 60 * 60 * 24;
127+
}
128+
129+
if (type == 5) {
130+
return 60 * 60 * 24;
131+
}
132+
return 0;
133+
}
134+
};
135+
136+
class CloudScheduler : public Property {
137+
private:
138+
Scheduler _value,
139+
_cloud_value;
140+
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) {}
143+
144+
virtual bool isDifferentFromCloud() {
145+
146+
return _value != _cloud_value;
147+
}
148+
149+
CloudScheduler& operator=(Scheduler aScheduler) {
150+
_value.start = aScheduler.start;
151+
_value.end = aScheduler.end;
152+
_value.duration = aScheduler.duration;
153+
_value.type = aScheduler.type;
154+
_value.mask = aScheduler.mask;
155+
updateLocalTimestamp();
156+
return *this;
157+
}
158+
159+
Scheduler getCloudValue() {
160+
return _cloud_value;
161+
}
162+
163+
Scheduler getValue() {
164+
return _value;
165+
}
166+
167+
virtual void fromCloudToLocal() {
168+
_value = _cloud_value;
169+
}
170+
virtual void fromLocalToCloud() {
171+
_cloud_value = _value;
172+
}
173+
virtual CborError appendAttributesToCloud() {
174+
CHECK_CBOR(appendAttribute(_value.start));
175+
CHECK_CBOR(appendAttribute(_value.end));
176+
CHECK_CBOR(appendAttribute(_value.duration));
177+
CHECK_CBOR(appendAttribute(_value.type));
178+
CHECK_CBOR(appendAttribute(_value.mask));
179+
return CborNoError;
180+
}
181+
virtual void setAttributesFromCloud() {
182+
setAttribute(_cloud_value.start);
183+
setAttribute(_cloud_value.end);
184+
setAttribute(_cloud_value.duration);
185+
setAttribute(_cloud_value.type);
186+
setAttribute(_cloud_value.mask);
187+
}
188+
};
189+
190+
#endif /* CLOUDSCHEDULER_H_ */

0 commit comments

Comments
 (0)