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__);
}
}
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();

View File

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

View File

@ -1,9 +1,18 @@
<?php
$dashboards[0] = array(
$dashboards["Général"] = array(
array(RDC_SALON_ECLAIRAGE_PANNEAU, "state"),
array(RDC_SDB_WC_ECLAIRAGE, "state_l1"),
array(RDC_SDB_WC_ECLAIRAGE, "state_l2"),
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" => 8,
"requested_brightness_level" => function($value) {($value < 500?50:$value*10/100);return $value;},
"tempf" => 0.5,
"tempc" => 0.5,
"humidity" => null,
"dewptf" => 0.5,
"windchillf" => 0.5,
"dewptc" => 0.5,
"windchillc" => 0.5,
"winddir" => 40,
"windspeedmph" => 5,
"windgustmph" => 5,
"windspeedkmh" => 5,
"windgustkmh" => 5,
"rainin" => null,
//"dailyrainin" => null,
//"weeklyrainin" => null,
@ -35,7 +35,7 @@ $properties2log = array(
//"yearlyrainin" => null,
"solarradiation" => 10,
"UV" => null,
"indoortempf" => 0.5,
"indoortempc" => 0.5,
"indoorhumidity" => null,
"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)
{
global $devices, $indexDevices;
global $indexDevices;
$msg = array($property => $state);
if ($deviceObject->properties[$property]["value"] != $state)
{

View File

@ -1,20 +1,20 @@
<?php
class rdc_chambre_eclairage extends hook
class rdc_store extends hook
{
public $hookName = "rdc_store";
public $active = true; //enable/disable hook (true => enabled)
public $timer = 0;
public $rafales = 0;
public $luminosite = 0;
public $luminosite_ext = 0;
public $luminance_min = 60;
public $luminance_max = 3000;
public $storeDownTime = 20;
public $storeUpTime = 25;
protected $storeLevel;
protected $devicelist = array(
RDC_EXTERIEUR_LUMINOSITE => array("illuminance_lux", false), // "ON"/"OFF"
METEO => array("rainin", false),
METEO => array("solarradiation", false),
//METEO => array("solarradiation", false),
METEO => array("windspeedkmh", false),
METEO => array("windgustkmh", false)
);
@ -25,114 +25,102 @@ class rdc_chambre_eclairage extends hook
// $value -> value of the property
public function callBack($device, $property, $value)
{
global $devices, $indexDevices;
global $mohaDB, $devices, $indexDevices;
/*
nowf = now.month*100+now.day
if ( nowf > 501 and nowf< 1015 ) then
storeLevel = dz.devices(398).level
print("storelevel => " .. storeLevel)
if (dz.data.timer ~= 0) then
timer = dz.time.compare(dz.data.timer).mins
if ( timer > 5) then
dz.data.timer = 0
end
print ("timer =" .. timer)
end
exterieur_lux = dz.devices(113).nValue
storeId = 398
--print(inspect({dz.data}))
store = dz.devices(storeId)
pluie = dz.devices(108).rainRate
dz.data.rafales.add( tonumber(dz.devices(116).sValue) )
rafale = dz.data.rafales.avg()
luminosite_inst = dz.devices(322).nValue
dz.data.luminosite.add( luminosite_inst )
luminosite = dz.data.luminosite.avg()
soleil = dz.devices(113).nValue
rndRafale = math.floor(rafale/2)
print ("luminosité " .. luminosite)
print ("rafale = " .. rafale .." => rndrafale =" .. rndRafale)
--print ("soleil".. soleil)
if (rndRafale >6) then
maxLevel = 0
else
maxLevel = dz.data.stoLevelMax[rndRafale]
end
print( "pluie => " .. tostring(pluie))
print("maxLevel =>" .. maxLevel)
datetime = Time()
----------------- fonctions -----------------------------
open = function( storeId, level)
--print(inspect({dz.devices(storeId)}))
print (storeLevel,level)
if ( storeLevel < level ) then
dz.data.timer = Time()
dz.devices(storeId).dimTo(level)
--dz.data.storeLevel = level
print ( "store to =>" .. tostring(level) )
dz.notify("Store","Store à " .. tostring(level) .. "%")
end
end
close = function( storeId, raison )
if ( dz.devices(storeId).level ~= 0 ) then -- and not dz.devices(storeId).active
dz.devices(storeId).open()
dz.data.storeLevel = 0
print ( "store fermé " .. raison)
dz.notify("Store","Store fermé car " .. raison)
end
end
wait = function(time)
if tonumber(time) ~= nil then
os.execute("sleep "..tonumber(time))
else
os.execute("sleep "..tonumber("0.1"))
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
}*/
$storeDevice = $indexDevices[RDC_STORE];
$storeLevel = $storeDevice->properties["position"];
$exterieur_lux = $mohaDB->moyenne($indexDevices[RDC_EXTERIEUR_LUMINOSITE], "illuminance_lux", 15);
$rain = $indexDevices[METEO]->properties["rainin"];
$rafale = $mohaDB->moyenne($indexDevices[METEO], "windgustkmh", 12);
$soleil = $mohaDB->moyenne($indexDevices[METEO], "solarradiation", 12);
if ($rafale >= 6 or $rain != 0)
{
$maxLevel = 0;
}else
{
$maxLevel = 100 / $rafale;
}
switch ($property)
{
case "illuminance_lux":
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":
$this->close("Pluie");
break;
case "windgustkmh";
case "windspeedkmh":
if($value > 50)
{
$this->close("Trop de vent");
}elseif ($value > 40 and $level > 20)
{
$this->maxLevel = 20;
$this->send(20);
}elseif ($value > 30 and $level > 40)
{
$this->maxLevel = 40;
$this->send(40);
}elseif ($value >20 and $level > 60)
{
$this->maxLevel = 60;
$this->send(60);
}elseif ($value >10 and $level > 80)
{
$this->maxLevel = 80;
$this->send(80);
}else
{
$this->maxLevel = 100;
}
$this->close("Fortes rafales de vent");
break;
}
}
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)
{
logger(ERROR, _("Can not load device db"), __FILE__ . ":" . __LINE__);
}else
{
mkIndexes($devices);
}
// topics definition
listHooks("./topics_callbacks", $hooksList);
if (!empty($hooksList))

View File

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

View File

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

View File

@ -1,40 +1,56 @@
<?php
require_once "events.php";
function webDashboard($socket, $n = 0, $page="/")
function webDashboard($socket, $n="Général", $page="/")
{
global $dashboards, $indexDevices;
require_once "webserver/javascript.php";
logger(DEBUG, _("webDashboard function"));
$response = ''; //<form action="action.php" method="get">
$response = insertJavascript();
if(array_key_exists($n, $dashboards))
{
foreach ($dashboards[$n] as $array)
{
print_r($array);
$device = $indexDevices[$array[0]];
$property = $array[1];
$propertyObject = $device->properties[$property];
$value = $propertyObject["value"];
if ($value === null)
if (array_key_exists($array[0], $indexDevices))
{
$value = "null";
}
logger(DEBUG, $device->friendlyName . " => " . bool2string(_($value)));
$response .= $device->friendlyName . aliases($device->friendlyName, $property) . " => " . bool2string(_($value)) . EOL;
if (array_key_exists("access", $propertyObject))
$device = $indexDevices[$array[0]];
$property = $array[1];
if (array_key_exists($array[1], $device->properties))
{
$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__);
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;
}
logger(ERROR, _("Device do not exist"), __FILE__ . ":" . __LINE__);
}
}
}else
@ -79,7 +95,7 @@ function displayChoice($device, $propertyName)
{
$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;
case "enum":
@ -98,7 +114,7 @@ function mkHTML($device, $propertyName, $choice)
logger(DEBUG, _("function mkHTML"), __FILE__ . ":" . __LINE__);
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;
}
@ -147,29 +163,8 @@ function webBrowse($socket, $argList, $page="/browse")
if (is_a($device, "device"))
{
$response .= '<script language="javascript">
<!--
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>';
require_once "webserver/javascript.php";
$response .= insertJavascript();
foreach($device->properties as $key => $value)
{
$response .= $key . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
@ -183,7 +178,7 @@ function webBrowse($socket, $argList, $page="/browse")
if(($value["access"] & 4))
{
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";

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>