feat(motion): handle motion sensor
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ export interface GatewayMessageGetIdListData extends Array<string> {
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -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 = <GatewayMessageReadAckReportMotionData> 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}});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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];
|
||||
});
|
||||
(<any> this).emit('subdevice-update', subdevice);
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user