sumorobot-web/assets/js/sumorobot.js
2018-08-15 20:18:56 +02:00

105 lines
3.5 KiB
JavaScript
Executable File

// Sumorobot constructor
var Sumorobot = function(wsUri, robotId) {
// Assign the WebSocket URI
this.wsUri = wsUri;
// Assign the SumoRobot ID
this.robotId = robotId;
// To keep track of the WebSocket connection
this.watchdogCounter = 0;
// When connection was intentionally closed
this.terminate = false;
// To store Blockly code
this.blocklyCode = '';
// If robot is moving
this.moving = false;
// Sensor data
this.sensors = {
'opponent': 99,
'line_left': 0,
'line_right': 0,
'battery_voltage': 0
};
// Start connecting to the WebSocket
this.connect();
};
// Function to initiate the WebSocket connection
Sumorobot.prototype.connect = function() {
// To have access to this object inside events
var self = this;
this.websocket = new WebSocket(this.wsUri);
// Setup connection watchdog interval
self.connectionTimer = setInterval(function() {
if (self.watchdogCounter == 0 && !self.terminate) {
$('#battery').removeClass('connected');
$('#battery').html('Disconnected');
}
// Reset watchdog counter
self.watchdogCounter = 0;
}, 3000);
// When the WebSocket gets connected
this.websocket.onopen = function(evt) {
// Send authentication packet
self.websocket.send(`{"setID": "browser-${self.robotId}@00000514", "passwd": "salakala"}`);
// Setup a timer to ping the robot
self.watchdogTimer = setInterval(function() {
// Send a ping to the robot
self.send(self.robotId, 'ping');
}, 500);
};
// When the WebSocket closes
this.websocket.onclose = function(evt) {
// Clear the timers
clearInterval(self.watchdogTimer);
clearInterval(self.connectionTimer);
// Try to recnnect to the sumorobot
// Only if the connection wasn't closed intentionally
if (!self.terminate) {
self.connect();
}
};
// When there is a message from the WebSocket
this.websocket.onmessage = function(evt) {
// When scope is received
var data = evt.data.replace(/'/g, '"').toLowerCase();
// Get SumoRobot battery voltage
self.sensors = JSON.parse(data);
// When sensor data received
if (self.sensors['battery_voltage']) {
$('#battery').html(self.sensors['battery_voltage'] + 'V');
$('#battery').addClass('connected');
}
// Count data received packets
self.watchdogCounter += 1;
};
// When there is an WebSocket error
this.websocket.onerror = function(err) {
console.log('ERROR websocket error: ' + err);
};
};
// Function to send WebSocket data
Sumorobot.prototype.send = function(cmd, val) {
if (cmd !== 'ping' && cmd !== 'stop') {
this.moving = true;
} else {
this.moving = false;
}
// Ready state constants: CONNECTING 0, OPEN 1, CLOSING 2, CLOSED 3
// https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
if (!this.terminate && this.websocket.readyState == 1) {
if (val) {
this.websocket.send(`{"to": "sumo-${this.robotId}@00000514", "cmd": "${cmd}", "val": "${val}"}`);
} else {
this.websocket.send(`{"to": "sumo-${this.robotId}@00000514", "cmd": "${cmd}"}`);
}
}
};
// Function to close the WebSocket connection
Sumorobot.prototype.close = function() {
this.terminate = true;
// Close the WebSocket connection
this.websocket.close();
};