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
|
|
|
}
|
|
|
|
|
2021-12-31 14:34:20 +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();
|
|
|
|
}
|
2021-12-31 14:34:20 +01:00
|
|
|
foreach ($json as $jsonDevice)
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
|
|
|
//print_r($device);
|
2021-12-31 14:34:20 +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();
|
|
|
|
}
|
2021-12-31 14:34:20 +01:00
|
|
|
$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;
|
|
|
|
}
|
2021-12-31 14:34:20 +01:00
|
|
|
$device["device"]->topic = $topic;
|
|
|
|
//$device["device"]->device = $jsonDevice;
|
|
|
|
$device["device"]->friendlyName = $jsonDevice->friendly_name;
|
|
|
|
if ($group)
|
|
|
|
{
|
|
|
|
//print_r($device);
|
|
|
|
$device["device"]->groupID = $jsonDevice->id;
|
|
|
|
$indexDevices[$device["device"]->groupID] = $jsonDevice->friendly_name;
|
|
|
|
}else
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
2022-01-02 18:14:13 +01:00
|
|
|
addDevice($device, $fn, $jsonDevice);
|
2021-12-30 16:18:32 +01:00
|
|
|
}
|
|
|
|
}
|
2021-12-31 23:09:58 +01:00
|
|
|
$dbInit += 1;
|
2021-12-31 14:34:20 +01:00
|
|
|
fwrite($logFh, "################################START##################################################");
|
2022-02-02 21:18:44 +01:00
|
|
|
fwrite($logFh, var_export($devices, true));
|
2021-12-31 14:34:20 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-01-02 18:14:13 +01:00
|
|
|
function addDevice(& $device, $fn, $jsonDevice )
|
|
|
|
{
|
|
|
|
global $listProperties, $listPropertiesKeys, $hooks, $indexDevices;
|
|
|
|
$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"];
|
|
|
|
}
|
|
|
|
|
2021-12-31 23:09:58 +01:00
|
|
|
function searchPropertyKey($fn, &$device, $object, $listPropertiesKeys)
|
|
|
|
{
|
|
|
|
foreach($listPropertiesKeys as $property)
|
|
|
|
{
|
|
|
|
foreach($object as $key => $value)
|
|
|
|
{
|
2022-01-03 21:11:52 +01:00
|
|
|
if (gettype($value) == "object" or gettype($value) == "array")
|
|
|
|
{
|
|
|
|
searchPropertyKey($fn, $device, $value, $listPropertiesKeys);
|
|
|
|
}
|
2021-12-31 23:09:58 +01:00
|
|
|
if ( isset($value->property))
|
|
|
|
{
|
2022-01-05 00:01:41 +01:00
|
|
|
$string = $value->property;
|
|
|
|
$device->{$string}["value"] = null;
|
|
|
|
$device->$string["functions"] = array();
|
2021-12-31 23:09:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//print_r($device);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-02 12:14:30 +01:00
|
|
|
function changeDevice($topic, $fn, &$device, $payloadArray)
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
2022-01-02 12:14:30 +01:00
|
|
|
//print_r($payloadArray);
|
2022-01-03 21:11:52 +01:00
|
|
|
if (!empty($payloadArray))
|
|
|
|
{
|
2022-02-02 21:18:44 +01:00
|
|
|
//echo "==================== New ChangeDevice =====================" .EOL;
|
|
|
|
|
2022-01-17 00:18:50 +01:00
|
|
|
iterateDevice($topic, $fn, $device, $device, $payloadArray);
|
2022-01-03 21:11:52 +01:00
|
|
|
}else
|
|
|
|
{
|
2022-01-30 00:21:50 +01:00
|
|
|
logger(ERROR, $fn . _(" => payloadArray is empty!"), __FILE__ . ":" . __LINE__);
|
2022-01-03 21:11:52 +01:00
|
|
|
}
|
|
|
|
|
2021-12-30 16:18:32 +01:00
|
|
|
}
|
|
|
|
|
2022-01-17 00:18:50 +01:00
|
|
|
function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $propertyTree="")
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
2022-01-17 21:01:11 +01:00
|
|
|
global $changed, $mohaDB, $testMode;
|
2022-01-17 00:18:50 +01:00
|
|
|
$deviceType = (gettype($device) == "object"); // = true if object
|
2022-02-02 21:18:44 +01:00
|
|
|
//echo "==================== New iterate =====================" .EOL;
|
2022-01-17 00:18:50 +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;
|
2022-01-02 12:14:30 +01:00
|
|
|
foreach($payloadArray as $key => $value)
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
2022-01-08 23:45:38 +01:00
|
|
|
$oldValue = null;
|
2021-12-30 16:18:32 +01:00
|
|
|
//echo "key =>"; print_r($key); echo EOL;
|
|
|
|
//echo "value =>"; print_r($value); echo EOL;
|
|
|
|
//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 == "object")
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
2022-01-17 00:18:50 +01:00
|
|
|
$propertyTree .= $key . "/";
|
2022-02-02 21:18:44 +01:00
|
|
|
//echo "PropertyTree " . $propertyTree . EOL;
|
2021-12-30 16:18:32 +01:00
|
|
|
//echo " is Object" . EOL;
|
2022-01-17 00:18:50 +01:00
|
|
|
if ($deviceType === true )
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
2022-02-02 21:18:44 +01:00
|
|
|
//echo "deviceType = true" . EOL;
|
2022-01-17 00:18:50 +01:00
|
|
|
if (!property_exists($device, $key))
|
|
|
|
{
|
2022-02-02 21:18:44 +01:00
|
|
|
//echo "Property do not exists" . EOL;
|
2022-01-17 00:18:50 +01:00
|
|
|
$device->{$key} = new stdClass;
|
|
|
|
}
|
|
|
|
//echo "iterating" . EOL;
|
2022-02-02 21:18:44 +01:00
|
|
|
//echo "===============>";
|
|
|
|
|
2022-01-17 00:18:50 +01:00
|
|
|
iterateDevice($topic, $fn, $parentDevice, $device->$key, $value, $propertyTree);
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
//echo "is array";
|
|
|
|
if (!array_key_exists($key, $device))
|
|
|
|
{
|
|
|
|
$device[$key] = new stdClass;
|
|
|
|
}
|
|
|
|
//echo "iterating" . EOL;
|
|
|
|
iterateDevice($topic, $fn, $parentDevice, $device[$key], $value, $propertyTree);
|
2021-12-30 16:18:32 +01:00
|
|
|
}
|
2022-01-17 00:18:50 +01:00
|
|
|
|
|
|
|
}elseif ($valueType == "array")
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
2022-01-17 00:18:50 +01:00
|
|
|
$propertyTree .= $key . "/";
|
|
|
|
if ($deviceType === true )
|
|
|
|
{
|
|
|
|
$device->$key = array();
|
|
|
|
iterateDevice($topic, $fn, $parentDevice, $device->$key, $value, $propertyTree);
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
$device[$key] = array();
|
|
|
|
iterateDevice($topic, $fn, $parentDevice, $device[$key], $value, $propertyTree);
|
|
|
|
}
|
2021-12-30 16:18:32 +01:00
|
|
|
|
|
|
|
}else
|
|
|
|
{
|
2022-02-02 21:18:44 +01:00
|
|
|
//var_dump($device);echo EOL;
|
2022-01-19 00:22:34 +01:00
|
|
|
//if (empty($device->$key) or $value != null)
|
|
|
|
//{
|
2022-01-17 00:18:50 +01:00
|
|
|
if (property_exists($device, $key))
|
2022-01-02 18:14:13 +01:00
|
|
|
{
|
2022-01-17 00:18:50 +01:00
|
|
|
if (is_array($device->$key))
|
|
|
|
{
|
2022-01-20 00:26:57 +01:00
|
|
|
$oldValue = $device->$key["value"];
|
2022-01-17 00:18:50 +01:00
|
|
|
}else
|
|
|
|
{
|
|
|
|
$oldValue = $device->$key;
|
|
|
|
}
|
2022-01-05 00:01:41 +01:00
|
|
|
}else
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
2022-01-17 00:18:50 +01:00
|
|
|
$device->{$key} = array("value" => null);
|
2022-01-05 00:01:41 +01:00
|
|
|
$device->$key["functions"] = array();
|
|
|
|
}
|
2022-01-27 18:41:16 +01:00
|
|
|
//echo $key . ' ===> oldvalue = ' . $oldValue . " value = " . $value . EOL;
|
2022-01-08 23:45:38 +01:00
|
|
|
if ($oldValue !== $value)
|
2022-01-05 00:01:41 +01:00
|
|
|
{
|
|
|
|
$device->$key["value"] = $value;
|
2022-01-19 00:22:34 +01:00
|
|
|
//$changed[$fn]["key"] = $key;
|
|
|
|
//$changed[$fn]["value"] = $value;
|
2022-01-28 23:05:58 +01:00
|
|
|
logger(INFO, sprintf(_("Device %s property %s, %s"), $fn, $propertyTree . $key, bool2string($value)), __FILE__ . ":" . __LINE__);
|
2022-01-19 00:22:34 +01:00
|
|
|
$mohaDB->logProperty($parentDevice, $propertyTree . $key, $value, $oldValue);
|
2022-01-23 09:46:06 +01:00
|
|
|
if (!empty($device->$key["functions"]))
|
|
|
|
{
|
2022-01-28 23:05:58 +01:00
|
|
|
logger(DEBUG,_("executing notifications functions"), __FILE__ . ":" . __LINE__);
|
2022-01-23 09:46:06 +01:00
|
|
|
foreach($device->$key["functions"] as $function)
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
2022-01-23 09:46:06 +01:00
|
|
|
$function($device, $key, $value);
|
2021-12-30 16:18:32 +01:00
|
|
|
}
|
|
|
|
}
|
2022-01-27 18:41:16 +01:00
|
|
|
}
|
|
|
|
|
2022-01-19 00:22:34 +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;
|
|
|
|
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($properties as $property)
|
|
|
|
{
|
|
|
|
if (flag)
|
|
|
|
{
|
|
|
|
$payload = ",";
|
|
|
|
}else {
|
|
|
|
$flag = true;
|
|
|
|
}
|
|
|
|
$payload .= $property . ':""';
|
|
|
|
}
|
|
|
|
$device->payload = $payload . "}";
|
|
|
|
logger(DEBUG, $device->payload);
|
|
|
|
$device->get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exit(0);
|
2022-01-06 13:03:26 +01:00
|
|
|
}
|
2021-12-30 16:18:32 +01:00
|
|
|
|
2022-02-07 16:58:42 +01:00
|
|
|
|
2021-12-30 16:18:32 +01:00
|
|
|
?>
|