Skip to content

Commit ca0533f

Browse files
author
Alberto Iannaccone
committed
fix provisioning
1 parent 55d5910 commit ca0533f

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
@@ -27,12 +27,12 @@
2727
* the GNU General Public License.
2828
*/
2929

30-
import { BehaviorSubject } from 'rxjs';
30+
import { BehaviorSubject, timer } from 'rxjs';
3131
import { takeUntil, filter, first } from 'rxjs/operators';
3232
import { provisioningSketch } from './sketches/provisioning.ino';
3333

3434
const BAUDRATE = 9600;
35-
35+
const UPLOAD_DONE_TIMER = 5000;
3636
export default class BoardConfiguration {
3737
constructor(daemon) {
3838
this.CONFIGURE_IN_PROGRESS = 'CONFIGURE_IN_PROGRESS';
@@ -49,6 +49,14 @@ export default class BoardConfiguration {
4949
});
5050
}
5151

52+
initConfig() {
53+
this.configuring.next({ status: this.CONFIGURE_IN_PROGRESS, msg: 'Starting board configuration...' });
54+
}
55+
56+
notifyError(msg) {
57+
this.configuring.next({ status: this.CONFIGURE_ERROR, msg: msg, err: msg});
58+
}
59+
5260
/**
5361
* Returns the correct Provisioning sketch after adding fqbn
5462
* @param {string} fqbn
@@ -81,19 +89,11 @@ export default class BoardConfiguration {
8189
}
8290
if (partialMessage.indexOf('Would you like to generate a new private key and CSR (y/N):') !== -1) {
8391
partialMessage = '';
84-
const serialData = {
85-
com_name: board.port,
86-
data: 'y\n'
87-
};
88-
this.daemon.writeSerial(board.port, serialData);
92+
this.daemon.writeSerial(board.port, 'y\n');
8993
}
9094
if (partialMessage.indexOf('Your ECCX08 is unlocked, would you like to lock it (y/N):') !== -1) {
9195
partialMessage = '';
92-
const serialData = {
93-
com_name: board.port,
94-
data: 'y\n'
95-
};
96-
this.daemon.writeSerial(board.port, serialData);
96+
this.daemon.writeSerial(board.port, 'y\n');
9797
}
9898

9999
const begin = partialMessage.indexOf('-----BEGIN CERTIFICATE REQUEST-----');
@@ -134,12 +134,7 @@ export default class BoardConfiguration {
134134
(notAfter.getUTCFullYear() - notBefore.getUTCFullYear()) + '\n' +
135135
compressedCert.serial + '\n' +
136136
compressedCert.signature + '\n';
137-
138-
const serialData = {
139-
com_name: board.port,
140-
data: answers
141-
};
142-
this.daemon.writeSerial(board.port, serialData);
137+
this.daemon.writeSerial(board.port, answers);
143138
});
144139

145140
return storing.finally(() => this.serialMessagesSubscription.unsubscribe());
@@ -152,7 +147,7 @@ export default class BoardConfiguration {
152147
* @param {function} createDeviceCb used to create the device associated to the user
153148
*/
154149
configure(compiledSketch, board, createDeviceCb) {
155-
this.configuring.next({ status: this.CONFIGURE_IN_PROGRESS, msg: 'Starting board configuration' });
150+
this.configuring.next({ status: this.CONFIGURE_IN_PROGRESS, msg: 'Uploading provisioning sketch...' });
156151
if (!this.daemon.channelOpen.getValue()) {
157152
const errorMessage = `Couldn't configure board at port ${board.port} because we there is no open channel to the Arduino Create Plugin.`;
158153
this.configuring.next({
@@ -199,26 +194,46 @@ export default class BoardConfiguration {
199194
}
200195

201196
this.daemon.uploadingDone.pipe(first()).subscribe(() => {
197+
this.configuring.next({
198+
status: this.CONFIGURE_IN_PROGRESS,
199+
msg: 'Provisioning sketch uploaded successfully. Opening serial monitor...'
200+
});
202201
this.daemon.serialMonitorOpened.pipe(takeUntil(this.daemon.serialMonitorOpened.pipe(filter(open => open))))
203202
.subscribe(() => {
203+
this.configuring.next({
204+
status: this.CONFIGURE_IN_PROGRESS,
205+
msg: 'Serial monitor opened. Generating CSR...'
206+
});
204207
this.getCsr(board)
205-
.then(csr => createDeviceCb(csr))
206-
.then(data => this.storeCertificate(data.compressed))
208+
.then(csr => {
209+
this.configuring.next({
210+
status: this.CONFIGURE_IN_PROGRESS,
211+
msg: 'CSR generated. Creating device...'
212+
});
213+
return createDeviceCb(csr)
214+
})
215+
.then(data => {
216+
this.configuring.next({
217+
status: this.CONFIGURE_IN_PROGRESS,
218+
msg: 'Device created. Storing certificate...'
219+
});
220+
return this.storeCertificate(data.compressed, board);
221+
})
207222
.then(() => this.configuring.next({ status: this.CONFIGURE_DONE }))
208223
.catch(reason => this.configuring.next({
209224
status: this.CONFIGURE_ERROR,
210-
msg: `Couldn't configure board at port ${board.port}. Configuration failed with error: ${reason}`,
225+
msg: `Couldn't configure board at port ${board.port}. Configuration failed with error: ${reason.message}`,
211226
err: reason.toString()
212227
}))
213228
.finally(() => this.daemon.closeSerialMonitor(board.port, BAUDRATE));
214229
}, error => {
215230
this.configuring.next({
216231
status: this.CONFIGURE_ERROR,
217-
msg: `Couldn't configure board at port ${board.port}. Configuration failed with error: ${error}`,
232+
msg: `Couldn't configure board at port ${board.port}. Configuration failed with error: ${error.message}`,
218233
err: error.toString()
219234
});
220235
});
221-
this.daemon.openSerialMonitor(board.port, BAUDRATE);
236+
timer(UPLOAD_DONE_TIMER).subscribe(() => this.daemon.openSerialMonitor(board.port, BAUDRATE));
222237
});
223238

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

0 commit comments

Comments
 (0)