From 1ea7e600c203f93571b357a45a8b3c6e3610c57d Mon Sep 17 00:00:00 2001 From: JonatanRek Date: Sat, 24 Aug 2019 13:07:07 +0200 Subject: [PATCH] Automation and api fixes --- api.php | 2 +- class/AutomationManager.php | 27 ++++-- controls/automation.php | 7 +- controls/home.php | 5 +- templates/automation.phtml | 1 - templates/js/post.js | 21 +++- templates/js/script.js | 108 +++++++++++++++------ templates/part/automationCreate.phtml | 2 +- templates/part/automationCreateFinal.phtml | 3 +- templates/part/automationEdit.phtml | 3 +- templates/part/deviceButton.phtml | 2 +- views/Ajax.php | 12 ++- views/Automation.php | 13 ++- 13 files changed, 152 insertions(+), 54 deletions(-) diff --git a/api.php b/api.php index 30301ce..7960ed7 100644 --- a/api.php +++ b/api.php @@ -41,7 +41,7 @@ if (isset($obj['user']) && $obj['user'] != ''){ } //automationExecution -//AutomationManager::executeAll(); +AutomationManager::executeAll(); //Record Cleaning RecordManager::clean(RECORDTIMOUT); diff --git a/class/AutomationManager.php b/class/AutomationManager.php index 374169c..d71ec63 100644 --- a/class/AutomationManager.php +++ b/class/AutomationManager.php @@ -16,8 +16,8 @@ class AutomationManager{ $scene = array ( 'name' => $name, 'on_days' => $onDays, - 'if_something' => $doCode, - 'do_something' => $ifCode, + 'if_something' => $ifCode, + 'do_something' => $doCode, ); try { Db::add ('automation', $scene); @@ -33,11 +33,12 @@ class AutomationManager{ } public function executeAll(){ - global $dateTimeOffset; + $dateTimeOffset; $automations = Db::loadAll ("SELECT * FROM automation"); $dayNameNow = strtolower (date('D', time())); + foreach ($automations as $automation) { - $onValue = $automation['if_something']; + $onValue = json_decode($automation['if_something'], true); $sceneDoJson = $automation['do_something']; $actionDays = json_decode($automation['on_days'], true); $value = time(); @@ -46,18 +47,25 @@ class AutomationManager{ if ($automation['active'] != 0){ if (in_array($dayNameNow, $actionDays)){ - if (in_array($onValue, ['sunSet','time','now'])) { - if ($onValue == 'sunSet') { + if (in_array($onValue['type'], ['sunSet','time','now'])) { + + if ($onValue['type'] == 'sunSet') { $value = date_sunset($value, SUNFUNCS_RET_TIMESTAMP, 50.0755381 , 14.4378005, 90, $dateTimeOffset); - } else if ($onValue == 'sunRise') { + } else if ($onValue['type'] == 'sunRise') { $value = date_sunrise($value, SUNFUNCS_RET_TIMESTAMP, 50.0755381 , 14.4378005, 90, $dateTimeOffset); - } else if ($onValue == 'time') { - $onValue = explode(':',$onValue); + } else if ($onValue['type'] == 'time') { + $onValue = explode(':',$onValue['value']); $today = date_create('now'); $onValue = $today->setTime($onValue[0], $onValue[1]); $value = $today->getTimestamp(); } + /* + Echo "Spouštění Automatizace"; + echo "Aktual" . date( "HH:mm", strtotime(time())); + echo "Run At" . date( "HH:mm", strtotime($value)); + */ + if (time() > $value){ if ($automation['executed'] == 0){ $run = true; @@ -89,3 +97,4 @@ class AutomationManager{ } } } +} diff --git a/controls/automation.php b/controls/automation.php index 277eb2f..08bc32f 100644 --- a/controls/automation.php +++ b/controls/automation.php @@ -1,8 +1,11 @@ $_POST['atSelector'], + "value" => $_POST['atSelectorValue'], + ], JSON_PRETTY_PRINT); $onDays = $_POST['atDays']; AutomationManager::create('name', $onDays, $doCode, $ifCode); diff --git a/controls/home.php b/controls/home.php index 756b387..039697f 100644 --- a/controls/home.php +++ b/controls/home.php @@ -13,13 +13,16 @@ if (isset($_POST) && !empty($_POST)){ $deviceOwnerUserId = $_POST['deviceOwnerUserId']; + $deviceOwnerRoomId = $_POST['deviceOwnerId']; + try { $args = array( 'owner' => $deviceOwnerUserId, 'name' => $deviceName, 'icon' => $deviceIcon, 'permission' => $permissionsInJson, - 'sleep_time' => $sleepTime + 'sleep_time' => $sleepTime, + 'room_id' => $deviceOwnerRoomId, ); DeviceManager::edit($deviceId, $args); } catch (\Exception $e) { diff --git a/templates/automation.phtml b/templates/automation.phtml index 2c03d14..e7739a9 100644 --- a/templates/automation.phtml +++ b/templates/automation.phtml @@ -52,7 +52,6 @@ prepare('lang',$LANG); diff --git a/templates/js/post.js b/templates/js/post.js index e8f8aa7..6ab734d 100644 --- a/templates/js/post.js +++ b/templates/js/post.js @@ -1,3 +1,22 @@ +function ajaxPostSimple(path, params, reload = false) { + navigator.vibrate([200]); + $.ajax({ + url: path, + type: 'POST', + data: params, + success: function(msg){ + console.log(msg); + if (reload){ + location.reload(); + } + }, + error: function (request, status, error) { + console.log('0'); + } + }); + return false; +} + function ajaxPost(path, params, self, reload = false) { navigator.vibrate([200]); $.ajax({ @@ -14,7 +33,7 @@ function ajaxPost(path, params, self, reload = false) { } console.log(msg); if (reload){ - location.reload(); + //location.reload(); } }, error: function (request, status, error) { diff --git a/templates/js/script.js b/templates/js/script.js index 8f19f87..f4f7012 100644 --- a/templates/js/script.js +++ b/templates/js/script.js @@ -10,15 +10,21 @@ if ('serviceWorker' in navigator) { }); } -$('#valueSelector').change(function() { + +$('select').change(function(e) { console.log($(this).val()); if( $(this).val() == 'time') { + $('#atTime').prop( "disabled", false ); + + $('#atDeviceValue').prop( "disabled", true ); + $('#atDeviceValueInt').prop( "disabled", true ); } else if( $(this).val() == 'atDeviceValue') { + $('#atDeviceValue').prop( "disabled", false ); $('#atDeviceValueInt').prop( "disabled", false ); - } else { - $('#atDeviceValue').prop( "disabled", true ); + + $('#atTime').prop( "disabled", true ); } }); @@ -30,14 +36,32 @@ $("div.square-content").on('touchend', function (e){ }); $("div.square-content").on('touchstart', function (eTarget) { - var id = $(this).attr('id').replace('device-', ''); + navigator.vibrate([500]); + var id = ''; + + var windowLoc = $(location).attr('pathname'); + windowLoc = windowLoc.substring(windowLoc.lastIndexOf("/")); + console.log(windowLoc); + if (windowLoc == "/") { + id = $(this).attr('id').replace('device-', ''); + } else if (windowLoc == "/scene") { + id = $(this).attr('id').replace('scene-', ''); + } else if (windowLoc == "/automation") { + id = $(this).attr('id').replace('automation-', ''); + } + var subId = $(this).attr('data-sub-device-id'); touch++; if(touch == 2 && touchSubId == subId){ console.log("Detail"); - $("#modal-detail-"+subId).removeClass('modal-container-hiden').show(); - ajaxChart(subId); + if (windowLoc == "/") { + $("#modal-detail-"+subId).removeClass('modal-container-hiden').show(); + ajaxChart(subId); + } else if (windowLoc == "/scene") { + + } else if (windowLoc == "/automation") { + } touch = 0; touchSubId = ""; return; @@ -47,6 +71,7 @@ $("div.square-content").on('touchstart', function (eTarget) { pressTimer = window.setTimeout(function (e) { console.log("Setting"); $("#modal-setting-"+id).removeClass('modal-container-hiden').show(); + touch = 0; }, 500); }); @@ -60,6 +85,8 @@ $("div.square-content").mousedown(function(e) { id = $(this).attr('id').replace('device-', ''); } else if (windowLoc == "/scene") { id = $(this).attr('id').replace('scene-', ''); + } else if (windowLoc == "/automation") { + id = $(this).attr('id').replace('automation-', ''); } $("#modal-setting-"+id).removeClass('modal-container-hiden').show(); console.log("Setting"); @@ -155,31 +182,36 @@ $( '[name="room"]' ).change(function (e) { }); -var autoUpdate = setInterval(function(){ - $.ajax({ - url: 'ajax', - type: 'POST', - dataType: 'json', - data: { - "action": 'getState' - }, - success: function(data){ - // console.log('DATA: ', data); - for (const key in data) { - if (data.hasOwnProperty(key)) { - const device = data[key]; - $('[data-sub-device-id="'+key+'"]') - .find('.device-button-value') - .text(device['value']) - .attr('title',device['time']) +var windowLoc = $(location).attr('pathname'); +windowLoc = windowLoc.substring(windowLoc.lastIndexOf("/")); +console.log(); +if (windowLoc == "/") { + var autoUpdate = setInterval(function(){ + $.ajax({ + url: 'ajax', + type: 'POST', + dataType: 'json', + data: { + "action": 'getState' + }, + success: function(data){ + // console.log('DATA: ', data); + for (const key in data) { + if (data.hasOwnProperty(key)) { + const device = data[key]; + $('[data-sub-device-id="'+key+'"]') + .find('.device-button-value') + .text(device['value']) + .attr('title',device['time']) + } } + }, + error: function (request, status, error) { + console.log("ERROR ajaxChart():", request, error); } - }, - error: function (request, status, error) { - console.log("ERROR ajaxChart():", request, error); - } - }); -},2000); + }); + },2000); +} @@ -191,6 +223,22 @@ $('.graph-period').on('click', function (e) { ajaxChart(subId, period, groupBy); }); +$( "button[name=remove]" ).click(function(e) { + if (confirm('Are you shure ?')) { + var windowLoc = $(location).attr('pathname'); + windowLoc = windowLoc.substring(windowLoc.lastIndexOf("/")); + console.log(windowLoc); + var id = null; + if (windowLoc == "/scene") { + id = $(this).data('scene-id'); + $("#scene-"+id+"-content").remove(); + } else if (windowLoc == "/automation") { + id = $(this).data('automation-id'); + $("#automation-"+id+"-content").remove(); + } + } +}); + @@ -219,8 +267,6 @@ $('.graph-period').on('click', function (e) { - - diff --git a/templates/part/automationCreate.phtml b/templates/part/automationCreate.phtml index f4c54a3..04b074a 100644 --- a/templates/part/automationCreate.phtml +++ b/templates/part/automationCreate.phtml @@ -29,7 +29,7 @@ diff --git a/templates/part/automationCreateFinal.phtml b/templates/part/automationCreateFinal.phtml index cd64174..909ece1 100644 --- a/templates/part/automationCreateFinal.phtml +++ b/templates/part/automationCreateFinal.phtml @@ -4,7 +4,8 @@

