2013-11-26 21:38:24 +01:00
|
|
|
// Python includes
|
|
|
|
#include <Python.h>
|
|
|
|
|
2013-12-11 21:58:59 +01:00
|
|
|
// Stl includes
|
|
|
|
#include <fstream>
|
|
|
|
|
2013-11-30 12:42:08 +01:00
|
|
|
// Qt includes
|
2013-12-11 21:58:59 +01:00
|
|
|
#include <QResource>
|
2013-11-30 12:42:08 +01:00
|
|
|
#include <QMetaType>
|
2013-12-11 21:58:59 +01:00
|
|
|
#include <QFile>
|
|
|
|
#include <QDir>
|
|
|
|
|
|
|
|
// hyperion util includes
|
|
|
|
#include <utils/jsonschema/JsonSchemaChecker.h>
|
2016-09-03 15:54:33 +02:00
|
|
|
#include <utils/FileUtils.h>
|
2013-11-30 12:42:08 +01:00
|
|
|
|
2013-11-26 21:38:24 +01:00
|
|
|
// effect engine includes
|
2013-11-24 16:10:48 +01:00
|
|
|
#include <effectengine/EffectEngine.h>
|
2013-11-26 21:38:24 +01:00
|
|
|
#include "Effect.h"
|
2016-06-12 22:27:24 +02:00
|
|
|
#include "HyperionConfig.h"
|
2013-11-24 16:10:48 +01:00
|
|
|
|
2016-08-08 00:17:00 +02:00
|
|
|
EffectEngine::EffectEngine(Hyperion * hyperion, const Json::Value & jsonEffectConfig)
|
|
|
|
: _hyperion(hyperion)
|
|
|
|
, _availableEffects()
|
|
|
|
, _activeEffects()
|
|
|
|
, _mainThreadState(nullptr)
|
|
|
|
, _log(Logger::getInstance("EFFECTENGINE"))
|
2013-11-24 16:10:48 +01:00
|
|
|
{
|
2016-09-13 11:51:16 +02:00
|
|
|
Q_INIT_RESOURCE(EffectEngine);
|
2013-11-29 23:22:49 +01:00
|
|
|
qRegisterMetaType<std::vector<ColorRgb>>("std::vector<ColorRgb>");
|
|
|
|
|
2013-11-24 16:10:48 +01:00
|
|
|
// connect the Hyperion channel clear feedback
|
|
|
|
connect(_hyperion, SIGNAL(channelCleared(int)), this, SLOT(channelCleared(int)));
|
|
|
|
connect(_hyperion, SIGNAL(allChannelsCleared()), this, SLOT(allChannelsCleared()));
|
|
|
|
|
|
|
|
// read all effects
|
2016-09-13 11:51:16 +02:00
|
|
|
const Json::Value & paths = jsonEffectConfig["paths"];
|
|
|
|
const Json::Value & disabledEfx = jsonEffectConfig["disable"];
|
|
|
|
|
|
|
|
QStringList efxPathList;
|
|
|
|
efxPathList << ":/effects/";
|
2013-12-11 21:58:59 +01:00
|
|
|
for (Json::UInt i = 0; i < paths.size(); ++i)
|
2013-11-30 12:42:08 +01:00
|
|
|
{
|
2016-09-13 11:51:16 +02:00
|
|
|
efxPathList << QString::fromStdString(paths[i].asString());
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList disableList;
|
|
|
|
for (Json::UInt i = 0; i < disabledEfx.size(); ++i)
|
|
|
|
{
|
|
|
|
disableList << QString::fromStdString(disabledEfx[i].asString());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::map<std::string, EffectDefinition> availableEffects;
|
|
|
|
foreach (const QString & path, efxPathList )
|
|
|
|
{
|
|
|
|
QDir directory(path);
|
2016-05-24 22:14:45 +02:00
|
|
|
if (directory.exists())
|
2013-12-11 21:58:59 +01:00
|
|
|
{
|
2016-05-24 22:14:45 +02:00
|
|
|
int efxCount = 0;
|
|
|
|
QStringList filenames = directory.entryList(QStringList() << "*.json", QDir::Files, QDir::Name | QDir::IgnoreCase);
|
|
|
|
foreach (const QString & filename, filenames)
|
2013-12-11 21:58:59 +01:00
|
|
|
{
|
2016-05-24 22:14:45 +02:00
|
|
|
EffectDefinition def;
|
2016-09-13 11:51:16 +02:00
|
|
|
if (loadEffectDefinition(path, filename, def))
|
2016-05-24 22:14:45 +02:00
|
|
|
{
|
2016-09-13 11:51:16 +02:00
|
|
|
if (availableEffects.find(def.name) != availableEffects.end())
|
|
|
|
{
|
|
|
|
Info(_log, "effect overload effect '%s' is now taken from %s'", def.name.c_str(), path.toUtf8().constData() );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( disableList.contains(QString::fromStdString(def.name)) )
|
|
|
|
{
|
|
|
|
Info(_log, "effect '%s' not loaded, because it is disabled in hyperion config", def.name.c_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
availableEffects[def.name] = def;
|
|
|
|
efxCount++;
|
|
|
|
}
|
2016-05-24 22:14:45 +02:00
|
|
|
}
|
2013-12-11 21:58:59 +01:00
|
|
|
}
|
2016-09-13 11:51:16 +02:00
|
|
|
Info(_log, "%d effects loaded from directory %s", efxCount, path.toUtf8().constData());
|
2013-12-11 21:58:59 +01:00
|
|
|
}
|
2013-11-30 12:42:08 +01:00
|
|
|
}
|
2013-11-26 21:38:24 +01:00
|
|
|
|
2016-09-13 11:51:16 +02:00
|
|
|
foreach(auto item, availableEffects)
|
|
|
|
{
|
|
|
|
_availableEffects.push_back(item.second);
|
|
|
|
}
|
|
|
|
|
2016-05-24 22:14:45 +02:00
|
|
|
if (_availableEffects.size() == 0)
|
|
|
|
{
|
2016-06-23 13:48:49 +02:00
|
|
|
Error(_log, "no effects found, check your effect directories");
|
2016-05-24 22:14:45 +02:00
|
|
|
}
|
|
|
|
|
2013-11-26 21:38:24 +01:00
|
|
|
// initialize the python interpreter
|
2016-06-23 13:48:49 +02:00
|
|
|
Debug(_log,"Initializing Python interpreter");
|
2014-02-26 18:10:17 +01:00
|
|
|
Effect::registerHyperionExtensionModule();
|
2013-11-26 21:38:24 +01:00
|
|
|
Py_InitializeEx(0);
|
|
|
|
PyEval_InitThreads(); // Create the GIL
|
2013-11-29 23:22:49 +01:00
|
|
|
_mainThreadState = PyEval_SaveThread();
|
2013-11-24 16:10:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
EffectEngine::~EffectEngine()
|
|
|
|
{
|
2013-11-26 21:38:24 +01:00
|
|
|
// clean up the Python interpreter
|
2016-06-23 13:48:49 +02:00
|
|
|
Debug(_log, "Cleaning up Python interpreter");
|
2013-11-29 23:22:49 +01:00
|
|
|
PyEval_RestoreThread(_mainThreadState);
|
|
|
|
Py_Finalize();
|
2013-11-24 16:10:48 +01:00
|
|
|
}
|
|
|
|
|
2013-12-01 14:09:01 +01:00
|
|
|
const std::list<EffectDefinition> &EffectEngine::getEffects() const
|
2013-11-24 16:10:48 +01:00
|
|
|
{
|
2013-12-01 14:09:01 +01:00
|
|
|
return _availableEffects;
|
2013-11-24 16:10:48 +01:00
|
|
|
}
|
|
|
|
|
2016-04-24 17:07:31 +02:00
|
|
|
const std::list<ActiveEffectDefinition> &EffectEngine::getActiveEffects()
|
|
|
|
{
|
|
|
|
_availableActiveEffects.clear();
|
|
|
|
|
|
|
|
for (Effect * effect : _activeEffects)
|
|
|
|
{
|
|
|
|
ActiveEffectDefinition activeEffectDefinition;
|
2016-09-13 11:51:16 +02:00
|
|
|
activeEffectDefinition.script = effect->getScript().toStdString();
|
|
|
|
activeEffectDefinition.name = effect->getName().toStdString();
|
2016-04-24 17:07:31 +02:00
|
|
|
activeEffectDefinition.priority = effect->getPriority();
|
|
|
|
activeEffectDefinition.timeout = effect->getTimeout();
|
|
|
|
activeEffectDefinition.args = effect->getArgs();
|
|
|
|
_availableActiveEffects.push_back(activeEffectDefinition);
|
|
|
|
}
|
|
|
|
|
|
|
|
return _availableActiveEffects;
|
|
|
|
}
|
|
|
|
|
2016-09-13 11:51:16 +02:00
|
|
|
bool EffectEngine::loadEffectDefinition(const QString &path, const QString &effectConfigFile, EffectDefinition & effectDefinition)
|
2013-12-11 21:58:59 +01:00
|
|
|
{
|
2016-09-13 11:51:16 +02:00
|
|
|
QString fileName = path + QDir::separator() + effectConfigFile;
|
|
|
|
QFile file(fileName);
|
2013-12-11 21:58:59 +01:00
|
|
|
|
2016-06-23 13:48:49 +02:00
|
|
|
Logger * log = Logger::getInstance("EFFECTENGINE");
|
2016-09-13 11:51:16 +02:00
|
|
|
if (!file.open(QIODevice::ReadOnly))
|
2013-12-11 21:58:59 +01:00
|
|
|
{
|
2016-09-13 11:51:16 +02:00
|
|
|
Error( log, "Effect file '%s' could not be loaded", fileName.toUtf8().constData());
|
2013-12-11 21:58:59 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-09-13 11:51:16 +02:00
|
|
|
QByteArray fileContent = file.readAll();
|
2013-12-11 21:58:59 +01:00
|
|
|
// Read the json config file
|
|
|
|
Json::Reader jsonReader;
|
|
|
|
Json::Value config;
|
2016-09-13 11:51:16 +02:00
|
|
|
const char* fileContent_cStr = reinterpret_cast<const char *>(fileContent.constData());
|
|
|
|
|
|
|
|
if (! Json::Reader().parse(fileContent_cStr, fileContent_cStr+fileContent.size(), config, false) )
|
2013-12-11 21:58:59 +01:00
|
|
|
{
|
2016-09-13 11:51:16 +02:00
|
|
|
Error( log, "Error while reading effect '%s': %s", fileName.toUtf8().constData(), jsonReader.getFormattedErrorMessages().c_str());
|
2013-12-11 21:58:59 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-09-13 11:51:16 +02:00
|
|
|
file.close();
|
2013-12-11 21:58:59 +01:00
|
|
|
|
|
|
|
// Read the json schema file
|
|
|
|
QResource schemaData(":effect-schema");
|
|
|
|
JsonSchemaChecker schemaChecker;
|
|
|
|
Json::Value schema;
|
|
|
|
Json::Reader().parse(reinterpret_cast<const char *>(schemaData.data()), reinterpret_cast<const char *>(schemaData.data()) + schemaData.size(), schema, false);
|
|
|
|
schemaChecker.setSchema(schema);
|
|
|
|
if (!schemaChecker.validate(config))
|
|
|
|
{
|
|
|
|
const std::list<std::string> & errors = schemaChecker.getMessages();
|
|
|
|
foreach (const std::string & error, errors) {
|
2016-09-13 11:51:16 +02:00
|
|
|
Error( log, "Error while checking '%s':%s", fileName.toUtf8().constData(), error.c_str());
|
2013-12-11 21:58:59 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup the definition
|
2016-09-13 11:51:16 +02:00
|
|
|
std::string scriptName = config["script"].asString();
|
2013-12-11 21:58:59 +01:00
|
|
|
effectDefinition.name = config["name"].asString();
|
2016-09-13 11:51:16 +02:00
|
|
|
if (scriptName.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (scriptName[0] == ':' )
|
|
|
|
effectDefinition.script = ":/effects/"+scriptName.substr(1);
|
|
|
|
else
|
|
|
|
effectDefinition.script = path.toStdString() + QDir::separator().toLatin1() + scriptName;
|
|
|
|
|
2013-12-11 21:58:59 +01:00
|
|
|
effectDefinition.args = config["args"];
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-24 16:10:48 +01:00
|
|
|
int EffectEngine::runEffect(const std::string &effectName, int priority, int timeout)
|
2013-12-01 14:09:01 +01:00
|
|
|
{
|
|
|
|
return runEffect(effectName, Json::Value(Json::nullValue), priority, timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
int EffectEngine::runEffect(const std::string &effectName, const Json::Value &args, int priority, int timeout)
|
2013-11-24 16:10:48 +01:00
|
|
|
{
|
2016-06-23 13:48:49 +02:00
|
|
|
Info( _log, "run effect %s on channel %d", effectName.c_str(), priority);
|
2013-11-26 21:38:24 +01:00
|
|
|
|
2013-12-01 14:09:01 +01:00
|
|
|
const EffectDefinition * effectDefinition = nullptr;
|
|
|
|
for (const EffectDefinition & e : _availableEffects)
|
|
|
|
{
|
|
|
|
if (e.name == effectName)
|
|
|
|
{
|
|
|
|
effectDefinition = &e;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (effectDefinition == nullptr)
|
2013-11-30 00:28:04 +01:00
|
|
|
{
|
|
|
|
// no such effect
|
2016-06-23 13:48:49 +02:00
|
|
|
Error(_log, "effect %s not found", effectName.c_str());
|
2013-11-30 00:28:04 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-09-13 11:51:16 +02:00
|
|
|
return runEffectScript(effectDefinition->script, effectName, args.isNull() ? effectDefinition->args : args, priority, timeout);
|
2013-12-01 16:35:45 +01:00
|
|
|
}
|
|
|
|
|
2016-09-13 11:51:16 +02:00
|
|
|
int EffectEngine::runEffectScript(const std::string &script, const std::string &name, const Json::Value &args, int priority, int timeout)
|
2013-12-01 16:35:45 +01:00
|
|
|
{
|
2013-11-26 21:38:24 +01:00
|
|
|
// clear current effect on the channel
|
|
|
|
channelCleared(priority);
|
|
|
|
|
|
|
|
// create the effect
|
2016-09-13 11:51:16 +02:00
|
|
|
Effect * effect = new Effect(_mainThreadState, priority, timeout, QString::fromStdString(script), QString::fromStdString(name), args);
|
2013-12-08 17:45:26 +01:00
|
|
|
connect(effect, SIGNAL(setColors(int,std::vector<ColorRgb>,int,bool)), _hyperion, SLOT(setColors(int,std::vector<ColorRgb>,int,bool)), Qt::QueuedConnection);
|
2013-11-26 21:38:24 +01:00
|
|
|
connect(effect, SIGNAL(effectFinished(Effect*)), this, SLOT(effectFinished(Effect*)));
|
|
|
|
_activeEffects.push_back(effect);
|
|
|
|
|
|
|
|
// start the effect
|
2016-09-13 11:51:16 +02:00
|
|
|
_hyperion->registerPriority("EFFECT: "+name, priority);
|
2013-11-26 21:38:24 +01:00
|
|
|
effect->start();
|
|
|
|
|
2013-11-24 16:10:48 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EffectEngine::channelCleared(int priority)
|
|
|
|
{
|
2013-11-26 21:38:24 +01:00
|
|
|
for (Effect * effect : _activeEffects)
|
|
|
|
{
|
|
|
|
if (effect->getPriority() == priority)
|
|
|
|
{
|
|
|
|
effect->abort();
|
|
|
|
}
|
|
|
|
}
|
2013-11-24 16:10:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void EffectEngine::allChannelsCleared()
|
|
|
|
{
|
2013-11-26 21:38:24 +01:00
|
|
|
for (Effect * effect : _activeEffects)
|
|
|
|
{
|
|
|
|
effect->abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EffectEngine::effectFinished(Effect *effect)
|
|
|
|
{
|
2013-11-30 15:54:47 +01:00
|
|
|
if (!effect->isAbortRequested())
|
|
|
|
{
|
|
|
|
// effect stopped by itself. Clear the channel
|
|
|
|
_hyperion->clear(effect->getPriority());
|
|
|
|
}
|
|
|
|
|
2016-06-23 13:48:49 +02:00
|
|
|
Info( _log, "effect finished");
|
2013-11-26 21:38:24 +01:00
|
|
|
for (auto effectIt = _activeEffects.begin(); effectIt != _activeEffects.end(); ++effectIt)
|
|
|
|
{
|
|
|
|
if (*effectIt == effect)
|
|
|
|
{
|
|
|
|
_activeEffects.erase(effectIt);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanup the effect
|
|
|
|
effect->deleteLater();
|
2016-09-13 11:51:16 +02:00
|
|
|
_hyperion->unRegisterPriority("EFFECT: " + effect->getName().toStdString());
|
2013-11-24 16:10:48 +01:00
|
|
|
}
|