debugage
This commit is contained in:
254
db_functions.php
254
db_functions.php
@ -26,9 +26,10 @@ function mkDevicesDB($topic, $json, $group=false)
|
||||
{
|
||||
$devices[$topic]= array();
|
||||
}
|
||||
//print_r($json);
|
||||
foreach ($json as $jsonDevice)
|
||||
{
|
||||
//print_r($device);
|
||||
|
||||
$fn = $jsonDevice->friendly_name;
|
||||
$fnTree = explode("/", $fn);
|
||||
$device = & $devices[$topic];
|
||||
@ -55,7 +56,7 @@ function mkDevicesDB($topic, $json, $group=false)
|
||||
$indexFriendlyNames[$fn] = & $device["device"];
|
||||
}else
|
||||
{
|
||||
addDevice($device, $fn, $jsonDevice);
|
||||
addDevice($device["device"], $fn, $jsonDevice);
|
||||
}
|
||||
}
|
||||
$dbInit += 1;
|
||||
@ -70,43 +71,69 @@ function mkDevicesDB($topic, $json, $group=false)
|
||||
function addDevice(& $device, $fn, $jsonDevice )
|
||||
{
|
||||
global $listProperties, $listPropertiesKeys, $hooks, $indexDevices, $indexFriendlyNames;
|
||||
$device["device"]->type = $jsonDevice->type;
|
||||
$device["device"]->ieeeAddress = $jsonDevice->ieee_address;
|
||||
$device->type = $jsonDevice->type;
|
||||
$device->ieeeAddress = $jsonDevice->ieee_address;
|
||||
if ( !empty($jsonDevice->power_source ) )
|
||||
{
|
||||
$device["device"]->powerSource = $jsonDevice->power_source;
|
||||
$device->powerSource = $jsonDevice->power_source;
|
||||
}
|
||||
if ($jsonDevice->definition != null)
|
||||
{
|
||||
$device["device"]->description = $jsonDevice->definition->description;
|
||||
searchPropertyKey($fn, $device["device"], $jsonDevice->definition->exposes, $listPropertiesKeys);
|
||||
$device->description = $jsonDevice->definition->description;
|
||||
searchPropertyKey($fn, $device, $jsonDevice->definition->exposes, $listPropertiesKeys);
|
||||
}
|
||||
searchPropertyValue($fn, $device["device"], $jsonDevice, $listProperties);
|
||||
searchPropertyValue($fn, $device, $jsonDevice, $listProperties);
|
||||
|
||||
//indexing device
|
||||
$indexDevices[$device["device"]->ieeeAddress] = & $device["device"];
|
||||
$indexFriendlyNames[$fn] = & $device["device"];
|
||||
$indexDevices[$device->ieeeAddress] = & $device;
|
||||
$indexFriendlyNames[$fn] = & $device;
|
||||
}
|
||||
|
||||
function searchPropertyKey($fn, &$device, $object, $listPropertiesKeys)
|
||||
function searchPropertyKey($fn, &$device, $inputObject, $listPropertiesKeys)
|
||||
{
|
||||
foreach($listPropertiesKeys as $property)
|
||||
//foreach($listPropertiesKeys as $propertyKey)
|
||||
//{
|
||||
logger(DEBUG, _("searching for property"), __FILE__ . ":" . __LINE__ );
|
||||
if (is_object($inputObject))
|
||||
{
|
||||
foreach($object as $key => $value)
|
||||
if (property_exists($inputObject, "property"))
|
||||
{
|
||||
if (gettype($value) == "object" or gettype($value) == "array")
|
||||
logger(DEBUG, _("propertyKey exists filling properties"), __FILE__ . ":" . __LINE__ );
|
||||
$string = $inputObject->property;
|
||||
$device->properties[$string]["value"] = null;
|
||||
$device->properties[$string]["functions"] = array();
|
||||
|
||||
foreach($inputObject as $key2 => $value2)
|
||||
{
|
||||
searchPropertyKey($fn, $device, $value, $listPropertiesKeys);
|
||||
|
||||
if ($key2 != "property")
|
||||
{
|
||||
$device->properties[$string][$key2] = $value2;
|
||||
logger(DEBUG, sprintf(_("property %s value %s"), $key2, print_r($device->properties[$string][$key2])), __FILE__ . ":" . __LINE__ );
|
||||
}
|
||||
}
|
||||
if ( isset($value->property))
|
||||
}else
|
||||
{
|
||||
if (property_exists($inputObject, "type"))
|
||||
{
|
||||
$string = $value->property;
|
||||
$device->properties[$string]["value"] = null;
|
||||
$device->properties[$string]["functions"] = array();
|
||||
$device->type = $inputObject->type;
|
||||
}
|
||||
foreach($inputObject as $key => $value)
|
||||
{
|
||||
logger(DEBUG, sprintf(_("key = %s"), $key), __FILE__ . ":" . __LINE__ );
|
||||
searchPropertyKey($fn, $device, $value, $listPropertiesKeys);
|
||||
}
|
||||
}
|
||||
//print_r($device);
|
||||
}elseif (is_array($inputObject))
|
||||
{
|
||||
foreach($inputObject as $value)
|
||||
{
|
||||
logger(DEBUG, _("value is object or group, iterating"), __FILE__ . ":" . __LINE__ );
|
||||
searchPropertyKey($fn, $device, $value, $listPropertiesKeys);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function searchPropertyValue($fn, &$device, $object, $listProperties)
|
||||
@ -143,18 +170,20 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro
|
||||
global $changed, $mohaDB, $testMode;
|
||||
$deviceType = (is_a($device, "device")); // = true if object
|
||||
//echo "==================== New iterate =====================" .EOL;
|
||||
//echo "devicetype = "; var_dump($deviceType); echo EOL;
|
||||
//echo "deviceType = "; var_dump($deviceType); echo EOL;
|
||||
//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 "value =>"; var_dump($value); echo EOL;
|
||||
//echo "type : " . gettype($value) .EOL;
|
||||
$valueType = gettype($value);
|
||||
if ($valueType == "object")
|
||||
{
|
||||
//logger(DEBUG, _("valueType == object"), __FILE__ . ":" . __LINE__);
|
||||
|
||||
$propertyTree .= $key . "/";
|
||||
//echo "PropertyTree " . $propertyTree . EOL;
|
||||
//echo " is Object" . EOL;
|
||||
@ -170,67 +199,104 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro
|
||||
//echo "===============>";
|
||||
|
||||
iterateDevice($topic, $fn, $parentDevice, $device->properties[$key], $value, $propertyTree);
|
||||
}else
|
||||
}elseif(is_array($device))
|
||||
{
|
||||
//echo "is array";
|
||||
if (!array_key_exists($key, $device->properties))
|
||||
if (!array_key_exists($key, $device))
|
||||
{
|
||||
$device->properties[$key] = new stdClass;
|
||||
$device[$key] = new stdClass;
|
||||
}
|
||||
//echo "iterating" . EOL;
|
||||
iterateDevice($topic, $fn, $parentDevice, $device->properties[$key], $value, $propertyTree);
|
||||
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);
|
||||
}else
|
||||
}elseif(is_array($device))
|
||||
{
|
||||
$device->properties[$key] = array();
|
||||
iterateDevice($topic, $fn, $parentDevice, $device->properties[$key], $value, $propertyTree);
|
||||
$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);
|
||||
}
|
||||
|
||||
}else
|
||||
{
|
||||
//logger(DEBUG,_("valueType is ") . $valueType, __FILE__ . ":" . __LINE__);
|
||||
|
||||
//var_dump($device);echo EOL;
|
||||
//if (empty($device->$key) or $value != null)
|
||||
//{
|
||||
if (property_exists($device, $key))
|
||||
if (is_a($device, "device"))
|
||||
{
|
||||
//logger(DEBUG,_("This is an object 'device'"), __FILE__ . ":" . __LINE__);
|
||||
if(array_key_exists($key, $device->properties))
|
||||
{
|
||||
if (is_array($device->$key))
|
||||
if ($device->properties[$key]["value"] != $value)
|
||||
{
|
||||
$oldValue = $device->$key["value"];
|
||||
}else
|
||||
{
|
||||
$oldValue = $device->$key;
|
||||
changeValue($device->properties[$key]["value"], $value, $parentDevice, $propertyTree, $key);
|
||||
}
|
||||
}else
|
||||
{
|
||||
$device->$key = array("value" => null);
|
||||
$device->$key["functions"] = array();
|
||||
$device->properties[$key] = array("value" => $value);
|
||||
$device->properties[$key]["functions"] = array();
|
||||
}
|
||||
//echo $key . ' ===> oldvalue = ' . $oldValue . " value = " . $value . EOL;
|
||||
if ($oldValue !== $value)
|
||||
}elseif (is_array($device))
|
||||
{
|
||||
//logger(DEBUG,_("deviceType") . $deviceType, __FILE__ . ":" . __LINE__);
|
||||
if (array_key_exists($key, $device))
|
||||
{
|
||||
$device->$key["value"] = $value;
|
||||
//$changed[$fn]["key"] = $key;
|
||||
//$changed[$fn]["value"] = $value;
|
||||
logger(INFO, sprintf(_("Device %s property %s, %s"), $fn, $propertyTree . $key, bool2string($value)), __FILE__ . ":" . __LINE__);
|
||||
$mohaDB->logProperty($parentDevice, $propertyTree . $key, $value, $oldValue);
|
||||
if (!empty($device->$key["functions"]))
|
||||
if (is_array($device[$key]))
|
||||
{
|
||||
logger(DEBUG,_("executing notifications functions"), __FILE__ . ":" . __LINE__);
|
||||
foreach($device->properties[$key]["functions"] as $function)
|
||||
if ($device[$key]["value"] != $value)
|
||||
{
|
||||
$function($device, $key, $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();
|
||||
}
|
||||
//}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -238,30 +304,90 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro
|
||||
function getDevicesValues($topic)
|
||||
{
|
||||
global $indexDevices, $topics;
|
||||
logger(DEBUG, _("getDevicesValues function" ));
|
||||
logger(DEBUG, _("getDevicesValues function" ), __FILE__ . ":" . __LINE__ );
|
||||
foreach($indexDevices as $device)
|
||||
{
|
||||
if ($device->topic == $topic)
|
||||
{
|
||||
logger(DEBUG, "device: " . $device->friendlyName);
|
||||
$payload = "{";
|
||||
logger(DEBUG, "device: " . $device->friendlyName, __FILE__ . ":" . __LINE__ );
|
||||
$device->payload = array();
|
||||
$flag = false;
|
||||
//$properties = array_slice($device, 12);
|
||||
//logger(DEBUG, print_r($properties, true));
|
||||
foreach($device->properties as $property => $value)
|
||||
if (!empty($device->properties))
|
||||
{
|
||||
|
||||
if ($flag)
|
||||
foreach($device->properties as $property => $value)
|
||||
{
|
||||
$payload .= ",";
|
||||
}else {
|
||||
$flag = true;
|
||||
$device->payload[$property] = "";
|
||||
}
|
||||
$payload .= '"' . $property . '":""';
|
||||
}
|
||||
$device->payload = $payload . "}";
|
||||
logger(DEBUG, $device->payload);
|
||||
$device->get();
|
||||
//logger(DEBUG, print_r($device->payload, true), __FILE__ . ":" . __LINE__ );
|
||||
$device->get();
|
||||
}else
|
||||
{
|
||||
logger(DEBUG, _("no properties to get for device: " . $device->friendlyName ), __FILE__ . ":" . __LINE__ );
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
//print_r($device[$value]);
|
||||
if (array_key_exists($value, $device))
|
||||
{
|
||||
$device = &$device[$value];
|
||||
}elseif($create === true)
|
||||
{
|
||||
$device[$value] = array();
|
||||
$device = &$device[$value];
|
||||
}else
|
||||
{
|
||||
logger(ERROR, sprintf(_(" device with friendlyname %s not found"), $fn), __FILE__ . ":" . __LINE__);
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
if (! array_key_exists("device", $device))
|
||||
{
|
||||
if ($create === true)
|
||||
{
|
||||
logger(WARNING, _("init of ") . $fn, __FILE__ . ":" . __LINE__);
|
||||
$device["device"] = new device;
|
||||
$device["device"]->type = $payloadArray->type;
|
||||
$device["device"]->ieeeAddress = $payloadArray->ieeeAddress;
|
||||
$device["device"]->friendlyname = $fn;
|
||||
$indexDevices[$device["device"]->ieeeAddress] = & $device["device"];
|
||||
$indexFriendlyNames[$fn] = & $device["device"];
|
||||
}
|
||||
}else
|
||||
{
|
||||
logger(INFO, sprintf(_(" device with friendlyname %s exists"), $fn), __FILE__ . ":" . __LINE__);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function changeValue(&$oldValue, $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(DEBUG,_("executing notifications functions"), __FILE__ . ":" . __LINE__);
|
||||
foreach($object["functions"] as $function)
|
||||
{
|
||||
$function($parentDevice, $key, $value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user