Skip to content

Commit 12bd9b6

Browse files
author
Stefania
committed
fixed conflict
2 parents 167c633 + 93943b3 commit 12bd9b6

File tree

3 files changed

+74
-31
lines changed

3 files changed

+74
-31
lines changed

src/reader-writer.js

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,42 @@
1+
import { Subject } from 'rxjs';
2+
13
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));
4+
constructor() {
5+
this.socket = null;
6+
this.pluginURL = null;
7+
this.messageSubject = new Subject();
8+
this.devicesList = {
9+
serial: [],
10+
network: []
11+
};
12+
this.messageSubject.subscribe(this.updateDevicesList.bind(this));
13+
this.openingSerial = null;
714
}
815

916
initSocket(socket) {
1017
this.socket = socket;
18+
this.socket.on('message', this.parseMessage.bind(this));
1119
}
1220

1321
initPluginUrl(pluginUrl) {
1422
this.pluginURL = pluginUrl;
1523
}
1624

25+
updateDevicesList(devicesInfo) {
26+
// Result of a list command
27+
if (devicesInfo.Ports) {
28+
if (devicesInfo.Network) {
29+
this.devicesList.network = devicesInfo.Ports;
30+
}
31+
else {
32+
this.devicesList.serial = devicesInfo.Ports;
33+
}
34+
console.log(this.devicesList);
35+
}
36+
}
37+
1738
parseMessage(message) {
1839
let jsonMessage;
19-
2040
try {
2141
jsonMessage = JSON.parse(message);
2242
}
@@ -25,10 +45,44 @@ export default class ReaderWriter {
2545
}
2646

2747
if (jsonMessage) {
28-
// Result of a list command
29-
if (jsonMessage.Ports) {
30-
this.devicesListStatus.next(jsonMessage);
31-
}
48+
this.messageSubject.next(jsonMessage);
49+
}
50+
}
51+
52+
openSerialMonitor(port) {
53+
if (this.openingSerial) {
54+
return this.openingSerial;
55+
}
56+
const serialPort = this.devicesList.serial.find(p => p.Name === port);
57+
if (!serialPort) {
58+
return Promise.reject(new Error('No board found'));
3259
}
60+
if (serialPort.IsOpen) {
61+
return Promise.resolve();
62+
}
63+
let checkOpen = null;
64+
this.openingSerial = new Promise((resolve, reject) => {
65+
checkOpen = message => {
66+
let data = null;
67+
try {
68+
data = JSON.parse(message);
69+
}
70+
catch (SyntaxError) {
71+
return;
72+
}
73+
if (data.Cmd === 'Open') {
74+
return resolve();
75+
}
76+
if (data.Cmd === 'OpenFail') {
77+
return reject(new Error('Failed to open serial'));
78+
}
79+
};
80+
this.messageSubject.subscribe(checkOpen);
81+
}).finally(() => {
82+
this.openingSerial = null;
83+
});
84+
this.socket.emit('command', `open ${port} 9600 timed`);
85+
return this.openingSerial;
3386
}
87+
3488
}

src/socket-daemon.js

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -74,31 +74,12 @@ export default class SocketDaemon {
7474

7575
this.agentDiscoveryStatus = new BehaviorSubject(AGENT_STATUS_NOT_FOUND);
7676
this.wsConnectionStatus = new BehaviorSubject(WS_STATUS_DISCONNECTED);
77-
this.devicesListStatus = new Subject();
7877
this.wsError = new Subject();
7978

80-
this.devicesList = {
81-
serial: [],
82-
network: []
83-
};
84-
85-
this.devicesListStatus.subscribe(devicesInfo => {
86-
if (devicesInfo.Network) {
87-
this.devicesList.network = devicesInfo.Ports;
88-
}
89-
else {
90-
this.devicesList.serial = devicesInfo.Ports;
91-
}
92-
console.log(this.devicesList);
93-
});
94-
95-
this.readerWriter = null;
79+
this.readerWriter = new ReaderWriter();
9680
this.wsConnectionStatus.subscribe(status => {
9781
if (status === WS_STATUS_CONNECTED) {
98-
this.readerWriter = new ReaderWriter(this.socket, this.agentInfo[this.selectedProtocol], this.devicesListStatus);
99-
}
100-
else {
101-
this.readerWriter = null;
82+
this.readerWriter.initSocket(this.socket);
10283
}
10384
});
10485
}
@@ -154,10 +135,14 @@ export default class SocketDaemon {
154135
if (r.response.url.indexOf(PROTOCOL.HTTPS) === 0) {
155136
this.selectedProtocol = PROTOCOL.HTTPS;
156137
}
138+
<<<<<<< HEAD
157139
else {
158140
// Protocol http, force 127.0.0.1 for old agent versions too
159141
this.agentInfo[this.selectedProtocol] = this.agentInfo[this.selectedProtocol].replace('localhost', '127.0.0.1');
160142
}
143+
=======
144+
this.readerWriter.initPluginUrl(this.agentInfo[this.selectedProtocol]);
145+
>>>>>>> 93943b354033c52810ae98200bee481ba5eef5d9
161146
return true;
162147
}
163148
return false;

test/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,7 @@ document.getElementById('connect').addEventListener('click', () => {
3232
Daemon.findAgent()
3333
.catch(showError);
3434
});
35+
36+
Daemon.readerWriter.messageSubject.subscribe(devicesInfo => {
37+
document.getElementById('boards-list').innerHTML = JSON.stringify(devicesInfo, null, 2);
38+
});

0 commit comments

Comments
 (0)