1
0

added some javascript to webserver\nsome debugging

This commit is contained in:
daniel Tartavel 2022-03-06 01:30:33 +01:00
parent a2d2909a23
commit 9dde1c2d53
13 changed files with 230 additions and 179 deletions

View File

@ -78,6 +78,16 @@ class db extends mysqli
logger(INFO, sprintf(_("New value (%s) of property: '%s' of device: %s stored in database"), bool2string($value), $property, $device->friendlyName), __FILE__ . ":" . __LINE__); logger(INFO, sprintf(_("New value (%s) of property: '%s' of device: %s stored in database"), bool2string($value), $property, $device->friendlyName), __FILE__ . ":" . __LINE__);
} }
} }
function moyenne($deviceObject, $property, $time)
{
$query = "SELECT AVG(value) WHERE device='" . $deviceObject->ieeeAddress . "' AND property='" . $property . "' AND TIMEDIFF(NOW(), 'date') < " . $time;
if(!$this->result = $this->query($query))
{
logger(ERROR, _("mysql query errror: ") . $this->error, __FILE__ . ":" . __LINE__);
}
var_dump($this->result);
}
} }
$mohaDB = new db(); $mohaDB = new db();

View File

@ -7,7 +7,12 @@ function aliases($fn, $property)
{ {
logger(DEBUG, "exists " . $fn . "/" . $property, __FILE__ . ":" . __LINE__); logger(DEBUG, "exists " . $fn . "/" . $property, __FILE__ . ":" . __LINE__);
return "/" . $aliases[$fn . "/" . $property]; return "/" . $aliases[$fn . "/" . $property];
}else
{
return "/" . _($property);
} }
} }
$aliases = array( $aliases = array(

View File

@ -1,9 +1,18 @@
<?php <?php
$dashboards[0] = array( $dashboards["Général"] = array(
array(RDC_SALON_ECLAIRAGE_PANNEAU, "state"), array(RDC_SALON_ECLAIRAGE_PANNEAU, "state"),
array(RDC_SDB_WC_ECLAIRAGE, "state_l1"), array(RDC_SDB_WC_ECLAIRAGE, "state_l1"),
array(RDC_SDB_WC_ECLAIRAGE, "state_l2"), array(RDC_SDB_WC_ECLAIRAGE, "state_l2"),
array(RDC_CHAMBRE_AMBIANCE, "state"), array(RDC_CHAMBRE_AMBIANCE, "state"),
array(RDC_CHAMBRE_ECLAIRAGE, "state_l1") array(RDC_CHAMBRE_ECLAIRAGE, "state_l1"),
array(METEO, "tempc"),
array(METEO, "humidity"),
array(METEO, "winddir"),
array(METEO, "windspeedkmh"),
array(METEO, "windgustkmh"),
array(METEO, "rainin"),
array(METEO, "barominmb"),
array(METEO, "UV"),
array(METEO, "solarradiation")
); );
?> ?>

View File

@ -21,13 +21,13 @@ $properties2log = array(
"illuminance_lux" => function($value) {($value < 500?50:$value*10/100); return $value;}, "illuminance_lux" => function($value) {($value < 500?50:$value*10/100); return $value;},
// "illuminance" => 8, // "illuminance" => 8,
"requested_brightness_level" => function($value) {($value < 500?50:$value*10/100);return $value;}, "requested_brightness_level" => function($value) {($value < 500?50:$value*10/100);return $value;},
"tempf" => 0.5, "tempc" => 0.5,
"humidity" => null, "humidity" => null,
"dewptf" => 0.5, "dewptc" => 0.5,
"windchillf" => 0.5, "windchillc" => 0.5,
"winddir" => 40, "winddir" => 40,
"windspeedmph" => 5, "windspeedkmh" => 5,
"windgustmph" => 5, "windgustkmh" => 5,
"rainin" => null, "rainin" => null,
//"dailyrainin" => null, //"dailyrainin" => null,
//"weeklyrainin" => null, //"weeklyrainin" => null,
@ -35,7 +35,7 @@ $properties2log = array(
//"yearlyrainin" => null, //"yearlyrainin" => null,
"solarradiation" => 10, "solarradiation" => 10,
"UV" => null, "UV" => null,
"indoortempf" => 0.5, "indoortempc" => 0.5,
"indoorhumidity" => null, "indoorhumidity" => null,
"baromin" => 10 "baromin" => 10
); );

View File

@ -256,4 +256,23 @@ function changeValue(&$property, $value, &$parentDevice, $propertyTree, $key)
} }
} }
function mkIndexes($devices)
{
global $indexDevices, $indexFriendlyNames;
logger(INFO, _("function mkIndexes"), __FILE__ . ":" . __LINE__);
foreach ($devices as $key => $object)
{
//print "======####" . $key . EOL;
if (is_a($object, "device"))
{
//echo "==========>" . $object->friendlyName . EOL;
$indexDevices[$object->ieeeAddress] = &$object;
$indexFriendlyNames[$object->friendlyName] = &$object;
}else
{
mkIndexes($object);
}
}
}
?> ?>

