Commit 86ba7b9e authored by D1plo1d's avatar D1plo1d

Throttling the DOM writes of pronserve inspector and stopping dom writes...

Throttling the DOM writes of pronserve inspector and stopping dom writes entirely when the window looses focus.
parent ad4c7153
console.log("w00t!"); (function() {
var $console;
var $console; var windowFocus;
var connect = function() { $(window).focus(function() {
// Let us open a web socket windowFocus = true;
var url = "ws://localhost:8888/socket?user=admin&password=admin"; if ($console) $console.append("Window refocused, restarting log.\n");
console.log(url); $(".focus-lost-overlay").addClass("out").removeClass("in");
var ws = new WebSocket(url); }).blur(function() {
$(function () { windowFocus = false;
$consoleWrapper = $(".console"); if ($console) $console.append("Window's focus, lost stopping logging...\n");
$console = $(".console pre"); $(".focus-lost-overlay").addClass("in").removeClass("out");
$console.html("Connecting...") }.debounce());
onConnect(ws)
});
};
var updateSensors = function() { var connect = function() {
$(".sensors .val").each(function() { // Let us open a web socket
$(this).html($(this).data("val")||"xx.x"); var url = "ws://localhost:8888/socket?user=admin&password=admin";
}) console.log(url);
}.throttle(800); var ws = new WebSocket(url);
$(function () {
$consoleWrapper = $(".console");
$console = $(".console pre");
$console.html("Connecting...\n")
onConnect(ws)
});
};
var graph = null; var updateSensorsUi = function() {
var graphData = []; $(".sensors .val").each(function() {
var graphResolution = 40; $(this).html($(this).data("val")||"xx.x");
})
}.throttle(800);
var updateGraph = function(current) { var graph = null;
current.time = Date.now(); var graphData = [];
if(graphData.length == graphResolution) graphData.shift(); var graphResolution = 40;
graphData.push(current);
if(graph == null) var updateGraphData = function(current) {
{ current.time = Date.now();
graph = new Morris.Line({ if(graphData.length == graphResolution) graphData.shift();
// ID of the element in which to draw the chart. graphData.push(current);
element: "temperature-graph",
// Chart data records -- each entry in this array corresponds to a point on
// the chart.
data: graphData,
// The name of the data record attribute that contains x-values.
xkey: 'timestamp',
// A list of names of data record attributes that contain y-values.
ykeys: ['extruder', 'bed'],
// Labels for the ykeys -- will be displayed when you hover over the
// chart.
labels: ['extruder °C', 'bed °C'],
hideHover: 'always',
ymax: 'auto 250',
//pointSize: 0,
//parseTime: false,
xLabels: "decade"
});
} }
else
{ var updateGraphUi = function(current) {
graph.setData(graphData); if(graph == null)
{
graph = new Morris.Line({
// ID of the element in which to draw the chart.
element: "temperature-graph",
// Chart data records -- each entry in this array corresponds to a point on
// the chart.
data: graphData,
// The name of the data record attribute that contains x-values.
xkey: 'timestamp',
// A list of names of data record attributes that contain y-values.
ykeys: ['extruder', 'bed'],
// Labels for the ykeys -- will be displayed when you hover over the
// chart.
labels: ['extruder °C', 'bed °C'],
hideHover: 'always',
ymax: 'auto 250',
//pointSize: 0,
//parseTime: false,
xLabels: "decade"
});
}
else
{
graph.setData(graphData);
}
} }
}
var onConnect = function(ws) { var updateUi = function(msg) {
ws.onopen = function() if(windowFocus == false) return;
{ updateSensorsUi();
$console.append("\nConnected."); updateGraphUi();
// Web Socket is connected, send data using send() $console.append($console.data("toBeWritten"));
$console.data("toBeWritten", "");
$consoleWrapper.scrollTop($console.innerHeight());
}
}; var onConnect = function(ws) {
ws.onmessage = function (evt) ws.onopen = function()
{
msg = JSON.parse(evt.data)
if(msg.sensors != undefined)
{ {
var sensorNames = ["bed", "extruder"]; $console.append("Connected.\n");
var values = {timestamp: msg.timestamp}; // Web Socket is connected, send data using send()
for (var i = 0; i < sensorNames.length; i++)
};
ws.onmessage = function (evt)
{
msg = JSON.parse(evt.data)
if(msg.sensors != undefined)
{ {
var name = sensorNames[i]; var sensorNames = ["bed", "extruder"];
var val = parseFloat(msg.sensors[name]); var values = {timestamp: msg.timestamp};
values[name] = val; for (var i = 0; i < sensorNames.length; i++)
$("."+name+" .val").data("val", val.format(1)) {
var name = sensorNames[i];
var val = parseFloat(msg.sensors[name]);
values[name] = val;
$("."+name+" .val").data("val", val.format(1))
}
if(windowFocus)
{
var previous = $console.data("toBeWritten") || "";
$console.data("toBeWritten", previous + evt.data + "\n");
}
updateGraphData(values);
requestAnimationFrame(updateUi);
} }
updateSensors(); };
updateGraph(values); ws.onclose = function()
} {
$console.append("\n"+evt.data); // websocket is closed.
$consoleWrapper.scrollTop($console.innerHeight()); $console.append("\nConnection closed.");
};
}; };
ws.onclose = function()
{
// websocket is closed.
$console.append("\nConnection closed.");
};
};
if ("WebSocket" in window) if ("WebSocket" in window)
{ {
connect(); connect();
} }
else else
{ {
// The browser doesn't support WebSocket // The browser doesn't support WebSocket
alert("Error: WebSocket NOT supported by your Browser!"); alert("Error: WebSocket NOT supported by your Browser!");
} }
\ No newline at end of file })();
\ No newline at end of file
...@@ -9,8 +9,8 @@ ...@@ -9,8 +9,8 @@
<body> <body>
<div class="container"> <div class="container-fluid">
<div class="row"> <div class="row-fluid">
<div class="span12"> <div class="span12">
<h1> <h1>
Pronserve Inspector Pronserve Inspector
...@@ -38,6 +38,8 @@ ...@@ -38,6 +38,8 @@
</div> </div>
<div class="focus-lost-overlay modal-backdrop fade out"></div>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/js/bootstrap.min.js"></script> <script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sugar/1.3.9/sugar.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/sugar/1.3.9/sugar.min.js"></script>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment