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');
|
2018-10-07 12:46:54 -07:00
|
|
|
const zigbeeShepherdConverters = require('zigbee-shepherd-converters');
|
2018-08-28 12:55:00 -07:00
|
|
|
|
|
|
|
class NetworkMap {
|
2019-02-04 10:36:49 -07:00
|
|
|
constructor(zigbee, mqtt, state, publishEntityState) {
|
2018-08-28 12:55:00 -07:00
|
|
|
this.zigbee = zigbee;
|
|
|
|
this.mqtt = mqtt;
|
|
|
|
this.state = state;
|
|
|
|
|
|
|
|
// Subscribe to topic.
|
|
|
|
this.topic = `${settings.get().mqtt.base_topic}/bridge/networkmap`;
|
|
|
|
|
|
|
|
// Set supported formats
|
|
|
|
this.supportedFormats = {
|
|
|
|
'raw': this.raw,
|
2018-08-28 13:07:57 -07:00
|
|
|
'graphviz': this.graphviz,
|
2018-08-28 12:55:00 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
onMQTTConnected() {
|
|
|
|
this.mqtt.subscribe(this.topic);
|
|
|
|
}
|
|
|
|
|
|
|
|
onMQTTMessage(topic, message) {
|
2018-08-28 12:55:00 -07:00
|
|
|
message = message.toString();
|
|
|
|
|
|
|
|
if (topic === this.topic && this.supportedFormats.hasOwnProperty(message)) {
|
|
|
|
this.zigbee.networkScan((result)=> {
|
2018-10-07 12:46:54 -07:00
|
|
|
const converted = this.supportedFormats[message](this.zigbee, result);
|
2018-08-28 12:55:00 -07:00
|
|
|
this.mqtt.publish(`bridge/networkmap/${message}`, converted, {});
|
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-10-23 11:39:48 -07:00
|
|
|
|
|
|
|
return false;
|
2018-08-28 12:55:00 -07:00
|
|
|
}
|
|
|
|
|
2018-10-07 12:46:54 -07:00
|
|
|
raw(zigbee, topology) {
|
2018-08-28 12:55:00 -07:00
|
|
|
return JSON.stringify(topology);
|
|
|
|
}
|
2018-08-28 13:07:57 -07:00
|
|
|
|
2018-10-07 12:46:54 -07:00
|
|
|
graphviz(zigbee, topology) {
|
|
|
|
let text = 'digraph G {\nnode[shape=record];\n';
|
2019-01-22 12:07:38 -07:00
|
|
|
let devStyle = '';
|
2018-10-07 12:46:54 -07:00
|
|
|
|
|
|
|
zigbee.getDevices().forEach((device) => {
|
|
|
|
const labels = [];
|
|
|
|
const friendlyDevice = settings.getDevice(device.ieeeAddr);
|
|
|
|
const friendlyName = friendlyDevice ? friendlyDevice.friendly_name : device.ieeeAddr;
|
|
|
|
|
|
|
|
// Add friendly name
|
2019-06-09 14:46:19 -07:00
|
|
|
labels.push(`${friendlyName}:${device.nwkAddr}`);
|
2018-10-07 12:46:54 -07:00
|
|
|
|
|
|
|
// Add the device type
|
2019-06-04 10:23:58 -07:00
|
|
|
const deviceType = utils.correctDeviceType(device);
|
|
|
|
labels.push(deviceType);
|
2018-10-07 12:46:54 -07:00
|
|
|
|
|
|
|
// Add the device model
|
|
|
|
const mappedModel = zigbeeShepherdConverters.findByZigbeeModel(device.modelId);
|
|
|
|
if (mappedModel) {
|
|
|
|
labels.push(`${mappedModel.vendor} ${mappedModel.description} (${mappedModel.model})`);
|
|
|
|
} else {
|
|
|
|
// This model is not supported by zigbee-shepherd-converters, add zigbee model information, if available
|
|
|
|
const zigbeeModel = [device.manufName, device.modelId].filter((a) => a).join(' ');
|
|
|
|
labels.push(zigbeeModel ? zigbeeModel : 'No model information available');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the device status (online/offline)
|
|
|
|
labels.push(device.status);
|
|
|
|
|
2019-01-22 12:07:38 -07:00
|
|
|
// Shape the record according to device type
|
2019-06-04 10:23:58 -07:00
|
|
|
if (deviceType == 'Coordinator') {
|
2019-01-22 12:07:38 -07:00
|
|
|
devStyle = 'style="bold"';
|
2019-06-04 10:23:58 -07:00
|
|
|
} else if (deviceType == 'Router') {
|
2019-01-22 12:07:38 -07:00
|
|
|
devStyle = 'style="rounded"';
|
|
|
|
} else {
|
|
|
|
devStyle = 'style="rounded, dashed"';
|
|
|
|
}
|
|
|
|
|
2018-10-07 12:46:54 -07:00
|
|
|
// Add the device with its labels to the graph as a node.
|
2019-01-22 12:07:38 -07:00
|
|
|
text += ` "${device.ieeeAddr}" [`+devStyle+`, label="{${labels.join('|')}}"];\n`;
|
2018-10-07 12:46:54 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an edge between the device and its parent to the graph
|
|
|
|
* 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.
|
|
|
|
*/
|
2019-06-08 23:16:38 -07:00
|
|
|
topology.filter((e) => (e.ieeeAddr === device.ieeeAddr) || (e.nwkAddr === device.nwkAddr)).forEach((e) => {
|
2019-01-22 12:07:38 -07:00
|
|
|
const lineStyle = (e.lqi==0) ? `style="dashed", ` : ``;
|
|
|
|
text += ` "${device.ieeeAddr}" -> "${e.parent}" [`+lineStyle+`label="${e.lqi}"]\n`;
|
2019-01-18 14:11:01 -07:00
|
|
|
});
|
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
|
|
|
}
|
2018-08-28 12:55:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = NetworkMap;
|