-
Notifications
You must be signed in to change notification settings - Fork 1
/
weather.js
37 lines (33 loc) · 1.17 KB
/
weather.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Weather Functionality
var weatherSetup = function(deviceClient) {
// Self Referentiation
var self = this;
// Variables for Limiting the Quantity of Weather Based Information Responses
var timesGetWeatherCalled = 0;
self.weatherIntervalID = 0;
// Function to publish forecast information to IOT Device
self.getWeather = function(request, response)
{
// Disconnect from Device after N iterations
if (timesGetWeatherCalled > 3)
{
clearInterval(self.weatherIntervalID);
timesGetWeatherCalled = 0;
}
else
{
console.log("WEATHER!!!");
var callURL = process.env.WEATHER_CALL_URL + "/api/weather/v1/geocode/" + response.lat1 + "/" + response.long1 + "/forecast/hourly/48hour.json?units=m&language=en-US";
request.get(callURL, {
json: true
},
function (error, response, body) {
console.log("forecast: " + JSON.stringify(body.forecasts[0]));
console.log("status", "json", JSON.stringify(body.forecasts[0].temp));
deviceClient.publish("status","json",'{"d":{"type" : "weather", "temp" : '+body.forecasts[0].temp+',"precip" : '+body.forecasts[0].precip_type+'}}',1);
});
}
++timesGetWeatherCalled;
}
}
module.exports = weatherSetup;