Skip to content

Commit 2e546d1

Browse files
author
Alberto Iannaccone
committed
add upload
1 parent 15ecad9 commit 2e546d1

File tree

3 files changed

+153
-16
lines changed

3 files changed

+153
-16
lines changed

src/daemon.js

Lines changed: 92 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
11
import { Subject, BehaviorSubject } from 'rxjs';
22
import { takeUntil, filter } from 'rxjs/operators';
33

4+
const UPLOAD_STATUS_NOPE = 'UPLOAD_STATUS_NOPE';
5+
const UPLOAD_STATUS_DONE = 'UPLOAD_STATUS_DONE';
6+
const UPLOAD_STATUS_ERROR = 'UPLOAD_STATUS_ERROR';
7+
const UPLOAD_STATUS_IN_PROGRESS = 'UPLOAD_STATUS_IN_PROGRESS';
8+
49
export default class Daemon {
510
constructor() {
611
this.socket = null;
712
this.pluginURL = null;
813
this.socketMessages = new Subject();
914
this.serialMonitorOpened = new BehaviorSubject(false);
1015
this.serialMonitorMessages = new Subject();
16+
this.uploading = new BehaviorSubject();
1117
this.devicesList = new BehaviorSubject({
1218
serial: [],
1319
network: []
1420
});
1521
this.socketMessages
1622
.subscribe(this.handleSocketMessage.bind(this));
17-
this.openingSerial = null;
18-
this.closingSerial = null;
1923

2024
const devicesListSubscription = this.devicesList.subscribe((devices) => {
2125
if (devices.serial && devices.serial.length > 0) {
2226
this.closeAllPorts();
2327
devicesListSubscription.unsubscribe();
2428
}
2529
});
26-
window.addEventListener('beforeunload', this.closeAllPorts);
2730
}
2831

2932
initSocket() {
@@ -46,7 +49,7 @@ export default class Daemon {
4649
* @param {Array<device>} a the first list
4750
* @param {Array<device>} b the second list
4851
*/
49-
devicesListAreEquals(a, b) {
52+
static devicesListAreEquals(a, b) {
5053
if (!a || !b || a.length !== b.length) {
5154
return false;
5255
}
@@ -57,13 +60,13 @@ export default class Daemon {
5760
// Result of a list command
5861
if (message.Ports) {
5962
const lastDevices = this.devicesList.getValue();
60-
if (message.Network && !this.devicesListAreEquals(lastDevices.network, message.Ports)) {
63+
if (message.Network && !Daemon.devicesListAreEquals(lastDevices.network, message.Ports)) {
6164
this.devicesList.next({
6265
serial: lastDevices.serial,
6366
network: message.Ports
6467
});
6568
}
66-
else if (!message.Network && !this.devicesListAreEquals(lastDevices.serial, message.Ports)) {
69+
else if (!message.Network && !Daemon.devicesListAreEquals(lastDevices.serial, message.Ports)) {
6770
this.devicesList.next({
6871
serial: message.Ports,
6972
network: lastDevices.network
@@ -74,6 +77,7 @@ export default class Daemon {
7477
if (message.D) {
7578
this.serialMonitorMessages.next(message.D);
7679
}
80+
// if (message.ProgrammerStatus )
7781
}
7882

7983
writeSerial(port, data) {
@@ -122,14 +126,91 @@ export default class Daemon {
122126
this.socket.emit('command', `close ${port}`);
123127
}
124128

125-
closeAllPorts(e) {
126-
if (e) {
127-
e.preventDefault();
128-
}
129+
closeAllPorts() {
129130
const devices = this.devicesList.getValue().serial;
130131
devices.forEach(device => {
131132
this.socket.emit('command', `close ${device.Name}`);
132133
});
133-
return;
134+
}
135+
136+
/**
137+
* Perform an upload via http on the daemon
138+
* target = {
139+
* board: "name of the board",
140+
* port: "port of the board",
141+
* auth_user: "Optional user to use as authentication",
142+
* auth_pass: "Optional pass to use as authentication"
143+
* auth_key: "Optional private key",
144+
* auth_port: "Optional alternative port (default 22)"
145+
* network: true or false
146+
* }
147+
* data = {
148+
* commandline: "commandline to execute",
149+
* signature: "signature of the commandline",
150+
* files: [
151+
* {name: "Name of a file to upload on the device", data: 'base64data'}
152+
* ],
153+
* options: {}
154+
* }
155+
* cb = callback function executing everytime a packet of data arrives through the websocket
156+
*/
157+
upload(target, data) {
158+
this.uploading.next({ status: UPLOAD_STATUS_IN_PROGRESS });
159+
160+
if (data.files.length === 0) { // At least one file to upload
161+
this.uploading.next({ status: UPLOAD_STATUS_ERROR, err: 'You need at least one file to upload' });
162+
return;
163+
}
164+
165+
// Main file
166+
const file = data.files[0];
167+
file.name = file.name.split('/');
168+
file.name = file.name[file.name.length - 1];
169+
170+
const payload = {
171+
board: target.board,
172+
port: target.port,
173+
commandline: data.commandline,
174+
signature: data.signature,
175+
hex: file.data,
176+
filename: file.name,
177+
extra: {
178+
auth: {
179+
username: target.auth_user,
180+
password: target.auth_pass,
181+
private_key: target.auth_key,
182+
port: target.auth_port
183+
},
184+
wait_for_upload_port: data.options.wait_for_upload_port === 'true' || data.options.wait_for_upload_port === true,
185+
use_1200bps_touch: data.options.use_1200bps_touch === 'true' || data.options.use_1200bps_touch === true,
186+
network: target.network,
187+
ssh: target.ssh,
188+
params_verbose: data.options.param_verbose,
189+
params_quiet: data.options.param_quiet,
190+
verbose: data.options.verbose
191+
},
192+
extrafiles: data.extrafiles || []
193+
};
194+
195+
for (let i = 1; i < data.files.length; i += 1) {
196+
payload.extrafiles.push({ filename: data.files[i].name, hex: data.files[i].data });
197+
}
198+
199+
fetch(`${this.pluginURL}/upload`, {
200+
method: 'POST',
201+
headers: {
202+
'Content-Type': 'text/plain; charset=utf-8'
203+
},
204+
body: JSON.stringify(payload)
205+
})
206+
.catch(error => {
207+
this.uploading.next({ status: UPLOAD_STATUS_ERROR, err: error });
208+
});
209+
}
210+
211+
212+
stopUpload() {
213+
this.uploading.next(false);
214+
this.socket.emit('command', 'killprogrammer');
134215
}
135216
}

0 commit comments

Comments
 (0)