1
0

modified device database and the code for managing it

This commit is contained in:
2022-02-12 15:23:58 +01:00
parent 314305bcb1
commit 7d1dd25f2a
13 changed files with 156 additions and 113 deletions

View File

@ -46,12 +46,13 @@ function mkDevicesDB($topic, $json, $group=false)
}
$device["device"]->topic = $topic;
//$device["device"]->device = $jsonDevice;
$device["device"]->friendlyName = $jsonDevice->friendly_name;
$device["device"]->friendlyName = $fn;
if ($group)
{
//print_r($device);
$device["device"]->groupID = $jsonDevice->id;
$indexDevices[$device["device"]->groupID] = $jsonDevice->friendly_name;
$indexDevices[$device["device"]->groupID] = & $device["device"];
$indexFriendlyNames[$fn] = & $device["device"];
}else
{
addDevice($device, $fn, $jsonDevice);
@ -68,7 +69,7 @@ function mkDevicesDB($topic, $json, $group=false)
function addDevice(& $device, $fn, $jsonDevice )
{
global $listProperties, $listPropertiesKeys, $hooks, $indexDevices;
global $listProperties, $listPropertiesKeys, $hooks, $indexDevices, $indexFriendlyNames;
$device["device"]->type = $jsonDevice->type;
$device["device"]->ieeeAddress = $jsonDevice->ieee_address;
if ( !empty($jsonDevice->power_source ) )
@ -84,6 +85,7 @@ function addDevice(& $device, $fn, $jsonDevice )
//indexing device
$indexDevices[$device["device"]->ieeeAddress] = & $device["device"];
$indexFriendlyNames[$fn] = & $device["device"];
}
function searchPropertyKey($fn, &$device, $object, $listPropertiesKeys)
@ -99,8 +101,8 @@ function searchPropertyKey($fn, &$device, $object, $listPropertiesKeys)
if ( isset($value->property))
{
$string = $value->property;
$device->{$string}["value"] = null;
$device->$string["functions"] = array();
$device->properties[$string]["value"] = null;
$device->properties[$string]["functions"] = array();
}
}
//print_r($device);
@ -139,7 +141,7 @@ function changeDevice($topic, $fn, &$device, $payloadArray)
function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $propertyTree="")
{
global $changed, $mohaDB, $testMode;
$deviceType = (gettype($device) == "object"); // = true if object
$deviceType = (is_a($device, "device")); // = true if object
//echo "==================== New iterate =====================" .EOL;
//echo "devicetype = "; var_dump($deviceType); echo EOL;
//echo "device =>";print_r($device);echo EOL;
@ -159,24 +161,24 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro
if ($deviceType === true )
{
//echo "deviceType = true" . EOL;
if (!property_exists($device, $key))
if (!array_key_exists($key, $device->properties))
{
//echo "Property do not exists" . EOL;
$device->{$key} = new stdClass;
$device->properties[$key] = new stdClass;
}
//echo "iterating" . EOL;
//echo "===============>";
iterateDevice($topic, $fn, $parentDevice, $device->$key, $value, $propertyTree);
iterateDevice($topic, $fn, $parentDevice, $device->properties[$key], $value, $propertyTree);
}else
{
//echo "is array";
if (!array_key_exists($key, $device))
if (!array_key_exists($key, $device->properties))
{
$device[$key] = new stdClass;
$device->properties[$key] = new stdClass;
}
//echo "iterating" . EOL;
iterateDevice($topic, $fn, $parentDevice, $device[$key], $value, $propertyTree);
iterateDevice($topic, $fn, $parentDevice, $device->properties[$key], $value, $propertyTree);
}
}elseif ($valueType == "array")
@ -184,12 +186,12 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro
$propertyTree .= $key . "/";
if ($deviceType === true )
{
$device->$key = array();
iterateDevice($topic, $fn, $parentDevice, $device->$key, $value, $propertyTree);
$device->properties[$key] = array();
iterateDevice($topic, $fn, $parentDevice, $device->properties[$key], $value, $propertyTree);
}else
{
$device[$key] = array();
iterateDevice($topic, $fn, $parentDevice, $device[$key], $value, $propertyTree);
$device->properties[$key] = array();
iterateDevice($topic, $fn, $parentDevice, $device->properties[$key], $value, $propertyTree);
}
}else
@ -208,7 +210,7 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro
}
}else
{
$device->{$key} = array("value" => null);
$device->$key = array("value" => null);
$device->$key["functions"] = array();
}
//echo $key . ' ===> oldvalue = ' . $oldValue . " value = " . $value . EOL;
@ -219,16 +221,15 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro
//$changed[$fn]["value"] = $value;
logger(INFO, sprintf(_("Device %s property %s, %s"), $fn, $propertyTree . $key, bool2string($value)), __FILE__ . ":" . __LINE__);
$mohaDB->logProperty($parentDevice, $propertyTree . $key, $value, $oldValue);
if (!empty($device->$key["functions"]))
{
logger(DEBUG,_("executing notifications functions"), __FILE__ . ":" . __LINE__);
foreach($device->$key["functions"] as $function)
if (!empty($device->$key["functions"]))
{
$function($device, $key, $value);
logger(DEBUG,_("executing notifications functions"), __FILE__ . ":" . __LINE__);
foreach($device->properties[$key]["functions"] as $function)
{
$function($device, $key, $value);
}
}
}
}
//}
}
}
@ -245,25 +246,24 @@ function getDevicesValues($topic)
logger(DEBUG, "device: " . $device->friendlyName);
$payload = "{";
$flag = false;
$properties = array_slice($device, 12);
logger(DEBUG, print_r($properties, true));
foreach($properties as $property)
//$properties = array_slice($device, 12);
//logger(DEBUG, print_r($properties, true));
foreach($device->properties as $property => $value)
{
if (flag)
if ($flag)
{
$payload = ",";
$payload .= ",";
}else {
$flag = true;
}
$payload .= $property . ':""';
$payload .= '"' . $property . '":""';
}
$device->payload = $payload . "}";
logger(DEBUG, $device->payload);
$device->get();
}
}
exit(0);
}
?>