PHP_SMART_HOME_V3/app/views/Home.php

195 lines
5.2 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()){
2020-04-28 09:52:14 +00:00
header('Location: ' . BASEURL . 'login');
2019-08-23 11:39:42 +00:00
}
$template = new Template('home');
2019-09-19 12:48:31 +00:00
//users instantialize
2020-02-18 20:30:44 +00:00
$users = UserManager::getUsers();
2019-09-19 12:48:31 +00:00
$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 = [];
2020-02-18 20:30:44 +00:00
$roomsData = RoomManager::getAllRooms();
2019-08-23 11:39:42 +00:00
foreach ($roomsData as $roomKey => $roomsData) {
$devices = [];
2020-02-18 20:30:44 +00:00
$devicesData = DeviceManager::getAllDevicesInRoom($roomsData['room_id']);
2019-08-23 11:39:42 +00:00
foreach ($devicesData as $deviceKey => $deviceData) {
$subDevices = [];
2020-02-18 20:30:44 +00:00
$subDevicesData = SubDeviceManager::getAllSubDevices($deviceData['device_id']);
2019-08-23 11:39:42 +00:00
foreach ($subDevicesData as $subDeviceKey => $subDeviceData) {
2020-02-18 20:30:44 +00:00
$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':
2020-02-25 14:02:16 +00:00
$replacementTrue = 'Light';
$replacementFalse = 'Dark';
$operator = '==';
$breakValue = 1;
if ($lastValue != 1 && $lastValue != 0) { //Digital Light Senzor
$operator = '<';
$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
2020-02-18 20:30:44 +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;
2020-02-18 20:30:44 +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']);
2020-02-18 20:30:44 +00:00
$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
2020-02-25 14:02:16 +00:00
if (
$lastSeen < $deviceData['sleep_time'] ||
$subDeviceData['type'] == "on/off" ||
$subDeviceData['type'] == "door"
) {
2019-10-10 12:38:10 +00:00
$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'],
2020-03-11 11:22:44 +00:00
'mac' => $deviceData['mac'],
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,
];
}
2020-02-18 20:30:44 +00:00
$rooms = RoomManager::getAllRooms();
2019-10-11 14:38:20 +00:00
$template->prepare('baseDir', BASEDIR);
2020-01-17 09:27:51 +00:00
$template->prepare('debugMod', DEBUGMOD);
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();
}
}