PHP_SMART_HOME_V3/app/views/Home.php

189 lines
5.1 KiB
PHP
Raw Normal View History

2019-08-23 11:39:42 +00:00
<?php
class Home extends Template
{
function __construct()
{
global $userManager;
2019-10-11 12:12:05 +00:00
global $langMng;
2019-08-23 11:39:42 +00:00
if (!$userManager->isLogin()){
2019-10-11 14:32:05 +00:00
header('Location: ' . BASEDIR . 'login');
2019-08-23 11:39:42 +00:00
}
$template = new Template('home');
2019-09-19 12:48:31 +00:00
//users instantialize
$users = UserManager::getUsers();
$template->prepare('users', $users);
//Users at home Info
$usersAtHome = '';
$i = 0;
foreach ($users as $user) {
if ($user['at_home'] == 'true') {
2019-10-30 14:18:29 +00:00
$i++;
2019-09-19 12:48:31 +00:00
$usersAtHome .= $user['username'];
2019-10-30 14:18:29 +00:00
if ($usersAtHome != "" && isset($users[$i + 1]) && $users[$i + 1]['at_home'] == 'true'){
2019-09-19 12:48:31 +00:00
$usersAtHome .= ', ';
}
}
}
$template->prepare('usersAtHome', $usersAtHome);
2019-10-10 12:38:10 +00:00
2019-08-23 11:39:42 +00:00
$roomsItems = [];
$roomsData = RoomManager::getAllRooms();
foreach ($roomsData as $roomKey => $roomsData) {
$devices = [];
$devicesData = DeviceManager::getAllDevicesInRoom($roomsData['room_id']);
foreach ($devicesData as $deviceKey => $deviceData) {
$subDevices = [];
$subDevicesData = SubDeviceManager::getAllSubDevices($deviceData['device_id']);
foreach ($subDevicesData as $subDeviceKey => $subDeviceData) {
$events = RecordManager::getLastRecord($subDeviceData['subdevice_id'], 5);
2019-10-30 14:18:29 +00:00
$eventsRaw = $events;
2019-08-23 11:39:42 +00:00
2019-10-10 12:38:10 +00:00
$connectionError = true;
$parsedValue = "";
$niceTime = "";
if (sizeof($events) > 1) {
$lastRecord = $events[0];
2019-10-10 12:38:10 +00:00
$lastValue = round($lastRecord['value']);
$parsedValue = $lastValue;
/*Value Parsing*/
2019-10-10 12:38:10 +00:00
//Last Value Parsing
switch ($subDeviceData['type']) {
case 'on/off':
$replacementTrue = 'On';
$replacementFalse = 'Off';
2019-10-10 13:13:43 +00:00
$operator = '==';
$breakValue = 1;
2019-10-10 12:38:10 +00:00
break;
case 'door':
$replacementTrue = 'Closed';
2019-10-10 12:38:10 +00:00
$replacementFalse = 'Open';
2019-10-10 13:13:43 +00:00
$operator = '==';
$breakValue = 1;
2019-10-10 12:38:10 +00:00
break;
2019-10-10 12:38:10 +00:00
case 'light':
$replacementTrue = 'Light';
$replacementFalse = 'Dark';
2019-10-10 13:13:43 +00:00
$operator = '==';
$breakValue = 1;
if ($lastValue != 1 || $lastValue != 0) { //Digital Light Senzor
2019-11-26 12:32:45 +00:00
$operator = '<';
2019-10-10 13:13:43 +00:00
$breakValue = 810;
}
2019-10-10 12:38:10 +00:00
break;
case 'water':
$replacementTrue = 'Wet';
$replacementFalse = 'Dry';
2019-10-10 13:13:43 +00:00
$operator = '==';
$breakValue = 1;
2019-10-10 12:38:10 +00:00
break;
default:
$replacementTrue = '';
$replacementFalse = '';
break;
}
if ($replacementTrue != '' && $replacementFalse != '') {
//parsing last values
$parsedValue = $replacementFalse;
2019-10-10 13:13:43 +00:00
if (Utilities::checkOperator($lastValue, $operator, $breakValue)) {
2019-10-10 12:38:10 +00:00
$parsedValue = $replacementTrue;
}
2019-10-30 14:18:29 +00:00
2019-10-10 12:38:10 +00:00
//parsing last events values
foreach ($events as $key => $value) {
2019-10-10 12:38:10 +00:00
$events[$key]['value'] = $replacementFalse;
2019-11-26 12:32:45 +00:00
if (Utilities::checkOperator($value['value'], $operator, $breakValue)) {
2019-10-10 12:38:10 +00:00
$events[$key]['value'] = $replacementTrue;
}
}
2019-08-23 11:39:42 +00:00
}
2019-10-10 12:38:10 +00:00
$LastRecordTime = new DateTime($lastRecord['time']);
$niceTime = Utilities::ago($LastRecordTime);
2019-08-23 11:39:42 +00:00
2019-10-10 12:38:10 +00:00
$interval = $LastRecordTime->diff(new DateTime());
$hours = $interval->format('%h');
$minutes = $interval->format('%i');
$lastSeen = ($hours * 60 + $minutes);
2019-08-23 11:39:42 +00:00
2019-10-10 12:38:10 +00:00
if ($lastSeen < $deviceData['sleep_time'] || $subDeviceData['type'] == "on/off") {
$connectionError = false;
}
2019-08-23 11:39:42 +00:00
}
$subDevices[$subDeviceData['subdevice_id']] = [
'events'=> $events,
2019-10-30 14:18:29 +00:00
'eventsRaw'=> $eventsRaw,
2019-08-23 11:39:42 +00:00
'type' => $subDeviceData['type'],
'unit' => $subDeviceData['unit'],
'comError' => $connectionError,
'lastRecort' => [
'value' => (empty($parsedValue) ? 0 : $parsedValue),
'time' => (empty($lastRecord['time']) ? "00:00" : $lastRecord['time']),
'niceTime' => (empty($niceTime) ? "00:00" : $niceTime),
2019-08-23 11:39:42 +00:00
],
];
}
$permissionArray = json_decode($deviceData['permission']);
$userIsDeviceAdmin = false;
if($permissionArray[1] == 3) {
$userIsDeviceAdmin = true;
} else if ($permissionArray[0] == 3) {
2019-10-10 12:38:10 +00:00
if ( $deviceData['owner'] == $_SESSION['user']['id']) {
2019-08-23 11:39:42 +00:00
$userIsDeviceAdmin = true;
}
}
$devices[$deviceData['device_id']] = [
'name' => $deviceData['name'],
'icon' => $deviceData['icon'],
'room' => $deviceData['room_id'],
'token' => $deviceData['token'],
2019-11-26 12:32:45 +00:00
'type' => $deviceData['type'],
2019-11-27 16:05:49 +00:00
'ip' => $deviceData['ip_address'],
2019-11-28 13:34:32 +00:00
'subnet' => $deviceData['subnet'],
'gateway' => $deviceData['gateway'],
2019-08-23 11:39:42 +00:00
'sleepTime' => $deviceData['sleep_time'],
2019-08-24 11:13:04 +00:00
'approved' => $deviceData['approved'],
2019-08-23 11:39:42 +00:00
'permission' => $permissionArray,
'owner' => $deviceData['owner'],
'userIsAdmin' => $userIsDeviceAdmin,
'subDevices' => $subDevices,
];
}
$roomsItems[$roomsData['room_id']] = [
'name' => $roomsData['name'],
'deviceCount' => $roomsData['device_count'],
'devices' => $devices,
];
}
$rooms = RoomManager::getAllRooms();
2019-10-11 14:38:20 +00:00
$template->prepare('baseDir', BASEDIR);
2019-08-23 11:39:42 +00:00
$template->prepare('title', 'Home');
2019-10-11 14:38:20 +00:00
$template->prepare('rooms', $rooms);
2019-10-11 12:12:05 +00:00
$template->prepare('langMng', $langMng);
2019-08-23 11:39:42 +00:00
$template->prepare('data', $roomsItems);
$template->render();
}
}