Termostatic radiator head Preparation #Feature

This commit is contained in:
JonatanRek 2020-03-30 08:56:06 +02:00
parent 0593bf09dd
commit e22d98cf6a
4 changed files with 186 additions and 150 deletions

View File

@ -85,5 +85,23 @@
$partial = new Partial('footer'); $partial = new Partial('footer');
$partial->render(); $partial->render();
?> ?>
<script>
$(document).on('keyup mouseup', '#value_control', function(event) {
$.ajax({
url: 'ajax',
type: 'POST',
//TODO: GET Attribute from ID
data: { subDevice_id : '46', action : 'set', value: this.value},
success: function(msg){
console.log("message");
console.log(msg);
},
error: function (request, status, error) {
console.log('0');
}
});
console.log(this.value);
});
</script>
</body> </body>
</html> </html>

View File

@ -5,6 +5,7 @@ function ajaxPostSimple(path, params, reload = false) {
type: 'POST', type: 'POST',
data: params, data: params,
success: function(msg){ success: function(msg){
console.log("message");
console.log(msg); console.log(msg);
if (reload){ if (reload){
location.reload(); location.reload();

View File

@ -15,7 +15,17 @@ if ($SUBDEVICE['type'] == 'on/off') {
<h5 unselectable="on" class="fa">&#x<?php echo $DEVICE['icon'] ?></h5> <h5 unselectable="on" class="fa">&#x<?php echo $DEVICE['icon'] ?></h5>
</div> </div>
<div class="col"> <div class="col">
<?php if ($SUBDEVICE['type'] == 'temp_cont') { ?>
<input type="number" step="5" class="device-button-value text-right" id="value_control" value="<?php echo $SUBDEVICE['lastRecort']['value'] ?>"><?php echo $SUBDEVICE['unit']?>
<style>
input.device-button-value.text-right {
width: inherit;
}
</style>
<?php } else { ?>
<h5 unselectable="on" class="device-button-value text-right" title="<?php echo $SUBDEVICE['lastRecort']['time']; ?>"><?php echo $SUBDEVICE['lastRecort']['value'] . $SUBDEVICE['unit']?></h5> <h5 unselectable="on" class="device-button-value text-right" title="<?php echo $SUBDEVICE['lastRecort']['time']; ?>"><?php echo $SUBDEVICE['lastRecort']['value'] . $SUBDEVICE['unit']?></h5>
<?php } ?>
</div> </div>
</div> </div>
<div class="row"> <div class="row">

View File

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