|
|
@@ -29,6 +29,7 @@ function Client(sock) {
|
|
|
if (this.active) {
|
|
|
delete clients[this.id];
|
|
|
require("./outputs.js").unregisterOutput(this.id);
|
|
|
+ this.sock = null;
|
|
|
}
|
|
|
});
|
|
|
this.radios = {};
|
|
|
@@ -44,13 +45,13 @@ function Client(sock) {
|
|
|
|
|
|
Client.prototype.onData = function(data) {
|
|
|
if (data.startsWith("HELO"))
|
|
|
- this.name = data.replace(/^HELO\s+/, "") +" (" +this.sock.remoteAddress +")";
|
|
|
+ this.name = data.substr("HELO".length).trim() +" (" +this.sock.remoteAddress +")";
|
|
|
else if (data.startsWith("STATUS")) {
|
|
|
data = JSON.parse(data.substr("STATUS".length));
|
|
|
if (data) {
|
|
|
this.status.battery = data.battery || this.status.battery;
|
|
|
- this.status.plugged = data.plugged !== undefined ? data.plugged :this.status.battery;
|
|
|
- this.status.plugged = data.volume !== undefined ? data.volume :this.status.volume;
|
|
|
+ this.status.plugged = data.plugged !== undefined ? data.plugged :this.status.plugged;
|
|
|
+ this.status.volume = data.volume !== undefined ? data.volume :this.status.volume;
|
|
|
this.status.lastUpdate = Date.now();
|
|
|
}
|
|
|
} else if (data.startsWith("PONG")) {
|
|
|
@@ -61,13 +62,20 @@ Client.prototype.onData = function(data) {
|
|
|
duration: now -this.ping.pending,
|
|
|
pending: 0
|
|
|
};
|
|
|
+ this.scheduleNextPing();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
Client.prototype.requestPing = function() {
|
|
|
- this.ping.pending = Date.now();
|
|
|
- this.sock.write("PING");
|
|
|
+ if (this.sock) {
|
|
|
+ this.ping.pending = Date.now();
|
|
|
+ this.sock.write("PING\n");
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+Client.prototype.scheduleNextPing = function() {
|
|
|
+ setTimeout(() => {this.requestPing();}, 90000); // 1 min 30 sec
|
|
|
}
|
|
|
|
|
|
Client.prototype.getName = function() {
|
|
|
@@ -87,7 +95,7 @@ Client.prototype.getInputs = function() {
|
|
|
}
|
|
|
|
|
|
Client.prototype.sendInputStates = function() {
|
|
|
- this.sock.write(JSON.stringify(this.radios) +"\n");
|
|
|
+ this.sock.write("SET" +JSON.stringify(this.radios) +"\n");
|
|
|
}
|
|
|
|
|
|
Client.prototype.setState = function(inputId, state) {
|
|
|
@@ -103,7 +111,7 @@ Client.prototype.setState = function(inputId, state) {
|
|
|
Client.prototype.getStatus = function() {
|
|
|
return {
|
|
|
status: this.status.lastUpdate ? this.status : undefined,
|
|
|
- ping: this.ping.lastUpdate ? this.ping : undefined
|
|
|
+ ping: this.ping.lastUpdate ? { lastUpdate: this.ping.lastUpdate, duration: this.ping.duration, pending: !!this.ping.pending } : undefined
|
|
|
};
|
|
|
}
|
|
|
|
|
|
@@ -112,6 +120,7 @@ function onClientConnection(sock) {
|
|
|
if (clients[cli.id])
|
|
|
clients[cli.id].kill();
|
|
|
clients[cli.id] = cli;
|
|
|
+ cli.requestPing();
|
|
|
require("./outputs.js").registerOutput(cli.id, cli);
|
|
|
}
|
|
|
|