-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathtempbug.agent.nut
76 lines (56 loc) · 2.27 KB
/
tempbug.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// TempBug Example Agent Code
/* GLOBALS and CONSTANTS -----------------------------------------------------*/
const SPARKFUN_BASE = "data.sparkfun.com";
const SPARKFUN_PUBLIC_KEY = "YOUR PUBLIC KEY HERE";
const SPARKFUN_PRIVATE_KEY = "YOUR PRIVATE KEY HERE";
/* CLASS AND GLOBAL FUNCTION DEFINITIONS -------------------------------------*/
class SparkFunStream {
_baseUrl = null;
_publicKey = null;
_privateKey = null;
constructor(baseUrl, publicKey, privateKey) {
_baseUrl = baseUrl;
_privateKey = privateKey;
_publicKey = publicKey;
}
function push(data, cb = null) {
assert(typeof(data == "table"));
// add private key to table
data["private_key"] <- _privateKey;
local url = format("https://%s/input/%s?%s", _baseUrl, _publicKey, http.urlencode(data));
// make the request
local request = http.get(url);
if (cb == null) {
return request.sendsync();
}
request.sendasync(cb);
}
function get(cb = null) {
local url = format("https://%s/output/%s.json", _baseUrl, _publicKey);
local request = http.get(url);
if(cb == null) {
return request.sendsync();
}
return request.sendasync(cb);
}
function clear(cb = null) {
local url = format("https://%s/input/%s/clear", _baseUrl, _publicKey);
local headers = { "phant-private-key": _privateKey };
local request = http.httpdelete(url, headers);
if (cb == null) {
return request.sendsync();
}
return request.sendasync(cb);
}
}
/* REGISTER DEVICE CALLBACKS ------------------------------------------------*/
device.on("data", function(datapoint) {
local resp = stream.push({"temp": datapoint.temp});
server.log(format("PUSH: %i - %s", resp.statuscode, resp.body));
});
/* REGISTER HTTP HANDLER -----------------------------------------------------*/
// This agent does not need an HTTP handler
/* RUNTIME BEGINS HERE -------------------------------------------------------*/
server.log("TempBug Agent Running");
// instantiate our SparkFun client
stream <- SparkFunStream(SPARKFUN_BASE, SPARKFUN_PUBLIC_KEY, SPARKFUN_PRIVATE_KEY);