From f9cda62f732352fcdd6d9f9c238f9839b90d45cd Mon Sep 17 00:00:00 2001 From: openbuilds-engineer Date: Wed, 29 Aug 2018 10:11:14 +0200 Subject: [PATCH] File Association --- app/js/main.js | 14 ++++++++ app/js/websocket.js | 7 ++-- index.js | 88 +++++++++++++++++++++++++++------------------ package.json | 9 ++++- 4 files changed, 81 insertions(+), 37 deletions(-) diff --git a/app/js/main.js b/app/js/main.js index 19a4ed5..d619765 100644 --- a/app/js/main.js +++ b/app/js/main.js @@ -57,6 +57,20 @@ $(document).ready(function() { fileOpen.addEventListener('change', readFile, false); } + + $.get("/gcode").done(function(data) { + console.log(data.length) + editor.session.setValue(data); + $('#controlTab').click() + if (webgl) { + $('#gcodeviewertab').click(); + } else { + $('#gcodeeditortab').click() + } + }); + + + }); function readFile(evt) { diff --git a/app/js/websocket.js b/app/js/websocket.js index 609ed3e..200d284 100644 --- a/app/js/websocket.js +++ b/app/js/websocket.js @@ -100,8 +100,11 @@ function initSocket() { printLog("Received new GCODE from API") editor.session.setValue(data); $('#controlTab').click() - $('#gcodeeditortab').click() - // gcodeeditortab + if (webgl) { + $('#gcodeviewertab').click(); + } else { + $('#gcodeeditortab').click() + } }); socket.on('gcodeupload', function(data) { diff --git a/index.js b/index.js index bd5263f..7766f4b 100644 --- a/index.js +++ b/index.js @@ -184,6 +184,7 @@ if (isElectron()) { } else { var uploadsDir = process.env.APPDATA || (process.platform == 'darwin' ? process.env.HOME + 'Library/Preferences' : '/var/local') } +var uploadedgcode = ""; // var to store uploaded gcode // fs.existsSync(uploadsDir) || fs.mkdirSync(uploadsDir) mkdirp(uploadsDir, function(err) { @@ -234,7 +235,7 @@ var re = new RegExp("^[a-f0-9]{32}"); var status = { driver: { version: require('./package').version, - ipaddress: ip.address() + ipaddress: ip.address(), }, machine: { inputs: [], @@ -500,6 +501,12 @@ app.get('/upload', (req, res) => { res.sendFile(__dirname + '/app/upload.html'); }) +app.get('/gcode', (req, res) => { + res.header("Access-Control-Allow-Origin", "*"); + res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); + res.send(uploadedgcode); +}) + // File Post app.post('/upload', function(req, res) { res.header("Access-Control-Allow-Origin", "*"); @@ -537,37 +544,7 @@ app.post('/upload', function(req, res) { jogWindow.focus(); jogWindow.setAlwaysOnTop(false); } - setTimeout(function() { - - fs.readFile(file.path, 'utf8', - function(err, data) { - if (err) { - console.log(err); - var output = { - 'command': '', - 'response': "ERROR: File Upload Failed" - } - io.sockets.emit('data', output); - if (jogWindow && !jogWindow.isFocused()) { - appIcon.displayBalloon({ - icon: nativeImage.createFromPath(iconPath), - title: "ERROR: File Upload Failed", - content: "OpenBuilds Machine Driver ERROR: File Upload Failed" - }) - } - } - if (data) { - io.sockets.emit('gcodeupload', data); - if (jogWindow && !jogWindow.isFocused()) { - appIcon.displayBalloon({ - icon: nativeImage.createFromPath(iconPath), - title: "GCODE Received", - content: "OpenBuilds Machine Driver received new GCODE" - }) - } - } - }); - }, 1500); + readFile(file.path) }); form.on('aborted', function() { @@ -890,7 +867,8 @@ io.on("connection", function(socket) { socket.on('runJob', function(data) { - // console.log(data) + console.log(data) + uploadedgcode = data; console.log('Run Job (' + data.length + ')'); if (status.comms.connectionStatus > 0) { if (data) { @@ -1512,6 +1490,28 @@ io.on("connection", function(socket) { }); +function readFile(path) { + if (path.length > 1) { + console.log('readfile: ' + path) + fs.readFile(path, 'utf8', + function(err, data) { + if (err) { + console.log(err); + var output = { + 'command': '', + 'response': "ERROR: File Upload Failed" + } + uploadedgcode = ""; + } + if (data) { + io.sockets.emit('gcodeupload', data); + uploadedgcode = data; + return data + } + }); + } +} + function machineSend(gcode) { // console.log("SENDING: " + gcode) if (port.isOpen) { @@ -1917,6 +1917,13 @@ if (isElectron()) { jogWindow.focus(); jogWindow.setAlwaysOnTop(false); } + // console.log('SingleInstance') + // console.log(commandLine) + var openFilePath = commandLine[1]; + if (openFilePath !== "") { + // console.log(openFilePath); + readFile(openFilePath); + } }); if (shouldQuit) { @@ -1930,7 +1937,15 @@ if (isElectron()) { function createApp() { createTrayIcon(); - if (process.platform == 'darwin') { + if (process.platform == 'win32' && process.argv.length >= 2) { + var openFilePath = process.argv[1]; + if (openFilePath !== "") { + console.log("path" + openFilePath); + readFile(openFilePath); + } + } + + if (process.platform == 'darwin' || uploadedgcode.length > 1) { if (jogWindow === null) { createJogWindow(); jogWindow.show() @@ -1946,6 +1961,9 @@ if (isElectron()) { } // createWindow(); // createJogWindow(); + // console.log("createoa") + + } function createTrayIcon() { @@ -2146,4 +2164,6 @@ if (isElectron()) { } } + + process.on('exit', () => console.log('exit')) \ No newline at end of file diff --git a/package.json b/package.json index 0897672..2dee056 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "OpenBuildsMachineDriver", - "version": "1.0.80", + "version": "1.0.82", "license": "AGPL-3.0", "description": "Machine Interface Driver for OpenBuilds", "author": "github.com/openbuilds ", @@ -49,6 +49,13 @@ "installerSidebar": "build/installerSidebar.bmp", "artifactName": "${productName}-Setup-${version}.${ext}" }, + "fileAssociations": [ + { + "ext": [".gcode", ".gc", ".tap", ".nc", ".cnc"], + "description": "GCODE File", + "role": "none" + } + ], "files": [ "**/*", "ssl/**/*"