Skip to content

Commit 5773316

Browse files
author
Alberto Iannaccone
committed
fix messages
1 parent 783de1f commit 5773316

File tree

3 files changed

+29
-24
lines changed

3 files changed

+29
-24
lines changed

src/boardConfiguration.js

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
*/
2929

3030
import { 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) => {
4444
const 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

9793
const 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

155148
export {

src/readMessages.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ const parseMessageList = (data) => {
6161
serialPorts = data.Ports;
6262
}
6363
callback('ports', {
64-
serial: this.serialPorts,
65-
network: this.networkPorts
64+
serial: serialPorts,
65+
network: networkPorts
6666
});
6767
};
6868

src/socketDaemon.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,15 @@ const semVerCompare = (a, b) => {
8181
/**
8282
* Check the agent version and call the update if needed.
8383
*/
84-
const update = () => new Promise(resolve => {
84+
const update = () => new Promise((resolve, reject) => {
8585
if (agentInfo.version && (semVerCompare(agentInfo.version, MIN_VERSION) >= 0 || agentInfo.version.indexOf('dev') !== -1)) {
8686
resolve(agentInfo);
8787
}
8888

8989
return fetch(`${agentInfo[selectedProtocol]}/update`, {
9090
method: 'POST'
91-
}).then(() => {
92-
return deferred.reject(); // We reject the promise because the daemon will be restarted, we need to continue looking for the port
93-
});
91+
}).then(() => reject()); // We reject the promise because the daemon will be restarted, we need to continue looking for the port
92+
9493
});
9594

9695
/**
@@ -258,6 +257,19 @@ const SocketDaemon = (callbacks) => {
258257
}
259258
};
260259

260+
/**
261+
* Uploads the sketch and performs action in order to configure the board for Arduino Cloud
262+
* @param {Object} compiledSketch the Object containing the provisioning sketch, ready to be compiled
263+
* @param {Object} board contains the board data
264+
* @param {function} createDeviceCb used to create the device associated to the user
265+
*/
266+
const configureBoard = (compiledSketch, board, createDeviceCb) => {
267+
if (!isConnected()) {
268+
return Promise.reject(new Error('We were not able to generate the CSR.'));
269+
}
270+
return configure(compiledSketch, board, createDeviceCb);
271+
};
272+
261273
return {
262274
connect,
263275
perform,
@@ -266,7 +278,7 @@ const SocketDaemon = (callbacks) => {
266278
upload,
267279
stopUpload,
268280
getProvisioningSketch,
269-
configure,
281+
configureBoard,
270282
onDisconnect,
271283
onConnect,
272284
onPortsUpdate: addPortsCallback,

0 commit comments

Comments
 (0)