From 1ec84005c0238c46f0324f2e716267d73686faed Mon Sep 17 00:00:00 2001 From: johan Date: Sat, 30 Nov 2013 17:41:38 +0100 Subject: [PATCH] Changed hyperion args variable in module from string to dict Former-commit-id: ae94dca70590e3ed15e991172b7458cf4298e655 --- include/effectengine/EffectEngine.h | 6 +++- libsrc/effectengine/CMakeLists.txt | 1 + libsrc/effectengine/Effect.cpp | 47 ++++++++++++++++++++++++++-- libsrc/effectengine/Effect.h | 12 ++++--- libsrc/effectengine/EffectEngine.cpp | 2 +- 5 files changed, 59 insertions(+), 9 deletions(-) diff --git a/include/effectengine/EffectEngine.h b/include/effectengine/EffectEngine.h index baed604b..3e08f491 100644 --- a/include/effectengine/EffectEngine.h +++ b/include/effectengine/EffectEngine.h @@ -1,5 +1,9 @@ #pragma once +// Json includes +#include + +// Hyperion includes #include // pre-declarioation @@ -30,7 +34,7 @@ public: struct EffectDefinition { std::string script; - std::string args; + Json::Value args; }; private slots: diff --git a/libsrc/effectengine/CMakeLists.txt b/libsrc/effectengine/CMakeLists.txt index d63fe76e..5e50dee3 100644 --- a/libsrc/effectengine/CMakeLists.txt +++ b/libsrc/effectengine/CMakeLists.txt @@ -32,5 +32,6 @@ add_library(effectengine target_link_libraries(effectengine hyperion + jsoncpp ${QT_LIBRARIES} ${PYTHON_LIBRARIES}) diff --git a/libsrc/effectengine/Effect.cpp b/libsrc/effectengine/Effect.cpp index 54ffd739..730287eb 100644 --- a/libsrc/effectengine/Effect.cpp +++ b/libsrc/effectengine/Effect.cpp @@ -1,3 +1,6 @@ +// Python include +#include + // stl includes #include #include @@ -17,7 +20,7 @@ PyMethodDef Effect::effectMethods[] = { }; -Effect::Effect(int priority, int timeout, const std::string & script, const std::string & args) : +Effect::Effect(int priority, int timeout, const std::string & script, const Json::Value & args) : QThread(), _priority(priority), _timeout(timeout), @@ -50,7 +53,8 @@ void Effect::run() PyObject_SetAttrString(module, "ledCount", Py_BuildValue("i", _imageProcessor->getLedCount())); // add a args variable to the interpreter - PyObject_SetAttrString(module, "args", Py_BuildValue("s", _args.c_str())); + PyObject_SetAttrString(module, "args", json2python(_args)); + //PyObject_SetAttrString(module, "args", Py_BuildValue("s", _args.c_str())); // Set the end time if applicable if (_timeout > 0) @@ -95,6 +99,45 @@ void Effect::effectFinished() emit effectFinished(this); } +PyObject *Effect::json2python(const Json::Value &json) const +{ + switch (json.type()) + { + case Json::nullValue: + return Py_BuildValue(""); + case Json::realValue: + return Py_BuildValue("d", json.asDouble()); + case Json::intValue: + case Json::uintValue: + return Py_BuildValue("i", json.asInt()); + case Json::booleanValue: + return Py_BuildValue("i", json.asBool() ? 1 : 0); + case Json::stringValue: + return Py_BuildValue("s", json.asCString()); + case Json::objectValue: + { + PyObject * obj = PyDict_New(); + for (Json::Value::iterator i = json.begin(); i != json.end(); ++i) + { + PyDict_SetItemString(obj, i.memberName(), json2python(*i)); + } + return obj; + } + case Json::arrayValue: + { + PyObject * list = PyList_New(json.size()); + for (Json::Value::iterator i = json.begin(); i != json.end(); ++i) + { + PyList_SetItem(list, i.index(), json2python(*i)); + } + return list; + } + } + + assert(false); + return nullptr; +} + PyObject* Effect::wrapSetColor(PyObject *self, PyObject *args) { // get the effect diff --git a/libsrc/effectengine/Effect.h b/libsrc/effectengine/Effect.h index d464f70d..f162c860 100644 --- a/libsrc/effectengine/Effect.h +++ b/libsrc/effectengine/Effect.h @@ -1,11 +1,11 @@ #pragma once -// Qt includes -#include - // Python includes #include +// Qt includes +#include + // Hyperion includes #include @@ -14,7 +14,7 @@ class Effect : public QThread Q_OBJECT public: - Effect(int priority, int timeout, const std::string & script, const std::string & args = ""); + Effect(int priority, int timeout, const std::string & script, const Json::Value & args = Json::Value()); virtual ~Effect(); virtual void run(); @@ -35,6 +35,8 @@ private slots: void effectFinished(); private: + PyObject * json2python(const Json::Value & json) const; + // Wrapper methods for Python interpreter extra buildin methods static PyMethodDef effectMethods[]; static PyObject* wrapSetColor(PyObject *self, PyObject *args); @@ -49,7 +51,7 @@ private: const std::string & _script; - const std::string & _args; + const Json::Value & _args; int64_t _endTime; diff --git a/libsrc/effectengine/EffectEngine.cpp b/libsrc/effectengine/EffectEngine.cpp index d28cdae5..878edd62 100644 --- a/libsrc/effectengine/EffectEngine.cpp +++ b/libsrc/effectengine/EffectEngine.cpp @@ -25,7 +25,7 @@ EffectEngine::EffectEngine(Hyperion * hyperion, const Json::Value & jsonEffectCo for (const std::string & name : effectNames) { const Json::Value & info = jsonEffectConfig[name]; - _availableEffects[name] = {info["script"].asString(), Json::FastWriter().write(info["args"])}; + _availableEffects[name] = {info["script"].asString(), info["args"]}; } // initialize the python interpreter