From 13f4bb8346049ef01149659c3b6d7ea5f91e7fc6 Mon Sep 17 00:00:00 2001 From: Silver Kuusik Date: Tue, 31 Oct 2017 17:59:15 +0100 Subject: [PATCH] edit block highlighting --- sumorobot.js | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) 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;