forked from AssociationInfoclimat/StatIC-WeeWX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatic.php
807 lines (709 loc) · 22.7 KB
/
static.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
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
<?php
/*
*
* Script permettant de générer un fichier texte pour le réseau StatIC de l'association Infoclimat
* pour une intégration à leur réseau d'une station météo (Davis VP2) fonctionnant sous le logiciel
* WEEWX sur une base de données SQLite ou MySQL.
*
* Le sript trouve automatiquement le système d'unité utilisé dans la BDD, et convertit les valeurs si
* nécéssaires
*
* Repris modifié et adapté pour l'association Infoclimat
* https://www.infoclimat.fr/stations/static.php
*
* Le détail du fonctionnement de ce script se trouve sur GitHub : https://github.com/AssociationInfoclimat/StatIC-WeeWX
*
*/
require_once "config.php";
// VERSION - NE PAS MODIFIER !!
$version = "weewx-".$db_type."-2.5";
// Fuseau horaire du script
$datetimeNow = date('Y-m-d H:i:s');
date_default_timezone_set($timezone);
// DEBUG
$options = getopt(null, array(
'debug:',
));
$debug = $options['debug'];
// START SQLITE
if ($db_type === "sqlite") {
// Connection à la BDD SQLite WeeWX
$db_handle = new SQLite3($db_file);
$query_string = "SELECT * FROM $db_table_sqlite ORDER BY dateTime DESC LIMIT 1;";
$db_handle->exec($query_string);
$result = $db_handle->query($query_string);
$row = $result->fetchArray(SQLITE3_ASSOC);
// Détermination du système d'unité utilisé dans la BDD
// 1 = US ; 16 = METRIC ; 17 = METRICWX
$unit=$row['usUnits'];
if ($debug=="true") {
echo "Version script : ".$version.PHP_EOL;
echo "Unite BDD : ".$unit.PHP_EOL.PHP_EOL;
}
// Établissement des timestamp stop et start
$stop=$row['dateTime'];
$minutes10=$stop-(600);
$minutes60=$stop-(3600);
// Établissement de la date et de l'heure du dernier relevé dispo en BDD
$date=gmdate('d/m/Y',$stop);
$heure=gmdate('H\hi',$stop);
// DEBUG heure
if ($debug=="true") {
echo "Timezone : ".$timezone.PHP_EOL;
$datetimeNowTimezone = date('Y-m-d H:i:s');
echo "Datetime : ".$datetimeNow.PHP_EOL;
echo "Datetime Timezone : ".$datetimeNowTimezone.PHP_EOL;
$timestampNowTimezone = time();
echo "TS Timezone : ".$timestampNowTimezone.PHP_EOL.PHP_EOL;
echo "TS WeeWX BDD STOP : ".$stop.PHP_EOL;
echo "Heure (gmdate) WeeWX BDD : ".$heure.PHP_EOL;
}
/*
*
* Mise en forme et calcul des paramètres temps réel
*
*/
// temperature
if ($row['outTemp'] === null){
$temp = '';
}else{
if ($unit=='1') {
$temp = round(($row['outTemp']-32)/1.8,1);
}elseif ($unit=='16' OR $unit=='17') {
$temp = round($row['outTemp'],1);
}
}
// pression
if ($row['barometer'] === null){
$pression = '';
}else{
if ($unit=='1') {
$pression = round($row['barometer']*33.8639,1);
}elseif ($unit=='16' OR $unit=='17') {
$pression = round($row['barometer'],1);
}
}
// hygro
if ($row['outHumidity'] === null){
$hygro = '';
}else{
$hygro = round($row['outHumidity'],1);
}
// dewpoint
if($row['dewpoint'] === null){
$dewpoint = '';
}else{
if ($unit=='1') {
$dewpoint = round(($row['dewpoint']-32)/1.8,1);
}elseif ($unit=='16' OR $unit=='17') {
$dewpoint = round($row['dewpoint'],1);
}
}
// intensite pluie
if ($row['rainRate'] === null){
$rainrate = '';
}else{
if ($unit=='1') {
$rainrate = round($row['rainRate']*25.4,1);
}elseif ($unit=='16') {
$rainrate = round($row['rainRate']*10,1);
}elseif ($unit=='17') {
$rainrate = round($row['rainRate'],1);
}
}
// rayonnement solaire
if ($row['radiation'] === null){
$solar = '';
}else{
$solar = round($row['radiation'],0);
}
// rayonnement UV
if($row['UV'] === null){
$uv = '';
}else{
$uv = round($row['UV'],1);
}
/*
* Calcul des moyennes pour le vent sur 10 minutes
*/
// Récupération et calcul de la vitesse moyenne du vent moyen des 10 dernières minutes
$sql = "SELECT AVG(windSpeed) FROM $db_table_sqlite WHERE dateTime >= '$minutes10' AND dateTime < '$stop';";
$res = $db_handle->query($sql);
$row = $res->fetchArray();
if($row[0] === null){
$avg_wind_10 = '';
}else{
$avg_wind_10 = round($row[0],1);
if ($unit=='1') {
$avg_wind_10 = round($row[0]*1.60934,1);
}elseif ($unit=='16') {
$avg_wind_10 = round($row[0],1);
}elseif ($unit=='17') {
$avg_wind_10 = round($row[0]*3.6,1);
}
}
// Récupération et calcul de la moyenne de la direction du vent sur 10 minutes
// Dans un premier temps, on définit une fonction qui permettra de calculer
// la moyenne de plusieurs angles en degrées
function mean_of_angles( $angles, $degrees = true ) {
if ( $degrees ) {
$angles = array_map("deg2rad", $angles); // Convert to radians
}
$s_ = 0;
$c_ = 0;
$len = count( $angles );
for ($i = 0; $i < $len; $i++) {
$s_ += sin( $angles[$i] );
$c_ += cos( $angles[$i] );
}
// $s_ /= $len;
// $c_ /= $len;
$mean = atan2( $s_, $c_ );
if ( $degrees ) {
$mean = rad2deg( $mean ); // Convert to degrees
}
if ($mean < 0) {
$mean_ok = $mean + 360;
} else {
$mean_ok = $mean;
}
return $mean_ok;
}
// Récupération des valeurs de direction du vent moyen des 10 dernières minutes
// Requete + mise en tableau de la réponse
$windDirArray = array();
$res = $db_handle->query("SELECT windDir FROM $db_table_sqlite WHERE dateTime >= '$minutes10' AND dateTime < '$stop'");
while ($row=$res->fetchArray(SQLITE3_ASSOC)) {
if (!is_null ($row['windDir'])) {
$windDirArray[] = $row['windDir'];
}
}
// Calcul de la moyenne avec la fonction `mean_of_angles` et le tableau
$avg_windDir_10_check = mean_of_angles($windDirArray);
// Vérification du résultat et arrondi
$avg_windDir_10 = '';
if (!is_null($avg_windDir_10_check)) {
if ($avg_wind_10 != 0) { // Si le vent moyen n'est pas égal à 0
$avg_windDir_10 = round($avg_windDir_10_check,1);
}
}
// Détermination de la rafale max des 10 dernières minutes
$sql = "SELECT max(windGust) FROM $db_table_sqlite WHERE dateTime >= '$minutes10' AND dateTime < '$stop';";
$res = $db_handle->query($sql);
$row = $res->fetchArray();
$max_windGust_10_check = $row[0];
if($max_windGust_10_check === null){
$max_windGust_10 = '';
}else{
if ($unit=='1') {
$max_windGust_10 = round($max_windGust_10_check*1.60934,1);
}elseif ($unit=='16') {
$max_windGust_10 = round($max_windGust_10_check,1);
}elseif ($unit=='17') {
$max_windGust_10 = round($max_windGust_10_check*3.6,1);
}
}
/*
* Intensité de pluie sur l'heure
*/
// Intensite pluie max sur 1 heure
$sql = "SELECT max(rainRate) FROM $db_table_sqlite WHERE dateTime >= '$minutes60' AND dateTime < '$stop';";
$res = $db_handle->query($sql);
$row = $res->fetchArray();
$max_rainRate_hour_check = $row[0];
if($max_rainRate_hour_check === null){
$max_rainRate_hour = '';
}else{
if ($unit=='1') {
$max_rainRate_hour = round($max_rainRate_hour_check*25.4,1);
}elseif ($unit=='16') {
$max_rainRate_hour = round($max_rainRate_hour_check*10,1);
}elseif ($unit=='17') {
$max_rainRate_hour = round($max_rainRate_hour_check,1);
}
}
/*
*
* Mise en forme et calcul des paramètres temps passé
*
*/
// Cumul pluie sur l'heure glissante
$sql = "SELECT sum(rain) FROM $db_table_sqlite WHERE dateTime >= '$minutes60' AND dateTime < '$stop';";
$res = $db_handle->query($sql);
$row = $res->fetchArray();
$cumul_rain_hour_check = $row[0];
if($cumul_rain_hour_check === null){
$cumul_rain_hour = '';
}else{
if ($unit=='1') {
$cumul_rain_hour = round($cumul_rain_hour_check*25.4,1);
}elseif ($unit=='16') {
$cumul_rain_hour = round($cumul_rain_hour_check*10,1);
}elseif ($unit=='17') {
$cumul_rain_hour = round($cumul_rain_hour_check,1);
}
}
// Cumul pluie de la journée
$today = strtotime('today midnight');
$sql = "SELECT sum(rain) FROM $db_table_sqlite WHERE dateTime >= '$today' AND dateTime < '$stop';";
$res = $db_handle->query($sql);
$row = $res->fetchArray();
$cumul_rain_today_check = $row[0];
if($cumul_rain_today_check === null){
$cumul_rain_today = '';
}else{
if ($unit=='1') {
$cumul_rain_today = round($cumul_rain_today_check*25.4,1);
}elseif ($unit=='16') {
$cumul_rain_today = round($cumul_rain_today_check*10,1);
}elseif ($unit=='17') {
$cumul_rain_today = round($cumul_rain_today_check,1);
}
}
// DEBUG today midnight
if ($debug=="true") {
$dateTimeToday = date('d-m-Y H:i:s',$today);
echo PHP_EOL;
echo "Datetime Today midnight : ".$dateTimeToday.PHP_EOL;
echo "TS Today midnight : ".$today.PHP_EOL;
}
// Cumul pluie de l'année
$yearNow = date('Y');
$startYear = strtotime('01-01-'.$yearNow);
$sql = "SELECT sum(rain) FROM $db_table_sqlite WHERE dateTime >= '$startYear' AND dateTime < '$stop';";
$res = $db_handle->query($sql);
$row = $res->fetchArray();
$cumul_rain_year_check = $row[0];
if($cumul_rain_year_check === null){
$cumul_rain_year = '';
}else{
if ($unit=='1') {
$cumul_rain_year = round($cumul_rain_year_check*25.4,1);
}elseif ($unit=='16') {
$cumul_rain_year = round($cumul_rain_year_check*10,1);
}elseif ($unit=='17') {
$cumul_rain_year = round($cumul_rain_year_check,1);
}
}
// Intensite pluie max de la journée
$sql = "SELECT dateTime, rainRate FROM $db_table_sqlite WHERE dateTime >= '$today' AND dateTime < '$stop' AND rainRate = (SELECT MAX(rainRate) FROM $db_table_sqlite WHERE dateTime >= '$today' AND dateTime < '$stop');";
$res = $db_handle->query($sql);
$row = $res->fetchArray();
$max_rainRate_today_check = $row[1];
if($max_rainRate_today_check === null){
$max_rainRate_today = '';
$max_rainRate_todayTime = '';
}else{
if ($unit=='1') {
$max_rainRate_today = round($max_rainRate_today_check*25.4,1);
}elseif ($unit=='16') {
$max_rainRate_today = round($max_rainRate_today_check*10,1);
}elseif ($unit=='17') {
$max_rainRate_today = round($max_rainRate_today_check,1);
}
$max_rainRate_todayTime = gmdate('H\hi',$row[0]);
}
// Max temp de la journée (fausse Tx puisque de 0h à 24h)
$sql = "SELECT dateTime, outTemp FROM $db_table_sqlite WHERE dateTime >= '$today' AND dateTime < '$stop' AND outTemp = (SELECT MAX(outTemp) FROM $db_table_sqlite WHERE dateTime >= '$today' AND dateTime < '$stop');";
$res = $db_handle->query($sql);
$row = $res->fetchArray();
$max_temp_today_check = $row[1];
if($max_temp_today_check === null){
$max_temp_today = '';
$max_temp_todayTime = '';
}else{
if ($unit=='1') {
$max_temp_today = round(($max_temp_today_check-32)/1.8,1);
}elseif ($unit=='16' OR $unit=='17') {
$max_temp_today = round($max_temp_today_check,1);
}
$max_temp_todayTime = gmdate('H\hi',$row[0]);
}
// Min temp de la journée (fausse Tn puisque de 0h à 24h)
$sql = "SELECT dateTime, outTemp FROM $db_table_sqlite WHERE dateTime >= '$today' AND dateTime < '$stop' AND outTemp = (SELECT MIN(outTemp) FROM $db_table_sqlite WHERE dateTime >= '$today' AND dateTime < '$stop');";
$res = $db_handle->query($sql);
$row = $res->fetchArray();
$min_temp_today_check = $row[1];
if($min_temp_today_check === null){
$min_temp_today = '';
$min_temp_todayTime = '';
}else{
if ($unit=='1') {
$min_temp_today = round(($min_temp_today_check-32)/1.8,1);
}elseif ($unit=='16' OR $unit=='17') {
$min_temp_today = round($min_temp_today_check,1);
}
$min_temp_todayTime = gmdate('H\hi',$row[0]);
}
}
// END SQLITE
// START MYSQL
elseif ($db_type === "mysql") {
// Connection à la BDD WeeWX
$con = mysqli_connect($db_host,$db_user,$db_pass,$db_name);
// Récupération du dernier relevé dispo en BDD
$sql = "SELECT * FROM $db_name.$db_table_mysql ORDER BY dateTime DESC LIMIT 1;";
$res= $con->query($sql);
$row = mysqli_fetch_assoc($res);
// Détermination du système d'unité utilisé dans la BDD
// 1 = US ; 16 = METRIC ; 17 = METRICWX
$unit=$row['usUnits'];
if ($debug=="true") {
echo "Version script : ".$version.PHP_EOL;
echo "Unite BDD : ".$unit.PHP_EOL.PHP_EOL;
}
// Établissement des timestamp stop et start
$stop=$row['dateTime'];
$minutes10=$stop-(600);
$minutes60=$stop-(3600);
// Établissement de la date et de l'heure du dernier relevé dispo en BDD
$date=date('d/m/Y',$stop);
$heure=date('H\hi',$stop);
// DEBUG heure
if ($debug=="true") {
echo "Timezone : ".$timezone.PHP_EOL;
$datetimeNowTimezone = date('Y-m-d H:i:s');
echo "Datetime : ".$datetimeNow.PHP_EOL;
echo "Datetime Timezone : ".$datetimeNowTimezone.PHP_EOL;
$timestampNowTimezone = time();
echo "TS Timezone : ".$timestampNowTimezone.PHP_EOL.PHP_EOL;
echo "TS WeeWX BDD STOP : ".$stop.PHP_EOL;
echo "Heure (gmdate) WeeWX BDD : ".$heure.PHP_EOL;
}
/*
*
* Mise en forme et calcul des paramètres temps réel
*
*/
// temperature
if ($row['outTemp'] === null){
$temp = '';
}else{
if ($unit=='1') {
$temp = round(($row['outTemp']-32)/1.8,1);
}elseif ($unit=='16' OR $unit=='17') {
$temp = round($row['outTemp'],1);
}
}
// pression
if ($row['barometer'] === null){
$pression = '';
}else{
if ($unit=='1') {
$pression = round($row['barometer']*33.8639,1);
}elseif ($unit=='16' OR $unit=='17') {
$pression = round($row['barometer'],1);
}
}
// hygro
if ($row['outHumidity'] === null){
$hygro = '';
}else{
$hygro = round($row['outHumidity'],1);
}
// dewpoint
if($row['dewpoint'] === null){
$dewpoint = '';
}else{
if ($unit=='1') {
$dewpoint = round(($row['dewpoint']-32)/1.8,1);
}elseif ($unit=='16' OR $unit=='17') {
$dewpoint = round($row['dewpoint'],1);
}
}
// intensite pluie
if ($row['rainRate'] === null){
$rainrate = '';
}else{
if ($unit=='1') {
$rainrate = round($row['rainRate']*25.4,1);
}elseif ($unit=='16') {
$rainrate = round($row['rainRate']*10,1);
}elseif ($unit=='17') {
$rainrate = round($row['rainRate'],1);
}
}
// rayonnement solaire
if ($row['radiation'] === null){
$solar = '';
}else{
$solar = round($row['radiation'],0);
}
// rayonnement UV
if($row['UV'] === null){
$uv = '';
}else{
$uv = round($row['UV'],1);
}
// Récupération et calcul de la vitesse moyenne du vent moyen des 10 dernières minutes
$sql = "SELECT AVG(windSpeed) FROM $db_name.$db_table_mysql WHERE dateTime >= '$minutes10' AND dateTime < '$stop';";
$res = $con->query($sql);
$row = mysqli_fetch_row($res);
if($row[0] === null){
$avg_wind_10 = '';
}else{
$avg_wind_10 = round($row[0],1);
if ($unit=='1') {
$avg_wind_10 = round($row[0]*1.60934,1);
}elseif ($unit=='16') {
$avg_wind_10 = round($row[0],1);
}elseif ($unit=='17') {
$avg_wind_10 = round($row[0]*3.6,1);
}
}
// Récupération et calcul de la moyenne de la direction du vent sur 10 minutes
// Dans un premier temps, on définit une fonction qui permettra de calculer
// la moyenne de plusieurs angles en degrées
function mean_of_angles( $angles, $degrees = true ) {
if ( $degrees ) {
$angles = array_map("deg2rad", $angles); // Convert to radians
}
$s_ = 0;
$c_ = 0;
$len = count( $angles );
for ($i = 0; $i < $len; $i++) {
$s_ += sin( $angles[$i] );
$c_ += cos( $angles[$i] );
}
// $s_ /= $len;
// $c_ /= $len;
$mean = atan2( $s_, $c_ );
if ( $degrees ) {
$mean = rad2deg( $mean ); // Convert to degrees
}
if ($mean < 0) {
$mean_ok = $mean + 360;
} else {
$mean_ok = $mean;
}
return $mean_ok;
}
// Récupération des valeurs de direction du vent moyen des 10 dernières minutes
$sql = "SELECT windDir FROM $db_name.$db_table_mysql WHERE dateTime >= '$minutes10' AND dateTime < '$stop';";
// Requete + mise en tableau de la réponse
$windDirArray = array();
foreach ($con->query($sql) as $row) {
if (!is_null ($row['windDir'])) {
$windDirArray[] = $row['windDir'];
}
}
// Calcul de la moyenne avec la fonction `mean_of_angles` et le tableau
$avg_windDir_10_check = mean_of_angles($windDirArray);
// Vérification du résultat et arrondi
$avg_windDir_10 = '';
if (!is_null($avg_windDir_10_check)) {
if ($avg_wind_10 != 0) { // Si le vent moyen n'est pas égal à 0
$avg_windDir_10 = round($avg_windDir_10_check,1);
}
}
// Détermination de la rafale max des 10 dernières minutes
$sql = "SELECT max(windGust) FROM $db_name.$db_table_mysql WHERE dateTime >= '$minutes10' AND dateTime < '$stop';";
$res = $con->query($sql);
$row = mysqli_fetch_row($res);
$max_windGust_10_check = $row[0];
if($max_windGust_10_check === null){
$max_windGust_10 = '';
}else{
if ($unit=='1') {
$max_windGust_10 = round($max_windGust_10_check*1.60934,1);
}elseif ($unit=='16') {
$max_windGust_10 = round($max_windGust_10_check,1);
}elseif ($unit=='17') {
$max_windGust_10 = round($max_windGust_10_check*3.6,1);
}
}
/*
* Intensité de pluie sur l'heure
*/
// Intensite pluie max sur 1 heure
$sql = "SELECT max(rainRate) FROM $db_name.$db_table_mysql WHERE dateTime >= '$minutes60' AND dateTime < '$stop';";
$res = $con->query($sql);
$row = mysqli_fetch_row($res);
$max_rainRate_hour_check = $row[0];
if($max_rainRate_hour_check === null){
$max_rainRate_hour = '';
}else{
if ($unit=='1') {
$max_rainRate_hour = round($max_rainRate_hour_check*25.4,1);
}elseif ($unit=='16') {
$max_rainRate_hour = round($max_rainRate_hour_check*10,1);
}elseif ($unit=='17') {
$max_rainRate_hour = round($max_rainRate_hour_check,1);
}
}
/*
*
* Mise en forme et calcul des paramètres temps passé
*
*/
// Cumul pluie sur l'heure glissante
$sql = "SELECT SUM(rain) FROM $db_table_mysql WHERE dateTime >= '$minutes60' AND dateTime < '$stop';";
$res = $con->query($sql);
$row = mysqli_fetch_row($res);
$cumul_rain_hour_check = $row[0];
if($cumul_rain_hour_check === null){
$cumul_rain_hour = '';
}else{
if ($unit=='1') {
$cumul_rain_hour = round($cumul_rain_hour_check*25.4,1);
}elseif ($unit=='16') {
$cumul_rain_hour = round($cumul_rain_hour_check*10,1);
}elseif ($unit=='17') {
$cumul_rain_hour = round($cumul_rain_hour_check,1);
}
}
// Cumul pluie de la journée
$today = strtotime('today midnight');
$sql = "SELECT SUM(rain) FROM $db_name.$db_table_mysql WHERE dateTime >= '$today' AND dateTime < '$stop';";
$res = $con->query($sql);
$row = mysqli_fetch_row($res);
$cumul_rain_today_check = $row[0];
if($cumul_rain_today_check === null){
$cumul_rain_today = '';
}else{
if ($unit=='1') {
$cumul_rain_today = round($cumul_rain_today_check*25.4,1);
}elseif ($unit=='16') {
$cumul_rain_today = round($cumul_rain_today_check*10,1);
}elseif ($unit=='17') {
$cumul_rain_today = round($cumul_rain_today_check,1);
}
}
// DEBUG today midnight
if ($debug=="true") {
$dateTimeToday = date('d-m-Y H:i:s',$today);
echo PHP_EOL;
echo "Datetime Today midnight : ".$dateTimeToday.PHP_EOL;
echo "TS Today midnight : ".$today.PHP_EOL;
}
// Cumul pluie de l'année
$yearNow = date('Y');
$startYear = strtotime('01-01-'.$yearNow);
$sql = "SELECT SUM(rain) FROM $db_name.$db_table_mysql WHERE dateTime >= '$startYear' AND dateTime < '$stop';";
$res = $con->query($sql);
$row = mysqli_fetch_row($res);
$cumul_rain_year_check = $row[0];
if($cumul_rain_year_check === null){
$cumul_rain_year = '';
}else{
if ($unit=='1') {
$cumul_rain_year = round($cumul_rain_year_check*25.4,1);
}elseif ($unit=='16') {
$cumul_rain_year = round($cumul_rain_year_check*10,1);
}elseif ($unit=='17') {
$cumul_rain_year = round($cumul_rain_year_check,1);
}
}
// Intensite pluie max de la journée
$sql = "SELECT dateTime, rainRate FROM $db_name.$db_table_mysql WHERE dateTime >= '$today' AND dateTime < '$stop' AND rainRate = (SELECT MAX(rainRate) FROM $db_name.$db_table_mysql WHERE dateTime >= '$today' AND dateTime < '$stop');";
$res = $con->query($sql);
$row = mysqli_fetch_row($res);
$max_rainRate_today_check = $row[1];
if($max_rainRate_today_check === null){
$max_rainRate_today = '';
$max_rainRate_todayTime = '';
}else{
if ($unit=='1') {
$max_rainRate_today = round($max_rainRate_today_check*25.4,1);
}elseif ($unit=='16') {
$max_rainRate_today = round($max_rainRate_today_check*10,1);
}elseif ($unit=='17') {
$max_rainRate_today = round($max_rainRate_today_check,1);
}
$max_rainRate_todayTime = gmdate('H\hi',$row[0]);
}
// Max temp de la journée (fausse Tx puisque de 0h à 24h)
$sql = "SELECT dateTime, outTemp FROM $db_name.$db_table_mysql WHERE dateTime >= '$today' AND dateTime < '$stop' AND outTemp = (SELECT MAX(outTemp) FROM $db_name.$db_table_mysql WHERE dateTime >= '$today' AND dateTime < '$stop');";
$res = $con->query($sql);
$row = mysqli_fetch_row($res);
$max_temp_today_check = $row[1];
if($max_temp_today_check === null){
$max_temp_today = '';
$max_temp_todayTime = '';
}else{
if ($unit=='1') {
$max_temp_today = round(($max_temp_today_check-32)/1.8,1);
}elseif ($unit=='16' OR $unit=='17') {
$max_temp_today = round($max_temp_today_check,1);
}
$max_temp_todayTime = gmdate('H\hi',$row[0]);
}
// Min temp de la journée (fausse Tn puisque de 0h à 24h)
$sql = "SELECT dateTime, outTemp FROM $db_name.$db_table_mysql WHERE dateTime >= '$today' AND dateTime < '$stop' AND outTemp = (SELECT MIN(outTemp) FROM $db_name.$db_table_mysql WHERE dateTime >= '$today' AND dateTime < '$stop');";
$res = $con->query($sql);
$row = mysqli_fetch_row($res);
$min_temp_today_check = $row[1];
if($min_temp_today_check === null){
$min_temp_today = '';
$min_temp_todayTime = '';
}else{
if ($unit=='1') {
$min_temp_today = round(($min_temp_today_check-32)/1.8,1);
}elseif ($unit=='16' OR $unit=='17') {
$min_temp_today = round($min_temp_today_check,1);
}
$min_temp_todayTime = gmdate('H\hi',$row[0]);
}
}
// END MYSQL
else {
echo "Le paramètre db_type est mal renseigné".PHP_EOL;
exit();
}
/*
*
* Génération du fichier texte
*
*/
$static=
"# INFORMATIONS\n"
."id_station=$id_station\n"
."type=txt\n"
."version=$version\n"
."date_releve=$date\n"
."heure_releve_utc=$heure\n"
."# PARAMETRES TEMPS REEL\n"
."temperature=$temp\n"
."pression=$pression\n"
."humidite=$hygro\n"
."point_de_rosee=$dewpoint\n"
."vent_dir_moy=$avg_windDir_10\n"
."vent_moyen=$avg_wind_10\n"
."vent_rafales=$max_windGust_10\n"
."pluie_intensite=$rainrate\n"
."pluie_intensite_maxi_1h=$max_rainRate_hour\n"
."# PARAMETRES TEMPS PASSE\n"
."pluie_cumul_1h=$cumul_rain_hour\n"
."pluie_cumul=$cumul_rain_today\n"
."pluie_cumul_heure_utc=$heure\n"
."pluie_cumul_annee=$cumul_rain_year\n"
."pluie_intensite_maxi=$max_rainRate_today\n"
."pluie_intensite_maxi_heure_utc=$max_rainRate_todayTime\n"
."tn_heure_utc=$min_temp_todayTime\n"
."tn_deg_c=$min_temp_today\n"
."tx_heure_utc=$max_temp_todayTime\n"
."tx_deg_c=$max_temp_today\n"
."# ENSOLEILLEMENT\n"
."radiations_solaires_wlk=$solar\n"
."uv_wlk=$uv\n";
// Enregistrement du fichier en local
$file = $folder."/StatIC_".$id_station.".txt";
$fp=fopen($file,'w');
fwrite($fp,$static);
fclose($fp);
// Push du fichier sur le serveur Infoclimat
// SEULEMENT SI LE TIMESTAMP DU DERNIER RELEVE EN BDD A MOINS DE 20 MINUTES
$timestampNow = time();
if ($debug=="true") {
echo PHP_EOL;
echo "TS Now Comparo FTP IC : ".$timestampNow.PHP_EOL;
}
if ($timestampNow - $stop < 1200) {
$conn_id = ftp_connect($ftp_server) or die("could not connect to $ftp_server");
if (!@ftp_login($conn_id, $ftp_username, $ftp_password)) { die("could not connect to infoclimat");}
ftp_pasv($conn_id, true);
$remote="StatIC_".$id_station.".txt";
ftp_put($conn_id, $remote, $file, FTP_ASCII);
ftp_close($conn_id);
}
?>