OpenBuilds-CONTROL/app/js/websocket.js

556 wiersze
18 KiB
JavaScript
Czysty Zwykły widok Historia

2018-06-19 08:07:03 +00:00
var socket, laststatus;;
var server = ''; //192.168.14.100';
var programBoard = {};
var grblParams = {}
var smoothieParams = {}
var nostatusyet = true;
2018-06-19 20:25:40 +00:00
var safeToUpdateSliders = false;
var laststatus
var simstopped = false;
2018-06-21 21:04:41 +00:00
var bellstate = false;
var toast = Metro.toast.create;
2018-06-19 20:25:40 +00:00
$(document).ready(function() {
initSocket();
2018-06-21 20:02:40 +00:00
$("#command").inputHistory({
enter: function() {
2018-06-21 20:02:40 +00:00
$("#sendCommand").click();
}
});
2018-06-21 20:02:40 +00:00
$("form").submit(function() {
return false;
});
2018-06-19 20:25:40 +00:00
});
function printLog(string) {
2018-08-24 19:00:03 +00:00
if (document.getElementById("console") !== null) {
if (string.isString) {
// split(/\r\n|\n|\r/);
string = string.replace(/\r\n|\n|\r/, "<br />");
}
if ($('#console p').length > 100) {
// remove oldest if already at 300 lines
$('#console p').first().remove();
}
var template = '<p class="pf">';
var time = new Date();
2018-06-19 20:25:40 +00:00
2018-08-24 19:00:03 +00:00
template += '<span class="fg-brandColor1">[' + (time.getHours() < 10 ? '0' : '') + time.getHours() + ":" + (time.getMinutes() < 10 ? '0' : '') + time.getMinutes() + ":" + (time.getSeconds() < 10 ? '0' : '') + time.getSeconds() + ']</span> ';
template += string;
$('#console').append(template);
$('#console').scrollTop($("#console")[0].scrollHeight - $("#console").height());
}
2018-06-19 20:25:40 +00:00
}
2018-08-10 19:42:30 +00:00
// function printUpdateLog(string) {
// if (string.isString) {
// // split(/\r\n|\n|\r/);
// string = string.replace(/\r\n|\n|\r/, "<br />");
// }
// if ($('#console p').length > 100) {
// // remove oldest if already at 300 lines
// $('#console p').first().remove();
// }
// var template = '<p class="pf">';
// var time = new Date();
//
// template += '<span class="fg-brandColor1">[' + (time.getHours() < 10 ? '0' : '') + time.getHours() + ":" + (time.getMinutes() < 10 ? '0' : '') + time.getMinutes() + ":" + (time.getSeconds() < 10 ? '0' : '') + time.getSeconds() + ']</span> ';
// template += string;
// $('#updateconsole').append(template);
// $('#updateconsole').scrollTop($("#updateconsole")[0].scrollHeight - $("#updateconsole").height());
// }
2018-07-12 13:06:53 +00:00
2018-06-19 20:25:40 +00:00
2018-06-19 08:07:03 +00:00
function initSocket() {
2018-06-19 20:25:40 +00:00
socket = io.connect(server); // socket.io init
2018-08-10 19:42:30 +00:00
printLog("<span class='fg-red'>[ Websocket ] </span><span class='fg-brown'>Bidirectional Websocket Interface Started</span>")
2018-06-19 20:25:40 +00:00
setTimeout(function() {
populatePortsMenu();
}, 2000);
2018-06-20 13:16:46 +00:00
socket.on('disconnect', function() {
console.log("WEBSOCKET DISCONNECTED")
2018-08-10 19:42:30 +00:00
printLog("<span class='fg-red'>[ Websocket ] </span><span class='fg-brown'> Disconnected. Driver probably quit or crashed</span>")
$("#websocketstatus").html("Disconnected")
2018-06-20 13:16:46 +00:00
});
socket.on('connect', function() {
$("#websocketstatus").html("Connected")
});
2018-06-29 19:48:34 +00:00
socket.on('gcodeupload', function(data) {
printLog("Received new GCODE from API")
editor.session.setValue(data);
2018-09-06 17:50:01 +00:00
parseGcodeInWebWorker(data)
$('#controlTab').click()
2018-08-29 08:11:14 +00:00
if (webgl) {
$('#gcodeviewertab').click();
} else {
$('#gcodeeditortab').click()
}
2018-06-29 19:48:34 +00:00
});
2018-08-09 12:20:42 +00:00
socket.on('gcodeupload', function(data) {
printLog("Activated window");
});
socket.on('integrationpopup', function(data) {
printLog("Integration called from " + data)
// editor.session.setValue(data);
$('#controlTab').click()
$('#consoletab').click()
// gcodeeditortab
});
2018-06-29 19:48:34 +00:00
2018-07-12 13:06:53 +00:00
socket.on('updatedata', function(data) {
// console.log(data.length, data)
var toPrint = data.response;
2018-08-10 19:42:30 +00:00
printLog("<span class='fg-red'>[ " + data.command + " ]</span> <span class='fg-green'>" + toPrint + "</span>")
2018-07-12 13:06:53 +00:00
});
socket.on('updateready', function(data) {
2018-08-10 19:42:30 +00:00
$('#availVersion').html(data)
Metro.dialog.open('#downloadUpdate')
// $('#applyupdatesbtn').prop('disabled', false);
2018-07-12 13:06:53 +00:00
});
socket.on('updateprogress', function(data) {
$('#downloadprogress').html(data + "%");
});
2018-06-29 19:48:34 +00:00
2018-06-19 20:25:40 +00:00
socket.on('data', function(data) {
// console.log(data.length, data)
2018-06-27 20:53:07 +00:00
var toPrint = data.response;
2018-06-19 08:07:03 +00:00
2018-06-20 13:00:05 +00:00
// Parse Grbl Settings Feedback
2018-06-27 20:53:07 +00:00
if (data.response.indexOf('$') === 0) {
2018-08-24 19:00:03 +00:00
if (typeof grblSettings !== 'undefined') {
grblSettings(data.response)
var key = data.response.split('=')[0].substr(1);
var descr = grblSettingCodes[key];
toPrint = data.response + " ;" + descr
printLog("<span class='fg-red'>[ " + data.command + " ]</span> <span class='fg-green'>" + toPrint + "</span>")
}
2018-06-19 20:25:40 +00:00
};
2018-06-20 13:00:05 +00:00
2018-06-19 20:25:40 +00:00
});
2018-06-21 20:02:40 +00:00
socket.on("grbl", function(data) {
showGrbl(true)
});
function showGrbl(bool) {
if (bool) {
sendGcode('$$')
$("#grblButtons").show()
$("#firmwarename").html('Grbl')
} else {
$("#grblButtons").hide()
$("#firmwarename").html('')
}
}
2018-06-19 20:25:40 +00:00
socket.on("queueCount", function(data) {
2018-06-22 19:49:51 +00:00
if (laststatus) {
if (laststatus.comms.connectionStatus == 3) {
2018-08-28 15:09:20 +00:00
editor.gotoLine(parseInt(data[1]) - parseInt(data[0]));
2018-06-22 19:49:51 +00:00
}
}
2018-06-21 20:02:40 +00:00
$('#gcodesent').html("Queue: " + parseInt(data[0]));
2018-08-01 20:20:17 +00:00
// calc percentage
var left = parseInt(data[0])
var total = parseInt(data[1])
var done = total - left;
var donepercent = parseInt(done / total * 100)
var progressbar = $("#progressbar").data("progress");
2018-08-24 19:00:03 +00:00
if (progressbar) {
progressbar.val(donepercent);
}
2018-06-19 20:25:40 +00:00
// }
})
socket.on('toastError', function(data) {
// console.log("toast", data)
2018-06-25 20:13:58 +00:00
toast("<i class='fas fa-exclamation-triangle'></i> " + data, null, 2300, "bg-red fg-white");
//
});
socket.on('toastSuccess', function(data) {
console.log("toast", data)
2018-06-25 20:13:58 +00:00
toast("<i class='fas fa-exclamation-triangle'></i> " + data, null, 2300, "bg-green fg-white");
//
});
2018-06-19 20:25:40 +00:00
socket.on('status', function(status) {
2018-06-26 10:33:13 +00:00
if (nostatusyet) {
2018-08-07 19:48:38 +00:00
$('#windowtitle').html("OpenBuilds Machine Driver v" + status.driver.version)
2018-06-26 10:33:13 +00:00
}
2018-06-19 20:25:40 +00:00
nostatusyet = false;
2018-06-26 10:33:13 +00:00
2018-06-19 20:25:40 +00:00
// if (!_.isEqual(status, laststatus)) {
if (laststatus !== undefined) {
if (!_.isEqual(status.comms.interfaces.ports, laststatus.comms.interfaces.ports)) {
var string = "Detected a change in available ports: ";
for (i = 0; i < status.comms.interfaces.ports.length; i++) {
string += "[" + status.comms.interfaces.ports[i].comName + "]"
}
printLog(string)
laststatus.comms.interfaces.ports = status.comms.interfaces.ports;
populatePortsMenu();
2018-06-19 08:07:03 +00:00
}
2018-06-19 20:25:40 +00:00
}
2018-06-27 19:23:34 +00:00
2018-06-19 20:25:40 +00:00
$('#runStatus').html("Controller: " + status.comms.runStatus);
2018-08-07 16:56:40 +00:00
// if (status.machine.firmware.state.units == "G20") {
// var unit = " in"
// } else if (status.machine.firmware.state.units == "G21") {
// var unit = " mm"
// } else {
var unit = " mm"
// }
if (unit == " mm") {
var xpos = status.machine.position.work.x + unit;
var ypos = status.machine.position.work.y + unit;
var zpos = status.machine.position.work.z + unit;
} else if (unit == " in") {
var xpos = (status.machine.position.work.x / 25.4).toFixed(2) + unit;
var ypos = (status.machine.position.work.y / 25.4).toFixed(2) + unit;
var zpos = (status.machine.position.work.z / 25.4).toFixed(2) + unit;
}
if ($('#xPos').html() != xpos) {
$('#xPos').html(xpos);
}
if ($('#yPos').html() != ypos) {
$('#yPos').html(ypos);
}
if ($('#zPos').html() != zpos) {
$('#zPos').html(zpos);
}
2018-06-19 20:25:40 +00:00
2018-08-28 15:16:17 +00:00
if (webgl) {
if (!isJogWidget) {
if (!simRunning) {
cone.position.x = status.machine.position.work.x
cone.position.y = status.machine.position.work.y
cone.position.z = (parseFloat(status.machine.position.work.z) + 20)
}
2018-08-28 15:09:20 +00:00
}
}
// $('#T0CurTemp').html(status.machine.temperature.actual.t0.toFixed(1) + " / " + status.machine.temperature.setpoint.t0.toFixed(1));
// $('#T1CurTemp').html(status.machine.temperature.actual.t1.toFixed(1) + " / " + status.machine.temperature.setpoint.t1.toFixed(1));
// $('#B0CurTemp').html(status.machine.temperature.actual.b.toFixed(1) + " / " + status.machine.temperature.setpoint.b.toFixed(1));
2018-06-27 19:23:34 +00:00
// setTemp(status.machine.temperature.actual.t0, status.machine.temperature.actual.t1, status.machine.temperature.actual.b)
2018-06-19 20:25:40 +00:00
if (safeToUpdateSliders) {
2018-08-24 19:00:03 +00:00
if ($('#fro').data('slider') && $('#tro').data('slider')) {
$('#fro').data('slider').val(status.machine.overrides.feedOverride)
$('#tro').data('slider').val(status.machine.overrides.spindleOverride)
}
2018-06-19 20:25:40 +00:00
}
// Grbl Pins Input Status
$('.pinstatus').removeClass('alert').addClass('success').html('OFF')
if (status.machine.inputs.length > 0) {
for (i = 0; i < status.machine.inputs.length; i++) {
switch (status.machine.inputs[i]) {
case 'X':
// console.log('PIN: X-LIMIT');
$('#xpin').removeClass('success').addClass('alert').html('ON')
break;
case 'Y':
// console.log('PIN: Y-LIMIT');
$('#ypin').removeClass('success').addClass('alert').html('ON')
break;
case 'Z':
// console.log('PIN: Z-LIMIT');
$('#zpin').removeClass('success').addClass('alert').html('ON')
break;
case 'P':
// console.log('PIN: PROBE');
$('#prbpin').removeClass('success').addClass('alert').html('ON')
break;
// case 'D':
// console.log('PIN: DOOR');
// $('#doorpin').removeClass('success').addClass('alert').html('ON')
// break;
// case 'H':
// console.log('PIN: HOLD');
// break;
// case 'R':
// console.log('PIN: SOFTRESET');
// break;
// case 'S':
// console.log('PIN: CYCLESTART');
// break;
}
}
}
$('#driverver').html("v" + status.driver.version);
if (!status.machine.firmware.type) {
$('#firmwarever').html("NOCOMM");
} else {
$('#firmwarever').html(status.machine.firmware.type + " v" + status.machine.firmware.version);
}
$('#commblocked').html(status.comms.blocked ? "BLOCKED" : "Ready");
var string = '';
switch (status.comms.connectionStatus) {
case 0:
string += "Not Connected"
break;
case 2:
string += "Connected"
break;
case 3:
string += "Streaming"
break;
case 4:
string += "Paused"
break;
case 5:
string += "Alarmed"
break;
}
$('#commstatus').html(string);
$('#runstatus').html(status.comms.runStatus);
$('#drvqueue').html(status.comms.queue);
if (status.machine.firmware.buffer.length > 0) {
$('#buffstatus').html(status.machine.firmware.buffer[0] + " blocks / " + status.machine.firmware.buffer[1] + " bytes");
} else {
$('#buffstatus').html("NOCOMM");
}
2018-08-07 16:56:40 +00:00
// if (status.machine.firmware.state) {
// if (status.machine.firmware.state.workoffset.length) {
// $('#wcostatus').html(status.machine.firmware.state.workoffset);
// } else {
// $('#wcostatus').html("NOCOMM");
// }
// if (status.machine.firmware.state.plane.length) {
// $('#planestatus').html(status.machine.firmware.state.plane);
// } else {
// $('#planestatus').html("NOCOMM");
// }
// if (status.machine.firmware.state.absrel.length) {
// if (status.machine.firmware.state.absrel == "G90") {
// $('#absrel').html(status.machine.firmware.state.absrel + " (absolute)");
// } else if (status.machine.firmware.state.absrel == "G91") {
// $('#absrel').html(status.machine.firmware.state.absrel + " (relative)");
// }
// } else {
// $('#absrel').html("NOCOMM");
// }
// if (status.machine.firmware.state.units.length) {
// if (status.machine.firmware.state.units == "G20") {
// $('#units').html(status.machine.firmware.state.units + " (inches)");
// $('#dist01label').html("0.1in");
// $('#dist1label').html("1in");
// $('#dist10label').html("10in");
// $('#dist100label').html("100in");
// } else if (status.machine.firmware.state.units == "G21") {
// $('#units').html(status.machine.firmware.state.units + " (mm)");
// $('#dist01label').html("0.1mm");
// $('#dist1label').html("1mm");
// $('#dist10label').html("10mm");
// $('#dist100label').html("100mm");
// }
// } else {
// $('#units').html("NOCOMM");
// }
//
// if (status.comms.interfaces.activePort) {
// $('#activeportstatus').html(status.comms.interfaces.activePort)
// } else {
// $('#activeportstatus').html("none")
// }
//
// }
2018-08-25 16:54:28 +00:00
// Set the Connection Toolbar option
setConnectBar(status.comms.connectionStatus, status);
setControlBar(status.comms.connectionStatus, status)
setJogPanel(status.comms.connectionStatus, status)
setConsole(status.comms.connectionStatus, status)
if (status.comms.connectionStatus != 5) {
bellstate = false
};
if (status.comms.connectionStatus == 0) {
showGrbl(false)
}
2018-06-19 20:25:40 +00:00
laststatus = status;
});
$('#sendCommand').on('click', function() {
var commandValue = $('#command').val();
sendGcode(commandValue);
// $('#command').val('');
2018-06-19 20:25:40 +00:00
});
$('#command').on('keypress', function(e) {
if (e.which === 13) {
$(this).attr("disabled", "disabled");
var commandValue = $('#command').val();
sendGcode(commandValue);
$('#command').val('');
$(this).removeAttr("disabled");
}
});
var bellflash = setInterval(function() {
if (!nostatusyet) {
if (laststatus) {
if (laststatus.comms.connectionStatus == 5) {
if (bellstate == false) {
$('#navbell').hide();
2018-06-25 20:13:58 +00:00
$('#navbellBtn1').hide();
$('#navbellBtn2').hide();
$('#navbellBtn3').hide();
bellstate = true
} else {
$('#navbell').show();
2018-06-25 20:13:58 +00:00
$('#navbellBtn1').show();
$('#navbellBtn2').show();
$('#navbellBtn3').show();
bellstate = false
}
2018-06-19 20:25:40 +00:00
} else {
$('#navbell').hide();
2018-06-25 20:13:58 +00:00
$('#navbellBtn1').hide();
$('#navbellBtn2').hide();
$('#navbellBtn3').hide();
2018-06-19 08:07:03 +00:00
}
}
2018-06-19 20:25:40 +00:00
}
}, 200);
2018-06-19 08:07:03 +00:00
};
function selectPort() {
2018-06-19 20:25:40 +00:00
socket.emit('connectTo', 'usb,' + $("#portUSB").val() + ',' + '115200');
2018-06-19 08:07:03 +00:00
};
function closePort() {
socket.emit('closePort', 1);
populatePortsMenu();
$('.mdata').val('');
}
function populatePortsMenu() {
2018-06-20 13:00:05 +00:00
var response = `<select id="select1" data-role="select" class="mt-4"><optgroup label="USB Ports">`
2018-06-19 20:25:40 +00:00
for (i = 0; i < laststatus.comms.interfaces.ports.length; i++) {
var port = friendlyPort(i)
2018-06-20 13:00:05 +00:00
response += `<option value="` + laststatus.comms.interfaces.ports[i].comName + `">` + laststatus.comms.interfaces.ports[i].comName + " " + port.note + `</option>`;
2018-06-19 20:25:40 +00:00
};
2018-06-20 13:00:05 +00:00
response += `</optgroup></select>`
var select = $("#portUSB").data("select");
select.data(response);
2018-06-21 20:02:40 +00:00
$('#portUSB').parent(".select").removeClass('disabled')
2018-06-27 19:23:34 +00:00
$("#connectBtn").attr('disabled', false);
2018-06-19 08:07:03 +00:00
}
function sendGcode(gcode) {
2018-06-19 20:25:40 +00:00
if (gcode) {
socket.emit('runCommand', gcode);
}
2018-06-19 08:07:03 +00:00
}
2018-06-22 19:49:51 +00:00
// function ContextLineRun() { //Rightclick Contextmenu in Ace editor: Send single line of gcode
// sendGcode(editor.session.getLine(editor.getSelectionRange().start.row));
// $('#editorContextMenu').hide();
// }
2018-06-19 08:07:03 +00:00
function feedOverride(step) {
if (socket) {
socket.emit('feedOverride', step);
}
2018-06-19 08:07:03 +00:00
}
function spindleOverride(step) {
if (socket) {
socket.emit('spindleOverride', step);
}
2018-06-19 08:07:03 +00:00
}
function friendlyPort(i) {
2018-06-19 20:25:40 +00:00
// var likely = false;
var img = 'usb.png';
var note = '';
var manufacturer = laststatus.comms.interfaces.ports[i].manufacturer
if (manufacturer == `(Standard port types)`) {
img = 'serial.png'
note = 'Motherboard Serial Port';
} else if (laststatus.comms.interfaces.ports[i].productId && laststatus.comms.interfaces.ports[i].vendorId) {
if (laststatus.comms.interfaces.ports[i].productId == '6015' && laststatus.comms.interfaces.ports[i].vendorId == '1D50') {
// found Smoothieboard
img = 'smoothieboard.png';
note = 'Smoothieware USB Port';
}
if (laststatus.comms.interfaces.ports[i].productId == '6001' && laststatus.comms.interfaces.ports[i].vendorId == '0403') {
// found FTDI FT232
img = 'usb.png';
note = 'FTDI USB to Serial';
}
if (laststatus.comms.interfaces.ports[i].productId == '6015' && laststatus.comms.interfaces.ports[i].vendorId == '0403') {
// found FTDI FT230x
img = 'usb.png';
note = 'FTDI USD to Serial';
}
if (laststatus.comms.interfaces.ports[i].productId == '606D' && laststatus.comms.interfaces.ports[i].vendorId == '1D50') {
// found TinyG G2
img = 'usb.png';
note = 'Tiny G2';
}
if (laststatus.comms.interfaces.ports[i].productId == '003D' && laststatus.comms.interfaces.ports[i].vendorId == '2341') {
// found Arduino Due Prog Port
img = 'due.png';
note = 'Arduino Due Prog';
2018-06-19 08:07:03 +00:00
}
2018-06-19 20:25:40 +00:00
if (laststatus.comms.interfaces.ports[i].productId == '0043' && laststatus.comms.interfaces.ports[i].vendorId == '2341' || laststatus.comms.interfaces.ports[i].productId == '0001' && laststatus.comms.interfaces.ports[i].vendorId == '2341' || laststatus.comms.interfaces.ports[i].productId == '0043' && laststatus.comms.interfaces.ports[i].vendorId == '2A03') {
// found Arduino Uno
img = 'uno.png';
note = 'Arduino Uno';
}
if (laststatus.comms.interfaces.ports[i].productId == '2341' && laststatus.comms.interfaces.ports[i].vendorId == '0042') {
// found Arduino Mega
img = 'mega.png';
note = 'Arduino Mega';
}
if (laststatus.comms.interfaces.ports[i].productId == '7523' && laststatus.comms.interfaces.ports[i].vendorId == '1A86') {
// found CH340
img = 'uno.png';
note = 'CH340 Arduino Fake';
}
if (laststatus.comms.interfaces.ports[i].productId == 'EA60' && laststatus.comms.interfaces.ports[i].vendorId == '10C4') {
// found CP2102
img = 'nodemcu.png';
note = 'NodeMCU';
}
if (laststatus.comms.interfaces.ports[i].productId == '2303' && laststatus.comms.interfaces.ports[i].vendorId == '067B') {
// found CP2102
// img = 'nodemcu.png';
note = 'Prolific USB to Serial';
}
} else {
img = "usb.png";
}
2018-06-19 08:07:03 +00:00
2018-06-19 20:25:40 +00:00
return {
img: img,
note: note
};
}