2828 */
2929
3030import { provisioningSketch } from './sketches/provisioning.ino' ;
31- import SocketDaemon from './socketDaemon ' ;
31+ import { perform , upload , addSerialCallback } from './readMessages ' ;
3232
3333/**
3434 * Returns the correct Provisioning sketch after adding fqbn
@@ -44,10 +44,6 @@ const getProvisioningSketch = (fqbn) => {
4444const getCsr = ( board ) => {
4545 let partialCsr = '' ;
4646 let gettingCsr = new Promise ( ( resolve , reject ) => {
47- if ( ! SocketDaemon . isConnected ( ) ) {
48- return reject ( new Error ( 'We were not able to generate the CSR.' ) ) ;
49- }
50-
5147 const parseCsrQuestions = message => {
5248 // TODO: store partial messages
5349
@@ -68,14 +64,14 @@ const getCsr = (board) => {
6864 com_name : board . port ,
6965 data : 'y\n'
7066 } ;
71- SocketDaemon . perform ( 'req_serial_monitor_write' , serialData ) ;
67+ perform ( 'req_serial_monitor_write' , serialData ) ;
7268 }
7369 if ( message . indexOf ( 'Your ECCX08 is unlocked, would you like to lock it (y/N):' ) !== - 1 ) {
7470 const serialData = {
7571 com_name : board . port ,
7672 data : 'y\n'
7773 } ;
78- SocketDaemon . perform ( 'req_serial_monitor_write' , serialData ) ;
74+ perform ( 'req_serial_monitor_write' , serialData ) ;
7975 }
8076 partialCsr += message ;
8177 const begin = partialCsr . indexOf ( '-----BEGIN CERTIFICATE REQUEST-----' ) ;
@@ -86,7 +82,7 @@ const getCsr = (board) => {
8682 }
8783 } ;
8884
89- SocketDaemon . onSerialOutput ( parseCsrQuestions ) ;
85+ addSerialCallback ( parseCsrQuestions ) ;
9086 } )
9187 . finally ( ( ) => {
9288 gettingCsr = false ;
@@ -96,9 +92,6 @@ const getCsr = (board) => {
9692
9793const storeCertificate = ( compressedCert , board ) => {
9894 const storing = new Promise ( ( resolve , reject ) => {
99- if ( ! SocketDaemon . isConnected ( ) ) {
100- return reject ( new Error ( 'We were not able to store the certificate on your board' ) ) ;
101- }
10295
10396 const parseCsr = ( message ) => {
10497 if ( message . indexOf ( 'Compressed cert' ) !== - 1 ) {
@@ -108,7 +101,7 @@ const storeCertificate = (compressedCert, board) => {
108101 return reject ( new Error ( message ) ) ;
109102 }
110103 } ;
111- SocketDaemon . onSerialOutput ( parseCsr ) ;
104+ addSerialCallback ( parseCsr ) ;
112105 const notBefore = new Date ( compressedCert . not_before ) ;
113106 const notAfter = new Date ( compressedCert . not_after ) ;
114107 // eslint-disable-next-line prefer-template
@@ -125,7 +118,7 @@ const storeCertificate = (compressedCert, board) => {
125118 com_name : board . port ,
126119 data : answers
127120 } ;
128- SocketDaemon . perform ( 'req_serial_monitor_write' , serialData ) ;
121+ perform ( 'req_serial_monitor_write' , serialData ) ;
129122 } ) ;
130123
131124 return storing ;
@@ -143,13 +136,13 @@ const configure = (compiledSketch, board, createDeviceCb) => {
143136 baudrate : 9600
144137 } ;
145138
146- SocketDaemon . upload ( board , compiledSketch )
147- . then ( ( ) => SocketDaemon . perform ( 'req_serial_monitor_open' , serialData ) )
139+ upload ( board , compiledSketch )
140+ . then ( ( ) => perform ( 'req_serial_monitor_open' , serialData ) )
148141 . then ( ( ) => getCsr ( board ) )
149- . then ( csr => createDeviceCb ( board . customName , board . id , csr ) )
142+ . then ( csr => createDeviceCb ( csr ) )
150143 . then ( data => storeCertificate ( data . compressed ) )
151144 . catch ( reason => new Error ( `Couldn't configure board: ${ reason } ` ) )
152- . finally ( ( ) => SocketDaemon . perform ( 'req_serial_monitor_close' , serialData ) ) ;
145+ . finally ( ( ) => perform ( 'req_serial_monitor_close' , serialData ) ) ;
153146} ;
154147
155148export {
0 commit comments