|
@@ -0,0 +1,236 @@
|
|
1
|
+var arrSensor = Array();
|
|
2
|
+
|
|
3
|
+$(document).ready(function() {
|
|
4
|
+
|
|
5
|
+ $('body').append(`<div class="container-fluid">
|
|
6
|
+ <div class="page-header">
|
|
7
|
+ <h2>Systemadministration</h2>
|
|
8
|
+ </div>
|
|
9
|
+ <div class="row" id="contentDiv">
|
|
10
|
+ <div class="col-md-5 col-lg-5">
|
|
11
|
+ <div class="panel panel-primary">
|
|
12
|
+ <div class="panel-heading" id="listSensorsHeading" style="font-size: 18px;">Verfügbare Sensoren (0/0)</div>
|
|
13
|
+ <div class="panel-body">
|
|
14
|
+ <ul class="list-group" id="listSensors"></ul>
|
|
15
|
+ <div id="alertDiv"></div>
|
|
16
|
+ <button class="btn btn-primary" disabled="" id="btnSubmit">Weiter</button>
|
|
17
|
+ </div>
|
|
18
|
+ </div>
|
|
19
|
+ </div>
|
|
20
|
+ </div>
|
|
21
|
+ </div>
|
|
22
|
+ <div class="footer">
|
|
23
|
+ <div class="container-fluid">
|
|
24
|
+ <p class="text-muted">© Copyright 2016 by Christian Högerle und Thomas Buck</p>
|
|
25
|
+ </div>
|
|
26
|
+ </div>`);
|
|
27
|
+
|
|
28
|
+ // aktuelle Uhrzeit fuer die Graphen ermitteln
|
|
29
|
+ var actTime = new Date();
|
|
30
|
+ actTime = actTime.getHours() + ":" + (actTime.getMinutes() < 10 ? '0':'') + actTime.getMinutes();
|
|
31
|
+
|
|
32
|
+ $('#listSensorsHeading').empty();
|
|
33
|
+ $('#listSensorsHeading').html("Verfügbare Sensoren (0/" + clients.length + ")");
|
|
34
|
+
|
|
35
|
+ // Alle clients im Array iterieren und Websocket abfragen
|
|
36
|
+ var count = [0];
|
|
37
|
+ jQuery.each(clients, function(index, client) {
|
|
38
|
+ webSocket(client, "2391", count, clients.length);
|
|
39
|
+ });
|
|
40
|
+
|
|
41
|
+ // Submit-Button fuer die Formulareingabe
|
|
42
|
+ $("#btnSubmit").click(function(event) {
|
|
43
|
+ $('#contentDiv').empty();
|
|
44
|
+ generateView(arrSensor, actTime);
|
|
45
|
+ });
|
|
46
|
+});
|
|
47
|
+
|
|
48
|
+function webSocket(wsUri, wsPort, count, clientsCount) {
|
|
49
|
+ websocket = new WebSocket("ws://" + wsUri + ":" + wsPort + "/");
|
|
50
|
+ websocket.onopen = function(evt) {};
|
|
51
|
+ websocket.onclose = function(evt) {};
|
|
52
|
+ websocket.onmessage = function(evt) {
|
|
53
|
+ var jsonData = jQuery.parseJSON(evt.data);
|
|
54
|
+ count[0]++;
|
|
55
|
+ var sensor = {id: count[0], ip: wsUri, actualTemp: jsonData['T'], actualHum: jsonData['H']};
|
|
56
|
+ var arrEEPROM = Array();
|
|
57
|
+ jQuery.each(jsonData['EEPROM'], function(index, data) {
|
|
58
|
+ arrEEPROM.push(data);
|
|
59
|
+ });
|
|
60
|
+ sensor.arrEEPROM = arrEEPROM;
|
|
61
|
+ arrSensor.push(sensor);
|
|
62
|
+ $('#listSensorsHeading').html("Verfügbare Sensoren (" + sensor.id + "/" + clientsCount + ")");
|
|
63
|
+ $('#listSensors').append('<li class="list-group-item">' +
|
|
64
|
+ ' Sensor ' + sensor.id +
|
|
65
|
+ ' | IP: ' + sensor.ip +
|
|
66
|
+ ' | aktuelle Temperatur: ' + sensor.actualTemp +
|
|
67
|
+ ' | aktuelle Luftfeuchtigkeit: ' + sensor.actualHum +
|
|
68
|
+ '</li>');
|
|
69
|
+
|
|
70
|
+ // Alle Sensoren erfolgreich abgefragt
|
|
71
|
+ if(count[0] == clientsCount) {
|
|
72
|
+ $('#btnSubmit').prop("disabled", false);
|
|
73
|
+ }
|
|
74
|
+ };
|
|
75
|
+ websocket.onerror = function(evt) {
|
|
76
|
+ if($('#websocketError').length ) {
|
|
77
|
+ $('.alert-danger').append('Sensor mit der IP:' + wsUri + ' konnte nicht abgefragt werden! <br>');
|
|
78
|
+ } else {
|
|
79
|
+ $('#alertDiv').append('<div class="alert alert-danger" id="websocketError">' +
|
|
80
|
+ '<strong>Fehler:</strong><br>Sensor mit der IP:' + wsUri + ' konnte nicht abgefragt werden! <br>' +
|
|
81
|
+ '</div>');
|
|
82
|
+ }
|
|
83
|
+ console.log(evt.data);
|
|
84
|
+ };
|
|
85
|
+}
|
|
86
|
+
|
|
87
|
+function generateView(arrSensor, actTime) {
|
|
88
|
+ $('#contentDiv').append(`<div class="col-md-12 col-lg-12">
|
|
89
|
+ <div class="panel panel-primary">
|
|
90
|
+ <ul class="nav nav-pills">
|
|
91
|
+ <li class="active"><a class="navtab" data-toggle="tab" href="#home">Home</a></li>
|
|
92
|
+ </ul>
|
|
93
|
+ <div class="panel-body">
|
|
94
|
+ <div id="contentPanel">
|
|
95
|
+ </div>
|
|
96
|
+ </div>
|
|
97
|
+ </div>
|
|
98
|
+ </div>`);
|
|
99
|
+
|
|
100
|
+ jQuery.each(arrSensor, function(index, sensor) {
|
|
101
|
+ $('.nav-pills').append('<li><a class="navtab" data-toggle="tab" href="#' + sensor.id + '">Sensor ' + sensor.id + '</a></li>');
|
|
102
|
+ });
|
|
103
|
+
|
|
104
|
+ // Flag fuer gemeinsamer Graph -> true
|
|
105
|
+ generateGraph(true, arrSensor, actTime);
|
|
106
|
+
|
|
107
|
+ $(".navtab").click(function(event) {
|
|
108
|
+ $('#contentPanel').empty();
|
|
109
|
+ if(event.target.text == "Home") {
|
|
110
|
+ // Flag fuer gemeinsamer Graph -> true
|
|
111
|
+ generateGraph(true, arrSensor, actTime);
|
|
112
|
+ } else {
|
|
113
|
+ generateGraph(false, arrSensor[(event.target.text.split(" ")[1] - 1)], actTime);
|
|
114
|
+ }
|
|
115
|
+ });
|
|
116
|
+}
|
|
117
|
+
|
|
118
|
+function generateGraph(flag, sensor, actTime) {
|
|
119
|
+ $('#contentPanel').append(`<div class="row">
|
|
120
|
+ <div class="col-md-6 col-lg-6">
|
|
121
|
+ <canvas id="temperaturChart"></canvas>
|
|
122
|
+ </div>
|
|
123
|
+ <div class="col-md-6 col-lg-6">
|
|
124
|
+ <canvas id="humidityChart"></canvas>
|
|
125
|
+ </div>
|
|
126
|
+ </div>`);
|
|
127
|
+ if(flag) { // ein Graph für alle Sensoren
|
|
128
|
+ var length = 0;
|
|
129
|
+ jQuery.each(sensor, function(index, tmp) {
|
|
130
|
+ if(length < tmp.arrEEPROM.length) {
|
|
131
|
+ length = tmp.arrEEPROM.length;
|
|
132
|
+ }
|
|
133
|
+ });
|
|
134
|
+
|
|
135
|
+ var labels = Array();
|
|
136
|
+ actHour = actTime.split(":")[0];
|
|
137
|
+ for(var i = length; i > 0; i--) {
|
|
138
|
+ labels.unshift(actHour + ":00");
|
|
139
|
+ actHour = (actHour - 1).mod(24);
|
|
140
|
+ }
|
|
141
|
+ labels.push(actTime);
|
|
142
|
+
|
|
143
|
+ var dataTemperature = Array();
|
|
144
|
+ var dataHumidity = Array();
|
|
145
|
+
|
|
146
|
+ var tmpDataTemperature = Array();
|
|
147
|
+ var tmpDataHumidity = Array();
|
|
148
|
+ jQuery.each(sensor, function(index, tmp) {
|
|
149
|
+ for(var i = 0; i < (length - tmp.arrEEPROM.length); i++) {
|
|
150
|
+ tmpDataTemperature.push([]);
|
|
151
|
+ tmpDataHumidity.push([]);
|
|
152
|
+ }
|
|
153
|
+ jQuery.each(tmp.arrEEPROM, function(index, value) {
|
|
154
|
+ tmpDataTemperature.push(value['T']);
|
|
155
|
+ tmpDataHumidity.push(value['H']);
|
|
156
|
+ });
|
|
157
|
+ tmpDataTemperature.push(tmp.actualTemp);
|
|
158
|
+ tmpDataHumidity.push(tmp.actualHum);
|
|
159
|
+
|
|
160
|
+ dataTemperature.push({label: "Sensor " + tmp.id, data: tmpDataTemperature, fill: false,
|
|
161
|
+ borderWidth: 3, borderColor : getRandomColor(),});
|
|
162
|
+ dataHumidity.push({label: "Sensor " + tmp.id, data: tmpDataHumidity, fill: false,
|
|
163
|
+ borderWidth: 3, borderColor : getRandomColor(),});
|
|
164
|
+
|
|
165
|
+ tmpDataTemperature = [];
|
|
166
|
+ tmpDataHumidity = [];
|
|
167
|
+ });
|
|
168
|
+ } else { // Graph für einzelnen Sensor
|
|
169
|
+ var labels = Array();
|
|
170
|
+
|
|
171
|
+ var tmpDataTemperature = Array();
|
|
172
|
+ var tmpDataHumidity = Array();
|
|
173
|
+ actHour = actTime.split(":")[0];
|
|
174
|
+ actHour = (actHour - sensor.arrEEPROM.length).mod(24);
|
|
175
|
+ jQuery.each(sensor.arrEEPROM, function(index, value) {
|
|
176
|
+ actHour = (actHour + 1).mod(24);
|
|
177
|
+ labels.push(actHour + ":00");
|
|
178
|
+ tmpDataTemperature.push(value['T']);
|
|
179
|
+ tmpDataHumidity.push(value['H']);
|
|
180
|
+ });
|
|
181
|
+
|
|
182
|
+ labels.push(actTime);
|
|
183
|
+ tmpDataTemperature.push(sensor.actualTemp);
|
|
184
|
+ tmpDataHumidity.push(sensor.actualHum);
|
|
185
|
+
|
|
186
|
+ var dataTemperature = [{label: 'Temperatur [C]', data: tmpDataTemperature,
|
|
187
|
+ fill: false, borderWidth: 3, borderColor: '#337ab7',}];
|
|
188
|
+ var dataHumidity = [{label: 'Luftfeuchtigkeit [%RH]', data: tmpDataHumidity,
|
|
189
|
+ fill: false, borderWidth: 3, borderColor: '#337ab7',}];
|
|
190
|
+ }
|
|
191
|
+
|
|
192
|
+ var tempCtx = $('#temperaturChart');
|
|
193
|
+ var humCtx = $('#humidityChart');
|
|
194
|
+
|
|
195
|
+ var tempChart = new Chart(tempCtx, {
|
|
196
|
+ type: 'line',
|
|
197
|
+ data: {
|
|
198
|
+ labels: labels,
|
|
199
|
+ datasets: dataTemperature,
|
|
200
|
+ },
|
|
201
|
+ options: {
|
|
202
|
+ title: {
|
|
203
|
+ display: true,
|
|
204
|
+ text: 'Temperaturverlauf'
|
|
205
|
+ }
|
|
206
|
+ }
|
|
207
|
+ });
|
|
208
|
+
|
|
209
|
+ var humCharts = new Chart(humCtx, {
|
|
210
|
+ type: 'line',
|
|
211
|
+ data: {
|
|
212
|
+ labels: labels,
|
|
213
|
+ datasets: dataHumidity,
|
|
214
|
+ },
|
|
215
|
+ options: {
|
|
216
|
+ title: {
|
|
217
|
+ display: true,
|
|
218
|
+ text: 'Luftfeuchtigkeitverlauf'
|
|
219
|
+ }
|
|
220
|
+ }
|
|
221
|
+ });
|
|
222
|
+}
|
|
223
|
+
|
|
224
|
+// Modulo-Bug: http://javascript.about.com/od/problemsolving/a/modulobug.htm
|
|
225
|
+Number.prototype.mod = function(n) {
|
|
226
|
+ return ((this%n)+n)%n;
|
|
227
|
+}
|
|
228
|
+
|
|
229
|
+function getRandomColor() {
|
|
230
|
+ var letters = '0123456789ABCDEF'.split('');
|
|
231
|
+ var color = '#';
|
|
232
|
+ for (var i = 0; i < 6; i++ ) {
|
|
233
|
+ color += letters[Math.floor(Math.random() * 16)];
|
|
234
|
+ }
|
|
235
|
+ return color;
|
|
236
|
+}
|