Skip to content

Commit 15ecad9

Browse files
author
Alberto Iannaccone
committed
close serial onenter and onexit
1 parent 8a7d586 commit 15ecad9

File tree

3 files changed

+52
-14
lines changed

3 files changed

+52
-14
lines changed

src/daemon.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,17 @@ export default class Daemon {
1313
network: []
1414
});
1515
this.socketMessages
16-
.subscribe(this.handleSocketMesage.bind(this));
16+
.subscribe(this.handleSocketMessage.bind(this));
1717
this.openingSerial = null;
1818
this.closingSerial = null;
19+
20+
const devicesListSubscription = this.devicesList.subscribe((devices) => {
21+
if (devices.serial && devices.serial.length > 0) {
22+
this.closeAllPorts();
23+
devicesListSubscription.unsubscribe();
24+
}
25+
});
26+
window.addEventListener('beforeunload', this.closeAllPorts);
1927
}
2028

2129
initSocket() {
@@ -38,24 +46,24 @@ export default class Daemon {
3846
* @param {Array<device>} a the first list
3947
* @param {Array<device>} b the second list
4048
*/
41-
static devicesListAreEquals(a, b) {
49+
devicesListAreEquals(a, b) {
4250
if (!a || !b || a.length !== b.length) {
4351
return false;
4452
}
45-
return a.every((item, index) => b[index].Name === item.Name);
53+
return a.every((item, index) => (b[index].Name === item.Name && b[index].IsOpen === item.IsOpen));
4654
}
4755

48-
handleSocketMesage(message) {
56+
handleSocketMessage(message) {
4957
// Result of a list command
5058
if (message.Ports) {
5159
const lastDevices = this.devicesList.getValue();
52-
if (message.Network && !Daemon.devicesListAreEquals(lastDevices.network, message.Ports)) {
60+
if (message.Network && !this.devicesListAreEquals(lastDevices.network, message.Ports)) {
5361
this.devicesList.next({
5462
serial: lastDevices.serial,
5563
network: message.Ports
5664
});
5765
}
58-
else if (!message.Network && !Daemon.devicesListAreEquals(lastDevices.serial, message.Ports)) {
66+
else if (!message.Network && !this.devicesListAreEquals(lastDevices.serial, message.Ports)) {
5967
this.devicesList.next({
6068
serial: message.Ports,
6169
network: lastDevices.network
@@ -68,6 +76,10 @@ export default class Daemon {
6876
}
6977
}
7078

79+
writeSerial(port, data) {
80+
this.socket.emit('command', `send ${port} ${data}`);
81+
}
82+
7183
openSerialMonitor(port) {
7284
if (this.serialMonitorOpened.getValue()) {
7385
return;
@@ -109,4 +121,15 @@ export default class Daemon {
109121
});
110122
this.socket.emit('command', `close ${port}`);
111123
}
124+
125+
closeAllPorts(e) {
126+
if (e) {
127+
e.preventDefault();
128+
}
129+
const devices = this.devicesList.getValue().serial;
130+
devices.forEach(device => {
131+
this.socket.emit('command', `close ${device.Name}`);
132+
});
133+
return;
134+
}
112135
}

test/app.jsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@ class App extends React.Component {
1717
serialDevices: [],
1818
networkDevices: [],
1919
agentInfo: '',
20-
serialMonitorContent: ''
20+
serialMonitorContent: '',
21+
serialPortOpen: ''
2122
};
2223
this.handleOpen = this.handleOpen.bind(this);
2324
this.handleClose = this.handleClose.bind(this);
25+
this.handleSend = this.handleSend.bind(this);
26+
this.showError = this.showError.bind(this);
27+
this.clearError = this.clearError.bind(this);
2428
this.daemon = Daemon;
2529
}
2630

@@ -65,15 +69,26 @@ class App extends React.Component {
6569
this.setState({ serialMonitorContent: '' });
6670
e.preventDefault();
6771
this.daemon.openSerialMonitor(port);
72+
this.setState({ serialPortOpen: port });
6873
}
6974

7075
handleClose(e, port) {
7176
e.preventDefault();
7277
this.daemon.closeSerialMonitor(port);
78+
this.setState({ serialPortOpen: null });
79+
}
80+
81+
handleSend(e) {
82+
e.preventDefault();
83+
const serialInput = document.getElementById('serial-input');
84+
const sendData = `${serialInput.value}\n`;
85+
this.daemon.writeSerial(this.state.serialPortOpen, sendData);
86+
serialInput.focus();
87+
serialInput.value = '';
7388
}
7489

7590
render() {
76-
const listSerialDevices = this.state.serialDevices.map((device, i) => (<li key={i}>{device.Name} - IsOpen: {device.IsOpen ? 'true' : 'false'} - <a href="#" onClick={(e) => this.handleOpen(e, device.Name)}>open</a> - <a href="#" onClick={(e) => this.handleClose(e, device.Name)}>close</a></li>));
91+
const listSerialDevices = this.state.serialDevices.map((device, i) => (<li key={i}>{device.Name} - IsOpen: <span className={device.IsOpen ? 'open' : 'closed'}>{device.IsOpen ? 'true' : 'false'}</span> - <a href="#" onClick={(e) => this.handleOpen(e, device.Name)}>open</a> - <a href="#" onClick={(e) => this.handleClose(e, device.Name)}>close</a></li>));
7792
const listNetworkDevices = this.state.networkDevices.map((device, i) => <li key={i}>{device.Name}</li>);
7893

7994
return (
@@ -102,6 +117,10 @@ class App extends React.Component {
102117
</div>
103118
<div className="serial-monitor">
104119
<h2>Serial Monitor</h2>
120+
<form onSubmit={this.handleSend}>
121+
<input id="serial-input" />
122+
<input type="submit" value="Send" />
123+
</form>
105124
<textarea id="serial-textarea" value={ this.state.serialMonitorContent }/>
106125
</div>
107126
</div>

test/index.html

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,14 @@
55
<meta http-equiv="X-UA-Compatible" content="ie=edge">
66
<title>Test Daemon</title>
77
<style>
8-
#error {
8+
#error, .not-found, .closed {
99
color: red
1010
}
1111

12-
.found {
12+
.found, .open {
1313
color: green;
1414
}
1515

16-
.not-found {
17-
color: red;
18-
}
19-
2016
pre {
2117
background: #f9f9f9;
2218
}

0 commit comments

Comments
 (0)