Skip to content

Commit

Permalink
Isolate legacy MQTT log. #3281
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed Apr 5, 2020
1 parent 9df1589 commit 552eec7
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 65 deletions.
82 changes: 61 additions & 21 deletions lib/extension/bridgeLegacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ class BridgeLegacy extends BaseExtension {
assert(entity, `Entity '${message}' does not exist`);
settings.whitelistDevice(entity.ID);
logger.info(`Whitelisted '${entity.friendlyName}'`);
this.mqtt.log('device_whitelisted', {friendly_name: entity.friendlyName});
this.mqtt.publish(
'bridge/log',
JSON.stringify({type: 'device_whitelisted', message: {friendly_name: entity.friendlyName}}),
);
} catch (error) {
logger.error(`Failed to whitelist '${message}' '${error}'`);
}
Expand Down Expand Up @@ -174,16 +177,18 @@ class BridgeLegacy extends BaseExtension {
if (topic.split('/').pop() == 'get') {
this.mqtt.publish(`bridge/config/devices`, JSON.stringify(devices), {});
} else {
this.mqtt.log('devices', devices);
this.mqtt.publish('bridge/log', JSON.stringify({type: 'devices', message: devices}));
}
}

groups(topic, message) {
this.mqtt.log('groups', settings.getGroups().map((g) => {
const payload = settings.getGroups().map((g) => {
const group = {...g};
delete group.friendlyName;
return group;
}));
});

this.mqtt.publish('bridge/log', JSON.stringify({type: 'groups', message: payload}));
}

