2018-04-18 09:25:40 -07:00
|
|
|
const ZShepherd = require('zigbee-shepherd');
|
|
|
|
const logger = require('./util/logger');
|
|
|
|
const settings = require('./util/settings');
|
2018-05-11 20:04:15 -07:00
|
|
|
const data = require('./util/data');
|
2018-05-21 02:49:02 -07:00
|
|
|
const debug = require('debug')('zigbee2mqtt:zigbee');
|
2018-04-18 09:25:40 -07:00
|
|
|
|
2018-05-24 11:19:04 -07:00
|
|
|
const advancedSettings = settings.get().advanced;
|
|
|
|
|
2018-04-18 09:25:40 -07:00
|
|
|
const shepherdSettings = {
|
2018-05-17 00:52:28 -07:00
|
|
|
net: {
|
2018-05-24 11:19:04 -07:00
|
|
|
panId: advancedSettings && advancedSettings.pan_id ? advancedSettings.pan_id : 0x1a62,
|
|
|
|
channelList: [advancedSettings && advancedSettings.channel ? advancedSettings.channel : 11],
|
2018-05-17 00:52:28 -07:00
|
|
|
},
|
2018-05-17 08:48:41 -07:00
|
|
|
dbPath: data.joinPath('database.db'),
|
2018-04-18 09:25:40 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
class Zigbee {
|
|
|
|
constructor() {
|
|
|
|
this.handleReady = this.handleReady.bind(this);
|
|
|
|
this.handleMessage = this.handleMessage.bind(this);
|
2018-05-17 08:48:41 -07:00
|
|
|
this.handleError = this.handleError.bind(this);
|
2018-04-18 09:25:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
start(onMessage, callback) {
|
|
|
|
logger.info(`Starting zigbee-shepherd`);
|
|
|
|
|
|
|
|
this.shepherd = new ZShepherd(settings.get().serial.port, shepherdSettings);
|
|
|
|
|
|
|
|
this.shepherd.start((error) => {
|
|
|
|
if (error) {
|
2018-06-04 08:36:46 -07:00
|
|
|
logger.warn('Error while starting zigbee-shepherd, attemping to fix... (takes 60 seconds)');
|
2018-06-01 12:04:48 -07:00
|
|
|
this.shepherd.controller._znp.close((() => null));
|
|
|
|
|
|
|
|
setTimeout(() => {
|
2018-06-04 08:36:46 -07:00
|
|
|
logger.info(`Starting zigbee-shepherd`);
|
2018-06-01 12:04:48 -07:00
|
|
|
this.shepherd.start((error) => {
|
|
|
|
if (error) {
|
|
|
|
logger.error('Error while starting zigbee-shepherd!');
|
|
|
|
callback(error);
|
|
|
|
} else {
|
|
|
|
logger.info('zigbee-shepherd started');
|
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
});
|
2018-06-04 08:36:46 -07:00
|
|
|
}, 60 * 1000);
|
2018-04-18 09:25:40 -07:00
|
|
|
} else {
|
|
|
|
logger.info('zigbee-shepherd started');
|
2018-06-01 12:04:48 -07:00
|
|
|
callback(null);
|
2018-04-18 09:25:40 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Register callbacks.
|
|
|
|
this.shepherd.on('ready', this.handleReady);
|
|
|
|
this.shepherd.on('ind', this.handleMessage);
|
2018-05-17 00:52:28 -07:00
|
|
|
this.shepherd.on('error', this.handleError);
|
|
|
|
|
2018-04-18 09:25:40 -07:00
|
|
|
this.onMessage = onMessage;
|
|
|
|
}
|
|
|
|
|
2018-05-21 02:49:02 -07:00
|
|
|
softReset(callback) {
|
|
|
|
this.shepherd.reset('soft', callback);
|
|
|
|
}
|
|
|
|
|
2018-04-18 09:25:40 -07:00
|
|
|
stop(callback) {
|
|
|
|
this.shepherd.stop((error) => {
|
2018-05-17 08:20:46 -07:00
|
|
|
logger.info('zigbee-shepherd stopped');
|
2018-04-18 09:25:40 -07:00
|
|
|
callback(error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleReady() {
|
|
|
|
// Set all Xiaomi devices (manufId === 4151) to be online, so shepherd won't try
|
|
|
|
// to query info from devices (which would fail because they go tosleep).
|
2018-05-17 00:52:28 -07:00
|
|
|
// Xiaomi lumi.plug has manufId === 4447 and can be in the sleep mode too
|
2018-05-16 10:29:47 -07:00
|
|
|
const devices = this.getAllClients();
|
2018-04-18 09:25:40 -07:00
|
|
|
devices.forEach((device) => {
|
2018-05-17 00:52:28 -07:00
|
|
|
if ((device.manufId === 4151) || (device.manufId === 4447)) {
|
2018-04-18 09:25:40 -07:00
|
|
|
this.shepherd.find(device.ieeeAddr, 1).getDevice().update({
|
|
|
|
status: 'online',
|
2018-05-17 08:20:46 -07:00
|
|
|
joinTime: Math.floor(Date.now() / 1000),
|
2018-04-18 09:25:40 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2018-05-16 10:29:47 -07:00
|
|
|
|
|
|
|
logger.info('zigbee-shepherd ready');
|
2018-04-24 10:30:56 -07:00
|
|
|
}
|
2018-04-18 09:25:40 -07:00
|
|
|
|
2018-05-17 00:52:28 -07:00
|
|
|
handleError(message) {
|
2018-05-17 08:48:41 -07:00
|
|
|
// This event may appear if zigbee-shepherd cannot decode bad packets (invalid checksum).
|
2018-05-17 00:52:28 -07:00
|
|
|
logger.error(message);
|
|
|
|
}
|
|
|
|
|
2018-04-24 10:30:56 -07:00
|
|
|
permitJoin(permit) {
|
|
|
|
if (permit) {
|
|
|
|
logger.info('Zigbee: allowing new devices to join.');
|
|
|
|
} else {
|
|
|
|
logger.info('Zigbee: disabling joining new devices.');
|
2018-04-18 09:25:40 -07:00
|
|
|
}
|
|
|
|
|
2018-04-24 10:30:56 -07:00
|
|
|
this.shepherd.permitJoin(permit ? 255 : 0, (error) => {
|
2018-04-18 09:25:40 -07:00
|
|
|
if (error) {
|
|
|
|
logger.info(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-23 09:17:47 -07:00
|
|
|
getAllClients() {
|
|
|
|
return this.shepherd.list().filter((device) => device.type !== 'Coordinator');
|
|
|
|
}
|
|
|
|
|
2018-05-21 02:49:02 -07:00
|
|
|
ping(deviceID) {
|
|
|
|
const device = this.shepherd._findDevByAddr(deviceID);
|
|
|
|
|
|
|
|
if (device) {
|
|
|
|
// Note: checkOnline has the callback argument but does not call callback
|
|
|
|
debug(`Check online ${deviceID}`);
|
|
|
|
this.shepherd.controller.checkOnline(device);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-18 09:25:40 -07:00
|
|
|
handleMessage(message) {
|
|
|
|
if (this.onMessage) {
|
|
|
|
this.onMessage(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-27 14:58:46 -07:00
|
|
|
getDevice(deviceID) {
|
|
|
|
return this.shepherd.list().find((d) => d.ieeeAddr === deviceID);
|
|
|
|
}
|
|
|
|
|
2018-06-04 12:36:51 -07:00
|
|
|
getCoordinator() {
|
|
|
|
const device = this.shepherd.list().find((d) => d.type === 'Coordinator');
|
|
|
|
return this.shepherd.find(device.ieeeAddr, 1);
|
|
|
|
}
|
|
|
|
|
2018-05-16 10:22:47 -07:00
|
|
|
publish(deviceID, cid, cmd, zclData, ep, callback) {
|
2018-05-30 13:28:08 -07:00
|
|
|
const device = this._findDevice(deviceID, ep);
|
|
|
|
if (!device) {
|
|
|
|
logger.error(`Zigbee cannot publish message to device because '${deviceID}' not known by zigbee-shepherd`);
|
2018-04-18 12:57:39 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-30 13:28:08 -07:00
|
|
|
logger.info(`Zigbee publish to '${deviceID}', ${cid} - ${cmd} - ${JSON.stringify(zclData)} - ${ep}`);
|
|
|
|
device.functional(cid, cmd, zclData, callback);
|
|
|
|
}
|
2018-04-18 12:57:39 -07:00
|
|
|
|
2018-05-30 13:28:08 -07:00
|
|
|
read(deviceID, cid, attr, ep, callback) {
|
|
|
|
const device = this._findDevice(deviceID, ep);
|
2018-04-18 09:25:40 -07:00
|
|
|
if (!device) {
|
2018-05-30 13:28:08 -07:00
|
|
|
logger.error(`Zigbee cannot read attribute from device because '${deviceID}' not known by zigbee-shepherd`);
|
2018-04-18 12:55:00 -07:00
|
|
|
return;
|
2018-04-18 09:25:40 -07:00
|
|
|
}
|
|
|
|
|
2018-05-30 13:28:08 -07:00
|
|
|
device.read(cid, attr, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
_findDevice(deviceID, ep) {
|
|
|
|
// Find device in zigbee-shepherd
|
|
|
|
let device = this.getDevice(deviceID);
|
|
|
|
if (!device || !device.epList || !device.epList.length) {
|
|
|
|
logger.error(`Zigbee cannot determine endpoint for '${deviceID}'`);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
ep = ep ? ep : device.epList[0];
|
|
|
|
device = this.shepherd.find(deviceID, ep);
|
|
|
|
return device;
|
2018-04-18 09:25:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Zigbee;
|