1
0
moha/db_functions.php

385 lines
11 KiB
PHP
Raw Permalink Normal View History

2021-12-30 16:18:32 +01:00
<?php
2022-01-28 23:05:58 +01:00
logger(DEBUG,_("Including db_functions.php"), __FILE__ . ":" . __LINE__);
2022-01-17 00:18:50 +01:00
2022-02-07 16:58:42 +01:00
//to save or not to save the DB, that is the question ...
2021-12-30 16:18:32 +01:00
function storeDB($db, $filepath)
{
logger(DEBUG,_("Storing database ") . $filepath, __FILE__ . ":" . __LINE__);
$data = serialize($db);
if (file_put_contents($filepath, $data) === false)
{
2022-04-23 02:00:52 +02:00
logger(ALERT,_("Failed storing database ") . $filepath , __FILE__ . ":" . __LINE__);
return false;
}else
{
return true;
}
}
2022-01-17 00:18:50 +01:00
function loadDB($filepath)
2022-01-17 00:18:50 +01:00
{
logger(DEBUG,_("Loading database ") . $filepath, __FILE__ . ":" . __LINE__);
2022-01-17 00:18:50 +01:00
$data = file_get_contents($filepath);
2022-02-07 16:58:42 +01:00
if ($data === false)
{
2022-04-23 02:00:52 +02:00
logger(ALERT,_("Failed loading database ") . $filepath , __FILE__ . ":" . __LINE__);
2022-02-07 16:58:42 +01:00
return false;
}
2022-04-23 02:00:52 +02:00
if (($db = unserialize($data)) === false)
{
logger(ALERT,_("Failed unserializing database ") . $filepath , __FILE__ . ":" . __LINE__);
}
return $db;
2021-12-30 16:18:32 +01:00
}
function mkDevicesDB($topic, $json, $group=false)
2021-12-30 16:18:32 +01:00
{
2022-03-04 22:30:16 +01:00
global $devices, $listProperties, $listPropertiesKeys, $indexDevices, $dbInit, $logFh, $hooks, $indexFriendlyNames;
2022-04-23 02:00:52 +02:00
if (!array_key_exists($topic, $devices))
2022-01-02 18:14:13 +01:00
{
$devices[$topic]= array();
}
2022-04-23 02:00:52 +02:00
//fwrite($logFh, var_export($json, true));
2022-02-23 10:23:16 +01:00
//print_r($json);
foreach ($json as $jsonDevice)
2021-12-30 16:18:32 +01:00
{
$fn = $jsonDevice["friendly_name"];
addDevice($topic, $fn, $jsonDevice, $group);
2021-12-30 16:18:32 +01:00
}
$dbInit += 1;
mkIndexes();
fwrite($logFh, "################################START##################################################");
2022-02-02 21:18:44 +01:00
fwrite($logFh, var_export($devices, true));
fwrite($logFh, "################################END##################################################");
2022-03-04 22:30:16 +01:00
logger(INFO, _("Devices DB made"), __FILE__ . ":" . __LINE__);
2021-12-30 16:18:32 +01:00
//print_r($devices);
}
function addDevice($topic, $fn, $jsonDevice, $group=false )
2022-01-02 18:14:13 +01:00
{
global $devices, $listProperties, $listPropertiesKeys, $hooks, $indexDevices, $indexFriendlyNames;
$fnTree = explode("/", $fn);
$device = & $devices[$topic];
2022-04-23 02:00:52 +02:00
//print_r($jsonDevice);
foreach($fnTree as $fnPart)
2022-01-02 18:14:13 +01:00
{
if (!array_key_exists($fnPart, $device))
{
$device[$fnPart] = array();
}
$device = & $device[$fnPart];
2022-01-02 18:14:13 +01:00
}
if (!array_key_exists("device", $device))
2022-01-02 18:14:13 +01:00
{
$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"];
2022-12-05 12:23:43 +01:00
$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);
2022-01-02 18:14:13 +01:00
}
//indexing device
//$indexDevices[$device->ieeeAddress] = & $device;
//$indexFriendlyNames[$topic][$fn] = & $device;
2022-01-02 18:14:13 +01:00
}
2022-02-23 10:23:16 +01:00
function searchPropertyKey($fn, &$device, $inputObject, $listPropertiesKeys)
{
2022-02-23 10:23:16 +01:00
//foreach($listPropertiesKeys as $propertyKey)
//{
2022-04-23 02:00:52 +02:00
//logger(DEBUG, _("searching for property"), __FILE__ . ":" . __LINE__ );
//if (is_object($inputObject))
//{
//print_r($inputObject);
if (array_key_exists("property", $inputObject))
{
2022-04-23 02:00:52 +02:00
//logger(DEBUG, _("property Key exists filling properties"), __FILE__ . ":" . __LINE__ );
$string = $inputObject["property"];
2022-03-04 22:30:16 +01:00
if (!array_key_exists($string, $device->properties))
{
$device->properties[$string]["value"] = null;
$device->properties[$string]["functions"] = array();
2022-09-09 16:53:49 +02:00
if(array_key_exists("access", $inputObject))
{
2022-12-05 12:23:43 +01:00
if ($inputObject["access"] & 2)
2022-09-09 16:53:49 +02:00
{
$device->properties[$string]["method"] = IDLE;
}
}
}
2022-02-23 10:23:16 +01:00
foreach($inputObject as $key2 => $value2)
{
2022-02-23 10:23:16 +01:00
if ($key2 != "property")
{
$device->properties[$string][$key2] = $value2;
2022-03-04 22:30:16 +01:00
//logger(DEBUG, sprintf(_("property %s value %s"), $key2, print_r($device->properties[$string][$key2])), __FILE__ . ":" . __LINE__ );
2022-02-23 10:23:16 +01:00
}
}
}else
{
if (array_key_exists("type", $inputObject))
2022-02-23 10:23:16 +01:00
{
$device->type = $inputObject["type"];
}
2022-04-23 02:00:52 +02:00
foreach($inputObject as $key => $value)
{
2022-04-23 02:00:52 +02:00
//logger(DEBUG, sprintf(_("key = %s"), $key), __FILE__ . ":" . __LINE__ );
if (is_array($value))
{
searchPropertyKey($fn, $device, $value, $listPropertiesKeys);
}
}
}
//print_r($device);
//}
/*elseif (is_array($inputObject))
2022-02-23 10:23:16 +01:00
{
foreach($inputObject as $value)
{
logger(DEBUG, _("value is object or group, iterating"), __FILE__ . ":" . __LINE__ );
searchPropertyKey($fn, $device, $value, $listPropertiesKeys);
}
}*/
2022-02-23 10:23:16 +01:00
}
function searchPropertyValue($fn, &$device, $object, $listProperties)
2021-12-30 16:18:32 +01:00
{
$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)
2021-12-30 16:18:32 +01:00
{
//print_r($payloadArray);
if (!empty($payloadArray))
{
iterateDevice($topic, $fn, $device, $device->properties, $payloadArray);
}else
{
2022-01-30 00:21:50 +01:00
logger(ERROR, $fn . _(" => payloadArray is empty!"), __FILE__ . ":" . __LINE__);
}
2021-12-30 16:18:32 +01:00
}
function iterateDevice($topic, $fn, &$parentDevice, &$properties, $payloadArray, $propertyTree="")
2021-12-30 16:18:32 +01:00
{
2022-01-17 21:01:11 +01:00
global $changed, $mohaDB, $testMode;
//if (is_a($device, "device")) // = true if object
2022-02-02 21:18:44 +01:00
//echo "==================== New iterate =====================" .EOL;
2022-02-23 10:23:16 +01:00
//echo "deviceType = "; var_dump($deviceType); echo EOL;
2021-12-30 16:18:32 +01:00
//echo "device =>";print_r($device);echo EOL;
2022-01-17 00:18:50 +01:00
//echo "PropertyTree ==============> " . $propertyTree . EOL;
foreach($payloadArray as $key => $value)
2021-12-30 16:18:32 +01:00
{
//$oldValue = null;
2021-12-30 16:18:32 +01:00
//echo "key =>"; print_r($key); echo EOL;
2022-02-23 10:23:16 +01:00
//echo "value =>"; var_dump($value); echo EOL;
2021-12-30 16:18:32 +01:00
//echo "type : " . gettype($value) .EOL;
2022-01-17 00:18:50 +01:00
$valueType = gettype($value);
2022-02-02 21:18:44 +01:00
if ($valueType == "array")
2021-12-30 16:18:32 +01:00
{
//logger(DEBUG,_("valueType == array"), __FILE__ . ":" . __LINE__);
2022-01-17 00:18:50 +01:00
$propertyTree .= $key . "/";
if(!array_key_exists($key, $properties))
{
$properties[$key] = array();
}
iterateDevice($topic, $fn, $parentDevice, $properties[$key], $value, $propertyTree);
2021-12-30 16:18:32 +01:00
}else
{
//logger(DEBUG,_("valueType is ") . $valueType, __FILE__ . ":" . __LINE__);
//var_dump($properties);echo EOL;
if (!array_key_exists($key, $properties))
2022-02-23 10:23:16 +01:00
{
$properties[$key] = array("value" => $value);
$properties[$key]["functions"] = array();
2022-09-09 16:53:49 +02:00
if (array_key_exists("access", $properties[$key]))
{
2022-12-05 12:23:43 +01:00
if ($properties[$key]["access]"] & 2)
2022-09-09 16:53:49 +02:00
{
$properties[$key]["method"] = IDLE;
}
}
2022-03-04 22:30:16 +01:00
}elseif ($properties[$key]["value"] !== $value)
{
changeValue($properties[$key], $value, $parentDevice, $propertyTree, $key);
2022-02-23 10:23:16 +01:00
}
2021-12-30 16:18:32 +01:00
}
}
}
2022-02-07 16:58:42 +01:00
function getDevicesValues($topic)
2022-01-06 13:03:26 +01:00
{
2022-02-07 16:58:42 +01:00
global $indexDevices, $topics;
2022-02-23 10:23:16 +01:00
logger(DEBUG, _("getDevicesValues function" ), __FILE__ . ":" . __LINE__ );
foreach($indexDevices as &$device)
2022-02-07 16:58:42 +01:00
{
if ($device->topic == $topic)
{
2022-02-23 10:23:16 +01:00
logger(DEBUG, "device: " . $device->friendlyName, __FILE__ . ":" . __LINE__ );
$device->payload = array();
2022-02-07 16:58:42 +01:00
$flag = false;
//$properties = array_slice($device, 12);
//logger(DEBUG, print_r($properties, true));
2022-02-23 10:23:16 +01:00
if (!empty($device->properties))
2022-02-07 16:58:42 +01:00
{
2022-02-23 10:23:16 +01:00
foreach($device->properties as $property => $value)
2022-02-07 16:58:42 +01:00
{
2022-03-04 22:30:16 +01:00
if (array_key_exists("access", $value))
{
if ($value["access"] & 4)
2022-03-04 22:30:16 +01:00
{
2022-12-05 12:23:43 +01:00
logger(DEBUG, _("Getting values of property: " . $property), __FILE__ . ":" . __LINE__ );
2022-03-04 22:30:16 +01:00
$device->payload[$property] = "";
2022-12-05 12:23:43 +01:00
$device->get();
break;
2022-03-04 22:30:16 +01:00
}
}
2022-02-07 16:58:42 +01:00
}
2022-12-05 12:23:43 +01:00
}else
{
2022-02-23 10:23:16 +01:00
logger(DEBUG, _("no properties to get for device: " . $device->friendlyName ), __FILE__ . ":" . __LINE__ );
2022-12-05 12:23:43 +01:00
}
2022-02-23 10:23:16 +01:00
}
}
}
function changeValue(&$property, $value, &$parentDevice, $propertyTree, $key)
2022-02-23 10:23:16 +01:00
{
2022-06-13 19:22:08 +02:00
global $mohaDB, $properties2log, $hooks;
2022-02-23 10:23:16 +01:00
//$changed[$fn]["key"] = $key;
//$changed[$fn]["value"] = $value;
2022-05-29 01:55:22 +02:00
if (array_key_exists($key, $properties2log))
{
logger(INFO, sprintf(_("Logging Device property %s, %s"), $propertyTree . $key, bool2string($value)), __FILE__ . ":" . __LINE__);
2022-06-10 12:44:09 +02:00
$mohaDB->logProperty($parentDevice, $key, $value);
2022-12-05 12:23:43 +01:00
/*
if (!empty($propertiesLoggers))
{
foreach($propertiesLoggers as $logger)
{
$logger->callBack($parentDevice, $key, $value);
}
}
*/
2022-05-29 01:55:22 +02:00
logger(DEBUG, sprintf(_("old value was %s, new is %s" ), bool2string($property["value"]), bool2string($value)), __FILE__ . ":" . __LINE__);
}
$property["value"] = $value;
2022-06-20 10:34:55 +02:00
$r = $parentDevice->friendlyName;
logger(DEBUG, _("Changed value of ") . $r . "=> " . bool2string($value) , __FILE__ . ":" . __LINE__);
if (!empty($property["functions"]))
2022-02-23 10:23:16 +01:00
{
2022-09-09 16:53:49 +02:00
logger(DEBUG,_("executing CallBack"), __FILE__ . ":" . __LINE__);
foreach($property["functions"] as $function)
2022-02-23 10:23:16 +01:00
{
try
{
2022-06-13 19:22:08 +02:00
$active = $hooks[$function[0]->hookName]->active;
logger(INFO, "active = " . bool2string($active), __FILE__ . ":" . __LINE__);
if ( $active === true )
2022-05-29 01:55:22 +02:00
{
$function($parentDevice, $key, $value);
}else
{
2022-06-13 19:22:08 +02:00
logger(INFO, $function[0]->hookName . _(" is disabled"), __FILE__ . ":" . __LINE__ );
2022-05-29 01:55:22 +02:00
}
}catch (Exception $e)
{
$s = 'Exception reçue : ' . $e->getMessage();
logger(ERROR, $parentDevice->friendlyName . "/" . $property .": " . $s);
}
2022-02-07 16:58:42 +01:00
}
}
2022-01-06 13:03:26 +01:00
}
2021-12-30 16:18:32 +01:00
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)
{
2022-04-23 02:00:52 +02:00
global $indexDevices, $indexFriendlyNames, $indexTypes, $indexProperties;
foreach ($object as $key => &$device)
{
if (is_a($device, "device"))
{
2022-04-23 02:00:52 +02:00
//$object = &$device;
//print("=============" . $device->friendlyName);
//if (!array_key_exists($object->ieeeAddress, $indexDevices))
//{
//print("============>");
2022-04-23 02:00:52 +02:00
$indexDevices[$device->ieeeAddress] = &$device;
$indexFriendlyNames[$device->friendlyName] = &$device;
if (property_exists($device, "type"))
{
2022-04-23 02:00:52 +02:00
$indexTypes[$device->type][] = &$object;
}
$properties = array_keys($device->properties);
foreach($properties as $property)
{
$indexProperties[$property][$device->ieeeAddress] = &$device;
}
//}
2022-04-23 02:00:52 +02:00
//return true;
}elseif (empty($device))
{
return true;
}else
{
iterate2device($device);
}
}
return false;
}
2021-12-30 16:18:32 +01:00
?>