1
0

- changed webserver to apiserver

- added webserver
- a lot of debugging
- install shell script
This commit is contained in:
2022-04-07 01:44:17 +02:00
parent 0d35b1ff3f
commit f8fc3f63ec
28 changed files with 463 additions and 223 deletions

View File

@ -40,36 +40,8 @@ function mkDevicesDB($topic, $json, $group=false)
//print_r($json);
foreach ($json as $jsonDevice)
{
$fn = $jsonDevice->friendly_name;
$fnTree = explode("/", $fn);
$device = & $devices[$topic];
foreach($fnTree as $fnPart)
{
if (!array_key_exists($fnPart, $device))
{
$device[$fnPart] = array();
}
$device = & $device[$fnPart];
}
if (!array_key_exists("device", $device))
{
$device["device"] = new device;
}
$device["device"]->topic = $topic;
//$device["device"]->device = $jsonDevice;
$device["device"]->friendlyName = $fn;
if ($group)
{
//print_r($device);
$device["device"]->groupID = $jsonDevice->id;
$device["device"]->ieeeAddress = $jsonDevice->id;
//$indexDevices[$device["device"]->groupID] = & $device["device"];
//$indexFriendlyNames[$topic][$fn] = & $device["device"];
}else
{
addDevice($topic, $device["device"], $fn, $jsonDevice);
}
$fn = $jsonDevice["friendly_name"];
addDevice($topic, $fn, $jsonDevice, $group);
}
$dbInit += 1;
mkIndexes();
@ -80,21 +52,52 @@ function mkDevicesDB($topic, $json, $group=false)
//print_r($devices);
}
function addDevice($topic, &$device, $fn, $jsonDevice )
function addDevice($topic, $fn, $jsonDevice, $group=false )
{
global $listProperties, $listPropertiesKeys, $hooks, $indexDevices, $indexFriendlyNames;
$device->type = $jsonDevice->type;
$device->ieeeAddress = $jsonDevice->ieee_address;
if ( !empty($jsonDevice->power_source ) )
global $devices, $listProperties, $listPropertiesKeys, $hooks, $indexDevices, $indexFriendlyNames;
$fnTree = explode("/", $fn);
$device = & $devices[$topic];
print_r($jsonDevice);
foreach($fnTree as $fnPart)
{
$device->powerSource = $jsonDevice->power_source;
if (!array_key_exists($fnPart, $device))
{
$device[$fnPart] = array();
}
$device = & $device[$fnPart];
}
if ($jsonDevice->definition != null)
if (!array_key_exists("device", $device))
{
$device->description = $jsonDevice->definition->description;
searchPropertyKey($fn, $device, $jsonDevice->definition->exposes, $listPropertiesKeys);
$device["device"] = new device;
}
$device["device"]->topic = $topic;
//$device["device"]->device = $jsonDevice;
$device["device"]->friendlyName = $fn;
if ($group)
{
//print_r($device);
$device["device"]->groupID = $jsonDevice["id"];
$device["device"]->ieeeAddress = $jsonDevice["id"];
//$indexDevices[$device["device"]->groupID] = & $device["device"];
//$indexFriendlyNames[$topic][$fn] = & $device["device"];
}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"];
if (array_key_exists("exposes", $jsonDevice["definition"]))
{
searchPropertyKey($fn, $device["device"], $jsonDevice["definition"]["exposes"], $listPropertiesKeys);
}
}
searchPropertyValue($fn, $device["device"], $jsonDevice, $listProperties);
}
searchPropertyValue($fn, $device, $jsonDevice, $listProperties);
//indexing device
//$indexDevices[$device->ieeeAddress] = & $device;
@ -106,16 +109,18 @@ function searchPropertyKey($fn, &$device, $inputObject, $listPropertiesKeys)
//foreach($listPropertiesKeys as $propertyKey)
//{
logger(DEBUG, _("searching for property"), __FILE__ . ":" . __LINE__ );
if (is_object($inputObject))
{
if (property_exists($inputObject, "property"))
//if (is_object($inputObject))
//{
//print_r($inputObject);
if (array_key_exists("property", $inputObject))
{
logger(DEBUG, _("propertyKey exists filling properties"), __FILE__ . ":" . __LINE__ );
$string = $inputObject->property;
logger(DEBUG, _("property Key exists filling properties"), __FILE__ . ":" . __LINE__ );
$string = $inputObject["property"];
if (!array_key_exists($string, $device->properties))
{
$device->properties[$string]["value"] = null;
$device->properties[$string]["functions"] = array();
$device->properties[$string]["method"] = IDLE;
}
foreach($inputObject as $key2 => $value2)
{
@ -127,25 +132,27 @@ function searchPropertyKey($fn, &$device, $inputObject, $listPropertiesKeys)
}
}else
{
if (property_exists($inputObject, "type"))
if (array_key_exists("type", $inputObject))
{
$device->type = $inputObject->type;
$device->type = $inputObject["type"];
}
foreach($inputObject as $key => $value)
/*foreach($inputObject as $key => $value)
{
logger(DEBUG, sprintf(_("key = %s"), $key), __FILE__ . ":" . __LINE__ );
searchPropertyKey($fn, $device, $value, $listPropertiesKeys);
}
}*/
}
//print_r($device);
}elseif (is_array($inputObject))
//}
/*elseif (is_array($inputObject))
{
foreach($inputObject as $value)
{
logger(DEBUG, _("value is object or group, iterating"), __FILE__ . ":" . __LINE__ );
searchPropertyKey($fn, $device, $value, $listPropertiesKeys);
}
}
}*/
}
@ -209,6 +216,7 @@ function iterateDevice($topic, $fn, &$parentDevice, &$properties, $payloadArray,
{
$properties[$key] = array("value" => $value);
$properties[$key]["functions"] = array();
$properties[$key]["method"] = IDLE;
}elseif ($properties[$key]["value"] !== $value)
{
changeValue($properties[$key], $value, $parentDevice, $propertyTree, $key);
@ -302,16 +310,16 @@ function iterate2device(&$object)
$object = &$device;
//print("=============" . $device->friendlyName);
if (!array_key_exists($object->ieeeAddress, $indexDevices))
{
//if (!array_key_exists($object->ieeeAddress, $indexDevices))
//{
//print("============>");
$indexDevices[$object->ieeeAddress] = &$object;
$indexFriendlyNames[$object->topic][$object->friendlyName] = &$object;
$indexFriendlyNames[$object->friendlyName] = &$object;
if (property_exists($object, "type"))
{
$indexTypes[$object->type][] = &$object;
}
}
//}
return true;
}else
{