-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
348 lines (336 loc) · 12.1 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://unpkg.com/react@17/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone@7.12.9/babel.min.js"></script>
<link
rel="stylesheet"
href="https://site-assets.fontawesome.com/releases/v6.6.0/css/all.css"
/>
<link rel="stylesheet" href="index.css" />
<title>Smart Home Manager - Dashboard</title>
</head>
<body>
<div class="sidebar"></div>
<div id="main">
<div id="app"></div>
</div>
<script type="text/babel">
const _devices = [];
fetch("/api/updates")
.then((res) => res.json())
.then((data) => {
_devices = organizeData(data);
})
.catch((error) => {
// mostly dev purposes but also incase the fetch fails
var organized = organizeData(_devices);
setDevices(() => {
organized.blind = organized.blind.map((device) => ({
...device,
status: Math.random() > 0.5 ? "open" : "closed",
}));
return organized;
});
});
function organizeData(data) {
if (Object.keys(data).length === 0) {
return {};
}
// create a dictionary, where the key is the device type and the value is a list of dictionaries of the devices
const organizedData = data.reduce((acc, device) => {
if (!acc[device.type]) {
acc[device.type] = [];
}
acc[device.type].push(device);
return acc;
}, {});
return organizedData;
}
function Sidebar() {
const navList = React.useRef(null);
function changeScreen(page) {
currentPage = page;
for (let i = 0; i < navList.current.children.length; i++) {
if (i !== currentPage) {
navList.current.children[i].classList.remove("active");
}
}
// get the html element of the navlist. Find the nth (li) child, and add the active class to that child
navList.current.children[currentPage].classList.add("active");
// remove the active class from the other children
rerender();
}
function toggleActive() {
document.querySelector(".sidebar").classList.toggle("active");
}
return (
<>
<div className="top">
<div className="logo">
<span>Smart Home Manager</span>
</div>
<i
className="fa-regular fa-bars"
id="btn"
onClick={toggleActive}
></i>
</div>
<ul id="navlist" ref={navList}>
<li>
<a href="#" onClick={() => changeScreen(0)}>
<i className="fa-regular fa-house"></i>
<span className="nav-item">Dashboard</span>
<span className="tooltip">Dashboard</span>
</a>
</li>
<li>
<a href="#" onClick={() => changeScreen(1)}>
<i className="fa-regular fa-gear"></i>
<span className="nav-item">Settings</span>
<span className="tooltip">Settings</span>
</a>
</li>
<li>
<a href="#" onClick={() => changeScreen(2)}>
<i className="fa-regular fa-info"></i>
<span className="nav-item">Server Info</span>
<span className="tooltip">Server Info</span>
</a>
</li>
<li>
<a href="#" onClick={() => changeScreen(3)}>
<i className="fa-regular fa-bluetooth"></i>
<span className="nav-item">Add A Device</span>
<span className="tooltip">Add A Device</span>
</a>
</li>
<li>
<a href="#">
<i className="fa-regular fa-gear"></i>
<span className="nav-item">Dashboard</span>
<span className="tooltip">Dashboard</span>
</a>
</li>
<li>
<a href="#">
<i className="fa-regular fa-gear"></i>
<span className="nav-item">Dashboard</span>
<span className="tooltip">Dashboard</span>
</a>
</li>
</ul>
</>
);
}
function FindNewDevice() {
const [deviceFound, setDeviceFound] = React.useState(false);
function sendCredentials() {
loc = document.querySelector("input[name=location]").value;
fetch("/api/credential-apply", {
method: "POST",
body: JSON.stringify({
location: loc,
}),
}).then((res) => {
if (res.status === 200) {
alert("Device added successfully!");
changeScreen(0);
} else {
alert(
"An error occurred while adding the device. Please ensure the device is on."
);
}
});
}
React.useEffect(() => {
const interval = setInterval(() => {
fetch("/api/network")
.then((res) => res.json())
.then((data) => {
if (data[0] === true) {
setDeviceFound(true);
} else {
setDeviceFound(false);
}
});
}, 1000);
return () => clearInterval(interval);
}, []);
return (
<>
<h1>Find a Device</h1>
{deviceFound && (
<>
<h2>Device Found!</h2>
<label htmlFor="location">Device Location</label>
<br />
<input
type="text"
className="inp"
name="location"
placeholder="Office, Kitchen, Dining Room 1"
></input>
<br />
<button className="op-btn" onClick={() => sendCredentials()}>
Finish Setup
</button>
</>
)}
{deviceFound === false && (
<>
<p>Scanning for devices...</p>
<p> If you have a device to add, please turn it on now.
The device should show up once both devices' leds turn <span style={{color:"green",padding:"0"}}>green</span>.
</p>
</>
)}
</>
);
}
function DeviceWidgets() {
const [devices, setDevices] = React.useState(organizeData(_devices));
// create the same setinterval function to fetch the updates
React.useEffect(() => {
//Implementing the setInterval method
const interval = setInterval(() => {
fetch("/api/updates")
.then((res) => res.json())
.then((data) => {
console.log(organizeData(data));
setDevices(organizeData(data));
})
.catch((error) => {
// mostly dev purposes but also incase the fetch fails
var organized = organizeData(_devices);
setDevices(() => {
organized.blind = organized.blind.map((device) => ({
...device,
status: Math.random() > 0.5 ? "open" : "closed",
}));
return organized;
});
});
}, 5000);
//Clearing the interval
return () => clearInterval(interval);
}, []);
// create a function that can fetch a device operation
function fetchOperation(id, operation) {
fetch(`/api/operation/${id}/${operation}`)
.then((res) => res.json())
.then((data) => {
console.log(data);
setDevices(data);
});
}
return (
<React.Fragment>
<h1>Your Devices</h1>
{Object.keys(devices).length === 0 && (
<p>No devices yet. Check the instructions on how to add one!</p>
)}
{Object.keys(devices).length > 0 && (
<React.Fragment>
<h3 className="category-title">Blinds</h3>
<div className="devices">
{devices.blind.map((device) => (
<div key={device.location} className="device">
<h3>{device.location}</h3>
<p>Device status: {device.status}</p>
<div className="sp">
<button
className="op-btn"
onClick={() => fetchOperation(device.id, "open")}
>
<i className="fa-solid fa-blinds-open"></i>
Open
</button>
<button
className="op-btn"
onClick={() => fetchOperation(device.id, "close")}
>
<i className="fa-solid fa-blinds"></i>
Close
</button>
</div>
</div>
))}
</div>
<h3 className="category-title">Cameras</h3>
<div className="devices">
{devices.camera !== undefined && (
<>
{devices.camera.map((device) => (
<div key={device.location} className="device">
<h3>{device.location}</h3>
<button className="op-btn">
<i className="fa-solid fa-video"></i>
View Stream
</button>
</div>
))}
</>
)}
</div>
</React.Fragment>
)}
</React.Fragment>
);
}
function ServerInfo() {
const [serverInfo, setServerInfo] = React.useState({});
// create an interval that fetchs the server info like i did in previous components
React.useEffect(() => {
const interval = setInterval(() => {
fetch("/api/server-info")
.then((res) => res.json())
.then((data) => {
console.log(data);
setServerInfo(data);
});
}, 5000);
return () => clearInterval(interval);
}, []);
return (
<>
<h1>Server Info</h1>
<h3>Server Uptime</h3>
<p>Server has been up for {serverInfo.time}</p>
<h3>Server Log file</h3>
<p>View the server log file <a style={{padding:"0"}} href="/logs">here</a></p>
</>
);
}
function SettingsScreen() {
return <h1>Settings</h1>;
}
var currentPage = 0;
function rerender() {
// delete any elements in the app div
ReactDOM.unmountComponentAtNode(document.getElementById("app"));
switch (currentPage) {
case 0:
ReactDOM.render(<DeviceWidgets />, document.getElementById("app"));
break;
case 1:
ReactDOM.render(<SettingsScreen />, document.getElementById("app"));
break;
case 2:
ReactDOM.render(<ServerInfo />, document.getElementById("app"));
break;
case 3:
ReactDOM.render(<FindNewDevice />, document.getElementById("app"));
break;
default:
ReactDOM.render(<DeviceWidgets />, document.getElementById("app"));
}
ReactDOM.render(<Sidebar />, document.querySelector(".sidebar"));
}
rerender();
</script>
</body>
</html>