-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
366 lines (292 loc) · 13.8 KB
/
index.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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?php
include_once 'core.php';
autOnly();
define('TITLE', 'Умный дом');
getHeader();
$date = @preg_match('#^(?:[0-9]{4})\-(?:[0-9]{2})\-(?:[0-9]{2})$#', $_GET['date']) ? $_GET['date'] : date('Y-m-d');
$view_date = explode('-', $date);
$view_date = array_reverse($view_date);
$view_date = implode('.', $view_date);
// Определяем stamp начала и конца дня
$day_start = strtotime($date.' 00:00:00');
$day_end = strtotime($date.' 23:59:59');
$unixtime = time();
// Определяем, сегодняшнюю ли дату смотрим
$__today_date = explode('.', date('d.m.Y', $unixtime));
$__stats_date = explode('.', date('d.m.Y', $day_start));
$_TODAY = $__today_date == $__stats_date ? TRUE : FALSE;
// Запрос на список всех устройств
$q_devices = mysql_query("SELECT * FROM `ewelink_devices` WHERE `deleted` = 0 ORDER BY `id_room` ASC");
// "глобальные" счетчики для всего
$_SUM['rooms'] = 0;
$_SUM['devices'] = 0;
$_SUM['events'] = 0;
$_SUM['on_events'] = 0;
$_SUM['off_events'] = 0;
$_SUM['uptime'] = 0;
// запрос на список комнат
$q_rooms = mysql_query("SELECT * FROM `rooms` WHERE `deleted` = 0");
$_ROOMS = array();
while($_tmp = mysql_fetch_assoc($q_rooms))
{
$_ROOMS[$_tmp['id']] = $_tmp['name'];
$_SUM['rooms']++;
}
// Определяем список типов датчиков
$q_sensors_types = mysql_query("SELECT * FROM `web_sensors_types` ORDER BY `id` ASC");
$_SENSORS_TYPES = array();
while($_tmp = mysql_fetch_assoc($q_sensors_types))
{
$_SENSORS_TYPES[$_tmp['id']] = $_tmp;
}
// Определяем время последнего события (ewelink-relay)
$q_last_event = mysql_query("SELECT * FROM `ewelink_events` ORDER BY `id` DESC LIMIT 1");
if(mysql_num_rows($q_last_event) == 1)
{
$__last_event = mysql_fetch_assoc($q_last_event);
}
else
{
$__last_event['time'] = $unixtime;
}
?>
<div class="alert alert-primary" role="alert">
Статистика за <b><?=$view_date?></b>
</div>
<div class="row">
<form action="index.php" method="get">
<div class="col-sm">
<input type="date" value="<?=$date?>" id="date" name="date" class="form-control" style="margin-bottom: 5px;" />
</div>
<div class="col-sm">
<input type="submit" value="Посмотреть" class="btn btn-primary" />
<a href="index.php" class="btn btn-default" href="Сегодня">Сегодня</a>
</div>
</form>
</div>
<h1 class="mt-5">Приборы</h1>
<span class="small">Последнее событие: <?=showTimeInterval($unixtime - $__last_event['time'])?> назад</span>
<div class="row">
<?php
while($_DEVICE = mysql_fetch_assoc($q_devices))
{
$_SUM['devices']++;
##################### делаем запрос на определение последнего события
$q_last = mysql_query("SELECT * FROM `ewelink_events` WHERE `id_device` = ".$_DEVICE['id']." ORDER BY `id` DESC LIMIT 1");
if(mysql_num_rows($q_last) == 0)
{
$status = 0;
$noEvents = true;
}
else
{
$event_last = mysql_fetch_assoc($q_last);
$noEvents = false;
$status = $event_last['action'];
}
##################### Начинаем рисовать блок. Выводим название
echo '<div class="col-sm-4 device_card" style="border: 1px dotted #ccc; border-radius: 5px; position: relative;">';
echo '<div style="text-align: center"><b>'.$_DEVICE['full_name'].' | '.$_ROOMS[$_DEVICE['id_room']].'</b> ';
##################### Определяем статус устройства, рисуем badge
if($status == 0)
{
echo '<a class="bi bi-lightbulb" href="ifttt_link.php?short_name='.$_DEVICE['short_name'].'&action=on&date='.$date.'" style="position:absolute; right: 5px; top: 5px; font-size: 1.7rem;"></a>';
}
else
{
echo '<a class="bi bi-lightbulb-fill" href="ifttt_link.php?short_name='.$_DEVICE['short_name'].'&action=off&date='.$date.'" style="position:absolute; right: 5px; top: 5px; font-size: 1.7rem;"></a>';
}
echo '</div>';
##################### Если девайс включен - рисуем сколько уже работает, если нет - когда выключился. Только для просмотра статистики текущего дня
if(!$noEvents && $_TODAY)
{
if($status == 1)
{
$string_now_uptime = showTimeInterval($unixtime - $event_last['time']);
$string_now_uptime = empty($string_now_uptime) ? '0 сек' : $string_now_uptime;
echo '<span class="small">Работает</span> <span class="badge badge-info">'.$string_now_uptime.'</span>';
}
else
{
$string_when_off = showWhen($event_last['time']);
echo '<span class="small">Выкл. с</span> <span class="badge badge-secondary">'.$string_when_off.'</span>';
}
echo '<br />';
}
##################### Работаем с событиями
// фикс если запускается раньше конца текущего дня
if($day_end > $unixtime)
{
$day_end = $unixtime;
}
$q = mysql_query("SELECT * FROM `ewelink_events` WHERE `id_device` = ".$_DEVICE['id']." AND (`time` BETWEEN ".$day_start." AND ".$day_end.")");
if(mysql_num_rows($q) > 0)
{
// Собираем список событий
$events = array();
while($event = mysql_fetch_assoc($q))
{
$events[] = array($event['action'], $event['time']);
}
// Определяем последнее событие
$c_events = count($events);
$last_event = $events[$c_events - 1][0];
// определяем первое событие
$first_event = $events[0][0];
// Если первое событие - включение, то ничего не делаем, если выключение - фиксируем включение в 00:00:00. Третий аргумент для пометки фейкового включения
if($first_event == 0)
{
// die('СВЕТ БЫЛ ВКЛЮЧЕН!');
$events[] = array(1, $day_start, true);
}
// Если последнее событие - выключение, то ничего не делаем, если включение - фиксируем выключение в 23:59:59. Третий аргумент для пометки фейкового выключения
if($last_event == 1)
{
$events[] = array(0, $day_end, true);
}
// считаем сумму и включения-выключения
$sum_off = 0;
$c_off = 0;
$sum_on = 0;
$c_on = 0;
foreach($events as $event)
{
if($event[0] == 0)
{
$sum_off += $event[1];
// фикс того, будто мы считаем включение в 00:00:00, чтобы не было расхождения total счетчика
if(!isset($event[2]))
{
$c_off++;
}
}
else
{
$sum_on += $event[1];
// фикс того, будто мы считаем выключение в 23:59:59, чтобы не было расхождения total счетчика
if(!isset($event[2]))
{
$c_on++;
}
}
}
$uptime = $sum_off - $sum_on;
}
else
{
// фикс: если свет включен, но событий за сегодня нет, для подсчета аптайма за сегодня берем начало текущего дня
if($status == 1)
{
$uptime = $day_end - $day_start;
}
else
{
// свет выключен, событий за прошлую дату нет -> система думает, что свет выключен
// ищем последнее событие до $day_start. если оно - включение, считаем, что свет работал весь день
$q_check_last_query = mysql_query("SELECT * FROM `ewelink_events` WHERE `id_device` = ".$_DEVICE['id']." AND `time` < ".$day_start." ORDER BY `time` DESC LIMIT 1");
if(mysql_num_rows($q_check_last_query) > 0)
{
$check_last_query = mysql_fetch_assoc($q_check_last_query);
if($check_last_query['action'] == 1)
{
// последнее событие - включение. устройство работало весь день
$uptime = 86399; // не спрашивайте почему
}
else
{
// последнее соыбтие - выключение. устройство не работало
$uptime = 0;
}
}
else
{
// событий вообще нет. устройство не работало, судя по всему еще ни разу
$uptime = 0;
}
}
$c_on = 0;
$c_off = 0;
}
$_SUM['on_events'] += $c_on;
$_SUM['off_events'] += $c_off;
# высчитываем аптайм
$string = showTimeInterval($uptime);
$_SUM['uptime'] += $uptime;
##################### Рисуем статистику
echo '<span class="small">'.($_TODAY ? 'За сегодня' : 'Всего за '.date('d.m', $day_start)).': </span> ';
echo !empty($string) ? '<span class="badge badge-info">'.$string : '<span class="badge badge-warning">не работало';
echo '</span><br />';
// раньше здесь был коллапс с полной статой
echo '<a href="events.php?id_device='.$_DEVICE['id'].'" class="badge badge-light">События</a>
<a href="device.php?id_device='.$_DEVICE['id'].'&action=view" class="badge badge-light">Подробно</a>
</div>
';
}
echo '</div>';
$_SUM['events'] = $_SUM['on_events'] + $_SUM['off_events'];
?>
<div class="row">
<div class="col-sm-4 device_card">
<div style="text-align: center"><b>Всего за <?=$_TODAY ? 'сегодня' : $view_date?></b></div>
<span class="small">Событий: </span> <span class="badge badge-success"><?=$_SUM['on_events']?></span> <span class="badge badge-danger"><?=$_SUM['off_events']?></span><br />
<span class="small">Время работы:</span> <span class="badge badge-info"><?=showTimeInterval($_SUM['uptime'])?></span>
</div>
</div>
<?php
##############################################################################################
##############################################################################################
##############################################################################################
#################################СЕКЦИЯ ПО РАБОТЕ С ДАТЧИКАМИ#################################
##############################################################################################
##############################################################################################
##############################################################################################
$q_sensors = mysql_query("SELECT * FROM `ewelink_sensors` WHERE `deleted` = 0 ORDER BY `id_room` ASC");
// Определяем время последнего события (sensor)
$q_last_event = mysql_query("SELECT * FROM `ewelink_sensors_events` ORDER BY `id` DESC LIMIT 1");
if(mysql_num_rows($q_last_event) == 1)
{
$__last_event = mysql_fetch_assoc($q_last_event);
}
else
{
$__last_event['time'] = $unixtime;
}
if(mysql_num_rows($q_sensors) > 0)
{
?>
<h1 class="mt-5">Датчики</h1>
<span class="small">Последнее событие: <?=showTimeInterval($unixtime - $__last_event['time'])?> назад</span>
<div class="row">
<?php
while($_SENSOR = mysql_fetch_assoc($q_sensors))
{
// последнее событие
// всего событий за сегодня
// всего событий за всё время
$q_last = mysql_query("SELECT * FROM `ewelink_sensors_events` WHERE `id_sensor` = ".$_SENSOR['id']." ORDER BY `id` DESC LIMIT 1");
// die(mysql_error());
if(mysql_num_rows($q_last) < 1)
{
$last_time = '<span class="badge badge-warning">не работало</span>';
}
else
{
$_event = mysql_fetch_assoc($q_last);
$last_time = '<span class="badge badge-secondary">'.showWhen($_event['time']).'</span>';
}
$c_events_today = mysql_num_rows(mysql_query("SELECT * FROM `ewelink_sensors_events` WHERE `id_sensor` = ".$_SENSOR['id']." AND `time` > ".$day_start." AND `time` < ".$day_end));
$c_events_overall = mysql_num_rows(mysql_query("SELECT * FROM `ewelink_sensors_events` WHERE `id_sensor` = ".$_SENSOR['id']));
?>
<div class="col-sm-4 device_card" style="border: 1px dotted #ccc; border-radius: 5px;">
<div style="text-align: center"><b><?=$_SENSOR['full_name'].' | '.$_ROOMS[$_SENSOR['id_room']]?></b></div>
<span class="small">Последнее событие: </span><?=$last_time?><br />
<span class="small"><?=$_TODAY ? 'Сегодня' : 'За '.date('d.m', $day_start)?> событий: </span><span class="badge badge-info"><?=$c_events_today?></span><br />
<span class="small">Всего событий: </span><span class="badge badge-info"><?=$c_events_overall?></span><br />
<a href="events_sensor.php?id_sensor=<?=$_SENSOR['id']?>" class="badge badge-light">События</a>
<a href="sensor.php?id_sensor=<?=$_SENSOR['id']?>&action=view" class="badge badge-light">Подробно</a>
</div>
<?php
}
echo '</div>';
}
getFooter();