2
0
Files
node-red-contrib-mi-devices/src/nodes/gateway/Searcher.ts
Pierre CLEMENT c1299336cb feat(gateway): move to lumi-aqara
Also add gateway search and subdevices discovery.
Closes #28, closes #27, closes #26, closes #17 and fixes #12
2018-01-23 10:48:48 +01:00

42 lines
1.5 KiB
TypeScript

import { Red } from "node-red";
import * as LumiAqara from 'lumi-aqara';
import { Constants } from "../constants";
import { IGatewayConfiguratorNode } from "./GatewayConfigurator";
export class Searcher {
static _gateways:LumiAqara.Gateway[] = [];
static discover(RED:Red) {
new Promise(() => {
const aqara = new LumiAqara();
aqara.on('gateway', (gateway:LumiAqara.Gateway) => {
let frontGateway = {
sid: gateway.sid,
ip: gateway.ip,
subdevices: []
};
this._gateways.push(frontGateway);
gateway.on('subdevice', (device:LumiAqara.SubDevice) => {
frontGateway.subdevices.push({
sid: device.getSid(),
type: device.getType()
});
});
RED.nodes.eachNode((tmpNode) => {
if(tmpNode.type.indexOf(`${Constants.NODES_PREFIX}-gateway configurator`) === 0) {
let tmpNodeInst = <IGatewayConfiguratorNode> RED.nodes.getNode(tmpNode.id);
if(tmpNodeInst && (tmpNodeInst.ip === gateway.ip || tmpNodeInst.sid === gateway.sid)) {
tmpNodeInst.gateway = gateway;
}
}
});
});
});
}
static get gateways():LumiAqara.Gateway {
return this._gateways;
}
}