Example #1
0
 public function execute($_options = null)
 {
     switch ($this->getType()) {
         case 'info':
             if ($this->getConfiguration('virtualAction', 0) == '0') {
                 $calcul = cmd::cmdToValue($this->getConfiguration('calcul'));
                 $test = new evaluate();
                 $result = $test->Evaluer($calcul);
                 if ($this->getSubType() == 'binary') {
                     if ($result) {
                         return 1;
                     } else {
                         return 0;
                     }
                 }
                 if (is_numeric($result)) {
                     return number_format($result, 2);
                 } else {
                     return $result;
                 }
             } else {
                 return $this->getConfiguration('value');
             }
             break;
         case 'action':
             $virtualCmd = virtualCmd::byId($this->getConfiguration('infoId'));
             if (!is_object($virtualCmd)) {
                 throw new Exception('Virtual info commande non trouvé, verifier ID');
             }
             if ($virtualCmd->getEqLogic()->getEqType_name() != 'virtual') {
                 throw new Exception('La cible de la commande virtuel n\'est pas un équipement de type virtuel');
             }
             if ($this->getSubType() == 'slider') {
                 $value = $_options['slider'];
             } else {
                 if ($this->getSubType() == 'color') {
                     $value = $_options['color'];
                 } else {
                     $value = $this->getConfiguration('value');
                 }
             }
             $virtualCmd->setConfiguration('value', $value);
             $virtualCmd->save();
             $virtualCmd->event($value);
             break;
     }
 }
Example #2
0
 public static function setTags($_expression, &$_scenario = null, $_quote = false, $_nbCall = 0)
 {
     if ($_nbCall > 10) {
         return $_expression;
     }
     $replace1 = array('#seconde#' => (int) date('s'), '#heure#' => (int) date('G'), '#minute#' => (int) date('i'), '#jour#' => (int) date('d'), '#mois#' => (int) date('m'), '#annee#' => (int) date('Y'), '#time#' => date('Gi'), '#timestamp#' => time(), '#seconde#' => (int) date('s'), '#date#' => date('md'), '#semaine#' => date('W'), '#sjour#' => '"' . date_fr(date('l')) . '"', '#smois#' => '"' . date_fr(date('F')) . '"', '#njour#' => (int) date('w'), '#hostname#' => '"' . gethostname() . '"', '#IP#' => '"' . network::getNetworkAccess('internal', 'ip') . '"', '#trigger#' => '');
     if (is_object($_scenario)) {
         $cmd = cmd::byId(str_replace('#', '', $_scenario->getRealTrigger()));
         if (is_object($cmd)) {
             $replace1['#trigger#'] = $cmd->getHumanName();
         } else {
             $replace1['#trigger#'] = $_scenario->getRealTrigger();
         }
     }
     if ($_scenario != null) {
         $replace1 = array_merge($replace1, $_scenario->getTags());
     }
     $replace2 = array();
     preg_match_all("/([a-zA-Z][a-zA-Z_]*?)\\((.*?)\\)/", $_expression, $matches, PREG_SET_ORDER);
     foreach ($matches as $match) {
         $function = $match[1];
         $replace_string = $match[0];
         if (substr_count($match[2], '(') != substr_count($match[2], ')')) {
             $pos = strpos($_expression, $match[2]) + strlen($match[2]);
             while (substr_count($match[2], '(') > substr_count($match[2], ')')) {
                 $match[2] .= $_expression[$pos];
                 $pos++;
                 if ($pos > strlen($_expression)) {
                     break;
                 }
             }
             $arguments = self::setTags($match[2], $_scenario, $_quote, $_nbCall++);
             $result = str_replace($match[2], $arguments, $_expression);
             while (substr_count($result, '(') > substr_count($result, ')')) {
                 $result .= ')';
             }
             $result = self::setTags($result, $_scenario, $_quote, $_nbCall++);
             return cmd::cmdToValue(str_replace(array_keys($replace1), array_values($replace1), $result), $_quote);
         } else {
             $arguments = explode(',', $match[2]);
         }
         if (method_exists(__CLASS__, $function)) {
             if ($function == 'trigger') {
                 if (!isset($arguments[0])) {
                     $arguments[0] = '';
                 }
                 $replace2[$replace_string] = self::trigger($arguments[0], $_scenario, $_quote);
             } else {
                 $replace2[$replace_string] = call_user_func_array(__CLASS__ . "::" . $function, $arguments);
             }
         } else {
             if (function_exists($function)) {
                 foreach ($arguments as &$argument) {
                     $argument = evaluate(self::setTags($argument, $_scenario, $_quote));
                 }
                 $replace2[$replace_string] = call_user_func_array($function, $arguments);
             }
         }
     }
     return cmd::cmdToValue(str_replace(array_keys($replace1), array_values($replace1), str_replace(array_keys($replace2), array_values($replace2), $_expression)), $_quote);
 }
 public function event($_value, $_loop = 1)
 {
     if ($this->getLogicalId() == 'nbimpulsionminute') {
         try {
             $calcul = $this->getConfiguration('calcul');
             $calcul = preg_replace("/#brut#/", $_value, $calcul);
             $calcul = scenarioExpression::setTags($calcul);
             $test = new evaluate();
             $result = $test->Evaluer($calcul);
             parent::event($result, $_loop);
         } catch (Exception $e) {
             $EqLogic = $this->getEqLogic();
             log::add('ipx800', 'error', $EqLogic->getName() . " error in " . $this->getConfiguration('calcul') . " : " . $e->getMessage());
             return scenarioExpression::setTags(str_replace('"', '', cmd::cmdToValue($this->getConfiguration('calcul'))));
         }
     } else {
         parent::event($_value, $_loop);
     }
 }
