mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-09 00:00:00 +01:00
some refactoring for static JS fronted
This commit is contained in:
parent
232acba2d3
commit
26ec26d7bb
3 changed files with 74 additions and 37 deletions
|
@ -2,7 +2,10 @@
|
|||
var connection;
|
||||
var timer;
|
||||
|
||||
var url = 'ws://134.130.169.31/test';
|
||||
var seq = 0;
|
||||
|
||||
var node = 'websocket'
|
||||
var url = 'ws://10.211.55.6:8080/' + node;
|
||||
var protocol = ['live'];
|
||||
|
||||
var plotData = [];
|
||||
|
@ -15,9 +18,15 @@ var plotOptions = {
|
|||
}
|
||||
};
|
||||
|
||||
var xDelta = 3*1000;
|
||||
var xDelta = 10*1000;
|
||||
|
||||
$(document).on('ready', function() {
|
||||
$.getJSON('/nodes.json', function(data) {
|
||||
$(data).each(function(index, node) {
|
||||
$(".node-selector").append($("<li>").text(node));
|
||||
});
|
||||
});
|
||||
|
||||
$('#slider').slider({
|
||||
min : 0,
|
||||
max : 100,
|
||||
|
@ -26,20 +35,19 @@ $(document).on('ready', function() {
|
|||
}
|
||||
});
|
||||
|
||||
connect();
|
||||
wsConnect();
|
||||
});
|
||||
|
||||
$(window).on('beforeunload', disconnect);
|
||||
$(window).on('beforeunload', wsDisconnect);
|
||||
|
||||
// update plot
|
||||
function update() {
|
||||
function plotUpdate() {
|
||||
var data = [];
|
||||
|
||||
// add data to arrays
|
||||
for (var i = 0; i < plotData.length; i++) {
|
||||
// remove old values
|
||||
while (plotData[i][0][0] < (Date.now() - xDelta))
|
||||
plotData[i].shift()
|
||||
//while (plotData[i].length > 0 && plotData[i][0][0] < (Date.now() - xDelta))
|
||||
// plotData[i].shift()
|
||||
|
||||
data[i] = {
|
||||
data : plotData[i],
|
||||
|
@ -58,11 +66,11 @@ function update() {
|
|||
}
|
||||
}
|
||||
|
||||
// update plot
|
||||
/* update plot */
|
||||
$.plot('#placeholder', data, $.extend(true, options, plotOptions));
|
||||
}
|
||||
|
||||
function disconnect() {
|
||||
function wsDisconnect() {
|
||||
connection.close();
|
||||
|
||||
$('#connectionStatus')
|
||||
|
@ -73,7 +81,7 @@ function disconnect() {
|
|||
clearInterval(timer);
|
||||
}
|
||||
|
||||
function connect() {
|
||||
function wsConnect() {
|
||||
connection = new WebSocket(url, protocol);
|
||||
|
||||
connection.onopen = function() {
|
||||
|
@ -81,13 +89,13 @@ function connect() {
|
|||
.text('Connected')
|
||||
.css('color', 'green');
|
||||
|
||||
timer = setInterval(update, 1.0 / 25);
|
||||
timer = setInterval(plotUpdate, 1.0 / 25);
|
||||
};
|
||||
|
||||
connection.onclose = function() {
|
||||
disconnect();
|
||||
wsDisconnect();
|
||||
|
||||
setTimeout(connect, 3000); // retry
|
||||
setTimeout(wsConnect, 3000); // retry
|
||||
};
|
||||
|
||||
connection.onerror = function(error) {
|
||||
|
@ -97,10 +105,11 @@ function connect() {
|
|||
};
|
||||
|
||||
connection.onmessage = function(e) {
|
||||
console.log(e.data);
|
||||
|
||||
var res = e.data.split(/\s+/).map(Number);
|
||||
var timestamp = res[0] * 1000;
|
||||
var values = res.slice(1)
|
||||
var data = [];
|
||||
|
||||
// add empty arrays for data
|
||||
while (plotData.length < values.length)
|
||||
|
@ -113,10 +122,28 @@ function connect() {
|
|||
};
|
||||
|
||||
function sendMsg(ts, values) {
|
||||
connection.send(ts / 1000 + " " + values.join(" "));
|
||||
connection.send(ts / 1000 + "(" + seq + ") " + values.join(" "));
|
||||
|
||||
seq += 1;
|
||||
}
|
||||
|
||||
/* Control event handlers */
|
||||
function onButtonClick(value) {
|
||||
sendMsg(Date.now, [ value ])
|
||||
};
|
||||
sendMsg(Date.now(), [ value ]);
|
||||
}
|
||||
|
||||
function onTextChange() {
|
||||
sendMsg(Date.now(), [ 1 ]);
|
||||
}
|
||||
|
||||
/* Helpers */
|
||||
function wsUrl(endpoint) {
|
||||
var l = window.location;
|
||||
|
||||
var protocol = (l.protocol === "https:") ? "wss://" : "ws://";
|
||||
var port = ((l.port != 80) && (l.port != 443)) ? ":" + l.port : "";
|
||||
|
||||
return protocol + l.hostname + port + endpoint;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1 +1 @@
|
|||
../../documentation/pictures/eonerc_logo.png
|
||||
../../doc/pictures/eonerc_logo.png
|
|
@ -20,31 +20,41 @@
|
|||
<h1>S2SS / WebSocket Mockup</h1>
|
||||
</div>
|
||||
<div id="container">
|
||||
<ul class="node-selector"></ul>
|
||||
|
||||
<div class="plot-container">
|
||||
<div id="placeholder" class="plot-placeholder"></div>
|
||||
</div>
|
||||
|
||||
|
||||
<p>
|
||||
Status: <span id="connectionStatus"></span>
|
||||
<button id="pause" onclick="disconnect()">Pause</button>
|
||||
<button id="pause" onclick="connect()">Play</button>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<span>Trigger:</span>
|
||||
<button onclick="onButtonClick(1)">On</button>
|
||||
<button onclick="onButtonClick(0)">Off</button>
|
||||
</p>
|
||||
<div class="controls">
|
||||
<p>
|
||||
<span>Trigger:</span>
|
||||
<button onclick="onButtonClick(1)">On</button>
|
||||
<button onclick="onButtonClick(0)">Off</button>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<span>Slider:</span>
|
||||
<div id="slider"></div>
|
||||
</p>
|
||||
<p>
|
||||
<span>Slider:</span>
|
||||
<div id="slider"></div>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<span>Value:</span>
|
||||
<input type="text" onchange="onTextChange()" />
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div class="connection">
|
||||
<span>Status:</span> <span id="connectionStatus"></span>
|
||||
<button id="pause" onclick="wsDisconnect()">Pause</button>
|
||||
<button id="pause" onclick="wsConnect()">Play</button>
|
||||
</div>
|
||||
</div>
|
||||
<div id="footer">
|
||||
<p>Copyright 2015: Institute for Automation of Complex Power Systems, EON Energy Research Center, RWTH Aachen University, Germany</p>
|
||||
<p>Authors: <a href="mailto:mgrigul@eonerc.rwth-aachen.de">Markus Grigul</a>, <a href="mailto:stvogel@eonerc.rwth-aachen.de">Steffen Vogel</a></p>
|
||||
<p>Copyright 2015: Institute for Automation of Complex Power Systems,
|
||||
EON Energy Research Center, RWTH Aachen University, Germany</p>
|
||||
<p>Authors: <a href="mailto:mgrigul@eonerc.rwth-aachen.de">Markus Grigul</a>,
|
||||
<a href="mailto:stvogel@eonerc.rwth-aachen.de">Steffen Vogel</a></p>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
|
|
Loading…
Add table
Reference in a new issue