Automation and api fixes

This commit is contained in:
JonatanRek 2019-08-24 13:07:07 +02:00
parent 97e955d382
commit 1ea7e600c2
13 changed files with 152 additions and 54 deletions

View File

@ -41,7 +41,7 @@ if (isset($obj['user']) && $obj['user'] != ''){
} }
//automationExecution //automationExecution
//AutomationManager::executeAll(); AutomationManager::executeAll();
//Record Cleaning //Record Cleaning
RecordManager::clean(RECORDTIMOUT); RecordManager::clean(RECORDTIMOUT);

View File

@ -16,8 +16,8 @@ class AutomationManager{
$scene = array ( $scene = array (
'name' => $name, 'name' => $name,
'on_days' => $onDays, 'on_days' => $onDays,
'if_something' => $doCode, 'if_something' => $ifCode,
'do_something' => $ifCode, 'do_something' => $doCode,
); );
try { try {
Db::add ('automation', $scene); Db::add ('automation', $scene);
@ -33,11 +33,12 @@ class AutomationManager{
} }
public function executeAll(){ public function executeAll(){
global $dateTimeOffset; $dateTimeOffset;
$automations = Db::loadAll ("SELECT * FROM automation"); $automations = Db::loadAll ("SELECT * FROM automation");
$dayNameNow = strtolower (date('D', time())); $dayNameNow = strtolower (date('D', time()));
foreach ($automations as $automation) { foreach ($automations as $automation) {
$onValue = $automation['if_something']; $onValue = json_decode($automation['if_something'], true);
$sceneDoJson = $automation['do_something']; $sceneDoJson = $automation['do_something'];
$actionDays = json_decode($automation['on_days'], true); $actionDays = json_decode($automation['on_days'], true);
$value = time(); $value = time();
@ -46,18 +47,25 @@ class AutomationManager{
if ($automation['active'] != 0){ if ($automation['active'] != 0){
if (in_array($dayNameNow, $actionDays)){ if (in_array($dayNameNow, $actionDays)){
if (in_array($onValue, ['sunSet','time','now'])) { if (in_array($onValue['type'], ['sunSet','time','now'])) {
if ($onValue == 'sunSet') {
if ($onValue['type'] == 'sunSet') {
$value = date_sunset($value, SUNFUNCS_RET_TIMESTAMP, 50.0755381 , 14.4378005, 90, $dateTimeOffset); $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); $value = date_sunrise($value, SUNFUNCS_RET_TIMESTAMP, 50.0755381 , 14.4378005, 90, $dateTimeOffset);
} else if ($onValue == 'time') { } else if ($onValue['type'] == 'time') {
$onValue = explode(':',$onValue); $onValue = explode(':',$onValue['value']);
$today = date_create('now'); $today = date_create('now');
$onValue = $today->setTime($onValue[0], $onValue[1]); $onValue = $today->setTime($onValue[0], $onValue[1]);
$value = $today->getTimestamp(); $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 (time() > $value){
if ($automation['executed'] == 0){ if ($automation['executed'] == 0){
$run = true; $run = true;
@ -89,3 +97,4 @@ class AutomationManager{
} }
} }
} }
}

View File

@ -1,8 +1,11 @@
<?php <?php
if (isset($_POST) && !empty($_POST)){ if (isset($_POST) && !empty($_POST)){
if (isset($_POST['modalFinal']) && $_POST['modalFinal'] == "Next") { if (isset($_POST['modalFinal']) && $_POST['modalFinal'] == "Next") {
$ifCode = json_encode($_POST['device'], JSON_PRETTY_PRINT); $doCode = json_encode($_POST['device'], JSON_PRETTY_PRINT);
$doCode = $_POST['atSelector']; $ifCode = json_encode([
"type" => $_POST['atSelector'],
"value" => $_POST['atSelectorValue'],
], JSON_PRETTY_PRINT);
$onDays = $_POST['atDays']; $onDays = $_POST['atDays'];
AutomationManager::create('name', $onDays, $doCode, $ifCode); AutomationManager::create('name', $onDays, $doCode, $ifCode);

View File

@ -13,13 +13,16 @@ if (isset($_POST) && !empty($_POST)){
$deviceOwnerUserId = $_POST['deviceOwnerUserId']; $deviceOwnerUserId = $_POST['deviceOwnerUserId'];
$deviceOwnerRoomId = $_POST['deviceOwnerId'];
try { try {
$args = array( $args = array(
'owner' => $deviceOwnerUserId, 'owner' => $deviceOwnerUserId,
'name' => $deviceName, 'name' => $deviceName,
'icon' => $deviceIcon, 'icon' => $deviceIcon,
'permission' => $permissionsInJson, 'permission' => $permissionsInJson,
'sleep_time' => $sleepTime 'sleep_time' => $sleepTime,
'room_id' => $deviceOwnerRoomId,
); );
DeviceManager::edit($deviceId, $args); DeviceManager::edit($deviceId, $args);
} catch (\Exception $e) { } catch (\Exception $e) {

View File

@ -52,7 +52,6 @@
</div> </div>
</div> </div>
<?php <?php
ECHO $_POST['modalNext'];
if (isset($_POST['modalNext'])) { if (isset($_POST['modalNext'])) {
$partial = new Partial('automationCreateFinal'); $partial = new Partial('automationCreateFinal');
$partial->prepare('lang',$LANG); $partial->prepare('lang',$LANG);

View File

@ -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) { function ajaxPost(path, params, self, reload = false) {
navigator.vibrate([200]); navigator.vibrate([200]);
$.ajax({ $.ajax({
@ -14,7 +33,7 @@ function ajaxPost(path, params, self, reload = false) {
} }
console.log(msg); console.log(msg);
if (reload){ if (reload){
location.reload(); //location.reload();
} }
}, },
error: function (request, status, error) { error: function (request, status, error) {

View File

@ -10,15 +10,21 @@ if ('serviceWorker' in navigator) {
}); });
} }
$('#valueSelector').change(function() {
$('select').change(function(e) {
console.log($(this).val()); console.log($(this).val());
if( $(this).val() == 'time') { if( $(this).val() == 'time') {
$('#atTime').prop( "disabled", false ); $('#atTime').prop( "disabled", false );
$('#atDeviceValue').prop( "disabled", true );
$('#atDeviceValueInt').prop( "disabled", true );
} else if( $(this).val() == 'atDeviceValue') { } else if( $(this).val() == 'atDeviceValue') {
$('#atDeviceValue').prop( "disabled", false ); $('#atDeviceValue').prop( "disabled", false );
$('#atDeviceValueInt').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) { $("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'); var subId = $(this).attr('data-sub-device-id');
touch++; touch++;
if(touch == 2 && touchSubId == subId){ if(touch == 2 && touchSubId == subId){
console.log("Detail"); console.log("Detail");
if (windowLoc == "/") {
$("#modal-detail-"+subId).removeClass('modal-container-hiden').show(); $("#modal-detail-"+subId).removeClass('modal-container-hiden').show();
ajaxChart(subId); ajaxChart(subId);
} else if (windowLoc == "/scene") {
} else if (windowLoc == "/automation") {
}
touch = 0; touch = 0;
touchSubId = ""; touchSubId = "";
return; return;
@ -47,6 +71,7 @@ $("div.square-content").on('touchstart', function (eTarget) {
pressTimer = window.setTimeout(function (e) { pressTimer = window.setTimeout(function (e) {
console.log("Setting"); console.log("Setting");
$("#modal-setting-"+id).removeClass('modal-container-hiden').show(); $("#modal-setting-"+id).removeClass('modal-container-hiden').show();
touch = 0;
}, 500); }, 500);
}); });
@ -60,6 +85,8 @@ $("div.square-content").mousedown(function(e) {
id = $(this).attr('id').replace('device-', ''); id = $(this).attr('id').replace('device-', '');
} else if (windowLoc == "/scene") { } else if (windowLoc == "/scene") {
id = $(this).attr('id').replace('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(); $("#modal-setting-"+id).removeClass('modal-container-hiden').show();
console.log("Setting"); console.log("Setting");
@ -155,7 +182,11 @@ $( '[name="room"]' ).change(function (e) {
}); });
var autoUpdate = setInterval(function(){ var windowLoc = $(location).attr('pathname');
windowLoc = windowLoc.substring(windowLoc.lastIndexOf("/"));
console.log();
if (windowLoc == "/") {
var autoUpdate = setInterval(function(){
$.ajax({ $.ajax({
url: 'ajax', url: 'ajax',
type: 'POST', type: 'POST',
@ -179,7 +210,8 @@ var autoUpdate = setInterval(function(){
console.log("ERROR ajaxChart():", request, error); console.log("ERROR ajaxChart():", request, error);
} }
}); });
},2000); },2000);
}
@ -191,6 +223,22 @@ $('.graph-period').on('click', function (e) {
ajaxChart(subId, period, groupBy); 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) {

View File

@ -29,7 +29,7 @@
<select class="input" name="devices[]" multiple> <select class="input" name="devices[]" multiple>
<?php foreach ($SUBDEVICES as $subDeviceKey => $subDeviceValue){ <?php foreach ($SUBDEVICES as $subDeviceKey => $subDeviceValue){
if ($subDeviceValue['type'] != 'on/off') continue;?> if ($subDeviceValue['type'] != 'on/off') continue;?>
<option value="<?php echo $subDeviceKey; ?>"><?php echo $subDeviceValue['name']; ?></option> <option value="<?php echo $subDeviceValue['masterDevice']; ?>"><?php echo $subDeviceValue['name']; ?></option>
<?php } ?> <?php } ?>
</select> </select>

View File

@ -4,7 +4,8 @@
<h4 class="mb-4"><?php echo $LANG['t_createAutomation']?></h4> <h4 class="mb-4"><?php echo $LANG['t_createAutomation']?></h4>
<form method="post"> <form method="post">
<div class="field"> <div class="field">
<input type="hidden" name="atSelector" value="<?php if (isset($_POST['atTime'])) { <input type="hidden" name="atSelector" value="<?php echo $_POST['atSelector']; ?>" required/>
<input type="hidden" name="atSelectorValue" value="<?php if (isset($_POST['atTime'])) {
echo $_POST['atTime']; echo $_POST['atTime'];
} else if (isset($_POST['atDeviceValue'])) { } else if (isset($_POST['atDeviceValue'])) {
$subDeviceId = $_POST['atDeviceValue']; $subDeviceId = $_POST['atDeviceValue'];

View File

@ -91,7 +91,8 @@
</div> </div>
</div> </div>
<input type="submit" class="button" name="modalFinal" value="<?php echo $LANG['b_edit'];?>"/> <input type="submit" class="button" name="modalFinal" value="<?php echo $LANG['b_edit'];?>"/>
<input type="submit" class="button is-danger" onClick="ajaxPost('ajax',{automation_id:'<?php echo $AUTOMATIONID ?>', 'action':'delete'}, this, true);" name="remove" value="<?php echo $LANG['b_remove'];?>"/> <input type="submit" class="button is-danger" onClick="ajaxPostSimple('ajax',{automation_id: '<?php echo $AUTOMATIONID ?>', action:'delete'}, true);" name="remove" value="<?php echo $LANG['b_remove'];?>"/>
</form> </form>
</div> </div>
</div> </div>

View File

@ -8,7 +8,7 @@ if ($SUBDEVICE['type'] == 'on/off') {
<div class="device-button col-4 col-sm-3 col-xl-2 square-wrap" <?php echo $action; ?> data-room-id="<?php echo $ROOMID; ?>"> <div class="device-button col-4 col-sm-3 col-xl-2 square-wrap" <?php echo $action; ?> data-room-id="<?php echo $ROOMID; ?>">
<div class="square"> <div class="square">
<div class="square-content <?php echo ($SUBDEVICE['comError'] ? "is-inactive" : "") ;?>" id="device-<?php echo $DEVICEID ?>" data-sub-device-id="<?php echo $SUBDEVICEID;?>"> <div class="square-content <?php echo (($SUBDEVICE['comError'] == 1 || $DEVICE['approved'] == 0) ? "is-inactive" : "") ;?>" id="device-<?php echo $DEVICEID ?>" data-sub-device-id="<?php echo $SUBDEVICEID;?>">
<div class="content"> <div class="content">
<div class="row"> <div class="row">
<div class="col"> <div class="col">

View File

@ -41,7 +41,11 @@ class Ajax extends Template
} else if (isset($_POST['automation_id'])){ } else if (isset($_POST['automation_id'])){
$automationId = $_POST['automation_id']; $automationId = $_POST['automation_id'];
if (isset($_POST['action']) && $_POST['action'] == 'delete') {
AutomationManager::remove($automationId);
}else {
AutomationManager::deactive($automationId); AutomationManager::deactive($automationId);
}
} else if (isset($_POST['subDevice']) && isset($_POST['action']) && $_POST['action'] == "chart") { } else if (isset($_POST['subDevice']) && isset($_POST['action']) && $_POST['action'] == "chart") {
//TODO lepe rozstrukturovat //TODO lepe rozstrukturovat
$subDeviceId = $_POST['subDevice']; $subDeviceId = $_POST['subDevice'];
@ -154,8 +158,12 @@ class Ajax extends Template
die(); die();
} else if (isset($_POST['scene_id'])) { } else if (isset($_POST['scene_id'])) {
$sceneId = $_POST['scene_id']; $sceneId = $_POST['scene_id'];
if (isset($_POST['action']) && $_POST['action'] == 'delete') {
SceneManager::delete($sceneId);
}else {
echo SceneManager::execScene($sceneId); echo SceneManager::execScene($sceneId);
} }
}
die(); die();

View File

@ -19,11 +19,19 @@ class Automation extends Template
$automations = []; $automations = [];
$automationsData = AutomationManager::getAll(); $automationsData = AutomationManager::getAll();
foreach ($automationsData as $automationKey => $automationData) { 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']] = [ $automations[$automationData['automation_id']] = [
'name' => '', 'name' => '',
'onDays' => implode(', ',json_decode($automationData['on_days'])), 'onDays' => json_decode($automationData['on_days']),
'ifSomething' => $automationData['if_something'], 'ifSomething' => $automationData['if_something'],
'doSomething' => $automationData['do_something'], 'doSomething' => $doSomething,
'active' => $automationData['active'], 'active' => $automationData['active'],
]; ];
} }
@ -37,6 +45,7 @@ class Automation extends Template
$approvedSubDevices[$subDeviceValue['subdevice_id']] = [ $approvedSubDevices[$subDeviceValue['subdevice_id']] = [
'name' => $allDevicesData[$deviceKey]['name'], 'name' => $allDevicesData[$deviceKey]['name'],
'type' => $subDeviceValue['type'], 'type' => $subDeviceValue['type'],
'masterDevice' => $subDeviceValue['device_id'],
]; ];
} }
} }