diff --git a/test/weather/weather_spec.js b/test/weather/weather_spec.js index 289dddf6..c3c4dbf9 100644 --- a/test/weather/weather_spec.js +++ b/test/weather/weather_spec.js @@ -16,13 +16,16 @@ describe('weather nodes', function() { weatherdata.should.have.property("windspeed", "8.7"); weatherdata.should.have.property("winddirection", "220"); weatherdata.should.have.property("location", "London"); - weatherdata.should.have.property("lon", "-0.13"); - weatherdata.should.have.property("lat", "51.51"); weatherdata.should.have.property("sunrise", "1412748812"); weatherdata.should.have.property("sunset", "1412788938"); weatherdata.should.have.property("clouds", "40"); } + var locationDataTest = function(locationdata){ + locationdata.should.have.property("lon", "-0.13"); + locationdata.should.have.property("lat", "51.51"); + } + before(function(done) { helper.startServer(done); if(nock){ @@ -85,6 +88,7 @@ describe('weather nodes', function() { //A trigger automatically happens when the node is deployed, so a single trigger here to receive different data is all that is neccessary. n3.on('input', function(msg) { var weatherdata = msg.payload; + var locationdata = msg.location; //Ensuring that two different outputs are received in N3 before finishing. if (changeTime === false){ weatherdata.should.have.property("weather", "Clouds"); @@ -94,9 +98,10 @@ describe('weather nodes', function() { done(); } weatherDataTest(weatherdata); + locationDataTest(locationdata); }); - n1.send({payload:{city:"test", country:"test"}}); + n1.send({location:{city:"test", country:"test"}}); }); }); @@ -121,7 +126,9 @@ describe('weather nodes', function() { //this ensures that the input function is only called once calledAlready = true; var weatherdata = msg.payload; + var locationdata = msg.location; weatherDataTest(weatherdata); + locationDataTest(locationdata); done(); }); //the node autotriggers for the first send, these triggers should all be ignored. @@ -157,7 +164,7 @@ describe('weather nodes', function() { done(); }); weatherNode1.should.have.property('id', 'weatherNode1'); - n1.send({payload:{lat: "fail", lon: "fail"}}); + n1.send({location:{lat: "fail", lon: "fail"}}); }); }); @@ -176,7 +183,9 @@ describe('weather nodes', function() { weatherNode1.should.have.property('id', 'weatherNode1'); n3.on('input', function(msg) { var weatherdata = msg.payload; + var locationdata = msg.location; weatherDataTest(weatherdata); + locationDataTest(locationdata); done(); }); @@ -198,7 +207,9 @@ describe('weather nodes', function() { weatherNode1.should.have.property('id', 'weatherNode1'); n3.on('input', function(msg) { var weatherdata = msg.payload; + var locationdata = msg.location; weatherDataTest(weatherdata); + locationDataTest(locationdata); done(); }); @@ -219,11 +230,13 @@ describe('weather nodes', function() { weatherNode1.should.have.property('id', 'weatherNode1'); n3.on('input', function(msg) { var weatherdata = msg.payload; + var locationdata = msg.location; weatherDataTest(weatherdata); + locationDataTest(locationdata); done(); }); - n1.send({payload:{lon:"-0.13", lat:"51.51"}}); + n1.send({location:{lon:"-0.13", lat:"51.51"}}); }); }); @@ -240,11 +253,13 @@ describe('weather nodes', function() { weatherNode1.should.have.property('id', 'weatherNode1'); n3.on('input', function(msg) { var weatherdata = msg.payload; + var locationdata = msg.location; weatherDataTest(weatherdata); + locationDataTest(locationdata); done(); }); - n1.send({payload:{city:"london", country:"england"}}); + n1.send({location:{city:"london", country:"england"}}); }); }); @@ -263,12 +278,14 @@ describe('weather nodes', function() { weatherNode1.should.have.property('id', 'weatherNode1'); n3.on('input', function(msg) { var weatherdata = msg.payload; + var locationdata = msg.location; weatherDataTest(weatherdata); + locationDataTest(locationdata); stub.restore(); done(); }); - n1.send({payload:{lat: "fail", lon: "fail", city:"london", country:"england"}}); + n1.send({location:{lat: "fail", lon: "fail", city:"london", country:"england"}}); }); }); @@ -293,12 +310,14 @@ describe('weather nodes', function() { weatherNode1.should.have.property('id', 'weatherNode1'); n3.on('input', function(msg) { var weatherdata = msg.payload; + var locationdata = msg.location; weatherDataTest(weatherdata); + locationDataTest(locationdata); stub.restore(); done(); }); - n1.send({payload:{lat: "fail", lon: "fail", city:"fail", country:"fail"}}); + n1.send({location:{city:"fail", country:"fail", lat: "fail", lon: "fail",}}); }); }); @@ -323,12 +342,14 @@ describe('weather nodes', function() { weatherNode1.should.have.property('id', 'weatherNode1'); n3.on('input', function(msg) { var weatherdata = msg.payload; + var locationdata = msg.location; weatherDataTest(weatherdata); + locationDataTest(locationdata); stub.restore(); done(); }); - n1.send({payload:{lat: "fail", lon: "fail", city:"fail", country:"fail"}}); + n1.send({location:{city:"fail", country:"fail", lat: "fail", lon: "fail"}}); }); }); @@ -348,7 +369,7 @@ describe('weather nodes', function() { }); weatherNode1.should.have.property('id', 'weatherNode1'); - n1.send({payload:{city:"fail", country:"fail"}}); + n1.send({location:{city:"fail", country:"fail"}}); }); }); diff --git a/weather/weather.html b/weather/weather.html index a5dd7cac..c8f63ea4 100644 --- a/weather/weather.html +++ b/weather/weather.html @@ -79,8 +79,8 @@
These can be passed in as settings on the node, or as the:
of the message input.
The node will always prioritise the parameters passed in when both of lat/lon or country/city are present.
@@ -97,12 +97,16 @@The node also sets:
+Weather data provided by openweathermap.org/
@@ -202,12 +206,15 @@The node also sets:
+Weather data provided by openweathermap.org/
diff --git a/weather/weather.js b/weather/weather.js index 0e03acd8..d868afaa 100644 --- a/weather/weather.js +++ b/weather/weather.js @@ -20,26 +20,30 @@ module.exports = function(RED) { //if there is data in the message input, it overwrites the node setting values. //If the data is erroneous or not there, the values remain the node settings. if(msg.payload){ + + } + if(msg.location){ //query node code to check the input for information. - if (msg.payload.city && msg.payload.country) { - city = msg.payload.city; - country = msg.payload.country; + if (msg.location.city && msg.location.country) { + city = msg.location.city; + country = msg.location.country; lat = ""; lon = ""; } - if (msg.payload.lat && msg.payload.lon){ - if(90 >= msg.payload.lat && 180 >= msg.payload.lon && msg.payload.lat >= -90 && msg.payload.lon >= -180){ - lat = msg.payload.lat; - lon = msg.payload.lon; + if (msg.location.lat && msg.location.lon){ + if(90 >= msg.location.lat && 180 >= msg.location.lon && msg.location.lat >= -90 && msg.location.lon >= -180){ + lat = msg.location.lat; + lon = msg.location.lon; city = ""; country = ""; } else { node.warn("Invalid lat/lon in input payload"); } - } + } } //wipe clear the payload if it exists, or create it if it doesn't msg.payload = {}; + msg.location = {}; //If there is a value missing, the URL is not initialised. if (lat && lon){ @@ -70,13 +74,11 @@ module.exports = function(RED) { msg.payload.windspeed = jsun.wind.speed; msg.payload.winddirection = jsun.wind.deg; msg.payload.location = jsun.name; - msg.payload.lon = jsun.coord.lon; - msg.payload.lat = jsun.coord.lat; msg.payload.sunrise = jsun.sys.sunrise; msg.payload.sunset = jsun.sys.sunset; msg.payload.clouds = jsun.clouds.all; - msg.lon = jsun.coord.lon; - msg.lat = jsun.coord.lat; + msg.location.lon = jsun.coord.lon; + msg.location.lat = jsun.coord.lat; msg.time = new Date(jsun.dt*1000); msg.payload.description = ("The weather in " + jsun.name + " at coordinates: " + jsun.coord.lat + ", " + jsun.coord.lon + " is " + jsun.weather[0].main + " (" + jsun.weather[0].description + ")." ); @@ -85,15 +87,15 @@ module.exports = function(RED) { if (jsun.message === "Error: Not found city"){ if (n.city && n.country && country != n.country && city != n.city){ node.warn("Invalid city/country in input payload, trying node city/country"); - msg.payload.country = n.country; - msg.payload.city = n.city; + msg.location.country = n.country; + msg.location.city = n.city; weatherPoll(node, n, msg, function(){ node.send(msg); }); } else if (n.lat && n.lon) { node.warn("Invalid city/country in input payload, trying node lat/lon"); - msg.payload.lat = n.lat; - msg.payload.lon = n.lon; + msg.location.lat = n.lat; + msg.location.lon = n.lon; weatherPoll(node, n, msg, function(){ node.send(msg); });