Skip to content

Commit 5e98fc8

Browse files
author
Stefania
committed
started cleanup common daemon
1 parent 1c11ef5 commit 5e98fc8

File tree

4 files changed

+46
-66
lines changed

4 files changed

+46
-66
lines changed

src/chrome-app-daemon.js

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,47 +26,26 @@
2626
* invalidate any other reasons why the executable file might be covered by
2727
* the GNU General Public License.
2828
*/
29-
import {
30-
Subject,
31-
interval
32-
} from 'rxjs';
33-
import { filter, startWith, takeUntil } from 'rxjs/operators';
3429
import Daemon from './daemon';
3530

36-
const POLLING_INTERVAL = 1000;
37-
3831
export default class ChromeOsDaemon extends Daemon {
3932
constructor(chromeExtensionId) {
4033
super();
4134
this.channel = null;
42-
this.appMessages = new Subject();
4335

44-
this.appMessages
45-
.subscribe(this.handleAppMessage.bind(this));
36+
this.openChannel(() => this.channel.postMessage({
37+
command: 'listPorts'
38+
}));
4639

47-
this.channelOpen
48-
.subscribe(channelOpen => {
49-
if (channelOpen) {
50-
interval(POLLING_INTERVAL)
51-
.pipe(startWith(0))
52-
.pipe(takeUntil(this.channelOpen.pipe(filter(status => !status))))
53-
.subscribe(() => this.channel.postMessage({
54-
command: 'listPorts'
55-
}));
56-
}
57-
else {
58-
this._wsConnect(chromeExtensionId);
59-
this.agentFound.next(false);
60-
}
61-
});
40+
this._appConnect(chromeExtensionId);
6241

6342
// close all ports?
6443
}
6544

6645
/**
6746
* Instantiate connection and events listeners for chrome app
6847
*/
69-
_wsConnect(chromeExtensionId) {
48+
_appConnect(chromeExtensionId) {
7049
if (chrome.runtime) {
7150
this.channel = chrome.runtime.connect(chromeExtensionId);
7251
this.channel.onMessage.addListener(message => {

src/daemon.js

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

44
const UPLOAD_STATUS_NOPE = 'UPLOAD_STATUS_NOPE';
55
const UPLOAD_STATUS_DONE = 'UPLOAD_STATUS_DONE';
66
const UPLOAD_STATUS_ERROR = 'UPLOAD_STATUS_ERROR';
77
const UPLOAD_STATUS_IN_PROGRESS = 'UPLOAD_STATUS_IN_PROGRESS';
88

9+
const POLLING_INTERVAL = 1500;
10+
911
export default class Daemon {
1012
constructor() {
11-
this.socket = null;
12-
this.pluginURL = null;
1313
this.agentInfo = {};
1414
this.agentFound = new BehaviorSubject(null);
1515
this.channelOpen = new BehaviorSubject(null);
1616
this.error = new Subject();
17-
this.socketMessages = new Subject();
17+
this.appMessages = new Subject();
1818
this.serialMonitorOpened = new BehaviorSubject(false);
1919
this.serialMonitorMessages = new Subject();
2020
this.uploading = new BehaviorSubject({ status: UPLOAD_STATUS_NOPE });
@@ -23,8 +23,8 @@ export default class Daemon {
2323
network: []
2424
});
2525
this.supportedBoards = new BehaviorSubject([]);
26-
this.socketMessages
27-
.subscribe(this.handleSocketMessage.bind(this));
26+
this.appMessages
27+
.subscribe(this.handleAppMessage.bind(this));
2828

2929
const devicesListSubscription = this.devicesList.subscribe((devices) => {
3030
if (devices.serial && devices.serial.length > 0) {
@@ -34,19 +34,19 @@ export default class Daemon {
3434
});
3535
}
3636

37-
initSocket() {
38-
this.socket.on('message', message => {
39-
try {
40-
this.socketMessages.next(JSON.parse(message));
41-
}
42-
catch (SyntaxError) {
43-
this.socketMessages.next(message);
44-
}
45-
});
46-
}
47-
48-
initPluginUrl(pluginUrl) {
49-
this.pluginURL = pluginUrl;
37+
openChannel(cb) {
38+
this.channelOpen
39+
.subscribe(channelOpen => {
40+
if (channelOpen) {
41+
interval(POLLING_INTERVAL)
42+
.pipe(startWith(0))
43+
.pipe(takeUntil(this.channelOpen.pipe(filter(status => !status))))
44+
.subscribe(cb);
45+
}
46+
else {
47+
this.agentFound.next(false);
48+
}
49+
});
5050
}
5151

5252
/**
@@ -61,7 +61,7 @@ export default class Daemon {
6161
return a.every((item, index) => (b[index].Name === item.Name && b[index].IsOpen === item.IsOpen));
6262
}
6363

64-
handleSocketMessage(message) {
64+
handleAppMessage(message) {
6565
// Result of a list command
6666
if (message.Ports) {
6767
this.handleListMessage(message);
@@ -138,7 +138,7 @@ export default class Daemon {
138138
if (!serialPort) {
139139
return this.serialMonitorOpened.error(new Error(`Can't find port ${port}`));
140140
}
141-
this.socketMessages
141+
this.appMessages
142142
.pipe(takeUntil(this.serialMonitorOpened.pipe(filter(open => open))))
143143
.subscribe(message => {
144144
if (message.Cmd === 'Open') {
@@ -159,7 +159,7 @@ export default class Daemon {
159159
if (!serialPort) {
160160
return this.serialMonitorOpened.error(new Error(`Can't find port ${port}`));
161161
}
162-
this.socketMessages
162+
this.appMessages
163163
.pipe(takeUntil(this.serialMonitorOpened.pipe(filter(open => !open))))
164164
.subscribe(message => {
165165
if (message.Cmd === 'Close') {

src/socket-daemon.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import {
3535
interval,
3636
timer
3737
} from 'rxjs';
38-
import { filter, startWith, takeUntil } from 'rxjs/operators';
38+
import { filter, takeUntil } from 'rxjs/operators';
3939
import Daemon from './daemon';
4040

4141
// Required agent version
@@ -51,7 +51,6 @@ const LOOPBACK_ADDRESS = '127.0.0.1';
5151
const LOOPBACK_HOSTNAME = 'localhost';
5252
const LOOKUP_PORT_START = 8991;
5353
const LOOKUP_PORT_END = 9000;
54-
const POLLING_INTERVAL = 1000;
5554
const CANT_FIND_AGENT_MESSAGE = 'Arduino Create Agent cannot be found';
5655
let updateAttempts = 0;
5756
let orderedPluginAddresses = [LOOPBACK_ADDRESS, LOOPBACK_HOSTNAME];
@@ -64,20 +63,10 @@ export default class SocketDaemon extends Daemon {
6463
constructor() {
6564
super();
6665
this.selectedProtocol = PROTOCOL.HTTP;
66+
this.socket = null;
67+
this.pluginURL = null;
6768

68-
this.channelOpen
69-
.subscribe(channelOpen => {
70-
if (channelOpen) {
71-
this.initSocket();
72-
interval(POLLING_INTERVAL)
73-
.pipe(startWith(0))
74-
.pipe(takeUntil(this.channelOpen.pipe(filter(status => !status))))
75-
.subscribe(() => this.socket.emit('command', 'list'));
76-
}
77-
else {
78-
this.agentFound.next(false);
79-
}
80-
});
69+
this.openChannel(() => this.socket.emit('command', 'list'));
8170

8271
this.agentFound
8372
.subscribe(agentFound => {
@@ -90,6 +79,17 @@ export default class SocketDaemon extends Daemon {
9079
});
9180
}
9281

82+
initSocket() {
83+
this.socket.on('message', message => {
84+
try {
85+
this.appMessages.next(JSON.parse(message));
86+
}
87+
catch (SyntaxError) {
88+
this.appMessages.next(message);
89+
}
90+
});
91+
}
92+
9393
/**
9494
* Look for the agent endpoint.
9595
* First search in http://LOOPBACK_ADDRESS, after in https://LOOPBACK_HOSTNAME if in Chrome or Firefox, otherwise vice versa.
@@ -129,7 +129,7 @@ export default class SocketDaemon extends Daemon {
129129
// Protocol http, force 127.0.0.1 for old agent versions too
130130
this.agentInfo[this.selectedProtocol] = this.agentInfo[this.selectedProtocol].replace('localhost', '127.0.0.1');
131131
}
132-
this.initPluginUrl(this.agentInfo[this.selectedProtocol]);
132+
this.pluginURL = this.agentInfo[this.selectedProtocol];
133133
return true;
134134
}
135135
return false;
@@ -164,6 +164,7 @@ export default class SocketDaemon extends Daemon {
164164
this.socket.emit('command', 'downloadtool windows-drivers latest arduino keep');
165165
this.socket.emit('command', 'downloadtool bossac 1.7.0 arduino keep');
166166

167+
this.initSocket();
167168
this.channelOpen.next(true);
168169
});
169170

test/app.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ class App extends React.Component {
176176
</span>
177177
</p>
178178
<p>
179-
Web socket status: <span className={ this.state.channelStatus ? 'found' : 'not-found' }>
179+
Channel status: <span className={ this.state.channelStatus ? 'found' : 'not-found' }>
180180
{ this.state.channelStatus ? 'Connected' : 'Not connected' }
181181
</span>
182182
</p>

0 commit comments

Comments
 (0)