From 9dde1c2d538b636238d093825a44e253bed148d1 Mon Sep 17 00:00:00 2001 From: daniel Tartavel Date: Sun, 6 Mar 2022 01:30:33 +0100 Subject: [PATCH] added some javascript to webserver\nsome debugging --- class/db.php | 10 ++ config/aliases.php | 5 + config/dashboard_conf.php | 13 +- config/properties2log.php | 12 +- db_functions.php | 19 +++ hooks/scripts/rdc_chambre_eclairage.php | 2 +- hooks/scripts/store.php | 210 +++++++++++------------- moha.php | 4 + topics_callbacks/pws2mqtt.php | 1 - topics_callbacks/zigbee2mqtt.php | 1 + webserver/cmd_functions.php | 95 +++++------ webserver/javascript.php | 29 ++++ webserver/sendrange.js | 8 - 13 files changed, 230 insertions(+), 179 deletions(-) create mode 100644 webserver/javascript.php delete mode 100644 webserver/sendrange.js diff --git a/class/db.php b/class/db.php index 0d641ad..d74fcb2 100644 --- a/class/db.php +++ b/class/db.php @@ -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(); diff --git a/config/aliases.php b/config/aliases.php index e1e5a61..e575759 100644 --- a/config/aliases.php +++ b/config/aliases.php @@ -7,7 +7,12 @@ function aliases($fn, $property) { logger(DEBUG, "exists " . $fn . "/" . $property, __FILE__ . ":" . __LINE__); return "/" . $aliases[$fn . "/" . $property]; + }else + { + return "/" . _($property); } + + } $aliases = array( diff --git a/config/dashboard_conf.php b/config/dashboard_conf.php index ef7db61..4d11f4f 100644 --- a/config/dashboard_conf.php +++ b/config/dashboard_conf.php @@ -1,9 +1,18 @@ diff --git a/config/properties2log.php b/config/properties2log.php index 9c238b6..ac823fb 100644 --- a/config/properties2log.php +++ b/config/properties2log.php @@ -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 ); diff --git a/db_functions.php b/db_functions.php index d2ba276..8a524ce 100644 --- a/db_functions.php +++ b/db_functions.php @@ -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); + } + } +} + ?> diff --git a/hooks/scripts/rdc_chambre_eclairage.php b/hooks/scripts/rdc_chambre_eclairage.php index 28ba84b..ec4cdd9 100644 --- a/hooks/scripts/rdc_chambre_eclairage.php +++ b/hooks/scripts/rdc_chambre_eclairage.php @@ -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) { diff --git a/hooks/scripts/store.php b/hooks/scripts/store.php index e797d42..ca4d658 100644 --- a/hooks/scripts/store.php +++ b/hooks/scripts/store.php @@ -1,20 +1,20 @@ 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(); ?> diff --git a/moha.php b/moha.php index 978a81a..e4b0997 100644 --- a/moha.php +++ b/moha.php @@ -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)) diff --git a/topics_callbacks/pws2mqtt.php b/topics_callbacks/pws2mqtt.php index dcb29a4..daffe8d 100644 --- a/topics_callbacks/pws2mqtt.php +++ b/topics_callbacks/pws2mqtt.php @@ -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]; diff --git a/topics_callbacks/zigbee2mqtt.php b/topics_callbacks/zigbee2mqtt.php index dc0886f..eb4de77 100644 --- a/topics_callbacks/zigbee2mqtt.php +++ b/topics_callbacks/zigbee2mqtt.php @@ -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": diff --git a/webserver/cmd_functions.php b/webserver/cmd_functions.php index d2c7ef4..bf93e0a 100644 --- a/webserver/cmd_functions.php +++ b/webserver/cmd_functions.php @@ -1,40 +1,56 @@ + $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 .= "     " . displayChoice($device, $property) . EOL; + } + if(($propertyObject["access"] & 4)) + { + logger(DEBUG, _("can get value") . ($propertyObject["access"] & 4), __FILE__ . ":" . __LINE__); + $response .= '     friendlyName . "/" . $propertyObject["name"] . '" value="' . _("Update") . "\" onmouseup=\"getPropertyValue('" . $device->topic . "','" . $device->friendlyName . "','" . $propertyObject["name"] . "')\">
" . 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 .= "     " . displayChoice($device, $property) . EOL; - } - if(($propertyObject["access"] & 4)) - { - logger(DEBUG, _("can get value") . ($propertyObject["access"] & 4), __FILE__ . ":" . __LINE__); - $response .= '     friendlyName . "/" . $propertyObject["name"] . '" value="' . _("Update") . "\" onmouseup=\"getPropertyValue('" . $device->topic . "','" . $propertyObject["name"] . "')\">
" . 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 .= 'friendlyName . "/" . $propertyName . "/" . $key . '" value="' . $value . "\" onmouseup=\"setPropertyValue('" . $device->topic . "', '" . $value . "', '" . $propertyName . "')\">"; + $html .= '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 .= ''; + require_once "webserver/javascript.php"; + $response .= insertJavascript(); foreach($device->properties as $key => $value) { $response .= $key . "     "; @@ -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 .= '     friendlyName . "/" . $key . '" value="' . _("Update") . "\" onmouseup=\"getPropertyValue('" . $device->topic . "', '" . $key . "')\">"; + $response .= '     friendlyName . "/" . $key . '" value="' . _("Update") . "\" onmouseup=\"getPropertyValue('" . $device->topic . "', '" . $device->friendlyName . "', '" . $key . "')\">"; } } $response .= "
\n" . $tab . "[
\n"; diff --git a/webserver/javascript.php b/webserver/javascript.php new file mode 100644 index 0000000..64c93f8 --- /dev/null +++ b/webserver/javascript.php @@ -0,0 +1,29 @@ + + + '; +} +?> diff --git a/webserver/sendrange.js b/webserver/sendrange.js deleted file mode 100644 index 6c1a58f..0000000 --- a/webserver/sendrange.js +++ /dev/null @@ -1,8 +0,0 @@ -