-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdashboard.php
285 lines (261 loc) · 14.9 KB
/
dashboard.php
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
<?php
require_once('utils.php');
session_start();
// Obtem credencias do ficheiro
$credentials = analyze_credentials('../credentials.txt');
// Verifica se algum utilizador já se encontra logado
if (!isset($_SESSION['username']) or !is_user($_SESSION['username'], $credentials)) {
header("Location: index.php");
}
// Obter crecenciais referentes ao utilizador logado
$user = get_user($_SESSION['username'], $credentials);
?>
<!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">
<link rel="icon" type="image/x-icon" href="/images/favicon.ico">
<title>Dashboard</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script defer src="https://use.fontawesome.com/releases/v5.0.13/js/solid.js" integrity="sha384-tzzSw1/Vo+0N5UhStP3bvwWPq+uvzCMfrN1fEFe+xBmv1C/AtVX5K0uZtmcHitFZ" crossorigin="anonymous"></script>
<script defer src="https://use.fontawesome.com/releases/v5.0.13/js/fontawesome.js" integrity="sha384-6OIrr52G08NpOFSZdxxz1xdNSndlD4vdcf/q2myIUVO0VsqaGHJsB0RaBE01VTOY" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
<!-- Import Robot Font -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">
<link rel="stylesheet" href="/styles/style.css">
</head>
<body>
<!-- Topbar -->
<nav id="topbar" class="navbar navbar-expand-lg bg-body-tertiary">
<div class="container-fluid">
<a id="sidebarCollapse" class="btn btn-secondary">
<i class="fas fa-bars"></i>
</a>
<h4 class="text-white">Dashboard</h4>
</div>
</nav>
<div class="wrapper">
<!-- Sidebar -->
<nav id="sidebar" class="d-flex flex-column">
<div class="sidebar-header">
<h4>Parque de Estacionamento</h4>
</div>
<ul class="flex-column mb-auto components">
<li>
<a href="dashboard.php" class="active">Dashboard</a>
</li>
</ul>
<ul class="flex-column components">
<li>
<a href="logout.php">Log out</a>
</li>
</ul>
</nav>
<!-- Page Content -->
<div id="content">
<div class="container pt-2">
<div class="row" id="files-cards">
<div class="col-sm-4">
<div class="card text-center mb-3">
<div class="card-header fw-bold sensor">Webcam</div>
<img id="webcam-image" src="" alt="Sem dados de webcam‽" class="card-image-top img-fluid">
<div class="card-body">
<small><b>Última atualização:</b> <span id="webcam-hora"></span></small>
<?php echo ($user[2] == 'admin' ? '<a href="historico.php?nome=webcam" class="text-primary">Histórico</a>' : '') ?>
</div>
</div>
</div>
<!-- Os outros sensores/atuadores vão aparecer aqui -->
</div>
</div>
</div>
</div>
<script>
// Abrir/Fechar sidebar
document.getElementById('sidebarCollapse').addEventListener('click', function() {
let sidebar = document.getElementById('sidebar');
sidebar.classList.toggle('active');
});
// Inicializa os sensores/atuadores
function setFilesCards() {
// Obtem a de sensores/atuadores da API
const getFiles = async () => {
const response = await fetch('/api/api.php?options');
// check if the response is ok
if (response.ok) {
const textValue = await response.text();
let files = textValue.trim().split("\r\n").sort(); // Separa cada nome de sensor/atuador em strings diferentes
files.forEach(file => {
const getFileData = async () => {
// Obtem todos os dados sobre os sensores/atuadores
const responseNome = await fetch('/api/api.php?nome=' + file + '&tipo=desc');
const responseInfo = await fetch('/api/api.php?nome=' + file + '&tipo=info');
const responseHora = await fetch('/api/api.php?nome=' + file + '&tipo=hora');
const responseSimb = await fetch('/api/api.php?nome=' + file + '&tipo=simbolo');
const responseValor = await fetch('/api/api.php?nome=' + file);
// check if the response is ok
if (responseNome.ok && responseInfo.ok) {
const nome = await responseNome.text();
const info = await responseInfo.text();
const valor = await responseValor.text();
const hora = await responseHora.text();
const simb = await responseSimb.text();
// Configura automaticamente o nome da imagem para cada sensor/atuador
let primeiro_nome = nome.split(' ')[0];
let switc = '';
let imagem = '';
if (info === 'atuador') {
switc = `<div class="form-check form-switch">
<label for="${file}-switch">Off/On</label>
<input id="${file}-switch" name="${file}" type="checkbox" role="switch" class="form-check-input" ${(valor === 'On' ? 'checked' : '')}>
</div>`;
imagem = (primeiro_nome + '_' + valor).toLowerCase();
} else {
imagem = primeiro_nome.toLowerCase();
}
// Adiciona o novo card do sensor/atuador na dashboard com os dados
let filesCards = document.getElementById("files-cards");
filesCards.innerHTML += `<div class="col-sm-4">
<div class="card text-center mb-3">
<div class="card-header fw-bold sensor">${nome}</div>
<img id="${file}-image" src="images/${imagem}.png" alt="${nome}" class="card-image-top">
<div class="card-body">
<h5 class="card-title mb-3"><span id="${file}">${valor}</span> ${simb}</h5>
<small><b>Última atualização:</b> <span id="${file}-hora">${hora}</span></small>
<?php echo ($user[2] == 'admin' ? "<a href=\"historico.php?nome=\${file}\" class=\"text-primary\">Histórico</a>" : '') ?>
${switc}
</div>
</div>
</div>`;
setInterval(updateFromAPI, 5000, file, nome, (info === 'atuador' ? true : false));
}
// Define um evento de click para todos os botões switch
document.querySelectorAll('.form-check-input').forEach(element => {
element.addEventListener('click', async function() {
let value = 'Off';
// Se o switch for ativado define o valor a enviar como ligado
if (this.checked) {
value = 'On';
}
// Envia o POST para atualizar o estado do atuador através da API
const sendPost = async () => {
const response = await fetch('/api/api.php', {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: "nome=" + this.name + "&valor=" + value,
});
if (response.ok) {
const textValue = await response.text();
const valores = textValue.split(';');
updateFromAPI(valores[0], valores[1], valores[2]);
} else {
// Caso ocorra algum erro retorna o botão switch ao estado original
this.checked = false;
}
}
sendPost().catch(error => console.error(error));
});
});
}
getFileData().catch(error => console.error(error));
});
}
}
getFiles().catch(error => console.error(error));
}
setFilesCards();
function updateFromAPI(id, nome, isAtuador = false) {
// Obtem a data e hora da ultima atualização do sensor/atuador na API
let spanHora = document.getElementById(id + "-hora");
const getHora = async () => {
const response = await fetch('/api/api.php?nome=' + id + "&tipo=hora");
// check if the response is ok
if (response.ok) {
const textValue = await response.text();
spanHora.innerHTML = textValue; // Atualiza a hora na pagina
}
}
getHora().catch(error => console.error(error));
// Obtem o valor do sensor/atuador
let spanValor = document.getElementById(id)
const getValor = async () => {
const response = await fetch('/api/api.php?nome=' + id);
// check if the response is ok
if (response.ok) {
const textValue = await response.text();
spanValor.innerHTML = textValue; // Atualiza o estado na pagina
// Atualiza o botão switch caso seja um atuador
let inputSwitch = document.getElementById(id + '-switch');
if (isAtuador) {
if (textValue === "On") {
inputSwitch.checked = true;
} else {
inputSwitch.checked = false;
}
}
// Atualiza a imagem
// Cria o caminha para obter a imagem
let imageName = '';
let primeiroNome = nome.split(" ")[0];
if (isAtuador && textValue != null) { // Configura o nome caso seja um atuador
imageName = (primeiroNome + "_" + textValue).toLowerCase();
} else { // Ou caso seja um sensor
imageName = primeiroNome.toLowerCase();
}
// Obtem a imagem correspondente ao sensor/atuador
const image = document.getElementById(id + "-image");
fetch("/images/" + imageName + ".png")
.then((response) => response.blob())
.then((blob) => {
const objectURL = URL.createObjectURL(blob);
image.src = objectURL;
});
}
}
getValor().catch(error => console.error(error));
}
// Function to update webcam
function updateWebcam() {
// Obtem a imagem correspondente ao sensor/atuador
const image = document.getElementById("webcam-image");
fetch("/api/api.php?nome=webcam")
.then((response) => response.blob())
.then((blob) => {
const objectURL = URL.createObjectURL(blob);
image.src = objectURL;
});
// Obtem a hora da ultima atualizaçao da webcam e mostra na dashboard
let spanHora = document.getElementById("webcam-hora");
const getHora = async () => {
const response = await fetch('/api/api.php?nome=webcam&tipo=hora');
// check if the response is ok
if (response.ok) {
const textValue = await response.text();
spanHora.innerHTML = textValue; // Atualiza a hora na pagina
}
}
getHora().catch(error => console.error(error));
}
<?php
foreach ($files as $file) {
$nome = file_get_contents('api/files/' . $file . '/nome.txt');
$info = file_get_contents('api/files/' . $file . '/info.txt');
// Cria um setInterval para cada sensor/atuador
echo "setInterval(updateFromAPI, 5000, '" . $file . "', '" . $nome . "', " . ($info == 'atuador' ? 'true' : 'false') . ");\n";
}
?>
updateWebcam();
setInterval(updateWebcam, 5000);
</script>
<!--Popper.JS -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<!--Bootstrap JS -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>