Skip to content

Commit 651d196

Browse files
committed
Simpler agent/ws status
1 parent 99e6071 commit 651d196

File tree

2 files changed

+29
-29
lines changed

2 files changed

+29
-29
lines changed

src/socket-daemon.js

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
BehaviorSubject,
3737
interval
3838
} from 'rxjs';
39+
import { filter } from 'rxjs/operators';
3940
import ReaderWriter from './reader-writer';
4041

4142
// Required agent version
@@ -55,11 +56,6 @@ const LOOKUP_PORT_END = 9000;
5556
const CANT_FIND_AGENT_MESSAGE = 'Arduino Create Agent cannot be found';
5657
let updateAttempts = 0;
5758

58-
export const AGENT_STATUS_FOUND = 'AGENT_FOUND';
59-
export const AGENT_STATUS_NOT_FOUND = 'AGENT_NOT_FOUND';
60-
export const WS_STATUS_CONNECTED = 'WS_CONNECTED';
61-
export const WS_STATUS_DISCONNECTED = 'WS_DISCONNECTED';
62-
6359
let orderedPluginAddresses = [LOOPBACK_ADDRESS, LOOPBACK_HOSTNAME];
6460

6561
if (browser.name !== 'chrome' && browser.name !== 'firefox') {
@@ -70,18 +66,23 @@ export default class SocketDaemon {
7066
constructor() {
7167
this.selectedProtocol = PROTOCOL.HTTP;
7268
this.agentInfo = {};
73-
this.found = false;
7469

75-
this.agentDiscoveryStatus = new BehaviorSubject(AGENT_STATUS_NOT_FOUND);
76-
this.wsConnectionStatus = new BehaviorSubject(WS_STATUS_DISCONNECTED);
70+
this.agentFound = new BehaviorSubject(false);
71+
this.wsConnected = new BehaviorSubject(false);
7772
this.wsError = new Subject();
7873

7974
this.readerWriter = new ReaderWriter();
80-
this.wsConnectionStatus.subscribe(status => {
81-
if (status === WS_STATUS_CONNECTED) {
82-
this.readerWriter.initSocket(this.socket);
83-
}
84-
});
75+
this.wsConnected
76+
.pipe(filter(status => status))
77+
.subscribe(() => {
78+
this.readerWriter.initSocket(this.socket)
79+
});
80+
81+
this.agentFound
82+
.pipe(filter(status => !status))
83+
.subscribe(() => {
84+
this.agentInfo = {};
85+
});
8586
}
8687

8788
/**
@@ -92,7 +93,7 @@ export default class SocketDaemon {
9293
findAgent() {
9394
const find = () => this.tryAllPorts()
9495
.catch(err => {
95-
this.agentDiscoveryStatus.next(AGENT_STATUS_NOT_FOUND);
96+
this.agentFound.next(false);
9697
return err;
9798
})
9899
.finally(() => {
@@ -127,10 +128,10 @@ export default class SocketDaemon {
127128

128129
return Promise.all(pluginLookups)
129130
.then(responses => {
130-
this.found = responses.some(r => {
131+
const found = responses.some(r => {
131132
if (r && r.response && r.response.status === 200) {
132133
this.agentInfo = r.data;
133-
this.agentDiscoveryStatus.next(AGENT_STATUS_FOUND);
134+
this.agentFound.next(true);
134135
this.wsConnect();
135136
if (r.response.url.indexOf(PROTOCOL.HTTPS) === 0) {
136137
this.selectedProtocol = PROTOCOL.HTTPS;
@@ -145,7 +146,7 @@ export default class SocketDaemon {
145146
return false;
146147
});
147148

148-
if (this.found) {
149+
if (found) {
149150
if (this.agentInfo.version && (semVerCompare(this.agentInfo.version, MIN_VERSION) >= 0 || this.agentInfo.version.indexOf('dev') !== -1)) {
150151
return this.agentInfo;
151152
}
@@ -180,7 +181,7 @@ export default class SocketDaemon {
180181
this.socket.emit('command', 'downloadtool windows-drivers latest arduino keep');
181182
this.socket.emit('command', 'downloadtool bossac 1.7.0 arduino keep');
182183

183-
this.wsConnectionStatus.next(WS_STATUS_CONNECTED);
184+
this.wsConnected.next(true);
184185

185186
// Periodically asks for the ports
186187
if (!this.portsPollingSubscription) {
@@ -195,7 +196,7 @@ export default class SocketDaemon {
195196
if (this.portsPollingSubscription) {
196197
this.portsPollingSubscription.unsubscribe();
197198
}
198-
this.wsConnectionStatus.next(WS_STATUS_DISCONNECTED);
199+
this.wsConnected.next(false);
199200
this.findAgent();
200201
});
201202
}
@@ -238,7 +239,7 @@ export default class SocketDaemon {
238239
* @return {Promise}
239240
*/
240241
stopPlugin() {
241-
if (this.found) {
242+
if (this.agentFound.getValue()) {
242243
return fetch(`${this.agentInfo[this.selectedProtocol]}/pause`, { method: 'POST' });
243244
}
244245
}

test/app.jsx

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React from 'react';
22
import Daemon from '../src';
3-
import { WS_STATUS_CONNECTED, AGENT_STATUS_FOUND } from '../src/socket-daemon';
43

54
const scrollToBottom = (target) => {
65
if (target) {
@@ -27,14 +26,14 @@ class App extends React.Component {
2726
}
2827

2928
componentDidMount() {
30-
Daemon.agentDiscoveryStatus.subscribe(status => {
29+
Daemon.agentFound.subscribe(status => {
3130
this.setState({
3231
agentStatus: status,
3332
agentInfo: JSON.stringify(Daemon.agentInfo, null, 2)
3433
});
3534
});
3635

37-
Daemon.wsConnectionStatus.subscribe(status => {
36+
Daemon.wsConnected.subscribe(status => {
3837
this.setState({ wsStatus: status });
3938
});
4039

@@ -86,15 +85,16 @@ class App extends React.Component {
8685
return (
8786
<div>
8887
<h1>Test Arduino Create Plugin</h1>
89-
<p>Agent status: <span id="agent-status" className={ this.state.agentStatus === AGENT_STATUS_FOUND ? 'found' : 'not-found' }>
90-
{ this.state.agentStatus }
88+
<button id="connect" onClick={ this.connect }>Connect</button>
89+
<p>Agent status: <span id="agent-status" className={ this.state.agentStatus ? 'found' : 'not-found' }>
90+
{ this.state.agentStatus ? 'Found' : 'Not found' }
91+
</span></p>
92+
<p>Web socket status: <span id="ws-status" className={ this.state.wsStatus ? 'found' : 'not-found' }>
93+
{ this.state.wsStatus ? 'Connected' : 'Not connected' }
9194
</span></p>
9295
<pre id="agent-info">
9396
{ this.state.agentInfo }
9497
</pre>
95-
<p>Web socket status: <span id="ws-status" className={ this.state.wsStatus === WS_STATUS_CONNECTED ? 'found' : 'not-found' }>
96-
{ this.state.wsStatus }
97-
</span></p>
9898
<div>
9999
<h2>Devices</h2>
100100
<strong>serial:</strong>
@@ -107,7 +107,6 @@ class App extends React.Component {
107107
</ul>
108108
<p id="error"></p>
109109
</div>
110-
<button id="connect" onClick={ this.connect }>Connect</button>
111110
<div className="serial-monitor">
112111
<h2>Serial Monitor</h2>
113112
<textarea id="serial-textarea" value={ this.state.serialMonitorContent }/>

0 commit comments

Comments
 (0)