Repository navigation
Expand file tree
/
Copy pathexample_connected_meters.js
More file actions
90 lines (74 loc) · 2.51 KB
/
Copy pathexample_connected_meters.js
File metadata and controls
90 lines (74 loc) · 2.51 KB
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
87
88
89
window.onload = function () {
var meterStatusData = bndry.dataSource.create('meter_status'),
subscription = null
;
/*
Processes the state field into a more useful form for drawing a bar graph over a time interval.
For the volume_1s query, the state key is the timestamp of the sample.
*/
function normalizeOctets (data, callback) {
var meterArray = new Array();
var i = 0;
for (var key in data.state)
{
if ( data.state.hasOwnProperty(key))
{
meterArray[i] = new Array();
if ( data.state[key]["connected"] == false )
{
meterArray[i] = data.state[key];
}
else
{
meterArray[i][0] = data.state[key]["exportAddress"];
meterArray[i][1] = data.state[key]["meterId"];
meterArray[i][2] = data.state[key]["connected"];
}
i = i + 1 ;
}
}
return meterArray;
}
function update(data) {
data = normalizeOctets(data);
var input = document.getElementById("myList");
var replacement = document.createElement('li');
replacement.id = "myList";
replacement.name = "myList";
for (var i in data )
{
if ( data[i][2] == true)
{
var node=document.createElement("LI");
var textnode=document.createTextNode("\nid:" + data[i][1]);
node.appendChild(textnode);
replacement.appendChild(node);
print("NODE CONNECTED: " + data[i][1]);
}
}
input.parentElement.replaceChild(replacement,input);
}
function subscribe() {
/* subscribe to the dataSource, passing the function to call when updates
are recieved from the Streaming API */
subscription = meterStatusData.addSubscriber(update);
document.getElementById('subscribe').disabled = true;
document.getElementById('unsubscribe').disabled = false;
}
function unsubscribe() {
// unsubscribe from the data source using the subscription id we stored earlier
meterStatusData.removeSubscriber(subscription);
subscription = null;
document.getElementById('subscribe').disabled = false;
document.getElementById('unsubscribe').disabled = true;
}
document.getElementById('subscribe').onclick = subscribe;
document.getElementById('unsubscribe').onclick = unsubscribe;
function log(msg) {
setTimeout(function() {
throw new Error(msg);
}, 0);
}
// kick everything off by subscribing the graph to the datasource
subscribe();
};