Skip to content

Commit 1deb760

Browse files
author
Alberto Iannaccone
committed
merge commit
2 parents 3a9dc0f + 92847fe commit 1deb760

File tree

7 files changed

+222
-42
lines changed

7 files changed

+222
-42
lines changed

README.md

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,51 @@ npm install create-plugin-communication --save
1010
## How to use
1111

1212
```js
13-
import Daemon from 'create-plugin-communication';ù
13+
import Daemon from 'create-plugin-communication';
1414

15-
// Ask for agent connection
16-
Daemon.findAgent()
15+
const daemon = new Daemon();
1716

18-
Daemon.agentDiscoveryStatus.subscribe(status => {
19-
// AGENT_FOUND / AGENT_NOT_FOUND
17+
daemon.agentFound.subscribe(status => {
18+
// true / false
2019
});
2120

22-
Daemon.wsConnectionStatus.subscribe(status => {
23-
// WS_CONNECTED / WS_DISCONNECTED
21+
daemon.wsConnected.subscribe(status => {
22+
// true / false
2423
});
2524

26-
Daemon.wsError.subscribe(err => {
25+
daemon.error.subscribe(err => {
2726
// handle err
2827
});
2928

30-
Daemon.readerWriter.messageSubject.subscribe(() => {
31-
const serialDevices = Daemon.readerWriter.devicesList.serial;
32-
const networkDevices = Daemon.readerWriter.devicesList.network;
29+
// List available devices (serial/network)
30+
daemon.devicesList.subscribe(devices => {
31+
const serialDevices = devices.serial;
32+
const networkDevices = devices.network;
3333
});
34+
35+
// Open serial monitor
36+
daemon.openSerialMonitor('port-name');
37+
38+
// Read from serial monitor
39+
daemon.serialMonitorMessages.subscribe(message => {
40+
console.log(message);
41+
});
42+
43+
// Write to serial monitor
44+
daemon.writeSerial('port-name', 'message');
45+
46+
// Close serial monitor
47+
daemon.closeSerialMonitor('port-name');
48+
3449
```
3550
## Development
3651
Just run `npm run dev` and open your browser on http://localhost:8000
3752

3853
## Agent communication
3954

