-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorbs.php
executable file
·239 lines (233 loc) · 10.3 KB
/
orbs.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
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
require '../includes/db.php';
require 'includes/check-signed-in.php';
require '../includes/class.Meter.php';
if (isset($_POST['submit'])) {
$stmt = $db->prepare('UPDATE orbs SET disabled = ? WHERE name = ?');
if ($_POST['submit'] === 'Turn off') {
$stmt->execute(array(1, $_POST['orb']));
} else {
$stmt->execute(array(0, $_POST['orb']));
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required meta tags always come first -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Orbs Backend</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body style="padding-top:5px">
<div class="container">
<div class="row">
<div class="col-xs-12">
<img src="images/env_logo.png" class="img-fluid" style="margin-bottom:15px">
<?php include 'includes/navbar.php'; ?>
</div>
</div>
<div style="height:20px;clear:both"></div>
<div class="row">
<div class="col-sm-12">
<h1>Oberlin orbs</h1>
<div class="table-resposive">
<table class="table">
<thead class="thead-default">
<tr>
<th>Name</th>
<th>IP</th>
<th>Electricity meter</th>
<th>Water meter</th>
<th>Electricity bin</th>
<th>Water bin</th>
<th>Electricity <code>relative_value</code> id</th>
<th>Water <code>relative_value</code> id</th>
<th>Disable</th>
</tr>
</thead>
<tbody>
<?php
$meter = new Meter($db);
$page = (empty($_GET['page'])) ? 0 : intval($_GET['page']) - 1;
$count = $db->query('SELECT COUNT(*) FROM orbs')->fetchColumn();
$limit = 15;
$offset = $limit * $page;
$final_page = ceil($count / $limit);
foreach ($db->query("SELECT name, elec_uuid, water_uuid, elec_rvid, water_rvid, disabled, INET_NTOA(ip) AS ip FROM orbs ORDER BY name ASC LIMIT {$offset}, {$limit}") as $row) {
$elec_last_updated = 0;
$elec_meter = '';
$water_meter = '';
$water_last_updated = 0;
$elec_outdated = false;
$water_outdated = false;
if ($row['elec_uuid'] != null) {
$elec_meter = $db->query("SELECT name FROM meters WHERE bos_uuid = '{$row['elec_uuid']}'");
if ($elec_meter->rowCount() != 1) {
$elec_meter = 'Meter not found';
$elec_outdated = true;
} else {
$elec_meter = $elec_meter->fetchColumn();
$stmt = $db->prepare('SELECT last_updated FROM relative_values WHERE meter_uuid = ?');
$stmt->execute(array($row['elec_uuid']));
$elec_last_updated = $stmt->fetchColumn();
$elec_outdated = ($elec_last_updated === false || (time()-60) > $elec_last_updated) ? true : false;
}
}
if ($row['water_uuid'] != null) {
$water_meter = $db->query("SELECT name FROM meters WHERE bos_uuid = '{$row['water_uuid']}'");
if ($water_meter->rowCount() != 1) {
$water_meter = 'Meter not found';
$water_outdated = true;
} else {
$water_meter = $water_meter->fetchColumn();
$stmt = $db->prepare('SELECT last_updated FROM relative_values WHERE meter_uuid = ?');
$stmt->execute(array($row['water_uuid']));
$water_last_updated = $stmt->fetchColumn();
$water_outdated = ($water_last_updated === false || (time()-60) > $water_last_updated) ? true : false;
}
}
?>
<tr<?php echo ($elec_last_updated === false || $elec_outdated || $water_last_updated === false || $water_outdated) ? ' class="table-danger"' : ''; ?>>
<td><?php echo $row['name']; ?></td>
<td id="ip<?php echo $row['ip'] ?>"><?php echo $row['ip']; ?></td>
<td><?php echo $elec_meter; ?></td>
<td><?php echo $water_meter; ?></td>
<?php
if ($row['elec_uuid'] == null) {
$elec = '-';
}
else {
$stmt = $db->prepare('SELECT relative_value FROM relative_values WHERE id = ?');
$stmt->execute(array($row['elec_rvid']));
$elec = round(($stmt->fetchColumn() / 100) * 4); // must be integer 0-4
}
echo "<td>{$elec}</td>";
if ($row['water_uuid'] == null) {
$water = '-';
}
else {
$stmt = $db->prepare('SELECT relative_value FROM relative_values WHERE id = ?');
$stmt->execute(array($row['water_rvid']));
$water = round(($stmt->fetchColumn() / 100) * 4); // must be integer 0-4
}
echo "<td>{$water}</td>";
?>
<td><?php if ($row['elec_uuid'] != null) {
echo $row['elec_rvid'];
if ($elec_last_updated === false) {
// var_dump($row['elec_uuid']);
echo ' (No relative value record)';
} elseif ($elec_outdated) {
echo ' (Outdated)';
}
}
?></td>
<td><?php if ($row['water_uuid'] != null) {
echo $row['water_rvid'];
if ($water_last_updated === false) {
// var_dump($row['water_uuid']);
echo ' (No relative value record)';
} elseif ($water_outdated) {
echo ' (Outdated)';
}
}
?></td>
<td>
<?php if ($row['disabled'] === '1') { ?>
<form action="" method="POST">
<input type="hidden" name="orb" value="<?php echo $row['name'] ?>">
<input type="submit" class="btn btn-primary" name="submit" value="Turn on">
</form>
<?php } else { ?>
<form action="" method="POST">
<input type="hidden" name="orb" value="<?php echo $row['name'] ?>">
<input type="submit" class="btn btn-danger" name="submit" value="Turn off">
</form>
<?php } ?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</div>
<div class="row">
<div class="col">
<nav aria-label="Page navigation">
<ul class="pagination pagination-lg justify-content-center">
<?php if ($page > 0) { ?>
<li class="page-item">
<a class="page-link" href="?sort=<?php echo (isset($_GET['sort'])) ? $_GET['sort'] : ''; ?>&page=<?php echo $page ?>" aria-label="Previous">
<span aria-hidden="true">«</span>
<span class="sr-only">Previous</span>
</a>
</li>
<?php }
for ($i = 1; $i <= $final_page; $i++) {
if ($page + 1 === $i) {
echo '<li class="page-item active"><a class="page-link" href="?page=' . $i . '">' . $i . '</a></li>';
}
else {
echo '<li class="page-item"><a class="page-link" href="?page=' . $i . '">' . $i . '</a></li>';
}
}
if ($page + 1 < $final_page) { ?>
<li class="page-item">
<a class="page-link" href="?page=<?php echo $page + 2 ?>" aria-label="Next">
<span aria-hidden="true">»</span>
<span class="sr-only">Next</span>
</a>
</li>
<?php } ?>
</ul>
</nav>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<script>
// function ping(ip) {
// $.ajax({
// url: 'http://' + ip,
// success: function(result) {
// $("#ip" + ip).text(ip + ' responded')
// },
// error: function(result){
// $("#ip" + ip).text(ip + ' timeout/error');
// }
// });
// }
// function ping(host, port, pong) {
// var started = new Date().getTime();
// var http = new XMLHttpRequest();
// http.open("GET", "http://" + host + ":" + port, /*async*/true);
// http.onreadystatechange = function() {
// if (http.readyState == 4) {
// var ended = new Date().getTime();
// var milliseconds = ended - started;
// if (pong != null) {
// pong(milliseconds);
// }
// }
// };
// try {
// http.send(null);
// } catch(exception) {
// // this is expected
// }
// }
// $.each(<?php //echo json_encode($to_ping) ?>, function( i, l ) {
// ping(l, 80);
// // alert( "Index #" + i + ": " + l );
// });
</script>
</body>
</html>