Tuto : Monitorer son infra domotique (serveur / batterie / last communication) sur JeedomConnect

Salut @ngrataloup,

J’ai fait la configuration pour les batteries et les Com.

pour les Com ça fonctionne, mais les batteries l’info ne remonte pas du core :

Pourtant j’en ai un en Warning :
image

le code que j’ai modifier, j’ai bien contrôler les noms des virtuels ! c’est ok et quand je lance le scenario, il actualise bien les virtuels :

require_once dirname(__FILE__) . '/../../plugins/JeedomConnect/core/class/JeedomConnectWidget.class.php';

$plugins                   = array('Deconz', 'Z-Wave', 'network'); // plugins à prendre en compte
$eqIdExclude               = array(); // equipements à exclure
// Equipement viruel
$eqVirtuel                 = '[Notifications][Vérification des batteries]';

// Choix des verifs
$checkBatteryEnable        = 1; // 1 -> activer le check batterie , 0 -> desactiver
$checkComEnable            = 1; // 1 -> activer le check com , 0 -> desactiver
// notification
$EnableNotifJC             = 0; // Autoriser les notifs
$cmdNotifJC                = "#[Norbert][JC_Norbert][Notif_Defaut_Infra]#"; // Commande de notification (avec les # autour)
$AlertPageIdJC             = 94; // ID de la page d'alerte sur JC
$heureNotifJC              = 6; // 1 seule notif par jour à $heureNotifJC heure
//Seuils d'alerte
$seuilBatteryWarning       = config::byKey('battery::warning', 'core'); // recupéré de la page equipement de la config générale Jeedom
$seuilBatteryDanger        = config::byKey('battery::danger', 'core'); // recupéré de la page equipement de la config générale Jeedom
$seuilComWarning           = 86400; // 86400 = 1 jour
$seuilComDanger            = 86400 * 2; // 2 jours
//initialisation des variables
$nbBatteryOK               = 0;
$nbBatteryWarning          = 0;
$nbBatteryDanger           = 0;
$nbBatteryTotal            = 0;
$listBatteryWarning        = '';
$listBatteryDanger         = '';
$nbComOK                   = 0;
$nbComWarning              = 0;
$nbComDanger               = 0;
$nbComTotal                = 0;
$listComWarning            = '';
$listComDanger             = '';

$eqVirtuelBatteryWarning   = '#' . $eqVirtuel . '[Batterie Warning]#';
$eqVirtuelBatteryDanger    = '#' . $eqVirtuel . '[Batterie Danger]#';
$eqVirtuelBatteryNbOK      = '#' . $eqVirtuel . '[NB Batterie OK]#';
$eqVirtuelBatteryNbWarning = '#' . $eqVirtuel . '[NB Batterie Warning]#';
$eqVirtuelBatteryNbDanger  = '#' . $eqVirtuel . '[NB Batterie Danger]#';
$eqVirtuelBatteryNbTotal   = '#' . $eqVirtuel . '[NB Equipements batterie]#';
$eqVirtuelComWarning       = '#' . $eqVirtuel . '[Com Warning]#';
$eqVirtuelComDanger        = '#' . $eqVirtuel . '[Com Danger]#';
$eqVirtuelComNbOK          = '#' . $eqVirtuel . '[NB Com OK]#';
$eqVirtuelComNbWarning     = '#' . $eqVirtuel . '[NB Com Warning]#';
$eqVirtuelComNbDanger      = '#' . $eqVirtuel . '[NB Com Danger]#';
$eqVirtuelComNbTotal       = '#' . $eqVirtuel . '[NB Equipements Com]#';

if ($checkBatteryEnable)
{ // 0 - pas de check, 1 - check batt, 2 ; check com, 3 check batt + com
  if ($checkComEnable) $checksEnable = 3;
  else $checksEnable = 1;
}
else
{
  if ($checkComEnable) $checksEnable = 2;
  else
  {
    $scenario->setLog("Aucun check activé");
    exit("Aucun check activé");
  }
}

