diff --git a/README.md b/README.md index 677ef75..b551c84 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ The following devices are currently supported: * Button switch * Aqara smart wireless switch * Motion sensor +* Aqara Motion sensor * Power plug (zigbee) * Power plug (wifi) * Yeelight White (mono) diff --git a/src/devices/Gateway.ts b/src/devices/Gateway.ts index fa3d2d5..785276e 100644 --- a/src/devices/Gateway.ts +++ b/src/devices/Gateway.ts @@ -48,8 +48,8 @@ export class Gateway extends events.EventEmitter { for (let SubDeviceClass of [Magnet, Motion, Switch, Weather]) { if (SubDeviceClass.acceptedModels.indexOf(msg.model) >= 0) { this._subdevices[msg.sid] = new SubDeviceClass(msg.sid, msg.model); - this._subdevices[msg.sid].on('values-updated', (sid: string) => { - this.emit("subdevice-values-updated", sid); + this._subdevices[msg.sid].on('values-updated', (sidOrMessage: string|any) => { + this.emit("subdevice-values-updated", sidOrMessage); }); this.emit("subdevice-found", msg.sid); } diff --git a/src/devices/GatewayMessageData.ts b/src/devices/GatewayMessageData.ts index 81d6441..04e8e9e 100644 --- a/src/devices/GatewayMessageData.ts +++ b/src/devices/GatewayMessageData.ts @@ -7,6 +7,7 @@ export interface GatewayMessageGetIdListData extends Array { export interface GatewayMessageDefaultSubdeviceData { voltage: number; + timestamp: number; } export interface GatewayMessageReadAckMagnetData extends GatewayMessageDefaultSubdeviceData { @@ -17,4 +18,10 @@ export interface GatewayMessageReadAckReportWeatherData extends GatewayMessageDe temperature?: string; humidity?: string; pressure?: string; +} + +export interface GatewayMessageReadAckReportMotionData extends GatewayMessageDefaultSubdeviceData { + status?: string; + no_motion?: string; + lux?: string; } \ No newline at end of file diff --git a/src/devices/GatewayServer.ts b/src/devices/GatewayServer.ts index 0841ef7..baed06f 100644 --- a/src/devices/GatewayServer.ts +++ b/src/devices/GatewayServer.ts @@ -157,7 +157,6 @@ 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/devices/Motion.ts b/src/devices/Motion.ts index 305e55c..44fa082 100644 --- a/src/devices/Motion.ts +++ b/src/devices/Motion.ts @@ -1,10 +1,13 @@ import {GatewaySubdevice} from "./GatewaySubdevice"; import {GatewayMessage} from "./GatewayMessage"; +import {GatewayMessageReadAckReportMotionData} from "./GatewayMessageData"; export class Motion extends GatewaySubdevice { + public lux = 0; + public lastMotionTimestamp; - static get acceptedModels():string[] { - return ['motion']; + static get acceptedModels(): string[] { + return ['motion', 'sensor_motion.aq2']; } get internalModel(): string { @@ -13,5 +16,15 @@ export class Motion extends GatewaySubdevice { handleMessage(msg: GatewayMessage) { super.handleMessage(msg); + if (msg.isReadAck() || msg.isReport()) { + let data = msg.data; + if (data.lux) { + this.lux = parseInt(data.lux); + } + if (data.status === "motion") { + this.lastMotionTimestamp = data.timestamp; + this.emit('values-updated', {sid: this.sid, data: {hasMotion: true}}); + } + } } } \ No newline at end of file diff --git a/src/nodes/gateway/GatewayConfigurator.ts b/src/nodes/gateway/GatewayConfigurator.ts index e64df11..71fe0cd 100644 --- a/src/nodes/gateway/GatewayConfigurator.ts +++ b/src/nodes/gateway/GatewayConfigurator.ts @@ -3,6 +3,7 @@ import {Constants} from "../constants"; import {GatewayServer} from "../../devices/GatewayServer"; import {Gateway} from "../../devices/Gateway"; import {GatewaySubdevice} from "../../devices/GatewaySubdevice"; +import {isString} from "util"; export interface IGatewayConfiguratorNode extends Node { ip: string; @@ -58,9 +59,13 @@ export default (RED: Red) => { this._gateway = GatewayServer.getInstance().getGateway(this.sid); if (this._gateway) { this._gateway.password = this.key; - this._gateway.on("subdevice-values-updated", (sid: string) => { + this._gateway.on("subdevice-values-updated", (sidOrMessage: string|any) => { + let sid = sidOrMessage.sid || sidOrMessage; let subdevice = this._gateway.getSubdevice(sid); if (subdevice) { + (sidOrMessage.data ? Object.keys(sidOrMessage.data) : []).forEach((key:string) => { + subdevice[key] = sidOrMessage.data[key]; + }); ( this).emit('subdevice-update', subdevice); } });