/
SparkFunStream.agent.nut
87 lines (67 loc) · 2.3 KB
/
SparkFunStream.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
77
78
79
80
81
82
83
84
85
86
/******************** Library Code *************************/
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);
}
}
/******************** Application Code ********************/
// Create a Sparkfun Data Stream
const SPARKFUN_BASE = "data.sparkfun.com";
const SPARKFUN_PUBLIC_KEY = "";
const SPARKFUN_PRIVATE_KEY = "";
stream <- SparkFunStream(SPARKFUN_BASE, SPARKFUN_PUBLIC_KEY, SPARKFUN_PRIVATE_KEY);
//Syncronous Push:
local resp = stream.push({ temp = 102.5 });
server.log(format("PUSH: %i - %s", resp.statuscode, resp.body));
//Asyncronous Push:
stream.push({ temp = 103}, function(resp) {
server.log(format("PUSH: %i - %s", resp.statuscode, resp.body));
})
// Syncronous Get:
resp = stream.get();
server.log(resp.body);
// ASyncronous Get:
stream.get(function(resp) {
server.log(resp.body);
});
// Syncronous Clear:
resp = stream.clear();
server.log(format("CLEAR: %i - %s", resp.statuscode, resp.body));
// Asyncronous Clear:
stream.clear(function(resp) {
server.log(format("CLEAR: %i - %s", resp.statuscode, resp.body));
});