public static function variable($_name, $_default = '') { $dataStore = dataStore::byTypeLinkIdKey('scenario', -1, trim($_name)); if (is_object($dataStore)) { $value = $dataStore->getValue($_default); if (strpos($value, ' ') !== false) { return '"' . $value . '"'; } return $value; } return $_default; }
public function execute(&$scenario = null) { if ($this->getOptions('enable', 1) == 0) { return; } $message = ''; try { if ($this->getType() == 'element') { $element = scenarioElement::byId($this->getExpression()); if (is_object($element)) { $this->setLog($scenario, __('Exécution d\'un bloc élément : ', __FILE__) . $this->getExpression()); return $element->execute($scenario); } return; } $options = $this->getOptions(); if (isset($options['enable'])) { unset($options['enable']); } if (is_array($options) && $this->getExpression() != 'wait') { foreach ($options as $key => $value) { if (is_string($value)) { $options[$key] = str_replace('"', '', self::setTags($value, $scenario)); } } } if ($this->getType() == 'action') { if ($this->getExpression() == 'icon') { if ($scenario != null) { $options = $this->getOptions(); $this->setLog($scenario, __('Changement de l\'icone du scénario : ', __FILE__) . $options['icon']); $scenario->setDisplay('icon', $options['icon']); $scenario->save(); } return; } else { if ($this->getExpression() == 'wait') { if (!isset($options['condition'])) { return; } $result = false; $occurence = 0; $limit = isset($options['timeout']) && is_numeric($options['timeout']) ? $options['timeout'] : 7200; while ($result !== true) { $expression = self::setTags($options['condition'], $scenario); $result = evaluate($expression); if ($occurence > $limit) { $this->setLog($scenario, __('[Wait] Condition valide par dépassement de temps', __FILE__)); return; } $occurence++; sleep(1); } $this->setLog($scenario, __('[Wait] Condition valide : ', __FILE__) . $expression); return; } else { if ($this->getExpression() == 'sleep') { if (isset($options['duration'])) { try { $options['duration'] = evaluate($options['duration']); } catch (Exception $e) { } if (is_numeric($options['duration']) && $options['duration'] > 0) { $this->setLog($scenario, __('Pause de ', __FILE__) . $options['duration'] . __(' seconde(s)', __FILE__)); if ($options['duration'] < 1) { return usleep($options['duration'] * 1000); } else { return sleep($options['duration']); } } } $this->setLog($scenario, __('Aucune durée trouvée pour l\'action sleep ou la durée n\'est pas valide : ', __FILE__) . $options['duration']); return; } else { if ($this->getExpression() == 'stop') { if ($scenario != null) { $scenario2 = scenario::byId($scenario->getId()); if ($scenario->getIsActive() != $scenario2->getIsActive()) { $scenario->setIsActive($scenario2->getIsActive()); } $this->setLog($scenario, __('Arret du scénario', __FILE__)); $scenario->setState('stop'); $scenario->setPID(''); $scenario->persistLog(); $scenario->save(); } die; } else { if ($this->getExpression() == 'log') { if ($scenario != null) { $scenario->setLog('Log : ' . $options['message']); if ($scenario->getConfiguration('speedPriority', 0) == 1) { $scenario->persistLog(); } } return; } else { if ($this->getExpression() == 'message') { message::add('scenario', $options['message']); return; } else { if ($this->getExpression() == 'equipement') { $eqLogic = eqLogic::byId(str_replace(array('#eqLogic', '#'), '', $this->getOptions('eqLogic'))); if (!is_object($eqLogic)) { throw new Exception(__('Action sur l\'équipement impossible. Equipement introuvable - Vérifiez l\'id : ', __FILE__) . $this->getOptions('eqLogic')); } switch ($this->getOptions('action')) { case 'show': $this->setLog($scenario, __('Equipement visible : ', __FILE__) . $eqLogic->getHumanName()); $eqLogic->setIsVisible(1); $eqLogic->save(); break; case 'hide': $this->setLog($scenario, __('Equipement masqué : ', __FILE__) . $eqLogic->getHumanName()); $eqLogic->setIsVisible(0); $eqLogic->save(); break; case 'deactivate': $this->setLog($scenario, __('Equipement désactivé : ', __FILE__) . $eqLogic->getHumanName()); $eqLogic->setIsEnable(0); $eqLogic->save(); break; case 'activate': $this->setLog($scenario, __('Equipement activé : ', __FILE__) . $eqLogic->getHumanName()); $eqLogic->setIsEnable(1); $eqLogic->save(); break; } return; } else { if ($this->getExpression() == 'say') { $this->setLog($scenario, __('Je dis : ', __FILE__) . $options['message']); nodejs::pushUpdate('jeedom::say', $options['message']); return; } else { if ($this->getExpression() == 'gotodesign') { $this->setLog($scenario, __('Changement design : ', __FILE__) . $options['plan_id']); nodejs::pushUpdate('jeedom::gotoplan', $options['plan_id']); return; } else { if ($this->getExpression() == 'return') { $this->setLog($scenario, __('Je vais retourner : ', __FILE__) . $options['message']); $scenario->setReturn($scenario->getReturn() . $options['message']); return; } else { if ($this->getExpression() == 'scenario') { if ($scenario != null && $this->getOptions('scenario_id') == $scenario->getId()) { $actionScenario =& $scenario; } else { $actionScenario = scenario::byId($this->getOptions('scenario_id')); } if (!is_object($actionScenario)) { throw new Exception(__('Action sur scénario impossible. Scénario introuvable - Vérifiez l\'id : ', __FILE__) . $this->getOptions('scenario_id')); } switch ($this->getOptions('action')) { case 'start': $this->setLog($scenario, __('Lancement du scénario : ', __FILE__) . $actionScenario->getName()); if (isset($options['tags'])) { $actionScenario->setTags($options['tags']); } if ($scenario != null) { return $actionScenario->launch(false, __('Lancement provoqué par le scénario : ', __FILE__) . $scenario->getHumanName()); } else { return $actionScenario->launch(false, __('Lancement provoqué', __FILE__)); } break; case 'stop': $this->setLog($scenario, __('Arrêt forcé du scénario : ', __FILE__) . $actionScenario->getName()); $actionScenario->stop(); break; case 'deactivate': $this->setLog($scenario, __('Désactivation du scénario : ', __FILE__) . $actionScenario->getName()); $actionScenario->setIsActive(0); $actionScenario->save(); break; case 'activate': $this->setLog($scenario, __('Activation du scénario : ', __FILE__) . $actionScenario->getName()); $actionScenario->setIsActive(1); $actionScenario->save(); break; } return; } else { if ($this->getExpression() == 'variable') { $options['value'] = self::setTags($options['value']); try { $result = evaluate($options['value']); if (!is_numeric($result)) { $result = $options['value']; } } catch (Exception $ex) { $result = $options['value']; } $this->setLog($scenario, __('Affectation de la variable ', __FILE__) . $this->getOptions('name') . __(' => ', __FILE__) . $options['value'] . ' = ' . $result); $dataStore = new dataStore(); $dataStore->setKey($this->getOptions('name')); $dataStore->setValue($result); $dataStore->setType('scenario'); $dataStore->setLink_id(-1); $dataStore->save(); return; } else { if ($this->getExpression() == 'ask') { $dataStore = new dataStore(); $dataStore->setType('scenario'); $dataStore->setKey($this->getOptions('variable')); $dataStore->setValue(''); $dataStore->setLink_id(-1); $dataStore->save(); $options_cmd = array('title' => '', 'message' => $options['question'], 'answer' => explode(';', $options['answer']), 'variable' => $this->getOptions('variable')); $cmd = cmd::byId(str_replace('#', '', $this->getOptions('cmd'))); if (!is_object($cmd)) { throw new Exception(__('Commande introuvable - Vérifiez l\'id : ', __FILE__) . $this->getOptions('cmd')); } $limit = isset($options['timeout']) ? $options['timeout'] : 300; $this->setLog($scenario, __('Demande ', __FILE__) . print_r($options_cmd, true)); $cmd->setConfiguration('storeVariable', $this->getOptions('variable')); $cmd->save(); $cmd->execCmd($options_cmd); $occurence = 0; $value = ''; while (true) { $dataStore = dataStore::byTypeLinkIdKey('scenario', -1, $this->getOptions('variable')); if (is_object($dataStore)) { $value = $dataStore->getValue(); } if ($value != '') { break; } if ($occurence > $limit) { break; } $occurence++; sleep(1); } if ($value == '') { $value = __('Aucune réponse', __FILE__); $dataStore = dataStore::byTypeLinkIdKey('scenario', -1, $this->getOptions('variable')); $dataStore->setValue($value); $dataStore->save(); } $this->setLog($scenario, __('Réponse ', __FILE__) . $value); return; } else { $cmd = cmd::byId(str_replace('#', '', $this->getExpression())); if (is_object($cmd)) { if ($cmd->getSubtype() == 'slider' && isset($options['slider'])) { $options['slider'] = evaluate($options['slider']); } if (is_array($options) && count($options) != 0) { $this->setLog($scenario, __('Exécution de la commande ', __FILE__) . $cmd->getHumanName() . __(" avec comme option(s) : \n", __FILE__) . print_r($options, true)); } else { $this->setLog($scenario, __('Exécution de la commande ', __FILE__) . $cmd->getHumanName()); } if (is_object($scenario) && $scenario->getConfiguration('cmdNoWait', 0) == 1) { $options['speedAndNoErrorReport'] = true; } return $cmd->execCmd($options); } $this->setLog($scenario, __('[Erreur] Aucune commande trouvée pour ', __FILE__) . $this->getExpression()); return; } } } } } } } } } } } } } } else { if ($this->getType() == 'condition') { $expression = self::setTags($this->getExpression(), $scenario, true); $message = __('Evaluation de la condition : [', __FILE__) . $expression . '] = '; $result = evaluate($expression); if (is_bool($result)) { if ($result) { $message .= __('Vrai', __FILE__); } else { $message .= __('Faux', __FILE__); } } else { $message .= $result; } $this->setLog($scenario, $message); return $result; } else { if ($this->getType() == 'code') { $this->setLog($scenario, __('Exécution d\'un bloc code', __FILE__)); return eval($this->getExpression()); } } } } catch (Exception $e) { $this->setLog($scenario, $message . $e->getMessage()); } }
public function getData($_key, $_private = false, $_default = '') { if ($_private) { $dataStore = dataStore::byTypeLinkIdKey('scenario', $this->getId(), $_key); } else { $dataStore = dataStore::byTypeLinkIdKey('scenario', -1, $_key); } if (is_object($dataStore)) { return $dataStore->getValue($_default); } return $_default; }
$eqLogic_return = utils::o2a($eqLogic); $eqLogic_return['cmds'] = array(); foreach ($eqLogic->getCmd() as $cmd) { $cmd_return = utils::o2a($cmd); if ($cmd->getType() == 'info') { $cmd_return['state'] = $cmd->execCmd(); } $eqLogic_return['cmds'][] = $cmd_return; } $return['eqLogics'][] = $eqLogic_return; } $jsonrpc->makeSuccess($return); } /* * ************************datastore*************************** */ if ($jsonrpc->getMethod() == 'datastore::byTypeLinkIdKey') { $jsonrpc->makeSuccess(dataStore::byTypeLinkIdKey($params['type'], $params['linkId'], $params['key'])); } if ($jsonrpc->getMethod() == 'datastore::save') { $dataStore = new dataStore(); $dataStore->setType($params['type']); $dataStore->setKey($params['key']); $dataStore->setValue($params['value']); $dataStore->setLink_id($params['linkId']); $dataStore->save(); $jsonrpc->makeSuccess('ok'); } /* * ************************Equipement*************************** */ if ($jsonrpc->getMethod() == 'eqLogic::all') { $jsonrpc->makeSuccess(utils::o2a(eqLogic::all())); } if ($jsonrpc->getMethod() == 'eqLogic::byType') {