212 lines
5.2 KiB
PHP
212 lines
5.2 KiB
PHP
<?php
|
|
|
|
function storeDB($db, $filepath)
|
|
{
|
|
$data = serialize($db);
|
|
$fp = fopen($filePath, "w");
|
|
fwrite($fp, $data);
|
|
fclose($fp);
|
|
}
|
|
|
|
function mkDevicesDB($topic, $json, $group=false)
|
|
{
|
|
global $devices, $listProperties, $listPropertiesKeys, $indexDevices, $dbInit, $logFh, $hooks;
|
|
if (!isset($devices[$topic]))
|
|
{
|
|
$devices[$topic]= array();
|
|
}
|
|
foreach ($json as $jsonDevice)
|
|
{
|
|
//print_r($device);
|
|
$fn = $jsonDevice->friendly_name;
|
|
$fnTree = explode("/", $fn);
|
|
$device = & $devices[$topic];
|
|
foreach($fnTree as $fnPart)
|
|
{
|
|
if (!isset($device[$fnPart]))
|
|
{
|
|
$device[$fnPart] = array();
|
|
}
|
|
$device = & $device[$fnPart];
|
|
}
|
|
if (!isset($device["device"]))
|
|
{
|
|
$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
|
|
{
|
|
addDevice($device, $fn, $jsonDevice);
|
|
}
|
|
}
|
|
$dbInit += 1;
|
|
fwrite($logFh, "################################START##################################################");
|
|
fwrite($logFh, print_r($devices, true));
|
|
fwrite($logFh, "################################END##################################################");
|
|
|
|
echo "Devices DB made" . EOL;
|
|
//print_r($devices);
|
|
}
|
|
|
|
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);
|
|
//$device["device"]->availability["functions"][] = $hooks["availability"]->getHook();
|
|
|
|
//indexing device
|
|
$indexDevices[$device["device"]->ieeeAddress] = & $device["device"];
|
|
|
|
//print_r($device);
|
|
}
|
|
|
|
function searchPropertyKey($fn, &$device, $object, $listPropertiesKeys)
|
|
{
|
|
foreach($listPropertiesKeys as $property)
|
|
{
|
|
foreach($object as $key => $value)
|
|
{
|
|
if (gettype($value) == "object" or gettype($value) == "array")
|
|
{
|
|
searchPropertyKey($fn, $device, $value, $listPropertiesKeys);
|
|
}
|
|
if ( isset($value->property))
|
|
{
|
|
$string = $value->property;
|
|
//echo "property ===> " . $value->property . EOL;
|
|
$device->{$string}["value"] = null;
|
|
$device->$string["functions"] = array();
|
|
}
|
|
}
|
|
//print_r($device);
|
|
}
|
|
}
|
|
|
|
function searchPropertyValue($fn, &$device, $object, $listProperties)
|
|
{
|
|
$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)
|
|
{
|
|
//$fnTree = explode("/", $fn);
|
|
//print_r($payloadArray);
|
|
if (!empty($payloadArray))
|
|
{
|
|
iterateDevice($topic, $fn, $device, $payloadArray);
|
|
}else
|
|
{
|
|
logger(ERROR, _("payloadArray is empty!"));
|
|
}
|
|
|
|
}
|
|
|
|
function iterateDevice($topic, $fn, &$device, $payloadArray)
|
|
{
|
|
global $changed;
|
|
//print_r($payloadArray);
|
|
//echo "device =>";print_r($device);echo EOL;
|
|
foreach($payloadArray as $key => $value)
|
|
{
|
|
$oldValue = null;
|
|
//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;
|
|
|
|
}else
|
|
{
|
|
$device->{$key}["value"] = null;
|
|
$device->$key["functions"] = array();
|
|
}
|
|
if ($oldValue !== $value)
|
|
{
|
|
$device->$key["value"] = $value;
|
|
$changed[$fn]["key"] = $key;
|
|
$changed[$fn]["value"] = $value;
|
|
logger(INFO, sprintf(_("Device %s property %s, value changed to %s"), $device->friendlyName, $key, $value));
|
|
$mohaDB->logProperty($device, $key, $value);
|
|
//echo "oldvalue => " . print_r($oldValue, true) . EOL;
|
|
/*if (empty($oldValue))
|
|
{
|
|
echo "Initializing " . $key;
|
|
}else
|
|
{
|
|
echo "changed " . $key . " value " . $oldValue;;
|
|
}
|
|
echo " to " . $value . EOL;*/
|
|
}
|
|
//print_r($device->functions); print_r($value);
|
|
//print_r($device);
|
|
if (!empty($device->$key["functions"]))
|
|
{
|
|
echo "executing notifications functions " . EOL;
|
|
foreach($device->$key["functions"] as $function)
|
|
{
|
|
//print_r($function);
|
|
$function($device, $key, $value);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
function getDevicesValues()
|
|
{
|
|
//TODO
|
|
}
|
|
|
|
?>
|