isundil 7 ani în urmă
părinte
comite
a326e1e7ad
3 a modificat fișierele cu 51 adăugiri și 1 ștergeri
  1. 2 1
      outputs.js
  2. 8 0
      public/script.js
  3. 41 0
      remoteOutput.js

+ 2 - 1
outputs.js

@@ -9,7 +9,8 @@ function listOutputs() {
 			name: i.name || i.getName(),
 			id: id,
 			inputs: i.inputs || i.getInputs(),
-			volumeControl: i.volumeControl
+			volumeControl: i.volumeControl,
+			status: i.getStatus()
 		});
 	}
 	return result;

+ 8 - 0
public/script.js

@@ -18,11 +18,19 @@
 		});
 	}
 
+	function makeOutputStatus(data) {
+		var statusDiv = document.createElement("div");
+		statusDiv.className = "status" +data.status ? "" : " no-status";
+		statusDiv.innerHTML = JSON.stringify(data.status);
+		return statusDiv;
+	}
+
 	function makeOutputNode(data) {
 		var child = document.createElement("fieldset"),
 			inputList = document.createElement("ul");
 		child.classList.add("output");
 		child.innerHTML += "<legend>" +data.name +"</legend>";
+		child.appendChild(makeOutputStatus(data));
 		for (var i in data.inputs) {
 			var inputItem = document.createElement("li");
 			inputItem.classList.add("input");

+ 41 - 0
remoteOutput.js

@@ -10,6 +10,17 @@ function Client(sock) {
 	this.sock = sock;
 	this.active = 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) => {
 		var dataStr = data.toString("utf-8");
 		dataStr.split(/[\r\n]/).forEach(i => i.length ? _this.onData(i) : i);
@@ -34,6 +45,29 @@ function Client(sock) {
 Client.prototype.onData = function(data) {
 	if (data.startsWith("HELO"))
 		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() {
@@ -66,6 +100,13 @@ Client.prototype.setState = function(inputId, state) {
 	return true;
 }
 
+Client.prototype.getStatus = function() {
+	return {
+		status: this.status.lastUpdate ? this.status : undefined,
+		ping: this.ping.lastUpdate ? this.ping : undefined
+	};
+}
+
 function onClientConnection(sock) {
 	var cli = new Client(sock);
 	if (clients[cli.id])