rename(topic, message) {
Expand Down Expand Up @@ -223,7 +228,11 @@ class BridgeLegacy extends BaseExtension {
const entity = this.zigbee.resolveEntity(to);
const eventData = isGroup ? {group: entity.group} : {device: entity.device};
this.eventBus.emit(`${isGroup ? 'group' : 'device'}Renamed`, eventData);
this.mqtt.log(`${isGroup ? 'group' : 'device'}_renamed`, {from, to});

this.mqtt.publish(
'bridge/log',
JSON.stringify({type: `${isGroup ? 'group' : 'device'}_renamed`, message: {from, to}}),
);
} catch (error) {
logger.error(`Failed to rename - ${from} to ${to}`);
}
Expand Down Expand Up @@ -254,7 +263,7 @@ class BridgeLegacy extends BaseExtension {

const group = settings.addGroup(name, id);
this.zigbee.createGroup(group.ID);
this.mqtt.log('group_added', name);
this.mqtt.publish('bridge/log', JSON.stringify({type: `group_added`, message: name}));
logger.info(`Added group '${name}'`);
}

Expand All @@ -264,7 +273,7 @@ class BridgeLegacy extends BaseExtension {
assert(entity && entity.type === 'group', `Group '${message}' does not exist`);
settings.removeGroup(message);
entity.group.removeFromDatabase();
this.mqtt.log('group_removed', message);
this.mqtt.publish('bridge/log', JSON.stringify({type: `group_removed`, message}));
logger.info(`Removed group '${name}'`);
}

Expand All @@ -291,7 +300,7 @@ class BridgeLegacy extends BaseExtension {
if (!entity) {
logger.error(`Cannot ${lookup[action][2]}, device '${message}' does not exist`);

this.mqtt.log(`device_${lookup[action][0]}_failed`, message);
this.mqtt.publish('bridge/log', JSON.stringify({type: `device_${lookup[action][0]}_failed`, message}));
return;
}

Expand All @@ -306,7 +315,7 @@ class BridgeLegacy extends BaseExtension {
this.state.remove(entity.settings.ID);

logger.info(`Successfully ${lookup[action][0]} ${entity.settings.friendlyName}`);
this.mqtt.log(`device_${lookup[action][0]}`, message);
this.mqtt.publish('bridge/log', JSON.stringify({type: `device_${lookup[action][0]}`, message}));
};

try {
Expand All @@ -323,7 +332,7 @@ class BridgeLegacy extends BaseExtension {
// eslint-disable-next-line
logger.error(`See https://www.zigbee2mqtt.io/information/mqtt_topics_and_message_structure.html#zigbee2mqttbridgeconfigremove for more info`);

this.mqtt.log(`device_${lookup[action][0]}_failed`, message);
this.mqtt.publish('bridge/log', JSON.stringify({type: `device_${lookup[action][0]}_failed`, message}));
}

if (action === 'ban') {
Expand Down Expand Up @@ -374,47 +383,78 @@ class BridgeLegacy extends BaseExtension {
}

if (type === 'deviceJoined') {
this.mqtt.log('device_connected', {friendly_name: resolvedEntity.name});
this.mqtt.publish(
'bridge/log',
JSON.stringify({type: `device_connected`, message: {friendly_name: resolvedEntity.name}}),
);
} else if (type === 'deviceInterview') {
if (data.status === 'successful') {
if (resolvedEntity.definition) {
const {vendor, description, model} = resolvedEntity.definition;
const log = {friendly_name: resolvedEntity.name, model, vendor, description, supported: true};
this.mqtt.log('pairing', 'interview_successful', log);
this.mqtt.publish(
'bridge/log',
JSON.stringify({type: `pairing`, message: 'interview_successful', meta: log}),
);
} else {
this.mqtt.log('pairing', 'interview_successful',
{friendly_name: resolvedEntity.name, supported: false});
const meta = {friendly_name: resolvedEntity.name, supported: false};
this.mqtt.publish(
'bridge/log',
JSON.stringify({type: `pairing`, message: 'interview_successful', meta}),
);
}
} else if (data.status === 'failed') {
this.mqtt.log('pairing', 'interview_failed', {friendly_name: resolvedEntity.name});
const meta = {friendly_name: resolvedEntity.name};
this.mqtt.publish(
'bridge/log',
JSON.stringify({type: `pairing`, message: 'interview_failed', meta}),
);
} else {
/* istanbul ignore else */
if (data.status === 'started') {
this.mqtt.log('pairing', 'interview_started', {friendly_name: resolvedEntity.name});
const meta = {friendly_name: resolvedEntity.name};
this.mqtt.publish(
'bridge/log',
JSON.stringify({type: `pairing`, message: 'interview_started', meta}),
);
}
}
} else if (type === 'deviceAnnounce') {
this.mqtt.log('device_announced', 'announce', {friendly_name: resolvedEntity.name});
const meta = {friendly_name: resolvedEntity.name};
this.mqtt.publish('bridge/log', JSON.stringify({type: `device_announced`, message: 'announce', meta}));
} else {
/* istanbul ignore else */
if (type === 'deviceLeave') {
const name = resolvedEntity ? resolvedEntity.name : data.ieeeAddr;
this.mqtt.log('device_removed', 'left_network', {friendly_name: name});
const meta = {friendly_name: name};
this.mqtt.publish(
'bridge/log',
JSON.stringify({type: `device_removed`, message: 'left_network', meta}),
);
}
}
}

async touchlinkFactoryReset() {
logger.info('Starting touchlink factory reset...');
this.mqtt.log('touchlink', 'reset_started', {status: 'started'});
this.mqtt.publish(
'bridge/log',
JSON.stringify({type: `touchlink`, message: 'reset_started', meta: {status: 'started'}}),
);
const result = await this.zigbee.touchlinkFactoryReset();

if (result) {
logger.info('Successfully factory reset device through Touchlink');
this.mqtt.log('touchlink', 'reset_success', {status: 'success'});
this.mqtt.publish(
'bridge/log',
JSON.stringify({type: `touchlink`, message: 'reset_success', meta: {status: 'success'}}),
);
} else {
logger.warn('Failed to factory reset device through Touchlink');
this.mqtt.log('touchlink', 'reset_failed', {status: 'failed'});
this.mqtt.publish(
'bridge/log',
JSON.stringify({type: `touchlink`, message: 'reset_failed', meta: {status: 'failed'}}),
);
}
}
}
Expand Down
36 changes: 27 additions & 9 deletions lib/extension/deviceBind.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,44 @@ class DeviceBind extends BaseExtension {
`Successfully ${type === 'bind' ? 'bound' : 'unbound'} cluster '${cluster}' from ` +
`'${sourceName}' to '${targetName}'`,
);
this.mqtt.log(
`device_${type}`,
{from: sourceName, to: targetName, cluster},
);

/* istanbul ignore else */
if (settings.get().advanced.legacy_api) {
const message = {from: sourceName, to: targetName, cluster};
this.mqtt.publish(
'bridge/log',
JSON.stringify({type: `device_${type}`, message}),
);
}
} catch (error) {
logger.error(
`Failed to ${type} cluster '${cluster}' from '${sourceName}' to ` +
`'${targetName}' (${error})`,
);
this.mqtt.log(
`device_${type}_failed`,
{from: sourceName, to: targetName, cluster},
);

/* istanbul ignore else */
if (settings.get().advanced.legacy_api) {
const message = {from: sourceName, to: targetName, cluster};
this.mqtt.publish(
'bridge/log',
JSON.stringify({type: `device_${type}_failed`, message}),
);
}
}
}
}

