18
18
*
19
19
*/
20
20
21
- import { BehaviorSubject } from 'rxjs' ;
21
+ import { BehaviorSubject , timer } from 'rxjs' ;
22
22
import { takeUntil , filter , first } from 'rxjs/operators' ;
23
23
import { provisioningSketch } from './sketches/provisioning.ino' ;
24
24
25
25
const BAUDRATE = 9600 ;
26
-
26
+ const UPLOAD_DONE_TIMER = 5000 ;
27
27
export default class BoardConfiguration {
28
28
constructor ( daemon ) {
29
29
this . CONFIGURE_IN_PROGRESS = 'CONFIGURE_IN_PROGRESS' ;
@@ -40,6 +40,14 @@ export default class BoardConfiguration {
40
40
} ) ;
41
41
}
42
42
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
+
43
51
/**
44
52
* Returns the correct Provisioning sketch after adding fqbn
45
53
* @param {string } fqbn
@@ -72,19 +80,11 @@ export default class BoardConfiguration {
72
80
}
73
81
if ( partialMessage . indexOf ( 'Would you like to generate a new private key and CSR (y/N):' ) !== - 1 ) {
74
82
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' ) ;
80
84
}
81
85
if ( partialMessage . indexOf ( 'Your ECCX08 is unlocked, would you like to lock it (y/N):' ) !== - 1 ) {
82
86
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' ) ;
88
88
}
89
89
90
90
const begin = partialMessage . indexOf ( '-----BEGIN CERTIFICATE REQUEST-----' ) ;
@@ -125,12 +125,7 @@ export default class BoardConfiguration {
125
125
( notAfter . getUTCFullYear ( ) - notBefore . getUTCFullYear ( ) ) + '\n' +
126
126
compressedCert . serial + '\n' +
127
127
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 ) ;
134
129
} ) ;
135
130
136
131
return storing . finally ( ( ) => this . serialMessagesSubscription . unsubscribe ( ) ) ;
@@ -143,7 +138,7 @@ export default class BoardConfiguration {
143
138
* @param {function } createDeviceCb used to create the device associated to the user
144
139
*/
145
140
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... ' } ) ;
147
142
if ( ! this . daemon . channelOpen . getValue ( ) ) {
148
143
const errorMessage = `Couldn't configure board at port ${ board . port } because we there is no open channel to the Arduino Create Plugin.` ;
149
144
this . configuring . next ( {
@@ -190,26 +185,46 @@ export default class BoardConfiguration {
190
185
}
191
186
192
187
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
+ } ) ;
193
192
this . daemon . serialMonitorOpened . pipe ( takeUntil ( this . daemon . serialMonitorOpened . pipe ( filter ( open => open ) ) ) )
194
193
. subscribe ( ( ) => {
194
+ this . configuring . next ( {
195
+ status : this . CONFIGURE_IN_PROGRESS ,
196
+ msg : 'Serial monitor opened. Generating CSR...'
197
+ } ) ;
195
198
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
+ } )
198
213
. then ( ( ) => this . configuring . next ( { status : this . CONFIGURE_DONE } ) )
199
214
. catch ( reason => this . configuring . next ( {
200
215
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 } ` ,
202
217
err : reason . toString ( )
203
218
} ) )
204
219
. finally ( ( ) => this . daemon . closeSerialMonitor ( board . port , BAUDRATE ) ) ;
205
220
} , error => {
206
221
this . configuring . next ( {
207
222
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 } ` ,
209
224
err : error . toString ( )
210
225
} ) ;
211
226
} ) ;
212
- this . daemon . openSerialMonitor ( board . port , BAUDRATE ) ;
227
+ timer ( UPLOAD_DONE_TIMER ) . subscribe ( ( ) => this . daemon . openSerialMonitor ( board . port , BAUDRATE ) ) ;
213
228
} ) ;
214
229
215
230
this . daemon . uploadingError . pipe ( first ( ) ) . subscribe ( upload => {
0 commit comments