Minot tweaks to MG_sensor_demo

Fix WS connect address
Only update graphs when connected

PUBLISHED_FROM=3a3c4e2d21595f48c811b45fb391f7709dc0bcf7
This commit is contained in:
Deomid Ryabkov authored and rojer committed 2016-04-19 13:16:28 +01:00
1 parent 56c687b39b
commit 7519a3765b
2 files changed
+26 -5

No files matched your search

+24 -5
View File
@@ -447,6 +447,8 @@ API.txt for details.
})(jQuery);
var ws = null;
var connected = false;
$(document).ready(function() {
var startTime = Date.now();
@@ -478,6 +480,7 @@ $(document).ready(function() {
};
var updateGraph = function(g) {
if (!connected) return;
var now = Date.now();
//if (now - g.lastUpdateTime < 50) return;
g.lastUpdateTime = now;
@@ -518,7 +521,7 @@ $(document).ready(function() {
]
},
temp: {
title: 'Temperature sensor',
title: 'Sensor die temperature',
plot: null,
lastUpdateTime: null,
data: [
@@ -533,10 +536,24 @@ $(document).ready(function() {
setInterval(function() { updateGraph(v); }, 100);
});
// Create Websocket connection. For simplicity, no reconnect logic is here.
//var ws = new WebSocket('ws://' + location.host);
var ws = new WebSocket('ws://192.168.1.54');
ws.onmessage = function(ev) {
function wsConnect() {
if (ws != null) return;
var wsAddr = 'ws://' + location.host;
console.log('Connecting to', wsAddr);
ws = new WebSocket(wsAddr);
ws.onopen = function() {
console.log('Connected');
connected = true;
};
ws.onclose = function() {
console.log('Disconnected');
connected = false;
ws = null;
};
ws.onmessage = handleWsMessage;
}
function handleWsMessage(ev) {
try {
var obj = JSON.parse(ev.data);
//var timestamp = startTime + parseInt(obj.ts * 1000);
@@ -565,4 +582,6 @@ $(document).ready(function() {
ws.send(JSON.stringify({ t: 1, v: state }));
});
wsConnect();
setInterval(wsConnect, 1000);
});