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
-
27
26
export default class BoardConfiguration {
28
27
constructor ( daemon ) {
29
28
this . CONFIGURE_IN_PROGRESS = 'CONFIGURE_IN_PROGRESS' ;
@@ -34,12 +33,22 @@ export default class BoardConfiguration {
34
33
this . daemon = daemon ;
35
34
this . serialMonitorContent = '' ;
36
35
this . configuring = new BehaviorSubject ( { status : this . CONFIGURE_NOPE } ) ;
37
-
36
+ this . configureDone = this . configuring . pipe ( filter ( configure => configure . status === this . CONFIGURE_DONE ) ) ;
37
+ this . configureInProgress = this . configuring . pipe ( filter ( configure => configure . status === this . CONFIGURE_IN_PROGRESS ) ) ;
38
+ this . configureError = this . configuring . pipe ( filter ( configure => configure . status === this . CONFIGURE_ERROR ) ) ;
38
39
this . daemon . serialMonitorMessages . subscribe ( message => {
39
40
this . serialMonitorContent += message ;
40
41
} ) ;
41
42
}
42
43
44
+ initConfig ( ) {
45
+ this . configuring . next ( { status : this . CONFIGURE_IN_PROGRESS , msg : 'Starting board configuration...' } ) ;
46
+ }
47
+
48
+ notifyError ( msg ) {
49
+ this . configuring . next ( { status : this . CONFIGURE_ERROR , msg : msg , err : msg } ) ;
50
+ }
51
+
43
52
/**
44
53
* Returns the correct Provisioning sketch after adding fqbn
45
54
* @param {string } fqbn
@@ -55,7 +64,6 @@ export default class BoardConfiguration {
55
64
let partialMessage = '' ;
56
65
const gettingCsr = new Promise ( ( resolve , reject ) => {
57
66
const parseCsrQuestions = message => {
58
- // TODO: store partial messages
59
67
partialMessage += message ;
60
68
61
69
if ( partialMessage . indexOf ( 'No ECCX08 present' ) !== - 1 ) {
@@ -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,36 +125,13 @@ 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 ( ) ) ;
137
132
}
138
133
139
- /**
140
- * Uploads the sketch and performs action in order to configure the board for Arduino Cloud
141
- * @param {Object } compiledSketch the Object containing the provisioning sketch, ready to be compiled
142
- * @param {Object } board contains the board data
143
- * @param {function } createDeviceCb used to create the device associated to the user
144
- */
145
- configure ( compiledSketch , board , createDeviceCb ) {
146
- this . configuring . next ( { status : this . CONFIGURE_IN_PROGRESS , msg : 'Starting board configuration' } ) ;
147
- if ( ! this . daemon . channelOpen . getValue ( ) ) {
148
- const errorMessage = `Couldn't configure board at port ${ board . port } because we there is no open channel to the Arduino Create Plugin.` ;
149
- this . configuring . next ( {
150
- status : this . CONFIGURE_ERROR ,
151
- msg : errorMessage ,
152
- err : 'cannot find plugin'
153
- } ) ;
154
- return ;
155
- }
156
- this . serialMonitorContent = '' ;
157
-
134
+ uploadSketch ( compiledSketch , board ) {
158
135
const uploadTarget = {
159
136
board : board . fqbn ,
160
137
port : board . port ,
@@ -172,12 +149,34 @@ export default class BoardConfiguration {
172
149
signature : board . upload [ 0 ] . options . signature ,
173
150
extrafiles : [ ] ,
174
151
options : {
175
- wait_for_upload_port : ( board . upload [ 0 ] . options . wait_for_upload_port === true || board . upload [ 0 ] . options . wait_for_upload_port === 'true' ) , // eslint-disable-line camelcase
176
- use_1200bps_touch : ( board . upload [ 0 ] . options . use_1200bps_touch === true || board . upload [ 0 ] . options . use_1200bps_touch === 'true' ) , // eslint-disable-line camelcase
177
- params_verbose : '-v' // eslint-disable-line camelcase
152
+ wait_for_upload_port : ( board . upload [ 0 ] . options . wait_for_upload_port === true || board . upload [ 0 ] . options . wait_for_upload_port === 'true' ) ,
153
+ use_1200bps_touch : ( board . upload [ 0 ] . options . use_1200bps_touch === true || board . upload [ 0 ] . options . use_1200bps_touch === 'true' ) ,
154
+ params_verbose : '-v'
178
155
}
179
156
} ;
180
157
158
+ this . daemon . upload ( uploadTarget , uploadData ) ;
159
+ }
160
+
161
+ /**
162
+ * Uploads the sketch and performs action in order to configure the board for Arduino Cloud
163
+ * @param {Object } compiledSketch the Object containing the provisioning sketch, ready to be compiled
164
+ * @param {Object } board contains the board data
165
+ * @param {function } createDeviceCb used to create the device associated to the user
166
+ */
167
+ configure ( compiledSketch , board , createDeviceCb ) {
168
+ this . configuring . next ( { status : this . CONFIGURE_IN_PROGRESS , msg : 'Uploading provisioning sketch...' } ) ;
169
+ if ( ! this . daemon . channelOpen . getValue ( ) ) {
170
+ const errorMessage = `Couldn't configure board at port ${ board . port } because we there is no open channel to the Arduino Create Plugin.` ;
171
+ this . configuring . next ( {
172
+ status : this . CONFIGURE_ERROR ,
173
+ msg : errorMessage ,
174
+ err : 'cannot find plugin'
175
+ } ) ;
176
+ return ;
177
+ }
178
+ this . serialMonitorContent = '' ;
179
+
181
180
// check the uploading status:
182
181
if ( this . daemon . uploading . getValue ( ) . status === this . daemon . UPLOAD_IN_PROGRESS ) {
183
182
// if there is an upload in course, notify observers;
@@ -190,32 +189,53 @@ export default class BoardConfiguration {
190
189
}
191
190
192
191
this . daemon . uploadingDone . pipe ( first ( ) ) . subscribe ( ( ) => {
192
+ this . configuring . next ( {
193
+ status : this . CONFIGURE_IN_PROGRESS ,
194
+ msg : 'Provisioning sketch uploaded successfully. Opening serial monitor...'
195
+ } ) ;
193
196
this . daemon . serialMonitorOpened . pipe ( takeUntil ( this . daemon . serialMonitorOpened . pipe ( filter ( open => open ) ) ) )
194
197
. subscribe ( ( ) => {
198
+ this . configuring . next ( {
199
+ status : this . CONFIGURE_IN_PROGRESS ,
200
+ msg : 'Serial monitor opened. Generating CSR...'
201
+ } ) ;
195
202
this . getCsr ( board )
196
- . then ( csr => createDeviceCb ( csr ) )
197
- . then ( data => this . storeCertificate ( data . compressed ) )
203
+ . then ( csr => {
204
+ this . configuring . next ( {
205
+ status : this . CONFIGURE_IN_PROGRESS ,
206
+ msg : 'CSR generated. Creating device...'
207
+ } ) ;
208
+ return createDeviceCb ( csr )
209
+ } )
210
+ . then ( data => {
211
+ this . configuring . next ( {
212
+ status : this . CONFIGURE_IN_PROGRESS ,
213
+ msg : 'Device created. Storing certificate...'
214
+ } ) ;
215
+ return this . storeCertificate ( data . compressed , board ) ;
216
+ } )
198
217
. then ( ( ) => this . configuring . next ( { status : this . CONFIGURE_DONE } ) )
199
218
. catch ( reason => this . configuring . next ( {
200
219
status : this . CONFIGURE_ERROR ,
201
- msg : `Couldn't configure board at port ${ board . port } . Configuration failed with error: ${ reason } ` ,
220
+ msg : `Couldn't configure board at port ${ board . port } . Configuration failed with error: ${ reason . message } ` ,
202
221
err : reason . toString ( )
203
222
} ) )
204
223
. finally ( ( ) => this . daemon . closeSerialMonitor ( board . port , BAUDRATE ) ) ;
205
224
} , error => {
206
225
this . configuring . next ( {
207
226
status : this . CONFIGURE_ERROR ,
208
- msg : `Couldn't configure board at port ${ board . port } . Configuration failed with error: ${ error } ` ,
227
+ msg : `Couldn't configure board at port ${ board . port } . Configuration failed with error: ${ error . message } ` ,
209
228
err : error . toString ( )
210
229
} ) ;
211
230
} ) ;
212
- this . daemon . openSerialMonitor ( board . port , BAUDRATE ) ;
231
+ this . daemon . openSerialMonitor ( board . port , BAUDRATE ) ;
213
232
} ) ;
214
233
215
234
this . daemon . uploadingError . pipe ( first ( ) ) . subscribe ( upload => {
216
235
this . configuring . next ( { status : this . CONFIGURE_ERROR , err : `Couldn't configure board at port ${ board . port } . Upload failed with error: ${ upload . err } ` } ) ;
217
236
} ) ;
218
237
219
- this . daemon . upload ( uploadTarget , uploadData ) ;
238
+ this . daemon . initUpload ( ) ;
239
+ this . uploadSketch ( compiledSketch , board ) ;
220
240
}
221
241
}
0 commit comments