-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
148 lines (128 loc) · 3.94 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<link rel="stylesheet" type="text/css" href="z2mvis.css">
</head>
<body>
<script src="//d3js.org/d3.v4.min.js"></script>
<script src="https://unpkg.com/[email protected]/viz.js" type="javascript/worker"></script>
<script src="https://unpkg.com/[email protected]/build/d3-graphviz.min.js"></script>
<script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script>
<script src="config.js"></script>
<div id="status"></div>Refresh mode: <button id="btn.manual" class="button" onclick="onManual()">Manual</button><button id="btn.auto" class="button" onclick="onAuto()">Auto</button>
<div id="graph" style="text-align: center;"></div>
<script>
function getSearchParameters() {
var prmstr = window.location.search.substr(1);
return prmstr != null && prmstr != "" ? transformToAssocArray(prmstr) : {};
}
function transformToAssocArray( prmstr ) {
var params = {};
var prmarr = prmstr.split("&");
for ( var i = 0; i < prmarr.length; i++) {
var tmparr = prmarr[i].split("=");
params[tmparr[0]] = tmparr[1];
}
return params;
}
function extend(obj, src) {
Object.keys(src).forEach(function(key) { obj[key] = src[key]; });
return obj;
}
var params = getSearchParameters();
var setparams = {
renderer: 'dot'
}
extend(setparams, z2mvisConfig);
extend(setparams, params);
var state = {
connection: "idle",
refresh: "auto"
}
var client = mqtt.connect("ws://"+setparams.mqttserver)
client.on('error', function(err) {
console.log("mqtt error");
});
client.stream.on('error', function(err) {
// console.log("mqtt connection error" + err);
// console.log(err);
state.connection = "Error";
renderState();
});
function requestRefresh(delay) {
state.connection= 'refresh delay';
renderState();
setTimeout( function() {
state.connection= 'updating';
renderState();
client.publish("zigbee2mqtt/bridge/networkmap", "graphviz");
}, delay);
}
client.subscribe("zigbee2mqtt/bridge/networkmap/graphviz")
var prevgraph = 0;
client.on("message", function (topic, payload) {
state.connection = 'ready';
renderState();
var graph = new TextDecoder("utf-8").decode(payload);
if(graph!==prevgraph) {
prevgraph = graph;
displayGraph(graph);
}
if(state.refresh=='auto')
requestRefresh(10000);
})
client.on('connect', function () {
console.log('Connected');
state.connection="ready";
renderState();
if(state.refresh=='auto')
requestRefresh(0);
})
var renderer = d3.select("#graph").graphviz({height:"700px", fit:true}).engine(setparams.renderer);
function displayGraph(content) {
renderer
.fade(true)
.renderDot(content);
}
function renderState()
{
var html = "MQTT server: " + setparams.mqttserver;
html+= " State: "+state.connection;
html +=' Renderer: ' + setparams.renderer;
if(state.connection == 'Error') {
html += '<br><br> <h2>MQTT Connection error<br></h2>Check the MQTT server IP address and the MQTT configuration. <br>See readme on <a href="https://github.com/lokonli/z2mvis">Github</a><br>'
}
document.getElementById("status").innerHTML = html;
}
function renderButtons() {
var btnManual = document.getElementById("btn.manual");
var btnAuto = document.getElementById("btn.auto");
if(state.refresh=="auto") {
btnManual.classList.remove("selected");
btnAuto.classList.add("selected")
}
else {
btnManual.classList.add("selected");
btnAuto.classList.remove("selected")
}
}
/* Eventhandler for the Manual button */
function onManual() {
state.refresh = "manual";
renderButtons();
if(state.connection=='ready')
requestRefresh(0);
}
/* Eventhandler for the Manual button */
function onAuto() {
if(state.refresh!=='auto') {
state.refresh = "auto";
renderButtons();
if (state.connection == 'ready')
requestRefresh(0);
}
}
/* Event handler for the Auto button */
renderState();
renderButtons();
</script>