From b6e311965a1f11001d4b3aef40dfb042d7a165ce Mon Sep 17 00:00:00 2001 From: Pierre CLEMENT Date: Wed, 21 Feb 2018 22:01:31 +0100 Subject: [PATCH] feat(gateway): addset light and play sounds --- .../xiaomi-actions.html | 201 ------------------ .../xiaomi-actions.js | 128 ----------- node-red-contrib-xiaomi-all/xiaomi-all.html | 124 ----------- node-red-contrib-xiaomi-all/xiaomi-all.js | 50 ----- src/devices/Gateway.ts | 25 ++- src/devices/GatewayServer.ts | 2 +- src/nodes/actions/GatewayLight.ts | 34 --- src/nodes/actions/GatewayPlaySound.ejs | 81 +++++++ src/nodes/actions/GatewayPlaySound.ts | 31 +++ src/nodes/actions/GatewayStopSound.ejs | 36 ++++ src/nodes/actions/GatewayStopSound.ts | 26 +++ .../actions/{GatewayLight.ejs => Light.ejs} | 6 +- src/nodes/actions/Light.ts | 31 +++ src/nodes/actions/index.ejs | 4 +- src/nodes/actions/index.ts | 15 +- src/nodes/gateway/Gateway.ts | 41 ++-- src/nodes/gateway/GatewayOut.ts | 18 +- src/utils.js | 94 -------- 18 files changed, 280 insertions(+), 667 deletions(-) delete mode 100644 node-red-contrib-xiaomi-all/xiaomi-all.html delete mode 100644 node-red-contrib-xiaomi-all/xiaomi-all.js delete mode 100644 src/nodes/actions/GatewayLight.ts create mode 100644 src/nodes/actions/GatewayPlaySound.ejs create mode 100644 src/nodes/actions/GatewayPlaySound.ts create mode 100644 src/nodes/actions/GatewayStopSound.ejs create mode 100644 src/nodes/actions/GatewayStopSound.ts rename src/nodes/actions/{GatewayLight.ejs => Light.ejs} (95%) create mode 100644 src/nodes/actions/Light.ts delete mode 100644 src/utils.js diff --git a/node-red-contrib-xiaomi-actions/xiaomi-actions.html b/node-red-contrib-xiaomi-actions/xiaomi-actions.html index f535e44..fe523c3 100644 --- a/node-red-contrib-xiaomi-actions/xiaomi-actions.html +++ b/node-red-contrib-xiaomi-actions/xiaomi-actions.html @@ -1,204 +1,3 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/node-red-contrib-xiaomi-all/xiaomi-all.js b/node-red-contrib-xiaomi-all/xiaomi-all.js deleted file mode 100644 index 04c96fc..0000000 --- a/node-red-contrib-xiaomi-all/xiaomi-all.js +++ /dev/null @@ -1,50 +0,0 @@ -module.exports = (RED) => { - function getOnlyModelsValue(input) { - var cleanOnlyModels = []; - input.forEach((value) => { - cleanOnlyModels = cleanOnlyModels.concat(value.split(',')); - }); - return cleanOnlyModels; - } - - function XiaomiAllNode(config) { - RED.nodes.createNode(this, config); - this.gateway = RED.nodes.getNode(config.gateway); - this.onlyModels = getOnlyModelsValue(config.onlyModels || []); - this.excludedSids = config.excludedSids; - - - this.isDeviceValid = (device) => { - if((!this.onlyModels || this.onlyModels.length == 0) && (!this.excludedSids || this.excludedSids.length == 0)) { - return true; - } - // Is excluded - if((this.excludedSids && this.excludedSids.length != 0) && this.excludedSids.indexOf(device.sid) >= 0) { - return false; - } - if((this.onlyModels && this.onlyModels.length != 0) && this.onlyModels.indexOf(device.model) >= 0) { - return true; - } - - return false; - } - - if (this.gateway) { - this.on('input', (msg) => { - // Filter input - if(msg.payload && msg.payload.model && msg.payload.sid) { - if(!this.isDeviceValid(msg.payload)) { - msg = null; - } - } - // Prepare for request - else { - msg.payload = this.gateway.deviceList.filter((device) => this.isDeviceValid(device)); - } - this.send(msg); - }); - } - } - - RED.nodes.registerType("xiaomi-all", XiaomiAllNode); -} diff --git a/src/devices/Gateway.ts b/src/devices/Gateway.ts index 695616b..fa3d2d5 100644 --- a/src/devices/Gateway.ts +++ b/src/devices/Gateway.ts @@ -39,7 +39,7 @@ export class Gateway extends events.EventEmitter { if (msg.isGetIdListAck()) { ( msg.data).forEach((sid) => { - this.send({cmd: "read", sid: sid}); + this.read(sid); }); } @@ -69,7 +69,17 @@ export class Gateway extends events.EventEmitter { return !!this._subdevices[sid]; } - setLight(brightness, rgb) { + getIdList() { + this.send({ + cmd: "get_id_list", + }) + } + + read(sid?: string) { + this.send({cmd: "read", sid: sid || this.sid}); + } + + setLight(brightness: number, rgb: { red: number, green: number, blue: number }) { this.send({ cmd: "write", data: { @@ -79,8 +89,15 @@ export class Gateway extends events.EventEmitter { }); } - playSound() { - + playSound(musicId: number, volume: number) { + this.send({ + cmd: "write", + data: { + mid: musicId, + volume: volume, + sid: this.sid + } + }); } send(message: any) { diff --git a/src/devices/GatewayServer.ts b/src/devices/GatewayServer.ts index 2ee5f6c..0841ef7 100644 --- a/src/devices/GatewayServer.ts +++ b/src/devices/GatewayServer.ts @@ -67,7 +67,7 @@ export class GatewayServer extends events.EventEmitter { if ((msg.isHeartbeat() || msg.isIam()) && msg.model === "gateway") { if (!this._gateways[msg.sid]) { this._gateways[msg.sid] = new Gateway(msg.sid, remote.address); - this.sendToGateway(msg.sid, {cmd: "get_id_list"}); + this._gateways[msg.sid].getIdList(); this.emit("gateway-online", msg.sid); } else { diff --git a/src/nodes/actions/GatewayLight.ts b/src/nodes/actions/GatewayLight.ts deleted file mode 100644 index 27794fb..0000000 --- a/src/nodes/actions/GatewayLight.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { Red, NodeProperties } from "node-red"; -import { Constants } from "../constants"; - -export default (RED:Red) => { - class GatewayLight { - public color:string; - public brightness:number; - - constructor(props:NodeProperties) { - RED.nodes.createNode( this, props); - ( this).setListeners(); - } - - protected setListeners() { - ( this).on('input', (msg) => { - let color = msg.color || this.color; - let brightness = msg.brightness || this.brightness; - if(msg.sid) { - msg.payload = { - cmd: "write", - data: { rgb: 123, sid: msg.sid } - }; - } - else { - msg.payload = { - brightness: brightness - }; - } - ( this).send(msg); - }); - } - } - RED.nodes.registerType(`${Constants.NODES_PREFIX}-actions gateway_light`, GatewayLight); -}; \ No newline at end of file diff --git a/src/nodes/actions/GatewayPlaySound.ejs b/src/nodes/actions/GatewayPlaySound.ejs new file mode 100644 index 0000000..e30e960 --- /dev/null +++ b/src/nodes/actions/GatewayPlaySound.ejs @@ -0,0 +1,81 @@ + + + + + + \ No newline at end of file diff --git a/src/nodes/actions/GatewayPlaySound.ts b/src/nodes/actions/GatewayPlaySound.ts new file mode 100644 index 0000000..c4c2f64 --- /dev/null +++ b/src/nodes/actions/GatewayPlaySound.ts @@ -0,0 +1,31 @@ +import {Red, NodeProperties} from "node-red"; +import {Constants} from "../constants"; + +export default (RED: Red) => { + class GatewayPlaySound { + public mid: number; + public volume: number; + + constructor(props: NodeProperties) { + RED.nodes.createNode( this, props); + this.mid = parseInt((props).mid); + this.volume = parseInt((props).volume); + ( this).setListeners(); + } + + protected setListeners() { + ( this).on('input', (msg) => { + if (msg.sid) { + msg.payload = { + action: "playSound", + mid: msg.mid || this.mid, + volume: msg.volume || this.volume + }; + } + ( this).send(msg); + }); + } + } + + RED.nodes.registerType(`${Constants.NODES_PREFIX}-actions gateway_play_sound`, GatewayPlaySound); +}; \ No newline at end of file diff --git a/src/nodes/actions/GatewayStopSound.ejs b/src/nodes/actions/GatewayStopSound.ejs new file mode 100644 index 0000000..5ecdbef --- /dev/null +++ b/src/nodes/actions/GatewayStopSound.ejs @@ -0,0 +1,36 @@ + + + + + \ No newline at end of file diff --git a/src/nodes/actions/GatewayStopSound.ts b/src/nodes/actions/GatewayStopSound.ts new file mode 100644 index 0000000..18d8444 --- /dev/null +++ b/src/nodes/actions/GatewayStopSound.ts @@ -0,0 +1,26 @@ +import {Red, NodeProperties} from "node-red"; +import {Constants} from "../constants"; + +export default (RED: Red) => { + class GatewayStopSound { + + constructor(props: NodeProperties) { + RED.nodes.createNode( this, props); + ( this).setListeners(); + } + + protected setListeners() { + ( this).on('input', (msg) => { + if (msg.sid) { + msg.payload = { + action: "playSound", + mid: 1000 + }; + } + ( this).send(msg); + }); + } + } + + RED.nodes.registerType(`${Constants.NODES_PREFIX}-actions gateway_stop_sound`, GatewayStopSound); +}; \ No newline at end of file diff --git a/src/nodes/actions/GatewayLight.ejs b/src/nodes/actions/Light.ejs similarity index 95% rename from src/nodes/actions/GatewayLight.ejs rename to src/nodes/actions/Light.ejs index 02887ec..d082636 100644 --- a/src/nodes/actions/GatewayLight.ejs +++ b/src/nodes/actions/Light.ejs @@ -1,6 +1,6 @@ - -