Move logging devices on startup to controller.js. #45

This commit is contained in:
Koenkk 2018-05-16 19:29:47 +02:00
parent e3c9448656
commit ca40cf622a
2 changed files with 24 additions and 23 deletions

View File

@ -27,6 +27,12 @@ class Controller {
if (error) {
logger.error('Failed to start');
} else {
// Log zigbee clients on startup.
const devices = this.zigbee.getAllClients();
logger.info(`Currently ${devices.length} devices are joined:`);
devices.forEach((device) => logger.info(this.getDeviceStartupLogMessage(device)));
// Connect to MQTT broker
const subscriptions = [
`${settings.get().mqtt.base_topic}/+/set`,
`${settings.get().mqtt.base_topic}/+/+/set`,
@ -62,6 +68,21 @@ class Controller {
this.zigbee.stop(callback);
}
getDeviceStartupLogMessage(device) {
let friendlyName = 'unknown';
let friendlyDevice = {model: 'unkown', description: 'unknown'};
if (deviceMapping[device.modelId]) {
friendlyDevice = deviceMapping[device.modelId];
}
if (settings.getDevice(device.ieeeAddr)) {
friendlyName = settings.getDevice(device.ieeeAddr).friendly_name
}
return `${friendlyName} (${device.ieeeAddr}): ${friendlyDevice.model} - ${friendlyDevice.vendor} ${friendlyDevice.description}`;
}
handleZigbeeMessage(message) {
debug('Recieved zigbee message with data', message.data);

View File

@ -1,7 +1,6 @@
const ZShepherd = require('zigbee-shepherd');
const logger = require('./util/logger');
const settings = require('./util/settings');
const deviceMapping = require('./devices');
const data = require('./util/data');
const shepherdSettings = {
@ -46,15 +45,9 @@ class Zigbee {
}
handleReady() {
logger.info('zigbee-shepherd ready');
const devices = this.getAllClients();
logger.info(`Currently ${devices.length} devices are joined:`);
devices.forEach((device) => logger.info(this.getDeviceLogMessage(device)));
// 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).
const devices = this.getAllClients();
devices.forEach((device) => {
if (device.manufId === 4151) {
this.shepherd.find(device.ieeeAddr, 1).getDevice().update({
@ -63,6 +56,8 @@ class Zigbee {
});
}
});
logger.info('zigbee-shepherd ready');
}
permitJoin(permit) {
@ -89,21 +84,6 @@ class Zigbee {
}
}
getDeviceLogMessage(device) {
let friendlyName = 'unknown';
let friendlyDevice = {model: 'unkown', description: 'unknown'};
if (deviceMapping[device.modelId]) {
friendlyDevice = deviceMapping[device.modelId];
}
if (settings.getDevice(device.ieeeAddr)) {
friendlyName = settings.getDevice(device.ieeeAddr).friendly_name
}
return `${friendlyName} (${device.ieeeAddr}): ${friendlyDevice.model} - ${friendlyDevice.vendor} ${friendlyDevice.description}`;
}
getDevice(deviceID) {
return this.shepherd.list().find((d) => d.ieeeAddr === deviceID);
}