forked from LukeSkywalker92/MMM-DWD-WarnWeather
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
100 lines (86 loc) · 2.96 KB
/
node_helper.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
/* Magic Mirror
* Module: MMM-DWD-WarnWeather
*
* By Luke Scheffler https://github.com/LukeSkywalker92
* MIT Licensed.
*
* extended by the possibility to only request warnings higher than a certain warning level
* By @spitzlbergerj
*/
var NodeHelper = require('node_helper');
const fetch = require('node-fetch');
module.exports = NodeHelper.create({
start: function () {
console.log('MMM-DWD-WarnWeather helper started...')
},
dataLoadingComplete: function (self, warningData, region, communityData) {
self.sendSocketNotification('WARNINGS_DATA', { warnings: warningData, region: region, community: communityData });
},
getWarningData: function (region, callback) {
var self = this;
var severityStr = " AND SEVERITY IN ('Extreme'";
if (region.sevThres < 4) {
severityStr = severityStr + ",'Severe'";
};
if (region.sevThres < 3) {
severityStr = severityStr + ",'Moderate'";
};
if (region.sevThres < 2) {
severityStr = severityStr + ",'Minor'";
};
severityStr = encodeURIComponent(severityStr + ")");
if (region.lng) {
var regionFilter = encodeURIComponent("CONTAINS(THE_GEOM, POINT(" + region.lng + " " + region.lat + "))");
}
else if (region.cellid) {
var regionFilter = encodeURIComponent("WARNCELLID=" + region.cellid);
} else {
var regionFilter = encodeURIComponent("AREADESC='" + region.reg + "'");
}
var communityData = [];
var warningData = [];
var nameurl = 'https://maps.dwd.de/geoserver/dwd/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=dwd:Warngebiete_Gemeinden&outputFormat=application%2Fjson&CQL_FILTER=' +
(regionFilter.includes("AREADESC") ? regionFilter.replace("AREADESC", "KURZNAME") : regionFilter);
// console.error(nameurl);
var warnurl = 'https://maps.dwd.de/geoserver/dwd/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=dwd:Warnungen_Gemeinden&outputFormat=application%2Fjson&CQL_FILTER=' + regionFilter + severityStr;
// console.error(warnurl);
var requests = 2;
//get name
fetch(nameurl, {
method: 'GET'
}).then(res => res.json()
).then(json => {
if (json.totalFeatures == 1) {
communityData = json.features[0];
}
if (--requests == 0) {
if (region.reg)
callback(self, warningData, region.reg, communityData);
else if (region.cellid)
callback(self, warningData, region.cellid, communityData);
}
});
//get warnings
fetch(warnurl, {
method: 'GET'
}).then(res => res.json()
).then(json => {
if (json.totalFeatures > 0) {
for (var i = 0; i < json.totalFeatures; i++) {
warningData.push(json.features[i]);
}
}
if (--requests == 0) {
if (region.reg)
callback(self, warningData, region.reg, communityData);
else if (region.cellid)
callback(self, warningData, region.cellid, communityData);
}
});
},
socketNotificationReceived: function (notification, payload) {
if (notification === 'GET_WARNINGS') {
this.getWarningData(payload, this.dataLoadingComplete);
}
}
});