if (!attemptedToBindSomething) {
logger.error(`Nothing to ${type} from '${sourceName}' to '${targetName}'`);
this.mqtt.log(`device_${type}_failed`, {from: sourceName, to: targetName});

/* istanbul ignore else */
if (settings.get().advanced.legacy_api) {
const message = {from: sourceName, to: targetName};
this.mqtt.publish(
'bridge/log',
JSON.stringify({type: `device_${type}_failed`, message}),
);
}
}
}
}
Expand Down
20 changes: 18 additions & 2 deletions lib/extension/entityPublish.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,15 @@ class EntityPublish extends BaseExtension {
const entity = this.zigbee.resolveEntity(entityName);

if (!entity) {
this.mqtt.log('entity_not_found', {friendly_name: entityName});
/* istanbul ignore else */
if (settings.get().advanced.legacy_api) {
const message = {friendly_name: entityName};
this.mqtt.publish(
'bridge/log',
JSON.stringify({type: `entity_not_found`, message}),
);
}

logger.error(`Entity '${entityName}' is unknown`);
return;
}
Expand Down Expand Up @@ -211,7 +219,15 @@ class EntityPublish extends BaseExtension {
`Publish '${topic.type}' '${key}' to '${entity.name}' failed: '${error}'`;
logger.error(message);
logger.debug(error.stack);
this.mqtt.log('zigbee_publish_error', message, {friendly_name: entity.name});

/* istanbul ignore else */
if (settings.get().advanced.legacy_api) {
const meta = {friendly_name: entity.name};
this.mqtt.publish(
'bridge/log',
JSON.stringify({type: `zigbee_publish_error`, message, meta}),
);
}
}

usedConverters.push(converter);
Expand Down
56 changes: 47 additions & 9 deletions lib/extension/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,16 @@ class Groups extends BaseExtension {

if (!group || group.type !== 'group') {
logger.error(`Group '${topicMatch[1]}' does not exist`);
this.mqtt.log(
`device_group_${type}_failed`,
{friendly_name: message, group: topicMatch[1], error: 'group doesn\'t exists'});

/* istanbul ignore else */
if (settings.get().advanced.legacy_api) {
const payload = {friendly_name: message, group: topicMatch[1], error: 'group doesn\'t exists'};
this.mqtt.publish(
'bridge/log',
JSON.stringify({type: `device_group_${type}_failed`, message: payload}),
);
}

return;
}
} else if (topic.match(topicRegexRemoveAll)) {
Expand All @@ -158,9 +165,16 @@ class Groups extends BaseExtension {
const entity = this.zigbee.resolveEntity(message);
if (!entity || !entity.type === 'device') {
logger.error(`Device '${message}' does not exist`);
this.mqtt.log(
`device_group_${type}_failed`,
{friendly_name: message, group: topicMatch[1], error: 'entity doesn\'t exists'});

/* istanbul ignore else */
if (settings.get().advanced.legacy_api) {
const payload = {friendly_name: message, group: topicMatch[1], error: 'entity doesn\'t exists'};
this.mqtt.publish(
'bridge/log',
JSON.stringify({type: `device_group_${type}_failed`, message: payload}),
);
}

return;
}

Expand Down Expand Up @@ -188,18 +202,42 @@ class Groups extends BaseExtension {
logger.info(`Adding '${entity.name}' to '${group.name}'`);
await entity.endpoint.addToGroup(group.group);
settings.addDeviceToGroup(group.settings.ID, keys);
this.mqtt.log('device_group_add', {friendly_name: entity.name, group: group.name});

/* istanbul ignore else */
if (settings.get().advanced.legacy_api) {
const payload = {friendly_name: entity.name, group: group.name};
this.mqtt.publish(
'bridge/log',
JSON.stringify({type: `device_group_add`, message: payload}),
);
}
} else if (type === 'remove') {
logger.info(`Removing '${entity.name}' from '${group.name}'`);
await entity.endpoint.removeFromGroup(group.group);
settings.removeDeviceFromGroup(group.settings.ID, keys);
this.mqtt.log('device_group_remove', {friendly_name: entity.name, group: group.name});

/* istanbul ignore else */
if (settings.get().advanced.legacy_api) {
const payload = {friendly_name: entity.name, group: group.name};
this.mqtt.publish(
'bridge/log',
JSON.stringify({type: `device_group_remove`, message: payload}),
);
}
} else { // remove_all
logger.info(`Removing '${entity.name}' from all groups`);
await entity.endpoint.removeFromAllGroups();
for (const settingsGroup of settings.getGroups()) {
settings.removeDeviceFromGroup(settingsGroup.ID, keys);
this.mqtt.log('device_group_remove_all', {friendly_name: entity.name});

/* istanbul ignore else */
if (settings.get().advanced.legacy_api) {
const payload = {friendly_name: entity.name};
this.mqtt.publish(
'bridge/log',
JSON.stringify({type: `device_group_remove_all`, message: payload}),
);
}
}
}
}
Expand Down
Loading

0 comments on commit 552eec7

Please sign in to comment.