From 69fef69bc53fd453ecc544b8fd9f67a02e0bb12d Mon Sep 17 00:00:00 2001 From: psyntium Date: Sat, 1 Apr 2017 12:09:22 +0800 Subject: [PATCH 1/3] added location service if msg.payload does not contain the latitude and longitude, it will try to use the location services to locate the latitude and longitude, assuming that the payload is a name of a city Signed-off-by: Syahrul Aiman --- weather/weather_insights.js | 52 +++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/weather/weather_insights.js b/weather/weather_insights.js index 111b039..d1d96f4 100644 --- a/weather/weather_insights.js +++ b/weather/weather_insights.js @@ -60,6 +60,58 @@ module.exports = function(RED) { geocode = [msg.location.lat, msg.location.lon].join('/'); } else if (config.geocode.match(lat_long_regex)) { geocode = config.geocode.replace(',', '/'); + } else if (typeof msg.payload === 'string' && msg.payload.trim() != "") { + + var request = require('request'); + var base_uri_v3 = '/api/weather/v3/location/'; + node.status({fill:"blue", shape:"dot", text:"getting location"}); + request({url: 'https://' + service_host + base_uri_v3 + "search", auth: {username: service_username, password: service_password}, qs: {query: msg.payload, language: language}}, function(error, response, body) { + node.status({}); + + if (error) { + node.error('Weather Insights service call failed with error HTTP response.', msg); + } else if (response.statusCode === 401) { + node.error('Weather Insights service call failure due to authentication failure.', msg); + } else if (response.statusCode === 404) { + node.error('Weather Insights service call failed due to HTTP 404 response to API call.', msg); + } else if (response.statusCode !== 200) { + node.error('Weather Insights service call failed due to non-200 HTTP response to API call.', msg); + } else { + var results = JSON.parse(body); + + if (results && results.location + && results.location.latitude && results.location.latitude.length > 0 + && results.location.longitude && results.location.longitude.length > 0) { + + geocode = results.location.latitude[0] + "/" + results.location.longitude[0]; + + node.status({fill:"blue", shape:"dot", text:"requesting"}); + request({url: 'https://' + service_host + base_uri + geocode + config.service, auth: {username: service_username, password: service_password}, qs: {units: config.units, language: language}}, function(error, response, body) { + node.status({}); + + if (error) { + node.error('Weather Insights service call failed with error HTTP response.', msg); + } else if (response.statusCode === 401) { + node.error('Weather Insights service call failure due to authentication failure.', msg); + } else if (response.statusCode === 404) { + node.error('Weather Insights service call failed due to HTTP 404 response to API call.', msg); + } else if (response.statusCode !== 200) { + node.error('Weather Insights service call failed due to non-200 HTTP response to API call.', msg); + } else { + var results = JSON.parse(body); + msg.forecasts = results.forecasts; + msg.observation = results.observation; + msg.observations = results.observations; + node.send(msg); + } + }); + return; + } else { + node.error('Weather Insights service could not find the location.', msg); + } + } + + }); } else { var message2 = 'Missing valid latlong parameters on either msg.payload, msg.location or node config.'; node.error(message2, msg); From 39ac53ad35f0bd26c1fd91a2f9d3b56f878308a9 Mon Sep 17 00:00:00 2001 From: psyntium Date: Mon, 3 Apr 2017 03:38:31 +0800 Subject: [PATCH 2/3] refactored error messages --- weather/weather_insights.js | 55 +++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 30 deletions(-) diff --git a/weather/weather_insights.js b/weather/weather_insights.js index d1d96f4..8b1bf47 100644 --- a/weather/weather_insights.js +++ b/weather/weather_insights.js @@ -16,6 +16,7 @@ module.exports = function(RED) { var cfenv = require('cfenv'); + var request = require('request'); var services = cfenv.getAppEnv().services; var username, password, host, base_uri = '/api/weather/v1/geocode/'; var service; @@ -60,22 +61,15 @@ module.exports = function(RED) { geocode = [msg.location.lat, msg.location.lon].join('/'); } else if (config.geocode.match(lat_long_regex)) { geocode = config.geocode.replace(',', '/'); - } else if (typeof msg.payload === 'string' && msg.payload.trim() != "") { - - var request = require('request'); + } else if (typeof msg.payload === 'string' && msg.payload.trim() != "") { var base_uri_v3 = '/api/weather/v3/location/'; node.status({fill:"blue", shape:"dot", text:"getting location"}); request({url: 'https://' + service_host + base_uri_v3 + "search", auth: {username: service_username, password: service_password}, qs: {query: msg.payload, language: language}}, function(error, response, body) { node.status({}); - - if (error) { - node.error('Weather Insights service call failed with error HTTP response.', msg); - } else if (response.statusCode === 401) { - node.error('Weather Insights service call failure due to authentication failure.', msg); - } else if (response.statusCode === 404) { - node.error('Weather Insights service call failed due to HTTP 404 response to API call.', msg); - } else if (response.statusCode !== 200) { - node.error('Weather Insights service call failed due to non-200 HTTP response to API call.', msg); + + var errorMsg = getErrorMsg(error, response.statusCode); + if (errorMsg) { + node.error(errorMsg, msg); } else { var results = JSON.parse(body); @@ -89,14 +83,9 @@ module.exports = function(RED) { request({url: 'https://' + service_host + base_uri + geocode + config.service, auth: {username: service_username, password: service_password}, qs: {units: config.units, language: language}}, function(error, response, body) { node.status({}); - if (error) { - node.error('Weather Insights service call failed with error HTTP response.', msg); - } else if (response.statusCode === 401) { - node.error('Weather Insights service call failure due to authentication failure.', msg); - } else if (response.statusCode === 404) { - node.error('Weather Insights service call failed due to HTTP 404 response to API call.', msg); - } else if (response.statusCode !== 200) { - node.error('Weather Insights service call failed due to non-200 HTTP response to API call.', msg); + var errorMsg = getErrorMsg(error, response.statusCode); + if (errorMsg) { + node.error(errorMsg, msg); } else { var results = JSON.parse(body); msg.forecasts = results.forecasts; @@ -118,20 +107,13 @@ module.exports = function(RED) { return; } - var request = require('request'); - node.status({fill:"blue", shape:"dot", text:"requesting"}); request({url: 'https://' + service_host + base_uri + geocode + config.service, auth: {username: service_username, password: service_password}, qs: {units: config.units, language: language}}, function(error, response, body) { node.status({}); - if (error) { - node.error('Weather Insights service call failed with error HTTP response.', msg); - } else if (response.statusCode === 401) { - node.error('Weather Insights service call failure due to authentication failure.', msg); - } else if (response.statusCode === 404) { - node.error('Weather Insights service call failed due to HTTP 404 response to API call.', msg); - } else if (response.statusCode !== 200) { - node.error('Weather Insights service call failed due to non-200 HTTP response to API call.', msg); + var errorMsg = getErrorMsg(error, response.statusCode); + if (errorMsg) { + node.error(errorMsg, msg); } else { var results = JSON.parse(body); msg.forecasts = results.forecasts; @@ -141,6 +123,19 @@ module.exports = function(RED) { } }); }); + + function getErrorMsg(error, statusCode) { + if (error) { + return 'Weather Insights service call failed with error HTTP response.'; + } else if (statusCode === 401) { + return 'Weather Insights service call failure due to authentication failure.'; + } else if (statusCode === 404) { + return 'Weather Insights service call failed due to HTTP 404 response to API call.'; + } else if (statusCode !== 200) { + return 'Weather Insights service call failed due to non-200 HTTP response to API call.'; + } + return false; + } } RED.nodes.registerType("weather_insights",Node, { From 2b0384073389ac23ef98d350e78dd525dea67330 Mon Sep 17 00:00:00 2001 From: psyntium Date: Mon, 3 Apr 2017 03:45:03 +0800 Subject: [PATCH 3/3] updated node info on using city as payload --- weather/weather_insights.html | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/weather/weather_insights.html b/weather/weather_insights.html index 7b01161..97db803 100644 --- a/weather/weather_insights.html +++ b/weather/weather_insights.html @@ -48,7 +48,7 @@
- +
@@ -66,10 +66,10 @@