/* Magic Mirror
 * Module: MMM-DWD-WarnWeather
 *
 * By Luke Scheffler https://github.com/LukeSkywalker92
 * MIT Licensed.
 */

var NodeHelper = require('node_helper');
var request = require('request');

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'";
		}
		else if (region.sevThres < 3) {
			severityStr = severityStr + ",'Moderate'";
		}
		else if (region.sevThres < 2) {
			severityStr = severityStr + ",'Minor'";
		}					 

		severityStr = severityStr + ")";
							 
		if (region.lng) {
			var regionFilter = encodeURIComponent("CONTAINS(THE_GEOM, POINT(" + region.lng + " " + region.lat + "))" + severityStr);
		}
    else {
		  var regionFilter = encodeURIComponent("AREADESC='" + region.reg + "'" + severityStr);
    }

		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);
		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;

		var requests = 2;

		//get name
		request({
			url: nameurl,
			method: 'GET'
		}, function (error, response, body) {
			var result = JSON.parse(body);
			if (result.totalFeatures == 1) {
				communityData = result.features[0];
			}
			if(--requests == 0) {
				callback(self, warningData, region, communityData);
			}
		});

		//get warnings
		request({
			url: warnurl,
			method: 'GET'
		}, function (error, response, body) {

			var result = JSON.parse(body);

			if (result.totalFeatures > 0) {
				for (var i = 0; i < result.totalFeatures; i++) {
					warningData.push(result.features[i]);
				}
			}
			if(--requests == 0) {
				callback(self, warningData, region, communityData);
			}
		});
	},

	socketNotificationReceived: function (notification, payload) {
    if (notification === 'GET_WARNINGS') {
			this.getWarningData(payload, this.dataLoadingComplete);
		}
	}

});

@@ -703,32 +703,28 @@
 evere'%22;%0A%09%09%7D
+;
 %0A%09%09
-else 
 if (region.s
@@ -791,16 +791,12 @@
 %0A%09%09%7D
+;
 %0A%09%09
-else 
 if (
@@ -864,16 +864,17 @@
 r'%22;%0A%09%09%7D
+;
 %09%09%09%09%09 %0A%0A
@@ -881,32 +881,51 @@
 %09%09severityStr = 
+encodeURIComponent(
 severityStr + %22)
@@ -925,16 +925,17 @@
 tr + %22)%22
+)
 ;%0A%09%09%09%09%09%09
@@ -1064,30 +1064,16 @@
 t + %22))%22
- + severityStr
 );%0A%09%09%7D%0A 
@@ -1159,22 +1159,8 @@
  %22'%22
- + severityStr
 );%0A 
@@ -1498,16 +1498,45 @@
 ilter);%0A
+%09%09// console.error(nameurl);%0A
 %09%09var wa
@@ -1719,16 +1719,59 @@
 onFilter
+ + severityStr;%0A%09%09// console.error(warnurl)
 ;%0A%0A%09%09var
@@ -2055,32 +2055,36 @@
 ningData, region
+.reg
 , communityData)
@@ -2433,32 +2433,36 @@
 ningData, region
+.reg
 , communityData)

Update node_helper.js