PHP_SMART_HOME_V3/app/views/Dashboard.php

91 lines
2.7 KiB
PHP
Raw Normal View History

2019-08-23 11:39:42 +00:00
<?php
class Dashboard 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('dashboard');
$dashboard = [];
$dashboardData = DashboardManager::getAllDashboards($userManager->getUserData('user_id'));
foreach ($dashboardData as $dashboardItemKey => $dashboardItemValue) {
$subDeviceData = SubDeviceManager::getSubDevice($dashboardItemValue['subdevice_id']);
$deviceData = SubDeviceManager::getSubDeviceMaster($dashboardItemValue['subdevice_id']);
$lastRecord = RecordManager::getLastRecord($dashboardItemValue['subdevice_id']);
$parsedValue = $lastRecord['value'];
//TODO: Opravit aby to bylo stejné parsování jako na HOME
if ($subDeviceData['type'] == "on/off") {
$parsedValue = ($parsedValue == 1 ? 'ON' : 'OFF');
}
if ($subDeviceData['type'] == "light") {
$parsedValue = ($parsedValue == 1 ? 'Light' : 'Dark');
}
$dashboard[$dashboardItemValue['dashboard_id']] = [
'icon' => $deviceData['icon'],
'id' => $subDeviceData['subdevice_id'],
'masterId' => $deviceData['device_id'],
'name' => $deviceData['name'],
'type' => $subDeviceData['type'],
'unit' => $subDeviceData['unit'],
'lastRecord' => [
'value' => $parsedValue,
],
];
}
$approvedSubDevices = [];
$allDevicesData = DeviceManager::getAllDevices();
foreach ($allDevicesData as $deviceKey => $deviceValue) {
if (!$deviceValue['approved']) continue;
$allSubDevicesData = SubDeviceManager::getAllSubDevices($deviceValue['device_id']);
foreach ($allSubDevicesData as $subDeviceKey => $subDeviceValue) {
$approvedSubDevices[$subDeviceValue['subdevice_id']] = [
'name' => $allDevicesData[$deviceKey]['name'],
'type' => $subDeviceValue['type'],
];
}
}
if (isset($_POST['deviceId'])){
$deviceData = DeviceManager::getDeviceById($_POST['deviceId']);
$subDevices = [];
$subDevicesData = SubDeviceManager::getAllSubDevices($_POST['deviceId']);
foreach ($subDevicesData as $subDeviceKey => $subDeviceData) {
$subDevices[$subDeviceData['subdevice_id']] = [
'type' => $subDeviceData['type'],
'unit' => $subDeviceData['unit'],
];
}
$device = [
'id' => $deviceData['device_id'],
'name' => $deviceData['name'],
'token' => $deviceData['token'],
'icon' => $deviceData['icon'],
'subDevices' => $subDevices,
];
$template->prepare('deviceData', $device);
}
$template->prepare('title', 'Nástěnka');
2019-10-11 12:12:05 +00:00
$template->prepare('langMng', $langMng);
2019-08-23 11:39:42 +00:00
$template->prepare('dashboard', $dashboard);
$template->prepare('subDevices', $approvedSubDevices);
$template->render();
}
}