Esempio n. 1
0
 /**
  *
  * @param type $_options
  * @param type $cache 0 = ignorer le cache , 1 = mode normal, 2 = cache utilisé même si expiré (puis marqué à recollecter)
  * @return command result
  * @throws Exception
  */
 public function execCmd($_options = null, $cache = 1, $_sendNodeJsEvent = true, $_quote = false)
 {
     if ($this->getEventOnly() == 1) {
         $cache = 2;
     }
     if ($this->getType() == 'info' && $cache != 0) {
         $mc = cache::byKey('cmd' . $this->getId(), $cache == 2 ? true : false);
         if ($cache == 2 || !$mc->hasExpired()) {
             if ($mc->hasExpired()) {
                 $this->setCollect(1);
             }
             $this->setCollectDate($mc->getOptions('collectDate', $mc->getDatetime()));
             $this->setValueDate($mc->getOptions('valueDate', $mc->getDatetime()));
             return $mc->getValue();
         }
     }
     $eqLogic = $this->getEqLogic();
     if (!is_object($eqLogic) || $eqLogic->getIsEnable() != 1) {
         throw new Exception(__('Equipement désactivé - impossible d\'exécuter la commande : ' . $this->getHumanName(), __FILE__));
     }
     $eqLogic->emptyCacheWidget();
     try {
         if ($_options !== null && $_options !== '') {
             $options = self::cmdToValue($_options);
             if (is_json($_options)) {
                 $options = json_decode($_options, true);
             }
         } else {
             $options = null;
         }
         if (isset($options['color'])) {
             $options['color'] = str_replace('"', '', $options['color']);
         }
         if ($this->getSubType() == 'color' && isset($options['color']) && substr($options['color'], 0, 1) != '#') {
             $options['color'] = cmd::convertColor($options['color']);
         }
         if ($this->getType() == 'action') {
             log::add('event', 'event', __('Exécution de la commande ', __FILE__) . $this->getHumanName() . __(' avec les paramètres ', __FILE__) . str_replace(array("\n", '  ', 'Array'), '', print_r($options, true)));
         }
         $value = $this->formatValue($this->execute($options), $_quote);
     } catch (Exception $e) {
         //Si impossible de contacter l'équipement
         $type = $eqLogic->getEqType_name();
         if ($eqLogic->getConfiguration('nerverFail') != 1) {
             $numberTryWithoutSuccess = $eqLogic->getStatus('numberTryWithoutSuccess', 0);
             $eqLogic->setStatus('numberTryWithoutSuccess', $numberTryWithoutSuccess);
             if ($numberTryWithoutSuccess >= config::byKey('numberOfTryBeforeEqLogicDisable')) {
                 $message = 'Désactivation de <a href="' . $eqLogic->getLinkToConfiguration() . '">' . $eqLogic->getName();
                 $message .= '</a> car il n\'a pas répondu ou mal répondu lors des 3 derniers essais';
                 message::add($type, $message);
                 $eqLogic->setIsEnable(0);
                 $eqLogic->save();
             }
         }
         log::add($type, 'error', __('Erreur execution de la commande ', __FILE__) . $this->getHumanName() . ' : ' . $e->getMessage());
         throw $e;
     }
     if ($this->getType() == 'info' && $value !== false) {
         if ($this->getCollectDate() == '') {
             $this->setCollectDate(date('Y-m-d H:i:s'));
         }
         if ($this->getValueDate() == '') {
             $this->setValueDate(date('Y-m-d H:i:s'));
         }
         cache::set('cmd' . $this->getId(), $value, $this->getCacheLifetime(), array('collectDate' => $this->getCollectDate(), 'valueDate' => $this->getValueDate()));
         $this->setCollect(0);
         $nodeJs = array(array('cmd_id' => $this->getId()));
         foreach (self::byValue($this->getId()) as $cmd) {
             $nodeJs[] = array('cmd_id' => $cmd->getId());
         }
         nodejs::pushUpdate('eventCmd', $nodeJs);
     }
     if ($this->getType() != 'action' && !is_array($value) && strpos($value, 'error') === false) {
         if ($eqLogic->getStatus('numberTryWithoutSuccess') != 0) {
             $eqLogic->setStatus('numberTryWithoutSuccess', 0);
         }
         $eqLogic->setStatus('lastCommunication', date('Y-m-d H:i:s'));
     }
     if ($this->getType() == 'action' && $options !== null && $this->getValue() == '') {
         if (isset($options['slider'])) {
             $this->setConfiguration('lastCmdValue', $options['slider']);
             $this->save();
         }
         if (isset($options['color'])) {
             $this->setConfiguration('lastCmdValue', $options['color']);
             $this->save();
         }
     }
     if ($this->getType() == 'action' && $this->getConfiguration('updateCmdId') != '') {
         $cmd = cmd::byId($this->getConfiguration('updateCmdId'));
         if (is_object($cmd)) {
             $value = $this->getConfiguration('updateCmdToValue');
             switch ($this->getSubType()) {
                 case 'slider':
                     $value = str_replace('#slider#', $options['slider'], $value);
                     break;
                 case 'color':
                     $value = str_replace('#color#', $options['color'], $value);
                     break;
             }
             $cmd->event($value);
         }
     }
     return $value;
 }