1
0
moha/db_functions.php

385 lines
11 KiB
PHP

<?php
logger(DEBUG,_("Including db_functions.php"), __FILE__ . ":" . __LINE__);
//to save or not to save the DB, that is the question ...
function storeDB($db, $filepath)
{
logger(DEBUG,_("Storing database ") . $filepath, __FILE__ . ":" . __LINE__);
$data = serialize($db);
if (file_put_contents($filepath, $data) === false)
{
logger(ALERT,_("Failed storing database ") . $filepath , __FILE__ . ":" . __LINE__);
return false;
}else
{
return true;
}
}
function loadDB($filepath)
{
logger(DEBUG,_("Loading database ") . $filepath, __FILE__ . ":" . __LINE__);
$data = file_get_contents($filepath);
if ($data === false)
{
logger(ALERT,_("Failed loading database ") . $filepath , __FILE__ . ":" . __LINE__);
return false;
}
if (($db = unserialize($data)) === false)
{
logger(ALERT,_("Failed unserializing database ") . $filepath , __FILE__ . ":" . __LINE__);
}
return $db;
}
function mkDevicesDB($topic, $json, $group=false)
{
global $devices, $listProperties, $listPropertiesKeys, $indexDevices, $dbInit, $logFh, $hooks, $indexFriendlyNames;
if (!array_key_exists($topic, $devices))
{
$devices[$topic]= array();
}
//fwrite($logFh, var_export($json, true));
//print_r($json);
foreach ($json as $jsonDevice)
{
$fn = $jsonDevice["friendly_name"];
addDevice($topic, $fn, $jsonDevice, $group);
}
$dbInit += 1;
mkIndexes();
fwrite($logFh, "################################START##################################################");
fwrite($logFh, var_export($devices, true));
fwrite($logFh, "################################END##################################################");
logger(INFO, _("Devices DB made"), __FILE__ . ":" . __LINE__);
//print_r($devices);
}
function addDevice($topic, $fn, $jsonDevice, $group=false )
{
global $devices, $listProperties, $listPropertiesKeys, $hooks, $indexDevices, $indexFriendlyNames;
$fnTree = explode("/", $fn);
$device = & $devices[$topic];
//print_r($jsonDevice);
foreach($fnTree as $fnPart)
{
if (!array_key_exists($fnPart, $device))
{
$device[$fnPart] = array();
}
$device = & $device[$fnPart];
}
if (!array_key_exists("device", $device))
{
$device["device"] = new device;
}
$device["device"]->topic = $topic;
//$device["device"]->device = $jsonDevice;
$device["device"]->friendlyName = $fn;
if ($group)
{
//print_r($device);
$device["device"]->groupID = $jsonDevice["id"];
$device["device"]->ieeeAddress = $jsonDevice["id"];
//$indexDevices[$device["device"]->groupID] = & $device["device"];
//$indexFriendlyNames[$topic][$fn] = & $device["device"];
}else
{
$device["device"]->type = $jsonDevice["type"];
$device["device"]->ieeeAddress = $jsonDevice["ieee_address"];
$hooks["availability"]->installHook($device);
if ( !empty($jsonDevice["power_source"] ))
{
$device["device"]->powerSource = $jsonDevice["power_source"];
}
if ($jsonDevice["definition"] != null)
{
$device["device"]->description = $jsonDevice["definition"]["description"];
if (array_key_exists("exposes", $jsonDevice["definition"]))
{
searchPropertyKey($fn, $device["device"], $jsonDevice["definition"]["exposes"], $listPropertiesKeys);
}
}
searchPropertyValue($fn, $device["device"], $jsonDevice, $listProperties);
}
//indexing device
//$indexDevices[$device->ieeeAddress] = & $device;
//$indexFriendlyNames[$topic][$fn] = & $device;
}
function searchPropertyKey($fn, &$device, $inputObject, $listPropertiesKeys)
{
//foreach($listPropertiesKeys as $propertyKey)
//{
//logger(DEBUG, _("searching for property"), __FILE__ . ":" . __LINE__ );
//if (is_object($inputObject))
//{
//print_r($inputObject);
if (array_key_exists("property", $inputObject))
{
//logger(DEBUG, _("property Key exists filling properties"), __FILE__ . ":" . __LINE__ );
$string = $inputObject["property"];
if (!array_key_exists($string, $device->properties))
{
$device->properties[$string]["value"] = null;
$device->properties[$string]["functions"] = array();
if(array_key_exists("access", $inputObject))
{
if ($inputObject["access"] & 2)
{
$device->properties[$string]["method"] = IDLE;
}
}
}
foreach($inputObject as $key2 => $value2)
{
if ($key2 != "property")
{
$device->properties[$string][$key2] = $value2;
//logger(DEBUG, sprintf(_("property %s value %s"), $key2, print_r($device->properties[$string][$key2])), __FILE__ . ":" . __LINE__ );
}
}
}else
{
if (array_key_exists("type", $inputObject))
{
$device->type = $inputObject["type"];
}
foreach($inputObject as $key => $value)
{
//logger(DEBUG, sprintf(_("key = %s"), $key), __FILE__ . ":" . __LINE__ );
if (is_array($value))
{
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)
{
$objectArray = (array)$object;
foreach($listProperties as $key => $value)
{
if (in_array($value, $objectArray))
{
//echo "$value trouvé =>";
$device->$key = $value;
//echo $device->$key . EOL;
}
}
}
function changeDevice($topic, $fn, &$device, $payloadArray)
{
//print_r($payloadArray);
if (!empty($payloadArray))
{
iterateDevice($topic, $fn, $device, $device->properties, $payloadArray);
}else
{
logger(ERROR, $fn . _(" => payloadArray is empty!"), __FILE__ . ":" . __LINE__);
}
}
function iterateDevice($topic, $fn, &$parentDevice, &$properties, $payloadArray, $propertyTree="")
{
global $changed, $mohaDB, $testMode;
//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;
//echo "PropertyTree ==============> " . $propertyTree . EOL;
foreach($payloadArray as $key => $value)
{
//$oldValue = null;
//echo "key =>"; print_r($key); echo EOL;
//echo "value =>"; var_dump($value); echo EOL;
//echo "type : " . gettype($value) .EOL;
$valueType = gettype($value);
if ($valueType == "array")
{
//logger(DEBUG,_("valueType == array"), __FILE__ . ":" . __LINE__);
$propertyTree .= $key . "/";
if(!array_key_exists($key, $properties))
{
$properties[$key] = array();
}
iterateDevice($topic, $fn, $parentDevice, $properties[$key], $value, $propertyTree);
}else
{
//logger(DEBUG,_("valueType is ") . $valueType, __FILE__ . ":" . __LINE__);
//var_dump($properties);echo EOL;
if (!array_key_exists($key, $properties))
{
$properties[$key] = array("value" => $value);
$properties[$key]["functions"] = array();
if (array_key_exists("access", $properties[$key]))
{
if ($properties[$key]["access]"] & 2)
{
$properties[$key]["method"] = IDLE;
}
}
}elseif ($properties[$key]["value"] !== $value)
{
changeValue($properties[$key], $value, $parentDevice, $propertyTree, $key);
}
}
}
}
function getDevicesValues($topic)
{
global $indexDevices, $topics;
logger(DEBUG, _("getDevicesValues function" ), __FILE__ . ":" . __LINE__ );
foreach($indexDevices as &$device)
{
if ($device->topic == $topic)
{
logger(DEBUG, "device: " . $device->friendlyName, __FILE__ . ":" . __LINE__ );
$device->payload = array();
$flag = false;
//$properties = array_slice($device, 12);
//logger(DEBUG, print_r($properties, true));
if (!empty($device->properties))
{
foreach($device->properties as $property => $value)
{
if (array_key_exists("access", $value))
{
if ($value["access"] & 4)
{
logger(DEBUG, _("Getting values of property: " . $property), __FILE__ . ":" . __LINE__ );
$device->payload[$property] = "";
$device->get();
break;
}
}
}
}else
{
logger(DEBUG, _("no properties to get for device: " . $device->friendlyName ), __FILE__ . ":" . __LINE__ );
}
}
}
}
function changeValue(&$property, $value, &$parentDevice, $propertyTree, $key)
{
global $mohaDB, $properties2log, $hooks;
//$changed[$fn]["key"] = $key;
//$changed[$fn]["value"] = $value;
if (array_key_exists($key, $properties2log))
{
logger(INFO, sprintf(_("Logging Device property %s, %s"), $propertyTree . $key, bool2string($value)), __FILE__ . ":" . __LINE__);
$mohaDB->logProperty($parentDevice, $key, $value);
/*
if (!empty($propertiesLoggers))
{
foreach($propertiesLoggers as $logger)
{
$logger->callBack($parentDevice, $key, $value);
}
}
*/
logger(DEBUG, sprintf(_("old value was %s, new is %s" ), bool2string($property["value"]), bool2string($value)), __FILE__ . ":" . __LINE__);
}
$property["value"] = $value;
$r = $parentDevice->friendlyName;
logger(DEBUG, _("Changed value of ") . $r . "=> " . bool2string($value) , __FILE__ . ":" . __LINE__);
if (!empty($property["functions"]))
{
logger(DEBUG,_("executing CallBack"), __FILE__ . ":" . __LINE__);
foreach($property["functions"] as $function)
{
try
{
$active = $hooks[$function[0]->hookName]->active;
logger(INFO, "active = " . bool2string($active), __FILE__ . ":" . __LINE__);
if ( $active === true )
{
$function($parentDevice, $key, $value);
}else
{
logger(INFO, $function[0]->hookName . _(" is disabled"), __FILE__ . ":" . __LINE__ );
}
}catch (Exception $e)
{
$s = 'Exception reçue : ' . $e->getMessage();
logger(ERROR, $parentDevice->friendlyName . "/" . $property .": " . $s);
}
}
}
}
function mkIndexes()
{
global $devices, $indexDevices, $indexFriendlyNames;
$device = null;
logger(INFO, _("function mkIndexes"), __FILE__ . ":" . __LINE__);
foreach ($devices as $key => &$object)
{
print "======####" . $key . EOL;
$r = iterate2device($object);
}
}
function iterate2device(&$object)
{
global $indexDevices, $indexFriendlyNames, $indexTypes, $indexProperties;
foreach ($object as $key => &$device)
{
if (is_a($device, "device"))
{
//$object = &$device;
//print("=============" . $device->friendlyName);
//if (!array_key_exists($object->ieeeAddress, $indexDevices))
//{
//print("============>");
$indexDevices[$device->ieeeAddress] = &$device;
$indexFriendlyNames[$device->friendlyName] = &$device;
if (property_exists($device, "type"))
{
$indexTypes[$device->type][] = &$object;
}
$properties = array_keys($device->properties);
foreach($properties as $property)
{
$indexProperties[$property][$device->ieeeAddress] = &$device;
}
//}
//return true;
}elseif (empty($device))
{
return true;
}else
{
iterate2device($device);
}
}
return false;
}
?>