PHP_SMART_HOME_V3/app/views/Rooms.php

88 lines
2.3 KiB
PHP
Raw Permalink Normal View History

2019-08-23 11:39:42 +00:00
<?php
class Rooms extends Template
{
function __construct()
{
global $userManager;
2020-02-11 14:09:15 +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('rooms');
2019-10-11 14:38:20 +00:00
$template->prepare('baseDir', BASEDIR);
2020-02-11 14:09:15 +00:00
$template->prepare('debugMod', DEBUGMOD);
2019-08-23 11:39:42 +00:00
$template->prepare('title', 'Rooms');
2020-02-11 14:09:15 +00:00
$template->prepare('langMng', $langMng);
2019-08-23 11:39:42 +00:00
$roomsItems = [];
$roomsData = RoomManager::getAllRooms();
foreach ($roomsData as $roomKey => $roomsData) {
$devicesData = DeviceManager::getAllDevicesInRoom($roomsData['room_id']);
$roomReading = [];
if ($roomsData['device_count'] == 0) {
continue;
}
$roomReadingCache = [];
$roomControlsCache = [];
foreach ($devicesData as $deviceKey => $deviceData) {
$subDevicesData = SubDeviceManager::getAllSubDevices($deviceData['device_id']);
foreach ($subDevicesData as $subDeviceKey => $subDeviceData) {
$subDeviceType = $subDeviceData['type'];
$subDeviceUnit = $subDeviceData['unit'];
$lastRecord = RecordManager::getLastRecord($subDeviceData['subdevice_id']);
if (in_array($subDeviceType, ['on/off','battery','door'])) {
if ($subDeviceType == 'on/off') {
$roomControlsCache[$subDeviceKey] = [
'type' => $subDeviceType,
'name' => $deviceData['name'],
'icon' => $deviceData['icon'],
'value' => $lastRecord['value'],
];
}
continue;
}
if (array_key_exists($subDeviceType, $roomReadingCache)) {
$roomReadingCache[$subDeviceType] = [
"value" => $roomReadingCache[$subDeviceType]['value'] + $lastRecord['value'],
"count" => $roomReadingCache[$subDeviceType]['count'] + 1,
"unit" => $subDeviceUnit,
];
} else {
$roomReadingCache[$subDeviceType] = [
"value" => $lastRecord['value'],
"count" => 1,
"unit" => $subDeviceUnit,
];
}
}
}
// parsing
foreach ($roomReadingCache as $type => $value) {
$roomReading[$type] = $value["value"] / $value["count"];
$roomReading[$type] .= @($value['unit']);
}
$roomsItems[$roomsData['room_id']] = [
'name' => $roomsData['name'],
'reading' => $roomReading,
'controls' => $roomControlsCache,
'deviceCount' => $roomsData['device_count'],
];
}
$template->prepare('rooms', $roomsItems);
$template->render();
}
}