ctucx.git: smartie-pwa

[js] smarthome web-gui

commit 93d19cebab06ad842d12aef2cc10096f56fbb981
parent a624a9a2c48d4c386de401f4cf2fba9ebe16ec40
Author: Milan Pässler <me@pbb.lc>
Date: Tue, 4 Jun 2019 21:53:19 +0200

Update 'websocket-relay/server.js'
1 file changed, 16 insertions(+), 18 deletions(-)
M
websocket-relay/server.js
|
34
++++++++++++++++------------------
diff --git a/websocket-relay/server.js b/websocket-relay/server.js
@@ -113,10 +113,10 @@ readRelays();
 
 /* powermeter */
 
-const counters = [
-	{ id: 60, name: "Kueche" },
-	{ id: 50, name: "Sonstiges" },
-];
+state.powermeter = {
+	60: { name: "Kueche" },
+	50: { name: "Sonstiges" },
+};
 
 const registers = {
 	voltage: 0,

@@ -163,12 +163,12 @@ async function fetchValues() {
 	socket.connect({'host': '192.168.1.1', 'port': 502 });
 	client.pipe(socket);
 
-	for (let counter of state.powermeter) {
+	for (let [id. counter] of Object.entries(state.powermeter)) {
 		for (let key of Object.keys(registers)) {
-			//try {
-				counter.values[key] = await getFloat32Register(counter.id, registers[key], client, socket);
+			try {
+				counter.values[key] = await getFloat32Register(id, registers[key], client, socket);
 				counter.lastUpdated = unixTimestamp();
-			//} catch(e) {}
+			} catch(e) {}
 		}
 	}
 	socket.end();

@@ -176,16 +176,14 @@ async function fetchValues() {
 	broadcastState();
 }
 
-state.powermeter = counters.map(function(c) {
-	return {
-		...c,
-		values: Object.keys(registers).reduce(function(result, key) {
-			result[key] = 0;
-			return result;
-		}, {}),
-		lastUpdated: unixTimestamp(),
-	};
-});
+
+for (let [id, counter] of state.powermeter) {
+	counter.values = Object.keys(registers).reduce(function(result, key) {
+		result[key] = 0;
+		return result;
+	}, {});
+	counter.lastUpdated: unixTimestamp();
+}
 
 setInterval(fetchValues, 60000);
 fetchValues();