Ajax Refactor/Oprimalization
This commit is contained in:
parent
00bccd5224
commit
cb4d8a89a0
@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
$action = "";
|
$action = "";
|
||||||
if ($SUBDEVICE['type'] == 'on/off') {
|
if ($SUBDEVICE['type'] == 'on/off') {
|
||||||
$action = 'onClick="ajaxPost(\'ajax\',{subDevice_id:\'' . $SUBDEVICEID . '\'}, this);"';
|
$action = 'onClick="ajaxPost(\'ajax\',{subDevice_id:\'' . $SUBDEVICEID . '\', action:\'change\'}, this);"';
|
||||||
}
|
}
|
||||||
//neaktivní zařízení is-inactive
|
//neaktivní zařízení is-inactive
|
||||||
?>
|
?>
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
<?php //TODO DOD2LAT UKL8D8N9?>
|
|
||||||
<div class="modal-container modal-container-hiden" id="modal-setting-<?php echo $SCENEID ?>">
|
<div class="modal-container modal-container-hiden" id="modal-setting-<?php echo $SCENEID ?>">
|
||||||
<div class="modal">
|
<div class="modal">
|
||||||
<div class="close">
|
<div class="close">
|
||||||
|
@ -21,165 +21,108 @@ class Ajax extends Template
|
|||||||
header('Location: ./');
|
header('Location: ./');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($_POST['subDevice_id'])){
|
if (
|
||||||
$subDeviceId = $_POST['subDevice_id'];
|
isset($_POST['automation_id']) &&
|
||||||
if (isset($_POST['lastRecord'])){
|
$_POST['automation_id'] != '' &&
|
||||||
echo RecordManager::getLastRecord($subDeviceId)['execuded'];
|
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();
|
die();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'deactive':
|
||||||
|
AutomationManager::deactive($automationId);
|
||||||
|
die();
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
echo 'no action detected';
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
} 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;
|
||||||
|
|
||||||
|
//Change On/Off Device State of Device Button
|
||||||
|
case 'change':
|
||||||
$subDeviceData = SubDeviceManager::getSubDevice($subDeviceId);
|
$subDeviceData = SubDeviceManager::getSubDevice($subDeviceId);
|
||||||
$deviceId = SubDeviceManager::getSubDeviceMaster($subDeviceId)['device_id'];
|
$deviceId = SubDeviceManager::getSubDeviceMaster($subDeviceId)['device_id'];
|
||||||
if ($subDeviceData['type'] == 'on/off'){
|
if ($subDeviceData['type'] == 'on/off'){
|
||||||
//TODO: Pridelat kontrolu změnit stav pouze pokud se poslední [executed] stav != novému
|
$lastValue = RecordManager::getLastRecord($subDeviceData['subdevice_id'])['value'];
|
||||||
if (RecordManager::getLastRecord($subDeviceData['subdevice_id'])['value'] == 0){
|
RecordManager::create($deviceId, 'on/off', !$lastValue);
|
||||||
RecordManager::create($deviceId, 'on/off', 1);
|
echo ($lastValue ? 'ON' : 'OFF');
|
||||||
echo 'ON';
|
|
||||||
}else{
|
|
||||||
RecordManager::create($deviceId, 'on/off', 0);
|
|
||||||
echo 'OFF';
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
} else if (isset($_POST['automation_id'])){
|
|
||||||
$automationId = $_POST['automation_id'];
|
|
||||||
if (isset($_POST['action']) && $_POST['action'] == 'delete') {
|
|
||||||
AutomationManager::remove($automationId);
|
|
||||||
}else {
|
|
||||||
AutomationManager::deactive($automationId);
|
|
||||||
}
|
|
||||||
} else if (isset($_POST['subDevice']) && isset($_POST['action']) && $_POST['action'] == "chart") {
|
|
||||||
//TODO lepe rozstrukturovat
|
|
||||||
$subDeviceId = $_POST['subDevice'];
|
|
||||||
$period = $_POST['period'];
|
|
||||||
$groupBy = $_POST['group'];
|
|
||||||
|
|
||||||
$subDevice = SubDeviceManager::getSubDevice($subDeviceId);
|
|
||||||
$records = RecordManager::getAllRecordForGraph($subDeviceId, $period, $groupBy);
|
|
||||||
|
|
||||||
$array = array_column($records, 'value');
|
|
||||||
$arrayTime = array_column($records, 'time');
|
|
||||||
$output = [];
|
|
||||||
|
|
||||||
foreach ($array as $key => $value) {
|
|
||||||
$output[$key]['y'] = $value;
|
|
||||||
if ($subDevice['type'] == 'light'){
|
|
||||||
if ($value > 810){
|
|
||||||
$output[$key]['y'] = 1;
|
|
||||||
} else {
|
|
||||||
$output[$key]['y'] = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$timeStamp = new DateTime($arrayTime[$key]);
|
|
||||||
$output[$key]['t'] = $timeStamp->format("Y-m-d") . 'T' . $timeStamp->format("H:i:s") . 'Z';
|
|
||||||
}
|
|
||||||
|
|
||||||
$data = json_encode($output);
|
|
||||||
$data = $output;
|
|
||||||
$arrayTimeStamps = array_column($records, 'time');
|
|
||||||
foreach ($arrayTimeStamps as $key => $value) {
|
|
||||||
$arrayTimeStamps[$key] = (new DateTime($value))->format(TIMEFORMAT);
|
|
||||||
}
|
|
||||||
|
|
||||||
$labels = json_encode($arrayTimeStamps);
|
|
||||||
$range = RANGES[$subDevice['type']];
|
|
||||||
$graphType = $range['graph'];
|
|
||||||
|
|
||||||
header('Content-Type: application/json');
|
|
||||||
|
|
||||||
echo Utilities::generateGraphJson($range['graph'], $data, $range);
|
|
||||||
die();
|
die();
|
||||||
} else if (isset($_POST['action']) && $_POST['action'] == "getState") {
|
break;
|
||||||
//State Update
|
|
||||||
$roomsData = RoomManager::getAllRooms();
|
//Waitin for execution of Changet walue for Device Button
|
||||||
$subDevices = [];
|
case 'executed':
|
||||||
foreach ($roomsData as $roomKey => $roomsData) {
|
echo RecordManager::getLastRecord($subDeviceId)['execuded'];
|
||||||
$devicesData = DeviceManager::getAllDevicesInRoom($roomsData['room_id']);
|
|
||||||
foreach ($devicesData as $deviceKey => $deviceData) {
|
|
||||||
$subDevicesData = SubDeviceManager::getAllSubDevices($deviceData['device_id']);
|
|
||||||
foreach ($subDevicesData as $subDeviceKey => $subDeviceData) {
|
|
||||||
$lastRecord = RecordManager::getLastRecord($subDeviceData['subdevice_id']);
|
|
||||||
$parsedValue = round($lastRecord['value']);
|
|
||||||
//TODO: Předelat na switch snažší přidávání
|
|
||||||
/*Value Parsing*/
|
|
||||||
if ($subDeviceData['type'] == "on/off") {
|
|
||||||
$parsedValue = ($parsedValue == 1 ? 'ON' : 'OFF');
|
|
||||||
}
|
|
||||||
if ($subDeviceData['type'] == "light") {
|
|
||||||
$replacementTrue = 'Light';
|
|
||||||
$replacementFalse = 'Dark';
|
|
||||||
if ($parsedValue != 1){
|
|
||||||
//Analog Reading
|
|
||||||
$parsedValue = ($parsedValue <= 810 ? $replacementTrue : $replacementFalse);
|
|
||||||
} else {
|
|
||||||
//Digital Reading
|
|
||||||
$parsedValue = ($parsedValue == 0 ? $replacementTrue : $replacementFalse);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ($subDeviceData['type'] == "door") {
|
|
||||||
$replacementTrue = 'Closed';
|
|
||||||
$replacementFalse = 'Opened';
|
|
||||||
$parsedValue = ($parsedValue == 1 ? $replacementTrue : $replacementFalse);
|
|
||||||
}
|
|
||||||
$subDevices[$subDeviceData['subdevice_id']] = [
|
|
||||||
'value' => $parsedValue .$subDeviceData['unit'],
|
|
||||||
'time' => $lastRecord['time'],
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
echo json_encode($subDevices);
|
|
||||||
die();
|
die();
|
||||||
} else if (isset($_POST['scene_id'])) {
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
echo 'no action detected';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else if (
|
||||||
|
isset($_POST['scene_id']) &&
|
||||||
|
$_POST['scene_id'] != '' &&
|
||||||
|
isset($_POST['action']) &&
|
||||||
|
$_POST['action'] != ''
|
||||||
|
) {
|
||||||
$sceneId = $_POST['scene_id'];
|
$sceneId = $_POST['scene_id'];
|
||||||
if (isset($_POST['action']) && $_POST['action'] == 'delete') {
|
switch ($_POST['action']) {
|
||||||
|
case 'delete':
|
||||||
SceneManager::delete($sceneId);
|
SceneManager::delete($sceneId);
|
||||||
}else {
|
|
||||||
echo SceneManager::execScene($sceneId);
|
|
||||||
}
|
|
||||||
} else if (isset($_POST['token'])) {
|
|
||||||
NotificationManager::addSubscriber($_SESSION['user']['id'], $_POST['token']);
|
|
||||||
}
|
|
||||||
|
|
||||||
die();
|
die();
|
||||||
|
break;
|
||||||
|
|
||||||
}
|
case 'execute':
|
||||||
}
|
echo SceneManager::execScene($sceneId);
|
||||||
|
die();
|
||||||
|
break;
|
||||||
|
|
||||||
/*$JSON = '{
|
default:
|
||||||
"type": "line",
|
echo 'no action detected';
|
||||||
"data": {
|
break;
|
||||||
"labels": ' . $data . ',
|
}
|
||||||
"datasets": [{
|
} else if (
|
||||||
"data": ' . $data . ',
|
isset($_POST['notification']) &&
|
||||||
"backgroundColor": "#7522bf",
|
$_POST['notification'] != '' &&
|
||||||
"lineTension": 0,
|
isset($_POST['action']) &&
|
||||||
"radius": 5
|
$_POST['action'] != ''
|
||||||
}]
|
) {
|
||||||
},
|
switch ($_POST['action']) {
|
||||||
"options": {
|
//add suscription to database
|
||||||
"legend": {
|
case 'subscribe':
|
||||||
"display": false
|
$subscriptionToken = $_POST['Token'];
|
||||||
},
|
NotificationManager::addSubscriber($_SESSION['user']['id'], $subscriptionToken);
|
||||||
"scales": {
|
die();
|
||||||
"xAxes": [{
|
break;
|
||||||
"type": "time",
|
|
||||||
"time": {
|
default:
|
||||||
"unit": "hour"
|
echo 'no action detected';
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}],
|
|
||||||
"yAxes": [{
|
|
||||||
"ticks": {
|
|
||||||
"min": ' . $range['min'] . ',
|
|
||||||
"max": ' . $range['max'] . ',
|
|
||||||
"steps": ' . $range['scale'] . '
|
|
||||||
}
|
|
||||||
}]
|
|
||||||
},
|
|
||||||
"tooltips": {
|
|
||||||
"enabled": false
|
|
||||||
},
|
|
||||||
"hover": {
|
|
||||||
"mode": null
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}';*/
|
|
||||||
|
Loading…
Reference in New Issue
Block a user