This repository has been archived by the owner on Apr 5, 2023. It is now read-only.
/
graph.js
305 lines (290 loc) · 11.3 KB
/
graph.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
The MIT License (MIT)
Copyright (c) 2016 Markus Gebhard <markus.gebhard@web.de>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
"use strict";
var GraphCtrl = function($scope) {
$scope.debug = false;
$scope.alerts = [];
$scope.closeAlert = function(index) {
$scope.alerts.splice(index, 1);
};
$scope.selCollapsed = false;
// the FLM port configuration
var flx;
// the Kube configuration
var kube;
// link to the web server's IP address for MQTT socket connection
var client;
var reconnectTimeout = 2e3;
// the FLM's web socket port from mosquitto
var broker = location.hostname;
var port = 8083;
// prepare graph display
// the received values
var series = new Array(), sensors = {};
// the selected series to show
var selSeries = new Array();
var color = 0;
var flotOptions = {
series: {
lines: {
show: true,
steps: true
},
points: {
show: false
}
},
grid: {
hoverable: true
},
xaxis: {
mode: "time",
timezone: "browser"
},
yaxis: {
min: 0
}
};
// the web socket connect function
function mqttConnect() {
var wsID = "FLM" + parseInt(Math.random() * 100, 10);
client = new Paho.MQTT.Client(broker, port, "", wsID);
var options = {
timeout: 3,
onSuccess: onConnect,
onFailure: function(message) {
setTimeout(mqttConnect, reconnectTimeout);
}
};
// define callback routines
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
client.connect(options);
}
// event handler on connection established
function onConnect() {
client.subscribe("/device/+/config/flx");
client.subscribe("/device/+/config/kube");
client.subscribe("/device/+/config/sensor");
client.subscribe("/sensor/+/gauge");
}
// event handler on connection lost
function onConnectionLost(responseObj) {
setTimeout(mqttConnect, reconnectTimeout);
if (responseObj.errorCode !== 0) console.log("onConnectionLost:" + responseObj.errorMessage);
}
// handle the received message
function onMessageArrived(mqttMsg) {
// split the received message at the slashes
var topic = mqttMsg.destinationName.split("/");
var payload = mqttMsg.payloadString;
// the sensor message type is the third value of the topic
switch (topic[1]) {
case "device":
handle_device(topic, payload);
break;
case "sensor":
handle_sensor(topic, payload);
break;
default:
break;
}
}
// handler for device configuration
function handle_device(topic, payload) {
var config = JSON.parse(payload);
switch (topic[4]) {
case "flx":
flx = config;
break;
case "kube":
kube = config;
break;
case "sensor":
for (var obj in config) {
var cfg = config[obj];
if (cfg.enable == "1") {
if (sensors[cfg.id] === undefined) sensors[cfg.id] = new Object();
sensors[cfg.id].id = cfg.id;
if (cfg.port !== undefined) sensors[cfg.id].port = cfg.port[0];
if (cfg.type !== undefined) sensors[cfg.id].type = cfg.type;
if (cfg.subtype !== undefined) sensors[cfg.id].subtype = cfg.subtype;
if (flx !== undefined) {
if (flx[cfg.port] !== undefined) {
sensors[cfg.id].name = flx[cfg.port].name;
if (cfg.subtype !== undefined) sensors[cfg.id].name = sensors[cfg.id].name + " " + cfg.subtype;
}
}
if (kube !== undefined && cfg.kid !== undefined) {
sensors[cfg.id].name = kube[cfg.kid].name + " " + cfg.type;
sensors[cfg.id].kid = cfg.kid;
}
}
}
break;
}
}
// handler for sensor readings
function handle_sensor(topic, payload) {
var sensor = {};
var msgType = topic[3];
var sensorId = topic[2];
if (sensors[sensorId] === undefined) {
sensors[sensorId] = new Object();
sensor.id = sensorId;
sensor.name = sensorId;
} else sensor = sensors[sensorId];
sensor[sensorId] = sensor;
var value = JSON.parse(payload);
// restrict output to Watts only
if (value.length != 3) return;
if (value[2] !== "W") return;
var now = new Date().getTime();
var diff = 0;
// now compute the gauge
switch (msgType) {
case "gauge":
if (value.length === undefined) {
sensor.gaugetimestamp = now;
sensor.gaugevalue = value;
sensor.gaugeunit = "";
} else {
switch (value.length) {
case 1:
sensor.gaugetimestamp = now;
sensor.gaugevalue = value[0];
sensor.gaugeunit = "";
break;
case 2:
sensor.gaugetimestamp = now;
sensor.gaugevalue = value[0];
sensor.gaugeunit = value[1];
break;
case 3:
// check time difference of received value to current time
// this is due to pulses being send on occurance, so potentially outdated
diff = now / 1e3 - value[0];
// flot.time requires UTC-like timestamps;
// see https://github.com/flot/flot/blob/master/API.md#time-series-data
sensor.gaugetimestamp = value[0] * 1e3;
sensor.gaugevalue = value[1];
sensor.gaugeunit = value[2];
break;
default:
break;
}
// check if current sensor was already registered
var obj = series.filter(function(o) {
return o.label == sensor.name;
});
// if time difference is too large, skip gauge
if (diff > 100) break;
// ...if current sensor does not exist yet, register it
if (obj[0] === undefined) {
obj = {};
obj.label = sensor.name;
obj.data = [ sensor.gaugetimestamp, sensor.gaugevalue ];
obj.color = color;
color++;
series.push(obj);
// add graph select option
$("#choices").append("<div class='checkbox'>" + "<small><label>" + "<input type='checkbox' id='" + sensor.name + "' checked='checked'></input>" + sensor.name + "</label></small>" + "</div>");
} else {
obj[0].data.push([ sensor.gaugetimestamp, sensor.gaugevalue ]);
// move out values older than 5 minutes
var limit = parseInt(obj[0].data[0]);
diff = (sensor.gaugetimestamp - limit) / 1e3;
if (diff > 300) {
var selGraph = new Array();
for (var i in series) {
var selObj = {};
selObj.label = series[i].label;
selObj.data = series[i].data.filter(function(v) {
return v[0] > limit;
});
selObj.color = series[i].color;
if (selObj.data != null) selGraph.push(selObj);
}
series = selGraph;
}
}
}
break;
default:
break;
}
// check the selected checkboxes
selSeries = [];
$("#choices").find("input:checked").each(function() {
var key = $(this).attr("id");
var s = series.filter(function(o) {
return o.label == key;
});
selSeries.push(s[0]);
});
// plot the selection
// compute graph boundaries
var width = $("#graphpanel").width();
var height = width * 3 / 4;
height = height > 600 ? 600 : height;
$("#graph").width(width).height(height);
if ($("#graph").length) $.plot("#graph", selSeries, flotOptions);
// and store the sensor configuration
sensors[sensorId] = sensor;
}
// the jquery related graph handling
$(function() {
// allow tooltip on datapoints
$("<div id='tooltip'></div>").css({
position: "absolute",
display: "none",
border: "1px solid #ccc",
padding: "2px",
opacity: .9
}).appendTo("body");
// assign hover function
$("#graph").on("plothover", function(event, pos, item) {
if (item) {
var itemTime = new Date(item.datapoint[0]);
var hrs = itemTime.getHours();
hrs = hrs < 10 ? "0" + hrs : hrs;
var min = itemTime.getMinutes();
min = min < 10 ? "0" + min : min;
var sec = itemTime.getSeconds();
sec = sec < 10 ? "0" + sec : sec;
var unit = "";
for (var s in sensors) {
if (sensors[s].name == item.series.label) {
unit = sensors[s].gaugeunit;
break;
}
}
$("#tooltip").html(item.series.label + " (" + hrs + ":" + min + ":" + sec + "): " + item.datapoint[1] + " " + unit).css({
top: item.pageY + 7,
left: item.pageX + 5
}).fadeIn(200);
} else $("#tooltip").hide();
});
});
mqttConnect();
};
// the part of the AngularJS application that handles the graph
GraphCtrl.$inject = [ "$scope" ];
angular.module("flmUiApp").controller("GraphCtrl", GraphCtrl);