public function execute(&$_scenario) { if ($this->getType() == 'if') { if ($this->getSubElement('if')->execute($_scenario)) { if ($this->getSubElement('if')->getOptions('allowRepeatCondition', 0) == 1) { if ($this->getSubElement('if')->getOptions('previousState', -1) != 1) { $this->getSubElement('if')->setOptions('previousState', 1); $this->getSubElement('if')->save(); } else { $_scenario->setLog(__('Non exécution des actions pour cause de répétition', __FILE__)); return; } } return $this->getSubElement('then')->execute($_scenario); } if (!is_object($this->getSubElement('else'))) { return; } if ($this->getSubElement('if')->getOptions('allowRepeatCondition', 0) == 1) { if ($this->getSubElement('if')->getOptions('previousState', -1) != 0) { $this->getSubElement('if')->setOptions('previousState', 0); $this->getSubElement('if')->save(); } else { $_scenario->setLog(__('Non exécution des actions pour cause de répétition', __FILE__)); return; } } return $this->getSubElement('else')->execute($_scenario); } else { if ($this->getType() == 'action') { return $this->getSubElement('action')->execute($_scenario); } else { if ($this->getType() == 'code') { return $this->getSubElement('code')->execute($_scenario); } else { if ($this->getType() == 'for') { $for = $this->getSubElement('for'); $limits = $for->getExpression(); $limits = intval(jeedom::evaluateExpression($limits[0]->getExpression())); if (!is_numeric($limits)) { $_scenario->setLog(__('[ERREUR] La condition pour une boucle doit être numérique : ', __FILE__) . $limits); throw new Exception(__('La condition pour une boucle doit être numérique : ', __FILE__) . $limits); } $return = false; for ($i = 1; $i <= $limits; $i++) { $return = $this->getSubElement('do')->execute($_scenario); } return $return; } else { if ($this->getType() == 'in') { $in = $this->getSubElement('in'); $in = $in->getExpression(); $time = ceil(str_replace('.', ',', jeedom::evaluateExpression($in[0]->getExpression()))); if (!is_numeric($time) || $time < 0) { $time = 0; } if ($time == 0) { $cmd = '/usr/bin/php ' . dirname(__FILE__) . '/../../core/php/jeeScenario.php '; $cmd .= ' scenario_id=' . $_scenario->getId(); $cmd .= ' scenarioElement_id=' . $this->getId(); $cmd .= ' >> ' . log::getPathToLog('scenario_element_execution') . ' 2>&1 &'; exec($cmd); } else { $crons = cron::searchClassAndFunction('scenario', 'doIn', '"scenarioElement_id":' . $this->getId()); if (is_array($crons)) { foreach ($crons as $cron) { if ($cron->getState() != 'run') { $cron->remove(); } } } $cron = new cron(); $cron->setClass('scenario'); $cron->setFunction('doIn'); $cron->setOption(array('scenario_id' => intval($_scenario->getId()), 'scenarioElement_id' => intval($this->getId()), 'second' => date('s'))); $cron->setLastRun(date('Y-m-d H:i:s')); $cron->setOnce(1); $next = strtotime('+ ' . $time . ' min'); $cron->setSchedule(date('i', $next) . ' ' . date('H', $next) . ' ' . date('d', $next) . ' ' . date('m', $next) . ' * ' . date('Y', $next)); $cron->save(); $_scenario->setLog(__('Tâche : ', __FILE__) . $this->getId() . __(' programmé à : ', __FILE__) . date('Y-m-d H:i:00', $next) . ' (+ ' . $time . ' min)'); } return true; } else { if ($this->getType() == 'at') { $at = $this->getSubElement('at'); $at = $at->getExpression(); $next = jeedom::evaluateExpression($at[0]->getExpression()); if ($next % 100 > 59) { if (strpos($at[0]->getExpression(), '-') !== false) { $next -= 40; } else { $next += 40; } } if (!is_numeric($next) || $next < 0) { $_scenario->setLog(__('Erreur dans bloc (type A) : ', __FILE__) . $this->getId() . __(', heure programmée invalide : ', __FILE__) . $next); } if ($next < date('Gi') + 1) { if (strlen($next) == 3) { $next = date('Y-m-d', strtotime('+1 day' . date('Y-m-d'))) . ' 0' . substr($next, 0, 1) . ':' . substr($next, 1, 3); } else { $next = date('Y-m-d', strtotime('+1 day' . date('Y-m-d'))) . ' ' . substr($next, 0, 2) . ':' . substr($next, 2, 4); } } else { if (strlen($next) == 3) { $next = date('Y-m-d') . ' 0' . substr($next, 0, 1) . ':' . substr($next, 1, 3); } else { $next = date('Y-m-d') . ' ' . substr($next, 0, 2) . ':' . substr($next, 2, 4); } } $next = strtotime($next); $crons = cron::searchClassAndFunction('scenario', 'doIn', '"scenarioElement_id":' . $this->getId()); if (is_array($crons)) { foreach ($crons as $cron) { if ($cron->getState() != 'run') { $cron->remove(); } } } $cron = new cron(); $cron->setClass('scenario'); $cron->setFunction('doIn'); $cron->setOption(array('scenario_id' => intval($_scenario->getId()), 'scenarioElement_id' => intval($this->getId()), 'second' => 0)); $cron->setLastRun(date('Y-m-d H:i:s')); $cron->setOnce(1); $cron->setSchedule(date('i', $next) . ' ' . date('H', $next) . ' ' . date('d', $next) . ' ' . date('m', $next) . ' * ' . date('Y', $next)); $cron->save(); $_scenario->setLog(__('Tâche : ', __FILE__) . $this->getId() . __(' programmée à : ', __FILE__) . date('Y-m-d H:i:00', $next)); return true; } } } } } } }