-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathActions.cpp
More file actions
277 lines (262 loc) · 9.14 KB
/
Actions.cpp
File metadata and controls
277 lines (262 loc) · 9.14 KB
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
// ********************
// Gestion des Actions
// ********************
#include <Arduino.h>
#include "Actions.h"
#include "EEPROM.h"
#include <WiFiClient.h>
//Class Action
Action::Action() {
Gpio = -1;
}
Action::Action(int aIdx) {
Gpio = -1; // si le n° de pin n'est pas valid, on ne fait rien
Idx = aIdx;
T_LastAction = int(millis() / 1000);
On = false;
Actif = 0; //0=Inactif,1=Decoupe ou On/Off, 2=Multi, 3= Train , 4=PWM
Kp = 10;
Ki = 10;
Kd = 10;
PID = false;
OutOn = 1;
OutOff = 0;
ForceOuvre = 100;
Tempo = 0;
Repet = 0;
tOnOff = 0;
H_Ouvre = 0;
ExtSelAct = 255;
ExtValide = 0; //Condition Action externe
ExtHequiv = 0; //Duree heure *100 action externe
ExtOuvert = 0; //Pourcent ouverture
for (int i = 0; i < 8; i++) {
Type[i] = 0; //0=NO(pas utilisé),1=OFF,2=ON,3=PW,4=Triac
Hdeb[i] = 0;
Hfin[i] = 0;
Vmin[i] = 0; //Seuil Pw On ou decoupe
Vmax[i] = 0; //Seuil Pw Off ou ouverture max triac
ONouvre[i] = 100;
Tinf[i] = -1600; //Temperarure * 10
Tsup[i] = -1600;
Hmin[i] = 0; //Heure deci *100 Min pour actif. 0=non utilisé
Hmax[i] = 0; //Heure deci *100 Max pour actif
CanalTemp[i] = -1;
SelAct[i] = 255; //Ref ESP/Action. 255=pas exploité
Ooff[i] = 0; //Ouvre Min Action pour Actif. 0 non utilisé
O_on[i] = 0;
Tarif[i] = 0;
}
}
void Action::Arreter() {
int Tseconde = int(millis() / 1000);
if ((Tseconde - T_LastAction) >= Tempo || Idx == 0 || Actif != 1) {
if (Gpio > 0) {
if (Actif == 4) { //PWM
ledcWrite(Gpio, OutOff * 255);
} else {
digitalWrite(Gpio, OutOff);
}
T_LastAction = Tseconde;
} else {
if (On || ((Tseconde - T_LastAction) > Repet && Repet != 0)) {
if (Actif > 0) CallExterne(Host, OrdreOff, Port);
T_LastAction = Tseconde;
}
}
On = false;
}
}
void Action::RelaisOn() {
int Tseconde = int(millis() / 1000);
if ((Tseconde - T_LastAction) >= Tempo) {
if (Gpio > 0) {
if (Actif == 4) { //PWM
ledcWrite(Gpio, OutOn * 255);
} else {
digitalWrite(Gpio, OutOn);
}
T_LastAction = Tseconde;
On = true;
} else {
if (Actif == 1) {
if (!On || ((Tseconde - T_LastAction) > Repet && Repet != 0)) {
CallExterne(Host, OrdreOn, Port);
T_LastAction = Tseconde;
}
On = true;
}
}
}
}
void Action::Prioritaire() {
int tempo_ = Tempo;
if (tOnOff != 0) {
Tempo = 0;
if (tOnOff > 0) {
RelaisOn();
} else {
Arreter();
}
Tempo = tempo_;
}
}
int16_t Action::CanalTempEnCours(int Heure) {
int16_t CanalEnCours = -1;
for (int i = 0; i < NbPeriode; i++) {
if (Heure >= Hdeb[i] && Heure <= Hfin[i]) {
CanalEnCours = CanalTemp[i];
}
}
return CanalEnCours;
}
Action::ParaPeriode Action::ParaEnCours(int Heure, float Temperature, int Ltarfbin, int Retard) { //Retourne type d'action active à cette heure , test temperature OK et seuils
ParaPeriode P;
P.Type = 1; //Off
int16_t Tempx10 = int(Temperature * 10.0); //Température en dixième de degré //Equivalent à Action Off
bool ConditionsOk;
for (int i = 0; i < NbPeriode; i++) {
if (Heure >= Hdeb[i] && Heure <= Hfin[i]) {
ConditionsOk = true;
if (Temperature > -100.0) {
if (Tinf[i] < 1500 && Tsup[i] < 1500 && Tinf[i] < Tsup[i]) { // on applique un hystérésis dont les valeurs sont Tinf et Tsup
if (Tempx10 > Tinf[i] && Tempx10 > Tsup[i]) Tseuil = Tinf[i];
if (Tempx10 < Tinf[i] && Tempx10 < Tsup[i]) Tseuil = Tsup[i];
if (Tempx10 > Tseuil) { ConditionsOk = false; }
} else {
if (Tinf[i] <= 1000 && Tempx10 > Tinf[i]) { ConditionsOk = false; }
if (Tsup[i] <= 1000 && Tempx10 < Tsup[i]) { ConditionsOk = false; }
}
}
if (Ltarfbin > 0 && (Ltarfbin & Tarif[i]) == 0) ConditionsOk = false;
if (SelAct[i] != 255) { //On conditionne à une autre action
if (Hmin[i] != 0 && (Hmin[i] > ExtHequiv || ExtValide == 0)) ConditionsOk = false;
if (Hmax[i] != 0 && (Hmax[i] < ExtHequiv || ExtValide == 0)) ConditionsOk = false;
if (Ooff[i] != 0 && ((int(Ooff[i]) >= ExtOuvert && Retard != 100) || ExtValide == 0)) ConditionsOk = false; //Inferieur au seuil bas
if (O_on[i] != 0 && ((int(O_on[i]) > ExtOuvert && Retard == 100) || ExtValide == 0)) ConditionsOk = false; //Inferieur au seuil haut et pas encore ouvert
}
if (ConditionsOk) P.Type = Type[i];
P.Vmin = Vmin[i];
P.Vmax = Vmax[i];
if (P.Type==2) P.Vmax=ONouvre[i] ;
}
}
if (tOnOff > 0) {
P.Type = 2; // Force On prioritaire
P.Vmax = ForceOuvre; //Ouverture Force prioritaire
}
if (tOnOff < 0) P.Type = 1; // Force Off
return P; //0=NO (pas utilisé),1=OFF,2=ON,3=PW,4=Triac
}
byte Action::TypeEnCours(int Heure, float Temperature, int Ltarfbin, int Retard) { //Retourne type d'action active à cette heure et test temperature OK
byte S = 1; //Off
int16_t Tempx10 = int(Temperature * 10.0); //Température en dixième de degré //Equivalent à Action Off
bool ConditionsOk;
for (int i = 0; i < NbPeriode; i++) {
if (Heure >= Hdeb[i] && Heure <= Hfin[i]) {
ConditionsOk = true;
if (Temperature > -100.0) {
if (Tinf[i] < 1500 && Tsup[i] < 1500 && Tinf[i] < Tsup[i]) { // on applique un hystérésis dont les valeurs sont Tinf et Tsup
if (Tempx10 > Tinf[i] && Tempx10 > Tsup[i]) Tseuil = Tinf[i];
if (Tempx10 < Tinf[i] && Tempx10 < Tsup[i]) Tseuil = Tsup[i];
if (Tempx10 > Tseuil) { ConditionsOk = false; }
} else {
if (Tinf[i] <= 1000 && Tempx10 > Tinf[i]) { ConditionsOk = false; }
if (Tsup[i] <= 1000 && Tempx10 < Tsup[i]) { ConditionsOk = false; }
}
}
if (Ltarfbin > 0 && (Ltarfbin & Tarif[i]) == 0) ConditionsOk = false;
if (SelAct[i] != 255) { //On conditionne à une autre action
if (Hmin[i] != 0 && (Hmin[i] > ExtHequiv || ExtValide == 0)) ConditionsOk = false;
if (Hmax[i] != 0 && (Hmax[i] < ExtHequiv || ExtValide == 0)) ConditionsOk = false;
if (Ooff[i] != 0 && ((int(Ooff[i]) >= ExtOuvert && Retard != 100) || ExtValide == 0)) ConditionsOk = false; //Inferieur au seuil bas
if (O_on[i] != 0 && ((int(O_on[i]) > ExtOuvert && Retard == 100) || ExtValide == 0)) ConditionsOk = false; //Inferieur au seuil haut et pas encore ouvert
}
if (ConditionsOk) S = Type[i];
}
}
if (tOnOff > 0) S = 2; // Force On prioritaire
if (tOnOff < 0) S = 1; // Force Off
return S; //0=NO (pas utilisé),1=OFF,2=ON,3=PW,4=Triac
}
byte Action::SelActEnCours(int Heure) {
int S = 255;
for (int i = 0; i < NbPeriode; i++) {
if (Heure >= Hdeb[i] && Heure <= Hfin[i]) {
S = SelAct[i];
}
}
return S;
}
int Action::Valmin(int Heure) { //Retourne la valeur Vmin (ex seuil Triac) à cette heure
int S = 0;
for (int i = 0; i < NbPeriode; i++) {
if (Heure >= Hdeb[i] && Heure <= Hfin[i]) {
S = Vmin[i];
}
}
return S;
}
int Action::Valmax(int Heure) { //Retourne la valeur Vmax (ex ouverture du Triac) à cette heure
int S = 0;
for (int i = 0; i < NbPeriode; i++) {
if (Heure >= Hdeb[i] && Heure <= Hfin[i]) {
S = Vmax[i];
}
}
return S;
}
void Action::InitGpio(int FreqPWM) { //Initialise les sorties GPIO pour des relais
int p;
String S;
String IS = "|"; //Input Separator
if (Idx > 0) {
T_LastAction = 0;
Gpio = -1;
p = OrdreOn.indexOf(IS);
if (p >= 0) {
Gpio = OrdreOn.substring(0, p).toInt();
OutOn = OrdreOn.substring(p + 1).toInt();
OutOff = (1 + OutOn) % 2;
if (Gpio > 0) {
if (Actif == 4) { //PWM
ledcAttachChannel(Gpio, FreqPWM, 8, Idx); //Affectation des channels
} else {
pinMode(Gpio, OUTPUT);
digitalWrite(Gpio, OutOff);
}
}
}
}
}
void Action::CallExterne(String host, String url, int port) {
if (url != "") {
// Use WiFiClient class to create TCP connections
WiFiClient clientExt;
char hostbuf[host.length() + 1];
host.toCharArray(hostbuf, host.length() + 1);
if (!clientExt.connect(hostbuf, port, 3000)) {
clientExt.stop();
delay(500);
if (!clientExt.connect(hostbuf, port, 3000)) {
delay(100); //Necessaire
StockMessage("connection to :" + host + " failed");
delay(100);
return;
}
}
clientExt.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (clientExt.available() == 0) {
if (millis() - timeout > 5000) {
StockMessage(">>> clientESP_Ext Timeout ! : " + host);
clientExt.stop();
return;
}
}
// Read all the lines of the reply from server
while (clientExt.available()) {
String line = clientExt.readStringUntil('\r');
}
}
}