Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding the triggeringValue() function for using in scenario #1944

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/class/cmd.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1705,7 +1705,7 @@ public function event($_value, $_datetime = null, $_loop = 1) {
$events = array();
if (!$repeat) {
$this->setCache(array('value' => $value, 'valueDate' => $this->getValueDate()));
scenario::check($this, false, $this->getGeneric_type(), $object);
scenario::check($this, false, $this->getGeneric_type(), $object, $value);
$level = $this->checkAlertLevel($value);
$events[] = array('cmd_id' => $this->getId(), 'value' => $value, 'display_value' => $display_value, 'valueDate' => $this->getValueDate(), 'collectDate' => $this->getCollectDate(), 'alertLevel' => $level);
$foundInfo = false;
Expand Down
35 changes: 27 additions & 8 deletions core/class/scenario.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class scenario {
private $_elements = array();
private $_changeState = false;
private $_realTrigger = '';
private $_realTriggeringValue = '';
private $_return = true;
private $_tags = array();
private $_do = true;
Expand Down Expand Up @@ -289,7 +290,7 @@ public static function byObjectId($_object_id, $_onlyEnable = true, $_onlyVisibl
* @param type $_forceSyncMode
* @return boolean
*/
public static function check($_event = null, $_forceSyncMode = false, $_generic = null, $_object = null) {
public static function check($_event = null, $_forceSyncMode = false, $_generic = null, $_object = null, $_value = null) {
if (config::byKey('enableScenario') != 1) {
return;
}
Expand Down Expand Up @@ -319,7 +320,7 @@ public static function check($_event = null, $_forceSyncMode = false, $_generic
} else {
$message .= ' genericType(' . $_generic . ')' . ' from ' . $_event->getHumanName();
}
$scenario->launch($trigger, $message, $_forceSyncMode);
$scenario->launch($trigger, $_value, $message, $_forceSyncMode);
}
}
}
Expand Down Expand Up @@ -349,7 +350,7 @@ public static function check($_event = null, $_forceSyncMode = false, $_generic

if (count($scenarios) > 0) {
foreach ($scenarios as $scenario_) {
$scenario_->launch($trigger, $message, $_forceSyncMode);
$scenario_->launch($trigger, $_value, $message, $_forceSyncMode);
}
}
return true;
Expand Down Expand Up @@ -754,7 +755,7 @@ public function testTrigger($_event) {
* @param type $_forceSyncMode
* @return boolean
*/
public function launch($_trigger = '', $_message = '', $_forceSyncMode = false) {
public function launch($_trigger = '', $_value = '', $_message = '', $_forceSyncMode = false) {
if (config::byKey('enableScenario') != 1 || $this->getIsActive() != 1) {
return false;
}
Expand Down Expand Up @@ -791,14 +792,15 @@ public function launch($_trigger = '', $_message = '', $_forceSyncMode = false)
$this->setCache(array('startingTime' => strtotime('now'), 'state' => 'starting'));
if ($this->getConfiguration('syncmode') == 1 || $_forceSyncMode) {
$this->setLog($GLOBALS['JEEDOM_SCLOG_TEXT']['launchScenarioSync']['txt']);
return $this->execute($_trigger, $_message);
return $this->execute($_trigger, $_value, $_message);
} else {
if (count($this->getTags()) != '') {
$this->setCache('tags', $this->getTags());
}
$cmd = __DIR__ . '/../../core/php/jeeScenario.php ';
$cmd .= ' scenario_id=' . $this->getId();
$cmd .= ' trigger=' . escapeshellarg($_trigger);
$cmd .= ' value=' . base64_encode($_value);
$cmd .= ' "message=' . escapeshellarg(sanitizeAccent($_message)) . '"';
$cmd .= ' >> ' . log::getPathToLog('scenario_execution') . ' 2>&1 &';
system::php($cmd);
Expand All @@ -811,7 +813,7 @@ public function launch($_trigger = '', $_message = '', $_forceSyncMode = false)
* @param type $_message
* @return type
*/
public function execute($_trigger = '', $_message = '') {
public function execute($_trigger = '', $_value = '', $_message = '') {
if (config::byKey('enableScenario') != 1) {
return;
}
Expand Down Expand Up @@ -839,7 +841,7 @@ public function execute($_trigger = '', $_message = '') {

$cmd = cmd::byId(str_replace('#', '', $_trigger));
if (is_object($cmd)) {
log::add('event', 'info', __('Exécution du scénario', __FILE__) . ' ' . $this->getHumanName() . ' ' . __('déclenché par :', __FILE__) . ' ' . $cmd->getHumanName());
log::add('event', 'info', __('Exécution du scénario', __FILE__) . ' ' . $this->getHumanName() . ' ' . __('déclenché par :', __FILE__) . ' #' . $cmd->getHumanName() . __('#, valeur :', __FILE__) . ' [' . $_value . ']');
if ($this->getConfiguration('timeline::enable')) {
$timeline = new timeline();
$timeline->setType('scenario');
Expand All @@ -850,7 +852,7 @@ public function execute($_trigger = '', $_message = '') {
$timeline->save();
}
} else {
log::add('event', 'info', __('Exécution du scénario', __FILE__) . ' ' . $this->getHumanName() . ' ' . __('déclenché par :', __FILE__) . ' ' . $_trigger);
log::add('event', 'info', __('Exécution du scénario', __FILE__) . ' ' . $this->getHumanName() . ' ' . __('déclenché par :', __FILE__) . ' #' . $_trigger . __('#, valeur :', __FILE__) . ' [' . $_value . ']');
if ($this->getConfiguration('timeline::enable')) {
$timeline = new timeline();
$timeline->setType('scenario');
Expand All @@ -873,6 +875,7 @@ public function execute($_trigger = '', $_message = '') {
$this->setState('in progress');
$this->setPID(getmypid());
$this->setRealTrigger($_trigger);
$this->setRealTriggeringValue($_value);
foreach (($this->getElement()) as $element) {
if (!$this->getDo()) {
break;
Expand Down Expand Up @@ -2009,6 +2012,13 @@ public function setConfiguration($_key, $_value) {
public function getRealTrigger() {
return $this->_realTrigger;
}
/**
*
* @return type
*/
public function getRealTriggeringValue() {
return $this->_realTriggeringValue;
}
/**
*
* @param type $_realTrigger
Expand All @@ -2018,6 +2028,15 @@ public function setRealTrigger($_realTrigger) {
$this->_realTrigger = $_realTrigger;
return $this;
}
/**
*
* @param type $_realTriggeringValue
* @return $this
*/
public function setRealTriggeringValue($_realTriggeringValue) {
$this->_realTriggeringValue = $_realTriggeringValue;
return $this;
}
/**
*
* @return type
Expand Down
9 changes: 9 additions & 0 deletions core/class/scenarioExpression.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,13 @@ public static function triggerValue(&$_scenario = null) {
return false;
}

public static function triggeringValue(&$_scenario = null) {
if ($_scenario !== null) {
return $_scenario->getRealTriggeringValue();
}
return false;
}

public static function round($_value, $_decimal = 0) {
$_value = self::setTags($_value);
try {
Expand Down Expand Up @@ -1244,6 +1251,8 @@ public static function setTags($_expression, &$_scenario = null, $_quote = false
$replace2[$replace_string] = self::trigger($arguments[0], $_scenario);
} elseif ($function == 'triggerValue') {
$replace2[$replace_string] = self::triggerValue($_scenario);
} elseif ($function == 'triggeringValue') {
$replace2[$replace_string] = self::triggeringValue($_scenario);
} elseif ($function == 'tag') {
if (!isset($arguments[0])) {
$arguments[0] = '';
Expand Down
2 changes: 1 addition & 1 deletion core/php/jeeScenario.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
die();
}
}
$scenario->execute(init('trigger'), init('message'));
$scenario->execute(init('trigger'), base64_decode(init('value')), init('message'));
} catch (Exception $e) {
log::add('scenario', 'error', __('Scenario :', __FILE__) . ' ' . $scenario->getHumanName() . '. ' . __('Erreur :', __FILE__) . ' ' . $e->getMessage());
$scenario->setState('error');
Expand Down