10
10
RTC_DS1307 RTC;
11
11
12
12
Cron::Cron (TimedCommand **iTimedCommands, int iTimedCommandsSize) {
13
- timedCommands = iTimedCommands;
14
- timedCommandsSize = iTimedCommandsSize;
13
+ timedCommands = iTimedCommands;
14
+ timedCommandsSize = iTimedCommandsSize;
15
15
16
- Wire.begin ();
16
+ Wire.begin ();
17
17
RTC.begin ();
18
18
}
19
19
@@ -23,159 +23,159 @@ Cron::~Cron() {
23
23
24
24
25
25
void Cron::loop (){
26
- bool match = false ;
27
- for (int i = 0 ; i< timedCommandsSize; i++){
28
- if (matchCron (timedCommands[i]->getCronString (),getTime ())){
29
- Command *command = timedCommands[i]->getCommand ();
30
- command->execute (timedCommands[i]->getParameters ());
31
- match = true ;
32
- }
33
- }
34
- if (match){
35
- delay (1000 );
36
- }
26
+ bool match = false ;
27
+ for (int i = 0 ; i< timedCommandsSize; i++){
28
+ if (matchCron (timedCommands[i]->getCronString (),getTime ())){
29
+ Command *command = timedCommands[i]->getCommand ();
30
+ command->execute (timedCommands[i]->getParameters ());
31
+ match = true ;
32
+ }
33
+ }
34
+ if (match){
35
+ delay (1000 );
36
+ }
37
37
}
38
38
39
39
DateTime Cron::getTime (){
40
- DateTime now = RTC.now ();
41
- return now;
40
+ DateTime now = RTC.now ();
41
+ return now;
42
42
}
43
43
44
44
boolean Cron::matchCron (String cronString, DateTime time){
45
- boolean secMatch,minMatch,hourMatch,dayMatch,monMatch,yearMatch;
46
- String tempTimeString;
47
- String commandString;
48
- int cronTime[6 ];
49
- cronString.trim ();
50
- tempTimeString = cronString.substring (0 ,cronString.indexOf (' .' ));
51
- if (tempTimeString.equals (" *" )) {
52
- cronTime[0 ] = -1 ;
53
- } else {
54
- cronTime[0 ] = tempTimeString.toInt ();
55
- }
56
- cronString.trim ();
57
- cronString = cronString.substring (cronString.indexOf (' .' ) + 1 );
58
- tempTimeString = cronString.substring (0 ,cronString.indexOf (' .' ));
59
- if (tempTimeString.equals (" *" )) {
60
- cronTime[1 ] = -1 ;
61
- } else {
62
- cronTime[1 ] = tempTimeString.toInt ();
63
- }
64
- cronString = cronString.substring (cronString.indexOf (' .' ) + 1 );
65
- tempTimeString = cronString.substring (0 ,cronString.indexOf (' .' ));
66
- if (tempTimeString.equals (" *" )) {
67
- cronTime[2 ] = -1 ;
68
- } else {
69
- cronTime[2 ] = tempTimeString.toInt ();
70
- }
71
- cronString = cronString.substring (cronString.indexOf (' .' ) + 1 );
72
- tempTimeString = cronString.substring (0 ,cronString.indexOf (' .' ));
73
- if (tempTimeString.equals (" *" )) {
74
- cronTime[3 ] = -1 ;
75
- } else {
76
- cronTime[3 ] = tempTimeString.toInt ();
77
- }
78
- cronString = cronString.substring (cronString.indexOf (' .' ) + 1 );
79
- tempTimeString = cronString.substring (0 ,cronString.indexOf (' .' ));
80
- if (tempTimeString.equals (" *" )) {
81
- cronTime[4 ] = -1 ;
82
- } else {
83
- cronTime[4 ] = tempTimeString.toInt ();
84
- }
85
- cronString = cronString.substring (cronString.indexOf (' .' ) + 1 );
86
- tempTimeString = cronString.substring (0 ,cronString.indexOf (' .' ));
87
- if (tempTimeString.equals (" *" )) {
88
- cronTime[5 ] = -1 ;
89
- } else {
90
- cronTime[5 ] = tempTimeString.toInt ();
91
- }
92
- cronString = cronString.substring (cronString.indexOf (' .' ) + 1 );
93
- commandString = cronString.substring (0 ,cronString.indexOf (' .' ));
94
-
95
- cronString = " " ;
96
-
97
- if (cronTime[5 ] == -1 ){
98
- yearMatch = true ;
99
- } else
100
-
101
- if (time .year () == cronTime[5 ]){
102
- yearMatch = true ;
103
- } else {
104
- yearMatch = false ;
105
- }
106
-
107
- if (cronTime[4 ] == -1 ){
108
- monMatch = true ;
109
- } else
110
-
111
- if (time .month () == cronTime[4 ]){
112
- monMatch = true ;
113
- } else {
114
- monMatch = false ;
115
- }
116
-
117
- if (cronTime[3 ] == -1 ){
118
- dayMatch = true ;
119
- } else
120
-
121
- if (time .day () == cronTime[3 ]){
122
- dayMatch = true ;
123
- } else {
124
- dayMatch = false ;
125
- }
126
-
127
- if (cronTime[2 ] == -1 ){
128
- hourMatch = true ;
129
- } else
130
-
131
- if (time .hour () == cronTime[2 ]){
132
- hourMatch = true ;
133
- } else {
134
- hourMatch = false ;
135
- }
136
-
137
- if (cronTime[1 ] == -1 ){
138
- minMatch = true ;
139
- } else
140
-
141
- if (time .minute () == cronTime[1 ]){
142
- minMatch = true ;
143
- } else {
144
- minMatch = false ;
145
- }
146
-
147
- if (cronTime[0 ] == -1 ){
148
- secMatch = true ;
149
- } else
150
-
151
- if (time .second () == cronTime[0 ]){
152
- secMatch = true ;
153
- } else {
154
- secMatch = false ;
155
- }
156
- if (secMatch && minMatch && hourMatch && dayMatch && monMatch && yearMatch){
157
- return true ;
158
- } else {
159
- return false ;
160
- }
45
+ boolean secMatch,minMatch,hourMatch,dayMatch,monMatch,yearMatch;
46
+ String tempTimeString;
47
+ String commandString;
48
+ int cronTime[6 ];
49
+ cronString.trim ();
50
+ tempTimeString = cronString.substring (0 ,cronString.indexOf (' .' ));
51
+ if (tempTimeString.equals (" *" )) {
52
+ cronTime[0 ] = -1 ;
53
+ } else {
54
+ cronTime[0 ] = tempTimeString.toInt ();
55
+ }
56
+ cronString.trim ();
57
+ cronString = cronString.substring (cronString.indexOf (' .' ) + 1 );
58
+ tempTimeString = cronString.substring (0 ,cronString.indexOf (' .' ));
59
+ if (tempTimeString.equals (" *" )) {
60
+ cronTime[1 ] = -1 ;
61
+ } else {
62
+ cronTime[1 ] = tempTimeString.toInt ();
63
+ }
64
+ cronString = cronString.substring (cronString.indexOf (' .' ) + 1 );
65
+ tempTimeString = cronString.substring (0 ,cronString.indexOf (' .' ));
66
+ if (tempTimeString.equals (" *" )) {
67
+ cronTime[2 ] = -1 ;
68
+ } else {
69
+ cronTime[2 ] = tempTimeString.toInt ();
70
+ }
71
+ cronString = cronString.substring (cronString.indexOf (' .' ) + 1 );
72
+ tempTimeString = cronString.substring (0 ,cronString.indexOf (' .' ));
73
+ if (tempTimeString.equals (" *" )) {
74
+ cronTime[3 ] = -1 ;
75
+ } else {
76
+ cronTime[3 ] = tempTimeString.toInt ();
77
+ }
78
+ cronString = cronString.substring (cronString.indexOf (' .' ) + 1 );
79
+ tempTimeString = cronString.substring (0 ,cronString.indexOf (' .' ));
80
+ if (tempTimeString.equals (" *" )) {
81
+ cronTime[4 ] = -1 ;
82
+ } else {
83
+ cronTime[4 ] = tempTimeString.toInt ();
84
+ }
85
+ cronString = cronString.substring (cronString.indexOf (' .' ) + 1 );
86
+ tempTimeString = cronString.substring (0 ,cronString.indexOf (' .' ));
87
+ if (tempTimeString.equals (" *" )) {
88
+ cronTime[5 ] = -1 ;
89
+ } else {
90
+ cronTime[5 ] = tempTimeString.toInt ();
91
+ }
92
+ cronString = cronString.substring (cronString.indexOf (' .' ) + 1 );
93
+ commandString = cronString.substring (0 ,cronString.indexOf (' .' ));
94
+
95
+ cronString = " " ;
96
+
97
+ if (cronTime[5 ] == -1 ){
98
+ yearMatch = true ;
99
+ } else
100
+
101
+ if (time .year () == cronTime[5 ]){
102
+ yearMatch = true ;
103
+ } else {
104
+ yearMatch = false ;
105
+ }
106
+
107
+ if (cronTime[4 ] == -1 ){
108
+ monMatch = true ;
109
+ } else
110
+
111
+ if (time .month () == cronTime[4 ]){
112
+ monMatch = true ;
113
+ } else {
114
+ monMatch = false ;
115
+ }
116
+
117
+ if (cronTime[3 ] == -1 ){
118
+ dayMatch = true ;
119
+ } else
120
+
121
+ if (time .day () == cronTime[3 ]){
122
+ dayMatch = true ;
123
+ } else {
124
+ dayMatch = false ;
125
+ }
126
+
127
+ if (cronTime[2 ] == -1 ){
128
+ hourMatch = true ;
129
+ } else
130
+
131
+ if (time .hour () == cronTime[2 ]){
132
+ hourMatch = true ;
133
+ } else {
134
+ hourMatch = false ;
135
+ }
136
+
137
+ if (cronTime[1 ] == -1 ){
138
+ minMatch = true ;
139
+ } else
140
+
141
+ if (time .minute () == cronTime[1 ]){
142
+ minMatch = true ;
143
+ } else {
144
+ minMatch = false ;
145
+ }
146
+
147
+ if (cronTime[0 ] == -1 ){
148
+ secMatch = true ;
149
+ } else
150
+
151
+ if (time .second () == cronTime[0 ]){
152
+ secMatch = true ;
153
+ } else {
154
+ secMatch = false ;
155
+ }
156
+ if (secMatch && minMatch && hourMatch && dayMatch && monMatch && yearMatch){
157
+ return true ;
158
+ } else {
159
+ return false ;
160
+ }
161
161
}
162
162
163
163
void Cron::printTime (){
164
- DateTime time = getTime ();
165
- Serial.print (time .year (), DEC);
166
- Serial.print (' /' );
167
- Serial.print (time .month (), DEC);
168
- Serial.print (' /' );
169
- Serial.print (time .day (), DEC);
170
- Serial.print (' ' );
171
- Serial.print (time .hour (), DEC);
172
- Serial.print (' :' );
173
- Serial.print (time .minute (), DEC);
174
- Serial.print (' :' );
175
- Serial.print (time .second (), DEC);
176
- Serial.println ();
164
+ DateTime time = getTime ();
165
+ Serial.print (time .year (), DEC);
166
+ Serial.print (' /' );
167
+ Serial.print (time .month (), DEC);
168
+ Serial.print (' /' );
169
+ Serial.print (time .day (), DEC);
170
+ Serial.print (' ' );
171
+ Serial.print (time .hour (), DEC);
172
+ Serial.print (' :' );
173
+ Serial.print (time .minute (), DEC);
174
+ Serial.print (' :' );
175
+ Serial.print (time .second (), DEC);
176
+ Serial.println ();
177
177
}
178
178
179
179
void Cron::setTime (DateTime time){
180
- RTC.adjust (time );
180
+ RTC.adjust (time );
181
181
}
0 commit comments