1
0

- indexFriendlyName modified\n- debugging

This commit is contained in:
daniel Tartavel 2022-02-24 21:46:10 +01:00
parent 34dca81ba5
commit f6b7b9e664
7 changed files with 41 additions and 145 deletions

View File

@ -43,6 +43,7 @@ class db extends mysqli
if (array_key_exists($property, $properties2log)) if (array_key_exists($property, $properties2log))
{ {
//echo "logging in database"; //echo "logging in database";
//var_dump($device);
$ieeeAddress = $device->ieeeAddress; $ieeeAddress = $device->ieeeAddress;
//print_r($ieeeAddress); //print_r($ieeeAddress);
$query = "INSERT INTO logs (device, property, value) VALUES('" . $this->protect($ieeeAddress) . "', '" . $this->protect($property) . "', '" . $this->protect($value) . "')"; $query = "INSERT INTO logs (device, property, value) VALUES('" . $this->protect($ieeeAddress) . "', '" . $this->protect($property) . "', '" . $this->protect($value) . "')";

View File

@ -31,7 +31,6 @@ class device
public $description; public $description;
public $functions; public $functions;
public $payload; public $payload;
public $availability;
public $toConfirm; public $toConfirm;
public $triggerDevice; public $triggerDevice;
public $properties = array(); public $properties = array();

View File

@ -53,22 +53,22 @@ function mkDevicesDB($topic, $json, $group=false)
//print_r($device); //print_r($device);
$device["device"]->groupID = $jsonDevice->id; $device["device"]->groupID = $jsonDevice->id;
$indexDevices[$device["device"]->groupID] = & $device["device"]; $indexDevices[$device["device"]->groupID] = & $device["device"];
$indexFriendlyNames[$fn] = & $device["device"]; $indexFriendlyNames[$topic][$fn] = & $device["device"];
}else }else
{ {
addDevice($device["device"], $fn, $jsonDevice); addDevice($topic, $device["device"], $fn, $jsonDevice);
} }
} }
$dbInit += 1; $dbInit += 1;
fwrite($logFh, "################################START##################################################"); fwrite($logFh, "################################START##################################################");
fwrite($logFh, var_export($devices, true)); fwrite($logFh, var_export($devices, true));
fwrite($logFh, "################################END##################################################"); fwrite($logFh, "################################END##################################################");
fwrite($logFh, var_export($indexFriendlyNames, true));
logger(DEBUG, _("Devices DB made"), __FILE__ . ":" . __LINE__); logger(DEBUG, _("Devices DB made"), __FILE__ . ":" . __LINE__);
//print_r($devices); //print_r($devices);
} }
function addDevice(& $device, $fn, $jsonDevice ) function addDevice($topic, & $device, $fn, $jsonDevice )
{ {
global $listProperties, $listPropertiesKeys, $hooks, $indexDevices, $indexFriendlyNames; global $listProperties, $listPropertiesKeys, $hooks, $indexDevices, $indexFriendlyNames;
$device->type = $jsonDevice->type; $device->type = $jsonDevice->type;
@ -86,7 +86,7 @@ function addDevice(& $device, $fn, $jsonDevice )
//indexing device //indexing device
$indexDevices[$device->ieeeAddress] = & $device; $indexDevices[$device->ieeeAddress] = & $device;
$indexFriendlyNames[$fn] = & $device; $indexFriendlyNames[$topic][$fn] = & $device;
} }
function searchPropertyKey($fn, &$device, $inputObject, $listPropertiesKeys) function searchPropertyKey($fn, &$device, $inputObject, $listPropertiesKeys)
@ -157,18 +157,18 @@ function changeDevice($topic, $fn, &$device, $payloadArray)
{ {
//echo "==================== New ChangeDevice =====================" .EOL; //echo "==================== New ChangeDevice =====================" .EOL;
iterateDevice($topic, $fn, $device, $device, $payloadArray); iterateDevice($topic, $fn, $device, $device->properties, $payloadArray);
}else }else
{ {
logger(ERROR, $fn . _(" => payloadArray is empty!"), __FILE__ . ":" . __LINE__); logger(ERROR, $fn . _(" => payloadArray is empty!"), __FILE__ . ":" . __LINE__);
} }
} }
function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $propertyTree="") function iterateDevice($topic, $fn, $parentDevice, &$properties, $payloadArray, $propertyTree="")
{ {
global $changed, $mohaDB, $testMode; global $changed, $mohaDB, $testMode;
$deviceType = (is_a($device, "device")); // = true if object //if (is_a($device, "device")) // = true if object
//echo "==================== New iterate =====================" .EOL; //echo "==================== New iterate =====================" .EOL;
//echo "deviceType = "; var_dump($deviceType); echo EOL; //echo "deviceType = "; var_dump($deviceType); echo EOL;
//echo "device =>";print_r($device);echo EOL; //echo "device =>";print_r($device);echo EOL;
@ -180,123 +180,23 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro
//echo "value =>"; var_dump($value); echo EOL; //echo "value =>"; var_dump($value); echo EOL;
//echo "type : " . gettype($value) .EOL; //echo "type : " . gettype($value) .EOL;
$valueType = gettype($value); $valueType = gettype($value);
if ($valueType == "object")
{
//logger(DEBUG, _("valueType == object"), __FILE__ . ":" . __LINE__);
if ($valueType == "array")
{
logger(DEBUG,_("valueType == array"), __FILE__ . ":" . __LINE__);
$propertyTree .= $key . "/"; $propertyTree .= $key . "/";
//echo "PropertyTree " . $propertyTree . EOL; $properties[$key] = array();
//echo " is Object" . EOL; iterateDevice($topic, $fn, $parentDevice, $properties[$key], $value, $propertyTree);
if ($deviceType === true )
{
//echo "deviceType = true" . EOL;
if (!array_key_exists($key, $device->properties))
{
//echo "Property do not exists" . EOL;
$device->properties[$key] = new stdClass;
}
//echo "iterating" . EOL;
//echo "===============>";
iterateDevice($topic, $fn, $parentDevice, $device->properties[$key], $value, $propertyTree);
}elseif(is_array($device))
{
//echo "is array";
if (!array_key_exists($key, $device))
{
$device[$key] = new stdClass;
}
//echo "iterating" . EOL;
iterateDevice($topic, $fn, $parentDevice, $device[$key], $value, $propertyTree);
}elseif(is_object($device))
{
if (!property_exists($device, $key))
{
$device->$key = new stdClass;
}
iterateDevice($topic, $fn, $parentDevice, $device->$key, $value, $propertyTree);
}
}elseif ($valueType == "array")
{
//logger(DEBUG,_("valueType == array"), __FILE__ . ":" . __LINE__);
$propertyTree .= $key . "/";
if ($deviceType === true )
{
$device->properties[$key] = array();
iterateDevice($topic, $fn, $parentDevice, $device->properties[$key], $value, $propertyTree);
}elseif(is_array($device))
{
$device[$key] = array();
iterateDevice($topic, $fn, $parentDevice, $device[$key], $value, $propertyTree);
}elseif(is_object($device))
{
$device->$key = array();
iterateDevice($topic, $fn, $parentDevice, $device->$key, $value, $propertyTree);
}
}else }else
{ {
//logger(DEBUG,_("valueType is ") . $valueType, __FILE__ . ":" . __LINE__); logger(DEBUG,_("valueType is ") . $valueType, __FILE__ . ":" . __LINE__);
//var_dump($properties);echo EOL;
//var_dump($device);echo EOL; if (!array_key_exists($key, $properties))
if (is_a($device, "device"))
{ {
//logger(DEBUG,_("This is an object 'device'"), __FILE__ . ":" . __LINE__); $properties[$key] = array("value" => $value);
if(array_key_exists($key, $device->properties)) $properties[$key]["functions"] = array();
{
if ($device->properties[$key]["value"] != $value)
{
changeValue($device->properties[$key]["value"], $value, $parentDevice, $propertyTree, $key);
}
}else
{
$device->properties[$key] = array("value" => $value);
$device->properties[$key]["functions"] = array();
}
}elseif (is_array($device))
{
//logger(DEBUG,_("deviceType") . $deviceType, __FILE__ . ":" . __LINE__);
if (array_key_exists($key, $device))
{
if (is_array($device[$key]))
{
if ($device[$key]["value"] != $value)
{
changeValue($device->$key["value"], $value, $parentDevice, $propertyTree, $key);
}
}else
{
changeValue($device[$key], $value, $parentDevice, $propertyTree, $key);
}
}else
{
$device[$key] = array("value" => $value);
$device[$key]["functions"] = array();
}
}elseif (is_object($device))
{
if(property_exists($device, $key))
{
if (is_array($device->$key))
{
if ($device->$key["value"] != $value)
{
changeValue($device->$key["value"], $value, $parentDevice, $propertyTree, $key);
}
}else
{
if ($device->$key != $value)
{
changeValue($device->$key, $value, $parentDevice, $propertyTree, $key);
}
}
}else
{
$device->$key = array("value" => $value);
$device->$key["functions"] = array();
}
} }
changeValue($properties[$key], $value, $parentDevice, $propertyTree, $key);
} }
} }
} }
@ -331,11 +231,10 @@ function getDevicesValues($topic)
} }
} }
function getDeviceByFriendlyname(&$device, $topic, $fn, $payloadArray, $create = false) /*function getDeviceByFriendlyname(&$device, $topic, $fn, $payloadArray, $create = false)
{ {
global $devices, $indexDevices, $indexFriendlyNames; global $devices, $indexDevices, $indexFriendlyNames;
$n = explode("/", $fn); $n = explode("/", $fn);
//echo "topic => ". $topic . EOL;
$device = &$devices[$topic]; $device = &$devices[$topic];
foreach($n as $value) foreach($n as $value)
{ {
@ -352,7 +251,6 @@ function getDeviceByFriendlyname(&$device, $topic, $fn, $payloadArray, $create =
logger(ERROR, sprintf(_(" device with friendlyname %s not found"), $fn), __FILE__ . ":" . __LINE__); logger(ERROR, sprintf(_(" device with friendlyname %s not found"), $fn), __FILE__ . ":" . __LINE__);
return false; return false;
} }
} }
if (! array_key_exists("device", $device)) if (! array_key_exists("device", $device))
{ {
@ -364,28 +262,28 @@ function getDeviceByFriendlyname(&$device, $topic, $fn, $payloadArray, $create =
$device["device"]->ieeeAddress = $payloadArray->ieeeAddress; $device["device"]->ieeeAddress = $payloadArray->ieeeAddress;
$device["device"]->friendlyname = $fn; $device["device"]->friendlyname = $fn;
$indexDevices[$device["device"]->ieeeAddress] = & $device["device"]; $indexDevices[$device["device"]->ieeeAddress] = & $device["device"];
$indexFriendlyNames[$fn] = & $device["device"]; $indexFriendlyNames[$topic][$fn] = & $device["device"];
} }
}else }else
{ {
logger(INFO, sprintf(_(" device with friendlyname %s exists"), $fn), __FILE__ . ":" . __LINE__); logger(INFO, sprintf(_(" device with friendlyname %s exists"), $fn), __FILE__ . ":" . __LINE__);
} }
//var_dump($device);
return true; return true;
} }*/
function changeValue(&$oldValue, $value, &$parentDevice, $propertyTree, $key) function changeValue(&$property, $value, &$parentDevice, $propertyTree, $key)
{ {
global $mohaDB; global $mohaDB;
$object = $parentDevice->properties[$key];
//$changed[$fn]["key"] = $key; //$changed[$fn]["key"] = $key;
//$changed[$fn]["value"] = $value; //$changed[$fn]["value"] = $value;
logger(INFO, sprintf(_("Logging Device %s property %s, %s"), $parentDevice->friendlyName, $propertyTree . $key, bool2string($value)), __FILE__ . ":" . __LINE__); logger(INFO, sprintf(_("Logging Device property %s, %s"), $propertyTree . $key, bool2string($value)), __FILE__ . ":" . __LINE__);
$mohaDB->logProperty($parentDevice, $key, $value, $oldValue); $mohaDB->logProperty($parentDevice, $key, $value, $property["value"]);
$oldvalue = $value; $property["value"] = $value;
if (!empty($object["functions"])) if (!empty($property["functions"]))
{ {
logger(DEBUG,_("executing notifications functions"), __FILE__ . ":" . __LINE__); logger(DEBUG,_("executing notifications functions"), __FILE__ . ":" . __LINE__);
foreach($object["functions"] as $function) foreach($property["functions"] as $function)
{ {
$function($parentDevice, $key, $value); $function($parentDevice, $key, $value);
} }

View File

@ -17,7 +17,7 @@ class availability
global $indexDevices; global $indexDevices;
foreach ($indexDevices as $value) foreach ($indexDevices as $value)
{ {
$value->availability["functions"][] = array($this,"callback"); $value->properties["availability"]["functions"][] = array($this,"callback");
} }
} }

View File

@ -21,8 +21,7 @@ $topics["linky2mqtt"]->callback = function($topic, $message)
$device["device"]->ieeeAddress = $payloadArray["ieeeAddress"]; $device["device"]->ieeeAddress = $payloadArray["ieeeAddress"];
$device["device"]->type = $payloadArray["type"]; $device["device"]->type = $payloadArray["type"];
$indexDevices[$device["device"]->ieeeAddress] = & $device["device"]; $indexDevices[$device["device"]->ieeeAddress] = & $device["device"];
$indexFriendlyNames[$fn] = & $device["device"]; $indexFriendlyNames[$topicName][$fn] = & $device["device"];
}else }else
{ {
$device = &$device[$fn]; $device = &$device[$fn];

View File

@ -22,7 +22,7 @@ $topics["pws2mqtt"]->callback = function($topic, $message)
$device["device"]->type = $payloadArray["type"]; $device["device"]->type = $payloadArray["type"];
$device["device"]->topic = $topicName; $device["device"]->topic = $topicName;
$indexDevices[$device["device"]->ieeeAddress] = & $device["device"]; $indexDevices[$device["device"]->ieeeAddress] = & $device["device"];
$indexFriendlyNames[$fn] = & $device["device"]; $indexFriendlyNames[$topicName][$fn] = & $device["device"];
}else }else
{ {

View File

@ -5,7 +5,7 @@ $devices["zigbee2mqtt"] = array();
$topics["zigbee2mqtt"]->callback = function($topic, $message) $topics["zigbee2mqtt"]->callback = function($topic, $message)
{ {
global $topics, $devices, $included, $logFh; global $topics, $devices, $included, $logFh, $indexFriendlyNames;
if ($topic[1] == "bridge") if ($topic[1] == "bridge")
{ {
@ -46,7 +46,7 @@ $topics["zigbee2mqtt"]->callback = function($topic, $message)
$topic = explode ("/", $message->topic, 2); // get topic name $topic = explode ("/", $message->topic, 2); // get topic name
$topicName = $topic[0]; $topicName = $topic[0];
$fn = $topic[1]; $fn = $topic[1];
$device = null; $device = new device;
$fnTree = explode ("/" , $topic[1]); // get friendlyname $fnTree = explode ("/" , $topic[1]); // get friendlyname
echo $topicName . " => " . $fn . EOL; echo $topicName . " => " . $fn . EOL;
@ -64,25 +64,24 @@ $topics["zigbee2mqtt"]->callback = function($topic, $message)
$fn = implode("/", $fnTree); $fn = implode("/", $fnTree);
$fn = implode("/", $fnTree); $fn = implode("/", $fnTree);
$payloadArray = array($fnTreeEnd => $message->payload); $payloadArray = array($fnTreeEnd => $message->payload);
//TODO payl
$payloadArray = array($fnTreeEnd => $message->payload);
//TODO payload can be a json object //TODO payload can be a json object
break; break;
default: default:
$payloadArray = json_decode($message->payload,true); $payloadArray = json_decode($message->payload,true);
} }
logger(DEBUG, _("friendlyname = ") . $fn, __FILE__ . ":" . __LINE__); logger(DEBUG, _("friendlyname = ") . $fn, __FILE__ . ":" . __LINE__);
if ((getDeviceByFriendlyname($device, $topicName, $fn, $payloadArray, true)) === false) if (!array_key_exists($fn, $indexFriendlyNames[$topicName]))
{ {
logger(ERROR, sprintf(_("device with friendlyname %s not found"), $fn), __FILE__ . ":" . __LINE__); logger(ERROR, sprintf(_("device with friendlyname %s not found"), $fn), __FILE__ . ":" . __LINE__);
}else
{
changeDevice($topicName, $fn, $indexFriendlyNames[$topicName][$fn], $payloadArray);
} }
print_r($device);
changeDevice($topicName, $fn, $device["device"], $payloadArray);
}else }else
{ {
logger(DEBUG, _("Zigbee2mqtt doing nothing !?")); logger(DEBUG, _("Zigbee2mqtt doing nothing !?"));
} }
}; };