-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·99 lines (88 loc) · 3.56 KB
/
Copy pathserver.js
File metadata and controls
executable file
·99 lines (88 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env node
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const fs = require('fs');
const spawn = require('child_process').spawn;
const rimraf = require('rimraf');
const cors = require('cors');
const config = require('./config');
// Load the config parameters from the config.js-file
var LOCALARDUINOPATH = config.localArduinoPath;
var BUILDERPATH = config.builderPath;
var LOCALBUILDPATH = config.localBuildPath;
// Extend functionality of express() with these submodules
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
app.use(cors());
app.options('*', cors());
// GET - Check if the server is active
app.get('/', function (req, res) {
res.writeHead(200);
res.write('Server is functional');
res.end();
});
// POST - Compiles the requested code and returns the hex-file in the response
app.post('/compile', function (req, res) {
if(req.body['data'] !== undefined && req.body['boardName'] !== undefined) {
var sketchName = 'tmp'+ parseInt(Math.random()*10000);
var boardName = req.body['boardName'];
var mainBuildPath = LOCALBUILDPATH+'/'+sketchName;
var sketchPath = LOCALBUILDPATH+'/'+sketchName+'/'+sketchName+'.ino';
var buildPath = LOCALBUILDPATH+'/'+sketchName+'/'+sketchName;
// Make tmp folder where arduino-builder will write compiled files to
fs.mkdir(mainBuildPath, function(err) {
if(err) {
return console.log(err);
} else {
fs.mkdir(buildPath, function(err) {
if(err) {
return console.log(err);
}
});
// Write (or overwrite) content of the .ino-file
fs.writeFile(sketchPath, req.body['data'], 'utf8', function(err) {
if(err) {
return console.log(err);
} else {
var outMsg = '';
var errMsg = '';
// Compile sketch
//var command = spawn('sh', ['/home/robin/zzzArduinoBuilder/script.sh', sketchName]); // Use this command if you want to run bash script instead of command
var command = spawn(BUILDERPATH+'/arduino-builder', ['-compile', '-hardware', BUILDERPATH+'/hardware', '-build-path', buildPath, '-tools', BUILDERPATH+'/hardware/tools', '-tools', BUILDERPATH+'/tools-builder', '-libraries', BUILDERPATH+'/libraries', '-libraries', LOCALARDUINOPATH+'/libraries', '-fqbn', boardName, sketchPath]);
command.stdout.on('data', function(data) {
outMsg += '\n' + data;
//console.log('stdout(' + command.pid + '): ' + data);
});
command.stderr.on('data', function(data) {
errMsg += '\n' + data;
//console.log('stderr(' + command.pid + '): ' + data);
});
command.on('close', function(code) {
if (code === 0) {
fs.readFile(buildPath+'/'+sketchName+'.ino.hex', 'utf8', function(err, data) {
//console.log('close(' + command.pid + '): Sending hex as response');
res.writeHead(200, {'Content-Type': 'application/json'});
res.write(JSON.stringify({'hex':data, 'out':outMsg, 'err':errMsg}));
res.end();
});
} else {
res.sendStatus(500); // Server error
}
// Delete tmp folder and sketch to save space
rimraf(mainBuildPath, function(){
console.log('cleared dir(' + command.pid + ')');
});
});
}
});
}
});
} else {
res.writeHead(200, {'Content-Type': 'application/json'});
res.write(JSON.stringify({'err':'Empty JSON request send from client'}));
res.end();
//console.log('Error: empty JSON request');
}
});
app.listen(config.port, () => console.log('Compile server listening on port ' + config.port + '!'));