sumorobot-web/assets/js/main.js

176 lines
6.0 KiB
JavaScript
Raw Normal View History

2018-07-08 18:51:59 +00:00
// The local/remote server URL
2018-08-12 10:37:57 +00:00
//var ROBOT_SERVER = '10.42.0.1';
var ROBOT_SERVER = 'ws.achex.ca:4010';
2018-07-08 18:51:59 +00:00
2018-08-12 10:37:57 +00:00
var workspace;
2018-07-08 18:51:59 +00:00
// The sumorobot object
2018-08-12 10:37:57 +00:00
var sumorobot;
2018-07-08 18:51:59 +00:00
// Disable / enable coding mode
var codingEnabled = false;
// Disable / enable live stream
var liveStreamVisible = false;
2018-08-12 10:37:57 +00:00
window.addEventListener('load', function() {
// Set the robot ID from the localstorage
$('#robot-id').val(getLocalStorageItem('sumorobot.robotId'));
2018-07-08 18:51:59 +00:00
// Key down event
$(document).keydown(function(e) {
// When the alt key is not pressed, don't process hotkeys
if (e.altKey == false) return;
// Prevent typing in textfields
e.preventDefault();
// Select the hotkey
switch(e.which) {
case 32: // space bar
2018-08-12 10:37:57 +00:00
if (sumorobot.moving) {
$('.btn-stop').addClass('hover');
$('.btn-stop').click();
2018-07-08 18:51:59 +00:00
} else {
2018-08-12 10:37:57 +00:00
$('.btn-start').addClass('hover');
$('.btn-start').click();
2018-07-08 18:51:59 +00:00
}
break;
case 37: // left
2018-08-12 10:37:57 +00:00
sumorobot.send('left');
2018-07-08 18:51:59 +00:00
break;
case 38: // up
2018-08-12 10:37:57 +00:00
sumorobot.send('forward');
2018-07-08 18:51:59 +00:00
break;
case 39: // right
2018-08-12 10:37:57 +00:00
sumorobot.send('right');
2018-07-08 18:51:59 +00:00
break;
case 40: // down
2018-08-12 10:37:57 +00:00
sumorobot.send('backward');
2018-07-08 18:51:59 +00:00
break;
case 67: // c
2018-08-12 10:37:57 +00:00
$('#panel').toggle();
2018-07-08 18:51:59 +00:00
break;
case 76: // l
// No live stream if cookies are disabled
if (!cookiesEnabled) {
// TODO: Open the cookie consent popup
return;
}
2018-08-12 10:37:57 +00:00
// Load the Mixer stream when it is not yet loaded
if ($('#stream').is(':empty')) {
$('#stream').html('<iframe src="https://mixer.com/embed/player/14551694"></iframe>');
2018-07-08 18:51:59 +00:00
}
2018-08-12 10:37:57 +00:00
$('#stream').toggle();
2018-07-08 18:51:59 +00:00
// Toggle live steam visible
liveStreamVisible = !liveStreamVisible;
// If not in coding mode
if (codingEnabled == false) {
2018-08-12 10:37:57 +00:00
$('#readOnlyBlocklyCode').toggle();
2018-07-08 18:51:59 +00:00
}
break;
case 80: // p
2018-08-12 10:37:57 +00:00
$('#blocklyDiv').toggle();
$('#blocklyArea').toggle();
$('#blocklyCode').toggle();
// When live stream is not active
2018-07-08 18:51:59 +00:00
if (liveStreamVisible == false) {
2018-08-12 10:37:57 +00:00
// Toggle Blockly Python code
$('#readOnlyBlocklyCode').toggle();
2018-07-08 18:51:59 +00:00
}
// Toggle coding enabled
codingEnabled = !codingEnabled;
if (codingEnabled) {
// Resize the coding editor
codingEditor.resize();
// Focus, so the user can start coding
codingEditor.focus();
}
break;
case 82: // r
// Implement something
break;
case 83: // s
2018-08-12 10:37:57 +00:00
$('.btn-stop').addClass('hover');
$('.btn-stop').click();
2018-07-08 18:51:59 +00:00
break;
case 84: // t
2018-08-12 10:37:57 +00:00
sumorobot.send('calibrate_line');
2018-07-08 18:51:59 +00:00
break;
case 87: // w
2018-08-12 10:37:57 +00:00
$('.btn-start').addClass('hover');
$('.btn-start').click();
2018-07-08 18:51:59 +00:00
break;
}
});
// Key up event
$(document).keyup(function(e) {
// When the alt key is not pressed, don't process hotkeys
if (e.altKey == false) return;
// Remove hover from buttons
$('.btn').removeClass('hover');
// If arrow keys were pressed
if (e.which == 37 || e.which == 38 || e.which == 39 || e.which == 40) {
2018-08-12 10:37:57 +00:00
sumorobot.send('stop');
2018-07-08 18:51:59 +00:00
}
});
// Start button listener
2018-08-12 10:37:57 +00:00
$('.btn-start').click(function() {
// When we are in Python coding mode
2018-07-08 18:51:59 +00:00
if (codingEnabled) {
2018-08-12 10:37:57 +00:00
// Get the Python code
parsedCode = codingEditor.getValue();
2018-07-08 18:51:59 +00:00
// Otherwise when we are in Blockly mode
} else {
2018-08-12 10:37:57 +00:00
// Get the code from the blocks
parsedCode = sumorobot.getBlocklyCode().replace(/;;.{20}/g, '');
2018-07-08 18:51:59 +00:00
}
2018-08-12 10:37:57 +00:00
// Escape the qoutes, replace new lines and send the code
sumorobot.send('code', parsedCode.replace(/"/g, '\\"').replace(/\n/g, ';;'));
// TODO: Highlight the blocks and lines
/*var lines = sumorobot.getBlocklyCode().replace(/ /g, '').split('\n');
// Process code lines
for (i in lines) {
if (lines[i]) {
console.log(lines[i].split(';;'));
}
}*/
2018-07-08 18:51:59 +00:00
});
// Stop button listener
2018-08-12 10:37:57 +00:00
$('.btn-stop').click(function() {
sumorobot.send('stop');
// TODO: Stop highlighting blocks and lines
2018-07-08 18:51:59 +00:00
});
2018-08-12 10:37:57 +00:00
// Enter (return) keypress listener on robot ID field
$('#robot-id').keypress(function(e) {
2018-07-08 18:51:59 +00:00
if (e.which == 13) {
// Simulate robot GO button click
2018-08-12 10:37:57 +00:00
$('.btn-robot-go').click();
2018-07-08 18:51:59 +00:00
}
});
2018-08-12 10:37:57 +00:00
// Robot GO button listener
$('.btn-robot-go').click(function() {
2018-07-08 18:51:59 +00:00
// Extract and validate the selected robot ID
2018-08-12 10:37:57 +00:00
var robotId = $('#robot-id').val().trim();
if (robotId === '' || /^([a-f0-9]{6})$/.test(robotId) == false) {
$('#robot-id, #robot-label').addClass('has-error');
2018-07-08 18:51:59 +00:00
return;
} else {
2018-08-12 10:37:57 +00:00
$('#robot-id, #robot-label').removeClass('has-error');
2018-07-08 18:51:59 +00:00
}
2018-08-12 10:37:57 +00:00
// Update robot ID in local storage
setLocalStorageItem('sumorobot.robotId', robotId);
// When a connection was already opened
if (sumorobot) {
// Close the connection
2018-07-08 18:51:59 +00:00
sumorobot.close();
}
// Connect to the selected robots WebSocket
sumorobot = new Sumorobot(`ws://${ROBOT_SERVER}`, robotId);
// Hide the configuration panel
2018-08-12 10:37:57 +00:00
$('#panel').hide();
2018-07-08 18:51:59 +00:00
});
});