-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.nut
60 lines (45 loc) · 1.78 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const USERNAME = "secret";
const PASSWORD = "secret";
const URL = "https://my.automation.domain/measurement";
HEADERS <- {
"Authorization" : "Basic " + http.base64encode(USERNAME + ":" + PASSWORD),
"Content-Type": "application/json"
};
function postTemperatureAndHumidity(result) {
local data = { "type" : "temperature",
"value" : result.temperature,
"location" : "living_room" };
local body = http.jsonencode(data);
server.log(body);
local response = http.post(URL, HEADERS, body).sendsync();
server.log("Code: " + response.statuscode + ". Message: " + response.body);
data = { "type" : "humidity",
"value" : result.humidity,
"location" : "living_room" };
body = http.jsonencode(data);
server.log(body);
response = http.post(URL, HEADERS, body).sendsync();
server.log("Code: " + response.statuscode + ". Message: " + response.body);
}
function postLight(light) {
local data = { "type" : "light",
"value" : light,
"location" : "living_room" };
local body = http.jsonencode(data);
server.log(body);
local response = http.post(URL, HEADERS, body).sendsync();
server.log("Code: " + response.statuscode + ". Message: " + response.body);
}
function postPressure(pressure) {
local data = { "type" : "pressure",
"value" : pressure,
"location" : "living_room" };
local body = http.jsonencode(data);
server.log(body);
local response = http.post(URL, HEADERS, body).sendsync();
server.log("Code: " + response.statuscode + ". Message: " + response.body);
}
device.on("temperatureAndHumidity", postTemperatureAndHumidity);
device.on("light", postLight);
device.on("pressure", postPressure);
// TODO: Be able to trigger measurement reading remotely