initial commit
Signed-off-by: Florian BOUILLON <f.bouillon@aptatio.com>
This commit is contained in:
89
src/main.js
Normal file
89
src/main.js
Normal file
@ -0,0 +1,89 @@
|
||||
const express = require('express')
|
||||
const fs = require('fs')
|
||||
const execSync = require('child_process').execSync;
|
||||
const { objectMap } = require('@dzeio/object-util')
|
||||
|
||||
const server = express()
|
||||
|
||||
fs.mkdirSync('files', { recursive: true })
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} data the GCode source
|
||||
* @returns {number} the time in seconds
|
||||
*/
|
||||
function decodeGCode(data) {
|
||||
|
||||
const line = data.split('\n').find((it) => it.includes('estimated printing time'))
|
||||
if (!line) {
|
||||
console.error('line not found :(')
|
||||
process.exit(1)
|
||||
}
|
||||
const time = line.split('=')[1].trim()
|
||||
let timeInSec = 0
|
||||
for (const it of time.split(' ')) {
|
||||
const lastChar = it.charAt(it.length - 1)
|
||||
const time = parseInt(it.slice(0, it.length - 1), 10)
|
||||
console.log(it)
|
||||
switch (lastChar) {
|
||||
case 'm':
|
||||
timeInSec += time * 60
|
||||
break;
|
||||
case 's':
|
||||
timeInSec += time
|
||||
break;
|
||||
case 'h':
|
||||
timeInSec += time * 60 * 60
|
||||
break;
|
||||
case 'd':
|
||||
timeInSec += time * 60 * 60 * 24
|
||||
break;
|
||||
default:
|
||||
throw new Error(`error parsing time ${it} (${time})`)
|
||||
}
|
||||
}
|
||||
console.log('orig', time, 'secs:', timeInSec, 'mins:', timeInSec / 60, 'hours:', timeInSec / 60 / 60, 'days:', timeInSec / 60 / 60 / 24)
|
||||
return timeInSec
|
||||
}
|
||||
|
||||
|
||||
server.use ((req, res, next) => {
|
||||
var data = '';
|
||||
req.setEncoding('utf8');
|
||||
req.on('data', function(chunk) {
|
||||
data += chunk;
|
||||
});
|
||||
|
||||
req.on('end', function() {
|
||||
req.body = data;
|
||||
next();
|
||||
});
|
||||
});
|
||||
|
||||
const path = process.cwd()
|
||||
|
||||
server.get('/', (req, res) => {
|
||||
res.send('send through POST in body the stl')
|
||||
})
|
||||
|
||||
const config = `${path}/configs/config.ini`
|
||||
|
||||
server.post('/', (req, res) => {
|
||||
const overrides = req.query
|
||||
const file = (Math.random() * 1000000).toFixed(0)
|
||||
const stlPath = `${path}/files/${file}.stl`
|
||||
const gcodePath = `${path}/files/${file}.gcode`
|
||||
console.log(stlPath)
|
||||
fs.writeFileSync(stlPath, req.body)
|
||||
const additionnalParams = objectMap(overrides, (v, k) => `--${k} ${v}`).join(' ')
|
||||
const cmd = `${process.env.SLICER_PATH} --export-gcode ${stlPath} --load ${config} --output ${gcodePath} ${additionnalParams}`
|
||||
execSync(cmd)
|
||||
const gcode = fs.readFileSync(gcodePath, 'utf-8')
|
||||
const time = decodeGCode(gcode)
|
||||
res.json({ timeToPrint: time, gcode })
|
||||
// res.sendFile(gcodePath)
|
||||
})
|
||||
|
||||
server.listen(3000, undefined, () => {
|
||||
console.log(`🚀 Server ready at localhost:3000`);
|
||||
})
|
Reference in New Issue
Block a user