1
0

- debug\n- webserver fonctionnel

This commit is contained in:
2022-03-04 22:30:16 +01:00
parent a29aa9b653
commit 9d10e51a24
17 changed files with 273 additions and 128 deletions

View File

@@ -1,50 +1,130 @@
<?php
require_once "events.php";
function webDashboard($socket, $n = 0)
function webDashboard($socket, $n = 0, $page="/")
{
global $dashboards, $indexDevices;
logger(DEBUG, _("webDashboard function"));
$response = "";
$response = ''; //<form action="action.php" method="get">
if(array_key_exists($n, $dashboards))
{
foreach ($dashboards[$n] as $ieeeAddress => $property)
foreach ($dashboards[$n] as $array)
{
$value = $indexDevices[$ieeeAddress]->properties[$property]["value"];
print_r($array);
$device = $indexDevices[$array[0]];
$property = $array[1];
$propertyObject = $device->properties[$property];
$value = $propertyObject["value"];
if ($value === null)
{
$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 . "','" . $propertyObject["name"] . "')\"><br>" . EOL;
}
}
logger(DEBUG, _($indexDevices[$ieeeAddress]->friendlyName . " => " . bool2string($value)));
$response .= $indexDevices[$ieeeAddress]->friendlyName . " => " . bool2string($value) . "<br>";
}
}else
{
$response = _("dashboard not found");
}
htmlSend($socket, $response);
}
function webBrowse($socket, $argList)
function displayChoice($device, $propertyName)
{
global $topics, $devices;
logger(INFO, _("function displayChoice"), __FILE__ . ":" . __LINE__);
$propertyObject = $device->properties[$propertyName];
if(array_key_exists("unit", $propertyObject))
{
$unit = $propertyObject["unit"];
logger(DEBUG, _("unit = ") . $unit, __FILE__ . ":" . __LINE__);
}
if (!array_key_exists("type", $propertyObject)) return "";
switch ($propertyObject["type"])
{
case "binary":
logger(DEBUG, _("type is binary"), __FILE__ . ":" . __LINE__);
$choice["on"] = $propertyObject["value_on"];
$choice["off"] = $propertyObject["value_off"];
if (array_key_exists("toggle", $propertyObject))
{
$choice["toggle"] = $propertyObject["toggle"];
}
$formHTML = mkHTML($device, $propertyName, $choice);
break;
case "numeric":
logger(DEBUG, _("type is numeric"), __FILE__ . ":" . __LINE__);
$formHTML = '<input type="range" id="' . $propertyObject["name"] . '"';
$formHTML .= ' name="' . $propertyObject["name"] . '"';
$formHTML .= ' min="' . $propertyObject["value_min"] . '"';
$formHTML .= ' max="' . $propertyObject["value_max"] . '"';
$formHTML .= ' value="' . $propertyObject["value"] . '"';
if (array_key_exists("value_step", $propertyObject))
{
$formHTML .= ' step="' . $propertyObject["value_step"] . '"';
}
$formHTML .= " onchange=\"setPropertyValue('" . $device->topic . "', this.value, '" . $propertyObject["name"] . "')\">";
break;
case "enum":
logger(DEBUG, _("type is enum"), __FILE__ . ":" . __LINE__);
$choice = $propertyObject["values"];
$formHTML = mkHTML($device, $propertyName, $choice);
break;
}
return $formHTML;
}
function mkHTML($device, $propertyName, $choice)
{
global $listenPort, $httpServerIp;
$html = "";
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 . "')\">";
}
return $html;
}
function webBrowse($socket, $argList, $page="/browse")
{
global $topics, $devices, $listenPort,$indexDevices;
logger(DEBUG, _("Generic response to choose device and property"), __FILE__ . ":" . __LINE__);
//$response = "<html><header></header><body>" . _("unknown command") . "</body></html>";
$response = "";
$flag = false;
$tab = "";
if (array_key_exists("topic", $argList))
{
if (array_key_exists($argList["topic"], $topics))
{
logger(DEBUG, _("Topic exists") , __FILE__ . ":" . __LINE__);
$topicRef = '<a href="/?browse&amp;topic=' . htmlentities($argList["topic"]);
logger(DEBUG, _("Topic exists: ") . $argList["topic"] , __FILE__ . ":" . __LINE__);
$topicRef = '<a href="/browse&amp;topic=' . htmlentities($argList["topic"]);
if (array_key_exists("fn", $argList))
{
logger(DEBUG, _("FriendlyName exists: ") . $argList["fn"] , __FILE__ . ":" . __LINE__);
$fn = "";
$fnArray = explode("/", $argList["fn"]);
$device = $devices[$argList["topic"]];
//var_dump($fnArray);
foreach($fnArray as $value)
{
@@ -64,16 +144,53 @@ function webBrowse($socket, $argList)
$device = $device["device"];
$fn .= "device/" . $fn;
}
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>';
foreach($device->properties as $key => $value)
{
$response .= $key . "<br>";
$response .= $tab . "[<br>";
echo memory_get_usage();
$response .= $key . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
if (array_key_exists("access", $value))
{
if(($value["access"] & 2))
{
logger(DEBUG, _("Write Access OK ") . ($value["access"] & 2), __FILE__ . ":" . __LINE__);
$response .= displayChoice($device, $key);
}
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 .= "<br>\n" . $tab . "[<br>\n";
//echo memory_get_usage();
//$response = value($key, $value, $response);
iterateProperty($key, $value, $response, $tab);
$response .= $tab . "]<br>";
iterateProperty($device, $key, $value, $response, $tab);
$response .= $tab . "]<br>\n";
}
/*foreach($device->properties as $key => $value)
{
@@ -86,17 +203,23 @@ function webBrowse($socket, $argList)
$response .= $topicRef . htmlentities("&fn=" . $fn . "/" . $key) . '">' . $key . "</a><br>\n";
}
}
logger(DEBUG, _("response: ") . EOL . $response , __FILE__ . ":" . __LINE__);
//logger(DEBUG, _("response: ") . EOL . $response , __FILE__ . ":" . __LINE__);
//htmlSend($socket, $response);
}else
{
foreach($devices[$argList["topic"]] as $key => $value)
logger(DEBUG, _("no FriendlyName given"), __FILE__ . ":" . __LINE__);
if (empty($devices[$argList["topic"]]))
{
print "key = " . print_r($key, true) . " value = " . print_r($value, true) . EOLR;
logger(DEBUG, _("devices de topic: ") . $key , __FILE__ . ":" . __LINE__);
$response .= $topicRef . htmlentities("&fn=" . $key) . '">' . $key . "</a><br>";
logger(DEBUG, _("response: ") . $response , __FILE__ . ":" . __LINE__);
$response .= _("No devices yet found");
}else
{
foreach($devices[$argList["topic"]] as $key => $value)
{
//print "key = " . print_r($key, true) . " value = " . print_r($value, true) . EOL;
logger(DEBUG, _("devices de topic: ") . $key , __FILE__ . ":" . __LINE__);
$response .= $topicRef . htmlentities("&fn=" . $key) . '">' . $key . "</a><br>\n";
logger(DEBUG, _("response: ") . $response , __FILE__ . ":" . __LINE__);
}
}
}
}else
@@ -110,22 +233,28 @@ function webBrowse($socket, $argList)
htmlSend($socket, $response);
}
function iterateProperty($property, $value, &$response, $tab="")
function iterateProperty($device, $property, $value, &$response, $tab="")
{
$tab .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
$tab .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
if (is_array($value) or is_object($value))
{
logger(DEBUG, _("is object or array"), __FILE__ . ":" . __LINE__ );
logger(DEBUG, _("is array"), __FILE__ . ":" . __LINE__ );
foreach($value as $key => $value2)
{
logger(DEBUG, $key, __FILE__ . ":" . __LINE__ );
iterateProperty($key, $value2, $response, $tab);
$response .= $tab . $key;
if (is_array($value2) or is_object($value2))
{
$response .= "<br>\n";
}else
{
$response .= " = ";
}
iterateProperty($device, $key, $value2, $response, $tab);
}
}else
{
$response .= $tab . $property . ' = ' . bool2string($value) . "<br>";//value($property, $value, "");
$response .= bool2string($value) . "<br>\n";//. displayChoice($device, $property); //value($property, $value, "");
}
}
@@ -136,7 +265,7 @@ function webListTopics()
$response = "";
foreach ($topics as $name => $topic)
{
$response .= '<a href="/?browse&topic=' . $name . '">' . $name ."</a><br>";
$response .= '<a href="/browse&topic=' . $name . '">' . $name ."</a><br>\n";
}
return $response;
}
@@ -162,15 +291,24 @@ function webGet($argList)
function webSet($argList)
{
global $indexFriendlyName;
if(!array_key_exists("topic", $argList) or !array_key_exists("fn", $argList) or !array_key_exists("property", $argList) or !array_key_exists("value", $argList))
{
$response = "SET: " . _("no parameters passed, need topic, fn, property and value") . "passed";
if (!array_key_exists($argList["topic"], $topics) )
{
$response = "SET: " . _("bad parameter passed: topic does not exists") . "<br>\n";
}
if (!array_key_exists(!array_key_exists($argList["fn"], $indexFriendlyName)))
{
$response = "SET: " . _("bad parameter passed: fn does not exists") . "<br>\n";
}
$response .= "SET: " . _("not all parameters passed, need topic, fn, property and value");
}else
{
$response = "setting property " . $argList["property"] . " of " . $argList["fn"] . " to value: " . $argList["value"];
$payload = array($argList["property"] => $argList["value"]);
publish(Z2M . "/" . $argList["fn"], $payload);
//removeEvent($indexFriendlyName($argList["fn"]), $argList["property"], "OFF");
}
return $response;
}
@@ -228,7 +366,7 @@ function webNotify($argList)
{
if (!array_key_exists("topic", $argList) or !array_key_exists("fn", $argList) or !array_key_exists("property", $argList) or !array_key_exists("condition", $argList) or !array_key_exists("value", $argList))
{
$response = _("Error: With 'notify' command, you need 4 parameters: topic, fn, property, condition, value");
$response = _("Error: With 'notify' command, you need 5 parameters: topic, fn, property, condition, value");
}else
{
$response = _("notify command have been set");