Raw implementation of network map. #252

This commit is contained in:
Koenkk
2018-08-28 21:55:00 +02:00
parent 0c32f25203
commit 2a520b0e81
4 changed files with 64 additions and 2 deletions
+13 -1
View File
@@ -3,6 +3,7 @@ const Zigbee = require('./zigbee');
const State = require('./state');
const logger = require('./util/logger');
const settings = require('./util/settings');
const ExtensionNetworkMap = require('./extension/networkMap');
const zigbeeShepherdConverters = require('zigbee-shepherd-converters');
const homeassistant = require('./homeassistant');
const objectAssignDeep = require(`object-assign-deep`);
@@ -94,6 +95,11 @@ class Controller {
});
}
// Initialize extensions.
this.extensions = [
new ExtensionNetworkMap(this.zigbee, this.mqtt, this.state),
];
// Resend all cached states.
this.sendAllCachedStates();
}
@@ -337,6 +343,12 @@ class Controller {
handleMQTTMessage(topic, message) {
logger.debug(`Recieved mqtt message on topic '${topic}' with data '${message}'`);
// Find extensions that could handle this.
const extensions = this.extensions.filter((e) => e.handleMQTTMessage);
// Call extensions.
const extensionResults = extensions.map((e) => e.handleMQTTMessage(topic, message));
if (topic.match(mqttConfigRegex)) {
this.handleMQTTMessageConfig(topic, message);
} else if (topic.match(mqttDeviceRegex) || topic.match(mqttDevicePrefixRegex)) {
@@ -348,7 +360,7 @@ class Controller {
clearTimeout(timer);
}, 20000);
}
} else {
} else if (!extensionResults.includes(true)) {
logger.warn(`Cannot handle MQTT message with topic '${topic}' and message '${message}'`);
}
}
+38
View File
@@ -0,0 +1,38 @@
const settings = require('../util/settings');
class NetworkMap {
constructor(zigbee, mqtt, state) {
this.zigbee = zigbee;
this.mqtt = mqtt;
this.state = state;
// Subscribe to topic.
this.topic = `${settings.get().mqtt.base_topic}/bridge/networkmap`;
this.mqtt.subscribe(this.topic);
// Set supported formats
this.supportedFormats = {
'raw': this.raw,
};
}
handleMQTTMessage(topic, message) {
message = message.toString();
if (topic === this.topic && this.supportedFormats.hasOwnProperty(message)) {
this.zigbee.networkScan((result)=> {
const converted = this.supportedFormats[message](result);
this.mqtt.publish(`bridge/networkmap/${message}`, converted, {});
});
return true;
}
}
raw(topology) {
return JSON.stringify(topology);
}
}
module.exports = NetworkMap;
+5 -1
View File
@@ -70,7 +70,11 @@ class MQTT {
handleConnect() {
logger.info('Connected to MQTT server');
this.publish('bridge/state', 'online', {retain: true, qos: 0});
this.subscriptions.forEach((topic) => this.client.subscribe(topic));
this.subscriptions.forEach((topic) => this.subscribe(topic));
}
subscribe(topic) {
this.client.subscribe(topic);
}
handleMessage(topic, message) {
+8
View File
@@ -212,6 +212,14 @@ class Zigbee {
device.read(cid, attr, callback);
}
networkScan(callback) {
logger.info('Starting network scan...');
this.shepherd.lqiScan().then((result) => {
logger.info('Network scan completed');
callback(result);
});
}
registerOnAfIncomingMsg(ieeeAddr, ep) {
const device = this._findDevice(ieeeAddr, ep);
device.onAfIncomingMsg = (message) => {