1
0
moha/db_functions.php

294 lines
8.4 KiB
PHP
Raw 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)
{
2022-01-17 00:18:50 +01:00
$data = serialize($db);
2022-02-07 16:58:42 +01:00
return file_put_contents($filepath, $data);
2022-01-17 00:18:50 +01:00
}
function loadDB(& $db, $filepath)
{
$data = file_get_contents($filepath);
2022-02-07 16:58:42 +01:00
if ($data === false)
{
return false;
}
2022-01-17 00:18:50 +01:00
$db = unserialize($data);
2021-12-30 16:18:32 +01:00
}
function mkDevicesDB($topic, $json, $group=false)
2021-12-30 16:18:32 +01:00
{
2022-01-02 18:14:13 +01:00
global $devices, $listProperties, $listPropertiesKeys, $indexDevices, $dbInit, $logFh, $hooks;
if (!isset($devices[$topic]))
{
$devices[$topic]= array();
}
2022-02-23 10:23:16 +01:00
//print_r($json);
foreach ($json as $jsonDevice)
2021-12-30 16:18:32 +01:00
{
2022-02-23 10:23:16 +01:00
$fn = $jsonDevice->friendly_name;
$fnTree = explode("/", $fn);
$device = & $devices[$topic];
foreach($fnTree as $fnPart)
2021-12-30 16:18:32 +01:00
{
2022-01-17 00:18:50 +01:00
if (!array_key_exists($fnPart, $device))
2022-01-02 18:14:13 +01:00
{
$device[$fnPart] = array();
}
$device = & $device[$fnPart];
2021-12-30 16:18:32 +01:00
}
2022-01-17 00:18:50 +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;
$indexDevices[$device["device"]->groupID] = & $device["device"];
$indexFriendlyNames[$topic][$fn] = & $device["device"];
}else
2021-12-30 16:18:32 +01:00
{
addDevice($topic, $device["device"], $fn, $jsonDevice);
2021-12-30 16:18:32 +01:00
}
}
$dbInit += 1;
fwrite($logFh, "################################START##################################################");
2022-02-02 21:18:44 +01:00
fwrite($logFh, var_export($devices, true));
fwrite($logFh, "################################END##################################################");
2022-01-28 23:05:58 +01:00
logger(DEBUG, _("Devices DB made"), __FILE__ . ":" . __LINE__);
2021-12-30 16:18:32 +01:00
//print_r($devices);
}
function addDevice($topic, & $device, $fn, $jsonDevice )
2022-01-02 18:14:13 +01:00
{
global $listProperties, $listPropertiesKeys, $hooks, $indexDevices, $indexFriendlyNames;
2022-02-23 10:23:16 +01:00
$device->type = $jsonDevice->type;
$device->ieeeAddress = $jsonDevice->ieee_address;
2022-01-02 18:14:13 +01:00
if ( !empty($jsonDevice->power_source ) )
{
2022-02-23 10:23:16 +01:00
$device->powerSource = $jsonDevice->power_source;
2022-01-02 18:14:13 +01:00
}
if ($jsonDevice->definition != null)
{
2022-02-23 10:23:16 +01:00
$device->description = $jsonDevice->definition->description;
searchPropertyKey($fn, $device, $jsonDevice->definition->exposes, $listPropertiesKeys);
2022-01-02 18:14:13 +01:00
}
2022-02-23 10:23:16 +01:00
searchPropertyValue($fn, $device, $jsonDevice, $listProperties);
2022-01-02 18:14:13 +01:00
//indexing device
2022-02-23 10:23:16 +01:00
$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)
//{
logger(DEBUG, _("searching for property"), __FILE__ . ":" . __LINE__ );
if (is_object($inputObject))
{
2022-02-23 10:23:16 +01:00
if (property_exists($inputObject, "property"))
{
2022-02-23 10:23:16 +01:00
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)
{
2022-02-23 10:23:16 +01:00
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;
}
2022-02-23 10:23:16 +01:00
foreach($inputObject as $key => $value)
{
2022-02-23 10:23:16 +01:00
logger(DEBUG, sprintf(_("key = %s"), $key), __FILE__ . ":" . __LINE__ );
searchPropertyKey($fn, $device, $value, $listPropertiesKeys);
}
}
//print_r($device);
2022-02-23 10:23:16 +01:00
}elseif (is_array($inputObject))
{
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))
{
2022-02-02 21:18:44 +01:00
//echo "==================== New ChangeDevice =====================" .EOL;
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 . "/";
$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-02-23 10:23:16 +01:00
}
changeValue($properties[$key], $value, $parentDevice, $propertyTree, $key);
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__ );
2022-02-07 16:58:42 +01:00
foreach($indexDevices as $device)
{
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-02-23 10:23:16 +01:00
$device->payload[$property] = "";
2022-02-07 16:58:42 +01:00
}
2022-02-23 10:23:16 +01:00
//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)
2022-02-23 10:23:16 +01:00
{
global $devices, $indexDevices, $indexFriendlyNames;
$n = explode("/", $fn);
$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[$topic][$fn] = & $device["device"];
2022-02-23 10:23:16 +01:00
}
}else
{
logger(INFO, sprintf(_(" device with friendlyname %s exists"), $fn), __FILE__ . ":" . __LINE__);
}
//var_dump($device);
2022-02-23 10:23:16 +01:00
return true;
}*/
2022-02-23 10:23:16 +01:00
function changeValue(&$property, $value, &$parentDevice, $propertyTree, $key)
2022-02-23 10:23:16 +01:00
{
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"]))
2022-02-23 10:23:16 +01:00
{
logger(DEBUG,_("executing notifications functions"), __FILE__ . ":" . __LINE__);
foreach($property["functions"] as $function)
2022-02-23 10:23:16 +01:00
{
$function($parentDevice, $key, $value);
2022-02-07 16:58:42 +01:00
}
}
2022-01-06 13:03:26 +01:00
}
2021-12-30 16:18:32 +01:00
?>