foreach ($plugins as $plugin)
{ // Pour chaque plugin
  foreach (eqLogic::byType($plugin, true) as $eqLogic)
  { // pour chaque equipement du plugin
    $eqId              = $eqLogic->getId();

    if (in_array($eqId, $eqIdExclude) == false)
    { // Si l'equipememnt n'et spas dans les equipements exclus des verifs
      $eqLogicName       = $eqLogic->getName();
      $eqId              = $eqLogic->getId();
      $eqLogicHumaneName = $eqLogic->getHumanName();
      $eqLogicId         = $eqLogic->getId();
      $eqLogicType       = $eqLogic->getEqType_name();
      $objID             = $eqLogic->getObject_id();
      switch ($plugin)
      {
        case "zigbee":
          $deviceModel       = $eqLogic->getConfiguration('device');
        break;
        case "openzwave":
          $deviceModel = $eqLogic->getConfiguration('product_name');
        break;
        case "jMQTT":
          $deviceModel = $eqLogic->getConfiguration('commentaire');
        break;
        default:
          $deviceModel = $eqLogic->getConfiguration('device_model');
        break;
      }

      // Calcul des niveau d'alarmes des batteries
      if ($checkBatteryEnable)
      {
        if (!empty($deviceModel))
        { // si il n'y a pas de numero de model, on considere que ce n'est pas un equipement matériel, donc pas de batterie !!
          switch ($deviceModel)
          {
            case 'TS0601_thermostat': // je fais un test dans ma sitution perso sur ce modele qui ne renvoie pas un % de batterie, mais un booleen batterie faible, ous pouvez integrer des exceptions de gesiton de batterie ici
              $batteryLowBinaire = cmd::byString("#" . $eqLogicHumaneName . "[Batterie faible]#")->execCmd();
              if ($batteryLowBinaire) $batteryStatus     = 0;
              else $batteryStatus     = 100;
              break;
            default:
              $batteryStatus = $eqLogic->getStatus('battery');
              break;
            }

            if (isset($batteryStatus) and $batteryStatus !== "")
            { // comptabilisation des batteries OK ou en warning ou en critical
              $nbBatteryTotal++;
              if ($batteryStatus <= $seuilBatteryDanger)
              {
                $nbBatteryDanger++;
                $listBatteryDanger = $listBatteryDanger . "<span style=\"font-size:13px\"><b>" . $eqLogicHumaneName . "</b> (" . $eqLogicType . "): " . $batteryStatus . "%<br></span>";
              }
              elseif ($batteryStatus <= $seuilBatteryWarning)
              {
                $nbBatteryWarning++;
                $listBatteryWarning = $listBatteryWarning . "<span style=\"font-size:13px\"><b>" . $eqLogicHumaneName . "</b> (" . $eqLogicType . "): " . $batteryStatus . "%<br></span>";
              }
              else
              {
                $nbBatteryOK++;
              }
            }
          }
      }

      // calcul des niveaux d'alarme des lastCom
      if ($checkComEnable)
      {
        $lastCom     = strtotime($eqLogic->getStatus('lastCommunication'));
        $elapsedTime = time() - $lastCom;

        if (isset($lastCom) and $lastCom !== "")
        { // comptabilisation des LastCom OK ou en warning ou en critical
          $nbComTotal++;
          if ($elapsedTime >= $seuilComDanger)
          {
            $nbComDanger++;
            $listComDanger = $listComDanger . "<span style=\"font-size:13px\"><b>" . $eqLogicHumaneName . "</b> (" . $eqLogicType . "): " . round($elapsedTime / 3600) . "h<br></span>";
          }
          elseif ($elapsedTime >= $seuilComWarning)
          {
            $nbComWarning++;
            $listComWarning = $listComWarning . "<span style=\"font-size:13px\"><b>" . $eqLogicHumaneName . "</b> (" . $eqLogicType . "): " . round($elapsedTime / 3600) . "h<br></span>";
          }
          else
          {
            $nbComOK++;
          }
          switch ($checksEnable)
          {
            case 1:
              $scenario->setLog("	# [" . $plugin . "] Equipment : " . $eqLogicHumaneName . " (" . $deviceModel . ") => battery : " . $batteryStatus . ", Com : N/A");
            break;
            case 2:
              $scenario->setLog("	# [" . $plugin . "] Equipment : " . $eqLogicHumaneName . " (" . $deviceModel . ") => battery : N/A, Com : " . $elapsedTime);
            break;
            case 3:
              $scenario->setLog("	# [" . $plugin . "] Equipment : " . $eqLogicHumaneName . " (" . $deviceModel . ") => battery : " . $batteryStatus . ", Com : " . $elapsedTime);
            break;
          }
        }
      }
    }
  }
}

