diff --git a/sumorobot.js b/sumorobot.js index cd34224..19f3b66 100755 --- a/sumorobot.js +++ b/sumorobot.js @@ -9,13 +9,13 @@ var sumostart = false; var sumorobot = null; /* Blockly workspace */ var workspace = null; -/* last hightlighted block */ +/* last hightlighted block id */ var highlighted = ""; /* disable enable hotkeys */ var hotkeys = true; /* the sumorobot code */ var sumocode = ""; - +/* block highlight WebSocket */ var block_highlight = null; var Sumorobot = function(wsUri) { @@ -52,17 +52,12 @@ Sumorobot.prototype.connect = function() { /* when there is a message from the WebSocket */ this.websocket.onmessage = function(evt) { /* when scope is received */ - if (evt.data.length == 20) { - workspace.highlightBlock(evt.data); - highlighted = evt.data; - } else { - var data = evt.data.replace(/'/g, '"').toLowerCase(); - console.log(data); - var battery = JSON.parse(data)["battery_voltage"]; - battery = Math.round((((battery - 3.6) * 100) / (4.2 - 3.6))); - $("#battery").html(battery + ""); - $("#bar").addClass("connected"); - } + var data = evt.data.replace(/'/g, '"').toLowerCase(); + console.log(data); + var battery = JSON.parse(data)["battery_voltage"]; + battery = Math.round((((battery - 3.6) * 100) / (4.2 - 3.6))); + $("#battery").html(battery + ""); + $("#bar").addClass("connected"); }; /* when there is an WebSocket error */ this.websocket.onerror = function(err) { @@ -398,9 +393,7 @@ window.onload = function() { $(".btn-stop").click(function() { sumostart = false; sumorobot.send("stop"); - setTimeout(function() { - workspace.highlightBlock(highlighted, false); - }, 500); + workspace.highlightBlock(highlighted, false); }); /* remote control checkbox listener */ @@ -424,12 +417,13 @@ window.onload = function() { $(this).addClass("btn-selected"); /* update robot IDs in local storage */ setLocalStorageItem("sumorobot.robotID" + index, robotID); - /* connect to the selected robot channel */ + /* connect to the selected robots WebSocket */ sumorobot = new Sumorobot("ws://" + robotServer + ":80/p2p/browser/" + robotID + "/"); + /* connect to the other block highlight WebSocket */ block_highlight = new WebSocket("ws://" + robotServer + ":80/p2p/browser/" + robotID + "-highlight/"); /* when there is a message from the WebSocket */ block_highlight.onmessage = function(evt) { - /* when block id received and sumorobot is started */ + /* when scope is received */ if (evt.data.length == 20 && sumostart) { workspace.highlightBlock(evt.data); highlighted = evt.data;