1
0
moha/db_functions.php

176 lines
4.1 KiB
PHP
Raw Normal View History

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);
}
function deviceTree(string $topic, array $fnTree, array &$device)
{
}
function mkDevicesDB($topic, $json, $group=false)
2021-12-30 16:18:32 +01:00
{
global $devices, $listProperties, $indexDevices, $dbInit, $logFh;
if (!isset($devices[$topic])) $devices[$topic]= array();
foreach ($json as $jsonDevice)
2021-12-30 16:18:32 +01:00
{
//print_r($device);
$fn = $jsonDevice->friendly_name;
$fnTree = explode("/", $fn);
$device = & $devices[$topic];
foreach($fnTree as $fnPart)
2021-12-30 16:18:32 +01:00
{
$device[$fnPart] = array();
$device = & $device[$fnPart];
2021-12-30 16:18:32 +01:00
}
$device["device"] = new device;
$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
{
$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;
}
//searchProperty($fn, $device["device"], $jsonDevice, $listProperties);
$indexDevices[$device["device"]->ieeeAddress] = & $device["device"];
//print_r($device);
2021-12-30 16:18:32 +01:00
}
}
$dbInit = true;
fwrite($logFh, "################################START##################################################");
2021-12-30 16:18:32 +01:00
fwrite($logFh, print_r($devices, true));
fwrite($logFh, "################################END##################################################");
2021-12-30 16:18:32 +01:00
echo "Devices DB made" .EOL;
//print_r($devices);
}
function searchProperty($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, $json)
2021-12-30 16:18:32 +01:00
{
//print_r($device);
/*foreach($fnTree as $fn)
{
//print_r($device);
if (!isset($device[$fn]))
{
}
$device = & $device[$fn];
}*/
//print_r($json);
$fnTree = explode("/", $fn);
print_r($json);
if ( ($jsonDevice = json_decode($json)) === null )
{
echo "json ========>>>>>>> " . print_r($json,true) . EOL;
if (!isset($device->value))
{
$device->{"value"} = $json;
$device->type = true; //set this as parameter and not device
}
print_r($device);
}else
{
iterateDevice($topic, $fn, $device, $jsonDevice);
}
2021-12-30 16:18:32 +01:00
}
function iterateDevice($topic, $fn, &$device, $json)
2021-12-30 16:18:32 +01:00
{
global $changed;
2021-12-30 16:18:32 +01:00
//echo "device =>";print_r($device);echo EOL;
foreach($json as $key => $value)
2021-12-30 16:18:32 +01:00
{
$oldValue = 0;
//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)
{
if (isset($device->$key)) $oldValue = $device->$key;
if ($oldValue != $value)
{
$device->{$key} = $value;
$changed[$fn]["key"] = $key;
$changed[$fn]["value"] = $value;
//echo "oldvalue => " . print_r($oldValue, true) . EOL;
/*if (empty($oldValue))
2021-12-30 16:18:32 +01:00
{
echo "Initializing " . $key;
}else
{
echo "changed " . $key . " value " . $oldValue;;
}
echo " to " . $value . EOL;*/
2021-12-30 16:18:32 +01:00
}
//print_r($device->functions);
if (!empty($device->functions))
{
echo "executing notifications functions " . EOL;
foreach($device->functions as $function)
{
//print_r($function);
$function($topic, $fn, $key, $value);
}
}
}
}
}
}
?>