diff --git a/assets/js/main.js b/assets/js/main.js index 7f144b6..3e04ab1 100755 --- a/assets/js/main.js +++ b/assets/js/main.js @@ -117,6 +117,12 @@ window.addEventListener('load', function() { var foundTrue = false; // Function to process code and highlight blocks and lines function processCode(index) { + + if(sumorobot.disconnected || !sumorobot.codeExecute) { + $('.btn-stop').click(); + return; + } + // When all lines are already processed if (lines.length == 0) return; // Split into code and block ID diff --git a/assets/js/sumorobot.js b/assets/js/sumorobot.js index e722d4a..0b3ccdb 100755 --- a/assets/js/sumorobot.js +++ b/assets/js/sumorobot.js @@ -8,10 +8,14 @@ var Sumorobot = function(wsUri, robotId) { this.watchdogCounter = 0; // When connection was intentionally closed this.terminate = false; + // when there is no connection with robot + this.disconnected = true; // To store Blockly code this.blocklyCode = ''; // If robot is moving this.moving = false; + + this.codeExecute = false; // Sensor data this.sensors = { 'opponent': 99, @@ -33,10 +37,11 @@ Sumorobot.prototype.connect = function() { self.connectionTimer = setInterval(function() { if (self.watchdogCounter == 0 && !self.terminate) { $("#battery img").attr("src", "assets/img/battery_disconnected.png"); + self.disconnected = true; } // Reset watchdog counter self.watchdogCounter = 0; - }, 3000); + }, 1500); // When the WebSocket gets connected this.websocket.onopen = function(evt) { // Setup a timer to ping the robot @@ -60,8 +65,11 @@ Sumorobot.prototype.connect = function() { this.websocket.onmessage = function(evt) { // When scope is received var data = evt.data.replace(/'/g, '"').toLowerCase(); + self.disconnected = false; + // Get SumoRobot battery voltage self.sensors = JSON.parse(data); + self.codeExecute = self.sensors.state; // When sensor data received if (self.sensors['battery_voltage']) { if (self.sensors['battery_voltage'] > 4.0) { @@ -85,6 +93,7 @@ Sumorobot.prototype.connect = function() { Sumorobot.prototype.send = function(cmd, val) { if (cmd !== 'ping' && cmd !== 'stop') { this.moving = true; + this.codeExecute = true; } else { this.moving = false; }