Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added location service #66

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
52 changes: 52 additions & 0 deletions weather/weather_insights.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down