|
@@ -10,6 +10,17 @@ function Client(sock) {
|
|
|
this.sock = sock;
|
|
this.sock = sock;
|
|
|
this.active = true;
|
|
this.active = true;
|
|
|
this.volumeControl = true;
|
|
this.volumeControl = true;
|
|
|
|
|
+ this.status = {
|
|
|
|
|
+ battery: undefined,
|
|
|
|
|
+ plugged: undefined,
|
|
|
|
|
+ volume: undefined,
|
|
|
|
|
+ lastUpdate: 0
|
|
|
|
|
+ };
|
|
|
|
|
+ this.ping = {
|
|
|
|
|
+ duration: 0,
|
|
|
|
|
+ lastUpdate: 0,
|
|
|
|
|
+ pending: 0
|
|
|
|
|
+ };
|
|
|
this.sock.on('data', (data) => {
|
|
this.sock.on('data', (data) => {
|
|
|
var dataStr = data.toString("utf-8");
|
|
var dataStr = data.toString("utf-8");
|
|
|
dataStr.split(/[\r\n]/).forEach(i => i.length ? _this.onData(i) : i);
|
|
dataStr.split(/[\r\n]/).forEach(i => i.length ? _this.onData(i) : i);
|
|
@@ -34,6 +45,29 @@ function Client(sock) {
|
|
|
Client.prototype.onData = function(data) {
|
|
Client.prototype.onData = function(data) {
|
|
|
if (data.startsWith("HELO"))
|
|
if (data.startsWith("HELO"))
|
|
|
this.name = data.replace(/^HELO\s+/, "") +" (" +this.sock.remoteAddress +")";
|
|
this.name = data.replace(/^HELO\s+/, "") +" (" +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.lastUpdate = Date.now();
|
|
|
|
|
+ }
|
|
|
|
|
+ } else if (data.startsWith("PONG")) {
|
|
|
|
|
+ if (this.ping.pending) {
|
|
|
|
|
+ const now = Date.now();
|
|
|
|
|
+ this.ping = {
|
|
|
|
|
+ lastUpdate: now,
|
|
|
|
|
+ duration: now -this.ping.pending,
|
|
|
|
|
+ pending: 0
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+Client.prototype.requestPing = function() {
|
|
|
|
|
+ this.ping.pending = Date.now();
|
|
|
|
|
+ this.sock.write("PING");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
Client.prototype.getName = function() {
|
|
Client.prototype.getName = function() {
|
|
@@ -66,6 +100,13 @@ Client.prototype.setState = function(inputId, state) {
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+Client.prototype.getStatus = function() {
|
|
|
|
|
+ return {
|
|
|
|
|
+ status: this.status.lastUpdate ? this.status : undefined,
|
|
|
|
|
+ ping: this.ping.lastUpdate ? this.ping : undefined
|
|
|
|
|
+ };
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
function onClientConnection(sock) {
|
|
function onClientConnection(sock) {
|
|
|
var cli = new Client(sock);
|
|
var cli = new Client(sock);
|
|
|
if (clients[cli.id])
|
|
if (clients[cli.id])
|