forked from baerengraben/ioBroker.swiss-weather-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
3076 lines (3008 loc) · 91.3 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";
/*
* Created with @iobroker/create-adapter v1.18.0
*/
const utils = require("@iobroker/adapter-core");
var dns = require('dns');
const cron = require('node-cron');
const { http, https } = require('follow-redirects');
const adapterName = "swiss-weather-api";
const undf = "undefined"
var defaultLanguage = "undefined";
var timeout;
var geolocationId;
var access_token;
let cronJob;
var today;
// @ts-ignore
var today1;
// @ts-ignore
var today2;
// @ts-ignore
var today3;
// @ts-ignore
var today4;
// @ts-ignore
var today5;
// @ts-ignore
var today6;
// @ts-ignore
var today7;
// @ts-ignore
var lastSuccessfulRun;
// @ts-ignore
Date.prototype.addDays = function(days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
}
/**
* 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
* @param actualDate Date Object
* @returns {string} hour + ":" + min + ":" + sec
*/
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();
return hour + ":" + min + ":" + sec;
}
/**
* returns formattet Date
* @param actualDate Date Object
* @returns {string} hour + ":" + minutes + " " + day + "." + month + "." + year
*/
function getActualDateFormattet(actualDate) {
var year = (actualDate.getFullYear());
var month = (actualDate.getMonth()<10?'0':'') + actualDate.getMonth();
var day = (actualDate.getDate()<10?'0':'') + actualDate.getDate();
var hour = (actualDate.getHours()<10?'0':'') + actualDate.getHours();
var minutes = (actualDate.getMinutes()<10?'0':'') + actualDate.getMinutes();
return hour + ":" + minutes + " " + day + "." + month + "." + year;
}
/**
* 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 en-GB to use for weekday names.");
self.defaultLanguage = "en-GB";
} 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
* @param body srf api response containing 60minutes json object
* @returns {*[]} containing Json for usage with Material Design JSON Chart
*/
function createJson(body) {
//Templates
let myHoursFull = [
"1h",
"2h",
"3h",
"4h",
"5h",
"6h",
"7h",
"8h",
"9h",
"10h",
"11h",
"12h",
"13h",
"14h",
"15h",
"16h",
"17h",
"18h",
"19h",
"20h",
"21h",
"22h",
"23h",
"24h"
];
let myHoursReduced = [
"1h",
"2h",
"3h"
];
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));
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));
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));
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));
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));
let myGraphsTemperaturDataDay4 = [];
let myGraphsNiederschlagDay4 = JSON.parse(JSON.stringify(myGraphsTemplateNiederschlag));
let myGraphsNiederschlagDataDay4 = [];
let myGraphsRegenwahrscheinlichkeitDay4 = JSON.parse(JSON.stringify(myGraphsTemplateRegenwahrscheinlichkeit));
let myGraphsRegenwahrscheinlichkeitDataDay4 = [];
body.forecast["60minutes"].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;
}
/**
* Get Token by REST-Calling SRF Weather API
* @param self this adapter
* @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();
}
/**
* Sets the current hour objects with the corresponding values of forecast.60minutes.day0
* @param self Adapter
*/
function setCurrentHour(self){
let path = self.namespace + ".forecast.60minutes.day0"
let updatePath = "forecast.current_hour";
self.log.info('update current hour...');
//get systemtime hour
var date = new Date();
var hour = (date.getHours()<10?'0':'') + date.getHours();
var local_background_color ;
var local_temperature ;
var local_text_color ;
var local_DD_DEG ;
var local_FF_KMH ;
var local_FX_KMH ;
var local_ICON_URL_COLOR ;
var local_ICON_URL_DARK ;
var local_ICON_URL_LIGHT ;
var local_PROBPCP_PERCENT ;
var local_RRR_MM ;
var local_SYMBOL_CODE ;
var local_TTH_C ;
var local_TTL_C ;
var local_TTT_C ;
var local_local_date_time ;
var local_type ;
// update current_hour objects
self.getState(path + '.00:00:00.DD_DEG', (err, state) => {
if (!state || state.val === null) {
self.log.debug('tried to update current_hour, but no forecast data is available for ' + path + '.00:00:00.DD_DEG' + '. Try my luck on next hour...');
} else {
self.log.debug('forecast data is available. State.val is: ' + state.val +': So updating current_hour...read correspondenting hour forecast from ' +
'swiss-weather-api.0.forecast.60minutes.day0.actual_hour and write it to swiss-weather-api.0.forecast.current_hour');
self.getState(path + '.' + hour +':00:00.cur_color.background_color', function(err, state) {
local_background_color = state.val;
});
self.getState(path + '.' + hour +':00:00.cur_color.temperature', function(err, state) {
local_temperature = state.val;
});
self.getState(path + '.' + hour +':00:00.cur_color.text_color', function(err, state) {
local_text_color = state.val;
});
self.getState(path + '.' + hour +':00:00.DD_DEG', function(err, state) {
local_DD_DEG = state.val;
});
self.getState(path + '.' + hour +':00:00.FF_KMH', function(err, state) {
local_FF_KMH = state.val;
});
self.getState(path + '.' + hour +':00:00.FX_KMH', function(err, state) {
local_FX_KMH = state.val;
});
self.getState(path + '.' + hour +':00:00.ICON_URL_COLOR', function(err, state) {
local_ICON_URL_COLOR = state.val;
});
self.getState(path + '.' + hour +':00:00.ICON_URL_DARK', function(err, state) {
local_ICON_URL_DARK = state.val;
});
self.getState(path + '.' + hour +':00:00.ICON_URL_LIGHT', function(err, state) {
local_ICON_URL_LIGHT = state.val;
});
self.getState(path + '.' + hour +':00:00.PROBPCP_PERCENT', function(err, state) {
local_PROBPCP_PERCENT = state.val;
});
self.getState(path + '.' + hour +':00:00.RRR_MM', function(err, state) {
local_RRR_MM = state.val;
});
self.getState(path + '.' + hour +':00:00.SYMBOL_CODE', function(err, state) {
local_SYMBOL_CODE = state.val;
});
self.getState(path + '.' + hour +':00:00.TTH_C', function(err, state) {
local_TTH_C = state.val;
});
self.getState(path + '.' + hour +':00:00.TTL_C', function(err, state) {
local_TTL_C = state.val;
});
self.getState(path + '.' + hour +':00:00.TTT_C', function(err, state) {
local_TTT_C = state.val;
});
self.getState(path + '.' + hour +':00:00.local_date_time', function(err, state) {
local_local_date_time = state.val;
});
self.getState(path + '.' + hour +':00:00.type', function(err, state) {
local_type = state.val;
});
//*** Create current_hour object ***
self.setObjectNotExists(updatePath, {
type: "channel",
common: {
name: "Holds the current hour data. This is updated on every full hour by coping the data from forecast.60minutes.day0 - actual hour",
role: "info"
},
native: {},
});
self.setObjectNotExists(updatePath + "." + "local_date_time", {
type: "state",
common: {
name: "Date for validity of record",
type: "string",
role: "text",
write: false
},
native: {},
}, function () {
self.setState(updatePath + "." + "local_date_time", {
val: local_local_date_time,
ack: true
});
});
self.setObjectNotExists(updatePath + "." + "TTT_C", {
type: "state",
common: {
name: "Current temperature in °C",
type: "number",
role: "value",
write: false
},
native: {},
}, function () {
self.setState(updatePath + "." + "TTT_C", {
val: local_TTT_C,
ack: true
});
});
self.setObjectNotExists(updatePath + "." + "TTL_C", {
type: "state",
common: {
name: "Error range lower limit",
type: "number",
role: "value",
write: false
},
native: {},
}, function () {
self.setState(updatePath + "." + "TTL_C", {
val: local_TTL_C,
ack: true
});
});
self.setObjectNotExists(updatePath + "." + "TTH_C", {
type: "state",
common: {
name: "Error range upper limit",
type: "number",
role: "value",
write: false
},
native: {},
}, function () {
self.setState(updatePath + "." + "TTH_C", {
val: local_TTH_C,
ack: true
});
});
self.setObjectNotExists(updatePath + "." + "PROBPCP_PERCENT", {
type: "state",
common: {
name: "Probability of precipitation in %",
type: "number",
role: "value",
write: false
},
native: {},
}, function () {
self.setState(updatePath + "." + "PROBPCP_PERCENT", {
val: local_PROBPCP_PERCENT,
ack: true
});
});
self.setObjectNotExists(updatePath + "." + "RRR_MM", {
type: "state",
common: {
name: "Precipitation total",
type: "number",
role: "value",
write: false
},
native: {},
}, function () {
self.setState(updatePath + "." + "RRR_MM", {
val: local_RRR_MM,
ack: true
});
});
self.setObjectNotExists(updatePath + "." + "FF_KMH", {
type: "state",
common: {
name: "Wind speed in km/h",
type: "number",
role: "value",
write: false
},
native: {},
}, function () {
self.setState(updatePath + "." + "FF_KMH", {
val: local_FF_KMH,
ack: true
});
});
self.setObjectNotExists(updatePath + "." + "FX_KMH", {
type: "state",
common: {
name: "Peak wind speed in km/h",
type: "number",
role: "value",
write: false
},
native: {},
}, function () {
self.setState(updatePath + "." + "FX_KMH", {
val: local_FX_KMH,
ack: true
});
});
self.setObjectNotExists(updatePath + "." + "DD_DEG", {
type: "state",
common: {
name: "Wind direction in angular degrees: 0 = North wind",
type: "number",
role: "value",
write: false
},
native: {},
}, function () {
self.setState(updatePath + "." + "DD_DEG", {
val: local_DD_DEG,
ack: true
});
});
self.setObjectNotExists(updatePath + "." + "SYMBOL_CODE", {
type: "state",
common: {
name: "Mapping to weather icon",
type: "number",
role: "value",
write: false
},
native: {},
}, function () {
self.setState(updatePath + "." + "SYMBOL_CODE", {
val: local_SYMBOL_CODE,
ack: true
});
});
self.setObjectNotExists(updatePath + "." + "ICON_URL_COLOR", {
type: "state",
common: {