forked from dmulcahey/zha-network-visualization-card
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zha-network-visualization-card.js
230 lines (204 loc) · 5.73 KB
/
zha-network-visualization-card.js
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import "https://unpkg.com/[email protected]/dist/vis-network.min.js?module";
function loadCSS(url) {
const link = document.createElement("link");
link.type = "text/css";
link.rel = "stylesheet";
link.href = url;
document.head.appendChild(link);
}
loadCSS("https://unpkg.com/[email protected]/dist/dist/vis-network.min.css");
class ZHANetworkVisualizationCard extends HTMLElement {
constructor() {
super();
this.bufferTime = 1000 * 60 * 5; //5 minutes
this.attachShadow({
mode: "open",
});
this.networkOptions = {
autoResize: true,
height: "1000px",
layout: {
improvedLayout: true,
},
physics: {
barnesHut: {
springConstant: 0,
avoidOverlap: 10,
damping: 0.09,
},
},
nodes: {
font: {
multi: "html",
},
},
edges: {
smooth: {
type: "continuous",
forceDirection: "none",
roundness: 0.6,
},
},
};
}
setConfig(config) {
// get & keep card-config and hass-interface
const root = this.shadowRoot;
if (root.lastChild) root.removeChild(root.lastChild);
this._config = Object.assign({}, config);
// assemble html
const card = document.createElement("ha-card");
const content = document.createElement("div");
this.timelabel = document.createElement("label");
this.filterinput = document.createElement("input");
card.appendChild(this.timelabel);
card.appendChild(this.filterinput);
card.appendChild(content);
this.nodes = [];
this.network = new vis.Network(content, {}, this.networkOptions);
this.network.on("click", function (properties) {
const ieee = properties.nodes[0];
if (ieee) {
let ev = new Event("zha-show-device-dialog", {
bubbles: true,
cancelable: false,
composed: true,
});
ev.detail = { ieee: ieee };
root.dispatchEvent(ev);
}
});
this.filterinput.oninput = function () {
let filterednodes = this.nodes.filter((x) =>
x.label.toLowerCase().includes(this.filterinput.value.toLowerCase())
);
this.network.selectNodes(filterednodes.map((x) => x.id));
}.bind(this);
root.appendChild(card);
}
_updateContent(data) {
this._updateDevices(data.devices);
this._updateTimestamp(data.time);
}
_updateTimestamp(timestamp) {
var date = new Date(timestamp * 1000);
var iso = date
.toISOString()
.match(/(\d{4}\-\d{2}\-\d{2})T(\d{2}:\d{2}:\d{2})/);
this.timelabel.innerHTML = iso[1] + " " + iso[2];
}
_updateDevices(devices) {
this.nodes = [];
var edges = [];
devices.map((device) => {
this.nodes.push({
id: device["ieee"],
label: this._buildLabel(device),
shape: this._getShape(device),
mass: this._getMass(device),
});
if (device.neighbours && device.neighbours.length > 0) {
device.neighbours.map((neighbour) => {
var idx = edges.findIndex(function (e) {
return device.ieee === e.to && neighbour.ieee === e.from;
});
if (idx === -1) {
edges.push({
from: device["ieee"],
to: neighbour["ieee"],
label: neighbour["lqi"] + "",
color: this._getLQI(neighbour["lqi"]),
});
} else {
edges[idx].color = this._getLQI(
(parseInt(edges[idx].label) + neighbour.lqi) / 2
);
edges[idx].label += "/" + neighbour["lqi"];
}
});
}
});
this.network.setData({ nodes: this.nodes, edges: edges });
}
_getLQI(lqi) {
if (lqi > 192) {
return { color: "#17ab00", highlight: "#17ab00" };
} else if (lqi > 128) {
return { color: "#e6b402", highlight: "#e6b402" };
} else if (lqi > 80) {
return { color: "#fc4c4c", highlight: "#fc4c4c" };
}
return { color: "#bfbfbf", highlight: "#bfbfbf" };
}
_getMass(device) {
if (device.device_type === "Coordinator") {
return 2;
} else if (device.device_type === "Router") {
return 4;
} else {
return 5;
}
}
_getShape(device) {
if (device.device_type === "Coordinator") {
return "box";
} else if (device.device_type === "Router") {
return "ellipse";
} else {
return "circle";
}
}
_buildLabel(device) {
var regDevices = this.deviceRegistry.filter((regDev) => {
return regDev.ieee === device.ieee;
});
var res =
regDevices.length > 0
? "<b>" + regDevices[0].user_given_name + "</b>" + "\n"
: "";
res += "<b>IEEE: </b>" + device.ieee;
res += "\n<b>Device Type: </b>" + device.device_type.replace("_", " ");
if (device.nwk != null) {
res += "\n<b>NWK: </b>" + device.nwk;
}
if (device.manufacturer != null && device.model != null) {
res += "\n<b>Device: </b>" + device.manufacturer + " " + device.model;
} else {
res += "\n<b>Device is not in <i>'zigbee.db'</i></b>";
}
if (device.offline) {
res += "\n<b>Device is <i>Offline</i></b>";
}
return res;
}
set hass(hass) {
if (
this.lastUpdated &&
new Date(this.lastUpdated + this.bufferTime) > Date.now()
) {
return;
}
hass
.callWS({
type: "zha/devices",
})
.then((devices) => {
this.deviceRegistry = devices;
hass
.callWS({
type: "zha_map/devices",
})
.then((zhaMapData) => {
this._updateContent(zhaMapData);
});
});
this.lastUpdated = Date.now();
}
getCardSize() {
return 10;
}
}
customElements.define(
"zha-network-visualization-card",
ZHANetworkVisualizationCard
);