2019-02-02 12:09:20 -07:00
|
|
|
const utils = require('../util/utils');
|
|
|
|
const interval = utils.secondsToMilliseconds(1);
|
|
|
|
const logger = require('../util/logger');
|
|
|
|
|
|
|
|
const foundationCfg = {manufSpec: 0, disDefaultRsp: 0};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extension required for Livolo device support.
|
|
|
|
*/
|
2019-02-13 12:55:14 -07:00
|
|
|
class Livolo {
|
2019-02-04 10:36:49 -07:00
|
|
|
constructor(zigbee, mqtt, state, publishEntityState) {
|
2019-02-02 12:09:20 -07:00
|
|
|
this.zigbee = zigbee;
|
|
|
|
this.timer = null;
|
|
|
|
this.configured = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
onZigbeeStarted() {
|
|
|
|
this.startTimer();
|
|
|
|
}
|
|
|
|
|
2019-02-04 08:55:44 -07:00
|
|
|
_resetDeviceState(ieeeAddr) {
|
|
|
|
this.configured[ieeeAddr] = {
|
|
|
|
stage: 0,
|
|
|
|
retry: 0,
|
|
|
|
waitresp: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
onZigbeeMessage(message, device, mappedDevice) {
|
|
|
|
if (!device) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((message.type == 'devInterview') ||
|
|
|
|
(message.type == 'devIncoming') ||
|
|
|
|
(message.type == 'endDeviceAnnce') ||
|
|
|
|
(message.type == 'devStatus')) {
|
|
|
|
if (this.configured.hasOwnProperty(device.ieeeAddr)) {
|
|
|
|
logger.info(`LIVOLO ${device.ieeeAddr}. (Re)joins in the network (after power off?)`);
|
|
|
|
this._resetDeviceState(device.ieeeAddr);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-02 12:09:20 -07:00
|
|
|
startTimer() {
|
|
|
|
this.clearTimer();
|
|
|
|
this.timer = setInterval(() => this.handleInterval(), interval);
|
|
|
|
}
|
|
|
|
|
|
|
|
clearTimer() {
|
|
|
|
if (this.timer) {
|
|
|
|
clearTimeout(this.timer);
|
|
|
|
this.timer = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stop() {
|
|
|
|
this.clearTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
_handleCommandRespSimple(err, rsp) {
|
2019-02-04 08:55:44 -07:00
|
|
|
this.ext.configured[this.ieeeAddr].waitresp = false;
|
|
|
|
|
2019-02-02 12:09:20 -07:00
|
|
|
if (err) {
|
2019-02-04 08:55:44 -07:00
|
|
|
if (this.ctype === 'toggle') {
|
|
|
|
logger.debug(`LIVOLO ${this.ieeeAddr}. Toggle command error:`, err.message);
|
2019-02-02 12:09:20 -07:00
|
|
|
}
|
|
|
|
} else {
|
2019-02-04 08:55:44 -07:00
|
|
|
logger.debug(`LIVOLO ${this.ieeeAddr}. Sucessfully configured`, rsp);
|
|
|
|
this.ext.configured[this.ieeeAddr].stage = 1; // sucessfully send command
|
|
|
|
this.ext.configured[this.ieeeAddr].retry = 0;
|
2019-02-02 12:09:20 -07:00
|
|
|
this.device.status = 'online';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_handleCommandRespWithData(err, rsp) {
|
2019-02-04 08:55:44 -07:00
|
|
|
this.ext.configured[this.ieeeAddr].waitresp = false;
|
|
|
|
|
2019-02-02 12:09:20 -07:00
|
|
|
if (err) {
|
2019-02-04 08:55:44 -07:00
|
|
|
logger.info(`LIVOLO ${this.ieeeAddr}. ${this.cid}.${this.ctype} response error:`, err.message);
|
|
|
|
if (this.ext.configured[this.ieeeAddr].retry >= 3) {
|
|
|
|
// errors in three sequental reads, stop polling, wait for a device message
|
|
|
|
this.device.status = 'offline';
|
|
|
|
logger.info(`LIVOLO ${this.ieeeAddr}. Stopped polling after 3 unsuccessful attempts`);
|
|
|
|
}
|
2019-02-02 12:09:20 -07:00
|
|
|
} else {
|
2019-02-04 08:55:44 -07:00
|
|
|
this.ext.configured[this.ieeeAddr].retry = 0;
|
2019-02-02 12:09:20 -07:00
|
|
|
this.device.status = 'online';
|
|
|
|
if (this.ext.zigbee) {
|
|
|
|
this.ext.zigbee.shepherd.emit('ind:reported', this.ep, this.cid, rsp, this.ep.last_af_msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// msg: { groupid, clusterid, srcaddr, srcendpoint, dstendpoint, wasbroadcast,
|
|
|
|
// linkquality, securityuse, timestamp, transseqnumber, len, data }
|
|
|
|
_handleAfMessage(msg, ep) {
|
|
|
|
ep.linkquality = msg.linkquality;
|
|
|
|
ep.last_af_msg = msg;
|
|
|
|
}
|
|
|
|
|
2019-02-04 08:55:44 -07:00
|
|
|
_sendToggle(zdev, ieeeAddr, ep, retry) {
|
|
|
|
this.zigbee.queue.push((queueCallback) => {
|
|
|
|
const cfg = {};
|
|
|
|
logger.debug(`LIVOLO ${ieeeAddr}. Sending the 'toggle' command. Retry: ${retry}`);
|
|
|
|
ep.functional('genOnOff', 'toggle', [cfg], foundationCfg,
|
|
|
|
this._handleCommandRespSimple.bind({
|
|
|
|
device: zdev,
|
|
|
|
ieeeAddr: ieeeAddr,
|
|
|
|
cid: 'genOnOff',
|
|
|
|
ctype: 'toggle',
|
|
|
|
ext: this,
|
|
|
|
}));
|
|
|
|
|
|
|
|
queueCallback();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_sendPoll(zdev, ieeeAddr, ep, retry) {
|
|
|
|
this.zigbee.queue.push((queueCallback) => {
|
|
|
|
ep.foundation('genOnOff', 'read', [{
|
|
|
|
attrId: 0, // onOff
|
|
|
|
}], this._handleCommandRespWithData.bind({
|
|
|
|
device: zdev,
|
|
|
|
ieeeAddr: ieeeAddr,
|
|
|
|
ep: ep,
|
|
|
|
cid: 'genOnOff',
|
|
|
|
ctype: 'read',
|
|
|
|
ext: this,
|
|
|
|
}));
|
|
|
|
|
|
|
|
queueCallback();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-02 12:09:20 -07:00
|
|
|
handleInterval() {
|
|
|
|
this.zigbee.getAllClients()
|
2019-02-03 09:11:41 -07:00
|
|
|
.filter((d) => d.manufName && d.manufName.startsWith('LIVOLO')) // LIVOLO
|
2019-02-02 12:09:20 -07:00
|
|
|
.filter((d) => d.type === 'EndDevice') // Filter end devices
|
|
|
|
.filter((d) => d.powerSource && d.powerSource !== 'Battery') // Remove battery powered devices
|
|
|
|
.forEach((d) => {
|
|
|
|
const zdev = this.zigbee.shepherd._findDevByAddr(d.ieeeAddr);
|
|
|
|
if (zdev && zdev.endpoints) {
|
|
|
|
const eplist = Object.keys(zdev.endpoints).filter((epId) => {
|
|
|
|
const ep2 = zdev.getEndpoint(epId);
|
|
|
|
const clist = ep2.getClusterList();
|
|
|
|
return clist && clist.includes(6); // 6 - genOnOff
|
|
|
|
});
|
|
|
|
|
|
|
|
if (eplist.length > 0) {
|
|
|
|
const ep = zdev.getEndpoint(eplist[0]);
|
|
|
|
|
|
|
|
ep.onAfIncomingMsg = this._handleAfMessage;
|
|
|
|
|
|
|
|
if (!this.configured.hasOwnProperty(d.ieeeAddr)) {
|
2019-02-04 08:55:44 -07:00
|
|
|
this._resetDeviceState(d.ieeeAddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
const state = this.configured[d.ieeeAddr];
|
|
|
|
if (state.waitresp) {
|
|
|
|
return;
|
2019-02-02 12:09:20 -07:00
|
|
|
}
|
|
|
|
|
2019-02-04 08:55:44 -07:00
|
|
|
if (state.retry < 3) {
|
|
|
|
if (state.stage === 0) {
|
|
|
|
state.retry += 1;
|
|
|
|
state.waitresp = true;
|
|
|
|
this._sendToggle(zdev, d.ieeeAddr, ep, state.retry);
|
|
|
|
} else if (state.stage === 1) {
|
|
|
|
state.retry += 1;
|
|
|
|
state.waitresp = true;
|
|
|
|
this._sendPoll(zdev, d.ieeeAddr, ep, state.retry);
|
|
|
|
}
|
2019-02-02 12:09:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-13 12:55:14 -07:00
|
|
|
module.exports = Livolo;
|