-
Notifications
You must be signed in to change notification settings - Fork 10
/
oak.js
372 lines (341 loc) · 12.3 KB
/
oak.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
'use strict';
var version = '1.0.2';
var spark = require('spark');
var fs = require('fs');
var readlineSync = require('readline-sync');
var mkdirp = require('mkdirp');
var path = require('path');
var colors = require('colors');
var windows = process.platform.indexOf("win") === 0;
var os = require("os");
var program = require('commander');
program.version(version)
.usage('[-d deviceName] <binary file to upload>')
.option('-d, --device <device name>', 'target device name')
.parse(process.argv);
var pathToBin;
var pathToConfig;
var config;
var flashTimeout;
var rebootTimeout = -1;
var rebooting = false;
var activeDeviceIndex = -1;
function clear() {
var i,lines;
var stdout = '';
if (windows === false) {
stdout += '\x1B[2J';
} else {
lines = process.stdout.getWindowSize()[1];
for (i=0; i<lines; i++) {
stdout += os.EOL;
}
}
// Reset cursur
stdout += '\x1B[0f';
process.stdout.write(stdout);
}
function loadConfig() {
try {
var config_contents = fs.readFileSync(pathToConfig + 'config.json', 'utf8');
config = JSON.parse(config_contents);
// try to recover from old config file format
if (config.devices === undefined) {
// build the new config var
var newConfig = {
access_token: config.access_token,
devices : []
};
// extract the new device name format
var newDeviceName = config.device_name.split('(');
if (newDeviceName.length == 0) {
return false;
}
newConfig.devices.push({'device_id' : newDeviceName[0].trim(), 'device_name' : config.device_name, 'active' : true});
config = newConfig;
}
if (program.device != undefined) {
// find device by name
for (var deviceId in config.devices) {
if (config.devices[deviceId].device_name == program.device) {
activeDeviceIndex = deviceId;
break;
}
}
} else {
// get the active device
for (deviceId in config.devices) {
if (config.devices[deviceId].active === true) {
activeDeviceIndex = deviceId;
break;
}
}
}
if (activeDeviceIndex == -1) {
return false;
}
return true;
}
catch(e) {
return false;
}
}
function loginOrFail() {
if (program.args.length == 1) {
errorAndQuit('Config file not found at: ' + pathToConfig + 'config.json - please run the oak tool from the command line with no arguments to configure.');
} else {
particleLogin();
}
}
pathToConfig = '~/';
if (process.platform == 'linux' || process.platform == 'freebsd') {
pathToConfig = process.env.HOME + '/.oak/';
} else if(process.platform == 'win32') {
pathToConfig = process.env.APPDATA + '\\oak\\';
} else if(process.platform == 'darwin') {
pathToConfig = process.env.HOME + '/Library/Preferences/oak/';
}
console.log('OakCLI tool version '+version);
if (!loadConfig()) {
loginOrFail();
} else {
if (program.args.length == 1) {
//should be a file
pathToBin = program.args[0].trim();
if (!fs.existsSync(pathToBin)) {
failAndUsage("Invalid file name.");
} else {
spark.login({accessToken: config.access_token}, function(err, data) {
if (err) {
if (err.code !== undefined){
if (err.code == 'ENOTFOUND'){
errorAndQuit('Could not connect to Particle.io');
}
}
fs.unlinkSync(pathToConfig+'config.json');
errorAndQuit('Your access token has expired, please run this tool from the command line to get a new token.');
}
spark.getDevice(config.devices[activeDeviceIndex].device_id, function(err, device) {
if (err) {
if (err.code && err.code == 'ENOTFOUND') {
errorAndQuit('Could not connect to Particle.io');
}
fs.unlinkSync(pathToConfig+'config.json');
errorAndQuit('Selected device is no longer available on this account, please run this tool from the command line to select a device.');
}
console.log('Using config file at: ' + pathToConfig + 'config.json');
var onlyPath = path.dirname(pathToBin);
var onlyFile = path.basename(pathToBin);
process.chdir(onlyPath);
console.log('Sending file to cloud, to flash ' + config.devices[activeDeviceIndex].device_name + ' (Device ID: ' + config.devices[activeDeviceIndex].device_id + ')');
device.flash([onlyFile], function(err, data) {
if (err || (data && data.ok == false)) {
errorAndQuit('An error occurred while flashing the device:');
} else {
startProgressBar("Flashing.");
flashTimeout = setTimeout(onFlashTimeout, 60 * 1 * 1000); // flash timeout : 1 minute
spark.getEventStream(null, config.devices[activeDeviceIndex].device_id, function(data) {
var eventData = data.data.trim();
if (data.name == 'spark/flash/status') {
clearTimeout(flashTimeout);
if (eventData == 'success') {
console.log('Done.');
rebootTimeout = setTimeout(onRebootTimeout, 30 * 1 * 1000); // reboot start timeout : 30 seconds
} else if (eventData == 'failed') {
// there is a bug in particle cloud : sometimes 'spark/flash/status'
// return 'failed' but flash is ok
// so wait for reboot
if (rebooting == false) { // we can have the offline message before the flash failed message
console.log(os.EOL + 'Failed but perhaps success.');
rebootTimeout = setTimeout(onRebootTimeout, 30 * 1 * 1000); // reboot start timeout : 30 seconds
}
//errorAndQuit('Flash failed');
}
}
if (data.name == 'spark/status') {
if (eventData == 'offline') {
clearTimeout(flashTimeout);
rebooting = true;
if (rebootTimeout != -1) {
clearTimeout(rebootTimeout);
}
rebootTimeout = setTimeout(onRebootTimeout, 60 * 1 * 1000); // online again timeout : 1 minutes
process.stdout.write(os.EOL + 'Rebooting Oak');
}
if (eventData == 'online') {
if (rebooting == false) {
errorAndQuit('Oak is back online before flash sucessfull.');
}
clearTimeout(rebootTimeout);
stopProgressBar();
console.log(os.EOL + 'Oak Ready'.green);
process.exit(0);
}
}
});
}
});
});
});
}
} else {
//go to config
selectAccountAndDevice();
}
}
var progressBarInterval = -1;
function progressBar() {
process.stdout.write('.');
}
function startProgressBar(message) {
if (message != null) {
process.stdout.write(message);
}
if (progressBarInterval == -1) {
progressBarInterval = setInterval(progressBar, 500);
}
}
function stopProgressBar() {
clearInterval(progressBarInterval);
progressBarInterval = -1;
}
function onFlashTimeout() {
clearInterval(progressBarInterval);
errorAndQuit('Flash timeout - flash failed.');
}
function onRebootTimeout() {
clearInterval(progressBarInterval);
errorAndQuit('Reboot timeout - flash likely failed.');
}
function selectAccountAndDevice() {
console.log("Logging in to Particle...".green);
spark.login({accessToken: config.access_token}, function(err, data) {
if (err) {
console.log('Your access token has expired, please login.');
fs.unlinkSync(pathToConfig + 'config.json');
particleLogin();
} else {
loginCallback(false, config);
}
});
}
function particleLogin() {
clear();
//prompt for user and password
var userName = readlineSync.questionEMail('Particle Username/Email:');
var password = readlineSync.question('Particle Password:', {
hideEchoBack: true // The typed text on screen is hidden by `*` (default).
});
console.log('Logging in to Particle...'.green);
spark.login({username: userName.trim(), password: password.trim()},loginCallback);
}
function loginCallback(err, access) {
//get devices
if (err) {
console.log('Invalid username or password.'.red);
readlineSync.question('Press enter to exit.');
process.exit(1);
}
var devicesList = [];
var idsList = [];
var device;
spark.listDevices(function(err, devices) {
if (err && err.code == 'ENOTFOUND') {
console.log('Could not connect to Particle.io'.red);
readlineSync.question('Press enter to exit.');
process.exit(1);
}
if (devices === null || devices.length == 0) {
console.log('No devices available.'.red);
readlineSync.question('Press enter to exit.');
process.exit(1);
}
for (var i in devices) {
device = devices[i];
var deviceName;
if (device.name === null) {
deviceName = 'Unnamed Device';
} else {
deviceName = device.name;
}
devicesList.push(deviceName + ' (Device ID: ' + device.id + ')');
idsList.push(device.id);
}
devicesList.push('------------------------');
devicesList.push('Switch Particle Accounts');
var selectedDevice = null;
if (config !== undefined && config.devices !== undefined && activeDeviceIndex != -1 && config.devices[activeDeviceIndex].device_name !== undefined ) {
selectedDevice = config.devices[activeDeviceIndex].device_name + ' (Device ID: ' + config.devices[activeDeviceIndex].device_id + ')';
}
clear();
while (1) {
var index;
console.log('OakCLI tool version '+version);
if (selectedDevice == null) {
index = readlineSync.keyInSelect(devicesList, 'Which device would you like to use?', {cancel: 'Exit'});
} else {
console.log("Currently selected device: ".yellow.bold+selectedDevice.cyan.bold);
index = readlineSync.keyInSelect(devicesList, 'Select a device to switch active devices:', {cancel: 'Exit'});
}
if (index < 0) {
process.exit(0);
} else if (devicesList[index] == '------------------------') {
clear();
continue;
} else if (devicesList[index] == 'Switch Particle Accounts') {
particleLogin();
return;
} else {
console.log('Saving');
selectedDevice = devicesList[index];
if (writeConfig(access.access_token, devices, idsList, index)) {
clear();
console.log('Configuration saved at ' + pathToConfig + 'config.json');
console.log('You can now upload files to this device.'.green.bold);
} else {
console.log('Could not write config file to: ' + pathToConfig + 'config.json');
}
}
}
});
}
function writeConfig(accessToken, devices, idsList, selectedDeviceIndex) {
if (!mkdirp.sync(pathToConfig)) {
if (!fs.existsSync(pathToConfig)) {
console.log('Could not create config directory: '.red + pathToConfig.red);
readlineSync.question('Press enter to exit.');
process.exit(1);
}
}
// "device_id":"d957040001b0a72e4ec79884","device_name":"oak1 (Device ID: d957040001b0a72e4ec79884)"
var toWrite = {
access_token: accessToken,
devices : []
};
for (var i in devices) {
toWrite.devices.push({'device_id' : idsList[i], 'device_name' : devices[i].name === null ? 'Unnamed Device' : devices[i].name, 'active' : (i == selectedDeviceIndex ? true : false)});
if (i == selectedDeviceIndex) {
// config file old style
toWrite.device_id = idsList[i];
toWrite.device_name = devices[i].name + ' (Device ID: ' + idsList[i] + ')';
}
}
try {
fs.writeFileSync(pathToConfig+'config.json', JSON.stringify(toWrite, null, 2));
} catch(e) {
return false;
}
return true;
}
function failAndUsage(errorText) {
console.log(errorText);
console.log('------');
console.log('Usage:');
console.log('\toak [-d device name] filename - Upload the \'filename\' binary file to the \'device_name\' oak or to the default selected Oak.');
console.log('\toak - Set the Particle account and default Oak device to use when uploading.');
process.exit(1);
}
function errorAndQuit(errorMessage) {
console.log(os.EOL + 'Error'.red + ' : ' + errorMessage);
process.exit(1);
}