Skip to content

Commit

Permalink
Use Warncellid if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Klami committed May 27, 2020
1 parent caf9fb8 commit 70340e6
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 47 deletions.
70 changes: 37 additions & 33 deletions MMM-DWD-WarnWeather.js
Expand Up @@ -47,18 +47,18 @@ Module.register("MMM-DWD-WarnWeather", {
this.community = [];
this.icons = {
THUNDERSTORM: 'sprite-gewitter',
WIND: 'sprite-sturm',
TORNADO: 'sprite-sturm',
RAIN: 'sprite-regen',
HAIL: 'sprite-schnee',
SNOWFALL: 'sprite-schnee',
SNOWDRIFT: 'sprite-schnee',
FOG: 'sprite-nebel',
FROST: 'sprite-frost',
GLAZE: 'sprite-glatteis',
THAW: 'sprite-tauwetter',
HEAT: 'sprite-hitze',
UV: 'sprite-uv'
WIND: 'sprite-sturm',
TORNADO: 'sprite-sturm',
RAIN: 'sprite-regen',
HAIL: 'sprite-schnee',
SNOWFALL: 'sprite-schnee',
SNOWDRIFT: 'sprite-schnee',
FOG: 'sprite-nebel',
FROST: 'sprite-frost',
GLAZE: 'sprite-glatteis',
THAW: 'sprite-tauwetter',
HEAT: 'sprite-hitze',
UV: 'sprite-uv'
}

this.updateWarnings(this);
Expand All @@ -67,11 +67,15 @@ Module.register("MMM-DWD-WarnWeather", {
// Make node_helper to get new warning-data
updateWarnings: function (self) {
if (self.config.region) {
var regionThreshold = {reg:self.config.region, sevThres:self.config.severityThreshold};
var regionThreshold = { reg: self.config.region, sevThres: self.config.severityThreshold };
self.sendSocketNotification('GET_WARNINGS', regionThreshold);
}
}
else if (self.config.warnCellID) {
var cellIDThreshold = { cellid: self.config.warnCellID, sevThres: self.config.severityThreshold };
self.sendSocketNotification('GET_WARNINGS', cellIDThreshold);
}
else if (self.config.lat && self.config.lng) {
var coordsThreshold = {lat:self.config.lat, lng:self.config.lng, sevThres:self.config.severityThreshold};
var coordsThreshold = { lat: self.config.lat, lng: self.config.lng, sevThres: self.config.severityThreshold };
self.sendSocketNotification('GET_WARNINGS', coordsThreshold);
}
setTimeout(self.updateWarnings, self.config.interval, self);
Expand All @@ -95,7 +99,7 @@ Module.register("MMM-DWD-WarnWeather", {

var locNotFound = document.createElement("div");
locNotFound.className = 'locationNotFound';
if (! this.community.hasOwnProperty('properties')){
if (!this.community.hasOwnProperty('properties')) {
locNotFound.innerHTML = 'Ort nicht gefunden';
wrapper.appendChild(locNotFound);
}
Expand Down Expand Up @@ -172,40 +176,40 @@ Module.register("MMM-DWD-WarnWeather", {
width = width || 75;

if (!str) {
return str;
return str;
}

var re = new RegExp(".{1," + width +
"}(\\s|$)|\\ S+?(\\s|$)", "g");
"}(\\s|$)|\\ S+?(\\s|$)", "g");

return str.match(RegExp(re)).join(brk);
return str.match(RegExp(re)).join(brk);
},

pointInPoly: function (point, vs) {
// ray-casting algorithm based on
// http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
// ray-casting algorithm based on
// http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html

var x = point[0], y = point[1];
var x = point[0], y = point[1];

var inside = false;
for (var i = 0, j = vs.length - 1; i < vs.length; j = i++) {
var xi = vs[i][0], yi = vs[i][1];
var xj = vs[j][0], yj = vs[j][1];
var inside = false;
for (var i = 0, j = vs.length - 1; i < vs.length; j = i++) {
var xi = vs[i][0], yi = vs[i][1];
var xj = vs[j][0], yj = vs[j][1];

var intersect = ((yi > y) != (yj > y))
&& (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
if (intersect) inside = !inside;
}
var intersect = ((yi > y) != (yj > y))
&& (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
if (intersect) inside = !inside;
}

return inside;
return inside;
},

socketNotificationReceived: function (notification, payload) {
Log.info(notification);

if (notification === 'WARNINGS_DATA') {
if(payload.community.hasOwnProperty('geometry')){
if (payload.region == this.config.region || this.pointInPoly([this.config.lng, this.config.lat], payload.community.geometry.coordinates[0])){
if (payload.community.hasOwnProperty('geometry')) {
if (payload.region == this.config.region || payload.region == this.config.warnCellID || this.pointInPoly([this.config.lng, this.config.lat], payload.community.geometry.coordinates[0])) {
Log.info(payload);
this.warnings = payload.warnings;
this.community = payload.community;
Expand Down
36 changes: 22 additions & 14 deletions node_helper.js
Expand Up @@ -16,14 +16,14 @@ module.exports = NodeHelper.create({
console.log('MMM-DWD-WarnWeather helper started...')
},

dataLoadingComplete: function (self, warningData, region, communityData){
self.sendSocketNotification('WARNINGS_DATA', {warnings: warningData, region: region, community: communityData});
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'";
};
Expand All @@ -32,22 +32,24 @@ module.exports = NodeHelper.create({
};
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 {
var regionFilter = encodeURIComponent("AREADESC='" + region.reg + "'");
}
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", "DWD_NAME"):regionFilter);
(regionFilter.includes("AREADESC") ? regionFilter.replace("AREADESC", "DWD_NAME") : 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);
Expand All @@ -63,8 +65,11 @@ module.exports = NodeHelper.create({
if (result.totalFeatures == 1) {
communityData = result.features[0];
}
if(--requests == 0) {
callback(self, warningData, region.reg, communityData);
if (--requests == 0) {
if (region.reg)
callback(self, warningData, region.reg, communityData);
else if (region.cellid)
callback(self, warningData, region.cellid, communityData);
}
});

Expand All @@ -81,14 +86,17 @@ module.exports = NodeHelper.create({
warningData.push(result.features[i]);
}
}
if(--requests == 0) {
callback(self, warningData, region.reg, communityData);
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') {
if (notification === 'GET_WARNINGS') {
this.getWarningData(payload, this.dataLoadingComplete);
}
}
Expand Down

0 comments on commit 70340e6

Please sign in to comment.