1
0
moha/db_functions.php

260 lines
7.5 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)
{
$data = serialize($db);
return file_put_contents($filepath, $data);
}
function loadDB(& $db, $filepath)
{
$data = file_get_contents($filepath);
if ($data === false)
{
return false;
}
$db = unserialize($data);
}
function mkDevicesDB($topic, $json, $group=false)
{
global $devices, $listProperties, $listPropertiesKeys, $indexDevices, $dbInit, $logFh, $hooks, $indexFriendlyNames;
if (!isset($devices[$topic]))
{
$devices[$topic]= array();
}
//print_r($json);
foreach ($json as $jsonDevice)
{
$fn = $jsonDevice->friendly_name;
$fnTree = explode("/", $fn);
$device = & $devices[$topic];
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;
$indexDevices[$device["device"]->groupID] = & $device["device"];
$indexFriendlyNames[$topic][$fn] = & $device["device"];
}else
{
addDevice($topic, $device["device"], $fn, $jsonDevice);
}
}
$dbInit += 1;
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, &$device, $fn, $jsonDevice )
{
global $listProperties, $listPropertiesKeys, $hooks, $indexDevices, $indexFriendlyNames;
$device->type = $jsonDevice->type;
$device->ieeeAddress = $jsonDevice->ieee_address;
if ( !empty($jsonDevice->power_source ) )
{
$device->powerSource = $jsonDevice->power_source;
}
if ($jsonDevice->definition != null)
{
$device->description = $jsonDevice->definition->description;
searchPropertyKey($fn, $device, $jsonDevice->definition->exposes, $listPropertiesKeys);
}
searchPropertyValue($fn, $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))
{
if (property_exists($inputObject, "property"))
{
logger(DEBUG, _("propertyKey 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();
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 (property_exists($inputObject, "type"))
{
$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)
{
$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 . "/";
$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();
}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"] & 5)
{
$device->payload[$property] = "";
}
}
}
//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 changeValue(&$property, $value, &$parentDevice, $propertyTree, $key)
{
global $mohaDB;
//$changed[$fn]["key"] = $key;
//$changed[$fn]["value"] = $value;
logger(INFO, sprintf(_("Logging Device property %s, %s"), $propertyTree . $key, bool2string($value)), __FILE__ . ":" . __LINE__);
$mohaDB->logProperty($parentDevice, $key, $value, $property["value"]);
logger(DEBUG, sprintf(_("old value was %s, new is %s" ), bool2string($property["value"]), bool2string($value)), __FILE__ . ":" . __LINE__);
$property["value"] = $value;
if (!empty($property["functions"]))
{
logger(DEBUG,_("executing notifications functions"), __FILE__ . ":" . __LINE__);
foreach($property["functions"] as $function)
{
$function($parentDevice, $key, $value);
}
}
}
?>