diff --git a/web/socket/api.js b/web/socket/api.js
new file mode 100644
index 000000000..612495259
--- /dev/null
+++ b/web/socket/api.js
@@ -0,0 +1,54 @@
+function Api(v, connected, error)
+{
+ this.version = v
+ this.ids = new Object();
+
+ var url = wsUrl('v1');
+ var self = this;
+
+ this.connection = new WebSocket(url, 'api');
+
+ this.connection.onopen = function() {
+ console.log('API connected', this.url);
+ connected();
+ };
+
+ this.connection.onerror = function(e) {
+ console.log('API request failed:', e);
+ error(e);
+ };
+
+ this.connection.onmessage = function(e) {
+ var resp = JSON.parse(e.data);
+ var handler;
+
+ console.log('API response received', resp);
+
+ handler = self.ids[resp.id];
+ if (handler !== undefined) {
+ handler(resp.response);
+
+ delete self.ids[resp.id];
+ }
+ };
+}
+
+Api.prototype.request = function(action, request, handler)
+{
+ var req = {
+ action : action,
+ request: request,
+ id : guid()
+ };
+
+ this.ids[req.id] = handler;
+
+ console.log('API request sent', req);
+
+ this.connection.send(JSON.stringify(req))
+}
+
+Api.prototype.close = function()
+{
+ this.connection.close();
+}
\ No newline at end of file
diff --git a/web/socket/app.js b/web/socket/app.js
index cfdead3f2..5f6f0f702 100644
--- a/web/socket/app.js
+++ b/web/socket/app.js
@@ -1,11 +1,13 @@
// global variables
+var api;
var connection;
var timer;
-var seq = 0;
-
+var nodes = [];
var currentNode;
-var nodes = [ ];
+
+var paused = false;
+var sequence = 0;
var plotData = [];
var plotOptions = {
@@ -17,96 +19,145 @@ var plotOptions = {
}
};
-var xDelta = 0.5*1000;
-var xPast = xDelta*0.9;
-var xFuture = xDelta*0.1;
+var updateRate = 25;
+var redrawPlot = true;
-$(document).on('ready', function() {
- $.getJSON('/nodes.json', function(data) {
- nodes = data;
-
- for (var i = 0; i < nodes.length; i++)
- if (nodes[i].name == getParameterByName("node"))
- currentNode = nodes[i];
-
- if (currentNode === undefined)
- currentNode = nodes[0];
-
- nodes.forEach(function(node, index) {
- $(".node-selector").append(
- $("
").append(
- $("", {
- text: node.description ? node.description : node.name,
- title: node.name,
- href: "?node=" + node.name
- })
- ).addClass(node.name == currentNode.name ? 'active' : '')
- );
- });
-
- wsConnect(wsUrl(currentNode.name), ["live"]);
- });
+var xDelta = 5000;
+var xPast = xDelta * 0.9;
+var xFuture = xDelta * 0.1;
+
+$(document).ready(function() {
+ api = new Api('v1', apiConnected);
$('#play').click(function(e, ui) {
- wsConnect(wsUrl(currentNode.name), ["live"]);
+ connection = wsConnect(currentNode);
+ paused = false;
});
$('#pause').click(function(e, ui) {
connection.close();
- });
-
- $('#slider').slider({
- min : 0,
- max : 100,
- slide : function(e, ui) {
- var msg = new Msg({
- timestamp : Date.now(),
- sequence : seq++
- }, [ ui.value ]);
-
-
- var blob = msg.toArrayBuffer()
- connection.send(blob);
- }
+ paused = true;
});
$('#timespan').slider({
- min : 200,
+ min : 1000,
max : 10000,
value : xDelta,
slide : function(e, ui) {
- plotUpdateWindow(ui.value);
+ updatePlotWindow(ui.value);
}
});
- $('#controls .buttons button').each(function(button) {
- $(button).addClass('on');
-
- $(button).onClick(function(value) {
- var msg = new Msg({
- timestamp : Date.now(),
- sequence : seq++
- }, [ value ]);
-
- connection.send(msg.toArrayBuffer());
- });
+ $('#updaterate').slider({
+ min : 1,
+ max : 50,
+ value : updateRate,
+ slide : function(e, ui) {
+ clearInterval(timer);
+ timer = setInterval(updatePlot, 1000.0 / updateRate);
+ updateRate = ui.value;
+ }
+ });
+
+ $('.inputs #slider').slider({
+ min : 0,
+ max : 100,
+ slide : sendData
});
- plotUpdateWindow(10*1000); /* start plot refresh timer for 10sec window */
+ $('.inputs-checkboxes input').checkboxradio()
+ .each(function(idx, elm) {
+ $(elm).change(sendData);
+ });
+
+ timer = setInterval(updatePlot, 1000.0 / updateRate);
});
$(window).on('beforeunload', function() {
connection.close();
+ api.close();
});
-function plotUpdateWindow(delta) {
- xDelta = delta
- xPast = xDelta*0.9;
- xFuture = xDelta*0.1;
+function sendData()
+{
+ var slider = $('.inputs #slider');
+ var checkboxes = $('.inputs-checkboxes input');
+
+ var data = [ $(slider).slider('value'), 0 ];
+
+ for (var i = 0; i < checkboxes.length; i++)
+ data[1] += (checkboxes[i].checked ? 1 : 0) << i;
+
+ var msg = new Msg({
+ timestamp : Date.now(),
+ sequence : sequence++,
+ id : currentNode.id,
+ data : data
+ });
+
+ console.log('Sending message', msg);
+
+ connection.send(msg.toArrayBuffer());
}
-function plotUpdate() {
+function apiConnected()
+{
+ api.request('nodes', {},
+ function(response) {
+ nodes = response;
+
+ console.log("Found " + nodes.length + " nodes:", nodes);
+
+ for (var i = 0; i < nodes.length; i++)
+ if (nodes[i].name == getParameterByName('node'))
+ currentNode = nodes[i];
+
+ if (currentNode === undefined)
+ currentNode = nodes[0];
+
+ if (currentNode !== undefined) {
+ updateNodeList();
+
+ connection = wsConnect(currentNode);
+ }
+ }
+ );
+}
+
+function updateNodeList()
+{
+ $('.node-selector').empty();
+
+ nodes.forEach(function(node, index) {
+ if (node.type == 'websocket') {
+ $('.node-selector').append(
+ $('