// Affichage-formatage des resultats dans les virtuels
if ($checkBatteryEnable)
{
  if ($nbBatteryWarning == 0) $listBatteryWarning = "<span style=\"font-size:13px\"><b>Aucun équipement en warning</b></span>";
  if ($nbBatteryDanger == 0) $listBatteryDanger  = "<span style=\"font-size:13px\"><b>Aucun équipement en critical</b></span>";
  cmd::byString($eqVirtuelBatteryNbOK)->event($nbBatteryOK);
  cmd::byString($eqVirtuelBatteryNbWarning)->event($nbBatteryWarning);
  cmd::byString($eqVirtuelBatteryNbDanger)->event($nbBatteryDanger);
  cmd::byString($eqVirtuelBatteryNbTotal)->event($nbBatteryTotal);
  cmd::byString($eqVirtuelBatteryWarning)->event($listBatteryWarning);
  cmd::byString($eqVirtuelBatteryDanger)->event($listBatteryDanger);
}
else $scenario->setLog("Check batterie desactivé");

if ($checkComEnable)
{
  if ($nbComWarning == 0) $listComWarning = "<span style=\"font-size:13px\"><b>Aucun équipement en warning</b></span>";
  if ($nbComDanger == 0) $listComDanger  = "<span style=\"font-size:13px\"><b>Aucun équipement en critical</b></span>";
  cmd::byString($eqVirtuelComNbOK)->event($nbComOK);
  cmd::byString($eqVirtuelComNbWarning)->event($nbComWarning);
  cmd::byString($eqVirtuelComNbDanger)->event($nbComDanger);
  cmd::byString($eqVirtuelComNbTotal)->event($nbComTotal);
  cmd::byString($eqVirtuelComWarning)->event($listComWarning);
  cmd::byString($eqVirtuelComDanger)->event($listComDanger);
}
else $scenario->setLog("Check Last Communication desactivé");

// Envoi d'une notif JC
if ($EnableNotifJC && date("G") == $heureNotifJC)
{
  if ($nbBatteryDanger >= 0 || $nbComDanger >= 1)
  {
    $cmd     = cmd::byString($cmdNotifJC);
    switch ($checksEnable)
    {
      case 1:
        $cmd->execCmd($options = array('title'         => 'title=Defaut infrastructure | gotoPageId=' . $AlertPageIdJC, 'message'         => "Batteries -><span style=\"color: green\"> OK : " . $nbBatteryOK . ",</span><span style=\"color: orange\"> Warning : " . $nbBatteryWarning . ", </span><span style=\"color: red\">Critical : " . $nbBatteryDanger . "</span>"), $cache   = 0);
      break;
      case 2:
        $cmd->execCmd($options = array('title'         => 'title=Defaut infrastructure | gotoPageId=' . $AlertPageIdJC, 'message'         => "Com -><span style=\"color: green\"> OK : " . $nbComOK . ",</span><span style=\"color: orange\"> Warning : " . $nbComWarning . ", </span><span style=\"color: red\">Critical : " . $nbComDanger . "</span>"), $cache   = 0);
      break;
      case 3:
        $cmd->execCmd($options = array('title'         => 'title=Defaut infrastructure | gotoPageId=' . $AlertPageIdJC, 'message'         => "Batteries -><span style=\"color: green\"> OK : " . $nbBatteryOK . ",</span><span style=\"color: orange\"> Warning : " . $nbBatteryWarning . ", </span><span style=\"color: red\">Critical : " . $nbBatteryDanger . "</span><br>Com       -><span style=\"color: green\"> OK : " . $nbComOK . ",</span><span style=\"color: orange\"> Warning : " . $nbComWarning . ", </span><span style=\"color: red\">Critical : " . $nbComDanger . "</span>"), $cache   = 0);
      break;
    }

  }
}
else $scenario->setLog("Pas de notif, prochaine notif à " . $heureNotifJC . "h");

Merci d’avance pour ton aide