This repository was archived by the owner on Feb 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgHebdo.c
592 lines (493 loc) · 14.2 KB
/
ProgHebdo.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef WIN32
#include <winsock2.h>
#endif
#include <time.h>
#include "Service.h"
#include "Fichier.h"
#include "xpp.h"
#include "ProgHebdo.h"
#ifdef WIN32
#define close closesocket
#endif
int g_bServiceStop = FALSE;
int g_bServicePause = FALSE;
int g_receiver_sockfd;
int g_IntervalHbeat;
PARAM *g_LstParam = NULL;
PRGHBDO *g_LstPrgHdbo = NULL;
char FichierCnf[256];
/****************************************************************************************************************/
/* Initialisation du module ProgHebdo */
int ProgHebdo_init()
{
char i_tmp[20];
char i_uniqueID[20];
char i_instance[20];
char i_interfacename[20];
int i_interfaceport;
int i_debuglevel;
*i_instance = '\0';
*i_uniqueID = '\0';
*i_interfacename = '\0';
i_interfaceport = 0;
i_debuglevel = 0;
if(Fcnf_Valeur(g_LstParam, "XAP_Port", i_tmp)==1) i_interfaceport = atoi(i_tmp); //3639
if(Fcnf_Valeur(g_LstParam, "XAP_Debug", i_tmp)==1) i_debuglevel = atoi(i_tmp); //0
Fcnf_Valeur(g_LstParam, "XAP_UID", i_uniqueID); //GUID
Fcnf_Valeur(g_LstParam, "XAP_Instance", i_instance); //DEFAULT_INSTANCE
Fcnf_Valeur(g_LstParam, "XAP_Interface", i_interfacename); //eth0
if(Fcnf_Valeur(g_LstParam, "ProgHebdo_Debug", i_tmp)==1) g_debuglevel = atoi(i_tmp); //0
if(Fcnf_Valeur(g_LstParam, "XAP_IntervalHbeat", i_tmp)==1) g_IntervalHbeat = atoi(i_tmp);
if(g_IntervalHbeat==0) g_IntervalHbeat = 60;
return xpp_init(i_uniqueID, i_interfacename, i_interfaceport, i_instance, i_debuglevel);
}
/****************************************************************************************************************/
/* Fonctions de gestion des plages */
int Plage_Jour2Num(char *Jour)
{
if(g_debuglevel>=DEBUG_DEBUG) Flog_Ecrire("Jour vers Num %s.",Jour);
if(STRICMP(Jour, "Lu") == 0) return 1;
if(STRICMP(Jour, "Ma") == 0) return 2;
if(STRICMP(Jour, "Me") == 0) return 3;
if(STRICMP(Jour, "Je") == 0) return 4;
if(STRICMP(Jour, "Ve") == 0) return 5;
if(STRICMP(Jour, "Sa") == 0) return 6;
if(STRICMP(Jour, "Di") == 0) return 7;
Flog_Ecrire("Jour inconnu %s (Jours possibles : Lu, Ma, Me, Je, Ve, Sa, Di).", Jour);
return 0;
}
int Plage_Heure2Num(char *Heure)
{
int h,m;
if(Heure[2]=!':')
{
Flog_Ecrire("Format d'heure incorrect %s (attendu HH:MM).", Heure);
return -1;
}
h = (Heure[0]-'0')*10+(Heure[1]-'0');
m = (Heure[3]-'0')*10+(Heure[4]-'0');
return h*100+m;
}
int Plage_ParseJoursPlage(char *PlageJour, PLAGE *stPlage, int Ind)
{
if(g_debuglevel>=DEBUG_DEBUG) Flog_Ecrire("Parse d'une plage de jours %s.",PlageJour);
char *Pos;
int i, j1, j2;
Pos = strchr(PlageJour, '-');
if(Pos==NULL) return Ind;
*Pos='\0';
j1 = Plage_Jour2Num(PlageJour);
j2 = Plage_Jour2Num(Pos+1);
if(j1==0)
{
Flog_Ecrire("Jour début %s inconnu dans une plage.", PlageJour);
return Ind;
}
if(j2==0)
{
Flog_Ecrire("Jour fin %s inconnu dans une plage.", Pos+1);
return Ind;
}
if(j1>j2) { i=j1; j1=j2; j2=i; }
for(i=j1; i<=j2; i++) stPlage->Jours[++Ind] = i;
return Ind;
}
int Plage_ParseJours(char *Jours, PLAGE *stPlage, int Ind)
{
// Ex : Lu,Ma-Je,Ve ou Lu-Ve ou *
char *Pos, *Jour, Semaine[]="Lu-Di";
int bFin;
int j;
if(g_debuglevel>=DEBUG_DEBUG) Flog_Ecrire("Parse des jours %s.",Jours);
bFin = FALSE;
Jour = Jours;
do
{
Pos = strchr(Jour, ',');
if(Pos!=NULL)
*Pos = '\0';
else
bFin = TRUE;
if(Jour[0] == '*') return Plage_ParseJours(Semaine, stPlage, 0);
if(strchr(Jour, '-'))
Ind = Plage_ParseJoursPlage(Jour, stPlage, Ind);
else
{
j = Plage_Jour2Num(Jour);
if(j!=0) stPlage->Jours[++Ind] = j;
}
Jour = Pos+1;
} while(!bFin);
return Ind;
}
HORAIRE *Plage_ParseHorairePlage(char *Horaire)
{
HORAIRE *stHoraire;
int Deb,Fin;
// Ex : 07:00-08:00 ou 20:30-21:30
if((Horaire[2]!=':')||(Horaire[5]!='-')||(Horaire[8]!=':'))
{
Flog_Ecrire("Format de plage d'heure incorrect %s (attendu HH:MM-HH:MM).", Horaire);
return NULL;
}
Horaire[5]='\0';
Deb = Plage_Heure2Num(Horaire);
Fin = Plage_Heure2Num(Horaire+6);
if( (Deb==-1) || (Fin==-1) ) return NULL;
//Créer l'horaire
stHoraire = malloc(sizeof(HORAIRE));
if(stHoraire==NULL)
{
if(g_debuglevel>=DEBUG_INFO) Flog_Ecrire("Pas assez de mémoire pour stocker les plages d'horaires.");
return NULL;
}
stHoraire->Debut = Deb;
stHoraire->Fin = Fin;
stHoraire->Suivant = NULL;
return stHoraire;
}
HORAIRE *Plage_ParseHoraires(char *Horaires)
{
// Ex : 07:00-08:00,20:30-21:30
char *Pos, *Horaire;
int bFin;
HORAIRE *stHoraire;
HORAIRE *LstHoraire=NULL;
bFin = FALSE;
Horaire = Horaires;
do
{
Pos = strchr(Horaire, ',');
if(Pos!=NULL)
*Pos = '\0';
else
bFin = TRUE;
stHoraire = Plage_ParseHorairePlage(Horaire);
if(stHoraire!=NULL)
{
stHoraire->Suivant = LstHoraire;
LstHoraire = stHoraire;
}
Horaire = Pos+1;
} while(!bFin);
return LstHoraire;
}
/****************************************************************************************************************/
/* Fonction principale de gestion des plages */
/* Ex : Plage1=Lu,Ma,Me,Je,Ve;07:00-08:00,20:30-21:30;22 */
/* Plage2=Lu-Ve;08:00-20:30;19 */
/* Plage3=Sa,Di;08:00-09:00,21:00-22:00;22 */
/* Plage4=Sa,Di;09:00-21:00;19 */
/* Plage5=*;09:00-21:00;20 */
int Plage_Parse(char *chPlage, PRGHBDO *PrgHbdo)
{
char *pos;
char *Jours, *Horaires, *Consigne;
PLAGE *stPlage;
if(g_debuglevel>=DEBUG_DEBUG) Flog_Ecrire("Parse de la plage %s.",chPlage);
//Isoler les jours
Jours = chPlage;
pos = strchr(Jours, ';');
if(pos==NULL)
{
Flog_Ecrire("La plage %s ne contient pas de jours (absence de ';')", chPlage);
return FALSE;
}
*pos = '\0';
//Isoler les Horaires
Horaires = pos+1;
pos = strchr(Horaires, ';');
if(pos==NULL)
{
Flog_Ecrire("La plage %s ne contient pas d'horaires (absence de ';')", chPlage);
return FALSE;
}
*pos = '\0';
//Isoler la consigne
Consigne = pos+1;
//Créer la plage
stPlage = malloc(sizeof(PLAGE));
if(stPlage==NULL)
{
if(g_debuglevel>=DEBUG_INFO) Flog_Ecrire("Pas assez de mémoire pour stocker les plages.");
return FALSE;
}
stPlage->Suivant = PrgHbdo->Plages;
PrgHbdo->Plages = stPlage;
stPlage->Valeur = atof(Consigne);
if(STRICMP(Consigne, "ON") == 0) stPlage->Valeur = 1; //atof("OFF") donne déjà 0
Plage_ParseJours(Jours, stPlage, 0);
stPlage->Horaire = Plage_ParseHoraires(Horaires);
return TRUE;
}
/****************************************************************************************************************/
/* Lire le paramétrage des plages dans le fichier de config */
int ProgHebdo_LireIni()
{
int i,j,No;
char Cle[128];
char Section[128];
char Temp[256];
PRGHBDO *PrgHdbo;
if (g_debuglevel>=DEBUG_DEBUG) Flog_Ecrire("Parcours des sections du fichier de conf.");
//Faire le ménage
while(g_LstPrgHdbo != NULL)
{
PrgHdbo = g_LstPrgHdbo;
g_LstPrgHdbo = PrgHdbo->Suivant;
free(PrgHdbo);
}
//Parcourir les sections
i=1;
No=1;
while(Fcnf_Section(g_LstParam, i, Section))
{
//Sauter la section XAP et ProgHebo
if(STRICMP(Section, "XAP")==0)
{
if(g_debuglevel>=DEBUG_DEBUG) Flog_Ecrire("Section XAP ignorée.");
i++;
continue;
}
if(STRICMP(Section, "ProgHebdo")==0)
{
if(g_debuglevel>=DEBUG_DEBUG) Flog_Ecrire("Section ProgHebdo ignorée.");
i++;
continue;
}
//Section valide ?
sprintf(Cle, "%s_Sortie", Section);
if(!Fcnf_Valeur(g_LstParam, Cle, Temp))
{
if(g_debuglevel>=DEBUG_INFO) Flog_Ecrire("Section %s ignorée car aucune sortie n'est défini.", Section);
i++;
continue;
}
//Allocation structure
PrgHdbo = malloc(sizeof(PRGHBDO));
if(PrgHdbo==NULL)
{
if(g_debuglevel>=DEBUG_INFO) Flog_Ecrire("Pas assez de mémoire pour stocker les programmations hebdomadaires.");
return FALSE;
}
//Lecture des paramètres
strcpy(PrgHdbo->Nom, Section);
strcpy(PrgHdbo->Sortie, Temp);
PrgHdbo->Plages = NULL;
PrgHdbo->bInit = FALSE;
PrgHdbo->No = No;
No++;
sprintf(Cle, "%s_Defaut", Section);
if(Fcnf_Valeur(g_LstParam, Cle, Temp))
{
PrgHdbo->Defaut = atof(Temp);
if(STRICMP(Temp, "ON") == 0) PrgHdbo->Defaut = 1; //atof("OFF") donne déjà 0
}
else
PrgHdbo->Defaut = 0;
//Lecture des plages
j=1;
do
{
sprintf(Cle, "%s_Plage%d", Section, j);
if(Fcnf_Valeur(g_LstParam, Cle, Temp))
{
if(g_debuglevel>=DEBUG_DEBUG) Flog_Ecrire("Lecture de la plage %s.",Cle);
Plage_Parse(Temp, PrgHdbo);
}
else
{
j=-1;
}
j++;
} while(j!=0);
if (g_debuglevel>=DEBUG_VERBOSE) Flog_Ecrire("Programmation %s : %s = %f par défaut", PrgHdbo->Nom, PrgHdbo->Sortie, PrgHdbo->Defaut);
PrgHdbo->Suivant = g_LstPrgHdbo;
g_LstPrgHdbo = PrgHdbo;
i++;
}
if (g_debuglevel>=DEBUG_DEBUG) Flog_Ecrire("Fin de parcours des sections du fichier de conf.");
return TRUE;
}
/****************************************************************************************************************/
/* Déclenchement */
PLAGE *CherchePlage(int Jour, int Heure, PLAGE *Plages)
{
int i, bOK;
PLAGE *Plage;
HORAIRE *Horaire;
Plage = Plages;
while(Plage!=NULL)
{
bOK = FALSE;
for(i=0; i<8; i++) if(Plage->Jours[i]==Jour)
{
bOK=TRUE;
i=8;
}
if(bOK==FALSE)
{
Plage = Plage->Suivant;
continue;
}
Horaire = Plage->Horaire;
while(Horaire != NULL)
{
if( (Horaire->Debut <= Heure) && (Horaire->Fin >= Heure) ) return Plage;
Horaire = Horaire->Suivant;
}
Plage = Plage->Suivant;
}
return NULL;
}
int ProgHebdo()
{
time_t timestamp;
struct tm * t;
int Jour;
int Heure;
double Valeur;
PRGHBDO *PrgHdbo;
PLAGE *Plage;
char tState[8];
char tValeur[16];
if (g_debuglevel>=DEBUG_DEBUG) Flog_Ecrire("Début de traitement des programmations.");
//Calcul Jour et heure actuel
timestamp = time(NULL);
t = localtime(×tamp);
Jour = t->tm_wday; //0->Di à 6->Sa à convertir vers 1->Lu à 7->Di
if(Jour==0) Jour = 7;
Heure = t->tm_hour*100+t->tm_min;
//Parcourir les plages
PrgHdbo = g_LstPrgHdbo;
while(PrgHdbo != NULL)
{
if (g_debuglevel>=DEBUG_VERBOSE) Flog_Ecrire("Traitement de %s.",PrgHdbo->Nom);
Plage = CherchePlage(Jour, Heure, PrgHdbo->Plages);
if(Plage!=NULL)
{
if (g_debuglevel>=DEBUG_VERBOSE) Flog_Ecrire("Une plage trouvée, valeur %f.",Plage->Valeur);
Valeur = Plage->Valeur;
}
else
{
if (g_debuglevel>=DEBUG_VERBOSE) Flog_Ecrire("Pas de plage trouvée, valeur par défaut %f.",PrgHdbo->Defaut);
Valeur = PrgHdbo->Defaut;
}
if((PrgHdbo->bInit==FALSE)||(PrgHdbo->Valeur!=Valeur))
{
PrgHdbo->bInit=TRUE;
PrgHdbo->Valeur=Valeur;
strcpy(tState, "OFF");
if(Valeur>0) strcpy(tState, "ON");
if (g_debuglevel>=DEBUG_VERBOSE) Flog_Ecrire("Fixe le capteur %s à %s (%s)",PrgHdbo->Sortie, tValeur, tState);
sprintf(tValeur, "%f", Valeur);
xpp_cmd(PrgHdbo->Sortie, tState, "", tValeur);
}
PrgHdbo = PrgHdbo->Suivant;
}
if (g_debuglevel>=DEBUG_DEBUG) Flog_Ecrire("Fin de traitement des programmations.");
return 0;
}
/****************************************************************************************************************/
/* Traitement d'un message */
int xpp_handler_service()
{
char i_temp[64];;
if (xpp_GetCmd("request:state", i_temp)!=0)
{
if (STRICMP(i_temp, "stop")==1) g_bServiceStop = TRUE;
if (STRICMP(i_temp, "start")==1) g_bServicePause = FALSE;
if (STRICMP(i_temp, "pause")==1) g_bServicePause = TRUE;
return 0;
}
if (xpp_GetCmd("request:init", i_temp)!=0)
{
if (STRICMP(i_temp, "config")==1)
{
Fcnf_Lire(FichierCnf, &g_LstParam);
ProgHebdo_LireIni();
}
return 0;
}
return 1;
}
/****************************************************************************************************************/
/* Fonctions services : start */
int ServiceStart()
{
fd_set i_rdfs;
struct timeval i_tv;
char i_xpp_buff[1500+1];
char Fichier[256];
//******************************************************************************************************************
//*** Initialisation générale
FichierInit(g_ServiceChemin, g_ServiceNom);
FichierStd(Fichier, TYPFIC_LOG);
Flog_Init(Fichier);
//******************************************************************************************************************
//*** Bavardage
Flog_Ecrire("Démarrage de %s", XAP_SOURCE);
//******************************************************************************************************************
//*** Lecture du fichier INI
FichierStd(FichierCnf, TYPFIC_CNF);
if(!Fcnf_Lire(FichierCnf, &g_LstParam))
{
FichierStd(FichierCnf, TYPFIC_LOC+TYPFIC_CNF);
Fcnf_Lire(FichierCnf, &g_LstParam);
}
Flog_Ecrire("Fichier de conf : %s", FichierCnf);
g_receiver_sockfd = ProgHebdo_init();
if (g_debuglevel>0) Flog_Ecrire("DebugLevel : %d", g_debuglevel);
//******************************************************************************************************************
//*** Lecture des plage
ProgHebdo_LireIni();
//******************************************************************************************************************
//*** Boucle principale
while(!g_bServiceStop)
{
// Send heartbeat periodically
xpp_heartbeat_tick(g_IntervalHbeat);
ProgHebdo();
FD_ZERO(&i_rdfs);
FD_SET(g_receiver_sockfd, &i_rdfs);
i_tv.tv_sec=10;
i_tv.tv_usec=0;
select(g_receiver_sockfd+1, &i_rdfs, NULL, NULL, &i_tv);
// Select either timed out, or there was data - go look for it.
if (FD_ISSET(g_receiver_sockfd, &i_rdfs))
{
// there was an incoming message, not that we care
if (xpp_PollIncoming(g_receiver_sockfd, i_xpp_buff, sizeof(i_xpp_buff))>0)
{
switch(xpp_DispatchReception(i_xpp_buff))
{
case XPP_RECEP_SERVICE_CMD :
xpp_handler_service();
break;
}
}
}
} // while
Flog_Ecrire("Arrêt de %s", XAP_SOURCE);
return 0;
} // main
/****************************************************************************************************************/
/* Fonctions services : pause */
void ServicePause(BOOL bPause)
{
g_bServicePause = bPause;
return;
}
/****************************************************************************************************************/
/* Fonctions services : stop */
void ServiceStop()
{
g_bServiceStop = TRUE;
shutdown(g_receiver_sockfd, 1);
close(g_receiver_sockfd);
return;
}