Refactor!

This commit is contained in:
Koenkk
2018-11-16 20:23:11 +01:00
parent b0d3c2f964
commit dd2147109a
21 changed files with 813 additions and 625 deletions
+29 -47
View File
@@ -6,32 +6,31 @@ const zclPacket = require('zcl-packet');
const utils = require('./util/utils');
const advancedSettings = settings.get().advanced;
const shepherdSettings = {
net: {
panId: advancedSettings && advancedSettings.pan_id ? advancedSettings.pan_id : 0x1a62,
channelList: [advancedSettings && advancedSettings.channel ? advancedSettings.channel : 11],
panId: advancedSettings.pan_id,
channelList: [advancedSettings.channel],
},
dbPath: data.joinPath('database.db'),
sp: {
baudRate: advancedSettings && advancedSettings.baudrate ? advancedSettings.baudrate : 115200,
rtscts: advancedSettings && (typeof(advancedSettings.rtscts) === 'boolean') ? advancedSettings.rtscts : true,
baudRate: advancedSettings.baudrate,
rtscts: advancedSettings.rtscts,
},
};
logger.debug(`Using zigbee-shepherd with settings: '${JSON.stringify(shepherdSettings)}'`);
class Zigbee {
constructor(onMessage) {
this.onMessage = onMessage;
this.handleReady = this.handleReady.bind(this);
this.handleMessage = this.handleMessage.bind(this);
this.handleError = this.handleError.bind(this);
constructor() {
this.onReady = this.onReady.bind(this);
this.onMessage = this.onMessage.bind(this);
this.onError = this.onError.bind(this);
this.messageHandler = null;
}
start(callback) {
start(messageHandler, callback) {
logger.info(`Starting zigbee-shepherd`);
this.messageHandler = messageHandler;
this.shepherd = new ZShepherd(settings.get().serial.port, shepherdSettings);
this.shepherd.start((error) => {
@@ -49,24 +48,24 @@ class Zigbee {
);
callback(error);
} else {
this._logStartupInfo();
this.logStartupInfo();
callback(null);
}
});
}, 60 * 1000);
}, utils.secondsToMilliseconds(60));
} else {
this._logStartupInfo();
this.logStartupInfo();
callback(null);
}
});
// Register callbacks.
this.shepherd.on('ready', this.handleReady);
this.shepherd.on('ind', this.handleMessage);
this.shepherd.on('error', this.handleError);
this.shepherd.on('ready', this.onReady);
this.shepherd.on('ind', this.onMessage);
this.shepherd.on('error', this.onError);
}
_logStartupInfo() {
logStartupInfo() {
logger.info('zigbee-shepherd started');
logger.info(`Coordinator firmware version: '${this.shepherd.info().firmware.revision}'`);
logger.debug(`zigbee-shepherd info: ${JSON.stringify(this.shepherd.info())}`);
@@ -83,22 +82,7 @@ class Zigbee {
});
}
handleReady() {
// Set all Xiaomi devices to be online, so shepherd won't try
// to query info from devices (which would fail because they go to sleep).
const devices = this.getAllClients();
devices.forEach((d) => {
if (utils.isXiaomiDevice(d)) {
const device = this.shepherd.find(d.ieeeAddr, 1);
if (device) {
device.getDevice().update({
status: 'online',
joinTime: Math.floor(Date.now() / 1000),
});
}
}
});
onReady() {
// Check if we have to turn off the led
if (settings.get().serial.disable_led) {
this.shepherd.controller.request('UTIL', 'ledControl', {ledid: 3, mode: 0});
@@ -107,7 +91,7 @@ class Zigbee {
logger.info('zigbee-shepherd ready');
}
handleError(message) {
onError(message) {
// This event may appear if zigbee-shepherd cannot decode bad packets (invalid checksum).
logger.error(message);
}
@@ -134,14 +118,14 @@ class Zigbee {
this.shepherd.remove(deviceID, (error) => {
if (error) {
logger.warn(`Failed to remove '${deviceID}', trying force remove...`);
this._forceRemove(deviceID, callback);
this.forceRemove(deviceID, callback);
} else {
callback(null);
}
});
}
_forceRemove(deviceID, callback) {
forceRemove(deviceID, callback) {
const device = this.shepherd._findDevByAddr(deviceID);
if (device) {
@@ -167,9 +151,9 @@ class Zigbee {
}
}
handleMessage(message) {
if (this.onMessage) {
this.onMessage(message);
onMessage(message) {
if (this.messageHandler) {
this.messageHandler(message);
}
}
@@ -187,7 +171,7 @@ class Zigbee {
}
publish(ieeAddr, cid, cmd, cmdType, zclData, cfg, ep, callback) {
const device = this._findDevice(ieeAddr, ep);
const device = this.findDevice(ieeAddr, ep);
if (!device) {
logger.error(`Zigbee cannot publish message to device because '${ieeAddr}' not known by zigbee-shepherd`);
return;
@@ -207,8 +191,6 @@ class Zigbee {
}
callback(error, rsp);
// TODO: Send read reponse back to MQTT.
};
if (cmdType === 'functional') {
@@ -229,7 +211,7 @@ class Zigbee {
}
registerOnAfIncomingMsg(ieeeAddr, ep) {
const device = this._findDevice(ieeeAddr, ep);
const device = this.findDevice(ieeeAddr, ep);
device.onAfIncomingMsg = (message) => {
// Parse the message
zclPacket.parse(message.data, message.clusterid, (error, zclData) => {
@@ -238,12 +220,12 @@ class Zigbee {
data: zclData,
};
this.handleMessage(message);
this.onMessage(message);
});
};
}
_findDevice(deviceID, ep) {
findDevice(deviceID, ep) {
// Find device in zigbee-shepherd
let device = this.getDevice(deviceID);
if (!device || !device.epList || !device.epList.length) {