Skip to content

Commit c5384c3

Browse files
author
Alberto Iannaccone
committed
fix provisioning
1 parent 0013208 commit c5384c3

File tree

1 file changed

+39
-24
lines changed

1 file changed

+39
-24
lines changed

src/boardConfiguration.js

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
*
1919
*/
2020

21-
import { BehaviorSubject } from 'rxjs';
21+
import { BehaviorSubject, timer } from 'rxjs';
2222
import { takeUntil, filter, first } from 'rxjs/operators';
2323
import { provisioningSketch } from './sketches/provisioning.ino';
2424

2525
const BAUDRATE = 9600;
26-
26+
const UPLOAD_DONE_TIMER = 5000;
2727
export default class BoardConfiguration {
2828
constructor(daemon) {
2929
this.CONFIGURE_IN_PROGRESS = 'CONFIGURE_IN_PROGRESS';
@@ -40,6 +40,14 @@ export default class BoardConfiguration {
4040
});
4141
}
4242

43+
initConfig() {
44+
this.configuring.next({ status: this.CONFIGURE_IN_PROGRESS, msg: 'Starting board configuration...' });
45+
}
46+
47+
notifyError(msg) {
48+
this.configuring.next({ status: this.CONFIGURE_ERROR, msg: msg, err: msg});
49+
}
50+
4351
/**
4452
* Returns the correct Provisioning sketch after adding fqbn
4553
* @param {string} fqbn
@@ -72,19 +80,11 @@ export default class BoardConfiguration {
7280
}
7381
if (partialMessage.indexOf('Would you like to generate a new private key and CSR (y/N):') !== -1) {
7482
partialMessage = '';
75-
const serialData = {
76-
com_name: board.port,
77-
data: 'y\n'
78-
};
79-
this.daemon.writeSerial(board.port, serialData);
83+
this.daemon.writeSerial(board.port, 'y\n');
8084
}
8185
if (partialMessage.indexOf('Your ECCX08 is unlocked, would you like to lock it (y/N):') !== -1) {
8286
partialMessage = '';
83-
const serialData = {
84-
com_name: board.port,
85-
data: 'y\n'
86-
};
87-
this.daemon.writeSerial(board.port, serialData);
87+
this.daemon.writeSerial(board.port, 'y\n');
8888
}
8989

9090
const begin = partialMessage.indexOf('-----BEGIN CERTIFICATE REQUEST-----');
@@ -125,12 +125,7 @@ export default class BoardConfiguration {
125125
(notAfter.getUTCFullYear() - notBefore.getUTCFullYear()) + '\n' +
126126
compressedCert.serial + '\n' +
127127
compressedCert.signature + '\n';
128-
129-
const serialData = {
130-
com_name: board.port,
131-
data: answers
132-
};
133-
this.daemon.writeSerial(board.port, serialData);
128+
this.daemon.writeSerial(board.port, answers);
134129
});
135130

136131
return storing.finally(() => this.serialMessagesSubscription.unsubscribe());
@@ -143,7 +138,7 @@ export default class BoardConfiguration {
143138
* @param {function} createDeviceCb used to create the device associated to the user
144139
*/
145140
configure(compiledSketch, board, createDeviceCb) {
146-
this.configuring.next({ status: this.CONFIGURE_IN_PROGRESS, msg: 'Starting board configuration' });
141+
this.configuring.next({ status: this.CONFIGURE_IN_PROGRESS, msg: 'Uploading provisioning sketch...' });
147142
if (!this.daemon.channelOpen.getValue()) {
148143
const errorMessage = `Couldn't configure board at port ${board.port} because we there is no open channel to the Arduino Create Plugin.`;
149144
this.configuring.next({
@@ -190,26 +185,46 @@ export default class BoardConfiguration {
190185
}
191186

192187
this.daemon.uploadingDone.pipe(first()).subscribe(() => {
188+
this.configuring.next({
189+
status: this.CONFIGURE_IN_PROGRESS,
190+
msg: 'Provisioning sketch uploaded successfully. Opening serial monitor...'
191+
});
193192
this.daemon.serialMonitorOpened.pipe(takeUntil(this.daemon.serialMonitorOpened.pipe(filter(open => open))))
194193
.subscribe(() => {
194+
this.configuring.next({
195+
status: this.CONFIGURE_IN_PROGRESS,
196+
msg: 'Serial monitor opened. Generating CSR...'
197+
});
195198
this.getCsr(board)
196-
.then(csr => createDeviceCb(csr))
197-
.then(data => this.storeCertificate(data.compressed))
199+
.then(csr => {
200+
this.configuring.next({
201+
status: this.CONFIGURE_IN_PROGRESS,
202+
msg: 'CSR generated. Creating device...'
203+
});
204+
return createDeviceCb(csr)
205+
})
206+
.then(data => {
207+
this.configuring.next({
208+
status: this.CONFIGURE_IN_PROGRESS,
209+
msg: 'Device created. Storing certificate...'
210+
});
211+
return this.storeCertificate(data.compressed, board);
212+
})
198213
.then(() => this.configuring.next({ status: this.CONFIGURE_DONE }))
199214
.catch(reason => this.configuring.next({
200215
status: this.CONFIGURE_ERROR,
201-
msg: `Couldn't configure board at port ${board.port}. Configuration failed with error: ${reason}`,
216+
msg: `Couldn't configure board at port ${board.port}. Configuration failed with error: ${reason.message}`,
202217
err: reason.toString()
203218
}))
204219
.finally(() => this.daemon.closeSerialMonitor(board.port, BAUDRATE));
205220
}, error => {
206221
this.configuring.next({
207222
status: this.CONFIGURE_ERROR,
208-
msg: `Couldn't configure board at port ${board.port}. Configuration failed with error: ${error}`,
223+
msg: `Couldn't configure board at port ${board.port}. Configuration failed with error: ${error.message}`,
209224
err: error.toString()
210225
});
211226
});
212-
this.daemon.openSerialMonitor(board.port, BAUDRATE);
227+
timer(UPLOAD_DONE_TIMER).subscribe(() => this.daemon.openSerialMonitor(board.port, BAUDRATE));
213228
});
214229

215230
this.daemon.uploadingError.pipe(first()).subscribe(upload => {

0 commit comments

Comments
 (0)