2022-02-23 10:23:16 +01:00
|
|
|
<?php
|
2022-03-04 22:30:16 +01:00
|
|
|
require_once "events.php";
|
2022-02-23 10:23:16 +01:00
|
|
|
|
2022-04-23 02:00:52 +02:00
|
|
|
function apiDashboard($socket, $n="Général")
|
2022-02-23 10:23:16 +01:00
|
|
|
{
|
|
|
|
global $dashboards, $indexDevices;
|
2022-04-07 01:44:17 +02:00
|
|
|
require_once "apiserver/javascript.php";
|
2022-05-05 21:28:26 +02:00
|
|
|
logger(DEBUG, _("apiDashboard function"), __FILE__ . ":" . __LINE__);
|
2022-03-06 01:30:33 +01:00
|
|
|
$response = insertJavascript();
|
2022-03-04 22:30:16 +01:00
|
|
|
|
2022-02-23 10:23:16 +01:00
|
|
|
if(array_key_exists($n, $dashboards))
|
|
|
|
{
|
2022-03-04 22:30:16 +01:00
|
|
|
foreach ($dashboards[$n] as $array)
|
2022-02-23 10:23:16 +01:00
|
|
|
{
|
2022-03-06 01:30:33 +01:00
|
|
|
if (array_key_exists($array[0], $indexDevices))
|
2022-02-23 10:23:16 +01:00
|
|
|
{
|
2022-03-06 01:30:33 +01:00
|
|
|
$device = $indexDevices[$array[0]];
|
|
|
|
$property = $array[1];
|
|
|
|
if (array_key_exists($array[1], $device->properties))
|
2022-03-04 22:30:16 +01:00
|
|
|
{
|
2022-03-06 01:30:33 +01:00
|
|
|
$propertyObject = $device->properties[$property];
|
|
|
|
$value = $propertyObject["value"];
|
2022-04-23 02:00:52 +02:00
|
|
|
logger(DEBUG, $device->friendlyName . " => " . bool2string(_($value)), __FILE__ . ":" . __LINE__);
|
2022-03-28 00:40:34 +02:00
|
|
|
$response .= $device->friendlyName . aliases($device->friendlyName, $property) . ' => ';
|
2022-03-06 01:30:33 +01:00
|
|
|
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__);
|
2022-03-28 00:40:34 +02:00
|
|
|
$response .= " " . displayChoice($device, $property, $value);
|
2022-06-21 08:57:58 +02:00
|
|
|
$response .= " " . displayMethod($device, $property);
|
2022-03-06 01:30:33 +01:00
|
|
|
}
|
|
|
|
if(($propertyObject["access"] & 4))
|
|
|
|
{
|
|
|
|
logger(DEBUG, _("can get value") . ($propertyObject["access"] & 4), __FILE__ . ":" . __LINE__);
|
2022-03-28 00:40:34 +02:00
|
|
|
$response .= '<input type="button" id="' . $device->topic ."/" . $device->friendlyName . "/" . $propertyObject["name"] . '" value="' . _("Update") . "\" onmouseup=\"getPropertyValue('" . $device->topic . "','" . $device->friendlyName . "','" . $propertyObject["name"] . "')\">";
|
2022-04-07 01:44:17 +02:00
|
|
|
}else
|
|
|
|
{
|
|
|
|
$response .= $value;
|
2022-03-28 00:40:34 +02:00
|
|
|
}
|
|
|
|
$response .= EOLH;
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
$response .= bool2string(_($value));
|
|
|
|
if(array_key_exists("unit", $propertyObject))
|
|
|
|
{
|
|
|
|
$response .= $propertyObject["unit"];
|
|
|
|
logger(DEBUG, _("unit = ") . $unit, __FILE__ . ":" . __LINE__);
|
2022-03-06 01:30:33 +01:00
|
|
|
}
|
2022-03-13 22:33:26 +01:00
|
|
|
$response .= EOLH;
|
2022-03-28 00:40:34 +02:00
|
|
|
}
|
|
|
|
|
2022-03-06 01:30:33 +01:00
|
|
|
}else
|
2022-03-04 22:30:16 +01:00
|
|
|
{
|
2022-03-28 00:40:34 +02:00
|
|
|
logger(ERROR, _("Property does not exists"), __FILE__ . ":" . __LINE__);
|
2022-03-04 22:30:16 +01:00
|
|
|
}
|
2022-03-06 01:30:33 +01:00
|
|
|
}else
|
|
|
|
{
|
2022-03-28 00:40:34 +02:00
|
|
|
logger(ERROR, _("Device does not exists"), __FILE__ . ":" . __LINE__);
|
2022-02-23 10:23:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}else
|
|
|
|
{
|
2022-03-28 00:40:34 +02:00
|
|
|
logger(ERROR, _("Dashboard does not exists"), __FILE__ . ":" . __LINE__);
|
2022-02-23 10:23:16 +01:00
|
|
|
$response = _("dashboard not found");
|
|
|
|
}
|
2022-03-28 00:40:34 +02:00
|
|
|
htmlSend($socket, $response, '<meta http-equiv="refresh" content="10">');
|
2022-02-23 10:23:16 +01:00
|
|
|
}
|
|
|
|
|
2022-03-28 00:40:34 +02:00
|
|
|
function displayChoice($device, $propertyName, $value)
|
2022-03-04 22:30:16 +01:00
|
|
|
{
|
2022-05-05 21:28:26 +02:00
|
|
|
logger(DEBUG, _("function displayChoice"), __FILE__ . ":" . __LINE__);
|
2022-03-04 22:30:16 +01:00
|
|
|
$propertyObject = $device->properties[$propertyName];
|
2022-03-28 00:40:34 +02:00
|
|
|
$unit = '';
|
2022-03-04 22:30:16 +01:00
|
|
|
|
|
|
|
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__);
|
2022-03-28 00:40:34 +02:00
|
|
|
$formHTML = bool2string($value);
|
2022-03-04 22:30:16 +01:00
|
|
|
$choice["on"] = $propertyObject["value_on"];
|
|
|
|
$choice["off"] = $propertyObject["value_off"];
|
|
|
|
if (array_key_exists("toggle", $propertyObject))
|
|
|
|
{
|
|
|
|
$choice["toggle"] = $propertyObject["toggle"];
|
|
|
|
}
|
2022-03-28 00:40:34 +02:00
|
|
|
$formHTML .= mkHTML($device, $propertyName, $choice);
|
2022-03-04 22:30:16 +01:00
|
|
|
break;
|
|
|
|
case "numeric":
|
2022-03-28 00:40:34 +02:00
|
|
|
logger(DEBUG, _("type is numeric") . "#" . $value . "#", __FILE__ . ":" . __LINE__);
|
|
|
|
$step = '';
|
|
|
|
if (array_key_exists("value_step", $propertyObject))
|
|
|
|
{
|
|
|
|
$step = ' step="' . $propertyObject["value_step"] . '"';
|
|
|
|
}
|
|
|
|
//$formHTML .= $device->friendlyName . aliases($device->friendlyName, $property) . ' => ';
|
|
|
|
$formHTML = '<input type="number" id="' . $device->topic . "/"
|
|
|
|
. $device->friendlyName . "/"
|
2022-04-07 01:44:17 +02:00
|
|
|
. $propertyObject["name"] . '"'
|
2022-03-28 00:40:34 +02:00
|
|
|
. ' min="' . $propertyObject["value_min"] . '"'
|
|
|
|
. ' max="' . $propertyObject["value_max"] . '"'
|
|
|
|
. $step
|
|
|
|
. ' value="' . $value . '">' . $unit . EOL;
|
|
|
|
$formHTML .= '<input type="range" id="' . $device->topic . "/" . $device->friendlyName . "/" . $propertyObject["name"] . '"';
|
2022-03-04 22:30:16 +01:00
|
|
|
$formHTML .= ' name="' . $propertyObject["name"] . '"';
|
|
|
|
$formHTML .= ' min="' . $propertyObject["value_min"] . '"';
|
|
|
|
$formHTML .= ' max="' . $propertyObject["value_max"] . '"';
|
|
|
|
$formHTML .= ' value="' . $propertyObject["value"] . '"';
|
2022-03-28 00:40:34 +02:00
|
|
|
$formHTML .= $step;
|
2022-04-07 01:44:17 +02:00
|
|
|
$formHTML .= " oninput=\"setPropertyNumberValue('" . $device->topic . "', '" . $device->friendlyName . "', this.value, '" . $propertyObject["name"] . "')\">";
|
2022-03-04 22:30:16 +01:00
|
|
|
break;
|
|
|
|
case "enum":
|
|
|
|
logger(DEBUG, _("type is enum"), __FILE__ . ":" . __LINE__);
|
2022-03-28 00:40:34 +02:00
|
|
|
$formHTML = bool2string($value);
|
2022-03-04 22:30:16 +01:00
|
|
|
$choice = $propertyObject["values"];
|
2022-03-28 00:40:34 +02:00
|
|
|
$formHTML .= mkHTML($device, $propertyName, $choice);
|
2022-03-04 22:30:16 +01:00
|
|
|
break;
|
2022-03-28 00:40:34 +02:00
|
|
|
default:
|
|
|
|
logger(DEBUG, "type of property not recognized:" . $propertyObject["type"] , __FILE__ . ":" . __LINE__);
|
2022-03-04 22:30:16 +01:00
|
|
|
}
|
2022-06-21 08:57:58 +02:00
|
|
|
|
2022-03-04 22:30:16 +01:00
|
|
|
return $formHTML;
|
|
|
|
}
|
|
|
|
|
2022-07-07 22:46:05 +02:00
|
|
|
function displayMethod($device, $propertyName)
|
2022-06-21 08:57:58 +02:00
|
|
|
{
|
2022-07-07 22:46:05 +02:00
|
|
|
$html = '<input type="button" id="' . $device->topic ."/" . $device->friendlyName . "/" . $propertyName . '" method="' . IDLE . "\" onmouseup=\"setPropertyMethod('" . $device->topic . "', '" . $device->friendlyName . "', '" . IDLE . "', '" . $propertyName . "')\">";
|
|
|
|
$html = '<input type="button" id="' . $device->topic ."/" . $device->friendlyName . "/" . $propertyName . "/" . '" method="' . AUTO . "\" onmouseup=\"setPropertyMethod('" . $device->topic . "', '" . $device->friendlyName . "', '" . AUTO . "', '" . $propertyName . "')\">";
|
|
|
|
$html = '<input type="button" id="' . $device->topic ."/" . $device->friendlyName . "/" . $propertyName . "/" . '" method="' . MANUAL . "\" onmouseup=\"setPropertyMethod('" . $device->topic . "', '" . $device->friendlyName . "', '" . MANUAL . "', '" . $propertyName . "')\">";
|
2022-06-21 08:57:58 +02:00
|
|
|
}
|
2022-03-04 22:30:16 +01:00
|
|
|
function mkHTML($device, $propertyName, $choice)
|
|
|
|
{
|
|
|
|
$html = "";
|
2022-05-05 21:28:26 +02:00
|
|
|
logger(DEBUG, "function mkHTML", __FILE__ . ":" . __LINE__);
|
2022-03-04 22:30:16 +01:00
|
|
|
foreach ($choice as $key => $value)
|
|
|
|
{
|
2022-04-07 01:44:17 +02:00
|
|
|
$html .= '<input type="button" id="' . $device->topic ."/" . $device->friendlyName . "/" . $propertyName . "/" . strtolower($value) . '" value="' . $value . "\" onmouseup=\"setPropertyValue('" . $device->topic . "', '" . $device->friendlyName . "', '" . $value . "', '" . $propertyName . "')\">";
|
2022-03-04 22:30:16 +01:00
|
|
|
}
|
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
2022-04-23 02:00:52 +02:00
|
|
|
function apiBrowse($socket, $argList, $page="/browse")
|
2022-02-23 10:23:16 +01:00
|
|
|
{
|
2022-03-04 22:30:16 +01:00
|
|
|
global $topics, $devices, $listenPort,$indexDevices;
|
2022-04-23 02:00:52 +02:00
|
|
|
logger(INFO, _("Generic response to choose device and property"), __FILE__ . ":" . __LINE__);
|
2022-02-23 10:23:16 +01:00
|
|
|
//$response = "<html><header></header><body>" . _("unknown command") . "</body></html>";
|
|
|
|
$response = "";
|
|
|
|
$flag = false;
|
|
|
|
$tab = "";
|
2022-03-04 22:30:16 +01:00
|
|
|
|
2022-02-23 10:23:16 +01:00
|
|
|
if (array_key_exists("topic", $argList))
|
|
|
|
{
|
|
|
|
if (array_key_exists($argList["topic"], $topics))
|
|
|
|
{
|
2022-03-04 22:30:16 +01:00
|
|
|
logger(DEBUG, _("Topic exists: ") . $argList["topic"] , __FILE__ . ":" . __LINE__);
|
|
|
|
$topicRef = '<a href="/browse&topic=' . htmlentities($argList["topic"]);
|
2022-02-23 10:23:16 +01:00
|
|
|
if (array_key_exists("fn", $argList))
|
|
|
|
{
|
|
|
|
logger(DEBUG, _("FriendlyName exists: ") . $argList["fn"] , __FILE__ . ":" . __LINE__);
|
|
|
|
$fn = "";
|
|
|
|
$fnArray = explode("/", $argList["fn"]);
|
|
|
|
$device = $devices[$argList["topic"]];
|
2022-03-04 22:30:16 +01:00
|
|
|
|
2022-02-23 10:23:16 +01:00
|
|
|
//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;
|
|
|
|
}
|
2022-03-04 22:30:16 +01:00
|
|
|
|
2022-02-23 10:23:16 +01:00
|
|
|
if (is_a($device, "device"))
|
|
|
|
{
|
2022-04-07 01:44:17 +02:00
|
|
|
require_once "apiserver/javascript.php";
|
2022-03-06 01:30:33 +01:00
|
|
|
$response .= insertJavascript();
|
2022-06-21 08:57:58 +02:00
|
|
|
|
2022-02-23 10:23:16 +01:00
|
|
|
foreach($device->properties as $key => $value)
|
|
|
|
{
|
2022-03-13 22:33:26 +01:00
|
|
|
$response = displayProperty($device, $key, $value, $response);
|
2022-02-23 10:23:16 +01:00
|
|
|
}
|
|
|
|
/*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";
|
|
|
|
}
|
|
|
|
}
|
2022-03-04 22:30:16 +01:00
|
|
|
//logger(DEBUG, _("response: ") . EOL . $response , __FILE__ . ":" . __LINE__);
|
2022-02-23 10:23:16 +01:00
|
|
|
//htmlSend($socket, $response);
|
|
|
|
}else
|
|
|
|
{
|
2022-03-04 22:30:16 +01:00
|
|
|
logger(DEBUG, _("no FriendlyName given"), __FILE__ . ":" . __LINE__);
|
|
|
|
if (empty($devices[$argList["topic"]]))
|
|
|
|
{
|
|
|
|
$response .= _("No devices yet found");
|
|
|
|
}else
|
2022-02-23 10:23:16 +01:00
|
|
|
{
|
2022-03-04 22:30:16 +01:00
|
|
|
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__);
|
|
|
|
}
|
2022-02-23 10:23:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}else
|
|
|
|
{
|
2022-04-23 02:00:52 +02:00
|
|
|
$response = apiListTopics();
|
2022-02-23 10:23:16 +01:00
|
|
|
}
|
|
|
|
}else
|
|
|
|
{
|
2022-04-23 02:00:52 +02:00
|
|
|
$response = apiListTopics();
|
2022-02-23 10:23:16 +01:00
|
|
|
}
|
|
|
|
htmlSend($socket, $response);
|
|
|
|
}
|
|
|
|
|
2022-03-13 22:33:26 +01:00
|
|
|
function displayProperty($device, $key, $value, $response)
|
|
|
|
{
|
2022-03-28 00:40:34 +02:00
|
|
|
$tab="";
|
2022-03-13 22:33:26 +01:00
|
|
|
$response .= $key . " ";
|
|
|
|
if (array_key_exists("access", $value))
|
|
|
|
{
|
|
|
|
if(($value["access"] & 2))
|
|
|
|
{
|
2022-03-28 00:40:34 +02:00
|
|
|
logger(DEBUG, _("Write Access OK: ") . ($value["access"] & 2) . "value:" . $value["value"], __FILE__ . ":" . __LINE__);
|
|
|
|
$response .= displayChoice($device, $key, $value["value"]);
|
2022-03-13 22:33:26 +01:00
|
|
|
}
|
|
|
|
if(($value["access"] & 4))
|
|
|
|
{
|
|
|
|
logger(DEBUG, _("can get value") . ($value["access"] & 4), __FILE__ . ":" . __LINE__);
|
|
|
|
$response .= ' <input type="button" id="' . $device->topic ."/" . $device->friendlyName . "/" . $key . '" value="' . _("Update") . "\" onmouseup=\"getPropertyValue('" . $device->topic . "', '" . $device->friendlyName . "', '" . $key . "')\">";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$response .= "<br>\n" . $tab . "[<br>\n";
|
|
|
|
//echo memory_get_usage();
|
|
|
|
//$response = value($key, $value, $response);
|
|
|
|
iterateProperty($device, $key, $value, $response, $tab);
|
|
|
|
$response .= $tab . "]<br>\n";
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2022-03-04 22:30:16 +01:00
|
|
|
function iterateProperty($device, $property, $value, &$response, $tab="")
|
2022-02-23 10:23:16 +01:00
|
|
|
{
|
2022-03-04 22:30:16 +01:00
|
|
|
$tab .= " ";
|
2022-02-23 10:23:16 +01:00
|
|
|
if (is_array($value) or is_object($value))
|
|
|
|
{
|
2022-03-04 22:30:16 +01:00
|
|
|
logger(DEBUG, _("is array"), __FILE__ . ":" . __LINE__ );
|
2022-02-23 10:23:16 +01:00
|
|
|
foreach($value as $key => $value2)
|
|
|
|
{
|
|
|
|
logger(DEBUG, $key, __FILE__ . ":" . __LINE__ );
|
2022-03-04 22:30:16 +01:00
|
|
|
$response .= $tab . $key;
|
|
|
|
if (is_array($value2) or is_object($value2))
|
|
|
|
{
|
|
|
|
$response .= "<br>\n";
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
$response .= " = ";
|
|
|
|
}
|
|
|
|
iterateProperty($device, $key, $value2, $response, $tab);
|
2022-02-23 10:23:16 +01:00
|
|
|
}
|
|
|
|
}else
|
|
|
|
{
|
2022-03-04 22:30:16 +01:00
|
|
|
$response .= bool2string($value) . "<br>\n";//. displayChoice($device, $property); //value($property, $value, "");
|
2022-02-23 10:23:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-23 02:00:52 +02:00
|
|
|
function apiListTopics()
|
2022-02-23 10:23:16 +01:00
|
|
|
{
|
|
|
|
global $topics;
|
2022-04-23 02:00:52 +02:00
|
|
|
logger(INFO, _("apiListTopics function"), __FILE__ . ":" . __LINE__);
|
2022-02-23 10:23:16 +01:00
|
|
|
$response = "";
|
|
|
|
foreach ($topics as $name => $topic)
|
|
|
|
{
|
2022-03-04 22:30:16 +01:00
|
|
|
$response .= '<a href="/browse&topic=' . $name . '">' . $name ."</a><br>\n";
|
2022-02-23 10:23:16 +01:00
|
|
|
}
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2022-04-23 02:00:52 +02:00
|
|
|
function apiGet($argList)
|
2022-02-23 10:23:16 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
|
{
|
2022-03-28 00:40:34 +02:00
|
|
|
logger(DEBUG, _("Getting "), __FILE__ . ":" . __LINE__ );
|
2022-02-23 10:23:16 +01:00
|
|
|
$property = $argList["property"];
|
2022-03-28 00:40:34 +02:00
|
|
|
$response = "Sending GET command for: " . $argList["topic"] . $argList["fn"];
|
|
|
|
$payload = array($argList["property"] => "");
|
|
|
|
publish($argList["topic"] . "/" . $argList["fn"], $payload, "get");
|
2022-02-23 10:23:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2022-04-23 02:00:52 +02:00
|
|
|
function apiSet($argList)
|
2022-02-23 10:23:16 +01:00
|
|
|
{
|
2022-06-10 12:44:09 +02:00
|
|
|
global $indexFriendlyNames, $topics;
|
2022-02-23 10:23:16 +01:00
|
|
|
if(!array_key_exists("topic", $argList) or !array_key_exists("fn", $argList) or !array_key_exists("property", $argList) or !array_key_exists("value", $argList))
|
|
|
|
{
|
2022-03-04 22:30:16 +01:00
|
|
|
if (!array_key_exists($argList["topic"], $topics) )
|
|
|
|
{
|
|
|
|
$response = "SET: " . _("bad parameter passed: topic does not exists") . "<br>\n";
|
|
|
|
}
|
2022-06-10 12:44:09 +02:00
|
|
|
if (!array_key_exists($argList["fn"], $indexFriendlyNames))
|
2022-03-04 22:30:16 +01:00
|
|
|
{
|
|
|
|
$response = "SET: " . _("bad parameter passed: fn does not exists") . "<br>\n";
|
|
|
|
}
|
|
|
|
$response .= "SET: " . _("not all parameters passed, need topic, fn, property and value");
|
2022-02-23 10:23:16 +01:00
|
|
|
}else
|
|
|
|
{
|
|
|
|
$response = "setting property " . $argList["property"] . " of " . $argList["fn"] . " to value: " . $argList["value"];
|
2022-06-20 10:34:55 +02:00
|
|
|
$device = $indexFriendlyNames[$argList["fn"]];
|
|
|
|
$device->payload = array($argList["property"] => $argList["value"]);
|
|
|
|
$device->set($argList["property"], IDLE);
|
2022-08-12 10:41:55 +02:00
|
|
|
publish($argList["topic"] . "/" . $argList["fn"], $device->payload, "set");
|
2022-06-20 10:34:55 +02:00
|
|
|
//removeEvent($indexFriendlyNames($argList["fn"]), $argList["property"], "OFF");
|
2022-02-23 10:23:16 +01:00
|
|
|
}
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2022-04-23 02:00:52 +02:00
|
|
|
function apiPrint($argList, $command)
|
2022-02-23 10:23:16 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-04-23 02:00:52 +02:00
|
|
|
function apiNotify($argList)
|
2022-02-23 10:23:16 +01:00
|
|
|
{
|
|
|
|
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))
|
|
|
|
{
|
2022-03-04 22:30:16 +01:00
|
|
|
$response = _("Error: With 'notify' command, you need 5 parameters: topic, fn, property, condition, value");
|
2022-02-23 10:23:16 +01:00
|
|
|
}else
|
|
|
|
{
|
|
|
|
$response = _("notify command have been set");
|
|
|
|
$monitored[] = new watch($argList["topic"], $argList["fn"], $argList["property"], $argList["condition"], $argList["value"]);
|
|
|
|
}
|
2022-03-13 22:33:26 +01:00
|
|
|
return $response;
|
|
|
|
}
|
2022-04-23 02:00:52 +02:00
|
|
|
|
|
|
|
function apiVerbose($argList)
|
|
|
|
{
|
|
|
|
global $logLevel, $logLevels;
|
2022-05-05 21:28:26 +02:00
|
|
|
logger(DEBUG, _("apiVerbose function"), __FILE__ . ":" . __LINE__);
|
2022-04-23 02:00:52 +02:00
|
|
|
$response = "";
|
|
|
|
//print_r($argList);
|
|
|
|
foreach ($logLevels as $value => $level)
|
|
|
|
{
|
|
|
|
//print $level . " => " . $value . " ==>" . strtoupper(trim($argList["cmd"])) . EOL;
|
|
|
|
if (strtoupper(trim($argList["verbose"])) == $level)
|
|
|
|
{
|
|
|
|
print "change of " . $level . EOL;
|
|
|
|
$logLevel ^= $value;
|
|
|
|
}
|
|
|
|
if ($logLevel & $value)
|
|
|
|
{
|
|
|
|
$response .= $level . _(" is active") . EOLH;
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
$response .= $level . _(" is inactive") . EOLH;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getFn($argList)
|
|
|
|
{
|
|
|
|
global $indexDevices;
|
|
|
|
$jsonArray = array();
|
|
|
|
logger(INFO, _("getFn function"), __FILE__ . ":" . __LINE__);
|
|
|
|
|
|
|
|
if (array_key_exists("ieeeAddress", $argList))
|
|
|
|
{
|
|
|
|
logger(DEBUG, "ieeeAddress = " . $argList["ieeeAddress"], __FILE__ . ":" . __LINE__);
|
|
|
|
if (array_key_exists($argList["ieeeAddress"], $indexDevices))
|
|
|
|
{
|
|
|
|
logger(DEBUG, "FriendlyName is " . $indexDevices[$argList["ieeeAddress"]]->friendlyName, __FILE__ . ":" . __LINE__);
|
|
|
|
$response = '["' . $argList["ieeeAddress"] . '":"' . $indexDevices[$argList["ieeeAddress"]]->friendlyName . '"]';
|
|
|
|
}
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
$flag = false;
|
|
|
|
//$response = "{";
|
|
|
|
foreach($indexDevices as $ieeeAddress => $device)
|
|
|
|
{
|
|
|
|
/*if ($flag == false)
|
|
|
|
{
|
|
|
|
$flag = true;
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
$response .= ', ';
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
//$response .= '"' . $ieeeAddress . '" : "' . $indexDevices[$ieeeAddress]->friendlyName . '"';
|
|
|
|
$jsonArray[$ieeeAddress] = $indexDevices[$ieeeAddress]->friendlyName;
|
|
|
|
array_multisort($jsonArray);
|
|
|
|
}
|
|
|
|
$response = json_encode($jsonArray, JSON_UNESCAPED_SLASHES);
|
|
|
|
print $response;
|
|
|
|
//print_r(json_decode($response));
|
|
|
|
}
|
|
|
|
logger(DEBUG, "response = " . $response, __FILE__ . ":" . __LINE__);
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
function PropertiesDashboard($argList)
|
|
|
|
{
|
|
|
|
global $indexProperties;
|
|
|
|
require_once "apiserver/javascript.php";
|
|
|
|
logger(INFO, _("propertiesDashboard function"), __FILE__ . ":" . __LINE__);
|
|
|
|
$response = insertJavascript();
|
|
|
|
$property = $argList["property"];
|
2022-08-12 10:41:55 +02:00
|
|
|
print_r($indexProperties[$property]);
|
|
|
|
foreach($indexProperties as $propertyName => $index)
|
2022-04-23 02:00:52 +02:00
|
|
|
{
|
2022-08-12 10:41:55 +02:00
|
|
|
if (str_contains($propertyName, $property ))
|
2022-04-23 02:00:52 +02:00
|
|
|
{
|
2022-08-12 10:41:55 +02:00
|
|
|
foreach($index as $device)
|
2022-04-23 02:00:52 +02:00
|
|
|
{
|
2022-08-12 10:41:55 +02:00
|
|
|
$propertyObject = $device->properties[$propertyName];
|
|
|
|
$value = $propertyObject["value"];
|
|
|
|
logger(DEBUG, $device->friendlyName . " => " . bool2string(_($value)), __FILE__ . ":" . __LINE__);
|
|
|
|
$response .= $device->friendlyName . aliases($device->friendlyName, $propertyName) . ' => ';
|
|
|
|
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, $propertyName, $value);
|
|
|
|
}
|
|
|
|
if(($propertyObject["access"] & 4))
|
|
|
|
{
|
|
|
|
logger(DEBUG, _("can get value") . ($propertyObject["access"] & 4), __FILE__ . ":" . __LINE__);
|
|
|
|
$response .= '<input type="button" id="' . $device->topic ."/" . $device->friendlyName . "/" . $propertyObject["name"] . '" value="' . _("Update") . "\" onmouseup=\"getPropertyValue('" . $device->topic . "','" . $device->friendlyName . "','" . $propertyObject["name"] . "')\">";
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
$response .= $value;
|
|
|
|
}
|
|
|
|
$response .= EOLH;
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
$response .= bool2string(_($value));
|
|
|
|
if(array_key_exists("unit", $propertyObject))
|
|
|
|
{
|
|
|
|
$response .= $propertyObject["unit"];
|
|
|
|
logger(DEBUG, _("unit = ") . $unit, __FILE__ . ":" . __LINE__);
|
|
|
|
}
|
|
|
|
$response .= EOLH;
|
|
|
|
}
|
2022-04-23 02:00:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-12 10:41:55 +02:00
|
|
|
return $response;
|
2022-04-23 02:00:52 +02:00
|
|
|
}
|
2022-06-13 19:23:22 +02:00
|
|
|
|
|
|
|
function apiHookActive($argList, $status)
|
|
|
|
{
|
|
|
|
global $hooks;
|
|
|
|
if (array_key_exists($argList["hook"], $hooks))
|
|
|
|
{
|
|
|
|
$hooks[$argList["hook"]]->active = $status;
|
|
|
|
if ($status)
|
|
|
|
{
|
|
|
|
return $argList["hook"] . _(" has been enabled");
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
return $argList["hook"] . _(" has been disabled");
|
|
|
|
}
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
return _("Hook does not exists");
|
|
|
|
}
|
2022-06-21 08:57:58 +02:00
|
|
|
}
|
2022-06-13 19:23:22 +02:00
|
|
|
|
2022-06-21 08:57:58 +02:00
|
|
|
function deleteDevice($argList)
|
|
|
|
{
|
|
|
|
global $indexFriendlyNames;
|
|
|
|
logger(DEBUG, _("removing ") . $indexFriendlyNames[$argList["fn"]]->friendlyname , __FILE__ . ":" . __LINE__);
|
|
|
|
//unset($indexFriendlyNames[$argList["fn"]]);
|
2022-06-13 19:23:22 +02:00
|
|
|
}
|
2022-03-13 22:33:26 +01:00
|
|
|
/*
|
2022-04-23 02:00:52 +02:00
|
|
|
function apiDisplayByType($argList)
|
2022-03-13 22:33:26 +01:00
|
|
|
{
|
|
|
|
global $indexTypes, $config;
|
|
|
|
require_once $config . "porpertiesbytype.php";
|
2022-04-07 01:44:17 +02:00
|
|
|
require_once "apiserver/javascript.php";
|
2022-03-13 22:33:26 +01:00
|
|
|
|
|
|
|
$response .= insertJavascript();
|
|
|
|
foreach($indexTypes[$argList["type"]] as $device)
|
|
|
|
{
|
|
|
|
foreach($)
|
|
|
|
displayProperty($device, $device->, $value, $response)
|
|
|
|
}
|
2022-02-23 10:23:16 +01:00
|
|
|
}
|
2022-03-13 22:33:26 +01:00
|
|
|
*/
|
2022-07-10 19:33:19 +02:00
|
|
|
|
|
|
|
function test()
|
|
|
|
{
|
|
|
|
global $hooks;
|
|
|
|
logger(DEBUG, _("Testing doors") , __FILE__ . ":" . __LINE__);
|
|
|
|
echo "testing doors";
|
|
|
|
return $hooks["alerte_intrusion"]->testPortes();
|
|
|
|
}
|
|
|
|
|
2022-02-23 10:23:16 +01:00
|
|
|
?>
|