-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.c
executable file
·385 lines (334 loc) · 7.25 KB
/
scheduler.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#include <stdio.h>
#include <stdlib.h>
#include <wiringPi.h>
#include <time.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "adc.h"
#include "lcd.h"
#include "scheduler.h"
/**
* If the relay is open or not
*/
time_t lastImmobileState=0;
char filenameStatus[]="/home/clement/scheduler/work/ST";
char filenameCountdown[]="/home/clement/scheduler/work/CD";
char filenameStandby[]="/home/clement/scheduler/work/SB";
char filenameLock[]="/home/clement/scheduler/work/LCK";
char filenameRemaining[]="/home/clement/scheduler/work/REM";
int state=0;
int pauseSt=IS_RUNNING;
/**
* Program pooling a directory, and doing a scheduling.
**/
int main (int argc, char** argv)
{
initPins();
adc_init();
lcd_init();
/** The status does not exist at the launch of the schduler prgram*/
clearFile();
int pid=readPid();
stopIfPidExists(pid);
writePid();
SetChrMode();
int nbSecond;
int remainingSeconds;
uchar intensity;
uchar tmpintensity;
int i;
int valueInFile;
time_t whenItsComplete ;
char timestr[7];
pinMode (RELAY_IN, OUTPUT);
// Permanent loop checking file.
int cycle=0;
while(1){
valueInFile=getCoundownValue();
#ifndef PROD
printf("pause:%d,valueInFile:%d\n",pauseSt,valueInFile);
#endif
if(valueInFile>=0){
nbSecond=valueInFile;
// The countdown
whenItsComplete = time(NULL)+nbSecond;
remainingSeconds=whenItsComplete-time(NULL);
}
else{remainingSeconds=-1;}
#ifndef PROD
printf("nbsecond:%d\n",nbSecond);
#endif
do {
/**
* Do a regular reset of the LCD
**/
if(cycle%500==0){
lcd_init();
}
else if(cycle%60==0){
resetLcd();
}
/**
* Write the remaining seconds
**/
if(cycle%60==0){
writeRemaining(remainingSeconds);
}
/**
* Increment only if not in pause (Cycle increments will be at the end of the cycle loop.
**/
updateStandbyStatus();
cycle++;
if(remainingSeconds>-1){
if(pauseSt==IS_RUNNING){
remainingSeconds=whenItsComplete-time(NULL);
}else{
whenItsComplete=remainingSeconds+time(NULL);
}
openRelay();
if(lastImmobileState==0||(time(NULL)-lastImmobileState)<NBSECONDBEFORECREENSHUTDOWN){
digitalWrite (TRANSISTOR, LOW);
}
else{
digitalWrite (TRANSISTOR, HIGH);
}
int seconds=remainingSeconds%60;
int hours=remainingSeconds/3600;
int minutes=remainingSeconds/60%60;
sprintf(timestr,"%02d:%02d:%02d",hours,minutes,seconds);
goHome();
lcd_text(timestr);
#ifndef PROD
printf("%s\n",timestr);
#endif
sleep(1);
}
/**
* Block measuring intensity
*/
if(cycle%20==0){
intensity=get_ADC_Result();
#ifndef PROD
printf("Intensite: %d\n",intensity);
#endif
}
#ifndef PROD
printf("Remaining seconds %d,cycle=%d\n",remainingSeconds,cycle);
#endif
} while ( remainingSeconds>0&& !isFilePresent());
#ifndef PROD
// printf("Sortie boucle décompte\n");
#endif
/**
* Every 10 ccyle there is a full reset of the screen. Otherwise it is light reset.
**/
if(cycle%100==0){
lcd_init();
}
nbSecond=0;
/**
*
**/
if(valueInFile==NO_FILE){
goHome();
closeRelay();
digitalWrite (TRANSISTOR, HIGH);
lcd_text("Expire " );
#ifndef PROD
printf("Expire\n");
#endif
}
else if(valueInFile==TV_ON){
goHome();
openRelay();
digitalWrite (TRANSISTOR, LOW);
lcd_text("Tele on " );
}
else if(valueInFile==TV_OFF){
goHome();
digitalWrite (TRANSISTOR, HIGH);
closeRelay();
lcd_text("Tele off " );
}
sleep(3);
}
}
/**
Opens the relay
**/
int openRelay(){
if(state != IS_OPEN){
#ifndef RELAY_DISABLED
digitalWrite (RELAY_IN, LOW);
#endif
state = IS_OPEN;
#ifndef PROD
printf("Open relay\n");
#endif
writeStatus(state);
}
}
/**
* Closes the relay
**/
int closeRelay(){
if(state != IS_CLOSED){
//pinMode (RELAY_IN, OUTPUT);
#ifndef RELAY_DISABLED
digitalWrite (RELAY_IN, HIGH);
#endif
state = IS_CLOSED;
#ifndef PROD
printf("Close relay\n");
#endif
writeStatus(state);
}
}
/**
* Check if file is present
**/
int isFilePresent(){
FILE * f=fopen (filenameCountdown, "rb");
return f!=0;
}
/**
Get coundown value.
*/
int getCoundownValue(){
int nbSecond=-3;
// read the content of the file
char * buffer = 0;
long length;
FILE * f=fopen (filenameCountdown, "rb");
if (f)
{
fseek (f, 0, SEEK_END);
length = ftell (f);
fseek (f, 0, SEEK_SET);
/** The buffer stor the content of the file */
buffer = malloc (length);
if (buffer)
{
fread (buffer, 1, length, f);
nbSecond = atoi(buffer);
}
fclose (f);
}
/**
* It the status is resumed
*/
if(f){
if(nbSecond>0){
lcd_init();
remove (filenameCountdown);
}
}
return nbSecond;
}
/**
Read the standby status from the file.
**/
void updateStandbyStatus(){
/**
Checking is the counter is running or not
**/
FILE * f=fopen (filenameStandby, "r");
if (f)
{
if(pauseSt!=IS_PAUSE){
pauseSt=IS_PAUSE;
lastImmobileState=time(NULL);
}
fclose(f);
}else{
lastImmobileState=0;
pauseSt=IS_RUNNING;
}
}
/**
Say if the relay is opebn or closed t oan external application by
wrtiing it in a file
**/
void writeStatus(int status){
FILE * f;
f = fopen(filenameStatus, "w");
/** Only write status if the file exists **/
if(f){
fprintf(f, "%d", status);
fclose(f);
}
}
/**
Save the PID
**/
void writePid(){
int pid=getpid();
FILE * f = fopen(filenameLock, "w");
/** Only write status if the file exists **/
if(f){
fprintf(f, "%d", pid);
fclose(f);
}
}
/**
Save the seconds remaining
**/
void writeRemaining(int remainingSeconds){
FILE * f = fopen(filenameRemaining, "w");
/** Only write status if the file exists **/
if(f){
fprintf(f, "%d", remainingSeconds);
fclose(f);
}
}
/**
* Remove all the files.
**/
void clearFile(){
remove(filenameStatus);
remove(filenameCountdown);
remove(filenameStandby);
}
/**
Read the standby status from the file.
**/
int readPid(){
int pid=-1;
// read the content of the file
char * buffer = 0;
long length;
FILE * f=fopen (filenameLock, "rb");
if (f)
{
fseek (f, 0, SEEK_END);
length = ftell (f);
fseek (f, 0, SEEK_SET);
/** The buffer stor the content of the file */
buffer = malloc (length);
if (buffer)
{
fread (buffer, 1, length, f);
pid = atoi(buffer);
}
fclose (f);
}
return pid;
}
/**
* Stops the program execution, if the program is already running.
**/
int stopIfPidExists(int pid){
char fileproc[20];
sprintf(fileproc,"/proc/%d",pid);
struct stat s;
int err = stat(fileproc, &s);
if(-1 == err) {
}else{
printf("Le processus %s is existing. Exiting\n",fileproc);
exit(1);
}
return 0;
}