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-10-02 12:15:12 -07:00
|
|
|
const utils = require('./util/utils');
|
2019-02-23 07:18:41 -07:00
|
|
|
const ZigbeeQueue = require('./util/zigbeeQueue');
|
2018-12-04 12:09:52 -07:00
|
|
|
const cieApp = require('./zapp/cie');
|
2019-03-02 06:04:52 -07:00
|
|
|
const objectAssignDeep = require('object-assign-deep');
|
2019-02-01 11:04:49 -07:00
|
|
|
const zclId = require('zcl-id');
|
2018-04-18 09:25:40 -07:00
|
|
|
|
2018-05-24 11:19:04 -07:00
|
|
|
const advancedSettings = settings.get().advanced;
|
2019-02-13 12:06:14 -07:00
|
|
|
|
|
|
|
if (advancedSettings.channel < 11 || advancedSettings.channel > 26) {
|
|
|
|
throw new Error(`'${advancedSettings.channel}' is an invalid channel, use a channel between 11 - 26.`);
|
|
|
|
}
|
|
|
|
|
2018-04-18 09:25:40 -07:00
|
|
|
const shepherdSettings = {
|
2018-05-17 00:52:28 -07:00
|
|
|
net: {
|
2018-11-16 12:23:11 -07:00
|
|
|
panId: advancedSettings.pan_id,
|
2019-02-02 12:09:20 -07:00
|
|
|
extPanId: advancedSettings.ext_pan_id,
|
2018-11-16 12:23:11 -07:00
|
|
|
channelList: [advancedSettings.channel],
|
2018-12-24 08:29:06 -07:00
|
|
|
precfgkey: settings.get().advanced.network_key,
|
2018-05-17 00:52:28 -07:00
|
|
|
},
|
2018-05-17 08:48:41 -07:00
|
|
|
dbPath: data.joinPath('database.db'),
|
2019-04-14 07:27:02 -07:00
|
|
|
coordBackupPath: data.joinPath('coordinator_backup.json'),
|
2018-06-14 12:37:19 -07:00
|
|
|
sp: {
|
2018-11-16 12:23:11 -07:00
|
|
|
baudRate: advancedSettings.baudrate,
|
|
|
|
rtscts: advancedSettings.rtscts,
|
2018-06-14 12:37:19 -07:00
|
|
|
},
|
2018-04-18 09:25:40 -07:00
|
|
|
};
|
|
|
|
|
2018-12-21 16:07:53 -07:00
|
|
|
const defaultCfg = {
|
|
|
|
manufSpec: 0,
|
|
|
|
disDefaultRsp: 0,
|
|
|
|
};
|
|
|
|
|
2019-03-02 06:04:52 -07:00
|
|
|
// Don't print network key.
|
|
|
|
const shepherdSettingsLog = objectAssignDeep.noMutate(shepherdSettings);
|
|
|
|
shepherdSettingsLog.net.precfgkey = 'HIDDEN';
|
|
|
|
logger.debug(`Using zigbee-shepherd with settings: '${JSON.stringify(shepherdSettingsLog)}'`);
|
2018-06-14 12:37:19 -07:00
|
|
|
|
2018-04-18 09:25:40 -07:00
|
|
|
class Zigbee {
|
2018-11-16 12:23:11 -07:00
|
|
|
constructor() {
|
|
|
|
this.onReady = this.onReady.bind(this);
|
|
|
|
this.onMessage = this.onMessage.bind(this);
|
|
|
|
this.onError = this.onError.bind(this);
|
|
|
|
this.messageHandler = null;
|
2019-05-07 12:00:17 -07:00
|
|
|
this.permitJoinTimer = null;
|
2019-02-01 11:04:49 -07:00
|
|
|
|
2019-02-23 07:18:41 -07:00
|
|
|
this.queue = new ZigbeeQueue();
|
2018-04-18 09:25:40 -07:00
|
|
|
}
|
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
start(messageHandler, callback) {
|
2018-04-18 09:25:40 -07:00
|
|
|
logger.info(`Starting zigbee-shepherd`);
|
2018-11-16 12:23:11 -07:00
|
|
|
this.messageHandler = messageHandler;
|
2018-04-18 09:25:40 -07:00
|
|
|
this.shepherd = new ZShepherd(settings.get().serial.port, shepherdSettings);
|
|
|
|
|
|
|
|
this.shepherd.start((error) => {
|
|
|
|
if (error) {
|
2019-04-14 07:27:02 -07:00
|
|
|
logger.info(`Error while starting zigbee-shepherd, attempting to fix... (takes 60 seconds) (${error})`);
|
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) {
|
2019-04-14 07:27:02 -07:00
|
|
|
logger.error(`Error while starting zigbee-shepherd! (${error})`);
|
2018-06-28 10:49:16 -07:00
|
|
|
logger.error(
|
|
|
|
'Press the reset button on the stick (the one closest to the USB) and start again'
|
|
|
|
);
|
2018-06-01 12:04:48 -07:00
|
|
|
callback(error);
|
|
|
|
} else {
|
2019-03-26 13:34:58 -07:00
|
|
|
this._handleStarted();
|
2018-06-01 12:04:48 -07:00
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
});
|
2018-11-16 12:23:11 -07:00
|
|
|
}, utils.secondsToMilliseconds(60));
|
2018-04-18 09:25:40 -07:00
|
|
|
} else {
|
2019-03-26 13:34:58 -07:00
|
|
|
this._handleStarted();
|
2018-06-01 12:04:48 -07:00
|
|
|
callback(null);
|
2018-04-18 09:25:40 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Register callbacks.
|
2018-11-16 12:23:11 -07:00
|
|
|
this.shepherd.on('ready', this.onReady);
|
|
|
|
this.shepherd.on('ind', this.onMessage);
|
|
|
|
this.shepherd.on('error', this.onError);
|
2019-03-26 13:34:58 -07:00
|
|
|
this._acceptDevIncoming = this._acceptDevIncoming.bind(this);
|
|
|
|
this.shepherd.acceptDevIncoming = this._acceptDevIncoming;
|
|
|
|
}
|
|
|
|
|
|
|
|
_handleStarted() {
|
|
|
|
this.logStartupInfo();
|
|
|
|
|
|
|
|
this.getAllClients().forEach((device) => {
|
|
|
|
if (settings.get().ban.includes(device.ieeeAddr)) {
|
2019-05-14 08:51:43 -07:00
|
|
|
logger.warn(`Banned device is connected (${device.ieeeAddr}), removing...`);
|
2019-03-26 13:34:58 -07:00
|
|
|
this.removeDevice(device.ieeeAddr, false, () => {});
|
|
|
|
}
|
|
|
|
});
|
2019-04-14 09:53:34 -07:00
|
|
|
|
|
|
|
this.shepherd.backupCoordinator(() => {});
|
2019-03-26 13:34:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
_acceptDevIncoming(devInfo, callback) {
|
2019-03-30 08:48:58 -07:00
|
|
|
logger.debug(
|
|
|
|
`Accept device incoming with ieeeAddr '${devInfo.ieeeAddr}' permit join is '${this.getPermitJoin()}'`
|
|
|
|
);
|
|
|
|
|
2019-04-01 11:30:27 -07:00
|
|
|
if (settings.get().ban.includes(devInfo.ieeeAddr)) {
|
|
|
|
logger.info(`Banned device tried to connect (${devInfo.ieeeAddr})`);
|
|
|
|
callback(null, false);
|
2019-03-26 13:34:58 -07:00
|
|
|
} else {
|
2019-04-01 11:30:27 -07:00
|
|
|
logger.debug(`Allowing device '${devInfo.ieeeAddr}' to join`);
|
|
|
|
callback(null, true);
|
2019-03-26 13:34:58 -07:00
|
|
|
}
|
2018-04-18 09:25:40 -07:00
|
|
|
}
|
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
logStartupInfo() {
|
2018-07-05 11:06:38 -07:00
|
|
|
logger.info('zigbee-shepherd started');
|
2019-03-17 12:04:51 -07:00
|
|
|
logger.info(`Coordinator firmware version: '${this.getFirmwareVersion()}'`);
|
2018-07-05 11:06:38 -07:00
|
|
|
logger.debug(`zigbee-shepherd info: ${JSON.stringify(this.shepherd.info())}`);
|
|
|
|
}
|
|
|
|
|
2019-03-17 12:04:51 -07:00
|
|
|
getFirmwareVersion() {
|
|
|
|
return this.shepherd.info().firmware.revision;
|
|
|
|
}
|
|
|
|
|
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) {
|
2019-05-07 12:00:17 -07:00
|
|
|
if (this.permitJoinTimer) {
|
|
|
|
clearInterval(this.permitJoinTimer);
|
|
|
|
}
|
|
|
|
|
2019-02-01 16:57:51 -07:00
|
|
|
this.queue.stop();
|
|
|
|
|
2019-04-14 07:27:02 -07:00
|
|
|
// Backup coordinator
|
|
|
|
this.shepherd.backupCoordinator(() => {
|
|
|
|
this.shepherd.stop((error) => {
|
|
|
|
logger.info('zigbee-shepherd stopped');
|
|
|
|
callback(error);
|
|
|
|
});
|
2018-04-18 09:25:40 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
onReady() {
|
2018-12-04 12:09:52 -07:00
|
|
|
// Mount cieApp
|
|
|
|
this.shepherd.mount(cieApp, (err, epId) => {
|
|
|
|
if (!err) {
|
2019-01-07 10:18:14 -07:00
|
|
|
logger.debug(`Mounted the cieApp (epId ${epId})`);
|
2018-12-04 12:09:52 -07:00
|
|
|
} else {
|
|
|
|
logger.error(`Failed to mount the cieApp`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-10-02 12:15:12 -07:00
|
|
|
// Check if we have to turn off the led
|
2018-07-21 12:13:28 -07:00
|
|
|
if (settings.get().serial.disable_led) {
|
|
|
|
this.shepherd.controller.request('UTIL', 'ledControl', {ledid: 3, mode: 0});
|
|
|
|
}
|
|
|
|
|
2019-02-13 12:43:22 -07:00
|
|
|
// Wait some time before we start the queue, many calls skip this queue which hangs the stick
|
|
|
|
setTimeout(() => {
|
|
|
|
this.queue.start();
|
|
|
|
}, 2000);
|
|
|
|
|
|
|
|
logger.info('zigbee-shepherd ready');
|
|
|
|
}
|
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
onError(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-12-01 07:30:59 -07:00
|
|
|
permitJoin(permit, callback) {
|
2018-04-24 10:30:56 -07:00
|
|
|
if (permit) {
|
2018-09-23 02:16:45 -07:00
|
|
|
logger.info('Zigbee: allowing new devices to join.');
|
2018-04-24 10:30:56 -07:00
|
|
|
} else {
|
2018-09-23 02:16:45 -07:00
|
|
|
logger.info('Zigbee: disabling joining new devices.');
|
2018-04-18 09:25:40 -07:00
|
|
|
}
|
|
|
|
|
2019-05-07 12:00:17 -07:00
|
|
|
// In zigbee 3.0 a network automatically closes after 254 seconds.
|
|
|
|
// As a workaround, we enable joining again.
|
|
|
|
if (this.permitJoinTimer) {
|
|
|
|
clearInterval(this.permitJoinTimer);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (permit) {
|
|
|
|
this.permitJoinTimer = setInterval(() => {
|
|
|
|
this.shepherd.permitJoin(255, (error) => {
|
|
|
|
if (error) {
|
|
|
|
logger.error('Failed to reenable joining');
|
|
|
|
} else {
|
2019-05-14 08:51:43 -07:00
|
|
|
logger.info('Successfully reenabled joining');
|
2019-05-07 12:00:17 -07:00
|
|
|
}
|
|
|
|
});
|
2019-05-19 10:10:51 -07:00
|
|
|
}, utils.secondsToMilliseconds(160));
|
2019-05-07 12:00:17 -07:00
|
|
|
}
|
|
|
|
|
2018-09-23 02:16:45 -07:00
|
|
|
this.shepherd.permitJoin(permit ? 255 : 0, (error) => {
|
2018-04-18 09:25:40 -07:00
|
|
|
if (error) {
|
|
|
|
logger.info(error);
|
|
|
|
}
|
2018-12-01 07:30:59 -07:00
|
|
|
|
|
|
|
if (callback) {
|
|
|
|
callback();
|
|
|
|
}
|
2018-04-18 09:25:40 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-12-01 07:30:59 -07:00
|
|
|
getPermitJoin() {
|
|
|
|
return this.shepherd.controller._permitJoinTime === 255;
|
|
|
|
}
|
|
|
|
|
2018-04-23 09:17:47 -07:00
|
|
|
getAllClients() {
|
2018-10-07 12:46:54 -07:00
|
|
|
return this.getDevices().filter((device) => device.type !== 'Coordinator');
|
2018-04-23 09:17:47 -07:00
|
|
|
}
|
|
|
|
|
2019-01-08 11:00:02 -07:00
|
|
|
removeDevice(deviceID, ban, callback) {
|
2019-03-26 13:34:58 -07:00
|
|
|
if (ban) {
|
|
|
|
settings.banDevice(deviceID);
|
|
|
|
}
|
|
|
|
|
2019-05-14 08:51:43 -07:00
|
|
|
const friendlyName = this.getDeviceFriendlyName(deviceID);
|
|
|
|
|
2019-03-26 13:34:58 -07:00
|
|
|
this.shepherd.remove(deviceID, {reJoin: true}, (error) => {
|
2018-06-09 03:27:04 -07:00
|
|
|
if (error) {
|
2019-05-14 08:51:43 -07:00
|
|
|
logger.warn(`Failed to remove '${friendlyName}', trying force remove...`);
|
2018-11-16 12:23:11 -07:00
|
|
|
this.forceRemove(deviceID, callback);
|
2018-06-06 12:13:32 -07:00
|
|
|
} else {
|
2019-05-14 08:51:43 -07:00
|
|
|
logger.info(`Removed ${friendlyName}`);
|
2018-06-09 03:27:04 -07:00
|
|
|
callback(null);
|
2018-06-06 12:13:32 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
forceRemove(deviceID, callback) {
|
2018-06-06 12:13:32 -07:00
|
|
|
const device = this.shepherd._findDevByAddr(deviceID);
|
2018-06-09 03:27:04 -07:00
|
|
|
|
2018-06-07 10:41:11 -07:00
|
|
|
if (device) {
|
2019-05-14 08:51:43 -07:00
|
|
|
const friendlyName = this.getDeviceFriendlyName(deviceID);
|
2019-03-26 13:34:58 -07:00
|
|
|
return this.shepherd._unregisterDev(device, (error) => {
|
2019-05-14 08:51:43 -07:00
|
|
|
logger.info(`Force removed ${friendlyName}`);
|
2019-03-26 13:34:58 -07:00
|
|
|
callback(error);
|
|
|
|
});
|
2018-06-07 10:41:11 -07:00
|
|
|
} else {
|
|
|
|
logger.warn(`Could not find ${deviceID} for force removal`);
|
2018-06-09 03:27:04 -07:00
|
|
|
callback(true);
|
2018-06-07 10:41:11 -07:00
|
|
|
}
|
2018-06-04 01:55:00 -07:00
|
|
|
}
|
2018-06-06 12:19:50 -07:00
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
onMessage(message) {
|
|
|
|
if (this.messageHandler) {
|
|
|
|
this.messageHandler(message);
|
2018-04-18 09:25:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-07 12:46:54 -07:00
|
|
|
getDevices() {
|
|
|
|
return this.shepherd.list();
|
|
|
|
}
|
|
|
|
|
2018-10-23 11:39:48 -07:00
|
|
|
getDevice(ieeeAddr) {
|
|
|
|
return this.getDevices().find((d) => d.ieeeAddr === ieeeAddr);
|
2018-04-27 14:58:46 -07:00
|
|
|
}
|
|
|
|
|
2019-05-14 08:51:43 -07:00
|
|
|
getDeviceFriendlyName(ieeeAddr) {
|
2019-05-14 10:25:13 -07:00
|
|
|
const device = settings.getDevice(ieeeAddr);
|
|
|
|
return device ? device.friendly_name || ieeeAddr : ieeeAddr;
|
2019-05-14 08:51:43 -07:00
|
|
|
}
|
|
|
|
|
2018-06-04 12:36:51 -07:00
|
|
|
getCoordinator() {
|
2018-10-07 12:46:54 -07:00
|
|
|
const device = this.getDevices().find((d) => d.type === 'Coordinator');
|
2018-06-04 12:36:51 -07:00
|
|
|
return this.shepherd.find(device.ieeeAddr, 1);
|
|
|
|
}
|
|
|
|
|
2019-05-19 10:33:30 -07:00
|
|
|
getScanable() {
|
|
|
|
return this.getDevices().filter((d) => d.type != 'EndDevice');
|
|
|
|
}
|
|
|
|
|
2018-12-21 16:07:53 -07:00
|
|
|
getGroup(ID) {
|
|
|
|
return this.shepherd.getGroup(ID);
|
|
|
|
}
|
|
|
|
|
2019-05-14 08:51:43 -07:00
|
|
|
getGroupFriendlyName(ID) {
|
|
|
|
let friendlyName = null;
|
|
|
|
friendlyName = settings.getGroup(ID).friendly_name;
|
|
|
|
return (friendlyName ? friendlyName : ID);
|
|
|
|
}
|
|
|
|
|
2018-08-28 12:55:00 -07:00
|
|
|
networkScan(callback) {
|
|
|
|
logger.info('Starting network scan...');
|
2019-05-19 10:33:30 -07:00
|
|
|
|
2019-05-21 12:04:14 -07:00
|
|
|
Promise.delay = function(t, val) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
setTimeout(resolve.bind(null, val), t);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Promise.raceAll = function(promises, timeoutTime, timeoutVal) {
|
|
|
|
return Promise.all(promises.map((p) => {
|
|
|
|
return Promise.race([p, Promise.delay(timeoutTime, timeoutVal)]);
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
2019-06-02 01:05:44 -07:00
|
|
|
const processResponse = function(parent) {
|
2019-05-23 09:53:47 -07:00
|
|
|
logger.debug(`Scanning device: '${parent}'`);
|
2019-05-19 10:33:30 -07:00
|
|
|
return function(data) {
|
|
|
|
const linkSet = [];
|
|
|
|
return new Promise((resolve) => {
|
2019-05-24 09:21:53 -07:00
|
|
|
logger.debug(`Processing scan for: '${parent}'`);
|
|
|
|
if (data) {
|
|
|
|
data.forEach(function(devinfo) {
|
|
|
|
devinfo.parent = parent;
|
|
|
|
linkSet.push(devinfo);
|
|
|
|
});
|
|
|
|
}
|
2019-05-19 10:33:30 -07:00
|
|
|
resolve(linkSet);
|
2019-05-23 09:53:47 -07:00
|
|
|
logger.debug(`Processed device: '${parent}', linkSet: %j`, linkSet);
|
2019-05-19 10:33:30 -07:00
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const allScans = this.getScanable().map((dev) => {
|
2019-05-23 09:53:47 -07:00
|
|
|
logger.debug(`Preparing asynch network scan for '${dev.ieeeAddr}'`);
|
2019-06-02 01:05:44 -07:00
|
|
|
// Delay the start of each device scan by a random time to avoid network congestion
|
|
|
|
return Promise.delay(Math.random()*3000, this.shepherd.lqi(dev.ieeeAddr))
|
|
|
|
.then(processResponse(dev.ieeeAddr))
|
2019-05-24 09:21:53 -07:00
|
|
|
.catch(() => {
|
|
|
|
return new Promise((resolve) => []);
|
|
|
|
});
|
2019-05-19 10:33:30 -07:00
|
|
|
}, this);
|
2019-05-23 09:53:47 -07:00
|
|
|
logger.debug('All network map promises created');
|
2019-05-24 09:21:53 -07:00
|
|
|
// Collect all lqi scan results but timeout after specified miliseconds if any haven't completed
|
|
|
|
Promise.raceAll(allScans, 8000, []).then((linkSets) => {
|
2019-06-02 01:05:44 -07:00
|
|
|
// Assemble the individual scan results then sort by ieeeAddr to generate consistent maps
|
|
|
|
const linkMap = [].concat(...linkSets)
|
|
|
|
.sort((a, b) => ((a.parent + '|' + a.ieeeAddr) > (b.parent + '|' + b.ieeeAddr)) ? 1
|
|
|
|
: (((b.parent + '|' + b.ieeeAddr) > (a.parent + '|' + a.ieeeAddr)) ? -1 : 0));
|
2018-08-28 12:55:00 -07:00
|
|
|
logger.info('Network scan completed');
|
2019-05-23 09:53:47 -07:00
|
|
|
logger.debug(`Link map: %j`, linkMap);
|
2019-05-19 10:33:30 -07:00
|
|
|
callback(linkMap);
|
|
|
|
})
|
2019-05-21 12:04:14 -07:00
|
|
|
.catch(function(result) {
|
2019-05-23 09:53:47 -07:00
|
|
|
logger.info(`Network scan failed: '${result}'`);
|
2019-05-19 10:33:30 -07:00
|
|
|
callback([]);
|
|
|
|
});
|
2018-08-28 12:55:00 -07:00
|
|
|
}
|
|
|
|
|
2019-02-01 11:04:49 -07:00
|
|
|
getEndpoint(ieeeAddr, ep) {
|
|
|
|
// If no ep is given, the first endpoint will be returned
|
2018-05-30 13:28:08 -07:00
|
|
|
// Find device in zigbee-shepherd
|
2019-02-01 11:04:49 -07:00
|
|
|
const device = this.getDevice(ieeeAddr);
|
2018-05-30 13:28:08 -07:00
|
|
|
if (!device || !device.epList || !device.epList.length) {
|
2019-02-01 11:04:49 -07:00
|
|
|
logger.error(`Zigbee cannot determine endpoint for '${ieeeAddr}'`);
|
2018-05-30 13:28:08 -07:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
ep = ep ? ep : device.epList[0];
|
2019-02-01 11:04:49 -07:00
|
|
|
const endpoint = this.shepherd.find(ieeeAddr, ep);
|
|
|
|
return endpoint;
|
|
|
|
}
|
|
|
|
|
2019-02-01 17:41:05 -07:00
|
|
|
publish(entityID, entityType, cid, cmd, cmdType, zclData, cfg=defaultCfg, ep, callback) {
|
|
|
|
let entity = null;
|
2019-05-14 08:51:43 -07:00
|
|
|
let friendlyName = null;
|
2019-02-01 17:41:05 -07:00
|
|
|
if (entityType === 'device') {
|
|
|
|
entity = this.getEndpoint(entityID, ep);
|
2019-05-14 08:51:43 -07:00
|
|
|
friendlyName = this.getDeviceFriendlyName(entityID);
|
2019-02-01 17:41:05 -07:00
|
|
|
} else if (entityType === 'group') {
|
|
|
|
entity = this.getGroup(entityID);
|
2019-05-14 08:51:43 -07:00
|
|
|
friendlyName = this.getGroupFriendlyName(entityID);
|
2019-02-01 17:41:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!entity) {
|
|
|
|
logger.error(
|
|
|
|
`Cannot publish message to ${entityType} because '${entityID}' is not known by zigbee-shepherd`
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-23 07:18:41 -07:00
|
|
|
this.queue.push(entityID, (queueCallback) => {
|
2019-02-01 17:41:05 -07:00
|
|
|
logger.info(
|
2019-05-14 08:51:43 -07:00
|
|
|
`Zigbee publish to ${entityType} '${friendlyName}', ${cid} - ${cmd} - ` +
|
2019-02-01 17:41:05 -07:00
|
|
|
`${JSON.stringify(zclData)} - ${JSON.stringify(cfg)} - ${ep}`
|
|
|
|
);
|
|
|
|
|
|
|
|
const callback_ = (error, rsp) => {
|
|
|
|
if (error) {
|
|
|
|
logger.error(
|
2019-05-14 08:51:43 -07:00
|
|
|
`Zigbee publish to ${entityType} '${friendlyName}', ${cid} ` +
|
|
|
|
`- ${cmd} - ${JSON.stringify(zclData)} ` +
|
2019-02-01 17:41:05 -07:00
|
|
|
`- ${JSON.stringify(cfg)} - ${ep} ` +
|
|
|
|
`failed with error ${error}`);
|
|
|
|
}
|
|
|
|
|
2019-02-01 18:14:31 -07:00
|
|
|
if (callback) {
|
|
|
|
callback(error, rsp);
|
|
|
|
}
|
2019-02-23 07:18:41 -07:00
|
|
|
|
|
|
|
queueCallback(error);
|
2019-02-01 17:41:05 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
if (cmdType === 'functional' && entity.functional) {
|
|
|
|
entity.functional(cid, cmd, zclData, cfg, callback_);
|
|
|
|
} else if (cmdType === 'foundation' && entity.foundation) {
|
|
|
|
entity.foundation(cid, cmd, zclData, cfg, callback_);
|
|
|
|
} else {
|
|
|
|
logger.error(`Unknown zigbee publish cmdType ${cmdType}`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-23 09:45:28 -07:00
|
|
|
ping(ieeeAddr, errorLogLevel='error', cb, mechanism='default') {
|
2019-05-14 08:51:43 -07:00
|
|
|
const friendlyName = this.getDeviceFriendlyName(ieeeAddr);
|
2019-03-23 09:45:28 -07:00
|
|
|
const callback = (error) => {
|
|
|
|
if (error) {
|
2019-05-14 08:51:43 -07:00
|
|
|
logger[errorLogLevel](`Failed to ping '${friendlyName}'`);
|
2019-03-23 09:45:28 -07:00
|
|
|
} else {
|
2019-05-14 08:51:43 -07:00
|
|
|
logger.debug(`Successfully pinged '${friendlyName}'`);
|
2019-03-23 09:45:28 -07:00
|
|
|
}
|
2019-02-01 16:57:51 -07:00
|
|
|
|
2019-03-23 09:45:28 -07:00
|
|
|
if (cb) {
|
|
|
|
cb(error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (mechanism === 'default') {
|
|
|
|
const device = this.shepherd._findDevByAddr(ieeeAddr);
|
|
|
|
if (device) {
|
|
|
|
logger.debug(`Ping ${ieeeAddr} (default)`);
|
|
|
|
this.queue.push(ieeeAddr, (queueCallback) => {
|
|
|
|
this.shepherd.controller.checkOnline(device, (error) => {
|
|
|
|
callback(error);
|
|
|
|
queueCallback(error);
|
|
|
|
});
|
2019-02-23 07:18:41 -07:00
|
|
|
});
|
2019-03-23 09:45:28 -07:00
|
|
|
}
|
|
|
|
} else if (mechanism === 'basic') {
|
|
|
|
const endpoint = this.getEndpoint(ieeeAddr, null);
|
|
|
|
if (endpoint) {
|
|
|
|
logger.debug(`Ping ${ieeeAddr} (basic)`);
|
|
|
|
this.queue.push(ieeeAddr, (queueCallback) => {
|
|
|
|
endpoint.foundation('genBasic', 'read', [{attrId: 0}], (error) => {
|
|
|
|
callback(error);
|
|
|
|
queueCallback(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2019-02-01 16:57:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-15 13:19:42 -07:00
|
|
|
bind(ep, cluster, target, callback) {
|
2019-05-14 08:51:43 -07:00
|
|
|
const friendlyName = this.getDeviceFriendlyName(ep.device.ieeeAddr);
|
|
|
|
const log = ` '${friendlyName}' - ${cluster}`;
|
2019-03-15 13:19:42 -07:00
|
|
|
target = !target ? this.getCoordinator() : target;
|
2019-02-01 11:43:22 -07:00
|
|
|
|
2019-02-23 07:18:41 -07:00
|
|
|
this.queue.push(ep.device.ieeeAddr, (queueCallback) => {
|
2019-03-15 13:19:42 -07:00
|
|
|
logger.debug(`Binding ${log}`);
|
2019-02-01 16:31:30 -07:00
|
|
|
ep.bind(cluster, target, (error) => {
|
2019-02-01 11:43:22 -07:00
|
|
|
if (error) {
|
2019-03-15 13:19:42 -07:00
|
|
|
logger.error(`Failed to bind ${log} - (${error})`);
|
2019-02-01 11:43:22 -07:00
|
|
|
} else {
|
2019-03-15 13:19:42 -07:00
|
|
|
logger.debug(`Successfully bound ${log}`);
|
2019-02-01 11:43:22 -07:00
|
|
|
}
|
2019-02-22 13:06:24 -07:00
|
|
|
|
2019-03-15 13:19:42 -07:00
|
|
|
callback(error);
|
|
|
|
queueCallback(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
unbind(ep, cluster, target, callback) {
|
2019-05-14 08:51:43 -07:00
|
|
|
const friendlyName = this.getDeviceFriendlyName(ep.device.ieeeAddr);
|
|
|
|
const log = ` '${friendlyName}' - ${cluster}`;
|
2019-03-15 13:19:42 -07:00
|
|
|
target = !target ? this.getCoordinator() : target;
|
|
|
|
|
|
|
|
this.queue.push(ep.device.ieeeAddr, (queueCallback) => {
|
|
|
|
logger.debug(`Unbinding ${log}`);
|
|
|
|
ep.unbind(cluster, target, (error) => {
|
|
|
|
if (error) {
|
|
|
|
logger.error(`Failed to unbind ${log} - (${error})`);
|
|
|
|
} else {
|
|
|
|
logger.debug(`Successfully unbound ${log}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(error);
|
2019-02-23 07:18:41 -07:00
|
|
|
queueCallback(error);
|
|
|
|
});
|
2019-02-01 11:43:22 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-23 07:39:24 -07:00
|
|
|
/*
|
|
|
|
* Setup reporting.
|
|
|
|
* Attributes is an array of attribute objects.
|
|
|
|
* each attribute object should contain the following properties:
|
2019-02-22 13:06:24 -07:00
|
|
|
* attr the attribute name,
|
|
|
|
* min the minimal time between reports in seconds,
|
|
|
|
* max the maximum time between reports in seconds,
|
|
|
|
* change the minimum amount of change before sending a report
|
|
|
|
*/
|
|
|
|
report(ep, cluster, attributes) {
|
2019-05-14 08:51:43 -07:00
|
|
|
const friendlyName = this.getDeviceFriendlyName(ep.device.ieeeAddr);
|
2019-02-26 12:21:35 -07:00
|
|
|
const cfgArr = attributes.map((attribute) => {
|
2019-02-22 13:06:24 -07:00
|
|
|
const attrId = zclId.attr(cluster, attribute.attr).value;
|
|
|
|
const dataType = zclId.attrType(cluster, attribute.attr).value;
|
2019-02-26 12:21:35 -07:00
|
|
|
return {
|
2019-02-22 13:06:24 -07:00
|
|
|
direction: 0,
|
|
|
|
attrId,
|
|
|
|
dataType,
|
|
|
|
minRepIntval: attribute.min,
|
|
|
|
maxRepIntval: attribute.max,
|
|
|
|
repChange: attribute.change,
|
2019-02-26 12:21:35 -07:00
|
|
|
};
|
|
|
|
});
|
2019-02-23 07:39:24 -07:00
|
|
|
|
2019-05-14 08:51:43 -07:00
|
|
|
const log=`for '${friendlyName}' - ${cluster} - ${attributes.length}`;
|
2019-02-01 11:04:49 -07:00
|
|
|
|
2019-02-23 07:39:24 -07:00
|
|
|
const configReport = () => {
|
|
|
|
this.queue.push(ep.device.ieeeAddr, (queueCallback) => {
|
|
|
|
ep.foundation(cluster, 'configReport', cfgArr, defaultCfg, (error) => {
|
|
|
|
if (error) {
|
|
|
|
logger.error(`Failed to setup reporting ${log} - (${error})`);
|
|
|
|
} else {
|
|
|
|
logger.debug(`Successfully setup reporting ${log}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
queueCallback(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
this.queue.push(ep.device.ieeeAddr, (queueCallback) => {
|
2019-02-01 11:04:49 -07:00
|
|
|
logger.debug(`Setup reporting ${log}`);
|
2019-02-23 07:39:24 -07:00
|
|
|
|
2019-02-22 13:06:24 -07:00
|
|
|
ep.bind(cluster, this.getCoordinator(), (error) => {
|
2019-02-01 11:04:49 -07:00
|
|
|
if (error) {
|
2019-02-22 13:06:24 -07:00
|
|
|
logger.error(`Failed to bind for reporting ${log} - (${error})`);
|
2019-02-01 11:04:49 -07:00
|
|
|
} else {
|
2019-02-23 07:39:24 -07:00
|
|
|
// Only if binding succeeds, setting-up reporting makes sense.
|
|
|
|
configReport();
|
2019-02-01 11:04:49 -07:00
|
|
|
}
|
2019-02-23 07:39:24 -07:00
|
|
|
|
|
|
|
queueCallback(error);
|
2019-02-22 10:58:50 -07:00
|
|
|
});
|
2019-02-01 11:04:49 -07:00
|
|
|
});
|
2018-04-18 09:25:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-23 07:39:24 -07:00
|
|
|
module.exports = Zigbee;
|