add connection watchdog

This commit is contained in:
Silver Kuusik 2018-05-17 18:00:19 +03:00 committed by GitHub
parent 93dc55d833
commit a33ebbf72b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 0 deletions

View File

@ -24,6 +24,8 @@ var codingEnabled = false;
/* disable / enable live stream */
var liveStreamVisible = false;
/* connection watchdog */
var watchdogCounter = 0;
/* control_if block id */
var controlBlockId = "";
/* last hightlighted block id */
@ -44,6 +46,15 @@ Sumorobot.prototype.connect = function() {
/* to have access to this object */
var self = this;
this.websocket = new WebSocket(this.wsUri);
/* setup connection watchdog */
setInterval(function() {
if (watchdogCounter == 0) {
$("#battery").removeClass("connected");
$("#battery").html("Disconnected");
}
/* reset watchdog counter */
watchdogCounter = 0;
}, 3000);
/* when the WebSocket gets connected */
this.websocket.onopen = function(evt) {
console.log("INFO websocket connected");
@ -70,6 +81,9 @@ Sumorobot.prototype.connect = function() {
var battery = JSON.parse(data)["battery_voltage"];
$("#battery").html(battery + "V");
$("#battery").addClass("connected");
/* keep track of data packets */
/* to see if we have a connection to the robot */
watchdogCounter += 1;
};
/* when there is an WebSocket error */
this.websocket.onerror = function(err) {