- indexFriendlyName modified\n- debugging
This commit is contained in:
162
db_functions.php
162
db_functions.php
@ -53,22 +53,22 @@ function mkDevicesDB($topic, $json, $group=false)
|
||||
//print_r($device);
|
||||
$device["device"]->groupID = $jsonDevice->id;
|
||||
$indexDevices[$device["device"]->groupID] = & $device["device"];
|
||||
$indexFriendlyNames[$fn] = & $device["device"];
|
||||
$indexFriendlyNames[$topic][$fn] = & $device["device"];
|
||||
}else
|
||||
{
|
||||
addDevice($device["device"], $fn, $jsonDevice);
|
||||
addDevice($topic, $device["device"], $fn, $jsonDevice);
|
||||
}
|
||||
}
|
||||
$dbInit += 1;
|
||||
fwrite($logFh, "################################START##################################################");
|
||||
fwrite($logFh, var_export($devices, true));
|
||||
fwrite($logFh, "################################END##################################################");
|
||||
|
||||
fwrite($logFh, var_export($indexFriendlyNames, true));
|
||||
logger(DEBUG, _("Devices DB made"), __FILE__ . ":" . __LINE__);
|
||||
//print_r($devices);
|
||||
}
|
||||
|
||||
function addDevice(& $device, $fn, $jsonDevice )
|
||||
function addDevice($topic, & $device, $fn, $jsonDevice )
|
||||
{
|
||||
global $listProperties, $listPropertiesKeys, $hooks, $indexDevices, $indexFriendlyNames;
|
||||
$device->type = $jsonDevice->type;
|
||||
@ -86,7 +86,7 @@ function addDevice(& $device, $fn, $jsonDevice )
|
||||
|
||||
//indexing device
|
||||
$indexDevices[$device->ieeeAddress] = & $device;
|
||||
$indexFriendlyNames[$fn] = & $device;
|
||||
$indexFriendlyNames[$topic][$fn] = & $device;
|
||||
}
|
||||
|
||||
function searchPropertyKey($fn, &$device, $inputObject, $listPropertiesKeys)
|
||||
@ -157,18 +157,18 @@ function changeDevice($topic, $fn, &$device, $payloadArray)
|
||||
{
|
||||
//echo "==================== New ChangeDevice =====================" .EOL;
|
||||
|
||||
iterateDevice($topic, $fn, $device, $device, $payloadArray);
|
||||
iterateDevice($topic, $fn, $device, $device->properties, $payloadArray);
|
||||
}else
|
||||
{
|
||||
logger(ERROR, $fn . _(" => payloadArray is empty!"), __FILE__ . ":" . __LINE__);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $propertyTree="")
|
||||
function iterateDevice($topic, $fn, $parentDevice, &$properties, $payloadArray, $propertyTree="")
|
||||
{
|
||||
global $changed, $mohaDB, $testMode;
|
||||
$deviceType = (is_a($device, "device")); // = true if object
|
||||
//if (is_a($device, "device")) // = true if object
|
||||
|
||||
//echo "==================== New iterate =====================" .EOL;
|
||||
//echo "deviceType = "; var_dump($deviceType); echo EOL;
|
||||
//echo "device =>";print_r($device);echo EOL;
|
||||
@ -180,123 +180,23 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro
|
||||
//echo "value =>"; var_dump($value); echo EOL;
|
||||
//echo "type : " . gettype($value) .EOL;
|
||||
$valueType = gettype($value);
|
||||
if ($valueType == "object")
|
||||
|
||||
if ($valueType == "array")
|
||||
{
|
||||
//logger(DEBUG, _("valueType == object"), __FILE__ . ":" . __LINE__);
|
||||
|
||||
logger(DEBUG,_("valueType == array"), __FILE__ . ":" . __LINE__);
|
||||
$propertyTree .= $key . "/";
|
||||
//echo "PropertyTree " . $propertyTree . EOL;
|
||||
//echo " is Object" . EOL;
|
||||
if ($deviceType === true )
|
||||
{
|
||||
//echo "deviceType = true" . EOL;
|
||||
if (!array_key_exists($key, $device->properties))
|
||||
{
|
||||
//echo "Property do not exists" . EOL;
|
||||
$device->properties[$key] = new stdClass;
|
||||
}
|
||||
//echo "iterating" . EOL;
|
||||
//echo "===============>";
|
||||
|
||||
iterateDevice($topic, $fn, $parentDevice, $device->properties[$key], $value, $propertyTree);
|
||||
}elseif(is_array($device))
|
||||
{
|
||||
//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(is_object($device))
|
||||
{
|
||||
if (!property_exists($device, $key))
|
||||
{
|
||||
$device->$key = new stdClass;
|
||||
}
|
||||
iterateDevice($topic, $fn, $parentDevice, $device->$key, $value, $propertyTree);
|
||||
}
|
||||
|
||||
}elseif ($valueType == "array")
|
||||
{
|
||||
//logger(DEBUG,_("valueType == array"), __FILE__ . ":" . __LINE__);
|
||||
$propertyTree .= $key . "/";
|
||||
if ($deviceType === true )
|
||||
{
|
||||
$device->properties[$key] = array();
|
||||
iterateDevice($topic, $fn, $parentDevice, $device->properties[$key], $value, $propertyTree);
|
||||
}elseif(is_array($device))
|
||||
{
|
||||
$device[$key] = array();
|
||||
iterateDevice($topic, $fn, $parentDevice, $device[$key], $value, $propertyTree);
|
||||
}elseif(is_object($device))
|
||||
{
|
||||
$device->$key = array();
|
||||
iterateDevice($topic, $fn, $parentDevice, $device->$key, $value, $propertyTree);
|
||||
}
|
||||
|
||||
$properties[$key] = array();
|
||||
iterateDevice($topic, $fn, $parentDevice, $properties[$key], $value, $propertyTree);
|
||||
}else
|
||||
{
|
||||
//logger(DEBUG,_("valueType is ") . $valueType, __FILE__ . ":" . __LINE__);
|
||||
|
||||
//var_dump($device);echo EOL;
|
||||
if (is_a($device, "device"))
|
||||
logger(DEBUG,_("valueType is ") . $valueType, __FILE__ . ":" . __LINE__);
|
||||
//var_dump($properties);echo EOL;
|
||||
if (!array_key_exists($key, $properties))
|
||||
{
|
||||
//logger(DEBUG,_("This is an object 'device'"), __FILE__ . ":" . __LINE__);
|
||||
if(array_key_exists($key, $device->properties))
|
||||
{
|
||||
if ($device->properties[$key]["value"] != $value)
|
||||
{
|
||||
changeValue($device->properties[$key]["value"], $value, $parentDevice, $propertyTree, $key);
|
||||
}
|
||||
}else
|
||||
{
|
||||
$device->properties[$key] = array("value" => $value);
|
||||
$device->properties[$key]["functions"] = array();
|
||||
}
|
||||
}elseif (is_array($device))
|
||||
{
|
||||
//logger(DEBUG,_("deviceType") . $deviceType, __FILE__ . ":" . __LINE__);
|
||||
if (array_key_exists($key, $device))
|
||||
{
|
||||
if (is_array($device[$key]))
|
||||
{
|
||||
if ($device[$key]["value"] != $value)
|
||||
{
|
||||
changeValue($device->$key["value"], $value, $parentDevice, $propertyTree, $key);
|
||||
}
|
||||
}else
|
||||
{
|
||||
changeValue($device[$key], $value, $parentDevice, $propertyTree, $key);
|
||||
}
|
||||
}else
|
||||
{
|
||||
$device[$key] = array("value" => $value);
|
||||
$device[$key]["functions"] = array();
|
||||
}
|
||||
}elseif (is_object($device))
|
||||
{
|
||||
if(property_exists($device, $key))
|
||||
{
|
||||
if (is_array($device->$key))
|
||||
{
|
||||
if ($device->$key["value"] != $value)
|
||||
{
|
||||
changeValue($device->$key["value"], $value, $parentDevice, $propertyTree, $key);
|
||||
}
|
||||
}else
|
||||
{
|
||||
if ($device->$key != $value)
|
||||
{
|
||||
changeValue($device->$key, $value, $parentDevice, $propertyTree, $key);
|
||||
}
|
||||
}
|
||||
}else
|
||||
{
|
||||
$device->$key = array("value" => $value);
|
||||
$device->$key["functions"] = array();
|
||||
}
|
||||
$properties[$key] = array("value" => $value);
|
||||
$properties[$key]["functions"] = array();
|
||||
}
|
||||
changeValue($properties[$key], $value, $parentDevice, $propertyTree, $key);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -331,11 +231,10 @@ function getDevicesValues($topic)
|
||||
}
|
||||
}
|
||||
|
||||
function getDeviceByFriendlyname(&$device, $topic, $fn, $payloadArray, $create = false)
|
||||
/*function getDeviceByFriendlyname(&$device, $topic, $fn, $payloadArray, $create = false)
|
||||
{
|
||||
global $devices, $indexDevices, $indexFriendlyNames;
|
||||
$n = explode("/", $fn);
|
||||
//echo "topic => ". $topic . EOL;
|
||||
$device = &$devices[$topic];
|
||||
foreach($n as $value)
|
||||
{
|
||||
@ -352,7 +251,6 @@ function getDeviceByFriendlyname(&$device, $topic, $fn, $payloadArray, $create =
|
||||
logger(ERROR, sprintf(_(" device with friendlyname %s not found"), $fn), __FILE__ . ":" . __LINE__);
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
if (! array_key_exists("device", $device))
|
||||
{
|
||||
@ -364,28 +262,28 @@ function getDeviceByFriendlyname(&$device, $topic, $fn, $payloadArray, $create =
|
||||
$device["device"]->ieeeAddress = $payloadArray->ieeeAddress;
|
||||
$device["device"]->friendlyname = $fn;
|
||||
$indexDevices[$device["device"]->ieeeAddress] = & $device["device"];
|
||||
$indexFriendlyNames[$fn] = & $device["device"];
|
||||
$indexFriendlyNames[$topic][$fn] = & $device["device"];
|
||||
}
|
||||
}else
|
||||
{
|
||||
logger(INFO, sprintf(_(" device with friendlyname %s exists"), $fn), __FILE__ . ":" . __LINE__);
|
||||
}
|
||||
//var_dump($device);
|
||||
return true;
|
||||
}
|
||||
}*/
|
||||
|
||||
function changeValue(&$oldValue, $value, &$parentDevice, $propertyTree, $key)
|
||||
function changeValue(&$property, $value, &$parentDevice, $propertyTree, $key)
|
||||
{
|
||||
global $mohaDB;
|
||||
$object = $parentDevice->properties[$key];
|
||||
//$changed[$fn]["key"] = $key;
|
||||
//$changed[$fn]["value"] = $value;
|
||||
logger(INFO, sprintf(_("Logging Device %s property %s, %s"), $parentDevice->friendlyName, $propertyTree . $key, bool2string($value)), __FILE__ . ":" . __LINE__);
|
||||
$mohaDB->logProperty($parentDevice, $key, $value, $oldValue);
|
||||
$oldvalue = $value;
|
||||
if (!empty($object["functions"]))
|
||||
logger(INFO, sprintf(_("Logging Device property %s, %s"), $propertyTree . $key, bool2string($value)), __FILE__ . ":" . __LINE__);
|
||||
$mohaDB->logProperty($parentDevice, $key, $value, $property["value"]);
|
||||
$property["value"] = $value;
|
||||
if (!empty($property["functions"]))
|
||||
{
|
||||
logger(DEBUG,_("executing notifications functions"), __FILE__ . ":" . __LINE__);
|
||||
foreach($object["functions"] as $function)
|
||||
foreach($property["functions"] as $function)
|
||||
{
|
||||
$function($parentDevice, $key, $value);
|
||||
}
|
||||
|
Reference in New Issue
Block a user