-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathqueries.php
363 lines (319 loc) · 12.6 KB
/
queries.php
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
<?php
// Conversion timestamp vs date
$date_sql = array (
"TIMESTAMP" => array (
"DATE" => "UNIX_TIMESTAMP(%field%)",
"TIMESTAMP" => "%field%"
),
"DATE" => array (
"DATE" => "%field%",
"TIMESTAMP" => "DATE(FROM_UNIXTIME(%field%))"
)
);
// Conversion timestamp vs date
function date_php ($dateIn, $typeIn, $typeOut) {
switch ($typeIn) {
case "DATE":
switch ($typeOut) {
case "DATE":
// Date vers Date
$dateOut = $dateIn;
break;
case "TIMESTAMP":
default :
// Date vers Timestamp
$datetime = new DateTime($dateIn);
$datetime->setTimezone(new DateTimeZone('Europe/Paris'));
$dateOut = $datetime->format("U"); // PHP 5.2
//$dateOut = $datetime->getTimestamp();
break;
}
break;
case "TIMESTAMP":
switch ($typeOut) {
case "TIMESTAMP":
// Timestamp vers Timestamp
$dateOut = $dateIn;
break;
case "DATE":
default :
// Timestamp vers Date
$datetime = new DateTime("@$dateIn"); // PHP 5.2
$datetime->setTimezone(new DateTimeZone('Europe/Paris'));
//$datetime->setTimestamp($dateIn);
$dateOut = $datetime->format("Y-m-d H:i:s");
break;
}
break;
default :
break;
}
return $dateOut;
}
// Retourne l'option souscrite pour la période choisie
// En cas de modification d'abonnement EDF durant la période,
// on peut avoir plusieurs options.
// Pour l'instant, ce n'est pas géré par le programme :
// on gère alors l'option courante.
function queryOPTARIF() {
global $db_connect, $config_table, $date_sql;
$mesures = array ("OPTARIF", "ISOUSC");
// Select
$query = "SELECT ";
// Mesures
foreach($mesures as $field) {
$query .= $config_table["table"][$field] . " AS " . $field . ", ";
}
// Suppression de la dernière virgule
$query = substr($query, 0, -2) . " ";
// From
$query .= "FROM " . $db_connect['table'] . " ";
// Where
$query .= str_replace(
array("%date%", "%table%"),
array($config_table["table"]["DATE"], $db_connect['table']),
"WHERE %date%=(SELECT MAX(%date%) FROM %table%)");
// $query .= "ORDER BY ".$config_table["table"]["DATE"]." DESC LIMIT 1;";
// Variante 1
// SELECT OPTARIF AS OPTARIF, ISOUSC AS ISOUSC
// FROM tbTeleinfo
// WHERE DATE=(SELECT MAX(DATE) FROM tbTeleinfo)
// Variante 2 (spécifique MySQL)
// SELECT OPTARIF AS OPTARIF, ISOUSC AS ISOUSC
// FROM tbTeleinfo
// ORDER BY DATE DESC
// LIMIT 1
return $query;
}
// Retourne l'intensité et la puissance maximales pour la période donnée
function queryMaxPeriod ($timestampdebut, $timestampfin, $optarif = null) {
global $db_connect, $config_table, $date_sql;
global $teleinfo;
$mesures = array ("PAPP");
// Intensités IINST1... IINST3
$numPhase = 1;
while (isset($config_table["table"]["IINST" . $numPhase])) {
$mesures[] = "IINST" . $numPhase;
$numPhase++;
}
$tDate = strtoupper($config_table["type_date"]);
$timestamp = str_replace ("%field%", $config_table["table"]["DATE"], $date_sql["TIMESTAMP"][$tDate]);
$date = str_replace ("%field%", $config_table["table"]["DATE"], $date_sql["DATE"][$tDate]);
// Select
$query = "SELECT ";
// Mesures
foreach($mesures as $field) {
$query .= str_replace(
array("%field%", "%mesure%"),
array($config_table["table"][$field], $field),
"MAX(%field%) as %mesure%, "); //"MAX(`%field%`) as %mesure%, ");
}
// Différents index
if ($optarif) {
foreach($teleinfo["PERIODES"][$optarif] as $field) {
$query .= str_replace(
array("%field%", "%mesure%"),
array($config_table["table"][$field], $field),
"MAX(%field%) as %mesure%, "); //"MAX(`%field%`) as %mesure%, ");
}
}
// Suppression de la dernière virgule
$query = substr($query, 0, -2) . " ";
// From
$query .= "FROM " . $db_connect['table'] . " ";
// Where
$datetimedebut = date_php($timestampdebut, "TIMESTAMP", $tDate);
$datetimefin = date_php($timestampfin, "TIMESTAMP", $tDate);
//$datefield = str_replace ("%field%", $config_table["table"]["DATE"], $date_sql["DATE"][$tDate]);
$datefield = $config_table["table"]["DATE"];
$query .= str_replace(
array("%date%", "%debut%", "%fin%"),
array($datefield, $datetimedebut, $datetimefin),
"WHERE %date% BETWEEN '%debut%' and '%fin%' ");
// Order By
$query .= "ORDER BY " . $config_table["table"]["DATE"];
// SELECT MAX(PAPP) as PAPP, MAX(IINST1) as IINST1, MAX(BASE) as BASE
// FROM tbTeleinfo
// WHERE DATE BETWEEN 'Y-m-d H:i:s' and 'Y-m-d H:i:s'
// ORDER BY DATE'
// SELECT MAX(`PAPP`) as PAPP, MAX(`IINST1`) as IINST1,
// FROM tbTeleTempo
// WHERE UNIX_TIMESTAMP(DATE) BETWEEN xxxx and yyyy
// ORDER BY DATE
return $query;
}
function queryMaxDate () {
global $db_connect, $config_table, $date_sql;
$mesures = array ("PAPP");
// Intensités IINST1... IINST3
$numPhase = 1;
while (isset($config_table["table"]["IINST" . $numPhase])) {
$mesures[] = "IINST" . $numPhase;
$numPhase++;
}
$tDate = strtoupper($config_table["type_date"]);
$timestamp = str_replace ("%field%", $config_table["table"]["DATE"], $date_sql["TIMESTAMP"][$tDate]);
// Select
$query = "SELECT ";
// Max
$query .= str_replace(
"%date%", $timestamp,
"MAX(%date%) AS DATE ");
// From
$query .= "FROM " . $db_connect['table'] . " ";
// SELECT MAX(UNIX_TIMESTAMP(DATE)) AS DATE
// FROM tbTeleinfo
// SELECT MAX(`DATE`) as DATE
// FROM tbTeleTempo
return $query;
}
function queryInstantly () {
global $db_connect, $config_table, $date_sql;
$mesures = array ("PAPP");
// Intensités IINST1... IINST3
$numPhase = 1;
while (isset($config_table["table"]["IINST" . $numPhase])) {
$mesures[] = "IINST" . $numPhase;
$numPhase++;
}
$mesures = array_merge($mesures, array("ISOUSC", "OPTARIF", "PTEC", "DEMAIN"));
$tDate = strtoupper($config_table["type_date"]);
$timestamp = str_replace ("%field%", $config_table["table"]["DATE"], $date_sql["TIMESTAMP"][$tDate]);
// Select Timestamp
$query = "SELECT " . $timestamp . " AS TIMESTAMP, ";
// Mesures
foreach($mesures as $field) {
$query .= $config_table["table"][$field] . " AS " . $field . ", ";
}
// Suppression de la dernière virgule
$query = substr($query, 0, -2) . " ";
// From
$query .= "FROM " . $db_connect['table'] . " ";
// Where
$query .= str_replace(
array("%date%", "%table%"),
array($config_table["table"]["DATE"], $db_connect['table']),
"WHERE %date%=(SELECT MAX(%date%) FROM %table%)");
// SELECT UNIX_TIMESTAMP(DATE) AS TIMESTAMP,
// PAPP AS PAPP, IINST1 AS IINST1, ISOUSC AS ISOUSC,
// OPTARIF AS OPTARIF, PTEC AS PTEC, DEMAIN AS DEMAIN
// FROM tbTeleinfo
// WHERE DATE=(SELECT MAX(DATE) FROM tbTeleinfo)"
return $query;
}
function queryDaily ($timestampdebut, $timestampfin, $optarif = null) {
global $db_connect, $config_table, $date_sql;
global $teleinfo;
$mesures = array ("PAPP");
// Intensités IINST1... IINST3
$numPhase = 1;
while (isset($config_table["table"]["IINST" . $numPhase])) {
$mesures[] = "IINST" . $numPhase;
$numPhase++;
}
$mesures = array_merge($mesures, array("OPTARIF", "PTEC"));
$tDate = strtoupper($config_table["type_date"]);
$timestamp = str_replace ("%field%", $config_table["table"]["DATE"], $date_sql["TIMESTAMP"][$tDate]);
// Select Timestamp
$query = "SELECT " . $timestamp . " AS TIMESTAMP, ";
// Mesures
foreach($mesures as $field) {
$query .= $config_table["table"][$field] . " AS " . $field . ", ";
}
// Différents index
if ($optarif) { // if ($config["recalculPuissance"]) {
foreach($teleinfo["PERIODES"][$optarif] as $field){
$query .= $config_table["table"][$field] . " AS " . $field . ", ";
}
}
// Suppression de la dernière virgule
$query = substr($query, 0, -2) . " ";
// From
$query .= "FROM " . $db_connect['table'] . " ";
// Where
$datetimedebut = date_php($timestampdebut, "TIMESTAMP", $tDate);
$datetimefin = date_php($timestampfin, "TIMESTAMP", $tDate);
//$datefield = str_replace ("%field%", $config_table["table"]["DATE"], $date_sql["DATE"][$tDate]);
$datefield = $config_table["table"]["DATE"];
$query .= str_replace(
array("%date%", "%debut%", "%fin%"),
array($datefield, $datetimedebut, $datetimefin),
"WHERE %date% BETWEEN '%debut%' and '%fin%' ");
// Order By
$query .= "ORDER BY " . $config_table["table"]["DATE"];
// SELECT UNIX_TIMESTAMP(DATE) AS TIMESTAMP,
// OPTARIF AS OPTARIF, PTEC AS PTEC, IINST1 AS IINST1, PAPP AS PAPP
// FROM tbTeleinfo
// WHERE DATE BETWEEN 'Y-m-d H:i:s' and 'Y-m-d H:i:s'
// ORDER BY DATE'
// SELECT UNIX_TIMESTAMP(DATE) as TIMESTAMP,
// OPTARIF AS OPTARIF, PTEC AS PTEC, IINST1 AS IINST1, PAPP AS PAPP
// FROM tbTeleinfo
// WHERE UNIX_TIMESTAMP(DATE) BETWEEN xxxx and yyyy
// ORDER BY DATE
return $query;
}
function queryHistory ($timestampdebut, $timestampfin, $dateformatsql, $optarif) {
global $db_connect, $config_table, $date_sql;
global $teleinfo;
$mesures = array ("OPTARIF", "PTEC");
$tDate = strtoupper($config_table["type_date"]);
$timestamp = str_replace ("%field%", $config_table["table"]["DATE"], $date_sql["TIMESTAMP"][$tDate]);
$date = str_replace ("%field%", $config_table["table"]["DATE"], $date_sql["DATE"][$tDate]);
// Select Timestamp
$query = "SELECT " . $timestamp . " AS TIMESTAMP, ";
// Période
$query .= str_replace(
array("%field%", "%format%"),
array($date, $dateformatsql),
"DATE_FORMAT(%field%, '%format%') AS PERIODE, ");
// Mesures
foreach($mesures as $field) {
$query .= $config_table["table"][$field] . " AS " . $field . ", ";
}
// Max-Min
foreach($teleinfo["PERIODES"][$optarif] as $field) {
$query .= str_replace(
array("%field%", "%mesure%"),
array($config_table["table"][$field], $field),
"ROUND(((MAX(NULLIF(%field%, 0)) - MIN(NULLIF(%field%, 0))) / 1000), 1) AS %mesure%, "); //"ROUND(((MAX(`%field%`) - MIN(`%field%`)) / 1000), 1) AS %mesure%, ");
}
// Suppression de la dernière virgule
$query = substr($query, 0, -2) . " ";
// From
$query .= "FROM " . $db_connect['table'] . " ";
// Where
$datetimedebut = date_php($timestampdebut, "TIMESTAMP", $tDate);
$datetimefin = date_php($timestampfin, "TIMESTAMP", $tDate);
//$datefield = str_replace ("%field%", $config_table["table"]["DATE"], $date_sql["DATE"][$tDate]);
$datefield = $config_table["table"]["DATE"];
$query .= str_replace(
array("%date%", "%debut%", "%fin%"),
array($datefield, $datetimedebut, $datetimefin),
"WHERE %date% BETWEEN '%debut%' and '%fin%' ");
// Group By
$query .= "GROUP BY PERIODE ";
// Order By
$query .= "ORDER BY " . $config_table["table"]["DATE"];
// SELECT UNIX_TIMESTAMP(DATE) AS TIMESTAMP, DATE_FORMAT(DATE, '%a %e') AS PERIODE,
// OPTARIF AS OPTARIF, PTEC AS PTEC,
// ROUND(((MAX(NULLIF(BASE, 0)) - MIN(NULLIF(BASE, 0))) / 1000), 1) AS BASE
// FROM tbTeleinfo
// WHERE UNIX_TIMESTAMP(DATE) BETWEEN 1485471600 and 1486854000
// GROUP BY PERIODE
// ORDER BY DATE
// SELECT UNIX_TIMESTAMP(DATE) AS TIMESTAMP, DATE_FORMAT(DATE, '%a %e') AS PERIODE,
// OPTARIF AS OPTARIF, PTEC AS PTEC,
// ROUND(((MAX(`BBRHPJB`) - MIN(`BBRHPJB`)) / 1000), 1) AS HPJB,
// ROUND(((MAX(`BBRHPJW`) - MIN(`BBRHPJW`)) / 1000), 1) AS HPJW,
// ROUND(((MAX(`BBRHPJR`) - MIN(`BBRHPJR`)) / 1000), 1) AS HPJR,
// ROUND(((MAX(`BBRHCJB`) - MIN(`BBRHCJB`)) / 1000), 1) AS HCJB,
// ROUND(((MAX(`BBRHCJW`) - MIN(`BBRHCJW`)) / 1000), 1) AS HCJW,
// ROUND(((MAX(`BBRHCJR`) - MIN(`BBRHCJR`)) / 1000), 1) AS HCJR
// FROM tbTeleTempo
// WHERE UNIX_TIMESTAMP(DATE) BETWEEN xxxx and yyyy
// GROUP BY PERIODE
// ORDER BY DATE
return $query;
}
?>