-
Notifications
You must be signed in to change notification settings - Fork 0
/
wifiRelay.html
240 lines (201 loc) · 5.52 KB
/
wifiRelay.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
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
231
232
233
234
235
236
237
238
239
240
<html>
<head>
<title>Wifi Power Outlet</title>
<meta charset = 'UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<script type='text/javascript'>
var switchState = false;
var switchWidgetState = false;
var switchWidget;
//intervall, jede Sekunde auf ./status zurückgreifen und button widget entsprechend aktualisieren
window.addEventListener('load', function(){
switchWidget = document.getElementsByClassName('switchWidget')[0];
switchWidget.addEventListener('click', switchDevice);
refreshStatus();
setInterval(refreshStatus, 500);
});
/**
* Queries the nodeMCU for the current switch state, writes it to the global siwtchState variable and updates the switchWidget
*/
function refreshStatus(){
var request = new XMLHttpRequest();
//console.log('Checking Device Status');
request.open('GET','/status');
request.addEventListener('load', function(event) {
if (request.status >= 200 && request.status < 300) {
//console.log('Status result: ' + request.responseText);
switchState = (request.responseText === 'ON') ? true : false;
//console.log('SwitchStateFlag = ' + switchState);
updateSwitchWidget();
} else {
console.warn(request.statusText, request.responseText);
}
});
request.send();
}
//Funktion, die widget switch handled
//Anfrage an ./switch
//Entsprechend der Antwort widget aktualisieren
function switchDevice(){
console.log('Switching the device!');
var request = new XMLHttpRequest();
request.open('GET','/switch');
request.addEventListener('load', function(event) {
if (request.status >= 200 && request.status < 300) {
//console.log('Switch result: ' + request.responseText);
switchState = (request.responseText === 'ON') ? true : false;
//console.log('SwitchStateFlag = ' + switchState);
updateSwitchWidget();
} else {
console.warn(request.statusText, request.responseText);
}
});
request.send();
}
/**
* Aktualisiert das SwitchWidget entsprechend dem Wert des physischen Switch
*/
function updateSwitchWidget(){
if (switchState !== switchWidgetState) {
//Ball nach rechts verschieben (css-Animation)
//Farbe vom Hintergrund ändern
//console.log('Changing the WIDGET');
var offColor = 'linear-gradient(to right, rgba(102,102,102,1) 0%, rgba(48,48,48,1) 100%)';
var onColor = 'linear-gradient(to left, rgba(31,49,166,1) 0%, rgba(32,124,229,1) 100%)';
var bgWidth = switchWidget.offsetWidth;
var knob = switchWidget.getElementsByTagName('SPAN')[0];
var knobWidth = knob.offsetWidth;
if (switchState === false){
console.log('Switched OFF - color: ' + offColor);
switchWidget.style.background = offColor;
knob.style.left = '1';
} else {
console.log('Switched ON - color: ' + onColor);
switchWidget.style.background = onColor;
var leftOffset = bgWidth - knobWidth - 1;
knob.style.left = leftOffset;
}
switchWidgetState = !switchWidgetState;
}
}
</script>
<style type='text/css'>
* {
margin: 0;
padding: 0;
}
body {
background: white;
font-family: 'Trebuchet MS', Helvetica, sans-serif;
}
a {
color: #186ff2;
text-decoration: none;
}
a:hover {
color: #1975ff;
cursor: hand;
text-decoration: underline;
}
header {
background: linear-gradient(to right, rgba(32,124,229,1) 0%, rgba(31,49,166,1) 100%);
box-shadow: 0px 5px 14px -1px rgba(0,0,0,0.63);
position: fixed;
height: 60px;
text-align: center;
width: 100%;
}
header h3 {
color: white;
font-size: 28;
padding-top: 15;
}
.main {
width: 90%;
padding-top: 100px;
margin: auto;
}
.switchUnit {
margin-top: 20px;
border: 1px solid rgb(150,150,150);
border-radius: 5px;
background: linear-gradient(to right, rgba(222,222,222,1) 0%, rgba(240,240,240,1) 100%);
width: 45%;
padding: 15px;
color: rgb(50, 50, 50);
}
.location {
font-size: 14;
color: rgb(150, 150, 150);
}
.switchWidget {
position: relative;
margin-top: 10px;
width: 50px;
height: 25px;
border-radius: 25px;
border-color: #232323;
border-width: 1px;
background: linear-gradient(to right, rgba(102,102,102,1) 0%, rgba(48,48,48,1) 100%);
}
.switchWidget span {
position: absolute;
left: 1;
top: 1;
border-radius: 100%;
border-width: 0;
background: linear-gradient(to bottom, #e2e2e2 0%,#dbdbdb 50%,#d1d1d1 51%,#fefefe 100%);
width: 23px;
height: 23px;
}
.switchWidget:hover {
cursor: pointer;
}
footer {
background: linear-gradient(to right, rgba(37,42,46,1) 0%, rgba(1,27,46,1) 100%);
width: 100%;
height: 30px;
position: fixed;
bottom: 0;
left: 0;
color: white;
text-align: center;
padding-top: 15px;
}
@media screen and (max-width: 395px) {
header {
height: 100px;
}
.switchUnit {
width: inherit;
}
.main {
width: 80%;
margin: auto;
}
}
@media screen and (max-width: 270px) {
footer {
height: 100px;
}
}
</style>
</head>
<body>
<header>
<h3>Wifi Power Outlet</h3>
</header>
<div class='main'>
<div class='switchUnit'>
<h2>My Outlet</h2>
<span class='location'>Its location</span>
<div class='switchWidget'>
<span><!--Circle--></span>
</div>
</div>
</div>
<footer>
<div class='width80'>Developed by <a href='www.fabianwildgrube.de'>Fabian Wildgrube</a>, 2018</div>
</footer>
</body>
</html>