From a9ebcc97038ef2395659e50531f72bb73f4e6f92 Mon Sep 17 00:00:00 2001 From: Brian Dunstan Date: Thu, 1 Sep 2022 20:50:33 +0800 Subject: [PATCH 01/13] Update index.js --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 3857d07..b019c6f 100755 --- a/index.js +++ b/index.js @@ -112,7 +112,7 @@ function started(){ } // Send a NOOP to the Cgate server to keep the connection open and detect if the connection was closed setInterval(function(){ - console.log('NOOP'); + if (logging == true) {console.log('NOOP')}; command.write('NOOP\n'); },KEEPALIVE); } From 0c4007cc6922e9819a05c57ad71d1d9beaaa7d5a Mon Sep 17 00:00:00 2001 From: Brian Dunstan Date: Fri, 2 Sep 2022 13:13:07 +0800 Subject: [PATCH 02/13] Set development parameters --- index.js | 1 - settings.js | 14 +++++++------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index b019c6f..2c89953 100755 --- a/index.js +++ b/index.js @@ -113,7 +113,6 @@ function started(){ // Send a NOOP to the Cgate server to keep the connection open and detect if the connection was closed setInterval(function(){ if (logging == true) {console.log('NOOP')}; - command.write('NOOP\n'); },KEEPALIVE); } diff --git a/settings.js b/settings.js index 3484639..3cecc44 100644 --- a/settings.js +++ b/settings.js @@ -4,26 +4,26 @@ exports.cbusip = '127.0.0.1'; //cbus project name -exports.cbusname = "HOME"; +exports.cbusname = "BURS1303"; //mqtt server ip:port exports.mqtt = '127.0.0.1:1883'; //username and password (unncomment to use) -//exports.mqttusername = 'user1'; -//exports.mqttpassword = 'password1'; +exports.mqttusername = 'HA-BursMQTT'; +exports.mqttpassword = '6M&4DJMg'; // net and app for automatically requesting values -// exports.getallnetapp = '254/56'; +exports.getallnetapp = '254/56'; // whether to request on start (requires getallnetapp set as well) -// exports.getallonstart = true; +exports.getallonstart = true; // how often to request after start (in seconds), (requires getallnetapp set as well) -// exports.getallperiod = 60*60; +exports.getallperiod = 60*60; // Sets MQTT retain flag for values coming from cgate -// exports.retainreads = true; +exports.retainreads = true; exports.messageinterval = 0; From f0778bef77991cc6eee631bd3c8cf1baadb32f46 Mon Sep 17 00:00:00 2001 From: bdnstn Date: Sat, 3 Sep 2022 12:10:46 +0800 Subject: [PATCH 03/13] Create .devcontainer.json --- .devcontainer.json | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 .devcontainer.json diff --git a/.devcontainer.json b/.devcontainer.json new file mode 100644 index 0000000..4bddb45 --- /dev/null +++ b/.devcontainer.json @@ -0,0 +1,24 @@ +{ + "name": "Example devcontainer for add-on repositories", + "image": "ghcr.io/home-assistant/devcontainer:addons", + "appPort": ["7123:8123", "7357:4357"], + "postStartCommand": "bash devcontainer_bootstrap", + "runArgs": ["-e", "GIT_EDITOR=code --wait", "--privileged"], + "containerEnv": { + "WORKSPACE_DIRECTORY": "${containerWorkspaceFolder}" + }, + "extensions": ["timonwong.shellcheck", "esbenp.prettier-vscode"], + "mounts": [ "type=volume,target=/var/lib/docker" ], + "settings": { + "terminal.integrated.profiles.linux": { + "zsh": { + "path": "/usr/bin/zsh" + } + }, + "terminal.integrated.defaultProfile.linux": "zsh", + "editor.formatOnPaste": false, + "editor.formatOnSave": true, + "editor.formatOnType": true, + "files.trimTrailingWhitespace": true + } +} From 7b9e49057e3e6a8c0c65ba368ef548bad5febd97 Mon Sep 17 00:00:00 2001 From: bdnstn Date: Sat, 3 Sep 2022 12:11:43 +0800 Subject: [PATCH 04/13] Create tasks.json --- .vscode/tasks.json | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 .vscode/tasks.json diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..091effa --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,20 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "Start Home Assistant", + "type": "shell", + "command": "supervisor_run", + "group": { + "kind": "test", + "isDefault": true + }, + "presentation": { + "reveal": "always", + "panel": "new" + }, + "problemMatcher": [] + } + ] + } + From f4277d5d5892daf9d3c5a703dbef29131fbf9b7d Mon Sep 17 00:00:00 2001 From: Brian Dunstan Date: Sat, 3 Sep 2022 14:22:14 +0800 Subject: [PATCH 05/13] Replace write('NOOP\n') deleted by accident. Replace accidently deleted line 116. --- index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/index.js b/index.js index 2c89953..b019c6f 100755 --- a/index.js +++ b/index.js @@ -113,6 +113,7 @@ function started(){ // Send a NOOP to the Cgate server to keep the connection open and detect if the connection was closed setInterval(function(){ if (logging == true) {console.log('NOOP')}; + command.write('NOOP\n'); },KEEPALIVE); } From 4ff9b087b4e5a2d6b6dacc97cc443c78d599fe3d Mon Sep 17 00:00:00 2001 From: Brian Dunstan Date: Sat, 3 Sep 2022 14:50:00 +0800 Subject: [PATCH 06/13] Unwind accidental inclusions in PR Sorry for the noise. I didn't realize my commits were all being included in the PR #1. I should have created a branch. --- .devcontainer.json | 24 ------------------------ .vscode/tasks.json | 20 -------------------- settings.js | 16 ++++++++-------- 3 files changed, 8 insertions(+), 52 deletions(-) delete mode 100644 .devcontainer.json delete mode 100644 .vscode/tasks.json diff --git a/.devcontainer.json b/.devcontainer.json deleted file mode 100644 index 4bddb45..0000000 --- a/.devcontainer.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "name": "Example devcontainer for add-on repositories", - "image": "ghcr.io/home-assistant/devcontainer:addons", - "appPort": ["7123:8123", "7357:4357"], - "postStartCommand": "bash devcontainer_bootstrap", - "runArgs": ["-e", "GIT_EDITOR=code --wait", "--privileged"], - "containerEnv": { - "WORKSPACE_DIRECTORY": "${containerWorkspaceFolder}" - }, - "extensions": ["timonwong.shellcheck", "esbenp.prettier-vscode"], - "mounts": [ "type=volume,target=/var/lib/docker" ], - "settings": { - "terminal.integrated.profiles.linux": { - "zsh": { - "path": "/usr/bin/zsh" - } - }, - "terminal.integrated.defaultProfile.linux": "zsh", - "editor.formatOnPaste": false, - "editor.formatOnSave": true, - "editor.formatOnType": true, - "files.trimTrailingWhitespace": true - } -} diff --git a/.vscode/tasks.json b/.vscode/tasks.json deleted file mode 100644 index 091effa..0000000 --- a/.vscode/tasks.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "version": "2.0.0", - "tasks": [ - { - "label": "Start Home Assistant", - "type": "shell", - "command": "supervisor_run", - "group": { - "kind": "test", - "isDefault": true - }, - "presentation": { - "reveal": "always", - "panel": "new" - }, - "problemMatcher": [] - } - ] - } - diff --git a/settings.js b/settings.js index 3cecc44..6cc15cd 100644 --- a/settings.js +++ b/settings.js @@ -4,28 +4,28 @@ exports.cbusip = '127.0.0.1'; //cbus project name -exports.cbusname = "BURS1303"; +exports.cbusname = "HOME"; //mqtt server ip:port exports.mqtt = '127.0.0.1:1883'; //username and password (unncomment to use) -exports.mqttusername = 'HA-BursMQTT'; -exports.mqttpassword = '6M&4DJMg'; +//exports.mqttusername = 'user1'; +//exports.mqttpassword = 'password1'; // net and app for automatically requesting values -exports.getallnetapp = '254/56'; +// exports.getallnetapp = '254/56'; // whether to request on start (requires getallnetapp set as well) -exports.getallonstart = true; +// exports.getallonstart = true; // how often to request after start (in seconds), (requires getallnetapp set as well) -exports.getallperiod = 60*60; +// exports.getallperiod = 60*60; // Sets MQTT retain flag for values coming from cgate -exports.retainreads = true; +// exports.retainreads = true; exports.messageinterval = 0; //logging -exports.logging = false; +exports.logging = false; \ No newline at end of file From 8b3c02392e88674974e61a612c83a792eb892822 Mon Sep 17 00:00:00 2001 From: Brian Dunstan Date: Thu, 6 Oct 2022 16:25:39 +0800 Subject: [PATCH 07/13] Update mqtt version number to 4.3.7 update mqtt version in package.json - old version number was causing null object error on server id. --- .gitignore | 1 + package.json | 2 +- settings.js | 16 ++++++++-------- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 3c3629e..9933453 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +package-lock.json diff --git a/package.json b/package.json index 63bb844..96f7af7 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "0.0.1", "private": true, "dependencies": { - "mqtt": "^0.3.12", + "mqtt": "^4.3.7", "xml2js": "^0.4.19" } } diff --git a/settings.js b/settings.js index 6cc15cd..6c3fe75 100644 --- a/settings.js +++ b/settings.js @@ -4,28 +4,28 @@ exports.cbusip = '127.0.0.1'; //cbus project name -exports.cbusname = "HOME"; +exports.cbusname = "BURS1303"; //mqtt server ip:port exports.mqtt = '127.0.0.1:1883'; //username and password (unncomment to use) -//exports.mqttusername = 'user1'; -//exports.mqttpassword = 'password1'; +exports.mqttusername = 'HA-BursMQTT'; +exports.mqttpassword = 'TW0Yo2DnO!7VzjAvoijttpn%EeNZ3o'; // net and app for automatically requesting values -// exports.getallnetapp = '254/56'; +exports.getallnetapp = '254/56'; // whether to request on start (requires getallnetapp set as well) -// exports.getallonstart = true; +exports.getallonstart = true; // how often to request after start (in seconds), (requires getallnetapp set as well) -// exports.getallperiod = 60*60; +exports.getallperiod = 60*60; // Sets MQTT retain flag for values coming from cgate -// exports.retainreads = true; +exports.retainreads = true; exports.messageinterval = 0; //logging -exports.logging = false; \ No newline at end of file +exports.logging = false; From 8613822c724342219acae35a40aea6b277933841 Mon Sep 17 00:00:00 2001 From: bdnstn Date: Thu, 6 Oct 2022 16:30:22 +0800 Subject: [PATCH 08/13] Update settings.js --- settings.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/settings.js b/settings.js index 6c3fe75..fed0687 100644 --- a/settings.js +++ b/settings.js @@ -10,8 +10,8 @@ exports.cbusname = "BURS1303"; exports.mqtt = '127.0.0.1:1883'; //username and password (unncomment to use) -exports.mqttusername = 'HA-BursMQTT'; -exports.mqttpassword = 'TW0Yo2DnO!7VzjAvoijttpn%EeNZ3o'; +exports.mqttusername = 'user'; +exports.mqttpassword = 'pass'; // net and app for automatically requesting values exports.getallnetapp = '254/56'; From cf27cdada49ff0d8a45cd1a0eaf90602bd344247 Mon Sep 17 00:00:00 2001 From: bdnstn Date: Sat, 5 Nov 2022 12:43:17 +0800 Subject: [PATCH 09/13] Update index.js --- index.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index b019c6f..8c7dbc9 100755 --- a/index.js +++ b/index.js @@ -171,7 +171,6 @@ client.on('connect', function() { // When connected } }); queue2.write('GET //'+settings.cbusname+'/'+parts[2]+'/'+parts[3]+'/'+parts[4]+' level\n'); - break; case "DECREASE": @@ -182,15 +181,26 @@ client.on('connect', function() { // When connected } }); queue2.write('GET //'+settings.cbusname+'/'+parts[2]+'/'+parts[3]+'/'+parts[4]+' level\n'); - break; + case "TERMINATE": + eventEmitter.on('level',function terminateRamp(address) { + if (address == parts[2]+'/'+parts[3]+'/'+parts[4]) { + queue2.write('TERMINATERAMP //'+settings.cbusname+'/'+parts[2]+'/'+parts[3]+'/'+parts[4]+'\n'); + eventEmitter.removeListener('level',terminateRamp); + } + }); + queue2.write('GET //'+settings.cbusname+'/'+parts[2]+'/'+parts[3]+'/'+parts[4]+' level\n'); + break; + case "ON": queue2.write('ON //'+settings.cbusname+'/'+parts[2]+'/'+parts[3]+'/'+parts[4]+'\n'); break; + case "OFF": queue2.write('OFF //'+settings.cbusname+'/'+parts[2]+'/'+parts[3]+'/'+parts[4]+'\n'); break; + default: var ramp = message.split(","); var num = Math.round(parseInt(ramp[0])*255/100) From 746098f825c5e2077d2ade2791596ba6ddcd4250 Mon Sep 17 00:00:00 2001 From: bdnstn Date: Sun, 6 Nov 2022 13:38:39 +0800 Subject: [PATCH 10/13] Change from TERMINATE to TERMINATERAMP `TERMINATERAMP` is the command in the C-Bus, so it make sense to use it in MQTT also. --- index.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 8c7dbc9..e7ccf56 100755 --- a/index.js +++ b/index.js @@ -183,15 +183,15 @@ client.on('connect', function() { // When connected queue2.write('GET //'+settings.cbusname+'/'+parts[2]+'/'+parts[3]+'/'+parts[4]+' level\n'); break; - case "TERMINATE": - eventEmitter.on('level',function terminateRamp(address) { - if (address == parts[2]+'/'+parts[3]+'/'+parts[4]) { - queue2.write('TERMINATERAMP //'+settings.cbusname+'/'+parts[2]+'/'+parts[3]+'/'+parts[4]+'\n'); - eventEmitter.removeListener('level',terminateRamp); - } - }); - queue2.write('GET //'+settings.cbusname+'/'+parts[2]+'/'+parts[3]+'/'+parts[4]+' level\n'); - break; + case "TERMINATERAMP": + eventEmitter.on('level',function terminateRamp(address) { + if (address == parts[2]+'/'+parts[3]+'/'+parts[4]) { + queue2.write('TERMINATERAMP //'+settings.cbusname+'/'+parts[2]+'/'+parts[3]+'/'+parts[4]+'\n'); + eventEmitter.removeListener('level',terminateRamp); + } + }); + queue2.write('GET //'+settings.cbusname+'/'+parts[2]+'/'+parts[3]+'/'+parts[4]+' level\n'); + break; case "ON": queue2.write('ON //'+settings.cbusname+'/'+parts[2]+'/'+parts[3]+'/'+parts[4]+'\n'); From 909ce66505c824640f0e354a6b4495c59745c301 Mon Sep 17 00:00:00 2001 From: Brian Dunstan Date: Thu, 29 Aug 2024 15:23:37 +0800 Subject: [PATCH 11/13] Logging parameter changed to integer --- index.js | 68 ++++++++++++++++++++++++++--------------------------- settings.js | 4 ++-- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/index.js b/index.js index e7ccf56..d80b125 100755 --- a/index.js +++ b/index.js @@ -98,21 +98,21 @@ event.connect(EVENTPORT, HOST); function started(){ if(commandConnected && eventConnected && client.connected){ - console.log('ALL CONNECTED'); + console.log('cgateweb: ALL CONNECTED'); if(settings.getallnetapp && settings.getallonstart) { - console.log('Getting all values'); + console.log('cgateweb: Getting all values'); queue2.write('GET //'+settings.cbusname+'/'+settings.getallnetapp+'/* level\n'); } if(settings.getallnetapp && settings.getallperiod) { clearInterval(interval); setInterval(function(){ - console.log('Getting all values'); + console.log('cgateweb: Getting all values'); queue2.write('GET //'+settings.cbusname+'/'+settings.getallnetapp+'/* level\n'); },settings.getallperiod*1000); } // Send a NOOP to the Cgate server to keep the connection open and detect if the connection was closed setInterval(function(){ - if (logging == true) {console.log('NOOP')}; + if (logging = 9) {console.log('cgateweb: NOOP')}; command.write('NOOP\n'); },KEEPALIVE); } @@ -125,7 +125,7 @@ client.on('disconnect',function(){ client.on('connect', function() { // When connected clientConnected = true; - console.log('CONNECTED TO MQTT: ' + settings.mqtt); + console.log('cgateweb: CONNECTED TO MQTT: ' + settings.mqtt); started() // Subscribe to MQTT @@ -133,7 +133,7 @@ client.on('connect', function() { // When connected // when a message arrives, do something with it client.on('message', function(topic, message, packet) { - if (logging == true) {console.log('Message received on ' + topic + ' : ' + message);} + if (logging > 0) {console.log('cgateweb: Message received on ' + topic + ' : ' + message);} parts = topic.split("/"); if (parts.length > 5) @@ -225,16 +225,16 @@ client.on('connect', function() { // When connected }); command.on('error',function(err){ - console.log('COMMAND ERROR:'+JSON.stringify(err)) + console.log('cgateweb: COMMAND ERROR:'+JSON.stringify(err)) }) event.on('error',function(err){ - console.log('EVENT ERROR:'+JSON.stringify(err)) + console.log('cgateweb: EVENT ERROR:'+JSON.stringify(err)) }) command.on('connect',function(err){ commandConnected = true; - console.log('CONNECTED TO C-GATE COMMAND PORT: ' + HOST + ':' + COMPORT); + console.log('cgateweb: CONNECTED TO C-GATE COMMAND PORT: ' + HOST + ':' + COMPORT); queue2.write('EVENT ON\n'); started() clearInterval(commandInterval); @@ -242,7 +242,7 @@ command.on('connect',function(err){ event.on('connect',function(err){ eventConnected = true; - console.log('CONNECTED TO C-GATE EVENT PORT: ' + HOST + ':' + EVENTPORT); + console.log('cgateweb: CONNECTED TO C-GATE EVENT PORT: ' + HOST + ':' + EVENTPORT); started() clearInterval(eventInterval); }) @@ -250,24 +250,24 @@ event.on('connect',function(err){ command.on('close',function(){ commandConnected = false; - console.log('COMMAND PORT DISCONNECTED') + console.log('cgateweb: COMMAND PORT DISCONNECTED') commandInterval = setTimeout(function(){ - console.log('COMMAND PORT RECONNECTING...') + console.log('cgateweb: COMMAND PORT RECONNECTING...') command.connect(COMPORT, HOST) },10000) }) event.on('close',function(){ eventConnected = false; - console.log('EVENT PORT DISCONNECTED') + console.log('cgateweb: EVENT PORT DISCONNECTED') eventInterval = setTimeout(function(){ - console.log('EVENT PORT RECONNECTING...') + console.log('cgateweb: EVENT PORT RECONNECTING...') event.connect(EVENTPORT, HOST) },10000) }) command.on('data',function(data) { - // if (logging == true) {console.log('Command data: ' + data);} + if (logging > 0) {console.log('cgateweb: Command data: ' + data);} // Was commented out. var lines = (buffer+data.toString()).split("\n"); buffer = lines[lines.length-1]; if (lines.length > 1) { @@ -279,14 +279,14 @@ command.on('data',function(data) { address = (parts2[0].substring(0,parts2[0].length-1)).split("/"); var level = parts2[1].split("="); if (parseInt(level[1]) == 0) { - if (logging == true) {console.log('C-Bus status received: '+address[3] +'/'+address[4]+'/'+address[5]+' OFF');} - if (logging == true) {console.log('C-Bus status received: '+address[3] +'/'+address[4]+'/'+address[5]+' 0%');} + if (logging > 0) {console.log('cgateweb: C-Bus status received: '+address[3] +'/'+address[4]+'/'+address[5]+' OFF');} + if (logging > 0) {console.log('cgateweb: C-Bus status received: '+address[3] +'/'+address[4]+'/'+address[5]+' 0%');} queue.publish('cbus/read/'+address[3]+'/'+address[4]+'/'+address[5]+'/state' , 'OFF',options, function() {}); queue.publish('cbus/read/'+address[3]+'/'+address[4]+'/'+address[5]+'/level' , '0',options, function() {}); eventEmitter.emit('level',address[3]+'/'+address[4]+'/'+address[5],0); } else { - if (logging == true) {console.log('C-Bus status received: '+address[3] +'/'+address[4]+'/'+address[5]+' ON');} - if (logging == true) {console.log('C-Bus status received: '+address[3] +'/'+address[4]+'/'+address[5]+' '+Math.round(parseInt(level[1])*100/255).toString()+'%');} + if (logging > 0) {console.log('cgateweb: C-Bus status received: '+address[3] +'/'+address[4]+'/'+address[5]+' ON');} + if (logging > 0) {console.log('cgateweb: C-Bus status received: '+address[3] +'/'+address[4]+'/'+address[5]+' '+Math.round(parseInt(level[1])*100/255).toString()+'%');} queue.publish('cbus/read/'+address[3]+'/'+address[4]+'/'+address[5]+'/state' , 'ON',options, function() {}); queue.publish('cbus/read/'+address[3]+'/'+address[4]+'/'+address[5]+'/level' , Math.round(parseInt(level[1])*100/255).toString(),options, function() {}); eventEmitter.emit('level',address[3]+'/'+address[4]+'/'+address[5],Math.round(parseInt(level[1]))); @@ -299,7 +299,7 @@ command.on('data',function(data) { } else if(parts1[0].split(" ")[0] == "344"){ parseString(tree, function (err, result) { try{ - if(logging === true) {console.log("C-Bus tree received:"+JSON.stringify(result))} + if(logging > 0) {console.log("C-Bus tree received:"+JSON.stringify(result))} queue.publish('cbus/read/'+treenet+'///tree',JSON.stringify(result)) }catch(err){ console.log(err) @@ -312,14 +312,14 @@ command.on('data',function(data) { address = (parts2[1].substring(0,parts2[1].length-1)).split("/"); var level = parts2[2].split("="); if (parseInt(level[1]) == 0) { - if (logging == true) {console.log('C-Bus status received: '+address[3] +'/'+address[4]+'/'+address[5]+' OFF');} - if (logging == true) {console.log('C-Bus status received: '+address[3] +'/'+address[4]+'/'+address[5]+' 0%');} + if (logging > 0) {console.log('cgateweb: C-Bus status received: '+address[3] +'/'+address[4]+'/'+address[5]+' OFF');} + if (logging > 0) {console.log('cgateweb: C-Bus status received: '+address[3] +'/'+address[4]+'/'+address[5]+' 0%');} queue.publish('cbus/read/'+address[3]+'/'+address[4]+'/'+address[5]+'/state' , 'OFF',options, function() {}); queue.publish('cbus/read/'+address[3]+'/'+address[4]+'/'+address[5]+'/level' , '0',options, function() {}); eventEmitter.emit('level',address[3]+'/'+address[4]+'/'+address[5],0); } else { - if (logging == true) {console.log('C-Bus status received: '+address[3] +'/'+address[4]+'/'+address[5]+' ON');} - if (logging == true) {console.log('C-Bus status received: '+address[3] +'/'+address[4]+'/'+address[5]+' '+Math.round(parseInt(level[1])*100/255).toString()+'%');} + if (logging > 0) {console.log('cgateweb: C-Bus status received: '+address[3] +'/'+address[4]+'/'+address[5]+' ON');} + if (logging > 0) {console.log('cgateweb: C-Bus status received: '+address[3] +'/'+address[4]+'/'+address[5]+' '+Math.round(parseInt(level[1])*100/255).toString()+'%');} queue.publish('cbus/read/'+address[3]+'/'+address[4]+'/'+address[5]+'/state' , 'ON',options, function() {}); queue.publish('cbus/read/'+address[3]+'/'+address[4]+'/'+address[5]+'/level' , Math.round(parseInt(level[1])*100/255).toString(),options, function() {}); eventEmitter.emit('level',address[3]+'/'+address[4]+'/'+address[5],Math.round(parseInt(level[1]))); @@ -336,34 +336,34 @@ command.on('data',function(data) { // Add a 'data' event handler for the client socket // data is what the server sent to this socket event.on('data', function(data) { - if (logging == true) {console.log('Event data: ' + data);} + if (logging > 0) {console.log('cgateweb: Event data: ' + data);} data.toString().split(/\r?\n/).forEach(line => { - if (logging == true) {console.log('Event line: ' + line);} + if (logging > 0) {console.log('cgateweb: Event line: ' + line);} var parts = line.split(" "); if(parts[0] == "lighting") { address = parts[2].split("/"); switch(parts[1]) { case "on": - if (logging == true) {console.log('C-Bus status received: '+address[3] +'/'+address[4]+'/'+address[5]+' ON');} - if (logging == true) {console.log('C-Bus status received: '+address[3] +'/'+address[4]+'/'+address[5]+' 100%');} + if (logging > 0) {console.log('cgateweb: C-Bus status received: '+address[3] +'/'+address[4]+'/'+address[5]+' ON');} + if (logging > 0) {console.log('cgateweb: C-Bus status received: '+address[3] +'/'+address[4]+'/'+address[5]+' 100%');} queue.publish('cbus/read/'+address[3]+'/'+address[4]+'/'+address[5]+'/state' , 'ON',options, function() {}); queue.publish('cbus/read/'+address[3]+'/'+address[4]+'/'+address[5]+'/level' , '100',options, function() {}); break; case "off": - if (logging == true) {console.log('C-Bus status received: '+address[3] +'/'+address[4]+'/'+address[5]+' OFF');} - if (logging == true) {console.log('C-Bus status received: '+address[3] +'/'+address[4]+'/'+address[5]+' 0%');} + if (logging > 0) {console.log('cgateweb: C-Bus status received: '+address[3] +'/'+address[4]+'/'+address[5]+' OFF');} + if (logging > 0) {console.log('cgateweb: C-Bus status received: '+address[3] +'/'+address[4]+'/'+address[5]+' 0%');} queue.publish('cbus/read/'+address[3]+'/'+address[4]+'/'+address[5]+'/state' , 'OFF',options, function() {}); queue.publish('cbus/read/'+address[3]+'/'+address[4]+'/'+address[5]+'/level' , '0',options, function() {}); break; case "ramp": if(parseInt(parts[3]) > 0) { - if (logging == true) {console.log('C-Bus status received: '+address[3] +'/'+address[4]+'/'+address[5]+' ON');} - if (logging == true) {console.log('C-Bus status received: '+address[3] +'/'+address[4]+'/'+address[5]+' '+Math.round(parseInt(parts[3])*100/255).toString()+'%');} + if (logging > 0) {console.log('cgateweb: C-Bus status received: '+address[3] +'/'+address[4]+'/'+address[5]+' ON');} + if (logging > 0) {console.log('cgateweb: C-Bus status received: '+address[3] +'/'+address[4]+'/'+address[5]+' '+Math.round(parseInt(parts[3])*100/255).toString()+'%');} queue.publish('cbus/read/'+address[3]+'/'+address[4]+'/'+address[5]+'/state' , 'ON',options, function() {}); queue.publish('cbus/read/'+address[3]+'/'+address[4]+'/'+address[5]+'/level' , Math.round(parseInt(parts[3])*100/255).toString(),options, function() {}); } else { - if (logging == true) {console.log('C-Bus status received: '+address[3] +'/'+address[4]+'/'+address[5]+' OFF');} - if (logging == true) {console.log('C-Bus status received: '+address[3] +'/'+address[4]+'/'+address[5]+' 0%');} + if (logging > 0) {console.log('cgateweb: C-Bus status received: '+address[3] +'/'+address[4]+'/'+address[5]+' OFF');} + if (logging > 0) {console.log('cgateweb: C-Bus status received: '+address[3] +'/'+address[4]+'/'+address[5]+' 0%');} queue.publish('cbus/read/'+address[3]+'/'+address[4]+'/'+address[5]+'/state' , 'OFF',options, function() {}); queue.publish('cbus/read/'+address[3]+'/'+address[4]+'/'+address[5]+'/level' , '0',options, function() {}); } diff --git a/settings.js b/settings.js index fed0687..6adfeb1 100644 --- a/settings.js +++ b/settings.js @@ -27,5 +27,5 @@ exports.retainreads = true; exports.messageinterval = 0; -//logging -exports.logging = false; +//logging > 0 writes logs except NOOP. logging = 9 includes NOOP logging. +exports.logging = 0; \ No newline at end of file From 63869fd90bd568f01e34187ac2f0f35f570a1ac5 Mon Sep 17 00:00:00 2001 From: Brian Dunstan Date: Thu, 29 Aug 2024 19:14:24 +0800 Subject: [PATCH 12/13] Fix equality operator --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index d80b125..4b2ce4a 100755 --- a/index.js +++ b/index.js @@ -112,7 +112,7 @@ function started(){ } // Send a NOOP to the Cgate server to keep the connection open and detect if the connection was closed setInterval(function(){ - if (logging = 9) {console.log('cgateweb: NOOP')}; + if (logging == 9) {console.log('cgateweb: NOOP')}; command.write('NOOP\n'); },KEEPALIVE); } From 0d1554603798db797ca49c66cafeeb632bf2e51a Mon Sep 17 00:00:00 2001 From: Brian Dunstan Date: Thu, 29 Aug 2024 20:42:54 +0800 Subject: [PATCH 13/13] Reduce keepalive from 30 to 10000 --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 4b2ce4a..bdc3daa 100755 --- a/index.js +++ b/index.js @@ -85,7 +85,7 @@ var queue2 = { var HOST = settings.cbusip; var COMPORT = 20023; var EVENTPORT = 20025; -var KEEPALIVE = 30; +var KEEPALIVE = 10000; var logging = settings.logging; @@ -112,7 +112,7 @@ function started(){ } // Send a NOOP to the Cgate server to keep the connection open and detect if the connection was closed setInterval(function(){ - if (logging == 9) {console.log('cgateweb: NOOP')}; + if (logging > 0) {console.log('cgateweb: NOOP')}; command.write('NOOP\n'); },KEEPALIVE); }