Skip to content

Commit 5baba04

Browse files
author
Alberto Iannaccone
committedJun 28, 2018
implement readerwriter class and read list response
·
next1.0.0
1 parent c900387 commit 5baba04

File tree

2 files changed

+66
-10
lines changed

2 files changed

+66
-10
lines changed
 

‎src/reader-writer.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
export default class ReaderWriter {
2+
constructor(socket, pluginUrl, devicesListStatus) {
3+
this.socket = socket;
4+
this.pluginUrl = pluginUrl;
5+
this.devicesListStatus = devicesListStatus;
6+
this.socket.on('message', this.parseMessage.bind(this));
7+
}
8+
9+
initSocket(socket) {
10+
this.socket = socket;
11+
}
12+
13+
initPluginUrl(pluginUrl) {
14+
this.pluginURL = pluginUrl;
15+
}
16+
17+
parseMessage(message) {
18+
let jsonMessage;
19+
20+
try {
21+
jsonMessage = JSON.parse(message);
22+
}
23+
catch (SyntaxError) {
24+
return;
25+
}
26+
27+
if (jsonMessage) {
28+
// Result of a list command
29+
if (jsonMessage.Ports) {
30+
this.devicesListStatus.next(jsonMessage);
31+
}
32+
}
33+
}
34+
}

‎src/socket-daemon.js

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ import {
3434
BehaviorSubject,
3535
interval
3636
} from 'rxjs';
37-
import { parseMessage, initSocket, initPluginUrl } from './readMessages';
38-
import { debug } from 'util';
37+
import ReaderWriter from './reader-writer';
38+
3939
// Required agent version
4040
const MIN_VERSION = '1.1.71';
4141

@@ -64,7 +64,33 @@ export default class SocketDaemon {
6464

6565
this.agentDiscoveryStatus = new BehaviorSubject(AGENT_STATUS_NOT_FOUND);
6666
this.wsConnectionStatus = new BehaviorSubject(WS_STATUS_DISCONNECTED);
67+
this.devicesListStatus = new Subject();
6768
this.wsError = new Subject();
69+
70+
this.devicesList = {
71+
serial: [],
72+
network: []
73+
};
74+
75+
this.devicesListStatus.subscribe(devicesInfo => {
76+
if (devicesInfo.Network) {
77+
this.devicesList.network = devicesInfo.Ports;
78+
}
79+
else {
80+
this.devicesList.serial = devicesInfo.Ports;
81+
}
82+
console.log(this.devicesList);
83+
});
84+
85+
this.readerWriter = null;
86+
this.wsConnectionStatus.subscribe(status => {
87+
if (status === WS_STATUS_CONNECTED) {
88+
this.readerWriter = new ReaderWriter(this.socket, this.agentInfo[this.selectedProtocol], this.devicesListStatus);
89+
}
90+
else {
91+
this.readerWriter = null;
92+
}
93+
});
6894
}
6995

7096
/**
@@ -100,7 +126,6 @@ export default class SocketDaemon {
100126
* @return {object} info - The agent info values.
101127
*/
102128
tryPorts(hostname) {
103-
console.log('tryPorts\n');
104129
const pluginLookups = [];
105130

106131
for (let port = LOOKUP_PORT_START; port < LOOKUP_PORT_END; port += 1) {
@@ -121,7 +146,6 @@ export default class SocketDaemon {
121146
if (r.response.url.indexOf(PROTOCOL.HTTPS) === 0) {
122147
this.selectedProtocol = PROTOCOL.HTTPS;
123148
}
124-
initPluginUrl(this.agentInfo[this.selectedProtocol]);
125149
return true;
126150
}
127151
return false;
@@ -149,8 +173,6 @@ export default class SocketDaemon {
149173
this.socket = io(address, { reconnection: false, forceNew: true });
150174

151175
this.socket.on('connect', () => {
152-
initSocket(this.socket);
153-
154176
// On connect download windows drivers which are indispensable for detection of boards
155177
this.socket.emit('command', 'downloadtool windows-drivers latest arduino keep');
156178
this.socket.emit('command', 'downloadtool bossac 1.7.0 arduino keep');
@@ -173,9 +195,6 @@ export default class SocketDaemon {
173195
this.wsConnectionStatus.next(WS_STATUS_DISCONNECTED);
174196
this.findAgent();
175197
});
176-
177-
// Parse messages
178-
this.socket.on('message', parseMessage);
179198
}
180199

181200
/**
@@ -188,7 +207,10 @@ export default class SocketDaemon {
188207
}
189208

190209
return fetch(`${this.agentInfo[this.selectedProtocol]}/update`, {
191-
method: 'POST'
210+
method: 'POST',
211+
headers: {
212+
'Content-Type': 'text/plain; charset=utf-8'
213+
}
192214
}).then(() => reject()); // We reject the promise because the daemon will be restarted, we need to continue looking for the port
193215
});
194216
}

0 commit comments

Comments
 (0)
Please sign in to comment.