-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
159 lines (150 loc) · 5.62 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
148
149
150
151
152
153
154
155
156
157
158
159
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {background-color: rgb(29, 29, 29);}
p {
color: beige;
font: 20px Arial, sans-serif;
max-width: 200px;
margin-bottom: 10px;
margin-top: 10px;
}
.inline {display: inline-block;}
.left {float:left;}
.right {float:right;}
.dot {
height: 25px;
width: 25px;
background-color: rgb(58, 58, 58);
border-radius: 50%;
display: inline-block;
}
.red {
background-color: rgb(248, 68, 68);
}
.green {
background-color: rgb(35, 218, 19);
}
button {
display: inline-block;
height: 30px;
margin-right: 20px;
color: beige;
background-color: rgb(66, 66, 56);
border-radius: 5%;
}
.center {
left: 50%;
top: 50%;
position: absolute;
transform: translate(-50%, -50%);
}
</style>
</head>
<body>
<div class="center">
<div>
<p id="statesText">JS disabled</p>
<div id="indicators" class="inline">
<div class="inline">
<p class="inline">power:</p>
<span id="dotPower" class="dot"></span>
</div>
<div class="inline">
<p class="inline">hdd:</p>
<span id="dotHdd" class="dot"></span>
</div>
</div>
</div>
<div>
<div class="inline">
<button id="powerButton">power</button>
<button id="resetButton">reset</button>
</div>
</div>
</div>
<script>
document.getElementById('statesText').innerHTML = 'status: can\'t get led status';
var powerButton = document.getElementById("powerButton")
var resetButton = document.getElementById("resetButton")
var isSseWork = false
var sseTimer
powerButton.addEventListener('mousedown', function (e) {
fetch(new Request('/set/power/on'));
});
resetButton.addEventListener('mousedown', function (e) {
fetch(new Request('/set/reset/on'));
});
document.addEventListener('mouseup', function (e) {
fetch(new Request('/set/all/off'));
});
var eventSource = new EventSource('/status/sse');
eventSource.onopen = function(e) {
console.log("Connection establish");
document.getElementById('statesText').innerHTML = 'status: online';
isSseWork = true;
};
eventSource.onerror = function(e) {
if (this.readyState == EventSource.CONNECTING) {
console.log("Connection error, reconnect");
document.getElementById('statesText').innerHTML = 'status: can\'t get led status, try to reconnect';
isSseWork = false;
} else {
console.log("Error: " + this.readyState);
document.getElementById('statesText').innerHTML = 'status: can\'t get led status, try to reconnect, error: ' + this.readyState;
isSseWork = false;
}
};
eventSource.onmessage = function(e) {
document.getElementById('statesText').innerHTML = 'status: online';
if (sseTimer != null) {
clearTimeout(sseTimer);
}
isSseWork = true;
const states = JSON.parse(e.data);
document.getElementById('dotPower').className= 'dot'
document.getElementById('dotHdd').className= 'dot'
if (states.power) {
document.getElementById('dotPower').className= 'dot green'
}
if (states.hdd) {
document.getElementById('dotHdd').className= 'dot red'
}
sseTimer = setTimeout(() => {document.getElementById('statesText').innerHTML = 'status: sse not send data in 2 seconds'; sSseWork = false;}, 2000)
};
setInterval(function() {
if (!isSseWork) {
const request = new Request('/status');
var start = Date.now();
fetch(request)
.then((response) => {
if (response.status === 200) {
return response.json();
} else {
document.getElementById('statesText').innerHTML = 'status(pooling): wrong json format';
throw new Error('Something went wrong on API server!');
}
})
.then((response) => {
document.getElementById('statesText').innerHTML = 'status(pooling): online';
document.getElementById('dotPower').className= 'dot'
document.getElementById('dotHdd').className= 'dot'
if (response.power) {
document.getElementById('dotPower').className= 'dot green'
}
if (response.hdd) {
document.getElementById('dotHdd').className= 'dot red'
}
}).catch((error) => {
document.getElementById('statesText').innerHTML = 'status(pooling): error '+ error;
});
}
}, 1000);
</script>
</body>
</html>