2021-12-30 16:18:32 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
function storeDB($db, $filepath)
|
|
|
|
{
|
|
|
|
$data = serialize($db);
|
|
|
|
$fp = fopen($filePath, "w");
|
|
|
|
fwrite($fp, $data);
|
|
|
|
fclose($fp);
|
|
|
|
}
|
|
|
|
|
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-02 18:14:13 +01:00
|
|
|
if (!isset($device[$fnPart]))
|
|
|
|
{
|
|
|
|
$device[$fnPart] = array();
|
|
|
|
}
|
2021-12-31 14:34:20 +01:00
|
|
|
$device = & $device[$fnPart];
|
2021-12-30 16:18:32 +01:00
|
|
|
}
|
2022-01-02 18:14:13 +01:00
|
|
|
if (!isset($device["device"]))
|
|
|
|
{
|
|
|
|
$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##################################################");
|
2021-12-30 16:18:32 +01:00
|
|
|
fwrite($logFh, print_r($devices, true));
|
2021-12-31 14:34:20 +01:00
|
|
|
fwrite($logFh, "################################END##################################################");
|
|
|
|
|
2021-12-31 23:09:58 +01:00
|
|
|
echo "Devices DB made" . EOL;
|
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);
|
|
|
|
|
|
|
|
// adding callback function for availability
|
|
|
|
//print_r($hooks);
|
2022-01-06 13:03:26 +01:00
|
|
|
//$device["device"]->availability["functions"][] = $hooks["availability"]->getHook();
|
2022-01-02 18:14:13 +01:00
|
|
|
|
|
|
|
//indexing device
|
|
|
|
$indexDevices[$device["device"]->ieeeAddress] = & $device["device"];
|
|
|
|
|
|
|
|
//print_r($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;
|
2021-12-31 23:09:58 +01:00
|
|
|
//echo "property ===> " . $value->property . EOL;
|
2022-01-05 00:01:41 +01:00
|
|
|
$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
|
|
|
//$fnTree = explode("/", $fn);
|
|
|
|
//print_r($payloadArray);
|
2022-01-03 21:11:52 +01:00
|
|
|
if (!empty($payloadArray))
|
|
|
|
{
|
|
|
|
iterateDevice($topic, $fn, $device, $payloadArray);
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
logger(ERROR, _("payloadArray is empty!"));
|
|
|
|
}
|
|
|
|
|
2021-12-30 16:18:32 +01:00
|
|
|
}
|
|
|
|
|
2022-01-02 12:14:30 +01:00
|
|
|
function iterateDevice($topic, $fn, &$device, $payloadArray)
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
|
|
|
global $changed;
|
2022-01-02 18:14:13 +01:00
|
|
|
//print_r($payloadArray);
|
2021-12-30 16:18:32 +01:00
|
|
|
//echo "device =>";print_r($device);echo 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;
|
|
|
|
if (gettype($value) == "object")
|
|
|
|
{
|
|
|
|
//echo " is Object" . EOL;
|
|
|
|
if (!property_exists($device, $key))
|
|
|
|
{
|
|
|
|
$device->{$key} = null;
|
|
|
|
}
|
|
|
|
//echo "iterating" . EOL;
|
|
|
|
iterateDevice($topic, $fn, $device, $value);
|
|
|
|
}elseif (gettype($value) == "array")
|
|
|
|
{
|
|
|
|
//echo "is array" . EOL;
|
|
|
|
|
|
|
|
iterateDevice($topic, $fn, $device, $value);
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
if (empty($device->$key) or $value != null)
|
|
|
|
{
|
2022-01-02 18:14:13 +01:00
|
|
|
if (isset($device->$key))
|
|
|
|
{
|
|
|
|
$oldValue = $device->$key;
|
|
|
|
|
2022-01-05 00:01:41 +01:00
|
|
|
}else
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
2022-01-05 00:01:41 +01:00
|
|
|
$device->{$key}["value"] = null;
|
|
|
|
$device->$key["functions"] = array();
|
|
|
|
}
|
2022-01-08 23:45:38 +01:00
|
|
|
if ($oldValue !== $value)
|
2022-01-05 00:01:41 +01:00
|
|
|
{
|
|
|
|
$device->$key["value"] = $value;
|
2021-12-30 16:18:32 +01:00
|
|
|
$changed[$fn]["key"] = $key;
|
|
|
|
$changed[$fn]["value"] = $value;
|
|
|
|
|
|
|
|
//echo "oldvalue => " . print_r($oldValue, true) . EOL;
|
2021-12-31 14:34:20 +01:00
|
|
|
/*if (empty($oldValue))
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
|
|
|
echo "Initializing " . $key;
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
echo "changed " . $key . " value " . $oldValue;;
|
|
|
|
}
|
2021-12-31 14:34:20 +01:00
|
|
|
echo " to " . $value . EOL;*/
|
2021-12-30 16:18:32 +01:00
|
|
|
}
|
2022-01-02 18:14:13 +01:00
|
|
|
//print_r($device->functions); print_r($value);
|
2022-01-06 13:03:26 +01:00
|
|
|
//print_r($device);
|
2022-01-05 00:01:41 +01:00
|
|
|
if (!empty($device->$key["functions"]))
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
2022-01-05 00:01:41 +01:00
|
|
|
echo "executing notifications functions " . EOL;
|
|
|
|
foreach($device->$key["functions"] as $function)
|
2021-12-30 16:18:32 +01:00
|
|
|
{
|
2022-01-06 13:03:26 +01:00
|
|
|
//print_r($function);
|
2022-01-02 18:14:13 +01:00
|
|
|
$function($device, $key, $value);
|
2021-12-30 16:18:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-06 13:03:26 +01:00
|
|
|
function getDevicesValues()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2021-12-30 16:18:32 +01:00
|
|
|
|
|
|
|
?>
|