sumorobot-web/assets/js/sumorobot.js

102 lines
3.4 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
this.watchdogTimer = 0;
// 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
this.blocklyCode = "";
// 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-05-17 15:00:19 +00:00
$("#battery").removeClass("connected");
$("#battery").html("Disconnected");
}
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
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) {
console.log("INFO websocket disconnected");
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
2017-10-31 16:59:15 +00:00
var battery = JSON.parse(data)["battery_voltage"];
2018-07-08 18:51:45 +00:00
// When sensor data received
if (battery) {
$("#battery").html(battery + "V");
$("#battery").addClass("connected");
}
// 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) {
console.log("ERROR websocket error: " + err);
};
};
2018-07-08 18:51:45 +00:00
// Function to send WebSocket data
Sumorobot.prototype.send = function(msg, val) {
// 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-07-08 18:51:45 +00:00
if (val === 'undefined') {
console.log("no val")
this.websocket.send(`{"to": "sumo-${this.robotId}@00000514", "cmd": "${msg}"}`);
} else {
console.log("val")
this.websocket.send(`{"to": "sumo-${this.robotId}@00000514", "cmd": "${msg}", "val": "${val}"}`);
}
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-07-08 18:51:45 +00:00
// When a WebSocket connection exists
if (this.websocket !== 'undefined') {
// Close the WebSocket connection
this.websocket.close();
2017-09-25 09:37:41 +00:00
}
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;
};