1
0
moha/hooks/scripts/rdc_store.php

264 lines
8.5 KiB
PHP
Raw Permalink Normal View History

2022-03-04 23:32:39 +01:00
<?php
class rdc_store extends hook
2022-03-04 23:32:39 +01:00
{
public $hookName = "rdc_store";
2022-06-13 19:22:08 +02:00
public $active = true; //enable/disable hook (true => enabled)
2022-03-04 23:32:39 +01:00
public $timer = 0;
public $luminance_min = 60;
public $luminance_max = 3000;
public $storeDownTime = 38;
public $storeUpTime = 41;
public $maxLevel;
2022-06-10 12:44:09 +02:00
public $storeLevel;
protected $propertyInitialized =array();
2022-03-04 23:32:39 +01:00
protected $devicelist = array(
RDC_EXTERIEUR_LUMINOSITE => "illuminance_lux",
2022-04-23 02:00:52 +02:00
RDC_SALON_LUMINOSITE => "illuminance_lux",
2022-09-05 13:47:41 +02:00
RDC_STORE_VIBRATION => "vibration",
METEO => "rainin",
2022-04-23 02:00:52 +02:00
METEO => "solarradiation",
METEO => "windspeedkmh",
METEO => "windgustkmh"
2022-03-04 23:32:39 +01:00
);
function installHooks(&$indexDevices)
{
return $this->installHooksFunction($indexDevices);
}
2022-03-04 23:32:39 +01:00
// callback fonction. Is called with these 3 parameters
// $device -> calling device
// $property -> property of the device (given by mqtt)
// $value -> value of the property
public function callBack($device, $property, $value)
{
global $mohaDB, $devices, $indexDevices;
2022-03-04 23:32:39 +01:00
logger(DEBUG, "Callback : RDC_STORE", __FILE__ . ":" . __LINE__);
$rain = 0;
2022-05-05 21:28:26 +02:00
static $rainS;
$exterieur_lux = 0;
2022-06-10 12:44:09 +02:00
$exterieurLuxMoyen =0;
$salon_lux = 0;
$rafale = 0;
$soleil = 0;
$storeDevice = $indexDevices[RDC_STORE];
2022-09-19 23:40:29 +02:00
$r = 0;
2022-06-13 19:22:08 +02:00
$moving = "STOP";
$store2level = false;
2022-09-19 23:40:29 +02:00
$lastVibration = 0;
2022-09-19 23:40:29 +02:00
if ($this->active == true)
{
if (array_key_exists("vibration", $indexDevices[RDC_STORE_VIBRATION]->properties))
2022-04-23 02:00:52 +02:00
{
$lastVibration = $mohaDB->moyenne($indexDevices[RDC_STORE_VIBRATION], "vibration", 5);
2022-09-19 23:40:29 +02:00
logger(DEBUG, "Last vibration inferior to 5 minutes" . bool2string($lastVibration ), __FILE__ . ":" . __LINE__);
2022-04-23 02:00:52 +02:00
}
2022-09-19 23:40:29 +02:00
if (array_key_exists("moving", $storeDevice->properties))
2022-06-10 12:44:09 +02:00
{
2022-09-19 23:40:29 +02:00
$moving = $storeDevice->properties["moving"]["value"];
logger(DEBUG, "Moving = " . $moving, __FILE__ . ":" . __LINE__);
2022-06-10 12:44:09 +02:00
}
2022-09-19 23:40:29 +02:00
if (array_key_exists(METEO, $indexDevices))
2022-06-13 19:22:08 +02:00
{
2022-09-19 23:40:29 +02:00
if (array_key_exists("rainin", $indexDevices[METEO]->properties))
2022-04-23 02:00:52 +02:00
{
2022-09-19 23:40:29 +02:00
$rainTmp = $indexDevices[METEO]->properties["rainin"]["value"];
$rain = $rainTmp - $rainS;
$rainS = $rainTmp;
logger(DEBUG, "rain=" . $rain, __FILE__ . ":" . __LINE__);
2022-04-23 02:00:52 +02:00
}
2022-09-19 23:40:29 +02:00
if (array_key_exists("windgustkmh", $indexDevices[METEO]->properties))
2022-06-10 12:44:09 +02:00
{
2022-09-19 23:40:29 +02:00
if ($indexDevices[METEO]->properties["windgustkmh"]["value"] > 50)
{
logger(ALERT, "Vent fort :" . $indexDevices[METEO]->properties["windgustkmh"]["value"], __FILE__ . ":" . __LINE__, $device);
}
$rafale = $mohaDB->moyenne($indexDevices[METEO], "windgustkmh", 10);
if ($rafale == 0)
{
$rafale = $indexDevices[METEO]->properties["windgustkmh"]["value"];
}
logger(DEBUG, "rafale=" . $rafale, __FILE__ . ":" . __LINE__);
2022-06-13 19:22:08 +02:00
}
2022-09-19 23:40:29 +02:00
if (array_key_exists("windspeedkmh", $indexDevices[METEO]->properties))
2022-06-13 19:22:08 +02:00
{
2022-09-19 23:40:29 +02:00
if ($indexDevices[METEO]->properties["windspeedkmh"]["value"] > 50)
{
logger(ALERT, "Vent fort :" . $indexDevices[METEO]->properties["windspeedkmh"]["value"], __FILE__ . ":" . __LINE__, $device);
}
$vent = $mohaDB->moyenne($indexDevices[METEO], "windspeedkmh", 6);
if ($vent == 0)
{
$vent = $indexDevices[METEO]->properties["windspeedkmh"]["value"];
}
logger(DEBUG, "vitesse du vent =" . $vent, __FILE__ . ":" . __LINE__);
2022-06-13 19:22:08 +02:00
}
2022-09-19 23:40:29 +02:00
if ($vent > $rafale)
2022-06-13 19:22:08 +02:00
{
2022-09-19 23:40:29 +02:00
$rafale = $vent;
}
if (array_key_exists("solarradiation", $indexDevices[METEO]->properties))
{
$soleil = $mohaDB->moyenne($indexDevices[METEO], "solarradiation", 10);
logger(DEBUG, "soleil=" . $soleil, __FILE__ . ":" . __LINE__);
2022-06-13 19:22:08 +02:00
}
2022-07-07 22:46:05 +02:00
}
2022-09-19 23:40:29 +02:00
if (array_key_exists("position", $storeDevice->properties))
2022-07-07 22:46:05 +02:00
{
2022-09-19 23:40:29 +02:00
$this->storeLevel = $storeDevice->properties["position"]["value"];
logger(DEBUG, "storeLevel=" . $this->storeLevel, __FILE__ . ":" . __LINE__);
2022-09-09 16:53:19 +02:00
}else
2022-07-07 22:46:05 +02:00
{
2022-09-19 23:40:29 +02:00
$this->storeLevel = 0;
2022-07-07 22:46:05 +02:00
}
2022-09-19 23:40:29 +02:00
if (array_key_exists("illuminance_lux", $indexDevices[RDC_FACADE_LUMINOSITE]->properties))
2022-06-13 19:22:08 +02:00
{
2022-09-19 23:40:29 +02:00
$facade_lux = $indexDevices[RDC_FACADE_LUMINOSITE]->properties["illuminance_lux"]["value"];
$facadeLuxMoyen = $mohaDB->moyenne($indexDevices[RDC_FACADE_LUMINOSITE], "illuminance_lux", 10);
if ($facadeLuxMoyen == 0)
{
$facadeLuxMoyen = $facade_lux;
}
logger(DEBUG, "facade_lux=" . $facade_lux . " et facadeLuxMoyen=" . $facadeLuxMoyen, __FILE__ . ":" . __LINE__);
}
2022-06-13 19:22:08 +02:00
2022-09-19 23:40:29 +02:00
if (array_key_exists("illuminance_lux", $indexDevices[RDC_EXTERIEUR_LUMINOSITE]->properties))
{
2022-09-19 23:40:29 +02:00
$exterieur_lux = $indexDevices[RDC_EXTERIEUR_LUMINOSITE]->properties["illuminance_lux"]["value"];
$exterieurLuxMoyen = $mohaDB->moyenne($indexDevices[RDC_EXTERIEUR_LUMINOSITE], "illuminance_lux", 10);
if ($exterieurLuxMoyen == 0)
{
$exterieurLuxMoyen = $exterieur_lux;
}
logger(DEBUG, "exterieur_lux=" . $exterieur_lux . " et exterieurLuxMoyen=" . $exterieurLuxMoyen, __FILE__ . ":" . __LINE__);
}
2022-09-19 23:40:29 +02:00
if (array_key_exists("illuminance_lux", $indexDevices[RDC_SALON_LUMINOSITE]->properties))
2022-06-13 19:22:08 +02:00
{
2022-09-19 23:40:29 +02:00
$salon_lux = $mohaDB->moyenne($indexDevices[RDC_SALON_LUMINOSITE], "illuminance_lux", 10);
if ($salon_lux == 0)
{
$salon_lux = $indexDevices[RDC_SALON_LUMINOSITE]->properties["illuminance_lux"]["value"];
}
logger(DEBUG, "salon_lux=" . $salon_lux, __FILE__ . ":" . __LINE__);
2022-09-09 16:53:19 +02:00
}
2022-09-19 23:40:29 +02:00
// Calcul de la position du store
if ($exterieurLuxMoyen > 36000 or $exterieur_lux > 50000 or $salon_lux >600)
{
2022-09-19 23:40:29 +02:00
logger(DEBUG, "exterieurLuxMoyen > 36000 or exterieur_lux > 50000 or salon_lux >600", __FILE__ . ":" . __LINE__);
2022-09-09 16:53:19 +02:00
$store2level = $this->storeLevel + 20;
2022-09-19 23:40:29 +02:00
}elseif ($exterieurLuxMoyen < 5000 and $salon_lux < 200 and $soleil < 100 and $facadeLuxMoyen < 10000)
{
logger(DEBUG, "exterieurLuxMoyen < 5000 and salon_lux < 200 and soleil < 100 and facadeLuxMoyen < 10000", __FILE__ . ":" . __LINE__);
$this->close("Luminosité faible");
2022-09-09 16:53:19 +02:00
}
2022-09-19 23:40:29 +02:00
logger(DEBUG, sprintf(_("property=%s, value=%s"), $property, $value), __FILE__ . ":" . __LINE__);
if ($rafale >= 70 or $rain != 0)
{
$this->maxLevel = 0;
logger(DEBUG, sprintf("rafale = %s and rain = %s",$rafale, $rain), __FILE__ . ":" . __LINE__);
$this->close("vent trop fort ou pluie");
return 0;
}elseif ($rafale != 0)
{
//calcul du maxlevel par rapport a la vitesse des rafales
$r = round(($rafale-20)/4, 0, PHP_ROUND_HALF_UP)*10;
if ($r < 0)
{
$r = 0;
}else
{
$r = 100 - $r;
}
2022-09-09 16:53:19 +02:00
2022-09-19 23:40:29 +02:00
if ($lastVibration == 0)
{
2022-09-19 23:40:29 +02:00
$this->maxLevel = 100 - $r;
}
2022-09-19 23:40:29 +02:00
logger(DEBUG, "rafale=" . $rafale, __FILE__ . ":" . __LINE__);
}
logger(DEBUG, "maxlevel=" . $this->maxLevel, __FILE__ . ":" . __LINE__);
switch ($property)
{
case "rainin":
logger(DEBUG, "CASE: rainin:" . $value, __FILE__ . ":" . __LINE__);
$this->close("Pluie");
break;
case "windgustkmh":
logger(DEBUG, "CASE: windgustkmh:" . $rafale, __FILE__ . ":" . __LINE__);
break;
case "vibration":
//$tmpStore = $this->storeLevel - 20;
/*if ($store2level == false or $store2level > $tmpStore)
{
$this->setTo($tmpStore, AUTO);
}*/
2022-09-23 00:25:48 +02:00
logger(DEBUG, "CASE: vibration:" . bool2string($value), __FILE__ . ":" . __LINE__);
if ($moving == "STOP")
{
$this->maxLevel -= 20;
2022-09-23 00:25:48 +02:00
}else
{
logger(DEBUG, "Store is moving so maxLevel not changed " . $moving, __FILE__ . ":" . __LINE__);
}
2022-09-23 00:25:48 +02:00
2022-09-19 23:40:29 +02:00
default:
break;
}
if ($this->maxLevel < 0)
{
$this->maxLevel = 0;
}
logger(DEBUG, "maxLevel = " . $this->maxLevel . ' result => $r = ' . $r, __FILE__ . ":" . __LINE__);
2022-09-23 00:25:48 +02:00
if ($store2level !== false) //$store2level = false then no change on store position
2022-09-09 16:53:19 +02:00
{
2022-09-23 00:25:48 +02:00
logger(DEBUG, "setting stor 2 store2level", __FILE__ . ":" . __LINE__);
$this->setTo($store2level, AUTO);
2022-06-13 19:22:08 +02:00
}
}
}
2022-03-04 23:32:39 +01:00
2022-06-20 10:34:55 +02:00
private function setTo($level, $method)
{
2022-04-23 02:00:52 +02:00
logger(DEBUG, "function 'set' to level : " . $level, __FILE__ . ":" . __LINE__);
2022-09-23 00:25:48 +02:00
if ($level > $this->maxLevel)
2022-06-10 12:44:09 +02:00
{
2022-09-23 00:25:48 +02:00
$level = $this->maxLevel;
}elseif ($level <= 100)
{
logger(DEBUG, "Setting store position to:" . $level, __FILE__ . ":" . __LINE__);
2022-09-05 13:47:41 +02:00
$this->send($level, $method);
2022-09-23 00:25:48 +02:00
}
}
2022-03-04 23:32:39 +01:00
private function close ($reason)
{
2022-06-10 12:44:09 +02:00
if ($this->storeLevel > 0)
{
logger(DEBUG, "Close store :" . $reason, __FILE__ . ":" . __LINE__);
2022-09-09 16:53:19 +02:00
$this->send(0, IDLE);
}
}
2022-03-04 23:32:39 +01:00
private function send($level, $method)
{
global $indexDevices;
$deviceObject = $indexDevices[RDC_STORE];
2022-12-05 12:23:43 +01:00
$msg = array("position" => 100 - $level);
logger(INFO, sprintf(_("publishing message: %s to %s"), json_encode($msg), $deviceObject->friendlyName), __FILE__ . ":" . __LINE__);
$deviceObject->payload = $msg;
2022-06-20 10:34:55 +02:00
$deviceObject->set("position", $method);
}
}
2022-03-04 23:32:39 +01:00
$hooks["rdc_store"] = new rdc_store();
2022-06-13 19:22:08 +02:00
2022-03-04 23:32:39 +01:00
?>