Hi.
I’ve used this code sample for my OpenHAB 2.4 instance, too. Unfortunately it isn’t working.
After some tests I’ve found a solution which I would present here, too.
The main things was that zigbee.dot wasn’t created with values from my bridge. This results in an empty zigbee.svg file. Within zigbee.html .min was missing at the js link for svg-zoom lib.
Withing the rule there is another way needed to publish the networkmap request to mqtt. Found this in some other posts, too.
I post only changes (and some parts around) to the main post.
/etc/openhab2/html/zigbee.html
<html>
<head>
<script src="svg-pan-zoom.min.js"></script>
</head>
<body style="margin: 0; padding: 0">
<embed type="image/svg+xml" src="zigbee.svg" id="map" width="100%" height="100%"/>
<script language="JavaScript">
document.getElementById('map').addEventListener('load', function(){
// Will get called after embed element was loaded
svgPanZoom(document.getElementById('map'), {
controlIconsEnabled: true
});
})
var panZoomMap = svgPanZoom('#map');
</script>
</body>
</html>
Because of that I’ve used the MQTT Binding for OH 2.4 the way only through this item wasn’t working.
First I’ve created a thing for bridge and channel for networkmap
/etc/openhab2/things/something.things
Bridge mqtt:broker:mymqtt "MQTT Broker v2" @ "MQTT" [
host="127.0.0.1",
secure=false,
port=1883,
qos=0,
clientid="OpenHAB2",
keep_alive_time=30000,
reconnect_time=60000
] {
Thing topic bridge "MQTT Zigbee Bridge C2531 Config" @ "MQTT" {
Channels:
Type switch : config-permit_join "Permit Join" [
stateTopic="zigbee2mqtt/bridge/config",
commandTopic="zigbee2mqtt/bridge/config/permit_join",
transformationPattern="JSONPATH:$.permit_join",
on="true",
off="false"
]
Type string : config-version "Version" [
stateTopic="zigbee2mqtt/bridge/config",
transformationPattern="JSONPATH:$.version"
]
Type string : config-commit "Commit" [
stateTopic="zigbee2mqtt/bridge/config",
transformationPattern="JSONPATH:$.commit"
]
Type string : config-coordinator_firmware "Coordinator Firmware" [
stateTopic="zigbee2mqtt/bridge/config",
transformationPattern="JSONPATH:$.coordinator_firmware"
]
Type string : config-log_level "Log Level" [
stateTopic="zigbee2mqtt/bridge/config",
transformationPattern="JSONPATH:$.log_level"
]
Type string : networkmap "Network Map" [
stateTopic="zigbee2mqtt/bridge/networkmap/graphviz"
]
}
/etc/openhab2/rules/zigbee_networkmap.rules
import java.io.FileWriter
import java.io.FileReader
import java.io.BufferedReader
// parameters
val String dotFile = "/etc/openhab2/html/zigbee.dot"
val String mapFile = "/etc/openhab2/html/zigbee.map"
val String svgFile = "/etc/openhab2/html/zigbee.svg"
rule "Request ZigBee Network Map in Graphviz format"
when
Time cron "0 3/5 * * * ?"
// Time cron "* * * * * ?"
then
val actions = getActions("mqtt","mqtt:broker:mymqtt")
actions.publishMQTT("zigbee2mqtt/bridge/networkmap","graphviz")
// ask zigbee2mqtt to generate graphviz network map (in DOT format)
//publish("mqttembeddedbroker", "zigbee2mqtt/bridge/networkmap", "graphviz")
end
rule "Fetch ZigBee tree in Graphviz format"
when
Item zigbee_NetworkMap changed
then
print(zigbee_NetworkMap)
// once zigbee2mqtt sent graphviz network map => store it and convert it to PNG/SVG
if (zigbee_NetworkMap.state !== NULL && zigbee_NetworkMap.state != "") {
var String dotString = zigbee_NetworkMap.state.toString
// load mapping file and regex-replace network addresses with friendly names
var FileReader fr = new FileReader(mapFile);
var BufferedReader br = new BufferedReader(fr);
var String line
var String[] split
var String regex
var String replace
while ((line = br.readLine()) !== null) {
split = line.split("=")
if(split.length == 2) {
regex = "(label=.*)(" + split.get(0) + ")(.*)"
replace = "$1" + split.get(1) + "$3"
dotString = dotString.replaceFirst(regex, replace)
}
}
fr.close();
// store result to DOT file
var fw = new FileWriter(dotFile, false);
fw.write(dotString);
fw.flush();
fw.close();
// render PNG/SVG
executeCommandLine("/usr/bin/sfdp -Nfontname=Arial -Nfontsize=9 -Ncolor=#666666 -Nstyle=filled -Nfillcolor=#eeeeee -Efontname=Arial -Efontsize=8 -Efontcolor=#cc0000 -Ecolor=#cccccc -Tsvg -o" + svgFile + " " + dotFile, 5000)
}
end
/etc/openhab2/items/zigbee.items
String zigbee_NetworkMap { channel="mqtt:topic:mymqtt:bridge:networkmap" }
Group zigbee_NetworkMap_Webview "ZigBee Topologie"
Sitemap has no additional changes…
Hope I’ve nothing forgotten to post here. Feel free to test …
