From 7bff50da164e7037cd8f780511f8dc816de45598 Mon Sep 17 00:00:00 2001 From: Pierre CLEMENT Date: Tue, 20 Feb 2018 22:33:47 +0100 Subject: [PATCH] fix(gateway): set password to send write commands --- src/devices/Gateway.ts | 13 ++++++++----- src/devices/GatewayServer.ts | 1 + src/nodes/gateway/GatewayConfigurator.ts | 15 +++++++++------ 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/devices/Gateway.ts b/src/devices/Gateway.ts index 950aa45..695616b 100644 --- a/src/devices/Gateway.ts +++ b/src/devices/Gateway.ts @@ -9,17 +9,21 @@ import {Color} from "../utils/Color"; export class Gateway extends events.EventEmitter { static iv: Buffer = Buffer.from([0x17, 0x99, 0x6d, 0x09, 0x3d, 0x28, 0xdd, 0xb3, 0xba, 0x69, 0x5a, 0x2e, 0x6f, 0x58, 0x56, 0x2e]); protected lastToken: string; - protected password: string; + protected _password: string; private _subdevices: { [sid: string]: GatewaySubdevice } = {}; constructor(public sid: string, public ip: string) { super(); } - get key(): string { - if (!this.lastToken || !this.password) return null; + set password(password: string) { + this._password = password; + } - var cipher = crypto.createCipheriv('aes-128-cbc', this.password, Gateway.iv); + get key(): string { + if (!this.lastToken || !this._password) return null; + + var cipher = crypto.createCipheriv('aes-128-cbc', this._password, Gateway.iv); var key = cipher.update(Buffer.from(this.lastToken), "ascii", "hex"); cipher.final('hex'); @@ -30,7 +34,6 @@ export class Gateway extends events.EventEmitter { if (msg.data) { if (msg.model === "gateway" && msg.sid === this.sid && msg.token) { this.lastToken = msg.token; - this.setLight(100, {red: 255, green: 0, blue: 0}); } } diff --git a/src/devices/GatewayServer.ts b/src/devices/GatewayServer.ts index 8f02871..2ee5f6c 100644 --- a/src/devices/GatewayServer.ts +++ b/src/devices/GatewayServer.ts @@ -157,6 +157,7 @@ export class GatewayServer extends events.EventEmitter { } sendToGateway(sid: string, message: any) { + console.log(message); if (this.server && this._gateways[sid]) { let msg = Buffer.from(JSON.stringify(message)); this.server.send(msg, 0, msg.length, GatewayServer.SERVER_PORT, this._gateways[sid].ip); diff --git a/src/nodes/gateway/GatewayConfigurator.ts b/src/nodes/gateway/GatewayConfigurator.ts index 2a93270..e64df11 100644 --- a/src/nodes/gateway/GatewayConfigurator.ts +++ b/src/nodes/gateway/GatewayConfigurator.ts @@ -56,12 +56,15 @@ export default (RED: Red) => { protected setGateway() { this._gateway = GatewayServer.getInstance().getGateway(this.sid); - this._gateway && this._gateway.on("subdevice-values-updated", (sid: string) => { - let subdevice = this._gateway.getSubdevice(sid); - if (subdevice) { - ( this).emit('subdevice-update', subdevice); - } - }); + if (this._gateway) { + this._gateway.password = this.key; + this._gateway.on("subdevice-values-updated", (sid: string) => { + let subdevice = this._gateway.getSubdevice(sid); + if (subdevice) { + ( this).emit('subdevice-update', subdevice); + } + }); + } } get gateway(): Gateway {