mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
JsonUtils & improvements (#476)
* add JsonUtils * update * repair * update * ident * Schema correct msg other adjusts * fix effDel, ExceptionLog, cleanup * fix travis qt5.2 * not so funny * use Qthread interrupt instead abort bool * update services
This commit is contained in:
@@ -78,9 +78,8 @@ void Effect::registerHyperionExtensionModule()
|
||||
PyImport_AppendInittab("hyperion", &PyInit_hyperion);
|
||||
}
|
||||
|
||||
Effect::Effect(PyThreadState * mainThreadState, int priority, int timeout, const QString & script, const QString & name, const QJsonObject & args, const QString & origin, unsigned smoothCfg)
|
||||
Effect::Effect(int priority, int timeout, const QString & script, const QString & name, const QJsonObject & args, const QString & origin, unsigned smoothCfg)
|
||||
: QThread()
|
||||
, _mainThreadState(mainThreadState)
|
||||
, _priority(priority)
|
||||
, _timeout(timeout)
|
||||
, _script(script)
|
||||
@@ -89,7 +88,6 @@ Effect::Effect(PyThreadState * mainThreadState, int priority, int timeout, const
|
||||
, _args(args)
|
||||
, _endTime(-1)
|
||||
, _interpreterThreadState(nullptr)
|
||||
, _abortRequested(false)
|
||||
, _imageProcessor(ImageProcessorFactory::getInstance().newImageProcessor())
|
||||
, _colors()
|
||||
, _origin(origin)
|
||||
@@ -99,15 +97,15 @@ Effect::Effect(PyThreadState * mainThreadState, int priority, int timeout, const
|
||||
_colors.resize(_imageProcessor->getLedCount());
|
||||
_colors.fill(ColorRgb::BLACK);
|
||||
|
||||
_log = Logger::getInstance("EFFECTENGINE");
|
||||
|
||||
// disable the black border detector for effects
|
||||
_imageProcessor->enableBlackBorderDetector(false);
|
||||
|
||||
|
||||
// init effect image for image based effects, size is based on led layout
|
||||
_image.fill(Qt::black);
|
||||
_painter = new QPainter(&_image);
|
||||
|
||||
// connect the finished signal
|
||||
connect(this, SIGNAL(finished()), this, SLOT(effectFinished()));
|
||||
Q_INIT_RESOURCE(EffectEngine);
|
||||
}
|
||||
|
||||
@@ -120,7 +118,7 @@ Effect::~Effect()
|
||||
void Effect::run()
|
||||
{
|
||||
// switch to the main thread state and acquire the GIL
|
||||
PyEval_RestoreThread(_mainThreadState);
|
||||
PyEval_AcquireLock();
|
||||
|
||||
// Initialize a new thread state
|
||||
_interpreterThreadState = Py_NewInterpreter();
|
||||
@@ -158,13 +156,109 @@ void Effect::run()
|
||||
}
|
||||
else
|
||||
{
|
||||
Error(Logger::getInstance("EFFECTENGINE"), "Unable to open script file %s.", QSTRING_CSTR(_script));
|
||||
Error(_log, "Unable to open script file %s.", QSTRING_CSTR(_script));
|
||||
}
|
||||
file.close();
|
||||
|
||||
if (!python_code.isEmpty())
|
||||
{
|
||||
PyRun_SimpleString(python_code.constData());
|
||||
PyObject *main_module = PyImport_ImportModule("__main__"); // New Reference
|
||||
PyObject *main_dict = PyModule_GetDict(main_module); // Borrowed reference
|
||||
Py_INCREF(main_dict); // Incref "main_dict" to use it in PyRun_String(), because PyModule_GetDict() has decref "main_dict"
|
||||
Py_DECREF(main_module); // // release "main_module" when done
|
||||
PyObject *result = PyRun_String(python_code.constData(), Py_file_input, main_dict, main_dict); // New Reference
|
||||
|
||||
if (!result)
|
||||
{
|
||||
if (PyErr_Occurred()) // Nothing needs to be done for a borrowed reference
|
||||
{
|
||||
Error(_log,"###### PYTHON EXCEPTION ######");
|
||||
Error(_log,"## In effect '%s'", QSTRING_CSTR(_name));
|
||||
/* Objects all initialized to NULL for Py_XDECREF */
|
||||
PyObject *errorType = NULL, *errorValue = NULL, *errorTraceback = NULL;
|
||||
|
||||
PyErr_Fetch(&errorType, &errorValue, &errorTraceback); // New Reference or NULL
|
||||
PyErr_NormalizeException(&errorType, &errorValue, &errorTraceback);
|
||||
|
||||
// Extract exception message from "errorValue"
|
||||
if(errorValue)
|
||||
{
|
||||
QString message;
|
||||
if(PyObject_HasAttrString(errorValue, "__class__"))
|
||||
{
|
||||
PyObject *classPtr = PyObject_GetAttrString(errorValue, "__class__"); // New Reference
|
||||
PyObject *class_name = NULL; /* Object "class_name" initialized to NULL for Py_XDECREF */
|
||||
class_name = PyObject_GetAttrString(classPtr, "__name__"); // New Reference or NULL
|
||||
|
||||
if(class_name && PyString_Check(class_name))
|
||||
message.append(PyString_AsString(class_name));
|
||||
|
||||
Py_DECREF(classPtr); // release "classPtr" when done
|
||||
Py_XDECREF(class_name); // Use Py_XDECREF() to ignore NULL references
|
||||
}
|
||||
|
||||
// Object "class_name" initialized to NULL for Py_XDECREF
|
||||
PyObject *valueString = NULL;
|
||||
valueString = PyObject_Str(errorValue); // New Reference or NULL
|
||||
|
||||
if(valueString && PyString_Check(valueString))
|
||||
{
|
||||
if(!message.isEmpty())
|
||||
message.append(": ");
|
||||
|
||||
message.append(PyString_AsString(valueString));
|
||||
}
|
||||
Py_XDECREF(valueString); // Use Py_XDECREF() to ignore NULL references
|
||||
|
||||
Error(_log, "## %s", QSTRING_CSTR(message));
|
||||
}
|
||||
|
||||
// Extract exception message from "errorTraceback"
|
||||
if(errorTraceback)
|
||||
{
|
||||
// Object "tracebackList" initialized to NULL for Py_XDECREF
|
||||
PyObject *tracebackModule = NULL, *methodName = NULL, *tracebackList = NULL;
|
||||
QString tracebackMsg;
|
||||
|
||||
tracebackModule = PyImport_ImportModule("traceback"); // New Reference or NULL
|
||||
methodName = PyString_FromString("format_exception"); // New Reference or NULL
|
||||
tracebackList = PyObject_CallMethodObjArgs(tracebackModule, methodName, errorType, errorValue, errorTraceback, NULL); // New Reference or NULL
|
||||
|
||||
if(tracebackList)
|
||||
{
|
||||
PyObject* iterator = PyObject_GetIter(tracebackList); // New Reference
|
||||
|
||||
PyObject* item;
|
||||
while( (item = PyIter_Next(iterator)) ) // New Reference
|
||||
{
|
||||
Error(_log, "## %s",QSTRING_CSTR(QString(PyString_AsString(item)).trimmed()));
|
||||
Py_DECREF(item); // release "item" when done
|
||||
}
|
||||
Py_DECREF(iterator); // release "iterator" when done
|
||||
}
|
||||
|
||||
// Use Py_XDECREF() to ignore NULL references
|
||||
Py_XDECREF(tracebackModule);
|
||||
Py_XDECREF(methodName);
|
||||
Py_XDECREF(tracebackList);
|
||||
|
||||
// Give the exception back to python and print it to stderr in case anyone else wants it.
|
||||
Py_XINCREF(errorType);
|
||||
Py_XINCREF(errorValue);
|
||||
Py_XINCREF(errorTraceback);
|
||||
|
||||
PyErr_Restore(errorType, errorValue, errorTraceback);
|
||||
//PyErr_PrintEx(0); // Remove this line to switch off stderr output
|
||||
}
|
||||
Error(_log,"###### EXCEPTION END ######");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Py_DECREF(result); // release "result" when done
|
||||
}
|
||||
|
||||
Py_DECREF(main_dict); // release "main_dict" when done
|
||||
}
|
||||
|
||||
// Clean up the thread state
|
||||
@@ -173,28 +267,8 @@ void Effect::run()
|
||||
PyEval_ReleaseLock();
|
||||
}
|
||||
|
||||
int Effect::getPriority() const
|
||||
{
|
||||
return _priority;
|
||||
}
|
||||
|
||||
bool Effect::isAbortRequested() const
|
||||
{
|
||||
return _abortRequested;
|
||||
}
|
||||
|
||||
void Effect::abort()
|
||||
{
|
||||
_abortRequested = true;
|
||||
}
|
||||
|
||||
void Effect::effectFinished()
|
||||
{
|
||||
emit effectFinished(this);
|
||||
}
|
||||
|
||||
PyObject *Effect::json2python(const QJsonValue &jsonData) const
|
||||
{
|
||||
{
|
||||
switch (jsonData.type())
|
||||
{
|
||||
case QJsonValue::Null:
|
||||
@@ -251,7 +325,7 @@ PyObject* Effect::wrapSetColor(PyObject *self, PyObject *args)
|
||||
Effect * effect = getEffect();
|
||||
|
||||
// check if we have aborted already
|
||||
if (effect->_abortRequested)
|
||||
if (effect->isInterruptionRequested())
|
||||
{
|
||||
return Py_BuildValue("");
|
||||
}
|
||||
@@ -333,7 +407,7 @@ PyObject* Effect::wrapSetImage(PyObject *self, PyObject *args)
|
||||
Effect * effect = getEffect();
|
||||
|
||||
// check if we have aborted already
|
||||
if (effect->_abortRequested)
|
||||
if (effect->isInterruptionRequested())
|
||||
{
|
||||
return Py_BuildValue("");
|
||||
}
|
||||
@@ -398,10 +472,10 @@ PyObject* Effect::wrapAbort(PyObject *self, PyObject *)
|
||||
// Test if the effect has reached it end time
|
||||
if (effect->_timeout > 0 && QDateTime::currentMSecsSinceEpoch() > effect->_endTime)
|
||||
{
|
||||
effect->_abortRequested = true;
|
||||
effect->requestInterruption();
|
||||
}
|
||||
|
||||
return Py_BuildValue("i", effect->_abortRequested ? 1 : 0);
|
||||
return Py_BuildValue("i", effect->isInterruptionRequested() ? 1 : 0);
|
||||
}
|
||||
|
||||
|
||||
@@ -453,7 +527,7 @@ PyObject* Effect::wrapImageShow(PyObject *self, PyObject *args)
|
||||
binaryImage.append((char) qBlue(scanline[j]));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
memcpy(image.memptr(), binaryImage.data(), binaryImage.size());
|
||||
std::vector<ColorRgb> v = effect->_colors.toStdVector();
|
||||
effect->_imageProcessor->process(image, v);
|
||||
@@ -513,7 +587,7 @@ PyObject* Effect::wrapImageLinearGradient(PyObject *self, PyObject *args)
|
||||
|
||||
gradient.setSpread(static_cast<QGradient::Spread>(spread));
|
||||
effect->_painter->fillRect(myQRect, gradient);
|
||||
|
||||
|
||||
return Py_BuildValue("");
|
||||
}
|
||||
else
|
||||
@@ -580,7 +654,7 @@ PyObject* Effect::wrapImageConicalGradient(PyObject *self, PyObject *args)
|
||||
}
|
||||
|
||||
effect->_painter->fillRect(myQRect, gradient);
|
||||
|
||||
|
||||
return Py_BuildValue("");
|
||||
}
|
||||
else
|
||||
@@ -616,7 +690,7 @@ PyObject* Effect::wrapImageRadialGradient(PyObject *self, PyObject *args)
|
||||
if ( argCount == 12 && PyArg_ParseTuple(args, "iiiiiiiiiiOi", &startX, &startY, &width, &height, ¢erX, ¢erY, &radius, &focalX, &focalY, &focalRadius, &bytearray, &spread) )
|
||||
{
|
||||
argsOK = true;
|
||||
}
|
||||
}
|
||||
if ( argCount == 9 && PyArg_ParseTuple(args, "iiiiiiiOi", &startX, &startY, &width, &height, ¢erX, ¢erY, &radius, &bytearray, &spread) )
|
||||
{
|
||||
argsOK = true;
|
||||
@@ -635,7 +709,7 @@ PyObject* Effect::wrapImageRadialGradient(PyObject *self, PyObject *args)
|
||||
focalY = centerY;
|
||||
focalRadius = radius;
|
||||
}
|
||||
|
||||
|
||||
if (argsOK)
|
||||
{
|
||||
if (PyByteArray_Check(bytearray))
|
||||
@@ -661,7 +735,7 @@ PyObject* Effect::wrapImageRadialGradient(PyObject *self, PyObject *args)
|
||||
|
||||
gradient.setSpread(static_cast<QGradient::Spread>(spread));
|
||||
effect->_painter->fillRect(myQRect, gradient);
|
||||
|
||||
|
||||
return Py_BuildValue("");
|
||||
}
|
||||
else
|
||||
@@ -742,7 +816,7 @@ PyObject* Effect::wrapImageDrawPie(PyObject *self, PyObject *args)
|
||||
{
|
||||
Effect * effect = getEffect();
|
||||
PyObject * bytearray = nullptr;
|
||||
|
||||
|
||||
QString brush;
|
||||
int argCount = PyTuple_Size(args);
|
||||
int radius, centerX, centerY;
|
||||
@@ -804,7 +878,7 @@ PyObject* Effect::wrapImageDrawPie(PyObject *self, PyObject *args)
|
||||
));
|
||||
}
|
||||
painter->setBrush(gradient);
|
||||
|
||||
|
||||
return Py_BuildValue("");
|
||||
}
|
||||
else
|
||||
@@ -908,7 +982,7 @@ PyObject* Effect::wrapImageDrawLine(PyObject *self, PyObject *args)
|
||||
painter->setPen(newPen);
|
||||
painter->drawLine(startX, startY, endX, endY);
|
||||
painter->setPen(oldPen);
|
||||
|
||||
|
||||
return Py_BuildValue("");
|
||||
}
|
||||
return nullptr;
|
||||
@@ -943,7 +1017,7 @@ PyObject* Effect::wrapImageDrawPoint(PyObject *self, PyObject *args)
|
||||
painter->setPen(newPen);
|
||||
painter->drawPoint(x, y);
|
||||
painter->setPen(oldPen);
|
||||
|
||||
|
||||
return Py_BuildValue("");
|
||||
}
|
||||
return nullptr;
|
||||
@@ -983,7 +1057,7 @@ PyObject* Effect::wrapImageDrawRect(PyObject *self, PyObject *args)
|
||||
painter->setPen(newPen);
|
||||
painter->drawRect(startX, startY, width, height);
|
||||
painter->setPen(oldPen);
|
||||
|
||||
|
||||
return Py_BuildValue("");
|
||||
}
|
||||
return nullptr;
|
||||
@@ -1028,7 +1102,7 @@ PyObject* Effect::wrapImageSave(PyObject *self, PyObject *args)
|
||||
QImage img(effect->_image.copy());
|
||||
effect->_imageStack.append(img);
|
||||
|
||||
return Py_BuildValue("i", effect->_imageStack.size()-1);
|
||||
return Py_BuildValue("i", effect->_imageStack.size()-1);
|
||||
}
|
||||
|
||||
PyObject* Effect::wrapImageMinSize(PyObject *self, PyObject *args)
|
||||
@@ -1045,7 +1119,7 @@ PyObject* Effect::wrapImageMinSize(PyObject *self, PyObject *args)
|
||||
if (width<w || height<h)
|
||||
{
|
||||
delete effect->_painter;
|
||||
|
||||
|
||||
effect->_image = effect->_image.scaled(qMax(width,w),qMax(height,h), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
|
||||
effect->_imageSize = effect->_image.size();
|
||||
effect->_painter = new QPainter(&(effect->_image));
|
||||
@@ -1070,10 +1144,10 @@ PyObject* Effect::wrapImageHeight(PyObject *self, PyObject *args)
|
||||
PyObject* Effect::wrapImageCRotate(PyObject *self, PyObject *args)
|
||||
{
|
||||
Effect * effect = getEffect();
|
||||
|
||||
|
||||
int argCount = PyTuple_Size(args);
|
||||
int angle;
|
||||
|
||||
|
||||
if ( argCount == 1 && PyArg_ParseTuple(args, "i", &angle ) )
|
||||
{
|
||||
angle = qMax(qMin(angle,360),0);
|
||||
@@ -1086,16 +1160,16 @@ PyObject* Effect::wrapImageCRotate(PyObject *self, PyObject *args)
|
||||
PyObject* Effect::wrapImageCOffset(PyObject *self, PyObject *args)
|
||||
{
|
||||
Effect * effect = getEffect();
|
||||
|
||||
|
||||
int offsetX = 0;
|
||||
int offsetY = 0;
|
||||
int argCount = PyTuple_Size(args);
|
||||
|
||||
|
||||
if ( argCount == 2 )
|
||||
{
|
||||
PyArg_ParseTuple(args, "ii", &offsetX, &offsetY );
|
||||
}
|
||||
|
||||
|
||||
effect->_painter->translate(QPoint(offsetX,offsetY));
|
||||
return Py_BuildValue("");
|
||||
}
|
||||
@@ -1103,10 +1177,10 @@ PyObject* Effect::wrapImageCOffset(PyObject *self, PyObject *args)
|
||||
PyObject* Effect::wrapImageCShear(PyObject *self, PyObject *args)
|
||||
{
|
||||
Effect * effect = getEffect();
|
||||
|
||||
|
||||
int sh,sv;
|
||||
int argCount = PyTuple_Size(args);
|
||||
|
||||
|
||||
if ( argCount == 2 && PyArg_ParseTuple(args, "ii", &sh, &sv ))
|
||||
{
|
||||
effect->_painter->shear(sh,sv);
|
||||
|
@@ -19,36 +19,26 @@ class Effect : public QThread
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
Effect(PyThreadState * mainThreadState, int priority, int timeout, const QString & script, const QString & name, const QJsonObject & args = QJsonObject(), const QString & origin="System", unsigned smoothCfg=0);
|
||||
Effect(int priority, int timeout, const QString & script, const QString & name, const QJsonObject & args = QJsonObject(), const QString & origin="System", unsigned smoothCfg=0);
|
||||
virtual ~Effect();
|
||||
|
||||
virtual void run();
|
||||
|
||||
int getPriority() const;
|
||||
|
||||
int getPriority() const { return _priority; };
|
||||
|
||||
QString getScript() const { return _script; }
|
||||
QString getName() const { return _name; }
|
||||
|
||||
int getTimeout() const {return _timeout; }
|
||||
|
||||
QJsonObject getArgs() const { return _args; }
|
||||
|
||||
bool isAbortRequested() const;
|
||||
int getTimeout() const {return _timeout; }
|
||||
|
||||
QJsonObject getArgs() const { return _args; }
|
||||
|
||||
/// This function registers the extension module in Python
|
||||
static void registerHyperionExtensionModule();
|
||||
|
||||
public slots:
|
||||
void abort();
|
||||
|
||||
signals:
|
||||
void effectFinished(Effect * effect);
|
||||
|
||||
void setColors(int priority, const std::vector<ColorRgb> &ledColors, const int timeout_ms, bool clearEffects, hyperion::Components componentconst, QString origin, unsigned smoothCfg);
|
||||
|
||||
private slots:
|
||||
void effectFinished();
|
||||
|
||||
private:
|
||||
PyObject * json2python(const QJsonValue & jsonData) const;
|
||||
|
||||
@@ -88,8 +78,6 @@ private:
|
||||
|
||||
void addImage();
|
||||
|
||||
PyThreadState * _mainThreadState;
|
||||
|
||||
const int _priority;
|
||||
|
||||
const int _timeout;
|
||||
@@ -104,19 +92,17 @@ private:
|
||||
|
||||
PyThreadState * _interpreterThreadState;
|
||||
|
||||
bool _abortRequested;
|
||||
|
||||
/// The processor for translating images to led-values
|
||||
ImageProcessor * _imageProcessor;
|
||||
|
||||
/// Buffer for colorData
|
||||
QVector<ColorRgb> _colors;
|
||||
|
||||
|
||||
|
||||
Logger* _log;
|
||||
|
||||
QString _origin;
|
||||
QSize _imageSize;
|
||||
QImage _image;
|
||||
QPainter* _painter;
|
||||
QVector<QImage> _imageStack;
|
||||
};
|
||||
|
||||
|
@@ -11,7 +11,7 @@
|
||||
|
||||
// hyperion util includes
|
||||
#include <utils/jsonschema/QJsonSchemaChecker.h>
|
||||
#include <utils/FileUtils.h>
|
||||
#include <utils/JsonUtils.h>
|
||||
#include <utils/Components.h>
|
||||
|
||||
// effect engine includes
|
||||
@@ -74,95 +74,21 @@ const std::list<ActiveEffectDefinition> &EffectEngine::getActiveEffects()
|
||||
|
||||
bool EffectEngine::loadEffectDefinition(const QString &path, const QString &effectConfigFile, EffectDefinition & effectDefinition)
|
||||
{
|
||||
Logger * log = Logger::getInstance("EFFECTENGINE");
|
||||
|
||||
QString fileName = path + QDir::separator() + effectConfigFile;
|
||||
QJsonParseError error;
|
||||
|
||||
// ---------- Read the effect json config file ----------
|
||||
|
||||
QFile file(fileName);
|
||||
if (!file.open(QIODevice::ReadOnly))
|
||||
{
|
||||
Error( log, "Effect file '%s' could not be loaded", QSTRING_CSTR(fileName));
|
||||
// Read and parse the effect json config file
|
||||
QJsonObject configEffect;
|
||||
if(!JsonUtils::readFile(fileName, configEffect, _log))
|
||||
return false;
|
||||
}
|
||||
|
||||
QByteArray fileContent = file.readAll();
|
||||
QJsonDocument configEffect = QJsonDocument::fromJson(fileContent, &error);
|
||||
|
||||
if (error.error != QJsonParseError::NoError)
|
||||
{
|
||||
// report to the user the failure and their locations in the document.
|
||||
int errorLine(0), errorColumn(0);
|
||||
|
||||
for( int i=0, count=qMin( error.offset,fileContent.size()); i<count; ++i )
|
||||
{
|
||||
++errorColumn;
|
||||
if(fileContent.at(i) == '\n' )
|
||||
{
|
||||
errorColumn = 0;
|
||||
++errorLine;
|
||||
}
|
||||
}
|
||||
|
||||
Error( log, "Error while reading effect: '%s' at Line: '%i' , Column: %i",QSTRING_CSTR( error.errorString()), errorLine, errorColumn);
|
||||
}
|
||||
|
||||
file.close();
|
||||
|
||||
// ---------- Read the effect json schema file ----------
|
||||
|
||||
Q_INIT_RESOURCE(EffectEngine);
|
||||
QFile schema(":effect-schema");
|
||||
|
||||
if (!schema.open(QIODevice::ReadOnly))
|
||||
{
|
||||
Error( log, "Schema not found: %s", QSTRING_CSTR(schema.errorString()));
|
||||
// validate effect config with effect schema(path)
|
||||
if(!JsonUtils::validate(fileName, configEffect, ":effect-schema", _log))
|
||||
return false;
|
||||
}
|
||||
|
||||
QByteArray schemaContent = schema.readAll();
|
||||
QJsonDocument configSchema = QJsonDocument::fromJson(schemaContent, &error);
|
||||
|
||||
if (error.error != QJsonParseError::NoError)
|
||||
{
|
||||
// report to the user the failure and their locations in the document.
|
||||
int errorLine(0), errorColumn(0);
|
||||
|
||||
for( int i=0, count=qMin( error.offset,schemaContent.size()); i<count; ++i )
|
||||
{
|
||||
++errorColumn;
|
||||
if(schemaContent.at(i) == '\n' )
|
||||
{
|
||||
errorColumn = 0;
|
||||
++errorLine;
|
||||
}
|
||||
}
|
||||
|
||||
Error( log, "ERROR: Json schema wrong: '%s' at Line: '%i' , Column: %i", QSTRING_CSTR(error.errorString()), errorLine, errorColumn);
|
||||
}
|
||||
|
||||
schema.close();
|
||||
|
||||
// ---------- validate effect config with effect schema ----------
|
||||
|
||||
QJsonSchemaChecker schemaChecker;
|
||||
schemaChecker.setSchema(configSchema.object());
|
||||
if (!schemaChecker.validate(configEffect.object()).first)
|
||||
{
|
||||
const QStringList & errors = schemaChecker.getMessages();
|
||||
for (auto & error : errors)
|
||||
{
|
||||
Error( log, "Error while checking '%s':%s", QSTRING_CSTR(fileName), QSTRING_CSTR(error));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// ---------- setup the definition ----------
|
||||
|
||||
// setup the definition
|
||||
effectDefinition.file = fileName;
|
||||
QJsonObject config = configEffect.object();
|
||||
QJsonObject config = configEffect;
|
||||
QString scriptName = config["script"].toString();
|
||||
effectDefinition.name = config["name"].toString();
|
||||
if (scriptName.isEmpty())
|
||||
@@ -200,48 +126,15 @@ bool EffectEngine::loadEffectDefinition(const QString &path, const QString &effe
|
||||
|
||||
bool EffectEngine::loadEffectSchema(const QString &path, const QString &effectSchemaFile, EffectSchema & effectSchema)
|
||||
{
|
||||
Logger * log = Logger::getInstance("EFFECTENGINE");
|
||||
|
||||
QString fileName = path + "schema/" + QDir::separator() + effectSchemaFile;
|
||||
QJsonParseError error;
|
||||
|
||||
// ---------- Read the effect schema file ----------
|
||||
|
||||
QFile file(fileName);
|
||||
if (!file.open(QIODevice::ReadOnly))
|
||||
{
|
||||
Error( log, "Effect schema '%s' could not be loaded", QSTRING_CSTR(fileName));
|
||||
// Read and parse the effect schema file
|
||||
QJsonObject schemaEffect;
|
||||
if(!JsonUtils::readFile(fileName, schemaEffect, _log))
|
||||
return false;
|
||||
}
|
||||
|
||||
QByteArray fileContent = file.readAll();
|
||||
QJsonDocument schemaEffect = QJsonDocument::fromJson(fileContent, &error);
|
||||
|
||||
if (error.error != QJsonParseError::NoError)
|
||||
{
|
||||
// report to the user the failure and their locations in the document.
|
||||
int errorLine(0), errorColumn(0);
|
||||
|
||||
for( int i=0, count=qMin( error.offset,fileContent.size()); i<count; ++i )
|
||||
{
|
||||
++errorColumn;
|
||||
if(fileContent.at(i) == '\n' )
|
||||
{
|
||||
errorColumn = 0;
|
||||
++errorLine;
|
||||
}
|
||||
}
|
||||
|
||||
Error( log, "Error while reading effect schema: '%s' at Line: '%i' , Column: %i", QSTRING_CSTR(error.errorString()), errorLine, errorColumn);
|
||||
return false;
|
||||
}
|
||||
|
||||
file.close();
|
||||
|
||||
// ---------- setup the definition ----------
|
||||
|
||||
QJsonObject tempSchemaEffect = schemaEffect.object();
|
||||
QString scriptName = tempSchemaEffect["script"].toString();
|
||||
// setup the definition
|
||||
QString scriptName = schemaEffect["script"].toString();
|
||||
effectSchema.schemaFile = fileName;
|
||||
fileName = path + QDir::separator() + scriptName;
|
||||
QFile pyFile(fileName);
|
||||
@@ -249,14 +142,14 @@ bool EffectEngine::loadEffectSchema(const QString &path, const QString &effectSc
|
||||
if (scriptName.isEmpty() || !pyFile.open(QIODevice::ReadOnly))
|
||||
{
|
||||
fileName = path + "schema/" + QDir::separator() + effectSchemaFile;
|
||||
Error( log, "Python script '%s' in effect schema '%s' could not be loaded", QSTRING_CSTR(scriptName), QSTRING_CSTR(fileName));
|
||||
Error( _log, "Python script '%s' in effect schema '%s' could not be loaded", QSTRING_CSTR(scriptName), QSTRING_CSTR(fileName));
|
||||
return false;
|
||||
}
|
||||
|
||||
pyFile.close();
|
||||
|
||||
effectSchema.pyFile = (scriptName.mid(0, 1) == ":" ) ? ":/effects/"+scriptName.mid(1) : path + QDir::separator() + scriptName;
|
||||
effectSchema.pySchema = tempSchemaEffect;
|
||||
effectSchema.pySchema = schemaEffect;
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -277,7 +170,7 @@ void EffectEngine::readEffects()
|
||||
|
||||
for(auto p : paths)
|
||||
{
|
||||
efxPathList << p.toString();
|
||||
efxPathList << p.toString().replace("$ROOT",_hyperion->getRootPath());
|
||||
}
|
||||
for(auto efx : disabledEfx)
|
||||
{
|
||||
@@ -309,7 +202,7 @@ void EffectEngine::readEffects()
|
||||
if (loadEffectDefinition(path, filename, def))
|
||||
{
|
||||
InfoIf(availableEffects.find(def.name) != availableEffects.end(), _log,
|
||||
"effect overload effect '%s' is now taken from %s'", QSTRING_CSTR(def.name), QSTRING_CSTR(path) );
|
||||
"effect overload effect '%s' is now taken from '%s'", QSTRING_CSTR(def.name), QSTRING_CSTR(path) );
|
||||
|
||||
if ( disableList.contains(def.name) )
|
||||
{
|
||||
@@ -326,7 +219,7 @@ void EffectEngine::readEffects()
|
||||
|
||||
// collect effect schemas
|
||||
efxCount = 0;
|
||||
directory = path + "schema/";
|
||||
directory = path.endsWith("/") ? (path + "schema/") : (path + "/schema/");
|
||||
QStringList pynames = directory.entryList(QStringList() << "*.json", QDir::Files, QDir::Name | QDir::IgnoreCase);
|
||||
for (const QString & pyname : pynames)
|
||||
{
|
||||
@@ -387,9 +280,9 @@ int EffectEngine::runEffectScript(const QString &script, const QString &name, co
|
||||
channelCleared(priority);
|
||||
|
||||
// create the effect
|
||||
Effect * effect = new Effect(_mainThreadState, priority, timeout, script, name, args, origin, smoothCfg);
|
||||
Effect * effect = new Effect(priority, timeout, script, name, args, origin, smoothCfg);
|
||||
connect(effect, SIGNAL(setColors(int,std::vector<ColorRgb>,int,bool,hyperion::Components,const QString,unsigned)), _hyperion, SLOT(setColors(int,std::vector<ColorRgb>,int,bool,hyperion::Components,const QString,unsigned)), Qt::QueuedConnection);
|
||||
connect(effect, SIGNAL(effectFinished(Effect*)), this, SLOT(effectFinished(Effect*)));
|
||||
connect(effect, &QThread::finished, this, &EffectEngine::effectFinished);
|
||||
_activeEffects.push_back(effect);
|
||||
|
||||
// start the effect
|
||||
@@ -405,7 +298,7 @@ void EffectEngine::channelCleared(int priority)
|
||||
{
|
||||
if (effect->getPriority() == priority)
|
||||
{
|
||||
effect->abort();
|
||||
effect->requestInterruption();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -414,13 +307,17 @@ void EffectEngine::allChannelsCleared()
|
||||
{
|
||||
for (Effect * effect : _activeEffects)
|
||||
{
|
||||
effect->abort();
|
||||
if (effect->getPriority() != 254)
|
||||
{
|
||||
effect->requestInterruption();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void EffectEngine::effectFinished(Effect *effect)
|
||||
void EffectEngine::effectFinished()
|
||||
{
|
||||
if (!effect->isAbortRequested())
|
||||
Effect* effect = qobject_cast<Effect*>(sender());
|
||||
if (!effect->isInterruptionRequested())
|
||||
{
|
||||
// effect stopped by itself. Clear the channel
|
||||
_hyperion->clear(effect->getPriority());
|
||||
|
Reference in New Issue
Block a user