1
0
moha/webserver/cmd_functions.php

239 lines
6.6 KiB
PHP
Raw Normal View History

2022-02-23 10:23:16 +01:00
<?php
function webDashboard($socket, $n = 0)
{
global $dashboards, $indexDevices;
logger(DEBUG, _("webDashboard function"));
$response = "";
if(array_key_exists($n, $dashboards))
{
foreach ($dashboards[$n] as $ieeeAddress => $property)
{
$value = $indexDevices[$ieeeAddress]->properties[$property]["value"];
if ($value === null)
{
$value ="null";
}
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)
{
global $topics, $devices;
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"]);
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)
{
if ($flag) $fn .= "/";
$fn .= $value;
if (!array_key_exists($value, $device))
{
htmlSend($socket, sprintf(_('"%s" does not exists'), $value));
return false;
}
$device = $device[$value];
$flag = true;
}
//var_dump($device);
if (array_key_exists("device", $device)) // TODO
{
$device = $device["device"];
$fn .= "device/" . $fn;
}
if (is_a($device, "device"))
{
foreach($device->properties as $key => $value)
{
$response .= $key . "<br>";
$response .= $tab . "[<br>";
echo memory_get_usage();
//$response = value($key, $value, $response);
iterateProperty($key, $value, $response, $tab);
$response .= $tab . "]<br>";
}
/*foreach($device->properties as $key => $value)
{
$response = value($key, $value, $response);
}*/
}else
{
foreach($device as $key => $value)
{
$response .= $topicRef . htmlentities("&fn=" . $fn . "/" . $key) . '">' . $key . "</a><br>\n";
}
}
logger(DEBUG, _("response: ") . EOL . $response , __FILE__ . ":" . __LINE__);
//htmlSend($socket, $response);
}else
{
foreach($devices[$argList["topic"]] as $key => $value)
{
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__);
}
}
}else
{
$response = webListTopics();
}
}else
{
$response = webListTopics();
}
htmlSend($socket, $response);
}
function iterateProperty($property, $value, &$response, $tab="")
{
$tab .= "&nbsp;&nbsp;&nbsp;";
if (is_array($value) or is_object($value))
{
logger(DEBUG, _("is object or array"), __FILE__ . ":" . __LINE__ );
foreach($value as $key => $value2)
{
logger(DEBUG, $key, __FILE__ . ":" . __LINE__ );
iterateProperty($key, $value2, $response, $tab);
}
}else
{
$response .= $tab . $property . ' = ' . bool2string($value) . "<br>";//value($property, $value, "");
}
}
function webListTopics()
{
global $topics;
logger(DEBUG, _("webListTopics function"));
$response = "";
foreach ($topics as $name => $topic)
{
$response .= '<a href="/?browse&topic=' . $name . '">' . $name ."</a><br>";
}
return $response;
}
function webGet($argList)
{
if(!array_key_exists("topic", $argList) or !array_key_exists("fn", $argList) or !array_key_exists("property", $argList))
{
$response = "GET: " . _("no parameters passed, need topic, fn and property");
}else
{
if (($device = getDevice($argList["topic"], $argList["fn"])) === false)
{
$response = sprintf(_('"%s" or "%s" does not exists'), $argList["topic"], $argList["fn"]);
}else
{
$property = $argList["property"];
$response = "GET: " . bool2string($device->$property["value"]);
}
}
return $response;
}
function webSet($argList)
{
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";
}else
{
$response = "setting property " . $argList["property"] . " of " . $argList["fn"] . " to value: " . $argList["value"];
$payload = array($argList["property"] => $argList["value"]);
publish(Z2M . "/" . $argList["fn"], $payload);
}
return $response;
}
function webPrint($argList, $command)
{
global $GLOBALS, $topics;
if (array_key_exists($argList["object"], $GLOBALS))
{
$var = $GLOBALS[$argList["object"]];
if (isset($argList["topic"]))
{
$topic = $argList["topic"];
}
if (isset($argList["address"]))
{
$var = $var[$argList["address"]];
}elseif (isset($argList["fn"]))
{
if(!empty($topic))
{
$var = getDevice($topic, $argList["fn"]);
}else
{
$str = _("topic is not defining: add &topic=\nThese topics are availables: ");
foreach($topics as $key => $value)
{
$str .= $key . EOL;
}
logger(ERROR, $str, __FILE__ . ":" . __LINE__);
}
}
$error = error_get_last();
if($error !== null)
{
$response = $error["message"] . " file: " . $error["file"] . " line: " . $error["line"];
}
if ($command === "print")
{
$response = "<pre>" . nl2br(print_r($var, true)) . "<pre>";
}elseif($command === "dump")
{
$response = "Dump" . EOL;
$response .= "<pre>" . nl2br(var_export($var, true)) . "<pre>";
}
}else
{
$response = _("Object do not exists");
}
return $response;
}
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");
}else
{
$response = _("notify command have been set");
$monitored[] = new watch($argList["topic"], $argList["fn"], $argList["property"], $argList["condition"], $argList["value"]);
}
}
?>