PHP_SMART_HOME_V3/app/views/Ajax.php

151 lines
3.7 KiB
PHP
Raw Normal View History

2019-08-23 11:39:42 +00:00
<?php
2019-09-19 12:48:31 +00:00
$files = scandir('app/class/');
2019-08-23 11:39:42 +00:00
$files = array_diff($files, array('.', '..'));
foreach($files as $file) {
2019-09-19 12:48:31 +00:00
include_once 'app/class/'. $file;
2019-08-23 11:39:42 +00:00
}
class Ajax extends Template
{
function __construct()
{
global $userManager;
global $lang;
if (!$userManager->isLogin()){
2019-10-11 14:32:05 +00:00
header('Location: ' . BASEDIR);
2019-08-23 11:39:42 +00:00
}
$is_ajax = 'XMLHttpRequest' == ( $_SERVER['HTTP_X_REQUESTED_WITH'] ?? '' );
if (!$is_ajax){
2019-10-11 14:32:05 +00:00
header('Location: ' . BASEDIR);
2019-08-23 11:39:42 +00:00
}
2019-10-09 17:23:20 +00:00
if (
isset($_POST['automation_id']) &&
$_POST['automation_id'] != '' &&
isset($_POST['action']) &&
$_POST['action'] != ''
) {
2019-08-23 11:39:42 +00:00
$automationId = $_POST['automation_id'];
2019-10-09 17:23:20 +00:00
//Automation Editation of Automations from Buttons/Details
switch ($_POST['action']) {
case 'delete':
2019-08-24 11:07:07 +00:00
AutomationManager::remove($automationId);
2019-10-09 17:23:20 +00:00
die();
break;
2019-08-23 11:39:42 +00:00
2019-10-09 17:23:20 +00:00
case 'deactive':
AutomationManager::deactive($automationId);
die();
break;
2019-08-23 11:39:42 +00:00
case 'restart':
AutomationManager::restart($automationId);
die();
break;
2019-10-09 17:23:20 +00:00
default:
echo 'no action detected';
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;
2019-08-23 11:39:42 +00:00
2019-10-09 17:23:20 +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);
2019-10-11 08:44:13 +00:00
echo (!$lastValue ? 'ON' : 'OFF');
2019-10-09 17:23:20 +00:00
}
die();
break;
2019-09-29 07:23:42 +00:00
2019-10-09 17:23:20 +00:00
//Waitin for execution of Changet walue for Device Button
case 'executed':
echo RecordManager::getLastRecord($subDeviceId)['execuded'];
die();
break;
2019-09-29 07:23:42 +00:00
2019-10-09 17:23:20 +00:00
default:
echo 'no action detected';
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'] != ''
) {
2019-09-29 07:23:42 +00:00
$sceneId = $_POST['scene_id'];
2019-10-09 17:23:20 +00:00
switch ($_POST['action']) {
case 'delete':
2019-09-29 07:23:42 +00:00
SceneManager::delete($sceneId);
2019-10-09 17:23:20 +00:00
die();
break;
case 'execute':
2019-09-29 07:23:42 +00:00
echo SceneManager::execScene($sceneId);
2019-10-09 17:23:20 +00:00
die();
break;
2019-09-29 07:23:42 +00:00
2019-10-09 17:23:20 +00:00
default:
echo 'no action detected';
break;
}
} else if (
isset($_POST['notification']) &&
$_POST['notification'] != '' &&
isset($_POST['action']) &&
$_POST['action'] != ''
) {
switch ($_POST['action']) {
//add suscription to database
case 'subscribe':
2019-10-11 08:44:13 +00:00
$subscriptionToken = $_POST['token'];
2019-10-09 17:23:20 +00:00
NotificationManager::addSubscriber($_SESSION['user']['id'], $subscriptionToken);
die();
break;
2019-09-26 13:53:45 +00:00
2019-10-11 08:44:13 +00:00
case 'sendTest':
echo "test";
$notificationData = [
'title' => 'Alert',
'body' => 'test notification',
'icon' => '',
];
$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;
2019-10-09 17:23:20 +00:00
default:
echo 'no action detected';
break;
}
}
2019-09-29 07:23:42 +00:00
}
2019-09-26 13:53:45 +00:00
}