270 lines
7.3 KiB
PHP
270 lines
7.3 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;
|
|
if (!isset($devices[$topic]))
|
|
{
|
|
$devices[$topic]= array();
|
|
}
|
|
foreach ($json as $jsonDevice)
|
|
{
|
|
//print_r($device);
|
|
$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[$fn] = & $device["device"];
|
|
}else
|
|
{
|
|
addDevice($device, $fn, $jsonDevice);
|
|
}
|
|
}
|
|
$dbInit += 1;
|
|
fwrite($logFh, "################################START##################################################");
|
|
fwrite($logFh, var_export($devices, true));
|
|
fwrite($logFh, "################################END##################################################");
|
|
|
|
logger(DEBUG, _("Devices DB made"), __FILE__ . ":" . __LINE__);
|
|
//print_r($devices);
|
|
}
|
|
|
|
function addDevice(& $device, $fn, $jsonDevice )
|
|
{
|
|
global $listProperties, $listPropertiesKeys, $hooks, $indexDevices, $indexFriendlyNames;
|
|
$device["device"]->type = $jsonDevice->type;
|
|
$device["device"]->ieeeAddress = $jsonDevice->ieee_address;
|
|
if ( !empty($jsonDevice->power_source ) )
|
|
{
|
|
$device["device"]->powerSource = $jsonDevice->power_source;
|
|
}
|
|
if ($jsonDevice->definition != null)
|
|
{
|
|
$device["device"]->description = $jsonDevice->definition->description;
|
|
searchPropertyKey($fn, $device["device"], $jsonDevice->definition->exposes, $listPropertiesKeys);
|
|
}
|
|
searchPropertyValue($fn, $device["device"], $jsonDevice, $listProperties);
|
|
|
|
//indexing device
|
|
$indexDevices[$device["device"]->ieeeAddress] = & $device["device"];
|
|
$indexFriendlyNames[$fn] = & $device["device"];
|
|
}
|
|
|
|
function searchPropertyKey($fn, &$device, $object, $listPropertiesKeys)
|
|
{
|
|
foreach($listPropertiesKeys as $property)
|
|
{
|
|
foreach($object as $key => $value)
|
|
{
|
|
if (gettype($value) == "object" or gettype($value) == "array")
|
|
{
|
|
searchPropertyKey($fn, $device, $value, $listPropertiesKeys);
|
|
}
|
|
if ( isset($value->property))
|
|
{
|
|
$string = $value->property;
|
|
$device->properties[$string]["value"] = null;
|
|
$device->properties[$string]["functions"] = array();
|
|
}
|
|
}
|
|
//print_r($device);
|
|
}
|
|
}
|
|
|
|
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))
|
|
{
|
|
//echo "==================== New ChangeDevice =====================" .EOL;
|
|
|
|
iterateDevice($topic, $fn, $device, $device, $payloadArray);
|
|
}else
|
|
{
|
|
logger(ERROR, $fn . _(" => payloadArray is empty!"), __FILE__ . ":" . __LINE__);
|
|
}
|
|
|
|
}
|
|
|
|
function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $propertyTree="")
|
|
{
|
|
global $changed, $mohaDB, $testMode;
|
|
$deviceType = (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 =>"; print_r($value); echo EOL;
|
|
//echo "type : " . gettype($value) .EOL;
|
|
$valueType = gettype($value);
|
|
if ($valueType == "object")
|
|
{
|
|
$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);
|
|
}else
|
|
{
|
|
//echo "is array";
|
|
if (!array_key_exists($key, $device->properties))
|
|
{
|
|
$device->properties[$key] = new stdClass;
|
|
}
|
|
//echo "iterating" . EOL;
|
|
iterateDevice($topic, $fn, $parentDevice, $device->properties[$key], $value, $propertyTree);
|
|
}
|
|
|
|
}elseif ($valueType == "array")
|
|
{
|
|
$propertyTree .= $key . "/";
|
|
if ($deviceType === true )
|
|
{
|
|
$device->properties[$key] = array();
|
|
iterateDevice($topic, $fn, $parentDevice, $device->properties[$key], $value, $propertyTree);
|
|
}else
|
|
{
|
|
$device->properties[$key] = array();
|
|
iterateDevice($topic, $fn, $parentDevice, $device->properties[$key], $value, $propertyTree);
|
|
}
|
|
|
|
}else
|
|
{
|
|
//var_dump($device);echo EOL;
|
|
//if (empty($device->$key) or $value != null)
|
|
//{
|
|
if (property_exists($device, $key))
|
|
{
|
|
if (is_array($device->$key))
|
|
{
|
|
$oldValue = $device->$key["value"];
|
|
}else
|
|
{
|
|
$oldValue = $device->$key;
|
|
}
|
|
}else
|
|
{
|
|
$device->$key = array("value" => null);
|
|
$device->$key["functions"] = array();
|
|
}
|
|
//echo $key . ' ===> oldvalue = ' . $oldValue . " value = " . $value . EOL;
|
|
if ($oldValue !== $value)
|
|
{
|
|
$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"]))
|
|
{
|
|
logger(DEBUG,_("executing notifications functions"), __FILE__ . ":" . __LINE__);
|
|
foreach($device->properties[$key]["functions"] as $function)
|
|
{
|
|
$function($device, $key, $value);
|
|
}
|
|
}
|
|
}
|
|
//}
|
|
}
|
|
}
|
|
}
|
|
|
|
function getDevicesValues($topic)
|
|
{
|
|
global $indexDevices, $topics;
|
|
logger(DEBUG, _("getDevicesValues function" ));
|
|
foreach($indexDevices as $device)
|
|
{
|
|
if ($device->topic == $topic)
|
|
{
|
|
logger(DEBUG, "device: " . $device->friendlyName);
|
|
$payload = "{";
|
|
$flag = false;
|
|
//$properties = array_slice($device, 12);
|
|
//logger(DEBUG, print_r($properties, true));
|
|
foreach($device->properties as $property => $value)
|
|
{
|
|
|
|
if ($flag)
|
|
{
|
|
$payload .= ",";
|
|
}else {
|
|
$flag = true;
|
|
}
|
|
$payload .= '"' . $property . '":""';
|
|
}
|
|
$device->payload = $payload . "}";
|
|
logger(DEBUG, $device->payload);
|
|
$device->get();
|
|
}
|
|
}
|
|
}
|
|
|
|
?>
|