View File

@ -44,7 +44,7 @@ class rdc_chambre_eclairage extends hook
private function send($deviceObject, $property, $state, $delayState = false, $method = MANUAL) private function send($deviceObject, $property, $state, $delayState = false, $method = MANUAL)
{ {
global $devices, $indexDevices; global $indexDevices;
$msg = array($property => $state); $msg = array($property => $state);
if ($deviceObject->properties[$property]["value"] != $state) if ($deviceObject->properties[$property]["value"] != $state)
{ {

View File

@ -1,20 +1,20 @@
<?php <?php
class rdc_chambre_eclairage extends hook class rdc_store extends hook
{ {
public $hookName = "rdc_store"; public $hookName = "rdc_store";
public $active = true; //enable/disable hook (true => enabled) public $active = true; //enable/disable hook (true => enabled)
public $timer = 0; public $timer = 0;
public $rafales = 0;
public $luminosite = 0;
public $luminosite_ext = 0;
public $luminance_min = 60; public $luminance_min = 60;
public $luminance_max = 3000; public $luminance_max = 3000;
public $storeDownTime = 20;
public $storeUpTime = 25;
protected $storeLevel;
protected $devicelist = array( protected $devicelist = array(
RDC_EXTERIEUR_LUMINOSITE => array("illuminance_lux", false), // "ON"/"OFF" RDC_EXTERIEUR_LUMINOSITE => array("illuminance_lux", false), // "ON"/"OFF"
METEO => array("rainin", false), METEO => array("rainin", false),
METEO => array("solarradiation", false), //METEO => array("solarradiation", false),
METEO => array("windspeedkmh", false), METEO => array("windspeedkmh", false),
METEO => array("windgustkmh", false) METEO => array("windgustkmh", false)
); );
@ -25,114 +25,102 @@ class rdc_chambre_eclairage extends hook
// $value -> value of the property // $value -> value of the property
public function callBack($device, $property, $value) public function callBack($device, $property, $value)
{ {
global $devices, $indexDevices; global $mohaDB, $devices, $indexDevices;
/* $storeDevice = $indexDevices[RDC_STORE];
nowf = now.month*100+now.day $storeLevel = $storeDevice->properties["position"];
if ( nowf > 501 and nowf< 1015 ) then $exterieur_lux = $mohaDB->moyenne($indexDevices[RDC_EXTERIEUR_LUMINOSITE], "illuminance_lux", 15);
storeLevel = dz.devices(398).level $rain = $indexDevices[METEO]->properties["rainin"];
print("storelevel => " .. storeLevel) $rafale = $mohaDB->moyenne($indexDevices[METEO], "windgustkmh", 12);
if (dz.data.timer ~= 0) then $soleil = $mohaDB->moyenne($indexDevices[METEO], "solarradiation", 12);
timer = dz.time.compare(dz.data.timer).mins if ($rafale >= 6 or $rain != 0)
if ( timer > 5) then {
dz.data.timer = 0 $maxLevel = 0;
end }else
print ("timer =" .. timer) {
end $maxLevel = 100 / $rafale;
exterieur_lux = dz.devices(113).nValue }
storeId = 398 switch ($property)
--print(inspect({dz.data})) {
store = dz.devices(storeId) case "illuminance_lux":
pluie = dz.devices(108).rainRate if ($exterieur_lux > 3000)
dz.data.rafales.add( tonumber(dz.devices(116).sValue) ) {
rafale = dz.data.rafales.avg() if ($rain == 0 and $maxLevel != 0)
luminosite_inst = dz.devices(322).nValue {
dz.data.luminosite.add( luminosite_inst ) $store2level = storeLevel + 15;
luminosite = dz.data.luminosite.avg() if ( $store2level > $maxLevel )
soleil = dz.devices(113).nValue {
rndRafale = math.floor(rafale/2) $this->open($maxLevel);
print ("luminosité " .. luminosite) }else
print ("rafale = " .. rafale .." => rndrafale =" .. rndRafale) {
--print ("soleil".. soleil) $this->open($max2level);
if (rndRafale >6) then }
maxLevel = 0 }
else }elseif ($exterieur_lux < 25000)
maxLevel = dz.data.stoLevelMax[rndRafale] {
end $this->close("Luminosité faible");
print( "pluie => " .. tostring(pluie)) }
print("maxLevel =>" .. maxLevel) break;
datetime = Time() case "rainin":
$this->close("Pluie");
----------------- fonctions ----------------------------- break;
case "windgustkmh";
open = function( storeId, level) case "windspeedkmh":
--print(inspect({dz.devices(storeId)})) if($value > 50)
print (storeLevel,level) {
if ( storeLevel < level ) then $this->close("Trop de vent");
dz.data.timer = Time() }elseif ($value > 40 and $level > 20)
dz.devices(storeId).dimTo(level) {
--dz.data.storeLevel = level $this->maxLevel = 20;
print ( "store to =>" .. tostring(level) ) $this->send(20);
dz.notify("Store","Store à " .. tostring(level) .. "%") }elseif ($value > 30 and $level > 40)
end {
end $this->maxLevel = 40;
$this->send(40);
close = function( storeId, raison ) }elseif ($value >20 and $level > 60)
if ( dz.devices(storeId).level ~= 0 ) then -- and not dz.devices(storeId).active {
dz.devices(storeId).open() $this->maxLevel = 60;
dz.data.storeLevel = 0 $this->send(60);
print ( "store fermé " .. raison) }elseif ($value >10 and $level > 80)
dz.notify("Store","Store fermé car " .. raison) {
end $this->maxLevel = 80;
end $this->send(80);
}else
wait = function(time) {
if tonumber(time) ~= nil then $this->maxLevel = 100;
os.execute("sleep "..tonumber(time)) }
else $this->close("Fortes rafales de vent");
os.execute("sleep "..tonumber("0.1")) break;
end }
end }
-------------------------------------- programme principal --------------------------------------
if ( datetime.matchesRule("between sunrise and sunset" ) ) then
if ( luminosite > 30000) then
if ( pluie == 0 and maxLevel ~=0 ) then
store2Level = storeLevel+15
if ( store2Level > maxLevel ) then
print( "level => " .. maxLevel)
open(storeId, maxLevel)
else --if (storeLevel ~= store2Level ) then
print( "level => " .. store2Level)
open( storeId, store2Level )
end
end
elseif ( storeLevel ~= 0 ) then
if (luminosite < 25000 and dz.data.timer == 0) then
print ("luminosité faible")
close( storeId, "luminosité faible" )
end
end
end
if (storeLevel ~= 0) then
-- pluie
if (pluie ~= 0) then
close(storeId, "il pleut")
-- rafale
elseif (rndRafale >= 7) then
close(storeId, "il y a des rafales de vent")
-- radiation solaires
elseif ( (soleil < 300 and luminosite < 30000) and dz.data.timer == 0 ) then
close(storeId , "il n'y a plus de soleil")
end
end
end
end
}*/
private function open ($level)
{
if ($this->storeLevel < $level)
{
$this->send($level);
}
}
private function close ($reason)
{
if ($this->storeLevel != 0)
{
print ( "store fermé " . $reason);
}
}
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();
$deviceObject->method = AUTO;
}
}
$hooks["rdc_store"] = new rdc_store();
?> ?>

View File

@ -198,9 +198,13 @@ logger(DEBUG, _("Loading stored devices datas from ") . $dataPath . "moha.db", _
if (loadDB($devices, $dataPath . "moha.db") === false) if (loadDB($devices, $dataPath . "moha.db") === false)
{ {
logger(ERROR, _("Can not load device db"), __FILE__ . ":" . __LINE__); logger(ERROR, _("Can not load device db"), __FILE__ . ":" . __LINE__);
}else
{
mkIndexes($devices);
} }
// topics definition // topics definition
listHooks("./topics_callbacks", $hooksList); listHooks("./topics_callbacks", $hooksList);
if (!empty($hooksList)) if (!empty($hooksList))

View File

@ -23,7 +23,6 @@ $topics["pws2mqtt"]->callback = function($topic, $message)
$device["device"]->topic = $topicName; $device["device"]->topic = $topicName;
$indexDevices[$device["device"]->ieeeAddress] = & $device["device"]; $indexDevices[$device["device"]->ieeeAddress] = & $device["device"];
$indexFriendlyNames[$topicName][$fn] = & $device["device"]; $indexFriendlyNames[$topicName][$fn] = & $device["device"];
}else }else
{ {
$device = &$device[$fn]; $device = &$device[$fn];

View File

@ -57,6 +57,7 @@ $topics["zigbee2mqtt"]->callback = function($topic, $message)
case "l1": case "l1":
case "l2": case "l2":
$fnTreeEnd = "state_" . $fnTreeEnd; $fnTreeEnd = "state_" . $fnTreeEnd;
case "set":
case "mode": case "mode":
case "contact": case "contact":
case "availability": case "availability":

View File

@ -1,40 +1,56 @@
<?php <?php
require_once "events.php"; require_once "events.php";
function webDashboard($socket, $n = 0, $page="/") function webDashboard($socket, $n="Général", $page="/")
{ {
global $dashboards, $indexDevices; global $dashboards, $indexDevices;
require_once "webserver/javascript.php";
logger(DEBUG, _("webDashboard function")); logger(DEBUG, _("webDashboard function"));
$response = ''; //<form action="action.php" method="get"> $response = insertJavascript();
if(array_key_exists($n, $dashboards)) if(array_key_exists($n, $dashboards))
{ {
foreach ($dashboards[$n] as $array) foreach ($dashboards[$n] as $array)
{ {
print_r($array); if (array_key_exists($array[0], $indexDevices))
$device = $indexDevices[$array[0]];
$property = $array[1];
$propertyObject = $device->properties[$property];
$value = $propertyObject["value"];
if ($value === null)
{ {
$value = "null"; $device = $indexDevices[$array[0]];
} $property = $array[1];
logger(DEBUG, $device->friendlyName . " => " . bool2string(_($value))); if (array_key_exists($array[1], $device->properties))
$response .= $device->friendlyName . aliases($device->friendlyName, $property) . " => " . bool2string(_($value)) . EOL; {
if (array_key_exists("access", $propertyObject))
$propertyObject = $device->properties[$property];
$value = $propertyObject["value"];
if ($value === null)
{
$value = "null";
}
logger(DEBUG, $device->friendlyName . " => " . bool2string(_($value)));
$response .= $device->friendlyName . aliases($device->friendlyName, $property) . " => " . bool2string(_($value)) . EOL;
if (array_key_exists("access", $propertyObject))
{
logger(DEBUG, _("Access = ") . $propertyObject["access"], __FILE__ . ":" . __LINE__);
if(($propertyObject["access"] & 2))
{
logger(DEBUG, _("Write Access OK ") . ($propertyObject["access"] & 2), __FILE__ . ":" . __LINE__);
$response .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . displayChoice($device, $property) . EOL;
}
if(($propertyObject["access"] & 4))
{
logger(DEBUG, _("can get value") . ($propertyObject["access"] & 4), __FILE__ . ":" . __LINE__);
$response .= '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" id="' . $device->topic ."/" . $device->friendlyName . "/" . $propertyObject["name"] . '" value="' . _("Update") . "\" onmouseup=\"getPropertyValue('" . $device->topic . "','" . $device->friendlyName . "','" . $propertyObject["name"] . "')\"><br>" . EOL;
}
}else
{
$response .= EOLH;
}
}else
{
logger(ERROR, _("Property do not exist"), __FILE__ . ":" . __LINE__);
}
}else
{ {
logger(DEBUG, _("Access = ") . $propertyObject["access"], __FILE__ . ":" . __LINE__); logger(ERROR, _("Device do not exist"), __FILE__ . ":" . __LINE__);
if(($propertyObject["access"] & 2))
{
logger(DEBUG, _("Write Access OK ") . ($propertyObject["access"] & 2), __FILE__ . ":" . __LINE__);
$response .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . displayChoice($device, $property) . EOL;
}
if(($propertyObject["access"] & 4))
{
logger(DEBUG, _("can get value") . ($propertyObject["access"] & 4), __FILE__ . ":" . __LINE__);
$response .= '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" id="' . $device->topic ."/" . $device->friendlyName . "/" . $propertyObject["name"] . '" value="' . _("Update") . "\" onmouseup=\"getPropertyValue('" . $device->topic . "','" . $propertyObject["name"] . "')\"><br>" . EOL;
}
} }
} }
}else }else
@ -79,7 +95,7 @@ function displayChoice($device, $propertyName)
{ {
$formHTML .= ' step="' . $propertyObject["value_step"] . '"'; $formHTML .= ' step="' . $propertyObject["value_step"] . '"';
} }
$formHTML .= " onchange=\"setPropertyValue('" . $device->topic . "', this.value, '" . $propertyObject["name"] . "')\">"; $formHTML .= " onchange=\"setPropertyValue('" . $device->topic . "', '" . $device->friendlyName . "', this.value, '" . $propertyObject["name"] . "')\">";
break; break;
case "enum": case "enum":
@ -98,7 +114,7 @@ function mkHTML($device, $propertyName, $choice)
logger(DEBUG, _("function mkHTML"), __FILE__ . ":" . __LINE__); logger(DEBUG, _("function mkHTML"), __FILE__ . ":" . __LINE__);
foreach ($choice as $key => $value) foreach ($choice as $key => $value)
{ {
$html .= '<input type="button" id="' . $device->topic ."/" . $device->friendlyName . "/" . $propertyName . "/" . $key . '" value="' . $value . "\" onmouseup=\"setPropertyValue('" . $device->topic . "', '" . $value . "', '" . $propertyName . "')\">"; $html .= '<input type="button" id="' . $device->topic ."/" . $device->friendlyName . "/" . $propertyName . "/" . $key . '" value="' . $value . "\" onmouseup=\"setPropertyValue('" . $device->topic . "', '" . $device->friendlyName . "', '" . $value . "', '" . $propertyName . "')\">";
} }
return $html; return $html;
} }
@ -147,29 +163,8 @@ function webBrowse($socket, $argList, $page="/browse")
if (is_a($device, "device")) if (is_a($device, "device"))
{ {
$response .= '<script language="javascript"> require_once "webserver/javascript.php";
<!-- $response .= insertJavascript();
function setPropertyValue(topic, value, property)
{
let xhr = new XMLHttpRequest();
xhr.open("GET", "http://192.168.1.253:' . $listenPort . '/set&fn=' . $device->friendlyName . '&property="+property+"&topic="+topic+"&value="+value);
xhr.onload = function () {};
xhr.send();
}
function getPropertyValue(topic,property)
{
let xhr = new XMLHttpRequest();
xhr.open("GET", "http://192.168.1.253:' . $listenPort . '/get&fn=' . $device->friendlyName . '&property="+property+"&topic="+topic );
xhr.onload = function () {};
xhr.send();
setTimeout(function()
{
location.reload();
}, 1000);
}
// -->
</script>';
foreach($device->properties as $key => $value) foreach($device->properties as $key => $value)
{ {
$response .= $key . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"; $response .= $key . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
@ -183,7 +178,7 @@ function webBrowse($socket, $argList, $page="/browse")
if(($value["access"] & 4)) if(($value["access"] & 4))
{ {
logger(DEBUG, _("can get value") . ($value["access"] & 4), __FILE__ . ":" . __LINE__); logger(DEBUG, _("can get value") . ($value["access"] & 4), __FILE__ . ":" . __LINE__);
$response .= '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" id="' . $device->topic ."/" . $device->friendlyName . "/" . $key . '" value="' . _("Update") . "\" onmouseup=\"getPropertyValue('" . $device->topic . "', '" . $key . "')\">"; $response .= '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" id="' . $device->topic ."/" . $device->friendlyName . "/" . $key . '" value="' . _("Update") . "\" onmouseup=\"getPropertyValue('" . $device->topic . "', '" . $device->friendlyName . "', '" . $key . "')\">";
} }
} }
$response .= "<br>\n" . $tab . "[<br>\n"; $response .= "<br>\n" . $tab . "[<br>\n";

29
webserver/javascript.php Normal file
View File

@ -0,0 +1,29 @@
<?php
function insertJavascript()
{
global $listenPort;
return '<script language="javascript">
<!--
function setPropertyValue(topic, fn, value, property)
{
let xhr = new XMLHttpRequest();
xhr.open("GET", "http://192.168.1.253:' . $listenPort . '/set&fn="+fn+"&property="+property+"&topic="+topic+"&value="+value);
xhr.onload = function () {};
xhr.send();
}
function getPropertyValue(topic, fn, property)
{
let xhr = new XMLHttpRequest();
xhr.open("GET", "http://192.168.1.253:' . $listenPort . '/get&fn="+fn+"&property="+property+"&topic="+topic );
xhr.onload = function () {};
xhr.send();
setTimeout(function()
{
location.reload();
}, 1000);
}
// -->
</script>';
}
?>

View File

@ -1,8 +0,0 @@
<script>
function sendRange(value, property)
{
let xhr = new XMLHttpRequest();
xhr.open("GET", "http://localhost:' . $listenPort . '/set&fn=' . $device->friendlyName . '&property="+property+"&topic=' . $device->topic . '&value=" + value);
xhr.send();
}
<script>