Skip to content

V2 #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Apr 5, 2019
Prev Previous commit
Next Next commit
Show installed tools table
  • Loading branch information
matteosuppo committed Mar 29, 2019
commit 9bd32a29a1c10442b6acfc59ae54c18f79ad109e
25 changes: 21 additions & 4 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
-->

<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
Expand All @@ -26,15 +27,20 @@
<style>
body {
color: #2c353a;
font-family: Lucida Grande,Lucida,Verdana,sans-serif;
font-family: Lucida Grande, Lucida, Verdana, sans-serif;
padding: 15px;
}

#error, .not-found, .closed, .error {
#error,
.not-found,
.closed,
.error {
color: red
}

.found, .open, .success {
.found,
.open,
.success {
color: green;
}

Expand All @@ -53,9 +59,20 @@
.section {
margin: 20px;
}

table {
border-collapse: collapse;
}

table td {
border: 1px solid black;
padding: 3px;
}
</style>
</head>

<body>
<div id="root"></div>
</body>
</html>

</html>
71 changes: 47 additions & 24 deletions demo/v2/v2.jsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,55 @@
import React from 'react';

class V2 extends React.Component {
constructor() {
super();
this.state = {
indexes: []
};
constructor() {
super();
this.state = {
tools: []
};

console.debug(this)
}
render() {
const indexes = this.state.indexes.map((index, i) =>
<li key={i}>
{index}
</li>);
}

return (
<section>
<h2>V2</h2>
<section>
<h3>Indexes</h3>
<ul>
{ indexes }
</ul>
</section>
</section>
)
}
componentDidMount() {
this.daemon = this.props.daemon;

this.daemon.agentV2Found.subscribe(daemonV2 => {
if (!daemonV2) {
return;
}
this.daemonV2 = daemonV2;
this.daemonV2.installedTools().then(res => {
this.setState({
tools: res
});
});
})
}

render() {
const tools = this.state.tools.map((tool, i) =>
<tr>
<td>{tool.packager}</td>
<td>{tool.name}</td>
<td>{tool.version}</td>
</tr>);

return (
<section>
<h2>V2</h2>
<section>
<h3>Installed tools</h3>
<table>
<tr>
<th>Packager</th>
<th>Name</th>
<th>Version</th>
</tr>
{tools}
</table>
</section>
</section >
)
}
}

export default V2;
7 changes: 6 additions & 1 deletion src/socket-daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ import io from 'socket.io-client';
import semVerCompare from 'semver-compare';
import { detect } from 'detect-browser';

import { timer } from 'rxjs';
import { timer, BehaviorSubject } from 'rxjs';
import { filter, takeUntil, first } from 'rxjs/operators';

import Daemon from './daemon';
import V2 from './v2';

// Required agent version
const browser = detect();
Expand Down Expand Up @@ -62,10 +63,14 @@ export default class SocketDaemon extends Daemon {

this.openChannel(() => this.socket.emit('command', 'list'));

this.agentV2Found = new BehaviorSubject(null);

this.agentFound
.subscribe(agentFound => {
if (agentFound) {
this._wsConnect();
this.v2 = new V2(this.pluginURL);
this.agentV2Found.next(this.v2);
}
else {
this.findAgent();
Expand Down
34 changes: 34 additions & 0 deletions src/v2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export default class SocketDaemonV2 {
constructor(daemonURL) {
this.daemonURL = daemonURL + '/v2/';
}

// installedTools uses the new v2 apis to ask the daemon a list of the tools already present in the system
installedTools() {
return fetch(`${this.daemonURL}/pkgs/tools/installed`, {
method: 'GET',
}).then(res => {
return res.json();
})
}

// installTool uses the new v2 apis to ask the daemon to download a specific tool on the system
// The expected payload is
// {
// "name": "avrdude",
// "version": "6.3.0-arduino9",
// "packager": "arduino",
// "url": "https://downloads.arduino.cc/...", // system-specific package containing the tool
// "signature": "e7Gh8309...", // proof that the url comes from a trusted source
// "checksum": "SHA256:90384nhfoso8..." // proof that the package wasn't tampered with
// }
installTool(payload) {
fetch(`${this.daemonURL}/pkgs/tools/installed`, {
method: 'POST',
body: JSON.stringify(payload)
})
.catch(error => {
console.error(error);
});
}
}