-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.nut
44 lines (37 loc) · 1.29 KB
/
agent.nut
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
38
39
40
41
42
43
44
#require "Wunderground.class.nut:1.0.0"
// Begin - Settings
const serverUrl = "http://yourhost.com";
const WUNDERGROUND_API_KEY = "xxxxxxxxxx";
const WUNDERGROUND_LOCATION = "NY/Albany";
// End - Settings
dataUrl <- serverUrl + "/api/message";
headers <- { "Content-Type" : "application/json" };
wunderground <- Wunderground(WUNDERGROUND_API_KEY, WUNDERGROUND_LOCATION);
function getWeather(data, callback) {
local currentTime = date().time;
local timeDiff = currentTime - data.timestamp;
// Only get weather if the imp data is fresh, otherwise weather will be outdated
if (timeDiff <= 300000) {
wunderground.getConditions(function(err, resp, wx) {
if (err == null) {
data.wx <- {};
data.wx.oat_f <- wx.temp_f;
data.wx.humidity <- wx.relative_humidity;
data.wx.pressure <- wx.pressure_mb;
}
callback(null, data);
});
} else {
// No weather
callback(null, data);
}
}
function postReading(reading) {
getWeather(reading, function(err, data) {
local json = http.jsonencode(data);
server.log(json);
local request = http.post(dataUrl, headers, json);
local response = request.sendsync();
});
}
device.on("reading", postReading);