Skip to content

Commit 8ed46dd

Browse files
author
Stefania
committed
added downloadtool test
1 parent 779dbc9 commit 8ed46dd

File tree

4 files changed

+152
-36
lines changed

4 files changed

+152
-36
lines changed

src/daemon.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default class Daemon {
1414
this.agentFound = new BehaviorSubject(null);
1515
this.channelOpen = new BehaviorSubject(null);
1616
this.error = new Subject();
17+
1718
this.appMessages = new Subject();
1819
this.serialMonitorOpened = new BehaviorSubject(false);
1920
this.serialMonitorMessages = new Subject();

src/socket-daemon.js

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import io from 'socket.io-client';
3131
import semVerCompare from 'semver-compare';
3232
import { detect } from 'detect-browser';
3333

34-
import { timer } from 'rxjs';
34+
import { BehaviorSubject, timer } from 'rxjs';
3535
import { takeUntil, filter } from 'rxjs/operators';
3636

3737
import Daemon from './daemon';
@@ -50,21 +50,32 @@ const LOOPBACK_ADDRESS = '127.0.0.1';
5050
const LOOPBACK_HOSTNAME = 'localhost';
5151
const LOOKUP_PORT_START = 8991;
5252
const LOOKUP_PORT_END = 9000;
53+
let orderedPluginAddresses = [LOOPBACK_ADDRESS, LOOPBACK_HOSTNAME];
54+
5355
const CANT_FIND_AGENT_MESSAGE = 'Arduino Create Agent cannot be found';
56+
5457
let updateAttempts = 0;
55-
let orderedPluginAddresses = [LOOPBACK_ADDRESS, LOOPBACK_HOSTNAME];
5658

5759
if (browser.name !== 'chrome' && browser.name !== 'firefox') {
5860
orderedPluginAddresses = [LOOPBACK_HOSTNAME, LOOPBACK_ADDRESS];
5961
}
6062

63+
const DOWNLOAD_NOPE = 'DOWNLOAD_NOPE';
64+
const DOWNLOAD_DONE = 'DOWNLOAD_DONE';
65+
const DOWNLOAD_ERROR = 'DOWNLOAD_ERROR';
66+
const DOWNLOAD_IN_PROGRESS = 'DOWNLOAD_IN_PROGRESS';
67+
6168
export default class SocketDaemon extends Daemon {
6269
constructor() {
6370
super();
6471
this.selectedProtocol = PROTOCOL.HTTP;
6572
this.socket = null;
6673
this.pluginURL = null;
6774

75+
this.downloading = new BehaviorSubject({ status: DOWNLOAD_NOPE });
76+
this.downloadingDone = this.downloading.pipe(filter(download => download.status === DOWNLOAD_DONE));
77+
this.downloadingError = this.downloading.pipe(filter(download => download.status === DOWNLOAD_ERROR));
78+
6879
this.openChannel(() => this.socket.emit('command', 'list'));
6980

7081
this.agentFound
@@ -188,6 +199,10 @@ export default class SocketDaemon extends Daemon {
188199
this.handleUploadMessage(message);
189200
}
190201

202+
if (message.DownloadStatus) {
203+
this.handleDownloadMessage(message);
204+
}
205+
191206
if (message.Err) {
192207
this.uploading.next({ status: this.UPLOAD_ERROR, err: message.Err });
193208
}
@@ -343,6 +358,25 @@ export default class SocketDaemon extends Daemon {
343358
}
344359
}
345360

361+
handleDownloadMessage(message) {
362+
if (this.downloading.getValue().status !== DOWNLOAD_IN_PROGRESS) {
363+
return;
364+
}
365+
switch (message.DownloadStatus) {
366+
case 'Pending':
367+
this.downloading.next({ status: DOWNLOAD_IN_PROGRESS, msg: message.Msg });
368+
break;
369+
case 'Success':
370+
this.downloading.next({ status: DOWNLOAD_DONE, msg: message.Msg });
371+
break;
372+
case 'Error':
373+
this.downloading.next({ status: DOWNLOAD_ERROR, err: message.Msg });
374+
break;
375+
default:
376+
this.downloading.next({ status: DOWNLOAD_IN_PROGRESS, msg: message.Msg });
377+
}
378+
}
379+
346380
/**
347381
* Perform an upload via http on the daemon
348382
* @param {Object} target = {
@@ -425,6 +459,7 @@ export default class SocketDaemon extends Daemon {
425459
* @param {string} replacementStrategy
426460
*/
427461
downloadToolCommand(toolName, toolVersion, packageName, replacementStrategy) {
462+
this.downloading.next({ status: DOWNLOAD_IN_PROGRESS });
428463
this.socket.emit('command', `downloadtool ${toolName} ${toolVersion} ${packageName} ${replacementStrategy}`);
429464
}
430465

test/app.jsx

Lines changed: 89 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ class App extends React.Component {
5151
serialPortOpen: '',
5252
uploadStatus: '',
5353
uploadError: '',
54+
downloadStatus: '',
55+
downloadError: '',
5456
supportedBoards: []
5557
};
5658
this.handleOpen = this.handleOpen.bind(this);
@@ -96,6 +98,13 @@ class App extends React.Component {
9698
this.setState({ uploadStatus: upload.status });
9799
console.log(upload);
98100
});
101+
102+
if (daemon.downloading) {
103+
daemon.downloading.subscribe(download => {
104+
this.setState({ downloadStatus: download.status });
105+
console.log(download);
106+
});
107+
}
99108
}
100109