Example #4
0
 public function getData($_startDate = null, $_endDate = null)
 {
     $nowtime = floatval(strtotime(date('Y-m-d H:i:s') . " UTC"));
     $now = strtotime(date('Y-m-d H:i:s'));
     $archiveTime = (config::byKey('historyArchiveTime') + 1) * 3600;
     $packetTime = config::byKey('historyArchivePackage') * 3600;
     $return = array('history' => array('power' => array(), 'consumption' => null), 'stats' => array('minPower' => null, 'maxPower' => null), 'real' => array('power' => 0, 'consumption' => 0));
     $cmd_histories = array();
     preg_match_all("/#([0-9]*)#/", $this->getPower(), $matches);
     foreach ($matches[1] as $cmd_id) {
         if (is_numeric($cmd_id)) {
             $cmd = cmd::byId($cmd_id);
             if (is_object($cmd) && $cmd->getIsHistorized() == 1) {
                 $prevDatetime = null;
                 $prevValue = 0;
                 foreach ($cmd->getHistory($_startDate, $_endDate) as $history) {
                     if (!isset($cmd_histories[$history->getDatetime()])) {
                         $cmd_histories[$history->getDatetime()] = array();
                     }
                     if (!isset($cmd_histories[$history->getDatetime()]['#' . $cmd_id . '#'])) {
                         if ($prevDatetime != null) {
                             $datetime = strtotime($history->getDatetime());
                             while ($now - strtotime($prevDatetime) > $archiveTime && strtotime($prevDatetime) < $datetime) {
                                 $prevDatetime = date('Y-m-d H:00:00', strtotime($prevDatetime) + $packetTime);
                                 $cmd_histories[$prevDatetime]['#' . $cmd_id . '#'] = 0;
                             }
                             while ($now - strtotime($prevDatetime) > 300 && strtotime($prevDatetime) < $datetime) {
                                 $prevDatetime = date('Y-m-d H:i:00', strtotime($prevDatetime) + 300);
                                 $cmd_histories[$prevDatetime]['#' . $cmd_id . '#'] = $prevValue;
                             }
                         }
                         if (strtotime($history->getDatetime()) <= $now) {
                             $cmd_histories[$history->getDatetime()]['#' . $cmd_id . '#'] = $history->getValue();
                         }
                     }
                     $prevDatetime = $history->getDatetime();
                     $prevValue = $history->getValue();
                 }
             }
         }
     }
     foreach ($cmd_histories as $datetime => $cmd_history) {
         $datetime = floatval(strtotime($datetime . " UTC"));
         $calcul = template_replace($cmd_history, $this->getPower());
         try {
             if ($datetime <= $nowtime) {
                 $test = new evaluate();
                 $result = floatval($test->Evaluer($calcul));
                 if ($this->getConsumption() == '' && count($return['history']['power']) > 0) {
                     $last_datetime = end(array_keys($return['history']['power']));
                     if ($datetime - $last_datetime > 0) {
                         $last_value = end($return['history']['power']);
                         $return['history']['consumption'][$datetime] = array($datetime, $last_value[1] * (($datetime - $last_datetime) / 1000) / 3600);
                         $return['real']['consumption'] += $return['history']['consumption'][$datetime][1];
                     }
                 }
                 $return['history']['power'][$datetime] = array($datetime * 1000, $result);
                 if ($return['stats']['minPower'] === null || $return['stats']['minPower'] > $result) {
                     $return['stats']['minPower'] = $result;
                 }
                 if ($return['stats']['maxPower'] === null || $return['stats']['maxPower'] < $result) {
                     $return['stats']['maxPower'] = $result;
                 }
             }
         } catch (Exception $e) {
         }
     }
     if ($this->getConsumption() == '' && count($return['history']['power']) > 0) {
         $last_datetime = end(array_keys($return['history']['power']));
         $last_value = end($return['history']['power']);
         if ($datetime - $last_datetime > 0) {
             $return['history']['consumption'][$datetime] = array($datetime * 1000, $last_value[1] * (($nowtime - $last_datetime) / 1000) / 3600);
             $return['real']['consumption'] += $return['history']['consumption'][$datetime][1];
         }
     }
     $calcul = cmd::cmdToValue($this->getPower());
     try {
         $test = new evaluate();
         $result = floatval($test->Evaluer($calcul));
         $return['real']['power'] = $result;
         $return['history']['power'][$nowtime] = array($nowtime * 1000, $result);
     } catch (Exception $e) {
     }
     if ($this->getConsumption() != '') {
         $cmd_histories = array();
         preg_match_all("/#([0-9]*)#/", $this->getConsumption(), $matches);
         foreach ($matches[1] as $cmd_id) {
             if (is_numeric($cmd_id)) {
                 $cmd = cmd::byId($cmd_id);
                 if (is_object($cmd) && $cmd->getIsHistorized() == 1) {
                     $prevDatetime = null;
                     foreach ($cmd->getHistory($_startDate, $_endDate) as $history) {
                         if (!isset($cmd_histories[$history->getDatetime()])) {
                             $cmd_histories[$history->getDatetime()] = array();
                         }
                         if (!isset($cmd_histories[$history->getDatetime()]['#' . $cmd_id . '#'])) {
                             if ($prevDatetime != null) {
                                 $datetime = strtotime($history->getDatetime());
                                 while ($now - strtotime($prevDatetime) > $archiveTime && strtotime($prevDatetime) < $datetime) {
                                     $prevDatetime = date('Y-m-d H:00:00', strtotime($prevDatetime) + $packetTime);
                                     $cmd_histories[$prevDatetime]['#' . $cmd_id . '#'] = 0;
                                 }
                                 while ($now - strtotime($prevDatetime) < 300 && strtotime($prevDatetime) < $datetime) {
                                     $prevDatetime = date('Y-m-d H:00:00', strtotime($prevDatetime) + 300);
                                     $cmd_histories[$prevDatetime]['#' . $cmd_id . '#'] = $prevValue;
                                 }
                             }
                             if (strtotime($history->getDatetime()) <= $now) {
                                 $cmd_histories[$history->getDatetime()]['#' . $cmd_id . '#'] = $history->getValue();
                             }
                         }
                         $prevDatetime = $history->getDatetime();
                         $prevValue = $history->getValue();
                     }
                 }
             }
         }
         foreach ($cmd_histories as $datetime => $cmd_history) {
             $datetime = floatval(strtotime($datetime . " UTC"));
             $calcul = template_replace($cmd_history, $this->getConsumption());
             try {
                 if ($datetime <= $nowtime) {
                     $test = new evaluate();
                     $result = floatval($test->Evaluer($calcul));
                     $return['history']['consumption'][$datetime] = array($datetime * 1000, $result);
                 }
             } catch (Exception $e) {
             }
         }
         $calcul = cmd::cmdToValue($this->getConsumption());
         try {
             $test = new evaluate();
             $result = floatval($test->Evaluer($calcul));
             $return['real']['consumption'] = $result;
             $return['history']['consumption'][$nowtime] = array($nowtime * 1000, $result);
         } catch (Exception $e) {
         }
     }
     if (is_array($return['history']['consumption'])) {
         ksort($return['history']['consumption']);
     }
     if (is_array($return['history']['power'])) {
         ksort($return['history']['power']);
     }
     return $return;
 }
 public function execute($_options = null)
 {
     switch ($this->getType()) {
         case 'info':
             if ($this->getConfiguration('virtualAction', 0) == '0') {
                 try {
                     $result = jeedom::evaluateExpression($this->getConfiguration('calcul'));
                     if ($this->getSubType() == 'numeric') {
                         if (is_numeric($result)) {
                             $result = number_format($result, 2);
                         } else {
                             $result = str_replace('"', '', $result);
                         }
                         if (strpos($result, '.') !== false) {
                             $result = str_replace(',', '', $result);
                         } else {
                             $result = str_replace(',', '.', $result);
                         }
                     }
                     return $result;
                 } catch (Exception $e) {
                     log::add('virtual', 'info', $e->getMessage());
                     return jeedom::evaluateExpression(str_replace('"', '', cmd::cmdToValue($this->getConfiguration('calcul'))));
                 }
             }
             break;
         case 'action':
             $virtualCmd = virtualCmd::byId($this->getConfiguration('infoId'));
             if (!is_object($virtualCmd)) {
                 $cmds = explode('&&', $this->getConfiguration('infoName'));
                 if (is_array($cmds)) {
                     foreach ($cmds as $cmd_id) {
                         $cmd = cmd::byId(str_replace('#', '', $cmd_id));
                         $cmd->execCmd($_options);
                     }
                     return;
                 } else {
                     $cmd = cmd::byId(str_replace('#', '', $this->getConfiguration('infoName')));
                     return $cmd->execCmd($_options);
                 }
             } else {
                 if ($virtualCmd->getEqType() != 'virtual') {
                     throw new Exception(__('La cible de la commande virtuel n\'est pas un équipement de type virtuel', __FILE__));
                 }
                 if ($this->getSubType() == 'slider') {
                     $value = $_options['slider'];
                 } else {
                     if ($this->getSubType() == 'color') {
                         $value = $_options['color'];
                     } else {
                         $value = $this->getConfiguration('value');
                     }
                 }
                 $result = jeedom::evaluateExpression($value);
                 if ($this->getSubtype() == 'message') {
                     $result = $_options['title'] . ' ' . $_options['message'];
                 }
                 $virtualCmd->event($result);
             }
             break;
     }
 }