-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
74 lines (70 loc) · 1.97 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
<!DOCTYPE html>
<html>
<head>
<style>
#cards {
display: flex;
flex-wrap: wrap;
flex-direction: row;
}
div.card {
border: 1px solid black;
border-radius: 5px;
width: 300px;
min-width: 300px;
background-color: #eeeeee;
padding: 0 10px;
margin: 10px;
}
</style>
</head>
<body>
<h1>Energy Control Panel</h1>
<div id="info"></div>
<div id="cards"></div>
<script>
const socket = new WebSocket('ws://HOST:PORT/?token=TOKEN&id=CLIENTID');
const info = document.getElementById('info');
const cardsDiv = document.getElementById('cards');
const writeLine = (text) => {
const line = document.createElement('div');
line.innerHTML = '<p>' + text + '</p>';
info.appendChild(line);
};
socket.addEventListener('open', () => {
writeLine('connected');
});
socket.addEventListener('close', () => {
writeLine('closed');
});
socket.addEventListener('message', ({ data }) => {
var json = JSON.parse(data);
json.cards.forEach((card) => {
var div = document.getElementById(card.id);
if (!div) {
div = document.createElement('div');
div.id = card.id;
div.classList.add('card');
cardsDiv.appendChild(div);
}
div.innerHTML = '';
card.lines.forEach((line) => {
p = document.createElement('p');
if (line.left)
p.innerHTML = line.left;
if (line.center) {
p.innerHTML = line.center;
p.style.textAlign = "center";
}
if (line.right) {
p.innerHTML = line.right;
p.style.textAlign = "right";
}
div.appendChild(p);
});
});
//writeLine(data);
});
</script>
</body>
</html>