isundil 6 жил өмнө
parent
commit
50693019b5
4 өөрчлөгдсөн 27 нэмэгдсэн , 14 устгасан
  1. 4 4
      config.js
  2. 1 1
      outputs.js
  3. 6 2
      public/script.js
  4. 16 7
      remoteOutput.js

+ 4 - 4
config.js

@@ -4,10 +4,10 @@ module.exports = {
 	TCP_PORT: 9001,
 	TCP_LISTEN: "192.168.0.5",
 	RADIOS: {
-		Arch: "http://192.168.0.5:80/arch.ogg",
-		Windows10: "http://192.168.0.5:80/win.ogg",
-		HDMI: "http://192.168.0.5:80/hdmi.ogg",
-		Spotify: "http://192.168.0.5:80/spotify.ogg"
+		Arch: "http://192.168.0.5:80/arch.mp3",
+		Windows10: "http://192.168.0.5:80/win.mp3",
+		HDMI: "http://192.168.0.5:80/hdmi.mp3",
+		Spotify: "http://192.168.0.5:80/spotify.mp3"
 	},
 	ALSA_INPUT: {
 		volume: {

+ 1 - 1
outputs.js

@@ -10,7 +10,7 @@ function listOutputs() {
 			id: id,
 			inputs: i.inputs || i.getInputs(),
 			volumeControl: i.volumeControl,
-			status: i.getStatus()
+			status: i.getStatus ? i.getStatus():undefined
 		});
 	}
 	return result;

+ 6 - 2
public/script.js

@@ -20,8 +20,12 @@
 
 	function makeOutputStatus(data) {
 		var statusDiv = document.createElement("div");
-		statusDiv.className = "status" +data.status ? "" : " no-status";
-		statusDiv.innerHTML = JSON.stringify(data.status);
+		statusDiv.className = "status";
+		if (data.status) {
+			statusDiv.innerHTML = JSON.stringify(data.status);
+		} else {
+			statusDiv.classList.add("no-status");
+		}
 		return statusDiv;
 	}
 

+ 16 - 7
remoteOutput.js

@@ -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);
 }