sumorobot-web/assets/js/sumorobot.js

103 lines
3.3 KiB
JavaScript
Raw Normal View History

2018-07-08 18:51:45 +00:00
// Sumorobot constructor
var Sumorobot = function(wsUri, robotId) {
// Assign the WebSocket URI
2017-09-25 09:37:41 +00:00
this.wsUri = wsUri;
2018-07-08 18:51:45 +00:00
// Assign the SumoRobot ID
this.robotId = robotId;
// To keep track of the WebSocket connection
2018-08-12 10:39:18 +00:00
this.watchdogCounter = 0;
2018-07-08 18:51:45 +00:00
// Timer to ping the SumoRobot
2017-10-02 19:41:10 +00:00
this.watchdogTimer = null;
2018-07-08 18:51:45 +00:00
// To store Blockly code
2018-08-12 10:39:18 +00:00
this.blocklyCode = '';
// If robot is moving
this.moving = false;
2018-07-08 18:51:45 +00:00
// Start connecting to the WebSocket
this.connect();
2017-09-25 09:37:41 +00:00
};
2018-07-08 18:51:45 +00:00
// Function to initiate the WebSocket connection
2017-09-25 09:37:41 +00:00
Sumorobot.prototype.connect = function() {
2018-07-08 18:51:45 +00:00
// To have access to this object inside events
2017-09-25 09:37:41 +00:00
var self = this;
this.websocket = new WebSocket(this.wsUri);
2018-07-08 18:51:45 +00:00
// Setup connection watchdog interval
2018-05-17 15:00:19 +00:00
setInterval(function() {
2018-07-08 18:51:45 +00:00
if (self.watchdogCounter == 0) {
2018-08-12 10:39:18 +00:00
$('#battery').removeClass('connected');
$('#battery').html('Disconnected');
2018-05-17 15:00:19 +00:00
}
2018-07-08 18:51:45 +00:00
// Reset watchdog counter
self.watchdogCounter = 0;
2018-05-17 15:00:19 +00:00
}, 3000);
2018-07-08 18:51:45 +00:00
// When the WebSocket gets connected
2017-09-25 09:37:41 +00:00
this.websocket.onopen = function(evt) {
2018-07-08 18:51:45 +00:00
// Send authentication packet
self.websocket.send(`{"setID": "browser-${self.robotId}@00000514", "passwd": "salakala"}`);
// Setup a timer to ping the robot
2017-10-02 19:41:10 +00:00
self.watchdogTimer = setInterval(function() {
2018-07-08 18:51:45 +00:00
// Send a ping to the robot
2018-08-12 10:39:18 +00:00
self.send(self.robotId, 'ping');
2018-04-08 17:46:36 +00:00
}, 500);
2017-09-25 09:37:41 +00:00
};
2018-07-08 18:51:45 +00:00
// When the WebSocket closes
2017-09-25 09:37:41 +00:00
this.websocket.onclose = function(evt) {
2018-07-08 18:51:45 +00:00
// Clear the pinging
2017-10-02 19:41:10 +00:00
clearInterval(self.watchdogTimer);
2018-07-08 18:51:45 +00:00
// Try to recnnect to the sumorobot
2017-09-25 09:37:41 +00:00
self.connect();
};
2018-07-08 18:51:45 +00:00
// When there is a message from the WebSocket
2017-09-25 09:37:41 +00:00
this.websocket.onmessage = function(evt) {
2018-07-08 18:51:45 +00:00
// When scope is received
2017-10-31 16:59:15 +00:00
var data = evt.data.replace(/'/g, '"').toLowerCase();
2018-07-08 18:51:45 +00:00
// Get SumoRobot battery voltage
2018-08-12 10:39:18 +00:00
var batVolt = JSON.parse(data)['battery_voltage'];
2018-07-08 18:51:45 +00:00
// When sensor data received
2018-08-12 10:39:18 +00:00
if (batVolt) {
$('#battery').html(batVolt + 'V');
$('#battery').addClass('connected');
2018-07-08 18:51:45 +00:00
}
// Count data received packets
self.watchdogCounter += 1;
2017-09-25 09:37:41 +00:00
};
2018-07-08 18:51:45 +00:00
// When there is an WebSocket error
2017-09-25 09:37:41 +00:00
this.websocket.onerror = function(err) {
2018-08-12 10:39:18 +00:00
console.log('ERROR websocket error: ' + err);
2017-09-25 09:37:41 +00:00
};
};
2018-07-08 18:51:45 +00:00
// Function to send WebSocket data
2018-08-12 10:39:18 +00:00
Sumorobot.prototype.send = function(cmd, val) {
if (cmd !== 'ping' && cmd !== 'stop') {
this.moving = true;
} else {
this.moving = false;
}
2018-07-08 18:51:45 +00:00
// Ready state constants: CONNECTING 0, OPEN 1, CLOSING 2, CLOSED 3
// https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
2017-09-25 09:37:41 +00:00
if (this.websocket.readyState == 1) {
2018-08-12 10:39:18 +00:00
if (val) {
this.websocket.send(`{"to": "sumo-${this.robotId}@00000514", "cmd": "${cmd}", "val": "${val}"}`);
2018-07-08 18:51:45 +00:00
} else {
2018-08-12 10:39:18 +00:00
this.websocket.send(`{"to": "sumo-${this.robotId}@00000514", "cmd": "${cmd}"}`);
2018-07-08 18:51:45 +00:00
}
2017-09-25 09:37:41 +00:00
}
};
2018-07-08 18:51:45 +00:00
// Function to close the WebSocket connection
2018-01-16 21:13:23 +00:00
Sumorobot.prototype.close = function() {
2018-08-12 10:39:18 +00:00
// Close the WebSocket connection
this.websocket.close();
2018-07-08 18:51:45 +00:00
};
2017-09-25 09:37:41 +00:00
2018-07-08 18:51:45 +00:00
// Function to get SumoRobot Blockly code
Sumorobot.prototype.getBlocklyCode = function() {
return this.blocklyCode;
};
2018-01-15 00:25:07 +00:00
2018-07-08 18:51:45 +00:00
// Function to set SumoRobot Blockly code
Sumorobot.prototype.setBlocklyCode = function(blocklyCode) {
this.blocklyCode = blocklyCode;
};