-
Notifications
You must be signed in to change notification settings - Fork 0
/
electricityd.js
137 lines (127 loc) · 4.41 KB
/
electricityd.js
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
var re_watts = /<ch1><watts>(0*)(\d+)<\/watts><\/ch1>/;
var re_temp = /<tmpr> *([\-\d.]+)<\/tmpr>/;
var re_time = /<time>([\-\d:]+)<\/time>/;
var re_demand = /<rainforest[^>]* timestamp="(\d+)s"[^>]*>.*?<InstantaneousDemand>.*?<Demand>((?:0[xX])?[\dA-Fa-f]+)<\/Demand>.*?<Multiplier>((?:0[xX])?[\dA-Fa-f]+)<\/Multiplier>.*?<Divisor>((?:0[xX])?[\dA-Fa-f]+)<\/Divisor>.*?<\/InstantaneousDemand>.*?<\/rainforest>/;
var re_tempered = /<tempered[^>]*>.*?<temperature>([\-\d.]+)<\/temperature>.*?<relative_humidity>([\-\d.]+)<\/relative_humidity>.*?<\/tempered>/;
var global_epoch = 0;
window.onerror = function(message, source, lineno, colno, error) {
msg = source + ':' + lineno + ':' + colno + ':' + message;
$('#error').prepend($('<span>').text(msg + '\n'));
};
function parseHexInt(s) {
return parseInt(s, 16) >> 0;
}
function updateReadings(timestamp, epoch) {
if (epoch) {
if (epoch != global_epoch) return;
} else {
epoch = ++global_epoch;
}
$.get('log', {'ts': timestamp}, function(data) {
if (epoch != global_epoch) return;
output = '';
log = data['log'];
var logFilter = window.location.hash ? new RegExp(window.location.hash.slice(1), 'i') : null;
for (i in log) {
line = log[i];
var watts = re_watts.exec(line);
if (watts) {
$('#watts').text(watts[1].replace(/0/g, ' ') + watts[2]);
}
var temp = re_temp.exec(line);
if (temp) {
$('#temp').text(temp[1]);
}
var time = re_time.exec(line);
if (time) {
$('#time').text(time[1]);
}
var idemand = re_demand.exec(line);
if (idemand) {
var i = 0;
var timestamp = new Date(parseInt(idemand[++i]) * 1000);
$('#time').text(timestamp.toLocaleTimeString());
var demand = parseHexInt(idemand[++i]) *
parseHexInt(idemand[++i]) /
parseHexInt(idemand[++i]) * 1000;
$('#watts').text(' '.slice(demand.toFixed().length) + demand.toFixed());
}
var tempered = re_tempered.exec(line);
if (tempered) {
var i = 0;
var temp = parseFloat(tempered[++i]);
$('#temp').text(temp.toFixed(1));
var humid = parseFloat(tempered[++i]);
$('#humid').text(humid.toFixed(1));
}
if (window.location.hash && line.search(logFilter) > -1) {
output = line + output;
}
}
if (output) {
$('#log').prepend($('<span>').text(output));
}
setTimeout(updateReadings, data['delay'] * 1000, data['ts'], epoch);
}).fail(function(xhr, status, error) {
if (epoch != global_epoch) return;
msg = 'Updates paused, refresh to resume.';
if (error) {
msg = 'Request failed: ' + error + '. ' + msg
}
$('#error').prepend($('<span>').text(msg + '\n'));
});
}
function updateHistory() {
var units = ['h', 'd', 'm'];
for (var i in units) {
var unit = units[i];
var wrapper = new google.visualization.ChartWrapper({
chartType: 'LineChart',
containerId: 'chart_' + unit,
dataSourceUrl: 'hist.gviz?sensor=0&unit=' + unit,
// refreshInterval: 30, // requires tqrt=scriptInjection as part of url
options: {
backgroundColor: { fill: 'none' },
colors: ['black'],
hAxis: {
baselineColor: 'none',
gridlines: { color: 'none' },
},
legend: 'none',
theme: 'maximized',
vAxis: {
baselineColor: 'none',
gridlines: { color: 'none' },
minValue: 0,
title: 'kWh/' + unit,
},
},
});
google.visualization.events.addListener(wrapper, 'error', function(e) {
msg = 'History update failed, refresh to resume.';
if (e.message) {
error = 'Error in query: ' + e.message;
msg = 'Request failed: ' + error + '. ' + msg;
}
$('#error').prepend($('<span>').text(msg + '\n'));
});
wrapper.draw();
};
}
function loadModules() {
// Load the Visualization API, the corechart package and set a callback.
google.load('visualization', '1.0', {'packages':['corechart'], 'callback': updateHistory});
}
function initLoader() {
var script = document.createElement("script");
script.src = "//www.google.com/jsapi?callback=loadModules";
document.getElementsByTagName("head")[0].appendChild(script);
}
$(function() {
initLoader();
updateReadings();
window.addEventListener('hashchange', function() {
$('#log').text('');
updateReadings();
});
});