Added checking for timeout of effect

Former-commit-id: 7768368d1a386447714610c28c50e3658b6037c5
This commit is contained in:
Johan 2013-11-28 14:29:31 +01:00
parent cec626f3ae
commit c82954d82a
2 changed files with 32 additions and 1 deletions

View File

@ -2,6 +2,9 @@
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
// Qt includes
#include <QDateTime>
// effect engin eincludes // effect engin eincludes
#include "Effect.h" #include "Effect.h"
@ -19,6 +22,7 @@ Effect::Effect(int priority, int timeout) :
QThread(), QThread(),
_priority(priority), _priority(priority),
_timeout(timeout), _timeout(timeout),
_endTime(-1),
_interpreterThreadState(nullptr), _interpreterThreadState(nullptr),
_abortRequested(false) _abortRequested(false)
{ {
@ -40,6 +44,12 @@ void Effect::run()
PyObject * thisCapsule = PyCapsule_New(this, nullptr, nullptr); PyObject * thisCapsule = PyCapsule_New(this, nullptr, nullptr);
Py_InitModule4("hyperion", effectMethods, nullptr, thisCapsule, PYTHON_API_VERSION); 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 // Run the effect script
std::string script = "test.py"; std::string script = "test.py";
FILE* file = fopen(script.c_str(), "r"); FILE* file = fopen(script.c_str(), "r");
@ -68,21 +78,39 @@ void Effect::effectFinished()
PyObject* Effect::wrapSetColor(PyObject *self, PyObject *args) PyObject* Effect::wrapSetColor(PyObject *self, PyObject *args)
{ {
Effect * effect = getEffect(self);
return Py_BuildValue("i", 42); return Py_BuildValue("i", 42);
} }
PyObject* Effect::wrapSetImage(PyObject *self, PyObject *args) PyObject* Effect::wrapSetImage(PyObject *self, PyObject *args)
{ {
Effect * effect = getEffect(self);
return Py_BuildValue("i", 42); return Py_BuildValue("i", 42);
} }
PyObject* Effect::wrapGetLedCount(PyObject *self, PyObject *args) PyObject* Effect::wrapGetLedCount(PyObject *self, PyObject *args)
{ {
Effect * effect = getEffect(self);
return Py_BuildValue("i", 42); return Py_BuildValue("i", 42);
} }
PyObject* Effect::wrapAbort(PyObject *self, PyObject *) PyObject* Effect::wrapAbort(PyObject *self, PyObject *)
{ {
Effect * effect = reinterpret_cast<Effect *>(PyCapsule_GetPointer(self, nullptr)); Effect * effect = getEffect(self);
return Py_BuildValue("i", effect->_abortRequested ? 1 : 0); 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<Effect *>(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;
}

View File

@ -34,12 +34,15 @@ private:
static PyObject* wrapSetImage(PyObject *self, PyObject *args); static PyObject* wrapSetImage(PyObject *self, PyObject *args);
static PyObject* wrapGetLedCount(PyObject *self, PyObject *args); static PyObject* wrapGetLedCount(PyObject *self, PyObject *args);
static PyObject* wrapAbort(PyObject *self, PyObject *args); static PyObject* wrapAbort(PyObject *self, PyObject *args);
static Effect * getEffect(PyObject *self);
private: private:
const int _priority; const int _priority;
const int _timeout; const int _timeout;
int64_t _endTime;
PyThreadState * _interpreterThreadState; PyThreadState * _interpreterThreadState;
bool _abortRequested; bool _abortRequested;