From 10a47df33b5322a7469d1643fcf9a1237042e1b2 Mon Sep 17 00:00:00 2001 From: Koen Kanters Date: Sat, 11 Apr 2020 20:45:50 +0200 Subject: [PATCH] Refactor --- lib/extension/networkMap.js | 47 +++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/lib/extension/networkMap.js b/lib/extension/networkMap.js index 8718e8b9b..5095fc825 100644 --- a/lib/extension/networkMap.js +++ b/lib/extension/networkMap.js @@ -8,9 +8,9 @@ class NetworkMap extends Extension { constructor(zigbee, mqtt, state, publishEntityState, eventBus) { super(zigbee, mqtt, state, publishEntityState, eventBus); - // Subscribe to topic. - this.topic = `${settings.get().mqtt.base_topic}/bridge/networkmap`; - this.topicRoutes = `${settings.get().mqtt.base_topic}/bridge/networkmap/routes`; + 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`; // Bind this.raw = this.raw.bind(this); @@ -24,16 +24,23 @@ class NetworkMap extends Extension { } onMQTTConnected() { - this.mqtt.subscribe(this.topic); - this.mqtt.subscribe(this.topicRoutes); + /* istanbul ignore else */ + if (this.legacyApi) { + this.mqtt.subscribe(this.legacyTopic); + this.mqtt.subscribe(this.legacyTopicRoutes); + } } async onMQTTMessage(topic, message) { - if ((topic === this.topic || topic === this.topicRoutes) && this.supportedFormats.hasOwnProperty(message)) { - const includeRoutes = topic === this.topicRoutes; - const topology = await this.networkScan(includeRoutes); - const converted = this.supportedFormats[message](topology); - this.mqtt.publish(`bridge/networkmap/${message}`, converted, {}); + /* 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); + const converted = this.supportedFormats[message](topology); + this.mqtt.publish(`bridge/networkmap/${message}`, converted, {}); + } } } @@ -61,9 +68,9 @@ class NetworkMap extends Extension { // Add the device model if (device.type !== 'Coordinator') { - const mappedModel = zigbeeHerdsmanConverters.findByZigbeeModel(device.modelID); - if (mappedModel) { - labels.push(`${mappedModel.vendor} ${mappedModel.description} (${mappedModel.model})`); + const definition = zigbeeHerdsmanConverters.findByZigbeeModel(device.modelID); + if (definition) { + labels.push(`${definition.vendor} ${definition.description} (${definition.model})`); } else { // This model is not supported by zigbee-herdsman-converters, add zigbee model information labels.push(`${device.manufacturerName} ${device.modelID}`); @@ -126,24 +133,24 @@ class NetworkMap extends Extension { for (const device of devices.filter((d) => d.type != 'EndDevice')) { failed.set(device, []); - const resolved = this.zigbee.resolveEntity(device); + const resolvedEntity = this.zigbee.resolveEntity(device); try { const result = await device.lqi(); lqis.set(device, result); - logger.debug(`LQI succeeded for '${resolved.name}'`); + logger.debug(`LQI succeeded for '${resolvedEntity.name}'`); } catch (error) { failed.get(device).push('lqi'); - logger.error(`Failed to execute LQI for '${resolved.name}'`); + logger.error(`Failed to execute LQI for '${resolvedEntity.name}'`); } if (includeRoutes) { try { const result = await device.routingTable(); routingTables.set(device, result); - logger.debug(`Routing table succeeded for '${resolved.name}'`); + logger.debug(`Routing table succeeded for '${resolvedEntity.name}'`); } catch (error) { failed.get(device).push('routingTable'); - logger.error(`Failed to execute routing table for '${resolved.name}'`); + logger.error(`Failed to execute routing table for '${resolvedEntity.name}'`); } } } @@ -153,9 +160,9 @@ class NetworkMap extends Extension { const networkMap = {nodes: [], links: []}; // Add nodes for (const device of devices) { - const resolved = this.zigbee.resolveEntity(device); + const resolvedEntity = this.zigbee.resolveEntity(device); networkMap.nodes.push({ - ieeeAddr: device.ieeeAddr, friendlyName: resolved.name, type: device.type, + ieeeAddr: device.ieeeAddr, friendlyName: resolvedEntity.name, type: device.type, networkAddress: device.networkAddress, manufacturerName: device.manufacturerName, modelID: device.modelID, failed: failed.get(device), lastSeen: device.lastSeen, });