- + - + +
\ No newline at end of file diff --git a/templates/part/deviceButton.phtml b/templates/part/deviceButton.phtml index cde0f77..1bdf669 100644 --- a/templates/part/deviceButton.phtml +++ b/templates/part/deviceButton.phtml @@ -8,7 +8,7 @@ if ($SUBDEVICE['type'] == 'on/off') {
data-room-id="">
-
" id="device-" data-sub-device-id=""> +
" id="device-" data-sub-device-id="">
diff --git a/views/Ajax.php b/views/Ajax.php index 69b4dca..cf224c8 100644 --- a/views/Ajax.php +++ b/views/Ajax.php @@ -41,7 +41,11 @@ class Ajax extends Template } else if (isset($_POST['automation_id'])){ $automationId = $_POST['automation_id']; - AutomationManager::deactive($automationId); + 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']; @@ -154,7 +158,11 @@ class Ajax extends Template die(); } else if (isset($_POST['scene_id'])) { $sceneId = $_POST['scene_id']; - echo SceneManager::execScene($sceneId); + if (isset($_POST['action']) && $_POST['action'] == 'delete') { + SceneManager::delete($sceneId); + }else { + echo SceneManager::execScene($sceneId); + } } die(); diff --git a/views/Automation.php b/views/Automation.php index 0ef1a20..6360af4 100644 --- a/views/Automation.php +++ b/views/Automation.php @@ -19,11 +19,19 @@ class Automation extends Template $automations = []; $automationsData = AutomationManager::getAll(); foreach ($automationsData as $automationKey => $automationData) { + $doSomething = []; + foreach (json_decode($automationData['do_something']) as $subdeviceId => $subDeviceState) { + $subDeviceMasterDeviceData = SubDeviceManager::getSubDeviceMaster($subdeviceId); + $doSomething[$subdeviceId] = [ + 'name' => $subDeviceMasterDeviceData['name'], + 'state' => $subDeviceState, + ]; + } $automations[$automationData['automation_id']] = [ 'name' => '', - 'onDays' => implode(', ',json_decode($automationData['on_days'])), + 'onDays' => json_decode($automationData['on_days']), 'ifSomething' => $automationData['if_something'], - 'doSomething' => $automationData['do_something'], + 'doSomething' => $doSomething, 'active' => $automationData['active'], ]; } @@ -37,6 +45,7 @@ class Automation extends Template $approvedSubDevices[$subDeviceValue['subdevice_id']] = [ 'name' => $allDevicesData[$deviceKey]['name'], 'type' => $subDeviceValue['type'], + 'masterDevice' => $subDeviceValue['device_id'], ]; } }