4055
To enable communication between your [local installation](http://localhost:8000/) and the [Arduino Create Agent](https://github.com/arduino/arduino-create-agent)
41-
add `origins = http://localhost:8000` on your agent config.ini file (if you are using https, add `origins = https://localhost:8000`).
56+
add `origins = http://localhost:8000` on your agent config.ini file
57+
(if you are using https, add `origins = https://localhost:8000`).
4258

4359
- On macOs ~/Applications/ArduinoCreateAgent-1.1/ArduinoCreateAgent.app/Contents/MacOS/config.ini
4460
- On Linux ~/ArduinoCreateAgent-1.1/config.ini

src/chrome-app-daemon.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* This file is part of create-plugin-communication.
3+
*
4+
* Copyright 2018 Arduino AG (http://www.arduino.cc/)
5+
*
6+
* create-plugin-communication is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*
20+
* As a special exception, you may use this file as part of a free software
21+
* library without restriction. Specifically, if other files instantiate
22+
* templates or use macros or inline functions from this file, or you compile
23+
* this file and link it with other files to produce an executable, this
24+
* file does not by itself cause the resulting executable to be covered by
25+
* the GNU General Public License. This exception does not however
26+
* invalidate any other reasons why the executable file might be covered by
27+
* the GNU General Public License.
28+
*/
29+
import {
30+
Subject,
31+
BehaviorSubject,
32+
interval
33+
} from 'rxjs';
34+
import { filter, startWith, takeUntil } from 'rxjs/operators';
35+
import Daemon from './daemon';
36+
37+
const POLLING_INTERVAL = 1000;
38+
39+
export default class ChromeOsDaemon extends Daemon {
40+
constructor(chromeExtensionId) {
41+
super();
42+
this.channel = null;
43+
this.agentInfo = {};
44+
this.agentFound = new BehaviorSubject(null);
45+
this.wsConnected = new BehaviorSubject(null);
46+
this.appMessages = new Subject();
47+
this.error = new Subject();
48+
49+
this.appMessages
50+
.subscribe(this.handleAppMessage.bind(this));
51+
52+
this.wsConnected
53+
.subscribe(wsConnected => {
54+
if (wsConnected) {
55+
interval(POLLING_INTERVAL)
56+
.pipe(startWith(0))
57+
.pipe(takeUntil(this.wsConnected.pipe(filter(status => !status))))
58+
.subscribe(() => this.channel.postMessage({
59+
command: 'listPorts'
60+
}));
61+
}
62+
else {
63+
this._wsConnect(chromeExtensionId);
64+
this.agentFound.next(false);
65+
}
66+
});
67+
68+
// close all ports?
69+
}
70+
71+
/**
72+
* Instantiate connection and events listeners for chrome app
73+
*/
74+
_wsConnect(chromeExtensionId) {
75+
if (chrome.runtime) {
76+
this.channel = chrome.runtime.connect(chromeExtensionId);
77+
this.channel.onMessage.addListener(message => {
78+
if (message.version) {
79+
this.agentInfo = message;
80+
this.agentFound.next(true);
81+
this.wsConnected.next(true);
82+
}
83+
else {
84+
this.appMessages.next(message);
85+
}
86+
});
87+
this.channel.onDisconnect.addListener(() => {
88+
this.wsConnected.next(false);
89+
this.agentFound.next(false);
90+
});
91+
}
92+
}
93+
94+
handleAppMessage(message) {
95+
if (message.ports) {
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+
}
109+
}
110+
111+
if (message.supportedBoards) {
112+
this.supportedBoards.next(message.supportedBoards);
113+
}
114+
}
115+
}

src/chromeAppDaemon.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
import { Deferred } from './readMessages';
3131

32-
let port = null;
32+
const port = null;
3333
let polling;
3434

3535
let uploading = false;
@@ -63,7 +63,7 @@ const callback = (name, data) => {
6363
};
6464

6565
const onMessage = (msg) => {
66-
if (msg.version) {
66+
/* if (msg.version) {
6767
if (!polling) {
6868
polling = setInterval(() => {
6969
if (!uploading) {
@@ -89,7 +89,7 @@ const onMessage = (msg) => {
8989
}));
9090
9191
callback('ports', ports);
92-
}
92+
} */
9393
else if (msg.uploadStatus) {
9494
if (msg.uploadStatus === 'success') {
9595
uploading = false;
@@ -143,7 +143,8 @@ const onMessage = (msg) => {
143143
}
144144
};
145145

146-
const onChromeDisconnect = () => {
146+
/*
147+
const onChromeDisconnect = () => {
147148
disconnected = true;
148149
149150
if (polling) {
@@ -153,7 +154,7 @@ const onChromeDisconnect = () => {
153154
disconnectCb();
154155
};
155156
156-
const connect = (chromeExtensionId) => {
157+
const connect = (chromeExtensionId) => {
157158
if ((port === null || disconnected) && chrome.runtime) {
158159
port = chrome.runtime.connect(chromeExtensionId);
159160
port.onMessage.addListener(onMessage);
@@ -162,7 +163,7 @@ const connect = (chromeExtensionId) => {
162163
else {
163164
errorCb('chromeExtensionNotFound');
164165
}
165-
};
166+
}; */
166167

167168
const perform = (action, data) => {
168169
const deferred = new Deferred();

src/daemon.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export default class Daemon {
1818
serial: [],
1919
network: []
2020
});
21+
this.supportedBoards = new BehaviorSubject([]);
2122
this.socketMessages
2223
.subscribe(this.handleSocketMessage.bind(this));
2324

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
*/
2929

3030
import SocketDaemon from './socket-daemon';
31-
import chromeAppDaemon from './chromeAppDaemon';
31+
import ChromeOsDaemon from './chrome-app-daemon';
3232

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

3535
export default Daemon;

0 commit comments

Comments
 (0)