1
0

debugging

This commit is contained in:
2022-01-17 00:18:50 +01:00
parent cd20e973cd
commit 4fb5504cdc
18 changed files with 379 additions and 269 deletions

View File

@ -1,11 +1,18 @@
<?php
logger(DEBUG,"Including db_functions.php");
// to save or not to save the DB, that is the question ...
function storeDB($db, $filepath)
{
$data = serialize($db);
$fp = fopen($filePath, "w");
fwrite($fp, $data);
fclose($fp);
$data = serialize($db);
file_put_contents($filepath, $data);
}
function loadDB(& $db, $filepath)
{
$data = file_get_contents($filepath);
$db = unserialize($data);
}
function mkDevicesDB($topic, $json, $group=false)
@ -23,13 +30,13 @@ function mkDevicesDB($topic, $json, $group=false)
$device = & $devices[$topic];
foreach($fnTree as $fnPart)
{
if (!isset($device[$fnPart]))
if (!array_key_exists($fnPart, $device))
{
$device[$fnPart] = array();
}
$device = & $device[$fnPart];
}
if (!isset($device["device"]))
if (!array_key_exists("device", $device))
{
$device["device"] = new device;
}
@ -71,13 +78,8 @@ function addDevice(& $device, $fn, $jsonDevice )
}
searchPropertyValue($fn, $device["device"], $jsonDevice, $listProperties);
// adding callback function for availability
//print_r($hooks);
//$device["device"]->availability["functions"][] = $hooks["availability"]->getHook();
//indexing device
$indexDevices[$device["device"]->ieeeAddress] = & $device["device"];
//print_r($device);
}
@ -119,11 +121,10 @@ function searchPropertyValue($fn, &$device, $object, $listProperties)
function changeDevice($topic, $fn, &$device, $payloadArray)
{
//$fnTree = explode("/", $fn);
//print_r($payloadArray);
if (!empty($payloadArray))
{
iterateDevice($topic, $fn, $device, $payloadArray);
iterateDevice($topic, $fn, $device, $device, $payloadArray);
}else
{
logger(ERROR, _("payloadArray is empty!"));
@ -131,43 +132,75 @@ function changeDevice($topic, $fn, &$device, $payloadArray)
}
function iterateDevice($topic, $fn, &$device, $payloadArray)
function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $propertyTree="")
{
global $changed;
global $changed, $mohaDB;
$deviceType = (gettype($device) == "object"); // = true if object
//echo "devicetype = "; var_dump($deviceType); echo EOL;
//print_r($payloadArray);
//echo "device =>";print_r($device);echo EOL;
//echo "PropertyTree ==============> " . $propertyTree . EOL;
foreach($payloadArray as $key => $value)
{
$oldValue = null;
//echo "key =>"; print_r($key); echo EOL;
//echo "value =>"; print_r($value); echo EOL;
//echo "type : " . gettype($value) .EOL;
if (gettype($value) == "object")
$valueType = gettype($value);
if ( $valueType == "object")
{
$propertyTree .= $key . "/";
//echo " is Object" . EOL;
if (!property_exists($device, $key))
if ($deviceType === true )
{
$device->{$key} = null;
if (!property_exists($device, $key))
{
$device->{$key} = new stdClass;
}
//echo "iterating" . EOL;
iterateDevice($topic, $fn, $parentDevice, $device->$key, $value, $propertyTree);
}else
{
//echo "is array";
if (!array_key_exists($key, $device))
{
$device[$key] = new stdClass;
}
//echo "iterating" . EOL;
iterateDevice($topic, $fn, $parentDevice, $device[$key], $value, $propertyTree);
}
}elseif ($valueType == "array")
{
$propertyTree .= $key . "/";
if ($deviceType === true )
{
$device->$key = array();
iterateDevice($topic, $fn, $parentDevice, $device->$key, $value, $propertyTree);
}else
{
$device[$key] = array();
iterateDevice($topic, $fn, $parentDevice, $device[$key], $value, $propertyTree);
}
//echo "iterating" . EOL;
iterateDevice($topic, $fn, $device, $value);
}elseif (gettype($value) == "array")
{
//echo "is array" . EOL;
iterateDevice($topic, $fn, $device, $value);
}else
{
//echo "db_functions".EOL;
//($device);
if (empty($device->$key) or $value != null)
{
if (isset($device->$key))
if (property_exists($device, $key))
{
$oldValue = $device->$key;
if (is_array($device->$key))
{
$oldValue = $device->$key["value"];
}else
{
$oldValue = $device->$key;
}
}else
{
$device->{$key}["value"] = null;
$device->{$key} = array("value" => null);
$device->$key["functions"] = array();
}
if ($oldValue !== $value)
@ -175,8 +208,10 @@ function iterateDevice($topic, $fn, &$device, $payloadArray)
$device->$key["value"] = $value;
$changed[$fn]["key"] = $key;
$changed[$fn]["value"] = $value;
logger(INFO, sprintf(_("Device %s property %s, value changed to %s"), $device->friendlyName, $key, $value));
$mohaDB->logProperty($device, $key, $value);
logger(INFO, sprintf(_("Device %s property %s, value changed to %s"), $fn, $propertyTree . $key, $value));
//print_r($device);
//$mohaDB->logProperty($parentDevice, $propertyTree . $key, $value, $oldValue); TODO re-activate
//echo "oldvalue => " . print_r($oldValue, true) . EOL;
/*if (empty($oldValue))
{
@ -187,11 +222,9 @@ function iterateDevice($topic, $fn, &$device, $payloadArray)
}
echo " to " . $value . EOL;*/
}
//print_r($device->functions); print_r($value);
//print_r($device);
if (!empty($device->$key["functions"]))
{
echo "executing notifications functions " . EOL;
logger(DEBUG,_("executing notifications functions"));
foreach($device->$key["functions"] as $function)
{
//print_r($function);