From c82954d82a25d88608d1419879af1b995e0cca63 Mon Sep 17 00:00:00 2001 From: Johan Date: Thu, 28 Nov 2013 14:29:31 +0100 Subject: [PATCH] Added checking for timeout of effect Former-commit-id: 7768368d1a386447714610c28c50e3658b6037c5 --- libsrc/effectengine/Effect.cpp | 30 +++++++++++++++++++++++++++++- libsrc/effectengine/Effect.h | 3 +++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/libsrc/effectengine/Effect.cpp b/libsrc/effectengine/Effect.cpp index 0e2a7ffc..498f0bde 100644 --- a/libsrc/effectengine/Effect.cpp +++ b/libsrc/effectengine/Effect.cpp @@ -2,6 +2,9 @@ #include #include +// Qt includes +#include + // effect engin eincludes #include "Effect.h" @@ -19,6 +22,7 @@ Effect::Effect(int priority, int timeout) : QThread(), _priority(priority), _timeout(timeout), + _endTime(-1), _interpreterThreadState(nullptr), _abortRequested(false) { @@ -40,6 +44,12 @@ void Effect::run() PyObject * thisCapsule = PyCapsule_New(this, nullptr, nullptr); Py_InitModule4("hyperion", effectMethods, nullptr, thisCapsule, PYTHON_API_VERSION); + // Set the end time if applicable + if (_timeout > 0) + { + _endTime = QDateTime::currentMSecsSinceEpoch() + _timeout; + } + // Run the effect script std::string script = "test.py"; FILE* file = fopen(script.c_str(), "r"); @@ -68,21 +78,39 @@ void Effect::effectFinished() PyObject* Effect::wrapSetColor(PyObject *self, PyObject *args) { + Effect * effect = getEffect(self); return Py_BuildValue("i", 42); } PyObject* Effect::wrapSetImage(PyObject *self, PyObject *args) { + Effect * effect = getEffect(self); return Py_BuildValue("i", 42); } PyObject* Effect::wrapGetLedCount(PyObject *self, PyObject *args) { + Effect * effect = getEffect(self); return Py_BuildValue("i", 42); } PyObject* Effect::wrapAbort(PyObject *self, PyObject *) { - Effect * effect = reinterpret_cast(PyCapsule_GetPointer(self, nullptr)); + Effect * effect = getEffect(self); return Py_BuildValue("i", effect->_abortRequested ? 1 : 0); } + +Effect * Effect::getEffect(PyObject *self) +{ + // Get the effect from the capsule in the self pointer + Effect * effect = reinterpret_cast(PyCapsule_GetPointer(self, nullptr)); + + // Test if the effect has reached it end time + if (effect->_timeout > 0 && QDateTime::currentMSecsSinceEpoch() > effect->_endTime) + { + effect->_abortRequested = true; + } + + // return the effect + return effect; +} diff --git a/libsrc/effectengine/Effect.h b/libsrc/effectengine/Effect.h index 9c16aa64..2521a967 100644 --- a/libsrc/effectengine/Effect.h +++ b/libsrc/effectengine/Effect.h @@ -34,11 +34,14 @@ private: static PyObject* wrapSetImage(PyObject *self, PyObject *args); static PyObject* wrapGetLedCount(PyObject *self, PyObject *args); static PyObject* wrapAbort(PyObject *self, PyObject *args); + static Effect * getEffect(PyObject *self); private: const int _priority; const int _timeout; + + int64_t _endTime; PyThreadState * _interpreterThreadState;