This repository has been archived by the owner on Aug 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
master.js
128 lines (111 loc) · 3.62 KB
/
master.js
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
console.log("inside of the master controller");
console.log("node version = ", process.versions);
var fs = require('fs');
var os = require('os');
var path = require('path');
var compile = require('./compile');
console.log("about to load the serial port");
try {
var sp = require('serialport');
} catch(e) {
console.log("error loading serial port");
console.log(e);
console.log(e.stack);
}
var uploader = require('./uploader');
var sketches = require('./sketches.js');
var serial = require('./serial.js');
var platform = require('./platform');
var LIBS = require('./libraries');
var BOARDS = require('./boards').loadBoards();
var OPTIONS = {
userlibs: platform.getSettings().userlibs
}
console.log('settings',platform.getSettings());
console.log("options",OPTIONS);
function makeCleanDir(outpath) {
if(fs.existsSync(outpath)) {
fs.readdirSync(outpath).forEach(function(file) {
fs.unlinkSync(outpath+'/'+file);
})
fs.rmdirSync(outpath);
}
fs.mkdirSync(outpath);
return outpath;
}
exports.listPorts = function(cb) {
sp.list(function(err,list) {
// format the data (workaround serialport issue on Win (&*nix?))
list.forEach(function(port) {
if(port.pnpId){
var data = /^USB\\VID_([a-fA-F0-9]{4})\&PID_([a-fA-F0-9]{4})/.exec(port.pnpId);
if(data){
port.vendorId = port.vendorId || '0x'+data[1];
port.productId = port.productId || '0x'+data[2];
}
}
});
cb(err,list);
});
}
exports.getBoards = function() {
return BOARDS;
}
exports.doCompile = function(code,board,sketch, cb, publishEvent) {
var BUILD_DIR = os.tmpdir() + '/build';
publishEvent({ type:'compile', message:BUILD_DIR});
console.log("compiling with build dir:", BUILD_DIR);
//create output dir
if(!fs.existsSync(BUILD_DIR)) {
fs.mkdirSync(BUILD_DIR);
}
var outpath = makeCleanDir(BUILD_DIR+'/out');
var sketchpath = makeCleanDir(BUILD_DIR+"/tmp");
var inoFile = path.join(sketchpath, sketch + '.ino');
fs.writeFileSync(inoFile, code);
publishEvent({ type:'compile', message:'writing to ' + inoFile });
var foundBoard = null;
BOARDS.forEach(function(bd) {
if(bd.id == board) foundBoard = bd;
})
OPTIONS.device = foundBoard;
OPTIONS.platform = platform.getPlatform(OPTIONS.device);
OPTIONS.platform.installIfNeeded(function() {
OPTIONS.name = sketch;
compile.compile(sketchpath,outpath,OPTIONS, publishEvent,
path.join(OPTIONS.platform.getUserSketchesDir(), sketch), cb);
}, function(per) {
console.log("percentage = ",per);
});
}
exports.getSketch = function (path, cb) {
sketches.getSketch(path,cb);
}
exports.saveSketch = function (args, cb) {
if(!args.file) throw new Error('missing filename');
if(!args.code) throw new Error('missing code');
sketches.saveSketch(args.file, args.code,cb);
}
exports.listSketches = function(cb) {
sketches.listSketchesFull(cb);
}
exports.makeNewSketch = function(name,cb) {
sketches.makeNewSketch(name,cb);
}
exports.deleteSketch = function(name,cb) {
sketches.deleteSketch(name,cb);
}
exports.searchLibraries = function(query,cb) {
LIBS.search(query,cb);
}
exports.upload = function(sketch, port, pub, cb) {
uploader.upload(sketch,port,OPTIONS,pub,cb);
}
exports.getSettings = function(args,cb) {
console.log("master doing get settings");
cb(platform.getSettings());
}
exports.setSettings = function(args,cb) {
console.log("master doing set settings");
platform.setSettings(args,cb);
}