101110
showError(err) {
@@ -128,6 +137,19 @@ class App extends React.Component {
128137
serialInput.value = '';
129138
}
130139

140+
handleDownloadTool(e) {
141+
e.preventDefault();
142+
const toolname = document.getElementById('toolname');
143+
const toolversion = document.getElementById('toolversion');
144+
const packageName = document.getElementById('package');
145+
const replacement = document.getElementById('replacement');
146+
daemon.downloadTool(toolname.value, toolversion.value, packageName.value, replacement.value);
147+
toolname.value = '';
148+
toolversion.value = '';
149+
packageName.value = '';
150+
replacement.value = '';
151+
}
152+
131153
render() {
132154
const listSerialDevices = this.state.serialDevices.map((device, i) =>
133155
<li key={i}>
@@ -161,42 +183,61 @@ class App extends React.Component {
161183
uploadClass = 'in-progress';
162184
}
163185

186+
let downloadClass;
187+
if (this.state.downloadStatus === daemon.DOWNLOAD_DONE) {
188+
downloadClass = 'success';
189+
}
190+
else if (this.state.downloadStatus === daemon.DOWNLOAD_ERROR) {
191+
downloadClass = 'error';
192+
}
193+
else if (this.state.downloadStatus === daemon.DOWNLOAD_IN_PROGRESS) {
194+
downloadClass = 'in-progress';
195+
}
196+
164197
return (
165198
<div>
166199
<h1>Test Arduino Create Plugin</h1>
167200

168-
<p>
169-
Agent status: <span className={ this.state.agentStatus ? 'found' : 'not-found' }>
170-
{ this.state.agentStatus ? 'Found' : 'Not found' }
171-
</span>
172-
</p>
173-
<p>
174-
Channel status: <span className={ this.state.channelStatus ? 'found' : 'not-found' }>
175-
{ this.state.channelStatus ? 'Connected' : 'Not connected' }
176-
</span>
177-
</p>
178-
179-
<pre>
180-
{ this.state.agentInfo }
181-
</pre>
201+
<div className="section">
202+
<h2>Plugin info</h2>
203+
204+
<p>
205+
Agent status: <span className={ this.state.agentStatus ? 'found' : 'not-found' }>
206+
{ this.state.agentStatus ? 'Found' : 'Not found' }
207+
</span>
208+
</p>
209+
<p>
210+
Channel status: <span className={ this.state.channelStatus ? 'found' : 'not-found' }>
211+
{ this.state.channelStatus ? 'Connected' : 'Not connected' }
212+
</span>
213+
</p>
214+
215+
<pre>
216+
{ this.state.agentInfo }
217+
</pre>
218+
</div>
182219

183220
<div className="section">
184-
<h2>Devices</h2>
221+
<h2>Connected Devices</h2>
222+
185223
<strong>serial:</strong>
186224
<ul>
187225
{ listSerialDevices }
188226
</ul>
227+
189228
<strong>network:</strong>
190229
<ul>
191230
{ listNetworkDevices }
192231
</ul>
232+
193233
<p id="error"></p>
194234
</div>
195235

196236
{
197237
this.state.supportedBoards.length ?
198238
<div className="section">
199239
<h2>Supported boards</h2>
240+
200241
<ul>
201242
{supportedBoards}
202243
</ul>
@@ -206,10 +247,12 @@ class App extends React.Component {
206247

207248
<div className="serial-monitor section">
208249
<h2>Serial Monitor</h2>
250+
209251
<form onSubmit={this.handleSend}>
210252
<input id="serial-input" />
211253
<input type="submit" value="Send" />
212254
</form>
255+
213256
<textarea id="serial-textarea" value={ this.state.serialMonitorContent }/>
214257
</div>
215258

@@ -219,6 +262,37 @@ class App extends React.Component {
219262
<div>Upload status: <span className={ uploadClass }> { this.state.uploadStatus }</span></div>
220263
<div>{ this.state.uploadError }</div>
221264
</div>
265+
266+
{ daemon.downloading ? <div className="section">
267+
<h2>Download tool (not supported on Chrome OS)</h2>
268+
269+
<div>
270+
<p>Example:</p>
271+
<dl>
272+
<dt>Tool Name:</dt>
273+
<dd>windows-drivers</dd>
274+
275+
<dt>Tool Version:</dt>
276+
<dd>latest</dd>
277+
278+
<dt>Package:</dt>
279+
<dd>arduino</dd>
280+
281+
<dt>Replacement Strategy:</dt>
282+
<dd>keep</dd>
283+
</dl>
284+
</div>
285+
286+
<form onSubmit={this.handleDownloadTool}>
287+
<div><input id="toolname" placeholder="Tool Name"/></div>
288+
<div><input id="toolversion" placeholder="Tool Version" /></div>
289+
<div><input id="package" placeholder="Package" /></div>
290+
<div><input id="replacement" placeholder="Replacement strategy"/></div>
291+
292+
<input type="submit" value="Download" />
293+
<div>Download status: <span className={ downloadClass }> { this.state.downloadStatus }</span></div>
294+
</form>
295+
</div> : null}
222296
</div>
223297
);
224298
}

test/index.html

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,37 @@
33
<meta charset="UTF-8">
44
<meta name="viewport" content="width=device-width, initial-scale=1.0">
55
<meta http-equiv="X-UA-Compatible" content="ie=edge">
6-
<title>Test Daemon</title>
6+
<title>Test Arduino Create Plugin</title>
77
<style>
8-
#error, .not-found, .closed, .error {
9-
color: red
10-
}
8+
body {
9+
color: #2c353a;
10+
font-family: Lucida Grande,Lucida,Verdana,sans-serif;
11+
padding: 15px;
12+
}
1113

12-
.found, .open, .success {
13-
color: green;
14-
}
14+
#error, .not-found, .closed, .error {
15+
color: red
16+
}
1517

16-
pre {
17-
background: #f9f9f9;
18-
}
18+
.found, .open, .success {
19+
color: green;
20+
}
1921

20-
.in-progress {
21-
color: blue;
22-
}
22+
pre {
23+
background: #f9f9f9;
24+
}
2325

24-
#serial-textarea {
25-
height: 300px;
26-
}
26+
.in-progress {
27+
color: blue;
28+
}
2729

28-
.section {
29-
margin: 20px;
30-
}
30+
#serial-textarea {
31+
height: 300px;
32+
}
33+
34+
.section {
35+
margin: 20px;
36+
}
3137
</style>
3238
</head>
3339
<body>

0 commit comments

Comments
 (0)