-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.js
3989 lines (3897 loc) · 126 KB
/
main.js
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
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"use strict";
//process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0'; Wieder entfernen - Hoffe sie haben jetzt das Zertifikatsmanagement im Griff...
/*
* Created with @iobroker/create-adapter v1.18.0
*/
const utils = require("@iobroker/adapter-core");
var dns = require('dns');
const cron = require('node-cron');
const { https } = require('follow-redirects');
const adapterName = "swiss-weather-api";
const undf = "undefined"
var defaultLanguage = "de";
var timeout;
var geolocationId;
var access_token;
let cronJob;
var today;
var today1;
var today2;
var today3;
var today4;
var today5;
var today6;
var today7;
var today8;
var today9;
var lastSuccessfulRun;
Date.prototype.addDays = function(days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
}
/**
* HACK!!!
* Helper function for https://github.com/baerengraben/ioBroker.swiss-weather-api/issues/78
* For Details see
* @param {Date} date obj.date_time
* @returns {String} String inluding a number for cleaning up the timestamp problem.
*/
function cleanUpTimestampProblem(date){
var myHours = date.getHours();
if (myHours > 0 && myHours < 3) {
return "1"
} else if (myHours > 3 && myHours < 6) {
return "2"
} else if (myHours > 6 && myHours < 9) {
return "3";
} else if (myHours > 9 && myHours < 12) {
return "4";
} else if (myHours > 12 && myHours < 15) {
return "5";
} else if (myHours > 15 && myHours < 18) {
return "6";
} else if (myHours > 18 && myHours < 21) {
return "7";
} else if (myHours > 21 && myHours < 24) {
return "8";
} else {
self.log.error("Error in cleanUpTimestampProblem(). This should not happend. If error persists, create an issue on https://github.com/baerengraben/ioBroker.swiss-weather-api.");
return "0";
};
}
/**
* Checks if JSON is valid
* @param str JSON String
* @returns {boolean} true == valid; false == invalid
*/
function isValidJSONString(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}
/**
* returns formattet Time as hhmm
* @param actualDate Date Object
* @returns {string} hour + min
*/
function getTimeFormattet(actualDate) {
var hour = (actualDate.getHours()<10?'0':'') + actualDate.getHours();
var min = (actualDate.getMinutes()<10?'0':'') + actualDate.getMinutes();
// var sec = (actualDate.getSeconds()<10?'0':'') + actualDate.getSeconds(); // removed due to https://github.com/baerengraben/ioBroker.swiss-weather-api/issues/57
return hour + min;
}
/**
* Get name of day
* @param date Date Object
* @param defaultLanguage language code
* @returns {string} Name of Day
*/
function getDayName(date,defaultLanguage){
var mainversion = parseInt(process.version.substr(1,2))
var weekday;
var arrayOfWeekdays = ["Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"]
// dateObj.toLocaleString("default", { weekday: "long" }) is only supported > nodejs 13
if (mainversion >12){
weekday = date.toLocaleString(defaultLanguage , { weekday: "long" });
} else {
var weekdayNumber = date.getDay();
weekday = arrayOfWeekdays[weekdayNumber];
}
return weekday;
}
/**
* Get longitude/latitude from system if not set or not valid
* do not change if we have already a valid value
* so we could use different settings compared to system if necessary
* @param self Adapter
*/
function getSystemData(self) {
if (typeof self.config.longitude === undf || self.config.longitude == null || self.config.longitude.length === 0 || isNaN(self.config.longitude)
|| typeof self.config.latitude === undf || self.config.latitude == null || self.config.latitude.length === 0 || isNaN(self.config.latitude)) {
self.log.info("longitude/longitude not set, get data from system ");
self.getForeignObject("system.config", (err, state) => {
if (err || state === undf || state === null) {
self.log.error("longitude/latitude not set in adapter-config and reading in system-config failed");
self.setState('info.connection', false, true);
} else {
self.config.longitude = state.common.longitude;
self.config.latitude = state.common.latitude;
self.log.info("system longitude: " + self.config.longitude + " latitude: " + self.config.latitude);
}
});
} else {
self.log.info("longitude/longitude will be set by self-Config - longitude: " + self.config.longitude + " latitude: " + self.config.latitude);
}
}
/**
* Reads ioBroker default Language and set it to adapter defaultLanguage
* @param self Adapter,
*/
function getSystemLanguage(self) {
self.getForeignObject("system.config", (err, state) => {
if (err || state === undf || state === null) {
self.log.error("Default language not set in system-config. Setting default language as 'de' to use for weekday names.");
self.defaultLanguage = "de";
} else {
self.defaultLanguage = state.common.language;
self.log.debug("use system language for weekday names: " + self.defaultLanguage);
}
});
}
/**
* Creates a json Object for usage with Material Design JSON Chart used for Widgets
* @param body srf api response containing days json object
* @returns {*{}} containing Json for usage with Material Design JSON Chart
*/
function createJsonForWidgets(body) {
let sun = [];
let rain = [];
let wind = [];
let calculatedMinSunHour;
let calculatedMaxSunHour;
let calculatedMinPrecipitation;
let calculatedMaxprecipitation;
let calculatedMinWind;
let calculatedMaxWind;
body["days"].forEach(function (obj, index) {
if (typeof obj.SUN_H !== undf || obj.SUN_H != null || typeof obj.RRR_MM !== undf || obj.RRR_MM != null || typeof obj.FF_KMH !== undf || obj.FF_KMH != null) {
sun.push(obj.SUN_H);
rain.push(obj.RRR_MM);
wind.push(obj.FF_KMH);
}
})
//Template
let axisLabelsTempl = ["","","","","","","","",""];
let rainTempl = {
"type": "bar",
"data": rain,
"yAxis_id": 0,
"barIsStacked": false,
"datalabel_show": true,
"line_UseFillColor": true,
"yAxis_show": false,
"color": "blue",
"legendText": "Total Niederschlag",
"yAxis_min": 0,
"yAxis_max": Math.max(...rain) + 5,
"yAxis_maxSteps": 1,
"yAxis_position": "left",
"yAxis_gridLines_show": true,
"yAxis_appendix": " mm",
"yAxis_gridLines_border_show": false,
"yAxis_zeroLineWidth": 0.1,
"yAxis_zeroLineColor": "black",
"displayOrder": 1,
"tooltip_AppendText": " mm",
"datalabel_append": " mm",
"datalabel_align": "bottom",
"datalabel_color": "rgb(27, 17, 28)",
"datalabel_backgroundColor": "blue",
"datalabel_borderRadius": 5
};
let sunTempl = {
"type": "line",
"data": sun,
"yAxis_id": 1,
"datalabel_show": true,
"line_UseFillColor": false,
"yAxis_show": false,
"color": "yellow",
"legendText": "Sonnenstunden",
"yAxis_min": 0,
"yAxis_max": Math.max(...sun) + 3,
"yAxis_maxSteps": 3,
"yAxis_position": "right",
"yAxis_gridLines_show": false,
"yAxis_appendix": " h",
"yAxis_gridLines_border_show": false,
"yAxis_zeroLineWidth": 0.1,
"yAxis_zeroLineColor": "black",
"displayOrder": 1,
"tooltip_AppendText": " h",
"datalabel_append": " h",
"datalabel_align": "left",
"datalabel_color": "rgb(27, 17, 28)",
"datalabel_backgroundColor": "yellow",
"datalabel_borderRadius": 5
};
let windTempl = {
"type": "line",
"data": wind,
"yAxis_id": 2,
"datalabel_show": true,
"line_UseFillColor": false,
"yAxis_show": false,
"color": "aqua",
"legendText": "Wind Geschwindigkeit in km/h",
"yAxis_min": 0,
"yAxis_max": Math.max(...wind) + 10,
"yAxis_maxSteps": 10,
"yAxis_position": "right",
"yAxis_gridLines_show": false,
"yAxis_appendix": " mm",
"yAxis_gridLines_border_show": false,
"yAxis_zeroLineWidth": 0.1,
"yAxis_zeroLineColor": "black",
"displayOrder": 1,
"tooltip_AppendText": " km/h",
"datalabel_append": " km/h",
"datalabel_align": "right",
"datalabel_color": "rgb(27, 17, 28)",
"datalabel_backgroundColor": "aqua",
"datalabel_borderRadius": 5
};
//Create JSON
var chartJsonWeek = {}; // empty Object
var axisLabelsWeek = 'axisLabels';
var graphsWeek = 'graphs';
chartJsonWeek[axisLabelsWeek] = JSON.parse(JSON.stringify(axisLabelsTempl));
chartJsonWeek[graphsWeek] = []; // empty Array, push graphs attributes in here
let myRainData = JSON.parse(JSON.stringify(rainTempl));
myRainData.yAxis_min =
chartJsonWeek[graphsWeek].push(myRainData);
chartJsonWeek[graphsWeek].push(JSON.parse(JSON.stringify(sunTempl)));
chartJsonWeek[graphsWeek].push(JSON.parse(JSON.stringify(windTempl)));
return chartJsonWeek;
}
/**
* Creates a json Object for usage with Material Design JSON Chart used for Views
* @param body srf api response containing hours json object
* @returns {*[]} containing Json for usage with Material Design JSON Chart
*/
function createJsonForViews(body) {
let maxTempDay0;
let maxTempDay1;
let maxTempDay2;
let maxTempDay3;
let maxTempDay4;
let minTempDay0;
let minTempDay1;
let minTempDay2;
let minTempDay3;
let minTempDay4;
let calculatedMinTempDay0;
let calculatedMinTempDay1;
let calculatedMinTempDay2;
let calculatedMinTempDay3;
let calculatedMinTempDay4;
let calculatedMaxTempDay0;
let calculatedMaxTempDay1;
let calculatedMaxTempDay2;
let calculatedMaxTempDay3;
let calculatedMaxTempDay4;
let deltaTemp = 5; // this value is added or subtracted to calculate the minimum and maximum temperature values on the y-axis.
// calculate avgTemp for each day
body["days"].forEach(function (obj, index) {
if (index == 0) {
if (typeof obj.TX_C !== undf || obj.TX_C != null || typeof obj.TN_C !== undf || obj.TN_C != null) {
maxTempDay0 = obj.TX_C;
minTempDay0 = obj.TN_C;
}
} else if (index == 1) {
if (typeof obj.TX_C !== undf || obj.TX_C != null || typeof obj.TN_C !== undf || obj.TN_C != null) {
maxTempDay1 = obj.TX_C;
minTempDay1 = obj.TN_C;
}
} else if (index == 2) {
if (typeof obj.TX_C !== undf || obj.TX_C != null || typeof obj.TN_C !== undf || obj.TN_C != null) {
maxTempDay2 = obj.TX_C;
minTempDay2 = obj.TN_C;
}
} else if (index == 3) {
if (typeof obj.TX_C !== undf || obj.TX_C != null || typeof obj.TN_C !== undf || obj.TN_C != null) {
maxTempDay3 = obj.TX_C;
minTempDay3 = obj.TN_C;
}
} else if (index == 4) {
if (typeof obj.TX_C !== undf || obj.TX_C != null || typeof obj.TN_C !== undf || obj.TN_C != null) {
maxTempDay4 = obj.TX_C;
minTempDay4 = obj.TN_C;
}
}
// When min/max could not be calculated, set default value 15
if (typeof maxTempDay0 === undf || maxTempDay0 == null){
maxTempDay0 = 30;
}
if (typeof maxTempDay1 === undf || maxTempDay1 == null){
maxTempDay1 = 30;
}
if (typeof maxTempDay2 === undf || maxTempDay2 == null){
maxTempDay2 = 30;
}
if (typeof maxTempDay3 === undf || maxTempDay3 == null){
maxTempDay3 = 30;
}
if (typeof maxTempDay4 === undf || maxTempDay4 == null){
maxTempDay4 = 30;
}
if (typeof minTempDay0 === undf || minTempDay0 == null){
minTempDay0 = 0;
}
if (typeof minTempDay1 === undf || minTempDay1 == null){
minTempDay1 = 0;
}
if (typeof minTempDay2 === undf || minTempDay2 == null){
minTempDay2 = 0;
}
if (typeof minTempDay3 === undf || minTempDay3 == null){
maxTempDay3 = 0;
}
if (typeof minTempDay4 === undf || minTempDay4 == null){
minTempDay4 = 0;
}
})
calculatedMinTempDay0 = minTempDay0 - deltaTemp;
calculatedMinTempDay1 = minTempDay1 - deltaTemp;
calculatedMinTempDay2 = minTempDay2 - deltaTemp;
calculatedMinTempDay3 = minTempDay3 - deltaTemp;
calculatedMinTempDay4 = minTempDay4 - deltaTemp;
calculatedMaxTempDay0 = maxTempDay0 + deltaTemp;
calculatedMaxTempDay1 = maxTempDay1 + deltaTemp;
calculatedMaxTempDay2 = maxTempDay2 + deltaTemp;
calculatedMaxTempDay3 = maxTempDay3 + deltaTemp;
calculatedMaxTempDay4 = maxTempDay4 + deltaTemp;
//Templates
let myHoursFull = [
"0h",
"1h",
"2h",
"3h",
"4h",
"5h",
"6h",
"7h",
"8h",
"9h",
"10h",
"11h",
"12h",
"13h",
"14h",
"15h",
"16h",
"17h",
"18h",
"19h",
"20h",
"21h",
"22h",
"23h"
];
let myHoursReduced = [
"0h",
"1h",
"2h"
];
let myGraphsTemplateTemperatur = {
"data": [
17,
19,
18,
19,
19,
20,
20,
21,
22,
24,
24,
24,
23,
22,
23,
23,
24,
23,
23,
22,
22,
21,
20,
20
],
"type": "line",
"color": "gray",
"legendText": "Temperatur",
"line_pointSizeHover": 5,
"line_pointSize": 0,
"line_Tension": 0.3,
"yAxis_show": false,
"yAxis_gridLines_show": false,
"yAxis_gridLines_ticks_length": 5,
"yAxis_min": 0,
"yAxis_max": 30,
"yAxis_step": 5,
"yAxis_position": "left",
"yAxis_appendix": " °C",
"yAxis_zeroLineWidth": 0.1,
"yAxis_zeroLineColor": "black",
"displayOrder": 0,
"tooltip_AppendText": " °C",
"datalabel_backgroundColor": [
"#2b9a44",
"#2b9a44",
"#3aa35b",
"#2b9a44",
"#2b9a44",
"#1d922e",
"#1d922e",
"#0e8917",
"#008000",
"#668f00",
"#668f00",
"#668f00",
"#338700",
"#008000",
"#338700",
"#338700",
"#668f00",
"#338700",
"#338700",
"#008000",
"#008000",
"#0e8917",
"#1d922e",
"#1d922e"
],
"datalabel_color": "white",
"datalabel_offset": -10,
"datalabel_fontFamily": "RobotoCondensed-Light",
"datalabel_fontSize": 12,
"datalabel_borderRadius": 6,
"datalabel_show": "auto",
"line_PointColor": [
"#2b9a44",
"#2b9a44",
"#3aa35b",
"#2b9a44",
"#2b9a44",
"#1d922e",
"#1d922e",
"#0e8917",
"#008000",
"#668f00",
"#668f00",
"#668f00",
"#338700",
"#008000",
"#338700",
"#338700",
"#668f00",
"#338700",
"#338700",
"#008000",
"#008000",
"#0e8917",
"#1d922e",
"#1d922e"
],
"line_PointColorBorder": [
"#2b9a44",
"#2b9a44",
"#3aa35b",
"#2b9a44",
"#2b9a44",
"#1d922e",
"#1d922e",
"#0e8917",
"#008000",
"#668f00",
"#668f00",
"#668f00",
"#338700",
"#008000",
"#338700",
"#338700",
"#668f00",
"#338700",
"#338700",
"#008000",
"#008000",
"#0e8917",
"#1d922e",
"#1d922e"
],
"line_PointColorHover": [
"#2b9a44",
"#2b9a44",
"#3aa35b",
"#2b9a44",
"#2b9a44",
"#1d922e",
"#1d922e",
"#0e8917",
"#008000",
"#668f00",
"#668f00",
"#668f00",
"#338700",
"#008000",
"#338700",
"#338700",
"#668f00",
"#338700",
"#338700",
"#008000",
"#008000",
"#0e8917",
"#1d922e",
"#1d922e"
],
"line_PointColorBorderHover": [
"#2b9a44",
"#2b9a44",
"#3aa35b",
"#2b9a44",
"#2b9a44",
"#1d922e",
"#1d922e",
"#0e8917",
"#008000",
"#668f00",
"#668f00",
"#668f00",
"#338700",
"#008000",
"#338700",
"#338700",
"#668f00",
"#338700",
"#338700",
"#008000",
"#008000",
"#0e8917",
"#1d922e",
"#1d922e"
],
"use_gradient_color": true,
"gradient_color": [
{
"value": -20,
"color": "#5b2c6f66"
},
{
"value": 0,
"color": "#2874a666"
},
{
"value": 14,
"color": "#73c6b666"
},
{
"value": 22,
"color": "#00800066"
},
{
"value": 27,
"color": "#ffa50066"
},
{
"value": 35,
"color": "#ff000066"
}
],
"use_line_gradient_fill_color": true,
"line_gradient_fill_color": [
{
"value": -20,
"color": "#5b2c6f66"
},
{
"value": 0,
"color": "#2874a666"
},
{
"value": 14,
"color": "#73c6b666"
},
{
"value": 22,
"color": "#00800066"
},
{
"value": 27,
"color": "#ffa50066"
},
{
"value": 35,
"color": "#ff000066"
}
]
};
let myGraphsTemplateRegenwahrscheinlichkeit = {
"data": [
50,
50,
50,
50,
50,
50,
50,
50,
50,
50,
50,
50,
50,
19,
33,
36,
23,
14,
16,
34,
46,
40,
24,
22
],
"type": "line",
"color": "#93BDFF",
"legendText": "Regenwahrscheinlichkeit",
"line_UseFillColor": true,
"line_pointSize": 0,
"line_pointSizeHover": 5,
"yAxis_min": 0,
"yAxis_max": 100,
"yAxis_maxSteps": 10,
"yAxis_position": "left",
"yAxis_gridLines_show": false,
"yAxis_gridLines_border_show": false,
"yAxis_zeroLineWidth": 0.1,
"yAxis_zeroLineColor": "black",
"yAxis_appendix": " %",
"displayOrder": 1,
"tooltip_AppendText": " %",
"datalabel_show": false
};
let myGraphsTemplateNiederschlag = {
"data": [
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"1.3",
"2.5",
0,
1.9,
1.17,
0,
0,
0,
0.18,
0.7,
0.2,
0,
0
],
"type": "bar",
"color": "#0061C1",
"legendText": "Niederschlag",
"yAxis_min": 0,
"yAxis_max": 5,
"yAxis_maxSteps": 10,
"yAxis_position": "right",
"yAxis_gridLines_show": false,
"yAxis_appendix": " mm",
"yAxis_gridLines_border_show": false,
"yAxis_zeroLineWidth": 0.1,
"yAxis_zeroLineColor": "black",
"displayOrder": 1,
"tooltip_AppendText": " mm",
"datalabel_show": false
};
var chartJson = [];
//Day0
var chartJsonDay0 = {} // empty Object
var axisLabelsDay0 = 'axisLabels';
var graphsDay0 = 'graphs';
chartJsonDay0[axisLabelsDay0] = []; // empty Array, push axisLabels attributes in here
chartJsonDay0[graphsDay0] = []; // empty Array, push graphs attributes in here
let myGraphsTemperaturDay0 = JSON.parse(JSON.stringify(myGraphsTemplateTemperatur));
myGraphsTemperaturDay0.yAxis_min = calculatedMinTempDay0; //setting y-axis min value
myGraphsTemperaturDay0.yAxis_max = calculatedMaxTempDay0; //setting y-axis max value
let myGraphsTemperaturDataDay0 = [];
let myGraphsNiederschlagDay0 = JSON.parse(JSON.stringify(myGraphsTemplateNiederschlag));
let myGraphsNiederschlagDataDay0 = [];
let myGraphsRegenwahrscheinlichkeitDay0 = JSON.parse(JSON.stringify(myGraphsTemplateRegenwahrscheinlichkeit));
let myGraphsRegenwahrscheinlichkeitDataDay0 = [];
//Day1
var chartJsonDay1 = {} // empty Object
var axisLabelsDay1 = 'axisLabels';
var graphsDay1 = 'graphs';
chartJsonDay1[axisLabelsDay1] = []; // empty Array, push axisLabels attributes in here
chartJsonDay1[graphsDay1] = []; // empty Array, push graphs attributes in here
let myGraphsTemperaturDay1 = JSON.parse(JSON.stringify(myGraphsTemplateTemperatur));
myGraphsTemperaturDay1.yAxis_min = calculatedMinTempDay1; //setting y-axis min value
myGraphsTemperaturDay1.yAxis_max = calculatedMaxTempDay1; //setting y-axis max value
let myGraphsTemperaturDataDay1 = [];
let myGraphsNiederschlagDay1 = JSON.parse(JSON.stringify(myGraphsTemplateNiederschlag));
let myGraphsNiederschlagDataDay1 = [];
let myGraphsRegenwahrscheinlichkeitDay1 = JSON.parse(JSON.stringify(myGraphsTemplateRegenwahrscheinlichkeit));
let myGraphsRegenwahrscheinlichkeitDataDay1 = [];
//Day2
var chartJsonDay2 = {} // empty Object
var axisLabelsDay2 = 'axisLabels';
var graphsDay2 = 'graphs';
chartJsonDay2[axisLabelsDay2] = []; // empty Array, push axisLabels attributes in here
chartJsonDay2[graphsDay2] = []; // empty Array, push graphs attributes in here
let myGraphsTemperaturDay2 = JSON.parse(JSON.stringify(myGraphsTemplateTemperatur));
myGraphsTemperaturDay2.yAxis_min = calculatedMinTempDay2; //setting y-axis min value
myGraphsTemperaturDay2.yAxis_max = calculatedMaxTempDay2; //setting y-axis max value
let myGraphsTemperaturDataDay2 = [];
let myGraphsNiederschlagDay2 = JSON.parse(JSON.stringify(myGraphsTemplateNiederschlag));
let myGraphsNiederschlagDataDay2 = [];
let myGraphsRegenwahrscheinlichkeitDay2 = JSON.parse(JSON.stringify(myGraphsTemplateRegenwahrscheinlichkeit));
let myGraphsRegenwahrscheinlichkeitDataDay2 = [];
//Day3
var chartJsonDay3 = {} // empty Object
var axisLabelsDay3 = 'axisLabels';
var graphsDay3 = 'graphs';
chartJsonDay3[axisLabelsDay3] = []; // empty Array, push axisLabels attributes in here
chartJsonDay3[graphsDay3] = []; // empty Array, push graphs attributes in here
let myGraphsTemperaturDay3 = JSON.parse(JSON.stringify(myGraphsTemplateTemperatur));
myGraphsTemperaturDay3.yAxis_min = calculatedMinTempDay3; //setting y-axis min value
myGraphsTemperaturDay3.yAxis_max = calculatedMaxTempDay3; //setting y-axis max value
let myGraphsTemperaturDataDay3 = [];
let myGraphsNiederschlagDay3 = JSON.parse(JSON.stringify(myGraphsTemplateNiederschlag));
let myGraphsNiederschlagDataDay3 = [];
let myGraphsRegenwahrscheinlichkeitDay3 = JSON.parse(JSON.stringify(myGraphsTemplateRegenwahrscheinlichkeit));
let myGraphsRegenwahrscheinlichkeitDataDay3 = [];
//Day4
var chartJsonDay4 = {} // empty Object
var axisLabelsDay4 = 'axisLabels';
var graphsDay4 = 'graphs';
chartJsonDay4[axisLabelsDay4] = []; // empty Array, push axisLabels attributes in here
chartJsonDay4[graphsDay4] = []; // empty Array, push graphs attributes in here
let myGraphsTemperaturDay4 = JSON.parse(JSON.stringify(myGraphsTemplateTemperatur));
myGraphsTemperaturDay4.yAxis_min = calculatedMinTempDay4; //setting y-axis min value
myGraphsTemperaturDay4.yAxis_max = calculatedMaxTempDay4; //setting y-axis max value
let myGraphsTemperaturDataDay4 = [];
let myGraphsNiederschlagDay4 = JSON.parse(JSON.stringify(myGraphsTemplateNiederschlag));
let myGraphsNiederschlagDataDay4 = [];
let myGraphsRegenwahrscheinlichkeitDay4 = JSON.parse(JSON.stringify(myGraphsTemplateRegenwahrscheinlichkeit));
let myGraphsRegenwahrscheinlichkeitDataDay4 = [];
body["hours"].forEach(function(obj,index) {
if (index < 24) {
if (typeof obj.TTT_C !== undf || obj.TTT_C != null) {
myGraphsTemperaturDataDay0.push(obj.TTT_C);
}
if (typeof obj.PROBPCP_PERCENT !== undf || obj.PROBPCP_PERCENT != null) {
myGraphsRegenwahrscheinlichkeitDataDay0.push(obj.PROBPCP_PERCENT);
}
if (typeof obj.RRR_MM !==undf || obj.RRR_MM != null) {
myGraphsNiederschlagDataDay0.push(obj.RRR_MM);
}
} else if (index > 23 && index < 48){
if (typeof obj.TTT_C !== undf || obj.TTT_C != null) {
myGraphsTemperaturDataDay1.push(obj.TTT_C);
}
if (typeof obj.PROBPCP_PERCENT !== undf || obj.PROBPCP_PERCENT != null) {
myGraphsRegenwahrscheinlichkeitDataDay1.push(obj.PROBPCP_PERCENT);
}
if (typeof obj.RRR_MM !== undf || obj.RRR_MM != null) {
myGraphsNiederschlagDataDay1.push(obj.RRR_MM);
}
} else if (index > 47 && index < 72){
if (typeof obj.TTT_C !== undf || obj.TTT_C != null) {
myGraphsTemperaturDataDay2.push(obj.TTT_C);
}
if (typeof obj.PROBPCP_PERCENT !== undf || obj.PROBPCP_PERCENT != null) {
myGraphsRegenwahrscheinlichkeitDataDay2.push(obj.PROBPCP_PERCENT);
}
if (typeof obj.RRR_MM !== undf || obj.RRR_MM != null) {
myGraphsNiederschlagDataDay2.push(obj.RRR_MM);
}
} else if (index > 71 && index < 96){
if (typeof obj.TTT_C !== undf || obj.TTT_C != null) {
myGraphsTemperaturDataDay3.push(obj.TTT_C);
}
if (typeof obj.PROBPCP_PERCENT !== undf || obj.PROBPCP_PERCENT != null) {
myGraphsRegenwahrscheinlichkeitDataDay3.push(obj.PROBPCP_PERCENT);
}
if (typeof obj.RRR_MM !== undf || obj.RRR_MM != null) {
myGraphsNiederschlagDataDay3.push(obj.RRR_MM);
}
} else if (index > 95){
if (typeof obj.TTT_C !== undf || obj.TTT_C != null) {
myGraphsTemperaturDataDay4.push(obj.TTT_C);
}
if (typeof obj.PROBPCP_PERCENT !== undf || obj.PROBPCP_PERCENT != null) {
myGraphsRegenwahrscheinlichkeitDataDay4.push(obj.PROBPCP_PERCENT);
}
if (typeof obj.RRR_MM !== undf || obj.RRR_MM != null) {
myGraphsNiederschlagDataDay4.push(obj.RRR_MM);
}
}
});
// Day0
myGraphsTemperaturDay0.data = myGraphsTemperaturDataDay0;
myGraphsRegenwahrscheinlichkeitDay0.data = myGraphsRegenwahrscheinlichkeitDataDay0;
myGraphsNiederschlagDay0.data = myGraphsNiederschlagDataDay0;
// Day1
myGraphsTemperaturDay1.data = myGraphsTemperaturDataDay1;
myGraphsRegenwahrscheinlichkeitDay1.data = myGraphsRegenwahrscheinlichkeitDataDay1;
myGraphsNiederschlagDay1.data = myGraphsNiederschlagDataDay1;
// Day2
myGraphsTemperaturDay2.data = myGraphsTemperaturDataDay2;
myGraphsRegenwahrscheinlichkeitDay2.data = myGraphsRegenwahrscheinlichkeitDataDay2;
myGraphsNiederschlagDay2.data = myGraphsNiederschlagDataDay2;
// Day3
myGraphsTemperaturDay3.data = myGraphsTemperaturDataDay3;
myGraphsRegenwahrscheinlichkeitDay3.data = myGraphsRegenwahrscheinlichkeitDataDay3;
myGraphsNiederschlagDay3.data = myGraphsNiederschlagDataDay3;
// Day4
myGraphsTemperaturDay4.data = myGraphsTemperaturDataDay4;
myGraphsRegenwahrscheinlichkeitDay4.data = myGraphsRegenwahrscheinlichkeitDataDay4;
myGraphsNiederschlagDay4.data = myGraphsNiederschlagDataDay4;
//Day0
chartJsonDay0[axisLabelsDay0] = myHoursFull;
chartJsonDay0[graphsDay0].push(myGraphsTemperaturDay0 );
chartJsonDay0[graphsDay0].push(myGraphsRegenwahrscheinlichkeitDay0);
chartJsonDay0[graphsDay0].push(myGraphsNiederschlagDay0);
//Day1
chartJsonDay1[axisLabelsDay1] = myHoursFull;
chartJsonDay1[graphsDay1].push(myGraphsTemperaturDay1);
chartJsonDay1[graphsDay1].push(myGraphsRegenwahrscheinlichkeitDay1);
chartJsonDay1[graphsDay1].push(myGraphsNiederschlagDay1);
//Day2
chartJsonDay2[axisLabelsDay2] = myHoursFull;
chartJsonDay2[graphsDay2].push(myGraphsTemperaturDay2);
chartJsonDay2[graphsDay2].push(myGraphsRegenwahrscheinlichkeitDay2);
chartJsonDay2[graphsDay2].push(myGraphsNiederschlagDay2);
//Day3
chartJsonDay3[axisLabelsDay3] = myHoursFull;
chartJsonDay3[graphsDay3].push(myGraphsTemperaturDay3);
chartJsonDay3[graphsDay3].push(myGraphsRegenwahrscheinlichkeitDay3);
chartJsonDay3[graphsDay3].push(myGraphsNiederschlagDay3);
//Day4
chartJsonDay4[axisLabelsDay4] = myHoursReduced;
chartJsonDay4[graphsDay4].push(myGraphsTemperaturDay4);
chartJsonDay4[graphsDay4].push(myGraphsRegenwahrscheinlichkeitDay4);
chartJsonDay4[graphsDay4].push(myGraphsNiederschlagDay4);
chartJson.push(chartJsonDay0);
chartJson.push(chartJsonDay1);
chartJson.push(chartJsonDay2);
chartJson.push(chartJsonDay3);
chartJson.push(chartJsonDay4);
return chartJson;
}
/**
* Deletes all Objects of Adapter
* @param self this Adapter
*/
async function deleteAllAdapterObjects(self){
self.log.debug('Deleting all geolocation objects');
let resp1 = await self.delObjectAsync('geolocation', {recursive: true});
self.log.debug('Deleting all forecast objects');
let resp2 = await self.delObjectAsync('forecast', {recursive: true});
}
/**
* Get Token by REST-Calling SRF Weather API
* @param self this adapterinfo.connection
* @param myCallback Callback
*/
function getToken(self,myCallback){
self.log.debug('getting Token...');
//Convert consumerKey and consumerSecret to base64
let data = self.config.consumerKey + ":" + self.config.consumerSecret;
let buff = Buffer.from(data);
let base64data = buff.toString('base64');
self.log.debug('"' + data + '" converted to Base64 is "' + base64data + '"');
//Options for getting Access-Token
var options_Access_Token = {
"json": true,
"method": "POST",
"hostname": "api.srgssr.ch",
"port": null,
"path": "/oauth/v1/accesstoken?grant_type=client_credentials",
"headers": {
"Authorization": "Basic " + base64data,
"Cache-Control": "no-cache",
"Content-Length": 0,
"Postman-Token": "24264e32-2de0-f1e3-f3f8-eab014bb6d76"
}
};
self.log.debug("Options to get Access Token: " + JSON.stringify(options_Access_Token));
var req = https.request(options_Access_Token, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
self.log.debug("Answer of Request Access Token: " + Buffer.concat(chunks).toString());
if (!isValidJSONString(Buffer.concat(chunks).toString())){
self.log.error("Delivered SRF-JSON is not valid: " + Buffer.concat(chunks).toString());
self.log.error("Possible cause is an incorrectly configured adapter. Please check configuration. If error persists, create an issue on https://github.com/baerengraben/ioBroker.swiss-weather-api.");
self.setState('info.connection', false, true);
return;
}
var body = JSON.parse(Buffer.concat(chunks).toString());
if (typeof body.access_token === undf || body.access_token == null) {
self.log.warn("Got no Token - Is Adapter correctly configured (consumerKey/consumerSecret)? It may also be that the maximum number of queries for today is exhausted");
self.setState('info.connection', false, true);
return;
} else if (body.access_token === ""){
self.log.warn("Got an empty Token - It may be that the maximum number of queries for today is exhausted");
self.setState('info.connection', false, true);
return;
}
access_token = body.access_token.toString();
self.log.debug("Access_Token : " + access_token);
myCallback(self,getForecast);
});
res.on("error", function (error) {
self.setState('info.connection', false, true);
self.log.error(error)
});
});
req.end();