28
28
*/
29
29
30
30
import { provisioningSketch } from './sketches/provisioning.ino' ;
31
- import SocketDaemon from './socketDaemon ' ;
31
+ import { perform , upload , addSerialCallback } from './readMessages ' ;
32
32
33
33
/**
34
34
* Returns the correct Provisioning sketch after adding fqbn
@@ -44,10 +44,6 @@ const getProvisioningSketch = (fqbn) => {
44
44
const getCsr = ( board ) => {
45
45
let partialCsr = '' ;
46
46
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
-
51
47
const parseCsrQuestions = message => {
52
48
// TODO: store partial messages
53
49
@@ -68,14 +64,14 @@ const getCsr = (board) => {
68
64
com_name : board . port ,
69
65
data : 'y\n'
70
66
} ;
71
- SocketDaemon . perform ( 'req_serial_monitor_write' , serialData ) ;
67
+ perform ( 'req_serial_monitor_write' , serialData ) ;
72
68
}
73
69
if ( message . indexOf ( 'Your ECCX08 is unlocked, would you like to lock it (y/N):' ) !== - 1 ) {
74
70
const serialData = {
75
71
com_name : board . port ,
76
72
data : 'y\n'
77
73
} ;
78
- SocketDaemon . perform ( 'req_serial_monitor_write' , serialData ) ;
74
+ perform ( 'req_serial_monitor_write' , serialData ) ;
79
75
}
80
76
partialCsr += message ;
81
77
const begin = partialCsr . indexOf ( '-----BEGIN CERTIFICATE REQUEST-----' ) ;
@@ -86,7 +82,7 @@ const getCsr = (board) => {
86
82
}
87
83
} ;
88
84
89
- SocketDaemon . onSerialOutput ( parseCsrQuestions ) ;
85
+ addSerialCallback ( parseCsrQuestions ) ;
90
86
} )
91
87
. finally ( ( ) => {
92
88
gettingCsr = false ;
@@ -96,9 +92,6 @@ const getCsr = (board) => {
96
92
97
93
const storeCertificate = ( compressedCert , board ) => {
98
94
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
- }
102
95
103
96
const parseCsr = ( message ) => {
104
97
if ( message . indexOf ( 'Compressed cert' ) !== - 1 ) {
@@ -108,7 +101,7 @@ const storeCertificate = (compressedCert, board) => {
108
101
return reject ( new Error ( message ) ) ;
109
102
}
110
103
} ;
111
- SocketDaemon . onSerialOutput ( parseCsr ) ;
104
+ addSerialCallback ( parseCsr ) ;
112
105
const notBefore = new Date ( compressedCert . not_before ) ;
113
106
const notAfter = new Date ( compressedCert . not_after ) ;
114
107
// eslint-disable-next-line prefer-template
@@ -125,7 +118,7 @@ const storeCertificate = (compressedCert, board) => {
125
118
com_name : board . port ,
126
119
data : answers
127
120
} ;
128
- SocketDaemon . perform ( 'req_serial_monitor_write' , serialData ) ;
121
+ perform ( 'req_serial_monitor_write' , serialData ) ;
129
122
} ) ;
130
123
131
124
return storing ;
@@ -143,13 +136,13 @@ const configure = (compiledSketch, board, createDeviceCb) => {
143
136
baudrate : 9600
144
137
} ;
145
138
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 ) )
148
141
. then ( ( ) => getCsr ( board ) )
149
- . then ( csr => createDeviceCb ( board . customName , board . id , csr ) )
142
+ . then ( csr => createDeviceCb ( csr ) )
150
143
. then ( data => storeCertificate ( data . compressed ) )
151
144
. 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 ) ) ;
153
146
} ;
154
147
155
148
export {
0 commit comments