add sensor data & connection termination

This commit is contained in:
Silver Kuusik 2018-08-15 19:58:07 +02:00
parent afa0b56db8
commit 657fc1a497

View File

@ -6,12 +6,19 @@ var Sumorobot = function(wsUri, robotId) {
this.robotId = robotId; this.robotId = robotId;
// To keep track of the WebSocket connection // To keep track of the WebSocket connection
this.watchdogCounter = 0; this.watchdogCounter = 0;
// Timer to ping the SumoRobot // When connection was intentionally closed
this.watchdogTimer = null; this.terminate = false;
// To store Blockly code // To store Blockly code
this.blocklyCode = ''; this.blocklyCode = '';
// If robot is moving // If robot is moving
this.moving = false; this.moving = false;
// Sensor data
this.sensors = {
'opponent': 99,
'line_left': 0,
'line_right': 0,
'battery_voltage': 0
};
// Start connecting to the WebSocket // Start connecting to the WebSocket
this.connect(); this.connect();
}; };
@ -22,8 +29,8 @@ Sumorobot.prototype.connect = function() {
var self = this; var self = this;
this.websocket = new WebSocket(this.wsUri); this.websocket = new WebSocket(this.wsUri);
// Setup connection watchdog interval // Setup connection watchdog interval
setInterval(function() { self.connectionTimer = setInterval(function() {
if (self.watchdogCounter == 0) { if (self.watchdogCounter == 0 && !self.terminate) {
$('#battery').removeClass('connected'); $('#battery').removeClass('connected');
$('#battery').html('Disconnected'); $('#battery').html('Disconnected');
} }
@ -42,20 +49,24 @@ Sumorobot.prototype.connect = function() {
}; };
// When the WebSocket closes // When the WebSocket closes
this.websocket.onclose = function(evt) { this.websocket.onclose = function(evt) {
// Clear the pinging // Clear the timers
clearInterval(self.watchdogTimer); clearInterval(self.watchdogTimer);
clearInterval(self.connectionTimer);
// Try to recnnect to the sumorobot // Try to recnnect to the sumorobot
// Only if the connection wasn't closed intentionally
if (!self.terminate) {
self.connect(); self.connect();
}
}; };
// When there is a message from the WebSocket // When there is a message from the WebSocket
this.websocket.onmessage = function(evt) { this.websocket.onmessage = function(evt) {
// When scope is received // When scope is received
var data = evt.data.replace(/'/g, '"').toLowerCase(); var data = evt.data.replace(/'/g, '"').toLowerCase();
// Get SumoRobot battery voltage // Get SumoRobot battery voltage
var batVolt = JSON.parse(data)['battery_voltage']; self.sensors = JSON.parse(data);
// When sensor data received // When sensor data received
if (batVolt) { if (self.sensors['battery_voltage']) {
$('#battery').html(batVolt + 'V'); $('#battery').html(self.sensors['battery_voltage'] + 'V');
$('#battery').addClass('connected'); $('#battery').addClass('connected');
} }
// Count data received packets // Count data received packets
@ -76,7 +87,7 @@ Sumorobot.prototype.send = function(cmd, val) {
} }
// Ready state constants: CONNECTING 0, OPEN 1, CLOSING 2, CLOSED 3 // Ready state constants: CONNECTING 0, OPEN 1, CLOSING 2, CLOSED 3
// https://developer.mozilla.org/en-US/docs/Web/API/WebSocket // https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
if (this.websocket.readyState == 1) { if (!this.terminate && this.websocket.readyState == 1) {
if (val) { if (val) {
this.websocket.send(`{"to": "sumo-${this.robotId}@00000514", "cmd": "${cmd}", "val": "${val}"}`); this.websocket.send(`{"to": "sumo-${this.robotId}@00000514", "cmd": "${cmd}", "val": "${val}"}`);
} else { } else {
@ -87,6 +98,7 @@ Sumorobot.prototype.send = function(cmd, val) {
// Function to close the WebSocket connection // Function to close the WebSocket connection
Sumorobot.prototype.close = function() { Sumorobot.prototype.close = function() {
this.terminate = true;
// Close the WebSocket connection // Close the WebSocket connection
this.websocket.close(); this.websocket.close();
}; };