2018-08-28 12:55:00 -07:00
|
|
|
const settings = require('../util/settings');
|
2019-06-04 10:23:58 -07:00
|
|
|
const utils = require('../util/utils');
|
2019-09-09 10:48:09 -07:00
|
|
|
const logger = require('../util/logger');
|
2020-04-11 09:10:56 -07:00
|
|
|
const Extension = require('./extension');
|
2020-09-24 09:06:43 -07:00
|
|
|
const stringify = require('json-stable-stringify-without-jsonify');
|
2018-08-28 12:55:00 -07:00
|
|
|
|
2020-04-11 11:58:22 -07:00
|
|
|
/**
|
|
|
|
* This extension creates a network map
|
|
|
|
*/
|
2020-04-11 09:10:56 -07:00
|
|
|
class NetworkMap extends Extension {
|
2020-01-09 13:47:19 -07:00
|
|
|
constructor(zigbee, mqtt, state, publishEntityState, eventBus) {
|
|
|
|
super(zigbee, mqtt, state, publishEntityState, eventBus);
|
2018-08-28 12:55:00 -07:00
|
|
|
|
2020-04-11 11:45:50 -07:00
|
|
|
this.legacyApi = settings.get().advanced.legacy_api;
|
|
|
|
this.legacyTopic = `${settings.get().mqtt.base_topic}/bridge/networkmap`;
|
|
|
|
this.legacyTopicRoutes = `${settings.get().mqtt.base_topic}/bridge/networkmap/routes`;
|
2020-06-15 11:10:30 -07:00
|
|
|
this.topic = `${settings.get().mqtt.base_topic}/bridge/request/networkmap`;
|
2018-08-28 12:55:00 -07:00
|
|
|
|
2019-06-17 12:28:27 -07:00
|
|
|
// Bind
|
|
|
|
this.raw = this.raw.bind(this);
|
|
|
|
this.graphviz = this.graphviz.bind(this);
|
2020-06-14 06:48:50 -07:00
|
|
|
this.plantuml = this.plantuml.bind(this);
|
2019-06-17 12:28:27 -07:00
|
|
|
|
2018-08-28 12:55:00 -07:00
|
|
|
// Set supported formats
|
|
|
|
this.supportedFormats = {
|
|
|
|
'raw': this.raw,
|
2018-08-28 13:07:57 -07:00
|
|
|
'graphviz': this.graphviz,
|
2020-06-14 06:48:50 -07:00
|
|
|
'plantuml': this.plantuml,
|
2018-08-28 12:55:00 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
async onMQTTMessage(topic, message) {
|
2020-04-11 11:45:50 -07:00
|
|
|
/* istanbul ignore else */
|
|
|
|
if (this.legacyApi) {
|
|
|
|
if ((topic === this.legacyTopic || topic === this.legacyTopicRoutes) &&
|
|
|
|
this.supportedFormats.hasOwnProperty(message)) {
|
|
|
|
const includeRoutes = topic === this.legacyTopicRoutes;
|
|
|
|
const topology = await this.networkScan(includeRoutes);
|
2020-10-03 14:24:08 -07:00
|
|
|
let converted = this.supportedFormats[message](topology);
|
|
|
|
converted = message === 'raw' ? stringify(converted) : converted;
|
2020-04-11 11:45:50 -07:00
|
|
|
this.mqtt.publish(`bridge/networkmap/${message}`, converted, {});
|
|
|
|
}
|
2018-08-28 12:55:00 -07:00
|
|
|
}
|
2020-06-15 11:10:30 -07:00
|
|
|
|
2020-12-02 12:38:48 -07:00
|
|
|
if (topic === this.topic) {
|
2020-06-15 11:10:30 -07:00
|
|
|
try {
|
|
|
|
message = utils.parseJSON(message, message);
|
|
|
|
const type = typeof message === 'object' ? message.type : message;
|
|
|
|
if (!this.supportedFormats.hasOwnProperty(type)) {
|
|
|
|
throw new Error(`Type '${type}' not supported, allowed are: ${Object.keys(this.supportedFormats)}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const routes = typeof message === 'object' && message.routes;
|
|
|
|
const topology = await this.networkScan(routes);
|
|
|
|
const value = this.supportedFormats[type](topology);
|
|
|
|
await this.mqtt.publish(
|
|
|
|
'bridge/response/networkmap',
|
2020-08-13 11:00:35 -07:00
|
|
|
stringify(utils.getResponse(message, {routes, type, value}, null)),
|
2020-06-15 11:10:30 -07:00
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
await this.mqtt.publish(
|
|
|
|
'bridge/response/networkmap',
|
2020-08-13 11:00:35 -07:00
|
|
|
stringify(utils.getResponse(message, {}, error.message)),
|
2020-06-15 11:10:30 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2018-08-28 12:55:00 -07:00
|
|
|
}
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
raw(topology) {
|
2020-10-03 13:47:20 -07:00
|
|
|
return topology;
|
2018-08-28 12:55:00 -07:00
|
|
|
}
|
2018-08-28 13:07:57 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
graphviz(topology) {
|
2019-06-26 10:30:38 -07:00
|
|
|
const colors = settings.get().map_options.graphviz.colors;
|
|
|
|
|
2018-10-07 12:46:54 -07:00
|
|
|
let text = 'digraph G {\nnode[shape=record];\n';
|
2019-09-09 10:48:09 -07:00
|
|
|
let style = '';
|
2018-10-07 12:46:54 -07:00
|
|
|
|
2020-05-30 09:27:25 -07:00
|
|
|
topology.nodes.forEach((node) => {
|
2018-10-07 12:46:54 -07:00
|
|
|
const labels = [];
|
|
|
|
|
|
|
|
// Add friendly name
|
2020-05-30 09:27:25 -07:00
|
|
|
labels.push(`${node.friendlyName}`);
|
2018-10-07 12:46:54 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
// Add the device short network address, ieeaddr and scan note (if any)
|
|
|
|
labels.push(
|
2020-05-30 09:27:25 -07:00
|
|
|
`${node.ieeeAddr} (${node.networkAddress})` +
|
|
|
|
((node.failed && node.failed.length) ? `failed: ${node.failed.join(',')}` : ''),
|
2019-09-09 10:48:09 -07:00
|
|
|
);
|
2018-10-07 12:46:54 -07:00
|
|
|
|
|
|
|
// Add the device model
|
2020-05-30 09:27:25 -07:00
|
|
|
if (node.type !== 'Coordinator') {
|
2020-09-02 08:17:35 -07:00
|
|
|
if (node.definition) {
|
|
|
|
labels.push(`${node.definition.vendor} ${node.definition.description} (${node.definition.model})`);
|
2019-09-09 10:48:09 -07:00
|
|
|
} else {
|
2019-11-09 12:38:47 -07:00
|
|
|
// This model is not supported by zigbee-herdsman-converters, add zigbee model information
|
2020-05-30 09:27:25 -07:00
|
|
|
labels.push(`${node.manufacturerName} ${node.modelID}`);
|
2019-09-09 10:48:09 -07:00
|
|
|
}
|
2018-10-07 12:46:54 -07:00
|
|
|
}
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
// Add the device last_seen timestamp
|
2019-06-17 12:28:27 -07:00
|
|
|
let lastSeen = 'unknown';
|
2020-05-30 09:27:25 -07:00
|
|
|
const date = node.type === 'Coordinator' ? Date.now() : node.lastSeen;
|
2019-09-09 10:48:09 -07:00
|
|
|
if (date) {
|
2020-06-14 08:43:27 -07:00
|
|
|
lastSeen = utils.formatDate(date, 'relative');
|
2019-06-17 12:28:27 -07:00
|
|
|
}
|
2019-09-09 10:48:09 -07:00
|
|
|
|
2019-11-06 11:49:03 -07:00
|
|
|
labels.push(lastSeen);
|
2018-10-07 12:46:54 -07:00
|
|
|
|
2019-01-22 12:07:38 -07:00
|
|
|
// Shape the record according to device type
|
2020-05-30 09:27:25 -07:00
|
|
|
if (node.type == 'Coordinator') {
|
2019-09-09 10:48:09 -07:00
|
|
|
style = `style="bold, filled", fillcolor="${colors.fill.coordinator}", ` +
|
2019-06-26 10:30:38 -07:00
|
|
|
`fontcolor="${colors.font.coordinator}"`;
|
2020-05-30 09:27:25 -07:00
|
|
|
} else if (node.type == 'Router') {
|
2019-09-09 10:48:09 -07:00
|
|
|
style = `style="rounded, filled", fillcolor="${colors.fill.router}", ` +
|
2019-06-26 10:30:38 -07:00
|
|
|
`fontcolor="${colors.font.router}"`;
|
2019-01-22 12:07:38 -07:00
|
|
|
} else {
|
2019-09-09 10:48:09 -07:00
|
|
|
style = `style="rounded, dashed, filled", fillcolor="${colors.fill.enddevice}", ` +
|
2019-09-09 10:04:07 -07:00
|
|
|
`fontcolor="${colors.font.enddevice}"`;
|
2019-01-22 12:07:38 -07:00
|
|
|
}
|
|
|
|
|
2018-10-07 12:46:54 -07:00
|
|
|
// Add the device with its labels to the graph as a node.
|
2020-05-30 09:27:25 -07:00
|
|
|
text += ` "${node.ieeeAddr}" [`+style+`, label="{${labels.join('|')}}"];\n`;
|
2018-10-07 12:46:54 -07:00
|
|
|
|
|
|
|
/**
|
2019-07-01 07:10:50 -07:00
|
|
|
* Add an edge between the device and its child to the graph
|
2018-10-07 12:46:54 -07:00
|
|
|
* NOTE: There are situations where a device is NOT in the topology, this can be e.g.
|
|
|
|
* due to not responded to the lqi scan. In that case we do not add an edge for this device.
|
|
|
|
*/
|
2020-05-30 09:27:25 -07:00
|
|
|
topology.links.filter((e) => (e.source.ieeeAddr === node.ieeeAddr)).forEach((e) => {
|
|
|
|
const lineStyle = (node.type=='EndDevice') ? 'penwidth=1, ' :
|
2019-09-09 10:48:09 -07:00
|
|
|
(!e.routes.length) ? 'penwidth=0.5, ' : 'penwidth=2, ';
|
|
|
|
const lineWeight = (!e.routes.length) ? `weight=0, color="${colors.line.inactive}", ` :
|
|
|
|
`weight=1, color="${colors.line.active}", `;
|
|
|
|
const textRoutes = e.routes.map((r) => r.destinationAddress);
|
|
|
|
const lineLabels = (!e.routes.length) ? `label="${e.linkquality}"` :
|
|
|
|
`label="${e.linkquality} (routes: ${textRoutes.join(',')})"`;
|
2020-05-30 09:27:25 -07:00
|
|
|
text += ` "${node.ieeeAddr}" -> "${e.target.ieeeAddr}"`;
|
2019-09-09 10:48:09 -07:00
|
|
|
text += ` [${lineStyle}${lineWeight}${lineLabels}]\n`;
|
|
|
|
});
|
2018-08-28 13:07:57 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
text += '}';
|
|
|
|
|
2018-10-07 12:46:54 -07:00
|
|
|
return text.replace(/\0/g, '');
|
2018-08-28 13:07:57 -07:00
|
|
|
}
|
2019-09-09 10:48:09 -07:00
|
|
|
|
2020-06-14 06:48:50 -07:00
|
|
|
plantuml(topology) {
|
|
|
|
const text = [];
|
|
|
|
|
|
|
|
text.push(`' paste into: https://www.planttext.com/`);
|
|
|
|
text.push(``);
|
|
|
|
text.push('@startuml');
|
|
|
|
|
|
|
|
topology.nodes.sort((a, b) => a.friendlyName.localeCompare(b.friendlyName)).forEach((node) => {
|
|
|
|
// Add friendly name
|
|
|
|
text.push(`card ${node.ieeeAddr} [`);
|
|
|
|
text.push(`${node.friendlyName}`);
|
|
|
|
text.push(`---`);
|
|
|
|
|
|
|
|
// Add the device short network address, ieeaddr and scan note (if any)
|
|
|
|
text.push(
|
|
|
|
`${node.ieeeAddr} (${node.networkAddress})` +
|
|
|
|
((node.failed && node.failed.length) ? ` failed: ${node.failed.join(',')}` : ''),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Add the device model
|
|
|
|
if (node.type !== 'Coordinator') {
|
|
|
|
text.push(`---`);
|
|
|
|
const definition = this.zigbee.resolveEntity(node.ieeeAddr).definition;
|
|
|
|
if (definition) {
|
|
|
|
text.push(`${definition.vendor} ${definition.description} (${definition.model})`);
|
|
|
|
} else {
|
|
|
|
// This model is not supported by zigbee-herdsman-converters, add zigbee model information
|
|
|
|
text.push(`${node.manufacturerName} ${node.modelID}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the device last_seen timestamp
|
|
|
|
let lastSeen = 'unknown';
|
|
|
|
const date = node.type === 'Coordinator' ? Date.now() : node.lastSeen;
|
|
|
|
if (date) {
|
2020-06-14 08:43:27 -07:00
|
|
|
lastSeen = utils.formatDate(date, 'relative');
|
2020-06-14 06:48:50 -07:00
|
|
|
}
|
|
|
|
text.push(`---`);
|
|
|
|
text.push(lastSeen);
|
|
|
|
text.push(`]`);
|
|
|
|
text.push(``);
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add edges between the devices
|
|
|
|
* NOTE: There are situations where a device is NOT in the topology, this can be e.g.
|
|
|
|
* due to not responded to the lqi scan. In that case we do not add an edge for this device.
|
|
|
|
*/
|
|
|
|
topology.links.forEach((link) => {
|
|
|
|
text.push(`${link.sourceIeeeAddr} --> ${link.targetIeeeAddr}: ${link.lqi}`);
|
|
|
|
});
|
|
|
|
text.push('');
|
|
|
|
|
|
|
|
text.push(`@enduml`);
|
|
|
|
|
|
|
|
return text.join(`\n`);
|
|
|
|
}
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
async networkScan(includeRoutes) {
|
|
|
|
logger.info(`Starting network scan (includeRoutes '${includeRoutes}')`);
|
2020-04-11 08:11:09 -07:00
|
|
|
const devices = this.zigbee.getDevices().filter((d) => d.type !== 'GreenPower');
|
2019-09-09 10:48:09 -07:00
|
|
|
const lqis = new Map();
|
|
|
|
const routingTables = new Map();
|
|
|
|
const failed = new Map();
|
|
|
|
|
|
|
|
for (const device of devices.filter((d) => d.type != 'EndDevice')) {
|
|
|
|
failed.set(device, []);
|
2021-01-22 11:16:35 -07:00
|
|
|
await utils.sleep(1); // sleep 1 second between each scan to reduce stress on network.
|
2020-04-11 11:45:50 -07:00
|
|
|
const resolvedEntity = this.zigbee.resolveEntity(device);
|
2019-09-09 10:48:09 -07:00
|
|
|
try {
|
|
|
|
const result = await device.lqi();
|
|
|
|
lqis.set(device, result);
|
2020-04-11 11:45:50 -07:00
|
|
|
logger.debug(`LQI succeeded for '${resolvedEntity.name}'`);
|
2019-09-09 10:48:09 -07:00
|
|
|
} catch (error) {
|
|
|
|
failed.get(device).push('lqi');
|
2020-04-11 11:45:50 -07:00
|
|
|
logger.error(`Failed to execute LQI for '${resolvedEntity.name}'`);
|
2019-09-09 10:48:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (includeRoutes) {
|
|
|
|
try {
|
|
|
|
const result = await device.routingTable();
|
|
|
|
routingTables.set(device, result);
|
2020-04-11 11:45:50 -07:00
|
|
|
logger.debug(`Routing table succeeded for '${resolvedEntity.name}'`);
|
2019-09-09 10:48:09 -07:00
|
|
|
} catch (error) {
|
|
|
|
failed.get(device).push('routingTable');
|
2020-04-11 11:45:50 -07:00
|
|
|
logger.error(`Failed to execute routing table for '${resolvedEntity.name}'`);
|
2019-09-09 10:48:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.info(`Network scan finished`);
|
|
|
|
|
|
|
|
const networkMap = {nodes: [], links: []};
|
|
|
|
// Add nodes
|
|
|
|
for (const device of devices) {
|
2020-04-11 11:45:50 -07:00
|
|
|
const resolvedEntity = this.zigbee.resolveEntity(device);
|
2020-09-02 08:17:35 -07:00
|
|
|
const definition = resolvedEntity.definition ? {
|
|
|
|
model: resolvedEntity.definition.model,
|
|
|
|
vendor: resolvedEntity.definition.vendor,
|
|
|
|
description: resolvedEntity.definition.description,
|
2021-01-27 12:51:30 -07:00
|
|
|
supports: Array.from(new Set((resolvedEntity.definition.exposes).map((e) => {
|
2020-11-22 04:54:19 -07:00
|
|
|
return e.hasOwnProperty('name') ?
|
|
|
|
e.name :
|
|
|
|
`${e.type} (${e.features.map((f) => f.name).join(', ')})`;
|
|
|
|
}))).join(', '),
|
2020-09-02 08:17:35 -07:00
|
|
|
} : null;
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
networkMap.nodes.push({
|
2020-04-11 11:45:50 -07:00
|
|
|
ieeeAddr: device.ieeeAddr, friendlyName: resolvedEntity.name, type: device.type,
|
2019-09-09 10:48:09 -07:00
|
|
|
networkAddress: device.networkAddress, manufacturerName: device.manufacturerName,
|
2019-09-12 13:48:23 -07:00
|
|
|
modelID: device.modelID, failed: failed.get(device), lastSeen: device.lastSeen,
|
2020-09-02 08:17:35 -07:00
|
|
|
definition,
|
2019-09-09 10:48:09 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add links
|
|
|
|
lqis.forEach((lqi, device) => {
|
|
|
|
for (const neighbor of lqi.neighbors) {
|
|
|
|
if (neighbor.relationship > 3) {
|
|
|
|
// Relationship is not active, skip it
|
|
|
|
continue;
|
|
|
|
}
|
2020-10-19 07:38:53 -07:00
|
|
|
|
|
|
|
// Some Xiaomi devices return 0x00 as the neighbor ieeeAddr (obviously not correct).
|
|
|
|
// Determine the correct ieeeAddr based on the networkAddress.
|
|
|
|
const neighborDevice = this.zigbee.getDeviceByNetworkAddress(neighbor.networkAddress);
|
|
|
|
if (neighbor.ieeeAddr === '0x0000000000000000' && neighborDevice) {
|
|
|
|
neighbor.ieeeAddr = neighborDevice.ieeeAddr;
|
|
|
|
}
|
2019-09-09 10:48:09 -07:00
|
|
|
|
|
|
|
const link = {
|
|
|
|
source: {ieeeAddr: neighbor.ieeeAddr, networkAddress: neighbor.networkAddress},
|
|
|
|
target: {ieeeAddr: device.ieeeAddr, networkAddress: device.networkAddress},
|
|
|
|
linkquality: neighbor.linkquality, depth: neighbor.depth, routes: [],
|
2019-09-17 09:19:42 -07:00
|
|
|
// DEPRECATED:
|
|
|
|
sourceIeeeAddr: neighbor.ieeeAddr, targetIeeeAddr: device.ieeeAddr,
|
|
|
|
sourceNwkAddr: neighbor.networkAddress, lqi: neighbor.linkquality,
|
|
|
|
relationship: neighbor.relationship,
|
2019-09-09 10:48:09 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
const routingTable = routingTables.get(device);
|
|
|
|
if (routingTable) {
|
|
|
|
link.routes = routingTable.table
|
|
|
|
.filter((t) => t.status === 'ACTIVE' && t.nextHop === neighbor.networkAddress);
|
|
|
|
}
|
|
|
|
|
|
|
|
networkMap.links.push(link);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return networkMap;
|
|
|
|
}
|
2018-08-28 12:55:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = NetworkMap;
|