making compatibility with mqtt's friendly names
This commit is contained in:
@ -8,37 +8,63 @@ function storeDB($db, $filepath)
|
||||
fclose($fp);
|
||||
}
|
||||
|
||||
function mkDevicesDB($topic, $json)
|
||||
function deviceTree(string $topic, array $fnTree, array &$device)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
function mkDevicesDB($topic, $json, $group=false)
|
||||
{
|
||||
global $devices, $listProperties, $indexDevices, $dbInit, $logFh;
|
||||
foreach ($json as $device)
|
||||
if (!isset($devices[$topic])) $devices[$topic]= array();
|
||||
foreach ($json as $jsonDevice)
|
||||
{
|
||||
//print_r($device);
|
||||
$fn = $device->friendly_name;
|
||||
$devices[$topic][$fn] = new device;
|
||||
$devices[$topic][$fn]->topic = $topic;
|
||||
$devices[$topic][$fn]->device = $device;
|
||||
$devices[$topic][$fn]->type = $device->type;
|
||||
$devices[$topic][$fn]->ieeeAddress = $device->ieee_address;
|
||||
$devices[$topic][$fn]->friendlyName = $device->friendly_name;
|
||||
if ( !empty($devices[$topic][$fn]->powerSource ) )
|
||||
$fn = $jsonDevice->friendly_name;
|
||||
$fnTree = explode("/", $fn);
|
||||
$device = & $devices[$topic];
|
||||
foreach($fnTree as $fnPart)
|
||||
{
|
||||
$devices[$topic][$fn]->powerSource = $device->power_source;
|
||||
$device[$fnPart] = array();
|
||||
$device = & $device[$fnPart];
|
||||
}
|
||||
if ($device->definition != null)
|
||||
|
||||
$device["device"] = new device;
|
||||
$device["device"]->topic = $topic;
|
||||
//$device["device"]->device = $jsonDevice;
|
||||
$device["device"]->friendlyName = $jsonDevice->friendly_name;
|
||||
if ($group)
|
||||
{
|
||||
$devices[$topic][$fn]->description = $device->definition->description;
|
||||
//print_r($device);
|
||||
$device["device"]->groupID = $jsonDevice->id;
|
||||
$indexDevices[$device["device"]->groupID] = $jsonDevice->friendly_name;
|
||||
}else
|
||||
{
|
||||
$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);
|
||||
}
|
||||
searchProperty($fn, $devices[$topic][$fn], $device, $listProperties);
|
||||
$indexDevices[$device->ieee_address] = $device->friendly_name;
|
||||
}
|
||||
$dbInit = true;
|
||||
fwrite($logFh, "################################START##################################################");
|
||||
fwrite($logFh, print_r($devices, true));
|
||||
fwrite($logFh, "################################END##################################################");
|
||||
|
||||
echo "Devices DB made" .EOL;
|
||||
//print_r($devices);
|
||||
}
|
||||
|
||||
function searchProperty($fn, $device, $object, $listProperties)
|
||||
function searchProperty($fn, &$device, $object, $listProperties)
|
||||
{
|
||||
$objectArray = (array)$object;
|
||||
foreach($listProperties as $key => $value)
|
||||
@ -52,17 +78,43 @@ function searchProperty($fn, $device, $object, $listProperties)
|
||||
}
|
||||
}
|
||||
|
||||
function changeDevice($topic, $fn, $device)
|
||||
function changeDevice($topic, $fn, &$device, $json)
|
||||
{
|
||||
//print_r($device);
|
||||
iterateDevice($topic, $fn, $device, $device->json);
|
||||
/*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);
|
||||
}
|
||||
}
|
||||
|
||||
function iterateDevice($topic, $fn, $device, $object)
|
||||
function iterateDevice($topic, $fn, &$device, $json)
|
||||
{
|
||||
global $changed;
|
||||
|
||||
//echo "device =>";print_r($device);echo EOL;
|
||||
foreach($object as $key => $value)
|
||||
foreach($json as $key => $value)
|
||||
{
|
||||
$oldValue = 0;
|
||||
//echo "key =>"; print_r($key); echo EOL;
|
||||
@ -95,14 +147,14 @@ function iterateDevice($topic, $fn, $device, $object)
|
||||
$changed[$fn]["value"] = $value;
|
||||
|
||||
//echo "oldvalue => " . print_r($oldValue, true) . EOL;
|
||||
if (empty($oldValue))
|
||||
/*if (empty($oldValue))
|
||||
{
|
||||
echo "Initializing " . $key;
|
||||
}else
|
||||
{
|
||||
echo "changed " . $key . " value " . $oldValue;;
|
||||
}
|
||||
echo " to " . $value . EOL;
|
||||
echo " to " . $value . EOL;*/
|
||||
}
|
||||
//print_r($device->functions);
|
||||
if (!empty($device->functions))
|
||||
|
Reference in New Issue
Block a user