Source of file start_scenario.php

Size: 3,182 Bytes - Last Modified: 2020-10-24T02:46:31+00:00

/home/travis/build/NextDom/nextdom-core/src/Api/start_scenario.php

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
<?php

/* This file is part of Jeedom.
 *
 * Jeedom is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Jeedom is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Jeedom. If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Execute scenario or element of a scenario
 *
 * Usage :
 *  - jeeScenario scenario_id=SCENARIO_ID scenarioElement_id=SCENARIO_ELEMENT_ID tags=TAGS
 *  - jeeScenario scenario_id=SCENARIO_ID trigger=TRIGGER message=MESSAGE
 *
 * Parameters :
 *  - SCENARIO_ID : Id of the scenario to execute
 *  - SCENARIO_ELEMENT_ID : Id of the specific element to execute
 *  - TAGS : ???
 *  - TRIGGER : Trigger that started the scenario
 *  - MESSAGE : ???
 */

namespace NextDom;

use NextDom\Enums\ScenarioState;
use NextDom\Helpers\LogHelper;
use NextDom\Helpers\ScriptHelper;
use NextDom\Helpers\Utils;
use NextDom\Managers\ConfigManager;
use NextDom\Managers\ScenarioManager;

require_once __DIR__ . "/../../src/core.php";

ScriptHelper::cliOrCrash();
ScriptHelper::parseArgumentsToGET();

$scenarioId = intval(Utils::init('scenario_id'));
if (Utils::init('scenarioElement_id') != '') {
    // Execute an element of the scenario
    ScenarioManager::doIn([
        'scenario_id' => $scenarioId,
        'scenarioElement_id' => Utils::init('scenarioElement_id'),
        'second' => 0,
        'tags' => json_decode(Utils::init('tags'), true)
    ]);
} else {
    // Execute a scenario
    $scenario = ScenarioManager::byId(intval($scenarioId));

    // Scenario not found
    if (!is_object($scenario)) {
        $errorMsg = __('scripts.scenario-not-found') . $scenarioId;
        LogHelper::addInfo('scenario', $errorMsg);
        die($errorMsg . "\n");
    }

    if (is_numeric($scenario->getTimeout()) && $scenario->getTimeout() != '' && $scenario->getTimeout() !== 0) {
        set_time_limit($scenario->getTimeout(ConfigManager::byKey('maxExecTimeScript', 'core', 1) * 60));
    }

    try {
        // If scenario is in progress, wait 1 second. If scenario is in progress again, stop the script
        if ($scenario->getState() === ScenarioState::IN_PROGRESS && $scenario->getConfiguration('allowMultiInstance', 0) === 0) {
            sleep(1);
            if ($scenario->getState() == ScenarioState::IN_PROGRESS) {
                die();
            }
        }
        $scenario->execute(Utils::init('trigger'), Utils::init('message'));
    } catch (\Throwable $e) {
        LogHelper::addError('scenario', __('scripts.scenario') . $scenario->getHumanName() . '. ' . __('scripts.error') . $e->getMessage());
        $scenario->setState('error');
        $scenario->setLog(__('scripts.error') . $e->getMessage());
        $scenario->setPID('');
        $scenario->persistLog();
        die();
    }
}