| 2 |
raymond |
1 |
#pragma once
|
|
|
2 |
#include <Arduino.h>
|
|
|
3 |
|
|
|
4 |
inline const char login_htm[] PROGMEM = R"string_literal(
|
|
|
5 |
<!DOCTYPE html>
|
|
|
6 |
<html lang="en">
|
|
|
7 |
<head>
|
|
|
8 |
<meta charset="UTF-8">
|
|
|
9 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
10 |
<title>Login</title>
|
|
|
11 |
<style>
|
|
|
12 |
#about {color:lightgray};
|
|
|
13 |
body {background-color: #f5f5f5; font-family: Arial, sans-serif;}
|
|
|
14 |
header, footer {padding: 10px; background-color: #024c5b; color: #fff; width: 50%; text-align: center;}
|
|
|
15 |
header {margin-top: 40px;}
|
|
|
16 |
label {display: block; margin: 5px;}
|
|
|
17 |
input[type="text"], input[type="password"], input[type="email"] {width: 80%; padding: 8px; border: 1px solid #ddd; border-radius: 3px;}
|
|
|
18 |
button[type="submit"] {margin-top: 20px; width: 50%; min-width: 60px; padding: 10px; background-color: #607D8B; color: white; border: none; cursor: pointer;}
|
|
|
19 |
button[type="submit"]:hover, .nav {background-color: #16729F;}
|
|
|
20 |
.container {display: flex; flex-direction: column; align-items: center; min-height: 100vh;}
|
|
|
21 |
.custom-container {padding: 10px; width: 50%; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); background-color: #ffffff;}
|
|
|
22 |
.content {padding: 20px; display: flex; flex-direction: column;}
|
|
|
23 |
.center {text-align: center};
|
|
|
24 |
</style>
|
|
|
25 |
</head>
|
|
|
26 |
<body>
|
|
|
27 |
<div class="container">
|
|
|
28 |
<header>
|
|
|
29 |
<h1>ESP32 RFID Logs - Login</h1>
|
|
|
30 |
</header>
|
|
|
31 |
<div class="custom-container">
|
|
|
32 |
<div class="content">
|
|
|
33 |
<form id="loginForm" class="center">
|
|
|
34 |
<div>
|
|
|
35 |
<label for="username">Username:</label>
|
|
|
36 |
<input type="text" id="username" name="username" required>
|
|
|
37 |
</div>
|
|
|
38 |
<div>
|
|
|
39 |
<label for="password">Password:</label>
|
|
|
40 |
<input type="password" id="password" name="password" required>
|
|
|
41 |
</div>
|
|
|
42 |
<button type="submit">Login</button>
|
|
|
43 |
</form>
|
|
|
44 |
</div>
|
|
|
45 |
</div>
|
|
|
46 |
<footer class="footer">
|
|
|
47 |
<div class="has-text-centered">
|
|
|
48 |
<p> RFID Log © 2024. All rights reserved.</p>
|
|
|
49 |
<a id=about target=_blank rel=noopener href="https://github.com/cotestatnt/esp-fs-webserver/">Created with https://github.com/cotestatnt/esp-fs-webserver/</a>
|
|
|
50 |
</div>
|
|
|
51 |
</footer>
|
|
|
52 |
</div>
|
|
|
53 |
|
|
|
54 |
<script>
|
|
|
55 |
// To avoid sharing plain text between client and server,
|
|
|
56 |
// send SHA256 of password input text (based on https://geraintluff.github.io/sha256/)
|
|
|
57 |
function sha256(ascii) {
|
|
|
58 |
var rightRotate = (value, amount) => (value >>> amount) | (value << (32 - amount));
|
|
|
59 |
var mathPow = Math.pow, maxWord = mathPow(2, 32), lengthProperty = 'length', result = '', words = [], asciiBitLength = ascii[lengthProperty] * 8;
|
|
|
60 |
var hash = sha256.h = sha256.h || [], k = sha256.k = sha256.k || [], primeCounter = k[lengthProperty];
|
|
|
61 |
var isComposite = {};
|
|
|
62 |
for (var candidate = 2; primeCounter < 64; candidate++) {
|
|
|
63 |
if (!isComposite[candidate]) {
|
|
|
64 |
for (i = 0; i < 313; i += candidate) isComposite[i] = candidate;
|
|
|
65 |
hash[primeCounter] = (mathPow(candidate, .5) * maxWord) | 0;
|
|
|
66 |
k[primeCounter++] = (mathPow(candidate, 1 / 3) * maxWord) | 0;
|
|
|
67 |
}
|
|
|
68 |
}
|
|
|
69 |
ascii += '\x80';
|
|
|
70 |
while (ascii[lengthProperty] % 64 - 56) ascii += '\x00';
|
|
|
71 |
for (i = 0; i < ascii[lengthProperty]; i++) {
|
|
|
72 |
j = ascii.charCodeAt(i);
|
|
|
73 |
if (j >> 8) return;
|
|
|
74 |
words[i >> 2] |= j << ((3 - i) % 4) * 8;
|
|
|
75 |
}
|
|
|
76 |
words[words[lengthProperty]] = ((asciiBitLength / maxWord) | 0);
|
|
|
77 |
words[words[lengthProperty]] = (asciiBitLength);
|
|
|
78 |
for (j = 0; j < words[lengthProperty];) {
|
|
|
79 |
var w = words.slice(j, j += 16);
|
|
|
80 |
var oldHash = hash.slice(0, 8);
|
|
|
81 |
for (i = 0; i < 64; i++) {
|
|
|
82 |
var i2 = i + j, w15 = w[i - 15], w2 = w[i - 2];
|
|
|
83 |
var a = hash[0], e = hash[4];
|
|
|
84 |
var temp1 = hash[7] + (rightRotate(e, 6) ^ rightRotate(e, 11) ^ rightRotate(e, 25)) + ((e & hash[5]) ^ ((~e) & hash[6])) + k[i] +
|
|
|
85 |
(w[i] = (i < 16) ? w[i] : (w[i - 16] + (rightRotate(w15, 7) ^ rightRotate(w15, 18) ^ (w15 >>> 3)) + w[i - 7] +
|
|
|
86 |
(rightRotate(w2, 17) ^ rightRotate(w2, 19) ^ (w2 >>> 10))) | 0);
|
|
|
87 |
|
|
|
88 |
var temp2 = (rightRotate(a, 2) ^ rightRotate(a, 13) ^ rightRotate(a, 22)) + ((a & hash[1]) ^ (a & hash[2]) ^ (hash[1] & hash[2]));
|
|
|
89 |
hash = [(temp1 + temp2) | 0].concat(hash);
|
|
|
90 |
hash[4] = (hash[4] + temp1) | 0;
|
|
|
91 |
}
|
|
|
92 |
for (i = 0; i < 8; i++) hash[i] = (hash[i] + oldHash[i]) | 0;
|
|
|
93 |
}
|
|
|
94 |
for (i = 0; i < 8; i++)
|
|
|
95 |
for (j = 3; j + 1; j--)
|
|
|
96 |
result += ((hash[i] >> (j * 8)) & 255).toString(16).padStart(2, '0');
|
|
|
97 |
return result;
|
|
|
98 |
};
|
|
|
99 |
|
|
|
100 |
document.getElementById('loginForm').addEventListener('submit', function(event) {
|
|
|
101 |
event.preventDefault();
|
|
|
102 |
const hash = sha256(document.getElementById('password').value);
|
|
|
103 |
const username = document.getElementById('username').value;
|
|
|
104 |
var formData = new FormData();
|
|
|
105 |
formData.append("username", document.getElementById('username').value);
|
|
|
106 |
formData.append("hash", hash);
|
|
|
107 |
|
|
|
108 |
fetch('/rfid', {
|
|
|
109 |
method: 'POST',
|
|
|
110 |
body: formData
|
|
|
111 |
})
|
|
|
112 |
.then(response => {
|
|
|
113 |
if (response.ok) {
|
|
|
114 |
var url = '/rfid?username=' + username + '&hash=' + hash;
|
|
|
115 |
window.location.href = url;
|
|
|
116 |
} else {
|
|
|
117 |
alert("Wrong password");
|
|
|
118 |
};
|
|
|
119 |
});
|
|
|
120 |
});
|
|
|
121 |
</script>
|
|
|
122 |
</body>
|
|
|
123 |
</html>
|
|
|
124 |
)string_literal";
|
|
|
125 |
|
|
|
126 |
|
|
|
127 |
|
|
|
128 |
|
|
|
129 |
|
|
|
130 |
static const char index_htm[] PROGMEM = R"string_literal(
|
|
|
131 |
<!DOCTYPE html>
|
|
|
132 |
<html lang="en">
|
|
|
133 |
<head>
|
|
|
134 |
<meta charset="UTF-8">
|
|
|
135 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
136 |
<title>RFID Log</title>
|
|
|
137 |
<style>
|
|
|
138 |
#about {color:lightgray};
|
|
|
139 |
details,main,summary{display: block;}
|
|
|
140 |
body {background-color: #f5f5f5; margin: 0; font-family: Arial, sans-serif; margin-top: 20px;}
|
|
|
141 |
header, footer {padding: 10px; background-color: #024c5b; color: #fff; width: 90%; text-align: center;}
|
|
|
142 |
label {display: block; margin-bottom: 5px;}
|
|
|
143 |
input[type="text"], input[type="password"], input[type="email"] {width: 100%; padding: 8px; border: 1px solid #ddd; border-radius: 3px;}
|
|
|
144 |
button.submit {margin: 0 20px -10px 20px; width: 80%; min-width: 60px; padding: 10px; background-color: #607D8B; color: white; border: none; cursor: pointer;}
|
|
|
145 |
button.submit:hover {background-color: #16729F;}
|
|
|
146 |
button[disabled]:hover, button[disabled] {background-color: #ccc; color: #666;}
|
|
|
147 |
|
|
|
148 |
.container {display: flex; flex-direction: column; align-items: center; min-height: 100vh;}
|
|
|
149 |
.custom-container {padding: 10px; width: 90%; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); background-color: #ffffff;}
|
|
|
150 |
.sidebar {background-color: #f8f9fa; flex: 0 0 10%; transition: all 0.3s ease;}
|
|
|
151 |
.tab {padding: 10px 20px; cursor: pointer;}
|
|
|
152 |
.tab.active {background-color: #e9ecef;}
|
|
|
153 |
.content {flex-grow: 1; display: flex; justify-content: center; align-items: flex-start;}
|
|
|
154 |
.main-content {width: 100%; padding: 0 0 20px 20px; min-height: 70vh; }
|
|
|
155 |
.table-container {display: flex; flex-direction: column; border: 1px solid #ddd; border-radius: 5px; overflow: hidden;}
|
|
|
156 |
.table-header {display: flex; background-color: #f2f2f2; padding: 10px 15px; font-weight: bold; justify-content: space-around;}
|
|
|
157 |
.table-body {display: flex; flex-direction: column;}
|
|
|
158 |
.table-body > div {display: flex; padding: 4px 10px; border-bottom: 1px solid #ddd; justify-content: space-around;}
|
|
|
159 |
.table-body > div:nth-child(even) {background-color: #f9f9f9;}
|
|
|
160 |
.table-body > div.selected {background-color: #d3d3d3; border: 1px solid orange; color: blue;}
|
|
|
161 |
.section {position: relative;}
|
|
|
162 |
.collapse-container {margin-bottom: 20px;}
|
|
|
163 |
.collapse-content {display: none; padding: 10px; border: 1px solid #ddd; border-radius: 5px; margin-bottom: 20px;}
|
|
|
164 |
.collapse-content.show {display: block;}
|
|
|
165 |
.form-row {display: flex; flex-wrap: wrap; margin-bottom: 10px; align-items: flex-end;}
|
|
|
166 |
.form-column {flex: 1; margin: 0 20px 0 20px;}
|
|
|
167 |
.form-column:last-child { margin-right: 40px; }
|
|
|
168 |
.but-row {display: flex; margin: 20px 0 10px 0;}
|
|
|
169 |
.button:hover {cursor: pointer; background-color: #16729F;}
|
|
|
170 |
.button:active {transform: scale(0.8);}
|
|
|
171 |
|
|
|
172 |
.floating {position: absolute; float: right; z-index: 1; right: -4px;}
|
|
|
173 |
.floating.top {top: -8px; }
|
|
|
174 |
.floating.bottom {bottom: -8px;}
|
|
|
175 |
.floating.inline {position: sticky;}
|
|
|
176 |
.floating .button {width: 24px; height: 24px; border-radius: 50%; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.8);
|
|
|
177 |
border: 1px solid transparent; font-family: monospace; font-size: larger; color: cornsilk;}
|
|
|
178 |
|
|
|
179 |
.green {background-color: #008000ba;}
|
|
|
180 |
.blue {background-color: #607D8B;}
|
|
|
181 |
.w05 {width: 5%;} .w10 {width: 10%;} .w20 {width: 20%;} .w30 {width: 30%;}
|
|
|
182 |
|
|
|
183 |
.d-modal{
|
|
|
184 |
width: -webkit-fill-available; border-radius: 10px; border-style: solid; border-width: 1px; border-color: #3333336e;
|
|
|
185 |
box-shadow: rgba(0, 0, 0, 0.24) 0 3px 8px;left: 50%; position: absolute; top: 60%; transform: translate(-50%, -50%);
|
|
|
186 |
flex-direction: column; background-color: hsl(199.53deg 18.3% 46.08% / 90%); z-index: 999;
|
|
|
187 |
}
|
|
|
188 |
.d-modal-title{color:#111827;padding:1.5em;position:relative;width:calc(100% - 4.5em);}
|
|
|
189 |
.d-modal-content{border-top:1px solid #e0e0e0;padding:1.5em;overflow:auto;}
|
|
|
190 |
.btn{display:inline-flex ;padding:10px 15px; background-color:#2E8BC0;color:#fff;border:0;cursor:pointer;min-width:40%;border-radius:5px;justify-content:center;}
|
|
|
191 |
.btn:hover{ filter: brightness(85%);}
|
|
|
192 |
.btn-bar{display:flex;padding:20px;justify-content:center;flex-wrap:wrap;grid-column-gap:30px;grid-row-gap:20px; order:998;}
|
|
|
193 |
|
|
|
194 |
/*Must be the last rule*/
|
|
|
195 |
.hide {display: none; }
|
|
|
196 |
.loader { width: 120px; height: 120px; border: 24px solid #dedbdba8; border-top: 24px solid #3498dbe3; border-radius: 50%;
|
|
|
197 |
animation: spin 2s linear infinite; position: fixed; top: 50%; left: 50%; margin: -60px; display: none;}
|
|
|
198 |
@keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }
|
|
|
199 |
|
|
|
200 |
</style>
|
|
|
201 |
</head>
|
|
|
202 |
<body>
|
|
|
203 |
<div class="loader" id="loader"></div>
|
|
|
204 |
|
|
|
205 |
<details id=modal-message>
|
|
|
206 |
<summary style="display: block"></summary>
|
|
|
207 |
<div class=d-modal>
|
|
|
208 |
<div class=d-modal-title><h2 id=message-title>t</h2></div>
|
|
|
209 |
<div class=d-modal-content><p id=message-body></p></div>
|
|
|
210 |
<div class=btn-bar>
|
|
|
211 |
<a id=ok-modal class="btn hide" onclick=closeModal(true)><span>OK</span></a>
|
|
|
212 |
<a id=close-modal class="btn" onclick=closeModal(false)><span>Close</span></a>
|
|
|
213 |
</div>
|
|
|
214 |
</div>
|
|
|
215 |
</details>
|
|
|
216 |
|
|
|
217 |
<div id="main-box" class="container">
|
|
|
218 |
<header>
|
|
|
219 |
<h1 class="title is-3">ESP32 RFID Logs</h1>
|
|
|
220 |
</header>
|
|
|
221 |
<div class="custom-container">
|
|
|
222 |
<div class="content">
|
|
|
223 |
<div class="sidebar">
|
|
|
224 |
<div class="tab" id="logsTab" data-target="logsContent">Logs</div>
|
|
|
225 |
<div class="tab" id="usersTab" data-target="usersContent">Users</div>
|
|
|
226 |
<div class="tab"><a id="setup" style="color: inherit; text-decoration: none;" href="#" disabled>Setup</a></div>
|
|
|
227 |
</div>
|
|
|
228 |
<div class="main-content">
|
|
|
229 |
<div id="logsContent" class="section">
|
|
|
230 |
|
|
|
231 |
<div class="floating top">
|
|
|
232 |
<button class="button green" onclick="toggleCollapse('collapse-logs')"><b id='toggle-logs'>+</b></button>
|
|
|
233 |
</div>
|
|
|
234 |
|
|
|
235 |
<div class="floating bottom">
|
|
|
236 |
<button class="button blue" id="prev-log"><b><</b></button>
|
|
|
237 |
<button class="button blue" id="next-log"><b>></b></button>
|
|
|
238 |
</div>
|
|
|
239 |
|
|
|
240 |
<div class="collapsible">
|
|
|
241 |
<div id="collapse-logs" class="collapse-content">
|
|
|
242 |
<div id="insertForm">
|
|
|
243 |
<div class="form-row">
|
|
|
244 |
<div class="form-column">
|
|
|
245 |
<input type="datetime-local" id="start">
|
|
|
246 |
<input type="datetime-local" id="end">
|
|
|
247 |
</div>
|
|
|
248 |
<div class="form-column">
|
|
|
249 |
<label for="username-log">Username:</label>
|
|
|
250 |
<input type="text" id="username-log" name="username-log" placeholder="Username">
|
|
|
251 |
</div>
|
|
|
252 |
<div class="form-column">
|
|
|
253 |
<label for="reader-log">Reader:</label>
|
|
|
254 |
<input type="text" id="reader-log" name="reader-log" placeholder="Reader number">
|
|
|
255 |
</div>
|
|
|
256 |
|
|
|
257 |
<div class="but-row">
|
|
|
258 |
<button class="submit" id="filter-log">Filter</button>
|
|
|
259 |
<button class="submit" id="export-log">Export</button>
|
|
|
260 |
</div>
|
|
|
261 |
</div>
|
|
|
262 |
</div>
|
|
|
263 |
</div>
|
|
|
264 |
</div>
|
|
|
265 |
|
|
|
266 |
<div class="table-container">
|
|
|
267 |
<div class="table-header">
|
|
|
268 |
<div class="w05">ID</div>
|
|
|
269 |
<div class="w30">Timestamp</div>
|
|
|
270 |
<div class="w30">Username</div>
|
|
|
271 |
<div class="w10">Tag Code</div>
|
|
|
272 |
<div class="w10">Reader</div>
|
|
|
273 |
</div>
|
|
|
274 |
<div id="logsTable" class="table-body">
|
|
|
275 |
<!-- //// -->
|
|
|
276 |
</div>
|
|
|
277 |
</div>
|
|
|
278 |
</div>
|
|
|
279 |
|
|
|
280 |
<div id="usersContent" class="section hide">
|
|
|
281 |
<div class="floating top">
|
|
|
282 |
<button class="button green" id="handle-users" onclick="toggleCollapse('collapse-user')" disabled><b id='toggle-user'>+</b></button>
|
|
|
283 |
</div>
|
|
|
284 |
<div class="collapsible">
|
|
|
285 |
<div id="collapse-user" class="collapse-content">
|
|
|
286 |
<!-- Form per l'inserimento di un record nella tabella -->
|
|
|
287 |
<div id="insertForm">
|
|
|
288 |
<div class="form-row">
|
|
|
289 |
<div class="form-column">
|
|
|
290 |
<label for="tagCode">Tag Code:</label>
|
|
|
291 |
<span style="display: inline-flex;">
|
|
|
292 |
<input type="text" id="tagCode" name="tagCode" placeholder="Tag Code">
|
|
|
293 |
|
|
|
294 |
<div class="floating inline">
|
|
|
295 |
<button id="get-tag" class="button blue" title="Read RFID tag code"><b>@</b></button>
|
|
|
296 |
|
|
|
297 |
</div>
|
|
|
298 |
</span>
|
|
|
299 |
</div>
|
|
|
300 |
<div class="form-column">
|
|
|
301 |
<label for="name">Name:</label>
|
|
|
302 |
<input type="text" id="name" name="name" placeholder="Name">
|
|
|
303 |
</div>
|
|
|
304 |
<div class="form-column">
|
|
|
305 |
<label for="level">Level:</label>
|
|
|
306 |
<input type="text" id="level" name="level" placeholder="Level">
|
|
|
307 |
</div>
|
|
|
308 |
|
|
|
309 |
</div>
|
|
|
310 |
<div class="form-row">
|
|
|
311 |
<div class="form-column">
|
|
|
312 |
<label for="username">Username:</label>
|
|
|
313 |
<input type="text" id="username" name="username" placeholder="Username">
|
|
|
314 |
</div>
|
|
|
315 |
<div class="form-column">
|
|
|
316 |
<label for="password">Password:</label>
|
|
|
317 |
<input type="password" id="password" name="password" placeholder="password">
|
|
|
318 |
</div>
|
|
|
319 |
<div class="form-column">
|
|
|
320 |
<label for="email">Email:</label>
|
|
|
321 |
<input type="email" id="email" name="email" placeholder="Email">
|
|
|
322 |
</div>
|
|
|
323 |
</div>
|
|
|
324 |
|
|
|
325 |
<div class="but-row">
|
|
|
326 |
<button class="submit" id="add-user">Insert</button>
|
|
|
327 |
<button class="submit" id="delete-user" disabled>Delete</button>
|
|
|
328 |
</div>
|
|
|
329 |
</div>
|
|
|
330 |
</div>
|
|
|
331 |
</div>
|
|
|
332 |
|
|
|
333 |
<div class="table-container">
|
|
|
334 |
<div class="table-header">
|
|
|
335 |
<div class="w05">ID</div>
|
|
|
336 |
<div class="w20">Username</div>
|
|
|
337 |
<div class="w05">Role</div>
|
|
|
338 |
<div class="w30">Name</div>
|
|
|
339 |
<div class="w30">Email</div>
|
|
|
340 |
<div class="w10">Tag Code</div>
|
|
|
341 |
</div>
|
|
|
342 |
<div id="usersTable" class="table-body">
|
|
|
343 |
<!-- //// -->
|
|
|
344 |
</div>
|
|
|
345 |
</div>
|
|
|
346 |
</div>
|
|
|
347 |
|
|
|
348 |
</div>
|
|
|
349 |
</div>
|
|
|
350 |
</div>
|
|
|
351 |
<footer class="footer">
|
|
|
352 |
<div class="has-text-centered">
|
|
|
353 |
<p> RFID Log © 2024. All rights reserved.</p>
|
|
|
354 |
<a id=about target=_blank rel=noopener href="https://github.com/cotestatnt/esp-fs-webserver/">Created with https://github.com/cotestatnt/esp-fs-webserver/</a>
|
|
|
355 |
</div>
|
|
|
356 |
</footer>
|
|
|
357 |
</div>
|
|
|
358 |
|
|
|
359 |
<script>
|
|
|
360 |
var userLevel = 0;
|
|
|
361 |
|
|
|
362 |
// Callback function called on modal box close
|
|
|
363 |
var closeCb = function(){};
|
|
|
364 |
|
|
|
365 |
// ID Element selector shorthands
|
|
|
366 |
var $ = function(el) {
|
|
|
367 |
return document.getElementById(el);
|
|
|
368 |
};
|
|
|
369 |
|
|
|
370 |
// Switch active page
|
|
|
371 |
function tabClick(el) {
|
|
|
372 |
const tabs = document.querySelectorAll('.tab');
|
|
|
373 |
const sections = document.querySelectorAll('.section');
|
|
|
374 |
tabs.forEach(t => t.classList.remove('active'));
|
|
|
375 |
sections.forEach(s => s.classList.add('hide'));
|
|
|
376 |
const target = this.dataset.target;
|
|
|
377 |
$(target).classList.remove('hide');
|
|
|
378 |
this.classList.remove('hide');
|
|
|
379 |
this.classList.add('active');
|
|
|
380 |
}
|
|
|
381 |
|
|
|
382 |
// Toggle the collapsible user section
|
|
|
383 |
function toggleCollapse(id, keep) {
|
|
|
384 |
if (keep)
|
|
|
385 |
$(id).classList.add('show');
|
|
|
386 |
else
|
|
|
387 |
$(id).classList.toggle('show');
|
|
|
388 |
|
|
|
389 |
const allRows = document.querySelectorAll('.table-body > div');
|
|
|
390 |
allRows.forEach(row => row.classList.remove('selected'));
|
|
|
391 |
const allInput = $('insertForm').querySelectorAll('input');
|
|
|
392 |
allInput.forEach(inp => inp.value = '');
|
|
|
393 |
$('add-user').innerHTML = 'Insert';
|
|
|
394 |
$('delete-user').disabled = true;
|
|
|
395 |
$('add-user').disabled = true;
|
|
|
396 |
const btnId = id.split('-')[1];
|
|
|
397 |
$('toggle-' + btnId).innerHTML = $(id).classList.contains('show') ? '-' : '+';
|
|
|
398 |
}
|
|
|
399 |
|
|
|
400 |
// Get logs records
|
|
|
401 |
function getLogs(filter) {
|
|
|
402 |
var formData = new FormData();
|
|
|
403 |
if (filter != undefined)
|
|
|
404 |
formData.append("filter", filter);
|
|
|
405 |
else
|
|
|
406 |
formData.append("filter", "");
|
|
|
407 |
const option = {
|
|
|
408 |
method: 'POST',
|
|
|
409 |
body: formData
|
|
|
410 |
};
|
|
|
411 |
|
|
|
412 |
const logs = $('logsTable');
|
|
|
413 |
fetch('/logs', option)
|
|
|
414 |
.then(response => {
|
|
|
415 |
if (!response.ok) {
|
|
|
416 |
throw new Error('Requesr error');
|
|
|
417 |
}
|
|
|
418 |
return response.json();
|
|
|
419 |
})
|
|
|
420 |
.then(data => {
|
|
|
421 |
logs.innerHTML = '';
|
|
|
422 |
data.forEach(log => {
|
|
|
423 |
const logEntry = document.createElement('div');
|
|
|
424 |
logEntry.innerHTML =
|
|
|
425 |
`<div class="w05">${log.id}</div>
|
|
|
426 |
<div class="w30">${new Date(log.epochTime * 1000).toLocaleString()}</div>
|
|
|
427 |
<div class="w30">${log.username}</div>
|
|
|
428 |
<div class="w10">${log.tagCode}</div>
|
|
|
429 |
<div class="w10">${log.readerID}</div>`;
|
|
|
430 |
logs.appendChild(logEntry);
|
|
|
431 |
});
|
|
|
432 |
$('loader').style.display = "none";
|
|
|
433 |
})
|
|
|
434 |
.catch(error => {
|
|
|
435 |
console.error('Error:', error);
|
|
|
436 |
logs.innerHTML = '';
|
|
|
437 |
$('loader').style.display = "none";
|
|
|
438 |
});
|
|
|
439 |
}
|
|
|
440 |
|
|
|
441 |
// Read the RFID code for current user
|
|
|
442 |
function getTagCode() {
|
|
|
443 |
fetch('/getCode')
|
|
|
444 |
.then(response => {
|
|
|
445 |
if (!response.ok) {
|
|
|
446 |
throw new Error('Request error');
|
|
|
447 |
}
|
|
|
448 |
return response.json();
|
|
|
449 |
})
|
|
|
450 |
.then(data => {
|
|
|
451 |
$('tagCode').value = data.tagCode;
|
|
|
452 |
$('add-user').disabled = false;
|
|
|
453 |
})
|
|
|
454 |
.catch(error => {
|
|
|
455 |
alert('Error:' + error);
|
|
|
456 |
});
|
|
|
457 |
}
|
|
|
458 |
|
|
|
459 |
// Get list of registered users
|
|
|
460 |
function getUsers() {
|
|
|
461 |
const usersTable = $('usersTable');
|
|
|
462 |
fetch('/users')
|
|
|
463 |
.then(response => {
|
|
|
464 |
if (!response.ok) {
|
|
|
465 |
throw new Error('Requesr error');
|
|
|
466 |
}
|
|
|
467 |
return response.json();
|
|
|
468 |
})
|
|
|
469 |
.then(data => {
|
|
|
470 |
data.forEach(user => {
|
|
|
471 |
const userEntry = document.createElement('div');
|
|
|
472 |
userEntry.innerHTML =
|
|
|
473 |
`<div class="w05" id="user">${user.id}</div>
|
|
|
474 |
<div class="w20" id="username">${user.username}</div>
|
|
|
475 |
<div class="w05" id="level">${user.level}</div>
|
|
|
476 |
<div class="w30" id="name">${user.name}</div>
|
|
|
477 |
<div class="w30" id="email">${user.email}</div>
|
|
|
478 |
<div class="w10" id="tagCode">${user.tagCode}</div>`;
|
|
|
479 |
usersTable.appendChild(userEntry);
|
|
|
480 |
|
|
|
481 |
userEntry.addEventListener('click', function(ev) {
|
|
|
482 |
const allRows = document.querySelectorAll('.table-body > div');
|
|
|
483 |
allRows.forEach(row => row.classList.remove('selected'));
|
|
|
484 |
|
|
|
485 |
if(userLevel >= 5) {
|
|
|
486 |
toggleCollapse('collapse-user', true);
|
|
|
487 |
this.classList.add('selected');
|
|
|
488 |
const cols = Array.from(ev.target.parentNode.querySelectorAll('div')).map(el => {
|
|
|
489 |
return {
|
|
|
490 |
id: el.id,
|
|
|
491 |
value: el.innerHTML
|
|
|
492 |
};
|
|
|
493 |
});
|
|
|
494 |
if (cols.length === 6) {
|
|
|
495 |
cols.forEach(item => {
|
|
|
496 |
$(item.id).value = item.value;
|
|
|
497 |
$(item.id).addEventListener('input', function() {
|
|
|
498 |
$('add-user').disabled = false;
|
|
|
499 |
});
|
|
|
500 |
});
|
|
|
501 |
}
|
|
|
502 |
$('delete-user').disabled = false;
|
|
|
503 |
$('add-user').innerHTML = 'Update';
|
|
|
504 |
}
|
|
|
505 |
});
|
|
|
506 |
});
|
|
|
507 |
})
|
|
|
508 |
.catch(error => {
|
|
|
509 |
console.error('Error:', error);
|
|
|
510 |
});
|
|
|
511 |
}
|
|
|
512 |
|
|
|
513 |
// Send command to MCU before the readings in order to handle properly logs record
|
|
|
514 |
function readTagCode() {
|
|
|
515 |
fetch('/waitCode')
|
|
|
516 |
.then(response => {
|
|
|
517 |
openModal('Read new TAG', "<br>Please hold your tag close to the RFID reader", getTagCode);
|
|
|
518 |
})
|
|
|
519 |
}
|
|
|
520 |
|
|
|
521 |
// Insert or update a new user record
|
|
|
522 |
function sendUserForm(url) {
|
|
|
523 |
var formData = new FormData();
|
|
|
524 |
formData.append("username", $('username').value);
|
|
|
525 |
formData.append("password", $('password').value);
|
|
|
526 |
formData.append("name", $('name').value);
|
|
|
527 |
formData.append("email", $('email').value);
|
|
|
528 |
formData.append("tagCode", $('tagCode').value);
|
|
|
529 |
formData.append("level", $('level').value);
|
|
|
530 |
const option = {
|
|
|
531 |
method: 'POST',
|
|
|
532 |
body: formData
|
|
|
533 |
};
|
|
|
534 |
fetch(url, option)
|
|
|
535 |
.then(response => {
|
|
|
536 |
if (!response.ok) {
|
|
|
537 |
throw new Error('Request error');
|
|
|
538 |
}
|
|
|
539 |
return response.text();
|
|
|
540 |
})
|
|
|
541 |
.then(result => {
|
|
|
542 |
openModal('Users', "<br>New record inserted or updated");
|
|
|
543 |
})
|
|
|
544 |
.catch(error => {
|
|
|
545 |
openModal('Error', error);
|
|
|
546 |
});
|
|
|
547 |
}
|
|
|
548 |
|
|
|
549 |
// Send command to MCU before the readings in order to handle properly logs record
|
|
|
550 |
function deleteUser() {
|
|
|
551 |
$('delete-user').disabled = true;
|
|
|
552 |
sendUserForm('/delUser');
|
|
|
553 |
}
|
|
|
554 |
|
|
|
555 |
// Show a message, if fn != undefinded run as callback on OK button press
|
|
|
556 |
function openModal(title, msg, fn) {
|
|
|
557 |
$('message-title').innerHTML = title;
|
|
|
558 |
$('message-body').innerHTML = msg;
|
|
|
559 |
$('modal-message').open = true;
|
|
|
560 |
$('main-box').style.filter = "blur(3px)";
|
|
|
561 |
if (typeof fn != 'undefined') {
|
|
|
562 |
closeCb = fn;
|
|
|
563 |
$('ok-modal').classList.remove('hide');
|
|
|
564 |
}
|
|
|
565 |
else
|
|
|
566 |
$('ok-modal').classList.add('hide');
|
|
|
567 |
}
|
|
|
568 |
|
|
|
569 |
// Clode modal box
|
|
|
570 |
function closeModal(do_cb) {
|
|
|
571 |
$('modal-message').open = false;
|
|
|
572 |
$('main-box').style.filter = "";
|
|
|
573 |
if (typeof closeCb != 'undefined' && do_cb)
|
|
|
574 |
closeCb();
|
|
|
575 |
}
|
|
|
576 |
|
|
|
577 |
function getRowTimestamp(id) {
|
|
|
578 |
// get last row
|
|
|
579 |
var divs = Array.from($(id).querySelectorAll('div:not([class^="w"])'));
|
|
|
580 |
//Get timestamp value
|
|
|
581 |
const [day, month, year, hour, minute, second] = Array.from(divs[divs.length - 1].querySelectorAll('div'))[1].innerHTML.split(/[\s,:\/]+/);
|
|
|
582 |
return (new Date(year, month - 1, day, hour, minute, second)).getTime()/1000;
|
|
|
583 |
}
|
|
|
584 |
|
|
|
585 |
function customFilter() {
|
|
|
586 |
let filter = ' WHERE ';
|
|
|
587 |
function includeAND(rule) {
|
|
|
588 |
return (filter != ' WHERE ') ? (' AND ' + rule) : rule;
|
|
|
589 |
}
|
|
|
590 |
if ( $('start').value)
|
|
|
591 |
filter += includeAND('epoch >= ' + (new Date($('start').value)) / 1000);
|
|
|
592 |
if ( $('end').value)
|
|
|
593 |
filter += includeAND('epoch <= ' + (new Date($('end').value)) / 1000);
|
|
|
594 |
if ( $('reader-log').value)
|
|
|
595 |
filter += includeAND('reader = ' + $('reader-log').value);
|
|
|
596 |
if ( $('username-log').value)
|
|
|
597 |
filter += includeAND("username = '" + $('username-log').value + "'");
|
|
|
598 |
getLogs(filter);
|
|
|
599 |
}
|
|
|
600 |
|
|
|
601 |
function exportCSV() {
|
|
|
602 |
// Create CSV data
|
|
|
603 |
const divs = Array.from($('logsTable').querySelectorAll('div:not([class^="w"])'));
|
|
|
604 |
let table = [];
|
|
|
605 |
|
|
|
606 |
divs.forEach(item => {
|
|
|
607 |
const cols = Array.from(item.querySelectorAll('div'));
|
|
|
608 |
let row = [];
|
|
|
609 |
cols.forEach(field => {
|
|
|
610 |
row.push(field.innerHTML)
|
|
|
611 |
});
|
|
|
612 |
table.push(row);
|
|
|
613 |
})
|
|
|
614 |
|
|
|
615 |
let csvString = 'id, timestamp, username, tagCode, reader\n'
|
|
|
616 |
for (let i = 1; i < table.length; i++) {
|
|
|
617 |
csvString += table[i].join(', ') + '\n';
|
|
|
618 |
}
|
|
|
619 |
|
|
|
620 |
// Download as CSV file
|
|
|
621 |
const blob = new Blob([csvString], { type: 'text/csv' });
|
|
|
622 |
const url = window.URL.createObjectURL(blob);
|
|
|
623 |
const a = document.createElement('a');
|
|
|
624 |
a.href = url;
|
|
|
625 |
a.download = 'data.csv';
|
|
|
626 |
document.body.appendChild(a);
|
|
|
627 |
a.click();
|
|
|
628 |
document.body.removeChild(a);
|
|
|
629 |
window.URL.revokeObjectURL(url);
|
|
|
630 |
}
|
|
|
631 |
|
|
|
632 |
function getUserLevel() {
|
|
|
633 |
fetch('/userLevel?username=cotestatnt', {
|
|
|
634 |
method: 'GET'
|
|
|
635 |
})
|
|
|
636 |
.then(response => {
|
|
|
637 |
console.log(response);
|
|
|
638 |
})
|
|
|
639 |
}
|
|
|
640 |
|
|
|
641 |
// Event listeners
|
|
|
642 |
$('add-user').addEventListener('click', function(event) {
|
|
|
643 |
event.preventDefault();
|
|
|
644 |
$('add-user').disabled = true;
|
|
|
645 |
$('delete-user').disabled = true;
|
|
|
646 |
sendUserForm('/addUser');
|
|
|
647 |
});
|
|
|
648 |
|
|
|
649 |
$('delete-user').addEventListener('click', function(event) {
|
|
|
650 |
event.preventDefault();
|
|
|
651 |
openModal('Delete user', "Do you really want to drop current user record?", deleteUser);
|
|
|
652 |
});
|
|
|
653 |
|
|
|
654 |
document.addEventListener('DOMContentLoaded', function() {
|
|
|
655 |
$('loader').style.display = "block";
|
|
|
656 |
getUsers();
|
|
|
657 |
getLogs();
|
|
|
658 |
|
|
|
659 |
$('logsTab').addEventListener('click', tabClick);
|
|
|
660 |
$('usersTab').addEventListener('click', tabClick);
|
|
|
661 |
$('get-tag').addEventListener('click', readTagCode);
|
|
|
662 |
$('filter-log').addEventListener('click', customFilter);
|
|
|
663 |
$('export-log').addEventListener('click', exportCSV);
|
|
|
664 |
|
|
|
665 |
$('next-log').addEventListener('click', function(){
|
|
|
666 |
$('loader').style.display = "block";
|
|
|
667 |
var ts = getRowTimestamp('logsTable');
|
|
|
668 |
var filter = ` WHERE epoch <= ${ts}`;
|
|
|
669 |
getLogs(filter);
|
|
|
670 |
});
|
|
|
671 |
|
|
|
672 |
$('prev-log').addEventListener('click', function(){
|
|
|
673 |
$('loader').style.display = "block";
|
|
|
674 |
var ts = getRowTimestamp('logsTable');
|
|
|
675 |
var filter = ` WHERE epoch >= ${ts}`;
|
|
|
676 |
getLogs(filter);
|
|
|
677 |
});
|
|
|
678 |
|
|
|
679 |
// Check if user has admin level (>= 5)
|
|
|
680 |
var usernameValue = document.cookie.replace(/(?:(?:^|.*;\s*)username\s*=\s*([^;]*).*$)|^.*$/, "$1");
|
|
|
681 |
userLevel = usernameValue.split(',')[1];
|
|
|
682 |
if(userLevel >= 5) {
|
|
|
683 |
console.log(usernameValue.split(',')[0], "is admin");
|
|
|
684 |
$('handle-users').disabled = false;
|
|
|
685 |
$('setup').href = '/setup';
|
|
|
686 |
}
|
|
|
687 |
|
|
|
688 |
});
|
|
|
689 |
|
|
|
690 |
</script>
|
|
|
691 |
</body>
|
|
|
692 |
</html>
|
|
|
693 |
)string_literal";
|