27
27
* the GNU General Public License.
28
28
*/
29
29
30
- import { BehaviorSubject } from 'rxjs' ;
30
+ import { BehaviorSubject , timer } from 'rxjs' ;
31
31
import { takeUntil , filter , first } from 'rxjs/operators' ;
32
32
import { provisioningSketch } from './sketches/provisioning.ino' ;
33
33
34
34
const BAUDRATE = 9600 ;
35
-
35
+ const UPLOAD_DONE_TIMER = 5000 ;
36
36
export default class BoardConfiguration {
37
37
constructor ( daemon ) {
38
38
this . CONFIGURE_IN_PROGRESS = 'CONFIGURE_IN_PROGRESS' ;
@@ -49,6 +49,14 @@ export default class BoardConfiguration {
49
49
} ) ;
50
50
}
51
51
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
+
52
60
/**
53
61
* Returns the correct Provisioning sketch after adding fqbn
54
62
* @param {string } fqbn
@@ -81,19 +89,11 @@ export default class BoardConfiguration {
81
89
}
82
90
if ( partialMessage . indexOf ( 'Would you like to generate a new private key and CSR (y/N):' ) !== - 1 ) {
83
91
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' ) ;
89
93
}
90
94
if ( partialMessage . indexOf ( 'Your ECCX08 is unlocked, would you like to lock it (y/N):' ) !== - 1 ) {
91
95
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' ) ;
97
97
}
98
98
99
99
const begin = partialMessage . indexOf ( '-----BEGIN CERTIFICATE REQUEST-----' ) ;
@@ -134,12 +134,7 @@ export default class BoardConfiguration {
134
134
( notAfter . getUTCFullYear ( ) - notBefore . getUTCFullYear ( ) ) + '\n' +
135
135
compressedCert . serial + '\n' +
136
136
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 ) ;
143
138
} ) ;
144
139
145
140
return storing . finally ( ( ) => this . serialMessagesSubscription . unsubscribe ( ) ) ;
@@ -152,7 +147,7 @@ export default class BoardConfiguration {
152
147
* @param {function } createDeviceCb used to create the device associated to the user
153
148
*/
154
149
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... ' } ) ;
156
151
if ( ! this . daemon . channelOpen . getValue ( ) ) {
157
152
const errorMessage = `Couldn't configure board at port ${ board . port } because we there is no open channel to the Arduino Create Plugin.` ;
158
153
this . configuring . next ( {
@@ -199,26 +194,46 @@ export default class BoardConfiguration {
199
194
}
200
195
201
196
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
+ } ) ;
202
201
this . daemon . serialMonitorOpened . pipe ( takeUntil ( this . daemon . serialMonitorOpened . pipe ( filter ( open => open ) ) ) )
203
202
. subscribe ( ( ) => {
203
+ this . configuring . next ( {
204
+ status : this . CONFIGURE_IN_PROGRESS ,
205
+ msg : 'Serial monitor opened. Generating CSR...'
206
+ } ) ;
204
207
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
+ } )
207
222
. then ( ( ) => this . configuring . next ( { status : this . CONFIGURE_DONE } ) )
208
223
. catch ( reason => this . configuring . next ( {
209
224
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 } ` ,
211
226
err : reason . toString ( )
212
227
} ) )
213
228
. finally ( ( ) => this . daemon . closeSerialMonitor ( board . port , BAUDRATE ) ) ;
214
229
} , error => {
215
230
this . configuring . next ( {
216
231
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 } ` ,
218
233
err : error . toString ( )
219
234
} ) ;
220
235
} ) ;
221
- this . daemon . openSerialMonitor ( board . port , BAUDRATE ) ;
236
+ timer ( UPLOAD_DONE_TIMER ) . subscribe ( ( ) => this . daemon . openSerialMonitor ( board . port , BAUDRATE ) ) ;
222
237
} ) ;
223
238
224
239
this . daemon . uploadingError . pipe ( first ( ) ) . subscribe ( upload => {
0 commit comments