mirror of https://github.com/nymea/nymea.git
Clenaup and polish types.
parent
a90841401c
commit
0cbd1ff5ec
|
|
@ -21,12 +21,6 @@ typedef struct _pyparam {
|
|||
PyObject* pyValue = nullptr;
|
||||
} PyParam;
|
||||
|
||||
static void PyParam_dealloc(PyParam * self) {
|
||||
Py_XDECREF(self->pyParamTypeId);
|
||||
Py_XDECREF(self->pyValue);
|
||||
Py_TYPE(self)->tp_free(self);
|
||||
}
|
||||
|
||||
static PyMethodDef PyParam_methods[] = {
|
||||
{nullptr, nullptr, 0, nullptr} // sentinel
|
||||
};
|
||||
|
|
@ -40,6 +34,7 @@ static PyMemberDef PyParam_members[] = {
|
|||
|
||||
static int PyParam_init(PyParam *self, PyObject *args, PyObject *kwds)
|
||||
{
|
||||
qWarning() << "++++ PyParam";
|
||||
static char *kwlist[] = {"paramTypeId", "value", nullptr};
|
||||
PyObject *paramTypeId = nullptr, *value = nullptr;
|
||||
|
||||
|
|
@ -47,18 +42,23 @@ static int PyParam_init(PyParam *self, PyObject *args, PyObject *kwds)
|
|||
return -1;
|
||||
|
||||
if (paramTypeId) {
|
||||
Py_XDECREF(self->pyParamTypeId);
|
||||
Py_INCREF(paramTypeId);
|
||||
self->pyParamTypeId = paramTypeId;
|
||||
}
|
||||
if (value) {
|
||||
Py_XDECREF(self->pyValue);
|
||||
Py_INCREF(value);
|
||||
self->pyValue = value;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void PyParam_dealloc(PyParam * self) {
|
||||
qWarning() << "---- PyParam";
|
||||
Py_XDECREF(self->pyParamTypeId);
|
||||
Py_XDECREF(self->pyValue);
|
||||
Py_TYPE(self)->tp_free(self);
|
||||
}
|
||||
|
||||
static PyTypeObject PyParamType = {
|
||||
PyVarObject_HEAD_INIT(NULL, 0)
|
||||
"nymea.Param", /* tp_name */
|
||||
|
|
@ -86,9 +86,14 @@ static PyTypeObject PyParamType = {
|
|||
|
||||
static PyParam* PyParam_fromParam(const Param ¶m)
|
||||
{
|
||||
PyParam *pyParam = PyObject_New(PyParam, &PyParamType);
|
||||
pyParam->pyParamTypeId = PyUnicode_FromString(param.paramTypeId().toString().toUtf8());
|
||||
pyParam->pyValue = QVariantToPyObject(param.value());
|
||||
PyObject *pyParamValue = QVariantToPyObject(param.value());
|
||||
PyObject *args = Py_BuildValue("(sO)", param.paramTypeId().toString().toUtf8().data(), pyParamValue);
|
||||
|
||||
PyParam *pyParam = (PyParam*)PyObject_CallObject((PyObject*)&PyParamType, args);
|
||||
|
||||
Py_DECREF(pyParamValue);
|
||||
Py_DECREF(args);
|
||||
|
||||
return pyParam;
|
||||
}
|
||||
|
||||
|
|
@ -101,9 +106,10 @@ static Param PyParam_ToParam(PyParam *pyParam)
|
|||
|
||||
static PyObject* PyParams_FromParamList(const ParamList ¶ms)
|
||||
{
|
||||
PyObject* result = PyTuple_New(params.count());
|
||||
PyObject* result = PyTuple_New(params.length());
|
||||
for (int i = 0; i < params.count(); i++) {
|
||||
PyTuple_SET_ITEM(result, i, (PyObject*)PyParam_fromParam(params.at(i)));
|
||||
PyParam *pyParam = PyParam_fromParam(params.at(i));
|
||||
PyTuple_SetItem(result, i, (PyObject*)pyParam);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
@ -112,7 +118,7 @@ static ParamList PyParams_ToParamList(PyObject *pyParams)
|
|||
{
|
||||
ParamList params;
|
||||
|
||||
if (pyParams != nullptr) {
|
||||
if (pyParams == nullptr) {
|
||||
return params;
|
||||
}
|
||||
|
||||
|
|
@ -130,7 +136,10 @@ static ParamList PyParams_ToParamList(PyObject *pyParams)
|
|||
|
||||
PyParam *pyParam = reinterpret_cast<PyParam*>(next);
|
||||
params.append(PyParam_ToParam(pyParam));
|
||||
Py_DECREF(next);
|
||||
}
|
||||
|
||||
Py_DECREF(iter);
|
||||
return params;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -18,24 +18,34 @@
|
|||
#pragma GCC diagnostic ignored "-Winvalid-offsetof"
|
||||
#pragma GCC diagnostic ignored "-Wwrite-strings"
|
||||
|
||||
/* Note: Thing is not threadsafe so we must never access thing directly in here.
|
||||
* For writing access, invoking with QueuedConnections will decouple stuff
|
||||
* For reading access, we keep a cache of the thing properties here and sync them
|
||||
* over to the cache when they change.
|
||||
* When using this, make sure to call PyThing_setThing() after constructing it.
|
||||
/* Note:
|
||||
* When using this, make sure to call PyThing_setThing() while holding the GIL to initialize
|
||||
* stuff after constructing it.
|
||||
*
|
||||
* The Thing class is not threadsafe and self->thing is owned by nymeas main thread.
|
||||
* So we must never directly access anything of it in here.
|
||||
*
|
||||
* For writing to it, invoking methods with QueuedConnections will thread-decouple stuff.
|
||||
* Make sure to lock the self->mutex while using the pointer to it for invoking stuff.
|
||||
*
|
||||
* For reading access, we keep copies of the thing properties here and sync them
|
||||
* over to the according py* members when they change.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
typedef struct _thing {
|
||||
PyObject_HEAD
|
||||
Thing *thing = nullptr;
|
||||
PyObject *name = nullptr;
|
||||
PyObject *id = nullptr;
|
||||
PyObject *thingClassId = nullptr;
|
||||
PyObject *params = nullptr;
|
||||
PyObject *settings = nullptr;
|
||||
PyObject *nameChangedHandler = nullptr;
|
||||
QMutex *mutex = nullptr;
|
||||
Thing *thing = nullptr; // the actual thing in nymea (not thread-safe!)
|
||||
QMutex *mutex = nullptr; // The mutex for accessing the thing pointer
|
||||
PyObject *pyId = nullptr;
|
||||
PyObject *pyThingClassId = nullptr;
|
||||
PyObject *pyName = nullptr;
|
||||
PyObject *pyParams = nullptr;
|
||||
PyObject *pySettings = nullptr;
|
||||
PyObject *pyNameChangedHandler = nullptr;
|
||||
PyObject *pySettingChangedHandler = nullptr;
|
||||
PyObject *pyStates = nullptr; // A copy of the things states
|
||||
} PyThing;
|
||||
|
||||
|
||||
|
|
@ -44,7 +54,7 @@ static PyObject* PyThing_new(PyTypeObject *type, PyObject */*args*/, PyObject */
|
|||
if (self == NULL) {
|
||||
return nullptr;
|
||||
}
|
||||
qWarning() << "*creating pyThing" << self;
|
||||
qWarning() << "*++++ PyThing" << self;
|
||||
self->mutex = new QMutex();
|
||||
|
||||
return (PyObject*)self;
|
||||
|
|
@ -54,103 +64,97 @@ static void PyThing_setThing(PyThing *self, Thing *thing)
|
|||
{
|
||||
self->thing = thing;
|
||||
|
||||
self->name = PyUnicode_FromString(self->thing->name().toUtf8().data());
|
||||
self->id = PyUnicode_FromString(self->thing->id().toString().toUtf8().data());
|
||||
self->thingClassId = PyUnicode_FromString(self->thing->thingClassId().toString().toUtf8().data());
|
||||
self->pyId = PyUnicode_FromString(self->thing->id().toString().toUtf8().data());
|
||||
self->pyThingClassId = PyUnicode_FromString(self->thing->thingClassId().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());
|
||||
|
||||
self->pyStates = PyList_New(thing->states().count());
|
||||
for (int i = 0; i < thing->states().count(); i++) {
|
||||
qWarning() << "i" << i;
|
||||
State state = thing->states().at(i);
|
||||
PyObject *pyState = Py_BuildValue("{s:s, s:O}",
|
||||
"stateTypeId", state.stateTypeId().toString().toUtf8().data(),
|
||||
"value", QVariantToPyObject(state.value()));
|
||||
PyList_SetItem(self->pyStates, i, pyState);
|
||||
}
|
||||
|
||||
|
||||
// Connects signal handlers from the Thing to sync stuff over to the pyThing in a
|
||||
// thread-safe manner.
|
||||
|
||||
// Those lambdas Will be executed in the main thread context. This means we
|
||||
// can access self->thing, but need to hold the GIL for interacting with python
|
||||
QObject::connect(thing, &Thing::nameChanged, [=](){
|
||||
self->mutex->lock();
|
||||
Py_XDECREF(self->name);
|
||||
self->name = PyUnicode_FromString(self->thing->name().toUtf8().data());
|
||||
|
||||
if (!self->nameChangedHandler) {
|
||||
self->mutex->unlock();
|
||||
return;
|
||||
}
|
||||
self->mutex->unlock();
|
||||
|
||||
PyGILState_STATE s = PyGILState_Ensure();
|
||||
PyObject_CallFunctionObjArgs(self->nameChangedHandler, self, nullptr);
|
||||
|
||||
Py_XDECREF(self->pyName);
|
||||
self->pyName = PyUnicode_FromString(self->thing->name().toUtf8().data());
|
||||
if (self->pyNameChangedHandler) {
|
||||
PyObject_CallFunctionObjArgs(self->pyNameChangedHandler, self, nullptr);
|
||||
}
|
||||
PyGILState_Release(s);
|
||||
});
|
||||
|
||||
self->params = PyParams_FromParamList(self->thing->params());
|
||||
|
||||
self->settings = PyParams_FromParamList(self->thing->settings());
|
||||
QObject::connect(thing, &Thing::settingChanged, [=](){
|
||||
QMutexLocker(self->mutex);
|
||||
Py_XDECREF(self->settings);
|
||||
self->settings = PyParams_FromParamList(self->thing->settings());
|
||||
QObject::connect(thing, &Thing::settingChanged, [=](const ParamTypeId ¶mTypeId, const QVariant &value){
|
||||
PyGILState_STATE s = PyGILState_Ensure();
|
||||
Py_XDECREF(self->pySettings);
|
||||
self->pySettings = PyParams_FromParamList(self->thing->settings());
|
||||
if (self->pySettingChangedHandler) {
|
||||
PyObject_CallFunctionObjArgs(self->pySettingChangedHandler, self, PyUnicode_FromString(paramTypeId.toString().toUtf8().data()), QVariantToPyObject(value), nullptr);
|
||||
}
|
||||
PyGILState_Release(s);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
static void PyThing_dealloc(PyThing * self) {
|
||||
qWarning() << "destryoing pything" << self;
|
||||
Py_XDECREF(self->name);
|
||||
Py_XDECREF(self->id);
|
||||
Py_XDECREF(self->thingClassId);
|
||||
Py_XDECREF(self->name);
|
||||
Py_XDECREF(self->params);
|
||||
Py_XDECREF(self->settings);
|
||||
Py_XDECREF(self->nameChangedHandler);
|
||||
qWarning() << "----- PyThing" << self;
|
||||
Py_XDECREF(self->pyId);
|
||||
Py_XDECREF(self->pyThingClassId);
|
||||
Py_XDECREF(self->pyName);
|
||||
Py_XDECREF(self->pyParams);
|
||||
Py_XDECREF(self->pySettings);
|
||||
Py_XDECREF(self->pyStates);
|
||||
Py_XDECREF(self->pyNameChangedHandler);
|
||||
Py_XDECREF(self->pySettingChangedHandler);
|
||||
delete self->mutex;
|
||||
Py_TYPE(self)->tp_free(self);
|
||||
Py_TYPE(self)->tp_free(self);
|
||||
}
|
||||
|
||||
static PyObject *PyThing_getName(PyThing *self, void */*closure*/)
|
||||
{
|
||||
QMutexLocker(self->mutex);
|
||||
if (!self->thing) {
|
||||
PyErr_SetString(PyExc_ValueError, "Thing has been removed from the system.");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Py_INCREF(self->name);
|
||||
return self->name;
|
||||
Py_INCREF(self->pyName);
|
||||
return self->pyName;
|
||||
}
|
||||
|
||||
static PyObject *PyThing_getId(PyThing *self, void */*closure*/)
|
||||
{
|
||||
QMutexLocker(self->mutex);
|
||||
if (!self->thing) {
|
||||
PyErr_SetString(PyExc_ValueError, "Thing has been removed from the system.");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Py_INCREF(self->id);
|
||||
return self->id;
|
||||
Py_INCREF(self->pyId);
|
||||
return self->pyId;
|
||||
}
|
||||
|
||||
static PyObject *PyThing_getThingClassId(PyThing *self, void */*closure*/)
|
||||
{
|
||||
QMutexLocker(self->mutex);
|
||||
if (!self->thing) {
|
||||
PyErr_SetString(PyExc_ValueError, "Thing has been removed from the system.");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Py_INCREF(self->thingClassId);
|
||||
return self->thingClassId;
|
||||
Py_INCREF(self->pyThingClassId);
|
||||
return self->pyThingClassId;
|
||||
}
|
||||
|
||||
static int PyThing_setName(PyThing *self, PyObject *value, void */*closure*/){
|
||||
QString name = QString(PyUnicode_AsUTF8(value));
|
||||
QMutexLocker(self->mutex);
|
||||
if (!self->thing) {
|
||||
return -1;
|
||||
}
|
||||
QMetaObject::invokeMethod(self->thing, "setName", Qt::QueuedConnection, Q_ARG(QString, name));
|
||||
return 0;
|
||||
}
|
||||
|
||||
static PyObject *PyThing_getSettings(PyThing *self, void */*closure*/)
|
||||
{
|
||||
QMutexLocker(self->mutex);
|
||||
if (!self->thing) {
|
||||
PyErr_SetString(PyExc_ValueError, "Thing has been removed from the system.");
|
||||
return nullptr;
|
||||
}
|
||||
Py_INCREF(self->settings);
|
||||
return self->settings;
|
||||
Py_INCREF(self->pySettings);
|
||||
return self->pySettings;
|
||||
}
|
||||
|
||||
static int PyThing_setSettings(PyThing */*self*/, PyObject */*value*/, void */*closure*/){
|
||||
|
|
@ -158,6 +162,49 @@ static int PyThing_setSettings(PyThing */*self*/, PyObject */*value*/, void */*c
|
|||
return 0;
|
||||
}
|
||||
|
||||
static PyObject * PyThing_stateValue(PyThing* self, PyObject* args)
|
||||
{
|
||||
char *stateTypeIdStr = nullptr;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "s", &stateTypeIdStr)) {
|
||||
qCWarning(dcThingManager) << "Error parsing parameters";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
StateTypeId stateTypeId = StateTypeId(stateTypeIdStr);
|
||||
PyObject *iterator = PyObject_GetIter(self->pyStates);
|
||||
while (iterator) {
|
||||
PyObject *pyState = PyIter_Next(iterator);
|
||||
if (!pyState) {
|
||||
break;
|
||||
}
|
||||
|
||||
PyObject *pyStateTypeId = PyDict_GetItemString(pyState, "stateTypeId");
|
||||
PyObject *tmp = PyUnicode_AsEncodedString(pyStateTypeId, "UTF-8", "strict");
|
||||
|
||||
StateTypeId stid = StateTypeId(PyBytes_AS_STRING(tmp));
|
||||
|
||||
Py_DECREF(pyStateTypeId);
|
||||
Py_XDECREF(tmp);
|
||||
|
||||
if (stid != stateTypeId) {
|
||||
Py_DECREF(pyState);
|
||||
continue;
|
||||
}
|
||||
|
||||
PyObject *pyStateValue = PyDict_GetItemString(pyState, "value");
|
||||
|
||||
Py_DECREF(pyState);
|
||||
Py_DECREF(iterator);
|
||||
|
||||
return pyStateValue;
|
||||
}
|
||||
|
||||
Py_DECREF(iterator);
|
||||
qCWarning(dcPythonIntegrations()) << "No state for stateTypeId:" << stateTypeId;
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
static PyObject * PyThing_setStateValue(PyThing* self, PyObject* args)
|
||||
{
|
||||
char *stateTypeIdStr = nullptr;
|
||||
|
|
@ -209,13 +256,15 @@ static PyGetSetDef PyThing_getset[] = {
|
|||
};
|
||||
|
||||
static PyMethodDef PyThing_methods[] = {
|
||||
{ "setStateValue", (PyCFunction)PyThing_setStateValue, METH_VARARGS, "Set a things state value" },
|
||||
{ "stateValue", (PyCFunction)PyThing_stateValue, METH_VARARGS, "Get a things state value by stateTypeId" },
|
||||
{ "setStateValue", (PyCFunction)PyThing_setStateValue, METH_VARARGS, "Set a certain things state value by stateTypeIp" },
|
||||
{ "emitEvent", (PyCFunction)PyThing_emitEvent, METH_VARARGS, "Emits an event" },
|
||||
{nullptr, nullptr, 0, nullptr} // sentinel
|
||||
};
|
||||
|
||||
static PyMemberDef PyThing_members[] = {
|
||||
{"nameChangedHandler", T_OBJECT_EX, offsetof(PyThing, nameChangedHandler), 0, "Set a callback for when the thing name changes"},
|
||||
{"params", T_OBJECT_EX, offsetof(PyThing, pyParams), READONLY, "Thing params"},
|
||||
{"nameChangedHandler", T_OBJECT_EX, offsetof(PyThing, pyNameChangedHandler), 0, "Set a callback for when the thing name changes"},
|
||||
{nullptr, 0, 0, 0, nullptr} /* Sentinel */
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -12,13 +12,29 @@
|
|||
#pragma GCC diagnostic ignored "-Winvalid-offsetof"
|
||||
#pragma GCC diagnostic ignored "-Wwrite-strings"
|
||||
|
||||
/* Note:
|
||||
*
|
||||
* When using this, make sure to call PyThingActionInfo_setInfo() while holding the GIL to initialize
|
||||
* stuff after constructing it. Also set info to nullptr while holding the GIL when the info object vanishes.
|
||||
*
|
||||
* The ThingActionInfo class is not threadsafe and self->info is owned by nymeas main thread.
|
||||
* So we must never directly access anything of it in here.
|
||||
*
|
||||
* For writing to it, invoking methods with QueuedConnections will thread-decouple stuff.
|
||||
* Make sure to check if the info object is still valid (it might not be if nymea finished
|
||||
* the setup and destroyed it but the PyThingSetupInfo is not garbage collected yet.
|
||||
*
|
||||
* For reading access, we keep copies of the thing properties here and sync them
|
||||
* over to the according py* members when they change.
|
||||
*
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
ThingActionInfo* info;
|
||||
PyThing *pyThing;
|
||||
PyObject *pyActionTypeId;
|
||||
PyObject *pyParams;
|
||||
QMutex *mutex;
|
||||
} PyThingActionInfo;
|
||||
|
||||
|
||||
|
|
@ -27,14 +43,26 @@ static PyObject* PyThingActionInfo_new(PyTypeObject *type, PyObject */*args*/, P
|
|||
if (self == NULL) {
|
||||
return nullptr;
|
||||
}
|
||||
self->mutex = new QMutex();
|
||||
qWarning() << "+++ PyThingActionInfo";
|
||||
return (PyObject*)self;
|
||||
}
|
||||
|
||||
void PyThingActionInfo_setInfo(PyThingActionInfo *self, ThingActionInfo *info, PyThing *pyThing)
|
||||
{
|
||||
self->info = info;
|
||||
self->pyThing = pyThing;
|
||||
Py_INCREF(pyThing);
|
||||
self->pyActionTypeId = PyUnicode_FromString(info->action().actionTypeId().toString().toUtf8());
|
||||
self->pyParams = PyParams_FromParamList(info->action().params());
|
||||
}
|
||||
|
||||
|
||||
static void PyThingActionInfo_dealloc(PyThingActionInfo * self)
|
||||
{
|
||||
delete self->mutex;
|
||||
qWarning() << "---- PyThingActionInfo";
|
||||
Py_DECREF(self->pyThing);
|
||||
Py_DECREF(self->pyActionTypeId);
|
||||
Py_DECREF(self->pyParams);
|
||||
Py_TYPE(self)->tp_free(self);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -17,6 +17,8 @@ typedef struct {
|
|||
PyObject* pyThingClassId;
|
||||
PyObject* pyName;
|
||||
PyObject* pyDescription;
|
||||
PyObject* pyThingId;
|
||||
PyObject* pyParams;
|
||||
} PyThingDescriptor;
|
||||
|
||||
|
||||
|
|
@ -24,39 +26,51 @@ static PyMemberDef PyThingDescriptor_members[] = {
|
|||
{"thingClassId", T_OBJECT_EX, offsetof(PyThingDescriptor, pyThingClassId), 0, "Descriptor thingClassId"},
|
||||
{"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."},
|
||||
{"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", nullptr};
|
||||
PyObject *thingClassId = nullptr, *name = nullptr, *description = nullptr;
|
||||
static char *kwlist[] = {"thingClassId", "name", "description", "thingId", "params", nullptr};
|
||||
PyObject *thingClassId = nullptr, *name = nullptr, *description = nullptr, *thingId = nullptr, *params = nullptr;
|
||||
|
||||
qWarning() << "++++ PyThingDescriptor";
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOO", kwlist, &thingClassId, &name, &description))
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOOO", kwlist, &thingClassId, &name, &description, &thingId, ¶ms))
|
||||
return -1;
|
||||
|
||||
if (thingClassId) {
|
||||
Py_XDECREF(self->pyThingClassId);
|
||||
Py_INCREF(thingClassId);
|
||||
self->pyThingClassId = thingClassId;
|
||||
}
|
||||
if (name) {
|
||||
Py_XDECREF(self->pyName);
|
||||
Py_INCREF(name);
|
||||
self->pyName = name;
|
||||
}
|
||||
if (description) {
|
||||
Py_XDECREF(self->pyDescription);
|
||||
Py_INCREF(description);
|
||||
self->pyDescription = description;
|
||||
}
|
||||
if (thingId) {
|
||||
Py_INCREF(thingId);
|
||||
self->pyThingId = thingId;
|
||||
}
|
||||
if (params) {
|
||||
Py_INCREF(params);
|
||||
self->pyParams = params;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void PyThingDescriptor_dealloc(PyThingDescriptor * self)
|
||||
{
|
||||
qWarning() << "---- PyThingDescriptor";
|
||||
Py_XDECREF(self->pyThingClassId);
|
||||
Py_XDECREF(self->pyName);
|
||||
Py_XDECREF(self->pyDescription);
|
||||
Py_XDECREF(self->pyThingId);
|
||||
Py_XDECREF(self->pyParams);
|
||||
Py_TYPE(self)->tp_free(self);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
#include "structmember.h"
|
||||
|
||||
#include "pythingdescriptor.h"
|
||||
#include "pyparam.h"
|
||||
|
||||
#include "integrations/thingdiscoveryinfo.h"
|
||||
|
||||
|
|
@ -18,10 +19,28 @@
|
|||
#pragma GCC diagnostic ignored "-Wwrite-strings"
|
||||
|
||||
|
||||
/* Note:
|
||||
* When using this, make sure to call PyThingDiscoveryInfo_setInfo() while holding the GIL to initialize
|
||||
* stuff after constructing it. Also set info to nullptr while holding the GIL when the info object vanishes.
|
||||
*
|
||||
* The ThingDiscoveryInfo class is not threadsafe and self->info is owned by nymeas main thread.
|
||||
* So we must never directly access anything of it in here.
|
||||
*
|
||||
* For writing to it, invoking methods with QueuedConnections will thread-decouple stuff.
|
||||
* Make sure to check if the info object is still valid (it might not be if nymea finished
|
||||
* the discovery and destroyed it but the PyThingDiscoveryInfo is not garbage collected yet.
|
||||
*
|
||||
* For reading access, we keep copies of the thing properties here and sync them
|
||||
* over to the according py* members when they change.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
ThingDiscoveryInfo* info;
|
||||
QMutex *mutex;
|
||||
// PyObject* pyThingClassId = nullptr;
|
||||
// PyObject *pyParams = nullptr;
|
||||
} PyThingDiscoveryInfo;
|
||||
|
||||
static PyObject* PyThingDiscoveryInfo_new(PyTypeObject *type, PyObject */*args*/, PyObject */*kwds*/)
|
||||
|
|
@ -30,13 +49,22 @@ static PyObject* PyThingDiscoveryInfo_new(PyTypeObject *type, PyObject */*args*/
|
|||
if (self == NULL) {
|
||||
return nullptr;
|
||||
}
|
||||
self->mutex = new QMutex();
|
||||
qWarning() << "++++ PyThingDiscoveryInfo";
|
||||
return (PyObject*)self;
|
||||
}
|
||||
|
||||
void PyThingDiscoveryInfo_setInfo(PyThingDiscoveryInfo *self, ThingDiscoveryInfo *info)
|
||||
{
|
||||
self->info = info;
|
||||
// self->pyThingClassId = PyUnicode_FromString(info->thingClassId().toString().toUtf8().data());
|
||||
// self->pyParams = PyParams_FromParamList(info->params());
|
||||
}
|
||||
|
||||
static void PyThingDiscoveryInfo_dealloc(PyThingDiscoveryInfo * self)
|
||||
{
|
||||
delete self->mutex;
|
||||
qWarning() << "---- PyThingDiscoveryInfo";
|
||||
// Py_DECREF(self->pyThingClassId);
|
||||
// Py_DECREF(self->pyParams);
|
||||
Py_TYPE(self)->tp_free(self);
|
||||
}
|
||||
|
||||
|
|
@ -64,11 +92,11 @@ static PyObject * PyThingDiscoveryInfo_addDescriptor(PyThingDiscoveryInfo* self,
|
|||
PyObject *pyObj = nullptr;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "O", &pyObj)) {
|
||||
PyErr_SetString(PyExc_ValueError, "Invalid argument. Not a ThingDescriptor.");
|
||||
PyErr_SetString(PyExc_ValueError, "Invalid argument to ThingDiscoveryInfo.addDescriptor(). Not a ThingDescriptor.");
|
||||
return nullptr;
|
||||
}
|
||||
if (pyObj->ob_type != &PyThingDescriptorType) {
|
||||
PyErr_SetString(PyExc_ValueError, "Invalid argument. Not a ThingDescriptor.");
|
||||
PyErr_SetString(PyExc_ValueError, "Invalid argument to ThingDiscoveryInfo.addDescriptor(). Not a ThingDescriptor.");
|
||||
return nullptr;
|
||||
}
|
||||
PyThingDescriptor *pyDescriptor = (PyThingDescriptor*)pyObj;
|
||||
|
|
@ -87,14 +115,28 @@ static PyObject * PyThingDiscoveryInfo_addDescriptor(PyThingDiscoveryInfo* self,
|
|||
}
|
||||
|
||||
ThingDescriptor descriptor(thingClassId, name, description);
|
||||
if (pyDescriptor->pyThingId) {
|
||||
descriptor.setThingId(ThingId(QString::fromUtf8(PyUnicode_AsUTF8(pyDescriptor->pyThingId))));
|
||||
}
|
||||
|
||||
if (pyDescriptor->pyParams) {
|
||||
descriptor.setParams(PyParams_ToParamList(pyDescriptor->pyParams));
|
||||
}
|
||||
|
||||
if (self->info) {
|
||||
QMetaObject::invokeMethod(self->info, "addThingDescriptor", Qt::QueuedConnection, Q_ARG(ThingDescriptor, descriptor));
|
||||
}
|
||||
|
||||
return Py_BuildValue("");
|
||||
Py_DECREF(pyDescriptor);
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
//static PyMemberDef PyThingDiscoveryInfo_members[] = {
|
||||
// {"thingClassId", T_OBJECT_EX, offsetof(PyThingDiscoveryInfo, pyThingClassId), READONLY, "The ThingClassId this discovery is for."},
|
||||
//// {"params", T_OBJECT_EX, offsetof(PyThingDiscoveryInfo, pyParams), READONLY, "The params for this discovery"},
|
||||
// {nullptr, 0, 0, 0, nullptr} /* Sentinel */
|
||||
//};
|
||||
|
||||
static PyMethodDef PyThingDiscoveryInfo_methods[] = {
|
||||
{ "addDescriptor", (PyCFunction)PyThingDiscoveryInfo_addDescriptor, METH_VARARGS, "Add a new descriptor to the discovery" },
|
||||
{ "finish", (PyCFunction)PyThingDiscoveryInfo_finish, METH_VARARGS, "Finish a discovery" },
|
||||
|
|
|
|||
|
|
@ -0,0 +1,188 @@
|
|||
#ifndef PYTHINGPAIRINGINFO_H
|
||||
#define PYTHINGPAIRINGINFO_H
|
||||
|
||||
|
||||
#include <Python.h>
|
||||
#include "structmember.h"
|
||||
|
||||
#include "pyparam.h"
|
||||
|
||||
#include "integrations/thingpairinginfo.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <QMetaEnum>
|
||||
#include <QMutex>
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Winvalid-offsetof"
|
||||
#pragma GCC diagnostic ignored "-Wwrite-strings"
|
||||
|
||||
/* Note:
|
||||
* When using this, make sure to call PyThingPairingInfo_setInfo() while holding the GIL to initialize
|
||||
* stuff after constructing it. Also set info to nullptr while holding the GIL when the info object vanishes.
|
||||
*
|
||||
* The ThingPairingInfo class is not threadsafe and self->info is owned by nymeas main thread.
|
||||
* So we must never directly access anything of it in here.
|
||||
*
|
||||
* For writing to it, invoking methods with QueuedConnections will thread-decouple stuff.
|
||||
* Make sure to check if the info object is still valid (it might not be if nymea finished
|
||||
* the pairing step and destroyed it but the PyThingPairingInfo is not garbage collected yet.
|
||||
*
|
||||
* For reading access, we keep copies of the thing properties here and sync them
|
||||
* over to the according py* members when they change.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
ThingPairingInfo* info;
|
||||
PyObject *pyTransactionId = nullptr;
|
||||
PyObject *pyThingClassId = nullptr;
|
||||
PyObject *pyThingId = nullptr;
|
||||
PyObject *pyThingName = nullptr;
|
||||
PyObject *pyParentId = nullptr;
|
||||
PyObject *pyParams = nullptr;
|
||||
PyObject *pyOAuthUrl = nullptr;
|
||||
} PyThingPairingInfo;
|
||||
|
||||
static PyMemberDef PyThingPairingInfo_members[] = {
|
||||
{"transactionId", T_OBJECT_EX, offsetof(PyThingPairingInfo, pyTransactionId), READONLY, "The transaction id for this pairing procedure."},
|
||||
{"thingClassId", T_OBJECT_EX, offsetof(PyThingPairingInfo, pyThingClassId), READONLY, "The ThingClassId for the thing to be set up."},
|
||||
{"thingId", T_OBJECT_EX, offsetof(PyThingPairingInfo, pyThingId), READONLY, "The ThingId for the thing to be set up."},
|
||||
{"thingName", T_OBJECT_EX, offsetof(PyThingPairingInfo, pyThingName), READONLY, "The ThingId for the thing to be set up."},
|
||||
{"parentId", T_OBJECT_EX, offsetof(PyThingPairingInfo, pyParentId), READONLY, "The ThingId for the parent of the thing to be set up."},
|
||||
{"params", T_OBJECT_EX, offsetof(PyThingPairingInfo, pyParams), READONLY, "The params for the thing to be set up."},
|
||||
{"oAuthUrl", T_OBJECT_EX, offsetof(PyThingPairingInfo, pyOAuthUrl), 0, "An OAuth url if required for the pairing."},
|
||||
{nullptr, 0, 0, 0, nullptr} /* Sentinel */
|
||||
};
|
||||
|
||||
static int PyThingPairingInfo_init(PyThingPairingInfo */*self*/, PyObject */*args*/, PyObject */*kwds*/)
|
||||
{
|
||||
qWarning() << "++++ ThingPairingInfo";
|
||||
return 0;
|
||||
}
|
||||
|
||||
void PyThingPairingInfo_setInfo(PyThingPairingInfo *self, ThingPairingInfo *info)
|
||||
{
|
||||
self->info = info;
|
||||
self->pyTransactionId = PyUnicode_FromString(info->transactionId().toString().toUtf8());
|
||||
self->pyThingClassId = PyUnicode_FromString(info->thingClassId().toString().toUtf8());
|
||||
self->pyThingId = PyUnicode_FromString(info->thingId().toString().toUtf8());
|
||||
self->pyThingName = PyUnicode_FromString(info->thingName().toUtf8());
|
||||
self->pyParentId = PyUnicode_FromString(info->parentId().toString().toUtf8());
|
||||
self->pyParams = PyParams_FromParamList(info->params());
|
||||
}
|
||||
|
||||
static void PyThingPairingInfo_dealloc(PyThingPairingInfo * self)
|
||||
{
|
||||
qWarning() << "---- ThingPairingInfo";
|
||||
Py_XDECREF(self->pyTransactionId);
|
||||
Py_XDECREF(self->pyThingClassId);
|
||||
Py_XDECREF(self->pyThingId);
|
||||
Py_XDECREF(self->pyThingName);
|
||||
Py_XDECREF(self->pyParentId);
|
||||
Py_XDECREF(self->pyParams);
|
||||
Py_XDECREF(self->pyOAuthUrl);
|
||||
Py_TYPE(self)->tp_free(self);
|
||||
}
|
||||
|
||||
static PyObject * PyThingPairingInfo_finish(PyThingPairingInfo* self, PyObject* args)
|
||||
{
|
||||
int status;
|
||||
char *message = nullptr;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "i|s", &status, &message)) {
|
||||
PyErr_SetString(PyExc_TypeError, "Invalid arguments in finish call. Expected: finish(ThingError, message = \"\"");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Thing::ThingError thingError = static_cast<Thing::ThingError>(status);
|
||||
QString displayMessage = message != nullptr ? QString(message) : QString();
|
||||
|
||||
if (self->info) {
|
||||
if (self->pyOAuthUrl) {
|
||||
QString oAuthUrl = QString::fromUtf8(PyUnicode_AsUTF8AndSize(self->pyOAuthUrl, nullptr));
|
||||
QMetaObject::invokeMethod(self->info, "setOAuthUrl", Qt::QueuedConnection, Q_ARG(QString, oAuthUrl));
|
||||
}
|
||||
QMetaObject::invokeMethod(self->info, "finish", Qt::QueuedConnection, Q_ARG(Thing::ThingError, thingError), Q_ARG(QString, displayMessage));
|
||||
}
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
static PyMethodDef PyThingPairingInfo_methods[] = {
|
||||
{ "finish", (PyCFunction)PyThingPairingInfo_finish, METH_VARARGS, "Finish a discovery" },
|
||||
{nullptr, nullptr, 0, nullptr} // sentinel
|
||||
};
|
||||
|
||||
static PyTypeObject PyThingPairingInfoType = {
|
||||
PyVarObject_HEAD_INIT(NULL, 0)
|
||||
"nymea.ThingPairingInfo", /* tp_name */
|
||||
sizeof(PyThingPairingInfo), /* tp_basicsize */
|
||||
0, /* tp_itemsize */
|
||||
(destructor)PyThingPairingInfo_dealloc, /* tp_dealloc */
|
||||
0, /* tp_print */
|
||||
0, /* tp_getattr */
|
||||
0, /* tp_setattr */
|
||||
0, /* tp_reserved */
|
||||
0, /* tp_repr */
|
||||
0, /* tp_as_number */
|
||||
0, /* tp_as_sequence */
|
||||
0, /* tp_as_mapping */
|
||||
0, /* tp_hash */
|
||||
0, /* tp_call */
|
||||
0, /* tp_str */
|
||||
0, /* tp_getattro */
|
||||
0, /* tp_setattro */
|
||||
0, /* tp_as_buffer */
|
||||
Py_TPFLAGS_DEFAULT, /* tp_flags */
|
||||
"ThingPairingInfo", /* tp_doc */
|
||||
0, /* tp_traverse */
|
||||
0, /* tp_clear */
|
||||
0, /* tp_richcompare */
|
||||
0, /* tp_weaklistoffset */
|
||||
0, /* tp_iter */
|
||||
0, /* tp_iternext */
|
||||
PyThingPairingInfo_methods, /* tp_methods */
|
||||
PyThingPairingInfo_members, /* tp_members */
|
||||
0, /* tp_getset */
|
||||
0, /* tp_base */
|
||||
0, /* tp_dict */
|
||||
0, /* tp_descr_get */
|
||||
0, /* tp_descr_set */
|
||||
0, /* tp_dictoffset */
|
||||
(initproc)PyThingPairingInfo_init, /* tp_init */
|
||||
0, /* tp_alloc */
|
||||
PyType_GenericNew, /* tp_new */
|
||||
0, /* tp_free */
|
||||
0, /* tp_is_gc */
|
||||
0, /* tp_bases */
|
||||
0, /* tp_mro */
|
||||
0, /* tp_cache */
|
||||
0, /* tp_subclasses */
|
||||
0, /* tp_weaklist */
|
||||
0, /* tp_del */
|
||||
0, /* tp_version_tag */
|
||||
0, /* tp_finalize */
|
||||
0, /* tp_vectorcall */
|
||||
0, /* tp_print DEPRECATED*/
|
||||
};
|
||||
|
||||
|
||||
|
||||
static void registerThingPairingInfoType(PyObject *module)
|
||||
{
|
||||
|
||||
if (PyType_Ready(&PyThingPairingInfoType) < 0) {
|
||||
return;
|
||||
}
|
||||
PyModule_AddObject(module, "ThingPairingInfo", (PyObject *)&PyThingPairingInfoType);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
#endif // PYTHINGPAIRINGINFO_H
|
||||
|
|
@ -12,27 +12,56 @@
|
|||
#pragma GCC diagnostic ignored "-Winvalid-offsetof"
|
||||
#pragma GCC diagnostic ignored "-Wwrite-strings"
|
||||
|
||||
/* Note:
|
||||
*
|
||||
* When using this, make sure to call PyThingSetupInfo_setInfo() while holding the GIL to initialize
|
||||
* stuff after constructing it. Also set info to nullptr while holding the GIL when the info object vanishes.
|
||||
*
|
||||
* The ThingSetupInfo class is not threadsafe and self->info is owned by nymeas main thread.
|
||||
* So we must never directly access anything of it in here.
|
||||
*
|
||||
* For writing to it, invoking methods with QueuedConnections will thread-decouple stuff.
|
||||
* Make sure to check if the info object is still valid (it might not be if nymea finished
|
||||
* the setup and destroyed it but the PyThingSetupInfo is not garbage collected yet.
|
||||
*
|
||||
* For reading access, we keep copies of the thing properties here and sync them
|
||||
* over to the according py* members when they change.
|
||||
*
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
ThingSetupInfo* info;
|
||||
PyThing *pyThing;
|
||||
QMutex *mutex;
|
||||
} PyThingSetupInfo;
|
||||
|
||||
|
||||
static PyObject* PyThingSetupInfo_new(PyTypeObject *type, PyObject */*args*/, PyObject */*kwds*/) {
|
||||
static PyObject* PyThingSetupInfo_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
|
||||
PyThingSetupInfo *self = (PyThingSetupInfo*)type->tp_alloc(type, 0);
|
||||
if (self == NULL) {
|
||||
return nullptr;
|
||||
}
|
||||
qWarning() << "++++ PyThingSetupInfo";
|
||||
self->mutex = new QMutex();
|
||||
|
||||
|
||||
static char *kwlist[] = {"thing", nullptr};
|
||||
PyObject *pyThing = nullptr;
|
||||
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", kwlist, &pyThing)) {
|
||||
PyErr_Print();
|
||||
PyErr_SetString(PyExc_ValueError, "Invalid arguments.");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
self->pyThing = (PyThing*)pyThing;
|
||||
Py_INCREF(self->pyThing);
|
||||
|
||||
return (PyObject*)self;
|
||||
}
|
||||
|
||||
static void PyThingSetupInfo_dealloc(PyThingSetupInfo * self) {
|
||||
qWarning() << "--- PyThingSetupInfo";
|
||||
delete self->mutex;
|
||||
Py_DECREF(self->pyThing);
|
||||
Py_TYPE(self)->tp_free(self);
|
||||
}
|
||||
|
||||
|
|
@ -48,7 +77,6 @@ static PyObject * PyThingSetupInfo_finish(PyThingSetupInfo* self, PyObject* args
|
|||
Thing::ThingError thingError = static_cast<Thing::ThingError>(status);
|
||||
QString displayMessage = message != nullptr ? QString(message) : QString();
|
||||
|
||||
QMutexLocker(self->mutex);
|
||||
if (self->info) {
|
||||
QMetaObject::invokeMethod(self->info, "finish", Qt::QueuedConnection, Q_ARG(Thing::ThingError, thingError), Q_ARG(QString, displayMessage));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
#include <QVariant>
|
||||
|
||||
/* Returns a PyObject. RefCount will be 1 */
|
||||
/* Returns a new reference to PyObject*. */
|
||||
PyObject *QVariantToPyObject(const QVariant &value)
|
||||
{
|
||||
PyObject *pyValue = nullptr;
|
||||
|
|
@ -20,7 +20,7 @@ PyObject *QVariantToPyObject(const QVariant &value)
|
|||
case QVariant::UInt:
|
||||
case QVariant::LongLong:
|
||||
case QVariant::ULongLong:
|
||||
pyValue = PyLong_FromLong(value.toLongLong());
|
||||
pyValue = PyLong_FromLongLong(value.toLongLong());
|
||||
break;
|
||||
case QVariant::String:
|
||||
case QVariant::ByteArray:
|
||||
|
|
@ -34,7 +34,7 @@ PyObject *QVariantToPyObject(const QVariant &value)
|
|||
Py_INCREF(pyValue);
|
||||
break;
|
||||
default:
|
||||
qCWarning(dcThingManager) << "Unhandled data type in conversion from Param to PyParam!";
|
||||
qCWarning(dcPythonIntegrations()) << "Unhandled data type in conversion from Param to PyParam!";
|
||||
pyValue = Py_None;
|
||||
Py_INCREF(pyValue);
|
||||
break;
|
||||
|
|
@ -46,6 +46,7 @@ PyObject *QVariantToPyObject(const QVariant &value)
|
|||
QVariant PyObjectToQVariant(PyObject *pyObject)
|
||||
{
|
||||
// FIXME: is there any better way to do this?
|
||||
qWarning() << "Error:" << PyErr_CheckSignals();
|
||||
PyObject* repr = PyObject_Repr(pyObject);
|
||||
PyObject* str = PyUnicode_AsEncodedString(repr, "utf-8", "~E~");
|
||||
const char *bytes = PyBytes_AS_STRING(str);
|
||||
|
|
@ -58,13 +59,6 @@ QVariant PyObjectToQVariant(PyObject *pyObject)
|
|||
return value;
|
||||
}
|
||||
|
||||
void PyDumpError()
|
||||
{
|
||||
if (!PyErr_Occurred()) {
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Write to stdout
|
||||
PyObject* pyLog_write(PyObject* /*self*/, PyObject* args)
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
#include "python/pythingsetupinfo.h"
|
||||
#include "python/pyparam.h"
|
||||
#include "python/pythingactioninfo.h"
|
||||
#include "python/pythingpairinginfo.h"
|
||||
|
||||
#include "pythonintegrationplugin.h"
|
||||
|
||||
|
|
@ -46,6 +47,8 @@ PyObject* PythonIntegrationPlugin::task_done(PyObject* self, PyObject* args)
|
|||
Py_XDECREF(repr);
|
||||
Py_XDECREF(str);
|
||||
|
||||
// PyObject *traceback = PyObject_CallMethodObjArgs(exception, "__traceback__", nullptr);
|
||||
|
||||
qCWarning(dcThingManager()) << "Exception in plugin:" << bytes;
|
||||
|
||||
PyErr_Clear();
|
||||
|
|
@ -88,6 +91,7 @@ PyMODINIT_FUNC PyInit_nymea(void)
|
|||
registerThingType(m);
|
||||
registerThingDescriptorType(m);
|
||||
registerThingDiscoveryInfoType(m);
|
||||
registerThingPairingInfoType(m);
|
||||
registerThingSetupInfoType(m);
|
||||
registerThingActionInfoType(m);
|
||||
|
||||
|
|
@ -176,15 +180,16 @@ PyObject *PythonIntegrationPlugin::pyMyThings(PyObject *self, PyObject */*args*/
|
|||
|
||||
PyObject *PythonIntegrationPlugin::pyAutoThingsAppeared(PyObject *self, PyObject *args)
|
||||
{
|
||||
PyObject *pyParams;
|
||||
PyObject *pyDescriptors;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "O", &pyParams)) {
|
||||
if (!PyArg_ParseTuple(args, "O", &pyDescriptors)) {
|
||||
qCWarning(dcThingManager()) << "Error parsing args. Not a param list";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
PyObject *iter = PyObject_GetIter(pyParams);
|
||||
PyObject *iter = PyObject_GetIter(pyDescriptors);
|
||||
if (!iter) {
|
||||
Py_DECREF(pyDescriptors);
|
||||
qCWarning(dcThingManager()) << "Error parsing args. Not a param list";
|
||||
return nullptr;
|
||||
}
|
||||
|
|
@ -200,7 +205,7 @@ PyObject *PythonIntegrationPlugin::pyAutoThingsAppeared(PyObject *self, PyObject
|
|||
|
||||
if (next->ob_type != &PyThingDescriptorType) {
|
||||
PyErr_SetString(PyExc_ValueError, "Invalid argument. Not a ThingDescriptor.");
|
||||
return nullptr;
|
||||
continue;
|
||||
}
|
||||
PyThingDescriptor *pyDescriptor = (PyThingDescriptor*)next;
|
||||
|
||||
|
|
@ -218,12 +223,21 @@ PyObject *PythonIntegrationPlugin::pyAutoThingsAppeared(PyObject *self, PyObject
|
|||
}
|
||||
|
||||
ThingDescriptor descriptor(thingClassId, name, description);
|
||||
|
||||
if (pyDescriptor->pyParams) {
|
||||
descriptor.setParams(PyParams_ToParamList(pyDescriptor->pyParams));
|
||||
}
|
||||
|
||||
descriptors.append(descriptor);
|
||||
Py_DECREF(next);
|
||||
}
|
||||
|
||||
PythonIntegrationPlugin *plugin = s_plugins.key(self);
|
||||
QMetaObject::invokeMethod(plugin, "autoThingsAppeared", Qt::QueuedConnection, Q_ARG(ThingDescriptors, descriptors));
|
||||
|
||||
Py_DECREF(iter);
|
||||
Py_DECREF(pyDescriptors);
|
||||
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
|
|
@ -306,7 +320,9 @@ bool PythonIntegrationPlugin::loadScript(const QString &scriptFile)
|
|||
|
||||
// Finally, import the plugin
|
||||
PyObject* sysPath = PySys_GetObject("path");
|
||||
PyList_Append(sysPath, PyUnicode_FromString(fi.absolutePath().toUtf8()));
|
||||
PyObject* importPath = PyUnicode_FromString(fi.absolutePath().toUtf8());
|
||||
PyList_Append(sysPath, importPath);
|
||||
Py_DECREF(importPath);
|
||||
m_module = PyImport_ImportModule(fi.baseName().toUtf8());
|
||||
|
||||
if (!m_module) {
|
||||
|
|
@ -349,6 +365,8 @@ bool PythonIntegrationPlugin::loadScript(const QString &scriptFile)
|
|||
PyObject *pyParamTypeId = PyUnicode_FromString(paramTypeId.toString().toUtf8());
|
||||
PyObject *pyValue = QVariantToPyObject(value);
|
||||
callPluginFunction("configValueChanged", pyParamTypeId, pyValue);
|
||||
Py_DECREF(pyParamTypeId);
|
||||
Py_DECREF(pyValue);
|
||||
});
|
||||
|
||||
return true;
|
||||
|
|
@ -373,49 +391,110 @@ void PythonIntegrationPlugin::discoverThings(ThingDiscoveryInfo *info)
|
|||
PyGILState_STATE s = PyGILState_Ensure();
|
||||
|
||||
PyThingDiscoveryInfo *pyInfo = (PyThingDiscoveryInfo*)PyObject_CallObject((PyObject*)&PyThingDiscoveryInfoType, NULL);
|
||||
pyInfo->info = info;
|
||||
PyThingDiscoveryInfo_setInfo(pyInfo, info);
|
||||
|
||||
PyGILState_Release(s);
|
||||
|
||||
connect(info, &ThingDiscoveryInfo::destroyed, this, [=](){
|
||||
auto s = PyGILState_Ensure();
|
||||
pyInfo->info = nullptr;
|
||||
Py_DECREF(pyInfo);
|
||||
PyGILState_Release(s);
|
||||
});
|
||||
|
||||
callPluginFunction("discoverThings", reinterpret_cast<PyObject*>(pyInfo));
|
||||
}
|
||||
|
||||
void PythonIntegrationPlugin::startPairing(ThingPairingInfo *info)
|
||||
{
|
||||
PyGILState_STATE s = PyGILState_Ensure();
|
||||
|
||||
PyThingPairingInfo *pyInfo = (PyThingPairingInfo*)PyObject_CallObject((PyObject*)&PyThingPairingInfoType, nullptr);
|
||||
PyThingPairingInfo_setInfo(pyInfo, info);
|
||||
|
||||
PyGILState_Release(s);
|
||||
|
||||
|
||||
connect(info, &ThingDiscoveryInfo::destroyed, this, [=](){
|
||||
QMutexLocker(pyInfo->mutex);
|
||||
connect(info, &ThingPairingInfo::destroyed, this, [=](){
|
||||
auto s = PyGILState_Ensure();
|
||||
pyInfo->info = nullptr;
|
||||
Py_DECREF(pyInfo);
|
||||
PyGILState_Release(s);
|
||||
});
|
||||
|
||||
callPluginFunction("discoverThings", reinterpret_cast<PyObject*>(pyInfo));
|
||||
bool result = callPluginFunction("startPairing", reinterpret_cast<PyObject*>(pyInfo));
|
||||
if (!result) {
|
||||
info->finish(Thing::ThingErrorHardwareFailure, "Plugin error: " + pluginName());
|
||||
}
|
||||
}
|
||||
|
||||
void PythonIntegrationPlugin::confirmPairing(ThingPairingInfo *info, const QString &username, const QString &secret)
|
||||
{
|
||||
PyGILState_STATE s = PyGILState_Ensure();
|
||||
|
||||
PyThingPairingInfo *pyInfo = (PyThingPairingInfo*)PyObject_CallObject((PyObject*)&PyThingPairingInfoType, nullptr);
|
||||
PyThingPairingInfo_setInfo(pyInfo, info);
|
||||
|
||||
PyGILState_Release(s);
|
||||
|
||||
|
||||
connect(info, &ThingPairingInfo::destroyed, this, [=](){
|
||||
auto s = PyGILState_Ensure();
|
||||
pyInfo->info = nullptr;
|
||||
Py_DECREF(pyInfo);
|
||||
PyGILState_Release(s);
|
||||
});
|
||||
|
||||
PyObject *pyUsername = PyUnicode_FromString(username.toUtf8().data());
|
||||
PyObject *pySecret = PyUnicode_FromString(secret.toUtf8().data());
|
||||
bool result = callPluginFunction("confirmPairing", reinterpret_cast<PyObject*>(pyInfo), pyUsername, pySecret);
|
||||
if (!result) {
|
||||
info->finish(Thing::ThingErrorHardwareFailure, "Plugin error: " + pluginName());
|
||||
}
|
||||
|
||||
Py_DECREF(pyUsername);
|
||||
Py_DECREF(pySecret);
|
||||
}
|
||||
|
||||
void PythonIntegrationPlugin::setupThing(ThingSetupInfo *info)
|
||||
{
|
||||
PyGILState_STATE s = PyGILState_Ensure();
|
||||
|
||||
PyThing *pyThing = (PyThing*)PyObject_CallObject((PyObject*)&PyThingType, NULL);
|
||||
dumpError();
|
||||
PyThing_setThing(pyThing, info->thing());
|
||||
PyThing *pyThing = nullptr;
|
||||
if (m_things.contains(info->thing())) {
|
||||
pyThing = m_things.value(info->thing());
|
||||
} else {
|
||||
pyThing = (PyThing*)PyObject_CallObject((PyObject*)&PyThingType, NULL);
|
||||
PyThing_setThing(pyThing, info->thing());
|
||||
m_things.insert(info->thing(), pyThing);
|
||||
}
|
||||
|
||||
PyObject *args = PyTuple_New(1);
|
||||
PyTuple_SetItem(args, 0, (PyObject*)pyThing);
|
||||
|
||||
PyThingSetupInfo *pyInfo = (PyThingSetupInfo*)PyObject_CallObject((PyObject*)&PyThingSetupInfoType, args);
|
||||
Py_DECREF(args);
|
||||
|
||||
PyThingSetupInfo *pyInfo = (PyThingSetupInfo*)PyObject_CallObject((PyObject*)&PyThingSetupInfoType, NULL);
|
||||
pyInfo->info = info;
|
||||
pyInfo->pyThing = pyThing;
|
||||
|
||||
m_things.insert(info->thing(), pyThing);
|
||||
|
||||
PyGILState_Release(s);
|
||||
|
||||
connect(info->thing(), &Thing::destroyed, this, [=](){
|
||||
cleanupPyThing(pyThing);
|
||||
auto s = PyGILState_Ensure();
|
||||
pyThing->thing = nullptr;
|
||||
Py_DECREF(pyThing);
|
||||
PyGILState_Release(s);
|
||||
});
|
||||
connect(info, &ThingSetupInfo::destroyed, this, [=](){
|
||||
QMutexLocker(pyInfo->mutex);
|
||||
auto s = PyGILState_Ensure();
|
||||
pyInfo->info = nullptr;
|
||||
Py_DECREF(pyInfo);
|
||||
PyGILState_Release(s);
|
||||
});
|
||||
|
||||
|
||||
bool result = callPluginFunction("setupThing", reinterpret_cast<PyObject*>(pyInfo));
|
||||
if (!result) {
|
||||
Py_DECREF(pyThing);
|
||||
// The python code did not even start, so let's finish (fail) the setup right away
|
||||
info->finish(Thing::ThingErrorSetupFailed);
|
||||
}
|
||||
|
|
@ -425,10 +504,12 @@ void PythonIntegrationPlugin::postSetupThing(Thing *thing)
|
|||
{
|
||||
PyThing* pyThing = m_things.value(thing);
|
||||
Py_INCREF(pyThing);
|
||||
|
||||
bool success = callPluginFunction("postSetupThing", reinterpret_cast<PyObject*>(pyThing));
|
||||
if (!success) {
|
||||
Py_DECREF(pyThing);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void PythonIntegrationPlugin::executeAction(ThingActionInfo *info)
|
||||
|
|
@ -438,19 +519,15 @@ void PythonIntegrationPlugin::executeAction(ThingActionInfo *info)
|
|||
PyGILState_STATE s = PyGILState_Ensure();
|
||||
|
||||
PyThingActionInfo *pyInfo = (PyThingActionInfo*)PyObject_CallObject((PyObject*)&PyThingActionInfoType, NULL);
|
||||
pyInfo->info = info;
|
||||
pyInfo->pyThing = pyThing;
|
||||
pyInfo->pyActionTypeId = PyUnicode_FromString(info->action().actionTypeId().toString().toUtf8());
|
||||
pyInfo->pyParams = PyParams_FromParamList(info->action().params());
|
||||
PyThingActionInfo_setInfo(pyInfo, info, pyThing);
|
||||
|
||||
PyGILState_Release(s);
|
||||
|
||||
connect(info, &ThingActionInfo::destroyed, this, [=](){
|
||||
QMutexLocker(pyInfo->mutex);
|
||||
pyInfo->pyActionTypeId = nullptr;
|
||||
Py_XDECREF(pyInfo->pyActionTypeId);
|
||||
auto s = PyGILState_Ensure();
|
||||
pyInfo->info = nullptr;
|
||||
Py_DECREF(pyInfo);
|
||||
PyGILState_Release(s);
|
||||
});
|
||||
|
||||
callPluginFunction("executeAction", reinterpret_cast<PyObject*>(pyInfo));
|
||||
|
|
@ -580,7 +657,7 @@ void PythonIntegrationPlugin::exportBrowserItemActionTypes(const ActionTypes &ac
|
|||
}
|
||||
|
||||
|
||||
bool PythonIntegrationPlugin::callPluginFunction(const QString &function, PyObject *param1, PyObject *param2)
|
||||
bool PythonIntegrationPlugin::callPluginFunction(const QString &function, PyObject *param1, PyObject *param2, PyObject *param3)
|
||||
{
|
||||
PyGILState_STATE s = PyGILState_Ensure();
|
||||
|
||||
|
|
@ -595,9 +672,7 @@ bool PythonIntegrationPlugin::callPluginFunction(const QString &function, PyObje
|
|||
}
|
||||
|
||||
|
||||
dumpError();
|
||||
|
||||
PyObject *result = PyObject_CallFunctionObjArgs(pFunc, param1, param2, nullptr);
|
||||
PyObject *result = PyObject_CallFunctionObjArgs(pFunc, param1, param2, param3, nullptr);
|
||||
|
||||
Py_XDECREF(pFunc);
|
||||
|
||||
|
|
@ -615,7 +690,7 @@ bool PythonIntegrationPlugin::callPluginFunction(const QString &function, PyObje
|
|||
return true;
|
||||
}
|
||||
|
||||
// Spawn a event loop for python
|
||||
// Spawn a event loop for the thread
|
||||
PyObject *new_event_loop = PyObject_GetAttrString(s_asyncio, "new_event_loop");
|
||||
PyObject *loop = PyObject_CallFunctionObjArgs(new_event_loop, nullptr);
|
||||
|
||||
|
|
@ -652,18 +727,3 @@ bool PythonIntegrationPlugin::callPluginFunction(const QString &function, PyObje
|
|||
return true;
|
||||
}
|
||||
|
||||
void PythonIntegrationPlugin::cleanupPyThing(PyThing *pyThing)
|
||||
{
|
||||
// It could happen that the python thread is currently holding the mutex
|
||||
// on the thing (e.g. PyThing_name).
|
||||
// We'd deadlock if we wait for the mutex forever here. So let's process events
|
||||
// while waiting for it...
|
||||
while (!pyThing->mutex->tryLock()) {
|
||||
qApp->processEvents(QEventLoop::EventLoopExec);
|
||||
}
|
||||
|
||||
pyThing->thing = nullptr;
|
||||
pyThing->mutex->unlock();
|
||||
Py_DECREF(pyThing);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -28,6 +28,8 @@ public:
|
|||
void init() override;
|
||||
void startMonitoringAutoThings() override;
|
||||
void discoverThings(ThingDiscoveryInfo *info) override;
|
||||
void startPairing(ThingPairingInfo *info) override;
|
||||
void confirmPairing(ThingPairingInfo *info, const QString &username, const QString &secret) override;
|
||||
void setupThing(ThingSetupInfo *info) override;
|
||||
void postSetupThing(Thing *thing) override;
|
||||
void executeAction(ThingActionInfo *info) override;
|
||||
|
|
@ -56,8 +58,7 @@ private:
|
|||
void exportBrowserItemActionTypes(const ActionTypes &actionTypes, const QString &thingClassName);
|
||||
|
||||
|
||||
bool callPluginFunction(const QString &function, PyObject *param1 = nullptr, PyObject *param2 = nullptr);
|
||||
void cleanupPyThing(PyThing *pyThing);
|
||||
bool callPluginFunction(const QString &function, PyObject *param1 = nullptr, PyObject *param2 = nullptr, PyObject *param3 = nullptr);
|
||||
|
||||
private:
|
||||
static PyThreadState* s_mainThread;
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ HEADERS += nymeacore.h \
|
|||
integrations/python/pythingactioninfo.h \
|
||||
integrations/python/pythingdescriptor.h \
|
||||
integrations/python/pythingdiscoveryinfo.h \
|
||||
integrations/python/pythingpairinginfo.h \
|
||||
integrations/python/pythingsetupinfo.h \
|
||||
integrations/python/pyutils.h \
|
||||
integrations/thingmanagerimplementation.h \
|
||||
|
|
|
|||
|
|
@ -117,3 +117,13 @@ void States::put(const QVariant &variant)
|
|||
{
|
||||
append(variant.value<State>());
|
||||
}
|
||||
|
||||
QVariant States::stateValue(const StateTypeId &stateTypeId)
|
||||
{
|
||||
foreach (const State & state, *this) {
|
||||
if (state.stateTypeId() == stateTypeId) {
|
||||
return state.value();
|
||||
}
|
||||
}
|
||||
return QVariant();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -69,6 +69,7 @@ public:
|
|||
States(const QList<State> &other);
|
||||
Q_INVOKABLE QVariant get(int index) const;
|
||||
Q_INVOKABLE void put(const QVariant &variant);
|
||||
Q_INVOKABLE QVariant stateValue(const StateTypeId &stateTypeId);
|
||||
};
|
||||
Q_DECLARE_METATYPE(States)
|
||||
|
||||
|
|
|
|||
|
|
@ -23,6 +23,15 @@
|
|||
"displayName": "Auto Python mock thing",
|
||||
"createMethods": ["auto"],
|
||||
"setupMethod": "justAdd",
|
||||
"paramTypes": [
|
||||
{
|
||||
"id": "8733557e-c599-4169-bcfa-5cc033c17b85",
|
||||
"name": "param1",
|
||||
"displayName": "Param 1",
|
||||
"type": "bool",
|
||||
"defaultValue": false
|
||||
}
|
||||
],
|
||||
"stateTypes": [
|
||||
{
|
||||
"id": "12c82472-56b0-4229-b324-4aaa6850320e",
|
||||
|
|
@ -58,6 +67,24 @@
|
|||
"displayName": "Python mock thing with discovery and pairing",
|
||||
"createMethods": ["discovery"],
|
||||
"setupMethod": "userAndPassword",
|
||||
"discoveryParamTypes": [
|
||||
{
|
||||
"id": "ef5f6b90-e9d8-4e77-a14d-6725cfb07116",
|
||||
"name": "resultCount",
|
||||
"displayName": "Result count",
|
||||
"type": "int",
|
||||
"defaultValue": 2
|
||||
}
|
||||
],
|
||||
"paramTypes": [
|
||||
{
|
||||
"id": "69328949-13ad-4bf4-b664-e7ee409ee51d",
|
||||
"name": "param1",
|
||||
"displayName": "Param 1",
|
||||
"type": "int",
|
||||
"defaultValue": 0
|
||||
}
|
||||
],
|
||||
"stateTypes": [
|
||||
{
|
||||
"id": "99d0af17-9e8c-42bb-bece-a5d114f051d3",
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ def configValueChanged(paramTypeId, value):
|
|||
for i in range(len(things), value):
|
||||
logger.log("Creating new auto thing")
|
||||
descriptor = nymea.ThingDescriptor(pyMockAutoThingClassId, "Python Mock auto thing")
|
||||
descriptor.params = [nymea.Param(pyMockAutoThingParam1ParamTypeId, True)]
|
||||
autoThingsAppeared([descriptor])
|
||||
|
||||
for i in range(value, len(things)):
|
||||
|
|
@ -30,6 +31,7 @@ def startMonitoringAutoThings():
|
|||
for i in range(len(things), configValue(pyMockPluginAutoThingCountParamTypeId)):
|
||||
logger.log("Creating new auto thing")
|
||||
descriptor = nymea.ThingDescriptor(pyMockAutoThingClassId, "Python Mock auto thing")
|
||||
descriptor.params = [nymea.Param(pyMockAutoThingParam1ParamTypeId, True)]
|
||||
autoThingsAppeared([descriptor])
|
||||
for i in range(configValue(pyMockPluginAutoThingCountParamTypeId), len(things)):
|
||||
logger.log("Removing auto thing")
|
||||
|
|
@ -39,16 +41,55 @@ def startMonitoringAutoThings():
|
|||
|
||||
|
||||
async def discoverThings(info):
|
||||
await asyncio.sleep(1)
|
||||
descriptor = nymea.ThingDescriptor(pyMockThingClassId, "Python mock thing")
|
||||
info.addDescriptor(descriptor)
|
||||
logger.log("Discovery started for") #, info.thingClassId, "with result count:") #, info.params[0].value)
|
||||
logger.log("a")
|
||||
await asyncio.sleep(1) # Some delay for giving a feeling of a discovery
|
||||
# Add 2 new discovery results
|
||||
logger.log("b")
|
||||
info.addDescriptor(nymea.ThingDescriptor(pyMockDiscoveryPairingThingClassId, "Python mock thing 1"))
|
||||
info.addDescriptor(nymea.ThingDescriptor(pyMockDiscoveryPairingThingClassId, "Python mock thing 2"))
|
||||
logger.log("c")
|
||||
# Also add existing ones again so reconfiguration is possible
|
||||
for thing in myThings():
|
||||
logger.log("d")
|
||||
if thing.thingClassId == pyMockDiscoveryPairingThingClassId:
|
||||
logger.log("e")
|
||||
info.addDescriptor(nymea.ThingDescriptor(pyMockDiscoveryPairingThingClassId, thing.name, thingId=thing.id))
|
||||
logger.log("f")
|
||||
|
||||
logger.log("g")
|
||||
info.finish(nymea.ThingErrorNoError)
|
||||
logger.log("h")
|
||||
|
||||
|
||||
async def startPairing(info):
|
||||
logger.log("startPairing for", info.thingName, info.thingId, info.params)
|
||||
info.finish(nymea.ThingErrorNoError, "Log in as user \"john\" with password \"smith\".")
|
||||
|
||||
|
||||
async def confirmPairing(info, username, secret):
|
||||
logger.log("confirming pairing for", info.thingName, username, secret)
|
||||
await asyncio.sleep(1)
|
||||
if username == "john" and secret == "smith":
|
||||
info.finish(nymea.ThingErrorNoError)
|
||||
else:
|
||||
info.finish(nymea.ThingErrorAuthenticationFailure, "Error logging in here!")
|
||||
|
||||
|
||||
async def setupThing(info):
|
||||
# logger.log("setupThing for", info.thing.name, info.thing.params)
|
||||
logger.log("setupThing for", info.thing.name)
|
||||
info.finish(nymea.ThingErrorNoError)
|
||||
|
||||
|
||||
def postSetupThing(thing):
|
||||
logger.log("postSetupThing for", thing.name, thing.params[0].value)
|
||||
thing.nameChangedHandler = lambda thing : logger.log("Thing name changed", thing.name)
|
||||
|
||||
if thing.thingClassId == pyMockAutoThingClassId:
|
||||
logger.log("State 1 value:", thing.stateValue(pyMockAutoState1StateTypeId))
|
||||
|
||||
|
||||
def autoThings():
|
||||
autoThings = []
|
||||
for thing in myThings():
|
||||
|
|
|
|||
Loading…
Reference in New Issue