From 8887e13d2eb2a4080a78505ae92663d1ef583b8d Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Tue, 6 Apr 2021 23:19:14 +0200 Subject: [PATCH] Add parentId member to pyThingDescriptor --- libnymea-core/integrations/python/pything.h | 10 ++++++++++ .../integrations/python/pythingdescriptor.h | 14 +++++++++++--- .../integrations/python/pythingdiscoveryinfo.h | 6 +++++- .../integrations/pythonintegrationplugin.cpp | 6 +++++- 4 files changed, 31 insertions(+), 5 deletions(-) diff --git a/libnymea-core/integrations/python/pything.h b/libnymea-core/integrations/python/pything.h index 1678e993..775a279a 100644 --- a/libnymea-core/integrations/python/pything.h +++ b/libnymea-core/integrations/python/pything.h @@ -40,6 +40,7 @@ typedef struct _thing { ThingClass *thingClass = nullptr; // A copy of the thing class. This is owned by the python thread PyObject *pyId = nullptr; PyObject *pyThingClassId = nullptr; + PyObject *pyParentId = nullptr; PyObject *pyName = nullptr; PyObject *pyParams = nullptr; PyObject *pySettings = nullptr; @@ -70,6 +71,7 @@ static void PyThing_setThing(PyThing *self, Thing *thing, PyThreadState *threadS self->pyId = PyUnicode_FromString(self->thing->id().toString().toUtf8().data()); self->pyThingClassId = PyUnicode_FromString(self->thing->thingClassId().toString().toUtf8().data()); + self->pyParentId = PyUnicode_FromString(self->thing->parentId().toString().toUtf8().data()); self->pyName = PyUnicode_FromString(self->thing->name().toUtf8().data()); self->pyParams = PyParams_FromParamList(self->thing->params()); self->pySettings = PyParams_FromParamList(self->thing->settings()); @@ -141,6 +143,7 @@ static void PyThing_dealloc(PyThing * self) { qCDebug(dcPythonIntegrations()) << "--- PyThing" << self; Py_XDECREF(self->pyId); Py_XDECREF(self->pyThingClassId); + Py_XDECREF(self->pyParentId); Py_XDECREF(self->pyName); Py_XDECREF(self->pyParams); Py_XDECREF(self->pySettings); @@ -169,6 +172,12 @@ static PyObject *PyThing_getThingClassId(PyThing *self, void */*closure*/) return self->pyThingClassId; } +static PyObject *PyThing_getParentId(PyThing *self, void */*closure*/) +{ + Py_INCREF(self->pyParentId); + return self->pyParentId; +} + static int PyThing_setName(PyThing *self, PyObject *value, void */*closure*/){ QString name = QString(PyUnicode_AsUTF8(value)); if (!self->thing) { @@ -337,6 +346,7 @@ static PyGetSetDef PyThing_getset[] = { {"name", (getter)PyThing_getName, (setter)PyThing_setName, "Thing name", nullptr}, {"id", (getter)PyThing_getId, 0, "ThingId", nullptr}, {"thingClassId", (getter)PyThing_getThingClassId, 0, "ThingClassId", nullptr}, + {"parentId", (getter)PyThing_getParentId, 0, "Parent thing id", nullptr}, {"settings", (getter)PyThing_getSettings, (setter)PyThing_setSettings, "Thing settings", nullptr}, {nullptr , nullptr, nullptr, nullptr, nullptr} /* Sentinel */ }; diff --git a/libnymea-core/integrations/python/pythingdescriptor.h b/libnymea-core/integrations/python/pythingdescriptor.h index e27d0440..bbcc9e55 100644 --- a/libnymea-core/integrations/python/pythingdescriptor.h +++ b/libnymea-core/integrations/python/pythingdescriptor.h @@ -19,6 +19,7 @@ typedef struct { PyObject* pyName; PyObject* pyDescription; PyObject* pyThingId; + PyObject* pyParentId; PyObject* pyParams; } PyThingDescriptor; @@ -28,17 +29,18 @@ static PyMemberDef PyThingDescriptor_members[] = { {"name", T_OBJECT_EX, offsetof(PyThingDescriptor, pyName), 0, "Descriptor name"}, {"description", T_OBJECT_EX, offsetof(PyThingDescriptor, pyDescription), 0, "Descriptor description"}, {"thingId", T_OBJECT_EX, offsetof(PyThingDescriptor, pyDescription), 0, "The thingId, if there exists a thing for this descriptor already."}, + {"parentId", T_OBJECT_EX, offsetof(PyThingDescriptor, pyParentId), 0, "The thingId of the parent, if this thing is a child."}, {"params", T_OBJECT_EX, offsetof(PyThingDescriptor, pyParams), 0, "Params for the thing described by this descriptor."}, {nullptr, 0, 0, 0, nullptr} /* Sentinel */ }; static int PyThingDescriptor_init(PyThingDescriptor *self, PyObject *args, PyObject *kwds) { - static char *kwlist[] = {"thingClassId", "name", "description", "thingId", "params", nullptr}; - PyObject *thingClassId = nullptr, *name = nullptr, *description = nullptr, *thingId = nullptr, *params = nullptr; + static char *kwlist[] = {"thingClassId", "name", "description", "thingId", "parentId", "params", nullptr}; + PyObject *thingClassId = nullptr, *name = nullptr, *description = nullptr, *thingId = nullptr, *parentId = nullptr, *params = nullptr; qCDebug(dcPythonIntegrations()) << "+++ PyThingDescriptor"; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOOO", kwlist, &thingClassId, &name, &description, &thingId, ¶ms)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOOOO", kwlist, &thingClassId, &name, &description, &thingId, &parentId, ¶ms)) return -1; if (thingClassId) { @@ -57,6 +59,11 @@ static int PyThingDescriptor_init(PyThingDescriptor *self, PyObject *args, PyObj Py_INCREF(thingId); self->pyThingId = thingId; } + if (parentId) { + Py_INCREF(parentId); + qCritical() << "++++" << ThingId(PyUnicode_AsUTF8(parentId)); + self->pyParentId = parentId; + } if (params) { Py_INCREF(params); self->pyParams = params; @@ -71,6 +78,7 @@ static void PyThingDescriptor_dealloc(PyThingDescriptor * self) Py_XDECREF(self->pyName); Py_XDECREF(self->pyDescription); Py_XDECREF(self->pyThingId); + Py_XDECREF(self->pyParentId); Py_XDECREF(self->pyParams); Py_TYPE(self)->tp_free(self); } diff --git a/libnymea-core/integrations/python/pythingdiscoveryinfo.h b/libnymea-core/integrations/python/pythingdiscoveryinfo.h index 2b6a4e56..abd1d24e 100644 --- a/libnymea-core/integrations/python/pythingdiscoveryinfo.h +++ b/libnymea-core/integrations/python/pythingdiscoveryinfo.h @@ -106,6 +106,10 @@ static PyObject * PyThingDiscoveryInfo_addDescriptor(PyThingDiscoveryInfo* self, if (pyDescriptor->pyThingClassId) { thingClassId = ThingClassId(PyUnicode_AsUTF8(pyDescriptor->pyThingClassId)); } + ThingId parentId; + if (pyDescriptor->pyParentId) { + parentId = ThingId(PyUnicode_AsUTF8(pyDescriptor->pyParentId)); + } QString name; if (pyDescriptor->pyName) { name = QString::fromUtf8(PyUnicode_AsUTF8(pyDescriptor->pyName)); @@ -115,7 +119,7 @@ static PyObject * PyThingDiscoveryInfo_addDescriptor(PyThingDiscoveryInfo* self, description = QString::fromUtf8(PyUnicode_AsUTF8(pyDescriptor->pyDescription)); } - ThingDescriptor descriptor(thingClassId, name, description); + ThingDescriptor descriptor(thingClassId, name, description, parentId); if (pyDescriptor->pyThingId) { descriptor.setThingId(ThingId(QString::fromUtf8(PyUnicode_AsUTF8(pyDescriptor->pyThingId)))); } diff --git a/libnymea-core/integrations/pythonintegrationplugin.cpp b/libnymea-core/integrations/pythonintegrationplugin.cpp index 5bb711f2..85ca21c9 100644 --- a/libnymea-core/integrations/pythonintegrationplugin.cpp +++ b/libnymea-core/integrations/pythonintegrationplugin.cpp @@ -135,6 +135,10 @@ PyObject *PythonIntegrationPlugin::pyAutoThingsAppeared(PyObject *self, PyObject if (pyDescriptor->pyThingClassId) { thingClassId = ThingClassId(PyUnicode_AsUTF8(pyDescriptor->pyThingClassId)); } + ThingId parentId; + if (pyDescriptor->pyParentId) { + parentId = ThingId(PyUnicode_AsUTF8(pyDescriptor->pyParentId)); + } QString name; if (pyDescriptor->pyName) { name = QString::fromUtf8(PyUnicode_AsUTF8(pyDescriptor->pyName)); @@ -144,7 +148,7 @@ PyObject *PythonIntegrationPlugin::pyAutoThingsAppeared(PyObject *self, PyObject description = QString::fromUtf8(PyUnicode_AsUTF8(pyDescriptor->pyDescription)); } - ThingDescriptor descriptor(thingClassId, name, description); + ThingDescriptor descriptor(thingClassId, name, description, parentId); if (pyDescriptor->pyParams) { descriptor.setParams(PyParams_ToParamList(pyDescriptor->pyParams));