Skip to content

Commit

Permalink
Merge pull request #11 from Z-Wave-Me/develop
Browse files Browse the repository at this point in the history
Added support for Z-Way Authentication token.
  • Loading branch information
bkorda committed Dec 12, 2021
2 parents 02a8ccd + 4c5a84a commit 83e94e2
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 21 deletions.
14 changes: 9 additions & 5 deletions lib/zway-socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const WebSocket = require('ws');

class ZWaySocket extends EventEmitter {
constructor({
authToken,
hostname,
port = 8083,
secure = false,
Expand All @@ -14,6 +15,7 @@ class ZWaySocket extends EventEmitter {
}={}) {
super();

this.authToken = authToken;
this.hostname = hostname;
this.port = port;
this.secure = secure;
Expand Down Expand Up @@ -46,9 +48,11 @@ class ZWaySocket extends EventEmitter {
}

try {
var address = this.buildAddress()
console.log(address)
this.socket = new WebSocket(address);
var address = this.buildAddress();
console.log(address);
var headers = {};
if (this.authToken) headers["Authorization"] = "Bearer " + this.authToken;
this.socket = new WebSocket(address, { headers: headers });
} catch (err) {
this.onClose(err);
throw err;
Expand All @@ -72,7 +76,7 @@ class ZWaySocket extends EventEmitter {
parseData(data) {
try {
var jsonRoot = JSON.parse(data);
var msg = JSON.parse(jsonRoot.data)
var msg = jsonRoot.data;
return msg;
} catch (err) {
return this.emit('error', err);
Expand Down Expand Up @@ -154,4 +158,4 @@ class ZWaySocket extends EventEmitter {
}
}

module.exports = ZWaySocket;
module.exports = ZWaySocket;
2 changes: 1 addition & 1 deletion nodes/out.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ module.exports = function(RED) {
node.status({}); //clean
}, 3000);
}
}).auth(node.server.login, node.server.pass);
}).auth(null, null, true, node.server.authToken);
}

formatHomeKit(message, payload) {
Expand Down
14 changes: 3 additions & 11 deletions nodes/server.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,8 @@
<input type="text" id="node-config-input-port">
</div> -->
<div class="form-row">
<label for="node-config-input-login" class="l-width"><i class="icon-lock"></i> User </span></label>
<input type="text" id="node-config-input-login">
</div>
<div class="form-row">
<label for="node-config-input-pass" class="l-width"><i class="icon-lock"></i> Password</span></label>
<input type="password" id="node-config-input-pass">
<label for="node-config-input-authToken" class="l-width"><i class="icon-lock"></i> Z-Way authorization token </span></label>
<input type="text" id="node-config-input-authToken">
</div>
<!-- <div class="form-row">
<label for="node-config-input-secure" class="l-width"><i class="icon-tag"></i> SSL</label>
Expand All @@ -45,14 +41,10 @@
required: true,
validate:RED.validators.number()
},
login: {
authToken: {
value: null,
required: true
},
pass: {
value: null,
required: true
}
// secure: {
// value: false,
// required: true
Expand Down
6 changes: 3 additions & 3 deletions nodes/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ module.exports = function(RED) {
node.name = n.name;
node.ip = n.ip;
node.port = 8083;
node.login = n.login;
node.pass = n.pass;
node.authToken = n.authToken;
node.secure = n.secure || false;
node.devices = {};

Expand All @@ -24,6 +23,7 @@ module.exports = function(RED) {
node.refreshDiscoverInterval = 15000;

node.socket = new ZWaySocket({
authToken: this.authToken,
hostname: this.ip,
secure: this.secure
});
Expand Down Expand Up @@ -86,7 +86,7 @@ module.exports = function(RED) {
node.discoverProcess = false;
callback(node.items);
return node.items;
}).auth(node.login, node.pass);
}).auth(null, null, true, node.authToken);
} else {
node.log('discoverDevices: Using cached devices');
callback(node.items);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/bkorda/node-red-contrib-zway/issues/"
},
"dependencies": {
"multiple-select": "^1.4.1",
"multiple-select": "^1.5.2",
"request": "latest",
"ws": "latest",
"events": "latest"
Expand Down

0 comments on commit 83e94e2

Please sign in to comment.