PHP_SMART_HOME_V3/app/views/Home.php

182 lines
5.4 KiB
PHP
Raw Normal View History

2019-08-23 11:39:42 +00:00
<?php
class Home extends Template
{
function __construct()
{
global $userManager;
global $lang;
if (!$userManager->isLogin()){
header('Location: ./login');
}
$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) {
$i++;
if ($user['at_home'] == 'true') {
$usersAtHome .= $user['username'];
if ($usersAtHome != "" && isset($users[$i + 1])){
$usersAtHome .= ', ';
}
}
}
$template->prepare('usersAtHome', $usersAtHome);
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);
$connectionError = false;
$parsedValue = "";
$niceTime = "";
if (sizeof($events) > 1) {
2019-09-09 19:30:50 +00:00
//TODO: skontrolovat zdali se jedná o poslední (opravdu nejaktuálnější) záznam
$lastRecord = $events[0];
$parsedValue = round($lastRecord['value']);
/*Value Parsing*/
if ($subDeviceData['type'] == "on/off") {
$parsedValue = ($parsedValue == 1 ? 'ON' : 'OFF');
2019-08-23 11:39:42 +00:00
}
if ($subDeviceData['type'] == "door") {
$replacementTrue = 'Closed';
$replacementFalse = 'Opened';
foreach ($events as $key => $value) {
$events[$key]['value'] = ($value['value'] == 1 ? $replacementTrue : $replacementFalse);
}
$parsedValue = ($parsedValue == 1 ? $replacementTrue : $replacementFalse);
}
if ($subDeviceData['type'] == "light") {
$replacementTrue = 'Light';
$replacementFalse = 'Dark';
foreach ($events as $key => $value) {
if ($parsedValue != 1){
//Analog Reading
$events[$key]['value'] = ($value['value'] <= 810 ? $replacementTrue : $replacementFalse);
} else {
//Digital Reading
$events[$key]['value'] = ($value['value'] == 0 ? $replacementTrue : $replacementFalse);
}
}
2019-08-23 11:39:42 +00:00
if ($parsedValue != 1){
//Analog Reading
$parsedValue = ($parsedValue <= 810 ? $replacementTrue : $replacementFalse);
2019-08-23 11:39:42 +00:00
} else {
//Digital Reading
$parsedValue = ($parsedValue == 0 ? $replacementTrue : $replacementFalse);
2019-08-23 11:39:42 +00:00
}
}
$date2 = new DateTime($lastRecord['time']);
2019-08-23 11:39:42 +00:00
$niceTime = $this->ago($date2);
2019-08-23 11:39:42 +00:00
$startDate = date_create($lastRecord['time']);
$interval = $startDate->diff(new DateTime());
$hours = $interval->format('%h');
$minutes = $interval->format('%i');
$lastSeen = ($hours * 60 + $minutes);
2019-08-23 11:39:42 +00:00
if ($lastSeen > $deviceData['sleep_time'] && $subDeviceData['type'] != "on/off") {
$connectionError = true;
}
} else {
2019-08-23 11:39:42 +00:00
$connectionError = true;
}
$subDevices[$subDeviceData['subdevice_id']] = [
'events'=> $events,
'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) {
if ( $deviceData['owner'] == $userManager->getUserData('user_id')) {
$userIsDeviceAdmin = true;
}
}
$devices[$deviceData['device_id']] = [
'name' => $deviceData['name'],
'icon' => $deviceData['icon'],
'room' => $deviceData['room_id'],
'token' => $deviceData['token'],
'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();
$template->prepare('rooms', $rooms);
$template->prepare('title', 'Home');
$template->prepare('lang', $lang);
$template->prepare('data', $roomsItems);
$template->render();
}
function ago( $datetime )
{
$interval = date_create('now')->diff( $datetime );
$suffix = ( $interval->invert ? ' ago' : '' );
if ( $v = $interval->y >= 1 ) return $this->pluralize( $interval->m, 'month' ) . $suffix;
if ( $v = $interval->d >= 1 ) return $this->pluralize( $interval->d, 'day' ) . $suffix;
if ( $v = $interval->h >= 1 ) return $this->pluralize( $interval->h, 'hour' ) . $suffix;
if ( $v = $interval->i >= 1 ) return $this->pluralize( $interval->i, 'minute' ) . $suffix;
return $this->pluralize( $interval->s, 'second' ) . $suffix;
}
function pluralize( $count, $text )
{
return $count . ( ( $count == 1 ) ? ( " $text" ) : ( " ${text}s" ) );
}
}