Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions test/weather/weather_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down Expand Up @@ -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");
Expand All @@ -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"}});
});
});

Expand All @@ -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.
Expand Down Expand Up @@ -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"}});
});
});

Expand All @@ -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();
});

Expand All @@ -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();
});

Expand All @@ -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"}});
});
});

Expand All @@ -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"}});
});
});

Expand All @@ -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"}});
});
});

Expand All @@ -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",}});
});
});

Expand All @@ -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"}});
});
});

Expand All @@ -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"}});
});
});

Expand Down
19 changes: 13 additions & 6 deletions weather/weather.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@
</ul>
<p>These can be passed in as settings on the node, or as the:</p>
<ul>
<li> <b>msg.payload.lat</b> and <b>msg.payload.lon</b>, or </li>
<li> <b>msg.payload.city</b> and <b>msg.payload.country</b> </li>
<li> <b>msg.location.lat</b> and <b>msg.location.lon</b>, or </li>
<li> <b>msg.location.city</b> and <b>msg.location.country</b> </li>
</ul>
<p>of the message input. </p>
<p>The node will always prioritise the parameters passed in when both of <b>lat/lon</b> or <b>country/city</b> are present.</p>
Expand All @@ -97,12 +97,16 @@
<li><b>windspeed</b> - the current wind speed at the location in Metres per second.</li><br>
<li><b>winddirection</b> - the current wind direction at the location in meteorological degrees.</li><br>
<li><b>town</b> - the name of the location from which the data was sourced.</li><br>
<li><b>lon</b> - the longitude of the location from which the data was sourced.</li><br>
<li><b>lat</b> - to the latitude of the location from which the data was sourced.</li><br>
<li><b>sunrise</b> - the time at which the sun rose in Unix UTC format.</li><br>
<li><b>sunset</b> - the time at which the sun will set in Unix UTC format.</li><br>
<li><b>clouds</b> - the current cloud coverage of the location in percent.</li><br>
</ul>

<p>The node also sets:</p>
<ul>
<li><b>msg.location.lat</b> - the longitude of the location from which the data was sourced.</li><br>
<li><b>msg.location.lon</b> - the latitude of the location from which the data was sourced.</li><br>
</ul>
<p>Weather data provided by <a href="http://openweathermap.org/" target="_blank">openweathermap.org/</a></p>
</script>

Expand Down Expand Up @@ -202,12 +206,15 @@
<li><b>windspeed</b> - the current wind speed at the location in Metres per second.</li><br>
<li><b>winddirection</b> - the current wind direction at the location in meteorological degrees.</li><br>
<li><b>town</b> - the name of the location from which the data was sourced.</li><br>
<li><b>lon</b> - the longitude of the location from which the data was sourced.</li><br>
<li><b>lat</b> - to the latitude of the location from which the data was sourced.</li><br>
<li><b>sunrise</b> - the time at which the sun rose in Unix UTC format.</li><br>
<li><b>sunset</b> - the time at which the sun will set in Unix UTC format.</li><br>
<li><b>clouds</b> - the current cloud coverage of the location in percent.</li><br>
</ul>
<p>The node also sets:</p>
<ul>
<li><b>msg.location.lat</b> - the longitude of the location from which the data was sourced.</li><br>
<li><b>msg.location.lon</b> - the latitude of the location from which the data was sourced.</li><br>
</ul>
<p>Weather data provided by <a href="http://openweathermap.org/" target="_blank">openweathermap.org/</a></p>
</script>

Expand Down
34 changes: 18 additions & 16 deletions weather/weather.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down Expand Up @@ -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 + ")." );
Expand All @@ -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);
});
Expand Down