Merge PR #411: Add parentId member to pyThingDescriptor

This commit is contained in:
Jenkins nymea 2021-04-19 11:38:31 +02:00
commit e43af9a49d
4 changed files with 31 additions and 5 deletions

View File

@ -40,6 +40,7 @@ typedef struct _thing {
ThingClass *thingClass = nullptr; // A copy of the thing class. This is owned by the python thread ThingClass *thingClass = nullptr; // A copy of the thing class. This is owned by the python thread
PyObject *pyId = nullptr; PyObject *pyId = nullptr;
PyObject *pyThingClassId = nullptr; PyObject *pyThingClassId = nullptr;
PyObject *pyParentId = nullptr;
PyObject *pyName = nullptr; PyObject *pyName = nullptr;
PyObject *pyParams = nullptr; PyObject *pyParams = nullptr;
PyObject *pySettings = 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->pyId = PyUnicode_FromString(self->thing->id().toString().toUtf8().data());
self->pyThingClassId = PyUnicode_FromString(self->thing->thingClassId().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->pyName = PyUnicode_FromString(self->thing->name().toUtf8().data());
self->pyParams = PyParams_FromParamList(self->thing->params()); self->pyParams = PyParams_FromParamList(self->thing->params());
self->pySettings = PyParams_FromParamList(self->thing->settings()); self->pySettings = PyParams_FromParamList(self->thing->settings());
@ -141,6 +143,7 @@ static void PyThing_dealloc(PyThing * self) {
qCDebug(dcPythonIntegrations()) << "--- PyThing" << self; qCDebug(dcPythonIntegrations()) << "--- PyThing" << self;
Py_XDECREF(self->pyId); Py_XDECREF(self->pyId);
Py_XDECREF(self->pyThingClassId); Py_XDECREF(self->pyThingClassId);
Py_XDECREF(self->pyParentId);
Py_XDECREF(self->pyName); Py_XDECREF(self->pyName);
Py_XDECREF(self->pyParams); Py_XDECREF(self->pyParams);
Py_XDECREF(self->pySettings); Py_XDECREF(self->pySettings);
@ -169,6 +172,12 @@ static PyObject *PyThing_getThingClassId(PyThing *self, void */*closure*/)
return self->pyThingClassId; 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*/){ static int PyThing_setName(PyThing *self, PyObject *value, void */*closure*/){
QString name = QString(PyUnicode_AsUTF8(value)); QString name = QString(PyUnicode_AsUTF8(value));
if (!self->thing) { if (!self->thing) {
@ -337,6 +346,7 @@ static PyGetSetDef PyThing_getset[] = {
{"name", (getter)PyThing_getName, (setter)PyThing_setName, "Thing name", nullptr}, {"name", (getter)PyThing_getName, (setter)PyThing_setName, "Thing name", nullptr},
{"id", (getter)PyThing_getId, 0, "ThingId", nullptr}, {"id", (getter)PyThing_getId, 0, "ThingId", nullptr},
{"thingClassId", (getter)PyThing_getThingClassId, 0, "ThingClassId", 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}, {"settings", (getter)PyThing_getSettings, (setter)PyThing_setSettings, "Thing settings", nullptr},
{nullptr , nullptr, nullptr, nullptr, nullptr} /* Sentinel */ {nullptr , nullptr, nullptr, nullptr, nullptr} /* Sentinel */
}; };

View File

@ -19,6 +19,7 @@ typedef struct {
PyObject* pyName; PyObject* pyName;
PyObject* pyDescription; PyObject* pyDescription;
PyObject* pyThingId; PyObject* pyThingId;
PyObject* pyParentId;
PyObject* pyParams; PyObject* pyParams;
} PyThingDescriptor; } PyThingDescriptor;
@ -28,17 +29,18 @@ static PyMemberDef PyThingDescriptor_members[] = {
{"name", T_OBJECT_EX, offsetof(PyThingDescriptor, pyName), 0, "Descriptor name"}, {"name", T_OBJECT_EX, offsetof(PyThingDescriptor, pyName), 0, "Descriptor name"},
{"description", T_OBJECT_EX, offsetof(PyThingDescriptor, pyDescription), 0, "Descriptor description"}, {"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."}, {"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."}, {"params", T_OBJECT_EX, offsetof(PyThingDescriptor, pyParams), 0, "Params for the thing described by this descriptor."},
{nullptr, 0, 0, 0, nullptr} /* Sentinel */ {nullptr, 0, 0, 0, nullptr} /* Sentinel */
}; };
static int PyThingDescriptor_init(PyThingDescriptor *self, PyObject *args, PyObject *kwds) static int PyThingDescriptor_init(PyThingDescriptor *self, PyObject *args, PyObject *kwds)
{ {
static char *kwlist[] = {"thingClassId", "name", "description", "thingId", "params", nullptr}; static char *kwlist[] = {"thingClassId", "name", "description", "thingId", "parentId", "params", nullptr};
PyObject *thingClassId = nullptr, *name = nullptr, *description = nullptr, *thingId = nullptr, *params = nullptr; PyObject *thingClassId = nullptr, *name = nullptr, *description = nullptr, *thingId = nullptr, *parentId = nullptr, *params = nullptr;
qCDebug(dcPythonIntegrations()) << "+++ PyThingDescriptor"; qCDebug(dcPythonIntegrations()) << "+++ PyThingDescriptor";
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOOO", kwlist, &thingClassId, &name, &description, &thingId, &params)) if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOOOO", kwlist, &thingClassId, &name, &description, &thingId, &parentId, &params))
return -1; return -1;
if (thingClassId) { if (thingClassId) {
@ -57,6 +59,11 @@ static int PyThingDescriptor_init(PyThingDescriptor *self, PyObject *args, PyObj
Py_INCREF(thingId); Py_INCREF(thingId);
self->pyThingId = thingId; self->pyThingId = thingId;
} }
if (parentId) {
Py_INCREF(parentId);
qCritical() << "++++" << ThingId(PyUnicode_AsUTF8(parentId));
self->pyParentId = parentId;
}
if (params) { if (params) {
Py_INCREF(params); Py_INCREF(params);
self->pyParams = params; self->pyParams = params;
@ -71,6 +78,7 @@ static void PyThingDescriptor_dealloc(PyThingDescriptor * self)
Py_XDECREF(self->pyName); Py_XDECREF(self->pyName);
Py_XDECREF(self->pyDescription); Py_XDECREF(self->pyDescription);
Py_XDECREF(self->pyThingId); Py_XDECREF(self->pyThingId);
Py_XDECREF(self->pyParentId);
Py_XDECREF(self->pyParams); Py_XDECREF(self->pyParams);
Py_TYPE(self)->tp_free(self); Py_TYPE(self)->tp_free(self);
} }

View File

@ -106,6 +106,10 @@ static PyObject * PyThingDiscoveryInfo_addDescriptor(PyThingDiscoveryInfo* self,
if (pyDescriptor->pyThingClassId) { if (pyDescriptor->pyThingClassId) {
thingClassId = ThingClassId(PyUnicode_AsUTF8(pyDescriptor->pyThingClassId)); thingClassId = ThingClassId(PyUnicode_AsUTF8(pyDescriptor->pyThingClassId));
} }
ThingId parentId;
if (pyDescriptor->pyParentId) {
parentId = ThingId(PyUnicode_AsUTF8(pyDescriptor->pyParentId));
}
QString name; QString name;
if (pyDescriptor->pyName) { if (pyDescriptor->pyName) {
name = QString::fromUtf8(PyUnicode_AsUTF8(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)); description = QString::fromUtf8(PyUnicode_AsUTF8(pyDescriptor->pyDescription));
} }
ThingDescriptor descriptor(thingClassId, name, description); ThingDescriptor descriptor(thingClassId, name, description, parentId);
if (pyDescriptor->pyThingId) { if (pyDescriptor->pyThingId) {
descriptor.setThingId(ThingId(QString::fromUtf8(PyUnicode_AsUTF8(pyDescriptor->pyThingId)))); descriptor.setThingId(ThingId(QString::fromUtf8(PyUnicode_AsUTF8(pyDescriptor->pyThingId))));
} }

View File

@ -137,6 +137,10 @@ PyObject *PythonIntegrationPlugin::pyAutoThingsAppeared(PyObject *self, PyObject
if (pyDescriptor->pyThingClassId) { if (pyDescriptor->pyThingClassId) {
thingClassId = ThingClassId(PyUnicode_AsUTF8(pyDescriptor->pyThingClassId)); thingClassId = ThingClassId(PyUnicode_AsUTF8(pyDescriptor->pyThingClassId));
} }
ThingId parentId;
if (pyDescriptor->pyParentId) {
parentId = ThingId(PyUnicode_AsUTF8(pyDescriptor->pyParentId));
}
QString name; QString name;
if (pyDescriptor->pyName) { if (pyDescriptor->pyName) {
name = QString::fromUtf8(PyUnicode_AsUTF8(pyDescriptor->pyName)); name = QString::fromUtf8(PyUnicode_AsUTF8(pyDescriptor->pyName));
@ -146,7 +150,7 @@ PyObject *PythonIntegrationPlugin::pyAutoThingsAppeared(PyObject *self, PyObject
description = QString::fromUtf8(PyUnicode_AsUTF8(pyDescriptor->pyDescription)); description = QString::fromUtf8(PyUnicode_AsUTF8(pyDescriptor->pyDescription));
} }
ThingDescriptor descriptor(thingClassId, name, description); ThingDescriptor descriptor(thingClassId, name, description, parentId);
if (pyDescriptor->pyParams) { if (pyDescriptor->pyParams) {
descriptor.setParams(PyParams_ToParamList(pyDescriptor->pyParams)); descriptor.setParams(PyParams_ToParamList(pyDescriptor->pyParams));