PHP_SMART_HOME_V3/www/app/views/Ajax.php

224 lines
5.9 KiB
PHP
Raw Normal View History

2019-08-23 11:39:42 +00:00
<?php
class Ajax extends Template
{
function __construct()
{
global $userManager;
global $lang;
2020-04-21 18:08:53 +00:00
2019-08-23 11:39:42 +00:00
if (!$userManager->isLogin()){
2020-04-28 09:52:14 +00:00
header('Location: ' . BASEURL);
2019-08-23 11:39:42 +00:00
}
2020-04-21 18:08:53 +00:00
2019-08-23 11:39:42 +00:00
$is_ajax = 'XMLHttpRequest' == ( $_SERVER['HTTP_X_REQUESTED_WITH'] ?? '' );
if (!$is_ajax){
2020-04-28 09:52:14 +00:00
header('Location: ' . BASEURL);
2019-08-23 11:39:42 +00:00
}
2020-04-21 18:08:53 +00:00
2019-10-09 17:23:20 +00:00
if (
isset($_POST['automation_id']) &&
$_POST['automation_id'] != '' &&
isset($_POST['action']) &&
$_POST['action'] != ''
) {
$automationId = $_POST['automation_id'];
//Automation Editation of Automations from Buttons/Details
switch ($_POST['action']) {
case 'delete':
AutomationManager::remove($automationId);
die();
break;
2020-04-21 18:08:53 +00:00
case 'deactive':
AutomationManager::deactive($automationId);
die();
break;
2020-04-21 18:08:53 +00:00
case 'restart':
AutomationManager::restart($automationId);
die();
break;
2020-04-21 18:08:53 +00:00
default:
echo 'no action detected';
2019-10-09 17:23:20 +00:00
break;
2019-09-29 07:23:42 +00:00
}
2019-10-09 17:23:20 +00:00
} else if (
isset($_POST['subDevice_id']) &&
$_POST['subDevice_id'] != '' &&
isset($_POST['action']) &&
$_POST['action'] != ''
) {
$subDeviceId = $_POST['subDevice_id'];
switch ($_POST['action']) {
case 'chart':
$period = $_POST['period'];
$groupBy = $_POST['group'];
header('Content-Type: application/json');
$graphData = ChartManager::generateChartData($subDeviceId, $period, $groupBy);
echo Utilities::generateGraphJson($graphData['graphType'], $graphData['graphData'], $graphData['graphRange']);
die();
break;
2020-04-21 18:08:53 +00:00
//Change On/Off Device State of Device Button
case 'change':
$subDeviceData = SubDeviceManager::getSubDevice($subDeviceId);
$deviceId = SubDeviceManager::getSubDeviceMaster($subDeviceId)['device_id'];
if ($subDeviceData['type'] == 'on/off'){
$lastValue = RecordManager::getLastRecord($subDeviceData['subdevice_id'])['value'];
RecordManager::create($deviceId, 'on/off', !$lastValue);
echo (!$lastValue ? 'ON' : 'OFF');
}
die();
break;
2020-04-21 18:08:53 +00:00
//Waitin for execution of Changet walue for Device Button
case 'executed':
echo RecordManager::getLastRecord($subDeviceId)['execuded'];
die();
break;
2020-04-21 18:08:53 +00:00
case 'set':
$value = $_POST['value'];
$subDevice = SubDeviceManager::getSubDevice($subDeviceId);
RecordManager::create($subDevice['device_id'], $subDevice['type'], $value);
echo 'test id' . $subDevice['device_id'] .$subDevice['type'] . $value ;
die();
break;
2020-04-21 18:08:53 +00:00
default:
echo 'no action detected';
2019-10-09 17:23:20 +00:00
break;
2019-09-29 07:23:42 +00:00
}
2019-10-09 17:23:20 +00:00
} else if (
isset($_POST['scene_id']) &&
$_POST['scene_id'] != '' &&
isset($_POST['action']) &&
$_POST['action'] != ''
) {
$sceneId = $_POST['scene_id'];
switch ($_POST['action']) {
case 'delete':
SceneManager::delete($sceneId);
die();
break;
2020-04-21 18:08:53 +00:00
case 'execute':
echo SceneManager::execScene($sceneId);
die();
break;
2020-04-21 18:08:53 +00:00
default:
echo 'no action detected';
2019-10-09 17:23:20 +00:00
break;
}
} else if (
isset($_POST['notification']) &&
$_POST['notification'] != '' &&
isset($_POST['action']) &&
$_POST['action'] != ''
) {
switch ($_POST['action']) {
//add suscription to database
case 'subscribe':
$subscriptionToken = $_POST['token'];
NotificationManager::addSubscriber($_SESSION['user']['id'], $subscriptionToken);
die();
break;
2020-04-21 18:08:53 +00:00
case 'sendTest':
$notificationData = [
'title' => 'Alert',
'body' => 'test notification',
'icon' => BASEDIR . '/app/templates/images/icon-192x192.png',
];
$notificationMng = new NotificationManager;
$subscribers = $notificationMng::getSubscription();
foreach ($subscribers as $key => $subscriber) {
echo $subscriber['user_id'];
if ($subscriber['user_id'] != $_SESSION['user']['id']) continue;
echo $notificationMng::sendSimpleNotification(SERVERKEY, $subscriber['token'], $notificationData);
}
die();
break;
2020-04-21 18:08:53 +00:00
default:
echo 'no action detected';
2019-10-09 17:23:20 +00:00
break;
}
2020-02-18 20:30:44 +00:00
} else if (
isset($_POST['action']) &&
$_POST['action'] != ''
) {
$updateData = [];
$allDevicesData = DeviceManager::getAllDevices();
foreach ($allDevicesData as $deviceKey => $deviceValue) {
$allSubDevices = SubDeviceManager::getAllSubDevices($deviceValue['device_id']);
foreach ($allSubDevices as $key => $subDevicesData) {
2020-04-21 18:08:53 +00:00
$lastRecord = RecordManager::getLastRecord($subDevicesData['subdevice_id']);
$parsedValue = $lastRecord['value'] . $subDevicesData['unit'];
2020-04-21 18:08:53 +00:00
//TODO: udělat parser a ten použít jak v houmu tak zde
switch ($subDevicesData['type']) {
case 'on/off':
$replacementTrue = 'On';
$replacementFalse = 'Off';
$operator = '==';
$breakValue = 1;
break;
2020-04-21 18:08:53 +00:00
case 'door':
$replacementTrue = 'Closed';
$replacementFalse = 'Open';
$operator = '==';
$breakValue = 1;
break;
2020-04-21 18:08:53 +00:00
case 'light':
$replacementTrue = 'Light';
$replacementFalse = 'Dark';
$operator = '==';
$breakValue = 1;
if ($lastRecord['value'] != 1 && $lastRecord['value'] != 0) { //Digital Light Senzor
$operator = '<';
$breakValue = 810;
}
break;
2020-04-21 18:08:53 +00:00
case 'water':
$replacementTrue = 'Wet';
$replacementFalse = 'Dry';
$operator = '==';
$breakValue = 1;
break;
2020-04-21 18:08:53 +00:00
default:
$replacementTrue = '';
$replacementFalse = '';
2020-02-18 20:30:44 +00:00
break;
}
2020-04-21 18:08:53 +00:00
2020-02-18 20:30:44 +00:00
if ($replacementTrue != '' && $replacementFalse != '') {
//parsing last values
$parsedValue = $replacementFalse;
2020-04-21 18:08:53 +00:00
2020-02-18 20:30:44 +00:00
if (Utilities::checkOperator($lastRecord['value'], $operator, $breakValue)) {
$parsedValue = $replacementTrue;
}
}
2020-04-21 18:08:53 +00:00
2020-02-18 20:30:44 +00:00
$updateData[$subDevicesData['subdevice_id']] = [
'time' => $lastRecord['time'],
'value' => $parsedValue,
];
}
}
2020-04-21 18:08:53 +00:00
2020-02-18 20:30:44 +00:00
//TODO: PRO JS VRACET DATA
echo json_encode($updateData, JSON_PRETTY_PRINT);
2019-10-09 17:23:20 +00:00
}
2019-09-29 07:23:42 +00:00
}
2019-09-26 13:53:45 +00:00
}