Skip to content

Commit 815e0b3

Browse files
author
Stefania
committed
completed moving things
1 parent b14e1af commit 815e0b3

File tree

6 files changed

+255
-181
lines changed

6 files changed

+255
-181
lines changed

src/chrome-app-daemon.js

Lines changed: 99 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ import { takeUntil, filter } from 'rxjs/operators';
3030

3131
import Daemon from './daemon';
3232

33+
const UPLOAD_DONE = 'UPLOAD_DONE';
34+
const UPLOAD_ERROR = 'UPLOAD_ERROR';
35+
const UPLOAD_IN_PROGRESS = 'UPLOAD_IN_PROGRESS';
36+
3337
export default class ChromeOsDaemon extends Daemon {
3438
constructor(chromeExtensionId) {
3539
super();
@@ -67,24 +71,41 @@ export default class ChromeOsDaemon extends Daemon {
6771

6872
handleAppMessage(message) {
6973
if (message.ports) {
70-
const lastDevices = this.devicesList.getValue();
71-
if (!Daemon.devicesListAreEquals(lastDevices.serial, message.ports)) {
72-
this.devicesList.next({
73-
serial: message.ports.map(port => ({
74-
Name: port.name,
75-
SerialNumber: port.serialNumber,
76-
IsOpen: port.isOpen,
77-
VendorID: port.vendorId,
78-
ProductID: port.productId
79-
})),
80-
network: []
81-
});
82-
}
74+
this.handleListMessage(message);
8375
}
8476

8577
if (message.supportedBoards) {
8678
this.supportedBoards.next(message.supportedBoards);
8779
}
80+
81+
if (message.serialData) {
82+
this.serialMonitorMessages.next(message.serialData);
83+
}
84+
85+
if (message.programmerstatus) {
86+
this.handleUploadMessage(message);
87+
}
88+
89+
if (message.err) {
90+
this.uploading.next({ status: UPLOAD_ERROR, err: message.Err });
91+
}
92+
}
93+
94+
95+
handleListMessage(message) {
96+
const lastDevices = this.devicesList.getValue();
97+
if (!Daemon.devicesListAreEquals(lastDevices.serial, message.ports)) {
98+
this.devicesList.next({
99+
serial: message.ports.map(port => ({
100+
Name: port.name,
101+
SerialNumber: port.serialNumber,
102+
IsOpen: port.isOpen,
103+
VendorID: port.vendorId,
104+
ProductID: port.productId
105+
})),
106+
network: []
107+
});
108+
}
88109
}
89110

90111
/**
@@ -93,12 +114,7 @@ export default class ChromeOsDaemon extends Daemon {
93114
closeAllPorts() {
94115
const devices = this.devicesList.getValue().serial;
95116
devices.forEach(device => {
96-
this.channel.postMessage({
97-
command: 'closePort',
98-
data: {
99-
name: device.Name
100-
}
101-
});
117+
this.closeSerialMonitor(device.Name);
102118
});
103119
}
104120

@@ -107,7 +123,7 @@ export default class ChromeOsDaemon extends Daemon {
107123
* @param {string} port the port name
108124
* @param {string} message the text to be sent to serial
109125
*/
110-
writeSerialCommand(port, message) {
126+
writeSerial(port, message) {
111127
this.channel.postMessage({
112128
command: 'writePort',
113129
data: {
@@ -178,12 +194,72 @@ export default class ChromeOsDaemon extends Daemon {
178194
});
179195
}
180196

197+
handleUploadMessage(message) {
198+
if (this.uploading.getValue().status !== UPLOAD_IN_PROGRESS) {
199+
return;
200+
}
201+
switch (message.uploadStatus) {
202+
case 'message':
203+
this.uploading.next({ status: UPLOAD_IN_PROGRESS, msg: message.message });
204+
break;
205+
case 'error':
206+
this.uploading.next({ status: UPLOAD_ERROR, err: message.message });
207+
break;
208+
case 'success':
209+
this.uploading.next({ status: UPLOAD_DONE, err: message.message });
210+
break;
211+
default:
212+
this.uploading.next({ status: UPLOAD_IN_PROGRESS });
213+
}
214+
}
215+
216+
/**
217+
* Perform an upload via http on the daemon
218+
* @param {Object} target = {
219+
* board: "name of the board",
220+
* port: "port of the board",
221+
* }
222+
* @param {Object} data = {
223+
* commandline: "commandline to execute",
224+
* files: [
225+
* {name: "Name of a file to upload on the device", data: 'base64data'}
226+
* ],
227+
* }
228+
*/
229+
upload(target, data) {
230+
this.uploading.next({ status: UPLOAD_IN_PROGRESS });
231+
232+
if (data.files.length === 0) { // At least one file to upload
233+
this.uploading.next({ status: UPLOAD_ERROR, err: 'You need at least one file to upload' });
234+
return;
235+
}
236+
237+
// Main file
238+
const file = data.files[0];
239+
file.name = file.name.split('/');
240+
file.name = file.name[file.name.length - 1];
241+
242+
const payload = {
243+
board: target.board,
244+
port: target.port,
245+
commandline: data.commandline,
246+
filename: file.name,
247+
data: file.data,
248+
};
249+
250+
window.oauth.token().then(token => {
251+
payload.token = token.token;
252+
this.channel.postMessage({
253+
command: 'upload',
254+
data: payload
255+
});
256+
});
257+
}
181258

182259
/**
183-
* Interrupt upload
260+
* Interrupt upload - not supported in Chrome app
184261
*/
185262
stopUpload() {
186-
this.uploading.next(false);
187-
this.socket.emit('command', 'killprogrammer');
263+
return new Error('Stop Upload not supported on Chrome OS');
188264
}
189265
}

src/chromeAppDaemon.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ const onMessage = (msg) => {
138138
promise.reject(msg.message);
139139
}
140140
} */
141-
else if (msg.serialData) {
141+
/* else if (msg.serialData) {
142142
callback('serial', { data: msg.serialData });
143-
}
143+
} */
144144
};
145145

146146
/*
@@ -250,11 +250,11 @@ const upload = (target, data, cb) => {
250250
port.postMessage({
251251
command: 'upload',
252252
data: {
253-
filename: file.name,
254-
data: file.data,
255253
board: target.board,
256254
port: target.port,
257255
commandline: data.commandline,
256+
filename: file.name,
257+
data: file.data,
258258
token: token.token
259259
}
260260
});

src/daemon.js

Lines changed: 2 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
import { Subject, BehaviorSubject, interval } from 'rxjs';
22
import { takeUntil, filter, startWith } from 'rxjs/operators';
33

4-
const UPLOAD__NOPE = 'UPLOAD__NOPE';
5-
const UPLOAD__DONE = 'UPLOAD__DONE';
6-
const UPLOAD__ERROR = 'UPLOAD__ERROR';
7-
const UPLOAD__IN_PROGRESS = 'UPLOAD__IN_PROGRESS';
8-
94
const POLLING_INTERVAL = 1500;
5+
const UPLOAD_NOPE = 'UPLOAD_NOPE';
106

117
export default class Daemon {
128
constructor() {
@@ -17,7 +13,7 @@ export default class Daemon {
1713
this.appMessages = new Subject();
1814
this.serialMonitorOpened = new BehaviorSubject(false);
1915
this.serialMonitorMessages = new Subject();
20-
this.uploading = new BehaviorSubject({ status: UPLOAD__NOPE });
16+
this.uploading = new BehaviorSubject({ status: UPLOAD_NOPE });
2117
this.devicesList = new BehaviorSubject({
2218
serial: [],
2319
network: []
@@ -60,144 +56,4 @@ export default class Daemon {
6056
}
6157
return a.every((item, index) => (b[index].Name === item.Name && b[index].IsOpen === item.IsOpen));
6258
}
63-
64-
handleAppMessage(message) {
65-
// Result of a list command
66-
if (message.Ports) {
67-
this.handleListMessage(message);
68-
}
69-
// Serial monitor message
70-
if (message.D) {
71-
this.serialMonitorMessages.next(message.D);
72-
}
73-
74-
if (message.ProgrammerStatus) {
75-
this.handleUploadMessage(message);
76-
}
77-
78-
if (message.Err) {
79-
this.uploading.next({ status: UPLOAD__ERROR, err: message.Err });
80-
}
81-
}
82-
83-
handleListMessage(message) {
84-
const lastDevices = this.devicesList.getValue();
85-
if (message.Network && !Daemon.devicesListAreEquals(lastDevices.network, message.Ports)) {
86-
this.devicesList.next({
87-
serial: lastDevices.serial,
88-
network: message.Ports
89-
});
90-
}
91-
else if (!message.Network && !Daemon.devicesListAreEquals(lastDevices.serial, message.Ports)) {
92-
this.devicesList.next({
93-
serial: message.Ports,
94-
network: lastDevices.network
95-
});
96-
}
97-
}
98-
99-
handleUploadMessage(message) {
100-
if (this.uploading.getValue().status !== UPLOAD__IN_PROGRESS) {
101-
return;
102-
}
103-
if (message.Flash === 'Ok' && message.ProgrammerStatus === 'Done') {
104-
this.uploading.next({ status: UPLOAD__DONE, msg: message.Flash });
105-
return;
106-
}
107-
switch (message.ProgrammerStatus) {
108-
case 'Starting':
109-
this.uploading.next({ status: UPLOAD__IN_PROGRESS, msg: `Programming with: ${message.Cmd}` });
110-
break;
111-
case 'Busy':
112-
this.uploading.next({ status: UPLOAD__IN_PROGRESS, msg: message.Msg });
113-
break;
114-
case 'Error':
115-
this.uploading.next({ status: UPLOAD__ERROR, err: message.Msg });
116-
break;
117-
case 'Killed':
118-
this.uploading.next({ status: UPLOAD__IN_PROGRESS, msg: `terminated by user` });
119-
this.uploading.next({ status: UPLOAD__ERROR, err: `terminated by user` });
120-
break;
121-
case 'Error 404 Not Found':
122-
this.uploading.next({ status: UPLOAD__ERROR, err: message.Msg });
123-
break;
124-
default:
125-
this.uploading.next({ status: UPLOAD__IN_PROGRESS, msg: message.Msg });
126-
}
127-
}
128-
129-
/**
130-
* Perform an upload via http on the daemon
131-
* target = {
132-
* board: "name of the board",
133-
* port: "port of the board",
134-
* auth_user: "Optional user to use as authentication",
135-
* auth_pass: "Optional pass to use as authentication"
136-
* auth_key: "Optional private key",
137-
* auth_port: "Optional alternative port (default 22)"
138-
* network: true or false
139-
* }
140-
* data = {
141-
* commandline: "commandline to execute",
142-
* signature: "signature of the commandline",
143-
* files: [
144-
* {name: "Name of a file to upload on the device", data: 'base64data'}
145-
* ],
146-
* options: {}
147-
* }
148-
* cb = callback function executing everytime a packet of data arrives through the websocket
149-
*/
150-
upload(target, data) {
151-
this.uploading.next({ status: UPLOAD__IN_PROGRESS });
152-
153-
if (data.files.length === 0) { // At least one file to upload
154-
this.uploading.next({ status: UPLOAD__ERROR, err: 'You need at least one file to upload' });
155-
return;
156-
}
157-
158-
// Main file
159-
const file = data.files[0];
160-
file.name = file.name.split('/');
161-
file.name = file.name[file.name.length - 1];
162-
163-
const payload = {
164-
board: target.board,
165-
port: target.port,
166-
commandline: data.commandline,
167-
signature: data.signature,
168-
hex: file.data,
169-
filename: file.name,
170-
extra: {
171-
auth: {
172-
username: target.auth_user,
173-
password: target.auth_pass,
174-
private_key: target.auth_key,
175-
port: target.auth_port
176-
},
177-
wait_for_upload_port: data.options.wait_for_upload_port === 'true' || data.options.wait_for_upload_port === true,
178-
use_1200bps_touch: data.options.use_1200bps_touch === 'true' || data.options.use_1200bps_touch === true,
179-
network: target.network,
180-
ssh: target.ssh,
181-
params_verbose: data.options.param_verbose,
182-
params_quiet: data.options.param_quiet,
183-
verbose: data.options.verbose
184-
},
185-
extrafiles: data.extrafiles || []
186-
};
187-
188-
for (let i = 1; i < data.files.length; i += 1) {
189-
payload.extrafiles.push({ filename: data.files[i].name, hex: data.files[i].data });
190-
}
191-
192-
fetch(`${this.pluginURL}/upload`, {
193-
method: 'POST',
194-
headers: {
195-
'Content-Type': 'text/plain; charset=utf-8'
196-
},
197-
body: JSON.stringify(payload)
198-
})
199-
.catch(error => {
200-
this.uploading.next({ status: UPLOAD__ERROR, err: error });
201-
});
202-
}
20359
}

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@
3030
import SocketDaemon from './socket-daemon';
3131
import ChromeOsDaemon from './chrome-app-daemon';
3232

33-
const Daemon = window.navigator.userAgent.indexOf(' CrOS ') !== -1 ? ChromeOsDaemon : SocketDaemon;
33+
const Daemon = true ? ChromeOsDaemon : SocketDaemon;
3434

3535
export default Daemon;

0 commit comments

Comments
 (0)