PHP_SMART_HOME_V3/app/views/Scene.php

84 lines
2.7 KiB
PHP
Raw Normal View History

2019-08-23 11:39:42 +00:00
<?php
class Scene 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('scene');
2019-10-11 14:38:20 +00:00
$template->prepare('baseDir', BASEDIR);
2019-08-23 11:39:42 +00:00
$template->prepare('title', 'Scény');
2019-10-11 12:12:05 +00:00
$template->prepare('langMng', $langMng);
2019-08-23 11:39:42 +00:00
$scenes = [];
foreach (SceneManager::getAllScenes() as $sceneId => $sceneData) {
$doSomething = [];
foreach (json_decode($sceneData['do_something']) as $subdeviceId => $subDeviceState) {
$subDeviceMasterDeviceData = SubDeviceManager::getSubDeviceMaster($subdeviceId);
$doSomething[$subdeviceId] = [
'name' => $subDeviceMasterDeviceData['name'],
'state' => $subDeviceState,
];
}
$scenes[$sceneData['scene_id']] = [
"name" => $sceneData['name'],
"icon" => $sceneData['icon'],
2019-08-24 11:13:04 +00:00
"doSomething" => $doSomething,
2019-08-23 11:39:42 +00:00
];
}
$template->prepare('scenes', $scenes);
$approvedSubDevices = [];
$allDevicesData = DeviceManager::getAllDevices();
foreach ($allDevicesData as $deviceKey => $deviceValue) {
if (!$deviceValue['approved']) continue;
$allSubDevicesData = SubDeviceManager::getAllSubDevices($deviceValue['device_id']);
foreach ($allSubDevicesData as $subDeviceKey => $subDeviceValue) {
if ($subDeviceValue['type'] != 'on/off') continue;
$approvedSubDevices[$subDeviceValue['subdevice_id']] = [
'name' => $allDevicesData[$deviceKey]['name'],
];
}
}
$template->prepare('subDevices', $approvedSubDevices);
$approvedSubDevices = [];
$allDevicesData = DeviceManager::getAllDevices();
foreach ($allDevicesData as $deviceKey => $deviceValue) {
if (!$deviceValue['approved']) continue;
$allSubDevicesData = SubDeviceManager::getAllSubDevices($deviceValue['device_id']);
foreach ($allSubDevicesData as $subDeviceKey => $subDeviceValue) {
if ($subDeviceValue['type'] != 'on/off') continue;
$approvedSubDevices[$subDeviceValue['subdevice_id']] = [
'name' => $allDevicesData[$deviceKey]['name'],
];
}
}
$template->prepare('subDevices', $approvedSubDevices);
if (isset($_POST['devices'])){
$devices = $_POST['devices'];
$devicesOBJ = [];
foreach ($devices as $deviceId) {
$deviceData = DeviceManager::getDeviceById($deviceId);
$subdeviceData = SubDeviceManager::getSubDeviceByMaster($deviceId, 'on/off');
$devicesOBJ[$deviceId] = [
'name' => $deviceData['name'],
'setableSubDevices' => $subdeviceData['subdevice_id'],
];
}
$template->prepare('setStateFormDevices', $devicesOBJ);
$template->prepare('sceneName', $_POST['sceneName']);
$template->prepare('sceneIcon', $_POST['sceneIcon']);
}
$template->render();
}
}