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