1
0
moha/hooks/scripts/rdc_store.php

162 lines
4.1 KiB
PHP
Raw 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";
public $active = true; //enable/disable hook (true => enabled)
public $timer = 0;
public $luminance_min = 60;
public $luminance_max = 3000;
public $storeDownTime = 38;
public $storeUpTime = 41;
protected $storeLevel;
protected $propertyInitialized =array();
2022-03-04 23:32:39 +01:00
protected $devicelist = array(
RDC_EXTERIEUR_LUMINOSITE => "illuminance_lux", // "ON"/"OFF"
METEO => "rainin",
//METEO => array("solarradiation", false),
METEO => "windspeedkmh",
METEO => "windgustkmh"
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;
$exterieur_lux = 0;
$rafale = 0;
$soleil = 0;
$storeDevice = $indexDevices[RDC_STORE];
if (array_key_exists("position", $storeDevice->properties))
{
$storeLevel = $storeDevice->properties["position"];
}else
{
return true;
}
if (array_key_exists("illuminance_lux", $indexDevices[RDC_EXTERIEUR_LUMINOSITE]->properties))
{
$exterieur_lux = $mohaDB->moyenne($indexDevices[RDC_EXTERIEUR_LUMINOSITE], "illuminance_lux", 15);
}
if (array_key_exists("rainin", $indexDevices[METEO]->properties))
{
$rain = $indexDevices[METEO]->properties["rainin"];
}
if (array_key_exists("windgustkmh", $indexDevices[METEO]->properties))
{
$rafale = $mohaDB->moyenne($indexDevices[METEO], "windgustkmh", 12);
}
if (array_key_exists("solarradiation", $indexDevices[METEO]->properties))
{
$soleil = $mohaDB->moyenne($indexDevices[METEO], "solarradiation", 12);
}
logger(DEBUG, sprintf(_("property=%s, value=%s"), $property, $value), __FILE__ . ":" . __LINE__);
if ($rafale >= 6 or $rain != 0)
{
$maxLevel = 0;
}else
{
if ($rafale != 0)
{
$maxLevel = 100 / $rafale;
}
}
switch ($property)
{
case "illuminance_lux":
logger(DEBUG, "CASE: illuminance_lux:" . $value, __FILE__ . ":" . __LINE__);
if ($exterieur_lux > 3000)
{
if ($rain == 0 and $maxLevel != 0)
{
$store2level = storeLevel + 15;
if ( $store2level > $maxLevel )
{
$this->open($maxLevel);
}else
{
$this->open($max2level);
}
}
}elseif ($exterieur_lux < 25000)
{
$this->close("Luminosité faible");
}
break;
case "rainin":
logger(DEBUG, "CASE: rainin:" . $value, __FILE__ . ":" . __LINE__);
$this->close("Pluie");
break;
case "windgustkmh";
logger(DEBUG, "CASE: windgustkmh:" . $value, __FILE__ . ":" . __LINE__);
case "windspeedkmh":
logger(DEBUG, "CASE: windspeedkmh:" . $value, __FILE__ . ":" . __LINE__);
if($value > 50)
{
$this->close("Trop de vent");
}elseif ($value > 40 and $this->storeLevel > 20)
{
$this->maxLevel = 20;
$this->send(20);
}elseif ($value > 30 and $this->storeLevel > 40)
{
$this->maxLevel = 40;
$this->send(40);
}elseif ($value >20 and $this->storeLevel > 60)
{
$this->maxLevel = 60;
$this->send(60);
}elseif ($value >10 and $this->storeLevel > 80)
{
$this->maxLevel = 80;
$this->send(80);
}else
{
$this->maxLevel = 100;
}
$this->close("Fortes rafales de vent");
break;
default:
}
}
2022-03-04 23:32:39 +01:00
private function open ($level)
{
if ($this->storeLevel < $level)
{
logger(DEBUG, "Open store :" . $level, __FILE__ . ":" . __LINE__);
$this->send(100 - $level);
}
}
2022-03-04 23:32:39 +01:00
private function close ($reason)
{
if ($this->storeLevel != 0)
{
logger(DEBUG, "Close store :" . $level, __FILE__ . ":" . __LINE__);
print ( "store fermé " . $reason);
$this->send(100);
}
}
2022-03-04 23:32:39 +01:00
private function send($level)
{
global $indexDevices;
$deviceObject = $indexDevices[RDC_STORE];
$msg = array("position" => $level);
logger(INFO, sprintf(_("publishing message: %s to %s"), json_encode($msg), $deviceObject->friendlyName), __FILE__ . ":" . __LINE__);
$deviceObject->payload = $msg;
$deviceObject->set(AUTO);
}
}
2022-03-04 23:32:39 +01:00
$hooks["rdc_store"] = new rdc_store();
2022-03-04 23:32:39 +01:00
?>