mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2023-10-10 13:36:59 +02: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:
parent
47641012ee
commit
838008568a
@ -58,7 +58,7 @@ sudo make install/strip
|
|||||||
# to uninstall (not very well tested, please keep that in mind)
|
# to uninstall (not very well tested, please keep that in mind)
|
||||||
sudo make uninstall
|
sudo make uninstall
|
||||||
# ... or run it from compile directory
|
# ... or run it from compile directory
|
||||||
bin/hyperiond myconfig.json
|
bin/hyperiond
|
||||||
# webui is located on localhost:8099
|
# webui is located on localhost:8099
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -143,5 +143,3 @@ This will install to ``/home/pi/apps/share/hyperion``
|
|||||||
### Integrating hyperion into your system
|
### Integrating hyperion into your system
|
||||||
|
|
||||||
... ToDo
|
... ToDo
|
||||||
|
|
||||||
|
|
||||||
|
@ -121,7 +121,7 @@ $(document).ready( function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
$('#btn_edit').off().on('click', function(){
|
$('#btn_edit').off().on('click', function(){
|
||||||
var name = $("#effectsdellist").val();
|
var name = $("#effectsdellist").val().replace("ext_","");
|
||||||
|
|
||||||
if(name.startsWith("int_"))
|
if(name.startsWith("int_"))
|
||||||
{ name = name.split("_").pop();
|
{ name = name.split("_").pop();
|
||||||
|
@ -274,7 +274,7 @@ function requestWriteEffect(effectName,effectPy,effectArgs)
|
|||||||
|
|
||||||
function requestTestEffect(effectName,effectPy,effectArgs)
|
function requestTestEffect(effectName,effectPy,effectArgs)
|
||||||
{
|
{
|
||||||
sendToHyperion("effect", "", '"effect":{"name":"'+effectName+'", "args":'+effectArgs+'}, "priority":'+webPrio+', "origin":"'+webOrigin+'", "pythonScript":"'+effectPy+'"}');
|
sendToHyperion("effect", "", '"effect":{"name":"'+effectName+'", "args":'+effectArgs+'}, "priority":'+webPrio+', "origin":"'+webOrigin+'", "pythonScript":"'+effectPy+'"');
|
||||||
}
|
}
|
||||||
|
|
||||||
function requestDeleteEffect(effectName)
|
function requestDeleteEffect(effectName)
|
||||||
|
@ -13,7 +13,6 @@
|
|||||||
### END INIT INFO
|
### END INIT INFO
|
||||||
|
|
||||||
DAEMON=hyperiond
|
DAEMON=hyperiond
|
||||||
DAEMONOPTS="/etc/hyperion/hyperion.config.json"
|
|
||||||
DAEMON_PATH="/usr/bin"
|
DAEMON_PATH="/usr/bin"
|
||||||
|
|
||||||
NAME=$DAEMON
|
NAME=$DAEMON
|
||||||
@ -29,7 +28,7 @@ start)
|
|||||||
else
|
else
|
||||||
printf "%-50s" "Starting $NAME..."
|
printf "%-50s" "Starting $NAME..."
|
||||||
cd $DAEMON_PATH
|
cd $DAEMON_PATH
|
||||||
PID=`$DAEMON $DAEMONOPTS > /dev/null 2>&1 & echo $!`
|
PID=`$DAEMON > /dev/null 2>&1 & echo $!`
|
||||||
#echo "Saving PID" $PID " to " $PIDFILE
|
#echo "Saving PID" $PID " to " $PIDFILE
|
||||||
if [ -z $PID ]; then
|
if [ -z $PID ]; then
|
||||||
printf "%s\n" "Fail"
|
printf "%s\n" "Fail"
|
||||||
|
@ -8,4 +8,4 @@ stop on (runlevel [!2345])
|
|||||||
|
|
||||||
respawn
|
respawn
|
||||||
|
|
||||||
exec /usr/bin/hyperiond /etc/hyperion/hyperion.config.json
|
exec /usr/bin/hyperiond
|
||||||
|
@ -3,7 +3,7 @@ Description=Hyperion ambient light systemd service
|
|||||||
After=network.target
|
After=network.target
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
ExecStart=/usr/bin/hyperiond /etc/hyperion/hyperion.config.json
|
ExecStart=/usr/bin/hyperiond
|
||||||
WorkingDirectory=/usr/share/hyperion/bin
|
WorkingDirectory=/usr/share/hyperion/bin
|
||||||
TimeoutStopSec=5
|
TimeoutStopSec=5
|
||||||
KillMode=mixed
|
KillMode=mixed
|
||||||
|
@ -319,17 +319,18 @@
|
|||||||
{
|
{
|
||||||
"enable" : true,
|
"enable" : true,
|
||||||
"document_root" : "/path/to/files",
|
"document_root" : "/path/to/files",
|
||||||
"port" : 8099
|
"port" : 8090
|
||||||
},
|
},
|
||||||
|
|
||||||
/// The configuration of the effect engine, contains the following items:
|
/// The configuration of the effect engine, contains the following items:
|
||||||
/// * paths : An array with absolute/relative location(s) of directories with effects
|
/// * paths : An array with absolute location(s) of directories with effects,
|
||||||
|
/// $ROOT is a keyword which will be replaced with the current rootPath that can be specified on startup from the commandline (defaults to your home directory)
|
||||||
/// * disable : An array with effect names that shouldn't be loaded
|
/// * disable : An array with effect names that shouldn't be loaded
|
||||||
"effects" :
|
"effects" :
|
||||||
{
|
{
|
||||||
"paths" :
|
"paths" :
|
||||||
[
|
[
|
||||||
"/storage/hyperion/effects",
|
"$ROOT/custom-effects",
|
||||||
"/usr/share/hyperion/effects"
|
"/usr/share/hyperion/effects"
|
||||||
],
|
],
|
||||||
"disable" :
|
"disable" :
|
||||||
|
@ -181,12 +181,12 @@
|
|||||||
{
|
{
|
||||||
"enable" : true,
|
"enable" : true,
|
||||||
"document_root" : "",
|
"document_root" : "",
|
||||||
"port" : 8099
|
"port" : 8090
|
||||||
},
|
},
|
||||||
|
|
||||||
"effects" :
|
"effects" :
|
||||||
{
|
{
|
||||||
"paths" : ["../custom-effects"],
|
"paths" : ["$ROOT/custom-effects"],
|
||||||
"disable": [""]
|
"disable": [""]
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -57,7 +57,7 @@ public slots:
|
|||||||
void allChannelsCleared();
|
void allChannelsCleared();
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void effectFinished(Effect * effect);
|
void effectFinished();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool loadEffectDefinition(const QString & path, const QString & effectConfigFile, EffectDefinition &effectDefinition);
|
bool loadEffectDefinition(const QString & path, const QString & effectConfigFile, EffectDefinition &effectDefinition);
|
||||||
|
@ -80,7 +80,18 @@ public:
|
|||||||
///
|
///
|
||||||
void freeObjects(bool emitCloseSignal=false);
|
void freeObjects(bool emitCloseSignal=false);
|
||||||
|
|
||||||
static Hyperion* initInstance(const QJsonObject& qjsonConfig, const QString configFile);
|
///
|
||||||
|
/// @brief creates a new Hyperion instance, usually called from the Hyperion Daemon
|
||||||
|
/// @param[in] qjsonConfig The configuration file
|
||||||
|
/// @param[in] rootPath Root path of all hyperion userdata
|
||||||
|
/// @return Hyperion instance pointer
|
||||||
|
///
|
||||||
|
static Hyperion* initInstance(const QJsonObject& qjsonConfig, const QString configFile, const QString rootPath);
|
||||||
|
|
||||||
|
///
|
||||||
|
/// @brief Get a pointer of this Hyperion instance
|
||||||
|
/// @return Hyperion instance pointer
|
||||||
|
///
|
||||||
static Hyperion* getInstance();
|
static Hyperion* getInstance();
|
||||||
|
|
||||||
///
|
///
|
||||||
@ -189,10 +200,12 @@ public:
|
|||||||
/// gets the methode how image is maped to leds
|
/// gets the methode how image is maped to leds
|
||||||
int getLedMappingType() { return _ledMAppingType; };
|
int getLedMappingType() { return _ledMAppingType; };
|
||||||
|
|
||||||
int getConfigVersionId() { return _configVersionId; };
|
/// get the configuration
|
||||||
|
|
||||||
QJsonObject getConfig() { return _qjsonConfig; };
|
QJsonObject getConfig() { return _qjsonConfig; };
|
||||||
|
|
||||||
|
/// get the root path for all hyperion user data files
|
||||||
|
QString getRootPath() { return _rootPath; };
|
||||||
|
|
||||||
/// unique id per instance
|
/// unique id per instance
|
||||||
QString id;
|
QString id;
|
||||||
|
|
||||||
@ -375,7 +388,7 @@ private:
|
|||||||
///
|
///
|
||||||
/// @param[in] qjsonConfig The Json configuration
|
/// @param[in] qjsonConfig The Json configuration
|
||||||
///
|
///
|
||||||
Hyperion(const QJsonObject& qjsonConfig, const QString configFile);
|
Hyperion(const QJsonObject& qjsonConfig, const QString configFile, const QString rootPath);
|
||||||
|
|
||||||
/// The specifiation of the led frame construction and picture integration
|
/// The specifiation of the led frame construction and picture integration
|
||||||
LedString _ledString;
|
LedString _ledString;
|
||||||
@ -409,6 +422,9 @@ private:
|
|||||||
/// the name of config file
|
/// the name of config file
|
||||||
QString _configFile;
|
QString _configFile;
|
||||||
|
|
||||||
|
/// root path for all hyperion user data files
|
||||||
|
QString _rootPath;
|
||||||
|
|
||||||
/// The timer for handling priority channel timeouts
|
/// The timer for handling priority channel timeouts
|
||||||
QTimer _timer;
|
QTimer _timer;
|
||||||
QTimer _timerBonjourResolver;
|
QTimer _timerBonjourResolver;
|
||||||
@ -439,8 +455,6 @@ private:
|
|||||||
|
|
||||||
int _ledMAppingType;
|
int _ledMAppingType;
|
||||||
|
|
||||||
int _configVersionId;
|
|
||||||
|
|
||||||
hyperion::Components _prevCompId;
|
hyperion::Components _prevCompId;
|
||||||
BonjourServiceBrowser _bonjourBrowser;
|
BonjourServiceBrowser _bonjourBrowser;
|
||||||
BonjourServiceResolver _bonjourResolver;
|
BonjourServiceResolver _bonjourResolver;
|
||||||
|
@ -180,7 +180,7 @@ private:
|
|||||||
|
|
||||||
if(_borderProcessor->enabled() && _borderProcessor->process(image))
|
if(_borderProcessor->enabled() && _borderProcessor->process(image))
|
||||||
{
|
{
|
||||||
Debug(Logger::getInstance("BLACKBORDER"), "BORDER SWITCH REQUIRED!!");
|
//Debug(Logger::getInstance("BLACKBORDER"), "BORDER SWITCH REQUIRED!!");
|
||||||
|
|
||||||
const hyperion::BlackBorder border = _borderProcessor->getCurrentBorder();
|
const hyperion::BlackBorder border = _borderProcessor->getCurrentBorder();
|
||||||
|
|
||||||
@ -198,8 +198,8 @@ private:
|
|||||||
_imageToLeds = new hyperion::ImageToLedsMap(image.width(), image.height(), border.horizontalSize, border.verticalSize, _ledString.leds());
|
_imageToLeds = new hyperion::ImageToLedsMap(image.width(), image.height(), border.horizontalSize, border.verticalSize, _ledString.leds());
|
||||||
}
|
}
|
||||||
|
|
||||||
Debug(Logger::getInstance("BLACKBORDER"), "CURRENT BORDER TYPE: unknown=%d hor.size=%d vert.size=%d",
|
//Debug(Logger::getInstance("BLACKBORDER"), "CURRENT BORDER TYPE: unknown=%d hor.size=%d vert.size=%d",
|
||||||
border.unknown, border.horizontalSize, border.verticalSize );
|
// border.unknown, border.horizontalSize, border.verticalSize );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,10 +1,64 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
// qt includes
|
||||||
|
#include <QFile>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
#include <QByteArray>
|
||||||
|
|
||||||
|
// util includes
|
||||||
|
#include "Logger.h"
|
||||||
|
|
||||||
namespace FileUtils {
|
namespace FileUtils {
|
||||||
|
|
||||||
QString getBaseName( QString sourceFile);
|
QString getBaseName( QString sourceFile);
|
||||||
QString getDirName( QString sourceFile);
|
QString getDirName( QString sourceFile);
|
||||||
|
|
||||||
|
///
|
||||||
|
/// @brief check if the file exists
|
||||||
|
/// @param[in] path The file path to check
|
||||||
|
/// @param[in] log The logger of the caller to print errors
|
||||||
|
/// @param[in] ignError Ignore errors during file read (no log output)
|
||||||
|
/// @return true on success else false
|
||||||
|
///
|
||||||
|
bool fileExists(const QString& path, Logger* log, bool ignError=false);
|
||||||
|
|
||||||
|
///
|
||||||
|
/// @brief read a file given by path.
|
||||||
|
/// @param[in] path The file path to read
|
||||||
|
/// @param[out] data The read data o success
|
||||||
|
/// @param[in] log The logger of the caller to print errors
|
||||||
|
/// @param[in] ignError Ignore errors during file read (no log output)
|
||||||
|
/// @return true on success else false
|
||||||
|
///
|
||||||
|
bool readFile(const QString& path, QString& data, Logger* log, bool ignError=false);
|
||||||
|
|
||||||
|
///
|
||||||
|
/// write a file given by path.
|
||||||
|
/// @param[in] path The file path to read
|
||||||
|
/// @param[in] data The data to write
|
||||||
|
/// @param[in] log The logger of the caller to print errors
|
||||||
|
/// @return true on success else false
|
||||||
|
///
|
||||||
|
bool writeFile(const QString& path, const QByteArray& data, Logger* log);
|
||||||
|
|
||||||
|
///
|
||||||
|
/// @brief delete a file by given path
|
||||||
|
/// @param[in] path The file path to delete
|
||||||
|
/// @param[in] log The logger of the caller to print errors
|
||||||
|
/// @return true on success else false
|
||||||
|
///
|
||||||
|
bool removeFile(const QString& path, Logger* log);
|
||||||
|
|
||||||
|
///
|
||||||
|
/// @brief Convert a path that may contain special placeholders
|
||||||
|
/// @param[in] path The path to convert
|
||||||
|
///
|
||||||
|
QString convertPath(const QString path);
|
||||||
|
|
||||||
|
///
|
||||||
|
/// @brief resolve the file error and print a message
|
||||||
|
/// @param[in] file The file which caused the error
|
||||||
|
/// @param[in] log The logger of the caller
|
||||||
|
///
|
||||||
|
void resolveFileError(const QFile& file, Logger* log);
|
||||||
};
|
};
|
||||||
|
@ -45,9 +45,10 @@ public:
|
|||||||
/// Constructor
|
/// Constructor
|
||||||
///
|
///
|
||||||
/// @param peerAddress provide the Address of the peer
|
/// @param peerAddress provide the Address of the peer
|
||||||
|
/// @param log The Logger class of the creator
|
||||||
/// @param noListener if true, this instance won't listen for hyperion push events
|
/// @param noListener if true, this instance won't listen for hyperion push events
|
||||||
///
|
///
|
||||||
JsonProcessor(QString peerAddress, bool noListener = false);
|
JsonProcessor(QString peerAddress, Logger* log, bool noListener = false);
|
||||||
~JsonProcessor();
|
~JsonProcessor();
|
||||||
|
|
||||||
///
|
///
|
||||||
@ -272,16 +273,4 @@ private:
|
|||||||
/// @param error String describing the error
|
/// @param error String describing the error
|
||||||
///
|
///
|
||||||
void sendErrorReply(const QString & error, const QString &command="", const int tan=0);
|
void sendErrorReply(const QString & error, const QString &command="", const int tan=0);
|
||||||
|
|
||||||
///
|
|
||||||
/// Check if a JSON messag is valid according to a given JSON schema
|
|
||||||
///
|
|
||||||
/// @param message JSON message which need to be checked
|
|
||||||
/// @param schemaResource Qt Resource identifier with the JSON schema
|
|
||||||
/// @param errors Output error message
|
|
||||||
/// @param ignoreRequired ignore the required value in JSON schema
|
|
||||||
///
|
|
||||||
/// @return true if message conforms the given JSON schema
|
|
||||||
///
|
|
||||||
bool checkJson(const QJsonObject & message, const QString &schemaResource, QString & errors, bool ignoreRequired = false);
|
|
||||||
};
|
};
|
||||||
|
65
include/utils/JsonUtils.h
Normal file
65
include/utils/JsonUtils.h
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <utils/FileUtils.h>
|
||||||
|
|
||||||
|
#include <QJsonObject>
|
||||||
|
#include <utils/Logger.h>
|
||||||
|
|
||||||
|
namespace JsonUtils{
|
||||||
|
///
|
||||||
|
/// @brief read a json file and get the parsed result on success
|
||||||
|
/// @param[in] path The file path to read
|
||||||
|
/// @param[out] obj Returns the parsed QJsonObject
|
||||||
|
/// @param[in] log The logger of the caller to print errors
|
||||||
|
/// @param[in] ignError Ignore errors during file read (no log output)
|
||||||
|
/// @return true on success else false
|
||||||
|
///
|
||||||
|
bool readFile(const QString& path, QJsonObject& obj, Logger* log, bool ignError=false);
|
||||||
|
|
||||||
|
///
|
||||||
|
/// @brief read a schema file and resolve $refs
|
||||||
|
/// @param[in] path The file path to read
|
||||||
|
/// @param[out] obj Returns the parsed QJsonObject
|
||||||
|
/// @param[in] log The logger of the caller to print errors
|
||||||
|
/// @return true on success else false
|
||||||
|
///
|
||||||
|
bool readSchema(const QString& path, QJsonObject& obj, Logger* log);
|
||||||
|
|
||||||
|
///
|
||||||
|
/// @brief parse a json QString and get the result on success
|
||||||
|
/// @param[in] path The file path/name just used for log messages
|
||||||
|
/// @param[in] data Data to parse
|
||||||
|
/// @param[out] obj Retuns the parsed QJsonObject
|
||||||
|
/// @param[in] log The logger of the caller to print errors
|
||||||
|
/// @return true on success else false
|
||||||
|
///
|
||||||
|
bool parse(const QString& path, const QString& data, QJsonObject& obj, Logger* log);
|
||||||
|
|
||||||
|
///
|
||||||
|
/// @brief Validate json data against a schema
|
||||||
|
/// @param[in] file The path/name of json file just used for log messages
|
||||||
|
/// @param[in] json The json data
|
||||||
|
/// @param[in] schemaP The schema path
|
||||||
|
/// @param[in] log The logger of the caller to print errors
|
||||||
|
/// @return true on success else false
|
||||||
|
///
|
||||||
|
bool validate(const QString& file, const QJsonObject& json, const QString& schemaPath, Logger* log);
|
||||||
|
|
||||||
|
///
|
||||||
|
/// @brief Write json data to file
|
||||||
|
/// @param[in] filenameThe file path to write
|
||||||
|
/// @param[in] json The json data to write
|
||||||
|
/// @param[in] log The logger of the caller to print errors
|
||||||
|
/// @return true on success else false
|
||||||
|
///
|
||||||
|
bool write(const QString& filename, const QJsonObject& json, Logger* log);
|
||||||
|
|
||||||
|
///
|
||||||
|
/// @brief resolve schema $ref attribute
|
||||||
|
/// @param[in] schema the schema to iterate
|
||||||
|
/// @param[out] obj the resolved object
|
||||||
|
/// @param[in] log The logger of the caller to print errors
|
||||||
|
/// @return true on success else false
|
||||||
|
///
|
||||||
|
bool resolveRefs(const QJsonObject& schema, QJsonObject& obj, Logger* log);
|
||||||
|
};
|
@ -22,6 +22,8 @@
|
|||||||
/// - addtionalProperties
|
/// - addtionalProperties
|
||||||
/// - minItems
|
/// - minItems
|
||||||
/// - maxItems
|
/// - maxItems
|
||||||
|
/// - minLength
|
||||||
|
/// - maxLength
|
||||||
|
|
||||||
class QJsonSchemaChecker
|
class QJsonSchemaChecker
|
||||||
{
|
{
|
||||||
|
@ -78,9 +78,8 @@ void Effect::registerHyperionExtensionModule()
|
|||||||
PyImport_AppendInittab("hyperion", &PyInit_hyperion);
|
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()
|
: QThread()
|
||||||
, _mainThreadState(mainThreadState)
|
|
||||||
, _priority(priority)
|
, _priority(priority)
|
||||||
, _timeout(timeout)
|
, _timeout(timeout)
|
||||||
, _script(script)
|
, _script(script)
|
||||||
@ -89,7 +88,6 @@ Effect::Effect(PyThreadState * mainThreadState, int priority, int timeout, const
|
|||||||
, _args(args)
|
, _args(args)
|
||||||
, _endTime(-1)
|
, _endTime(-1)
|
||||||
, _interpreterThreadState(nullptr)
|
, _interpreterThreadState(nullptr)
|
||||||
, _abortRequested(false)
|
|
||||||
, _imageProcessor(ImageProcessorFactory::getInstance().newImageProcessor())
|
, _imageProcessor(ImageProcessorFactory::getInstance().newImageProcessor())
|
||||||
, _colors()
|
, _colors()
|
||||||
, _origin(origin)
|
, _origin(origin)
|
||||||
@ -99,6 +97,8 @@ Effect::Effect(PyThreadState * mainThreadState, int priority, int timeout, const
|
|||||||
_colors.resize(_imageProcessor->getLedCount());
|
_colors.resize(_imageProcessor->getLedCount());
|
||||||
_colors.fill(ColorRgb::BLACK);
|
_colors.fill(ColorRgb::BLACK);
|
||||||
|
|
||||||
|
_log = Logger::getInstance("EFFECTENGINE");
|
||||||
|
|
||||||
// disable the black border detector for effects
|
// disable the black border detector for effects
|
||||||
_imageProcessor->enableBlackBorderDetector(false);
|
_imageProcessor->enableBlackBorderDetector(false);
|
||||||
|
|
||||||
@ -106,8 +106,6 @@ Effect::Effect(PyThreadState * mainThreadState, int priority, int timeout, const
|
|||||||
_image.fill(Qt::black);
|
_image.fill(Qt::black);
|
||||||
_painter = new QPainter(&_image);
|
_painter = new QPainter(&_image);
|
||||||
|
|
||||||
// connect the finished signal
|
|
||||||
connect(this, SIGNAL(finished()), this, SLOT(effectFinished()));
|
|
||||||
Q_INIT_RESOURCE(EffectEngine);
|
Q_INIT_RESOURCE(EffectEngine);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,7 +118,7 @@ Effect::~Effect()
|
|||||||
void Effect::run()
|
void Effect::run()
|
||||||
{
|
{
|
||||||
// switch to the main thread state and acquire the GIL
|
// switch to the main thread state and acquire the GIL
|
||||||
PyEval_RestoreThread(_mainThreadState);
|
PyEval_AcquireLock();
|
||||||
|
|
||||||
// Initialize a new thread state
|
// Initialize a new thread state
|
||||||
_interpreterThreadState = Py_NewInterpreter();
|
_interpreterThreadState = Py_NewInterpreter();
|
||||||
@ -158,13 +156,109 @@ void Effect::run()
|
|||||||
}
|
}
|
||||||
else
|
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();
|
file.close();
|
||||||
|
|
||||||
if (!python_code.isEmpty())
|
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
|
// Clean up the thread state
|
||||||
@ -173,26 +267,6 @@ void Effect::run()
|
|||||||
PyEval_ReleaseLock();
|
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
|
PyObject *Effect::json2python(const QJsonValue &jsonData) const
|
||||||
{
|
{
|
||||||
switch (jsonData.type())
|
switch (jsonData.type())
|
||||||
@ -251,7 +325,7 @@ PyObject* Effect::wrapSetColor(PyObject *self, PyObject *args)
|
|||||||
Effect * effect = getEffect();
|
Effect * effect = getEffect();
|
||||||
|
|
||||||
// check if we have aborted already
|
// check if we have aborted already
|
||||||
if (effect->_abortRequested)
|
if (effect->isInterruptionRequested())
|
||||||
{
|
{
|
||||||
return Py_BuildValue("");
|
return Py_BuildValue("");
|
||||||
}
|
}
|
||||||
@ -333,7 +407,7 @@ PyObject* Effect::wrapSetImage(PyObject *self, PyObject *args)
|
|||||||
Effect * effect = getEffect();
|
Effect * effect = getEffect();
|
||||||
|
|
||||||
// check if we have aborted already
|
// check if we have aborted already
|
||||||
if (effect->_abortRequested)
|
if (effect->isInterruptionRequested())
|
||||||
{
|
{
|
||||||
return Py_BuildValue("");
|
return Py_BuildValue("");
|
||||||
}
|
}
|
||||||
@ -398,10 +472,10 @@ PyObject* Effect::wrapAbort(PyObject *self, PyObject *)
|
|||||||
// Test if the effect has reached it end time
|
// Test if the effect has reached it end time
|
||||||
if (effect->_timeout > 0 && QDateTime::currentMSecsSinceEpoch() > effect->_endTime)
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -19,12 +19,12 @@ class Effect : public QThread
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
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 ~Effect();
|
||||||
|
|
||||||
virtual void run();
|
virtual void run();
|
||||||
|
|
||||||
int getPriority() const;
|
int getPriority() const { return _priority; };
|
||||||
|
|
||||||
QString getScript() const { return _script; }
|
QString getScript() const { return _script; }
|
||||||
QString getName() const { return _name; }
|
QString getName() const { return _name; }
|
||||||
@ -33,22 +33,12 @@ public:
|
|||||||
|
|
||||||
QJsonObject getArgs() const { return _args; }
|
QJsonObject getArgs() const { return _args; }
|
||||||
|
|
||||||
bool isAbortRequested() const;
|
|
||||||
|
|
||||||
/// This function registers the extension module in Python
|
/// This function registers the extension module in Python
|
||||||
static void registerHyperionExtensionModule();
|
static void registerHyperionExtensionModule();
|
||||||
|
|
||||||
public slots:
|
|
||||||
void abort();
|
|
||||||
|
|
||||||
signals:
|
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);
|
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:
|
private:
|
||||||
PyObject * json2python(const QJsonValue & jsonData) const;
|
PyObject * json2python(const QJsonValue & jsonData) const;
|
||||||
|
|
||||||
@ -88,8 +78,6 @@ private:
|
|||||||
|
|
||||||
void addImage();
|
void addImage();
|
||||||
|
|
||||||
PyThreadState * _mainThreadState;
|
|
||||||
|
|
||||||
const int _priority;
|
const int _priority;
|
||||||
|
|
||||||
const int _timeout;
|
const int _timeout;
|
||||||
@ -104,14 +92,13 @@ private:
|
|||||||
|
|
||||||
PyThreadState * _interpreterThreadState;
|
PyThreadState * _interpreterThreadState;
|
||||||
|
|
||||||
bool _abortRequested;
|
|
||||||
|
|
||||||
/// The processor for translating images to led-values
|
/// The processor for translating images to led-values
|
||||||
ImageProcessor * _imageProcessor;
|
ImageProcessor * _imageProcessor;
|
||||||
|
|
||||||
/// Buffer for colorData
|
/// Buffer for colorData
|
||||||
QVector<ColorRgb> _colors;
|
QVector<ColorRgb> _colors;
|
||||||
|
|
||||||
|
Logger* _log;
|
||||||
|
|
||||||
QString _origin;
|
QString _origin;
|
||||||
QSize _imageSize;
|
QSize _imageSize;
|
||||||
@ -119,4 +106,3 @@ private:
|
|||||||
QPainter* _painter;
|
QPainter* _painter;
|
||||||
QVector<QImage> _imageStack;
|
QVector<QImage> _imageStack;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
|
|
||||||
// hyperion util includes
|
// hyperion util includes
|
||||||
#include <utils/jsonschema/QJsonSchemaChecker.h>
|
#include <utils/jsonschema/QJsonSchemaChecker.h>
|
||||||
#include <utils/FileUtils.h>
|
#include <utils/JsonUtils.h>
|
||||||
#include <utils/Components.h>
|
#include <utils/Components.h>
|
||||||
|
|
||||||
// effect engine includes
|
// effect engine includes
|
||||||
@ -74,95 +74,21 @@ const std::list<ActiveEffectDefinition> &EffectEngine::getActiveEffects()
|
|||||||
|
|
||||||
bool EffectEngine::loadEffectDefinition(const QString &path, const QString &effectConfigFile, EffectDefinition & effectDefinition)
|
bool EffectEngine::loadEffectDefinition(const QString &path, const QString &effectConfigFile, EffectDefinition & effectDefinition)
|
||||||
{
|
{
|
||||||
Logger * log = Logger::getInstance("EFFECTENGINE");
|
|
||||||
|
|
||||||
QString fileName = path + QDir::separator() + effectConfigFile;
|
QString fileName = path + QDir::separator() + effectConfigFile;
|
||||||
QJsonParseError error;
|
|
||||||
|
|
||||||
// ---------- Read the effect json config file ----------
|
// Read and parse the effect json config file
|
||||||
|
QJsonObject configEffect;
|
||||||
QFile file(fileName);
|
if(!JsonUtils::readFile(fileName, configEffect, _log))
|
||||||
if (!file.open(QIODevice::ReadOnly))
|
|
||||||
{
|
|
||||||
Error( log, "Effect file '%s' could not be loaded", QSTRING_CSTR(fileName));
|
|
||||||
return false;
|
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);
|
Q_INIT_RESOURCE(EffectEngine);
|
||||||
QFile schema(":effect-schema");
|
// validate effect config with effect schema(path)
|
||||||
|
if(!JsonUtils::validate(fileName, configEffect, ":effect-schema", _log))
|
||||||
if (!schema.open(QIODevice::ReadOnly))
|
|
||||||
{
|
|
||||||
Error( log, "Schema not found: %s", QSTRING_CSTR(schema.errorString()));
|
|
||||||
return false;
|
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;
|
effectDefinition.file = fileName;
|
||||||
QJsonObject config = configEffect.object();
|
QJsonObject config = configEffect;
|
||||||
QString scriptName = config["script"].toString();
|
QString scriptName = config["script"].toString();
|
||||||
effectDefinition.name = config["name"].toString();
|
effectDefinition.name = config["name"].toString();
|
||||||
if (scriptName.isEmpty())
|
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)
|
bool EffectEngine::loadEffectSchema(const QString &path, const QString &effectSchemaFile, EffectSchema & effectSchema)
|
||||||
{
|
{
|
||||||
Logger * log = Logger::getInstance("EFFECTENGINE");
|
|
||||||
|
|
||||||
QString fileName = path + "schema/" + QDir::separator() + effectSchemaFile;
|
QString fileName = path + "schema/" + QDir::separator() + effectSchemaFile;
|
||||||
QJsonParseError error;
|
|
||||||
|
|
||||||
// ---------- Read the effect schema file ----------
|
// Read and parse the effect schema file
|
||||||
|
QJsonObject schemaEffect;
|
||||||
QFile file(fileName);
|
if(!JsonUtils::readFile(fileName, schemaEffect, _log))
|
||||||
if (!file.open(QIODevice::ReadOnly))
|
|
||||||
{
|
|
||||||
Error( log, "Effect schema '%s' could not be loaded", QSTRING_CSTR(fileName));
|
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
QByteArray fileContent = file.readAll();
|
// setup the definition
|
||||||
QJsonDocument schemaEffect = QJsonDocument::fromJson(fileContent, &error);
|
QString scriptName = schemaEffect["script"].toString();
|
||||||
|
|
||||||
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();
|
|
||||||
effectSchema.schemaFile = fileName;
|
effectSchema.schemaFile = fileName;
|
||||||
fileName = path + QDir::separator() + scriptName;
|
fileName = path + QDir::separator() + scriptName;
|
||||||
QFile pyFile(fileName);
|
QFile pyFile(fileName);
|
||||||
@ -249,14 +142,14 @@ bool EffectEngine::loadEffectSchema(const QString &path, const QString &effectSc
|
|||||||
if (scriptName.isEmpty() || !pyFile.open(QIODevice::ReadOnly))
|
if (scriptName.isEmpty() || !pyFile.open(QIODevice::ReadOnly))
|
||||||
{
|
{
|
||||||
fileName = path + "schema/" + QDir::separator() + effectSchemaFile;
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
pyFile.close();
|
pyFile.close();
|
||||||
|
|
||||||
effectSchema.pyFile = (scriptName.mid(0, 1) == ":" ) ? ":/effects/"+scriptName.mid(1) : path + QDir::separator() + scriptName;
|
effectSchema.pyFile = (scriptName.mid(0, 1) == ":" ) ? ":/effects/"+scriptName.mid(1) : path + QDir::separator() + scriptName;
|
||||||
effectSchema.pySchema = tempSchemaEffect;
|
effectSchema.pySchema = schemaEffect;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -277,7 +170,7 @@ void EffectEngine::readEffects()
|
|||||||
|
|
||||||
for(auto p : paths)
|
for(auto p : paths)
|
||||||
{
|
{
|
||||||
efxPathList << p.toString();
|
efxPathList << p.toString().replace("$ROOT",_hyperion->getRootPath());
|
||||||
}
|
}
|
||||||
for(auto efx : disabledEfx)
|
for(auto efx : disabledEfx)
|
||||||
{
|
{
|
||||||
@ -309,7 +202,7 @@ void EffectEngine::readEffects()
|
|||||||
if (loadEffectDefinition(path, filename, def))
|
if (loadEffectDefinition(path, filename, def))
|
||||||
{
|
{
|
||||||
InfoIf(availableEffects.find(def.name) != availableEffects.end(), _log,
|
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) )
|
if ( disableList.contains(def.name) )
|
||||||
{
|
{
|
||||||
@ -326,7 +219,7 @@ void EffectEngine::readEffects()
|
|||||||
|
|
||||||
// collect effect schemas
|
// collect effect schemas
|
||||||
efxCount = 0;
|
efxCount = 0;
|
||||||
directory = path + "schema/";
|
directory = path.endsWith("/") ? (path + "schema/") : (path + "/schema/");
|
||||||
QStringList pynames = directory.entryList(QStringList() << "*.json", QDir::Files, QDir::Name | QDir::IgnoreCase);
|
QStringList pynames = directory.entryList(QStringList() << "*.json", QDir::Files, QDir::Name | QDir::IgnoreCase);
|
||||||
for (const QString & pyname : pynames)
|
for (const QString & pyname : pynames)
|
||||||
{
|
{
|
||||||
@ -387,9 +280,9 @@ int EffectEngine::runEffectScript(const QString &script, const QString &name, co
|
|||||||
channelCleared(priority);
|
channelCleared(priority);
|
||||||
|
|
||||||
// create the effect
|
// 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(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);
|
_activeEffects.push_back(effect);
|
||||||
|
|
||||||
// start the effect
|
// start the effect
|
||||||
@ -405,7 +298,7 @@ void EffectEngine::channelCleared(int priority)
|
|||||||
{
|
{
|
||||||
if (effect->getPriority() == priority)
|
if (effect->getPriority() == priority)
|
||||||
{
|
{
|
||||||
effect->abort();
|
effect->requestInterruption();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -414,13 +307,17 @@ void EffectEngine::allChannelsCleared()
|
|||||||
{
|
{
|
||||||
for (Effect * effect : _activeEffects)
|
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
|
// effect stopped by itself. Clear the channel
|
||||||
_hyperion->clear(effect->getPriority());
|
_hyperion->clear(effect->getPriority());
|
||||||
|
@ -35,11 +35,11 @@
|
|||||||
|
|
||||||
Hyperion* Hyperion::_hyperion = nullptr;
|
Hyperion* Hyperion::_hyperion = nullptr;
|
||||||
|
|
||||||
Hyperion* Hyperion::initInstance(const QJsonObject& qjsonConfig, const QString configFile) // REMOVE jsonConfig variable when the conversion from jsonCPP to QtJSON is finished
|
Hyperion* Hyperion::initInstance(const QJsonObject& qjsonConfig, const QString configFile, const QString rootPath)
|
||||||
{
|
{
|
||||||
if ( Hyperion::_hyperion != nullptr )
|
if ( Hyperion::_hyperion != nullptr )
|
||||||
throw std::runtime_error("Hyperion::initInstance can be called only one time");
|
throw std::runtime_error("Hyperion::initInstance can be called only one time");
|
||||||
Hyperion::_hyperion = new Hyperion(qjsonConfig, configFile);
|
Hyperion::_hyperion = new Hyperion(qjsonConfig, configFile, rootPath);
|
||||||
|
|
||||||
return Hyperion::_hyperion;
|
return Hyperion::_hyperion;
|
||||||
}
|
}
|
||||||
@ -381,7 +381,7 @@ MessageForwarder * Hyperion::getForwarder()
|
|||||||
return _messageForwarder;
|
return _messageForwarder;
|
||||||
}
|
}
|
||||||
|
|
||||||
Hyperion::Hyperion(const QJsonObject &qjsonConfig, const QString configFile)
|
Hyperion::Hyperion(const QJsonObject &qjsonConfig, const QString configFile, const QString rootPath)
|
||||||
: _ledString(createLedString(qjsonConfig["leds"], createColorOrder(qjsonConfig["device"].toObject())))
|
: _ledString(createLedString(qjsonConfig["leds"], createColorOrder(qjsonConfig["device"].toObject())))
|
||||||
, _ledStringClone(createLedStringClone(qjsonConfig["leds"], createColorOrder(qjsonConfig["device"].toObject())))
|
, _ledStringClone(createLedStringClone(qjsonConfig["leds"], createColorOrder(qjsonConfig["device"].toObject())))
|
||||||
, _muxer(_ledString.leds().size())
|
, _muxer(_ledString.leds().size())
|
||||||
@ -390,6 +390,7 @@ Hyperion::Hyperion(const QJsonObject &qjsonConfig, const QString configFile)
|
|||||||
, _messageForwarder(createMessageForwarder(qjsonConfig["forwarder"].toObject()))
|
, _messageForwarder(createMessageForwarder(qjsonConfig["forwarder"].toObject()))
|
||||||
, _qjsonConfig(qjsonConfig)
|
, _qjsonConfig(qjsonConfig)
|
||||||
, _configFile(configFile)
|
, _configFile(configFile)
|
||||||
|
, _rootPath(rootPath)
|
||||||
, _timer()
|
, _timer()
|
||||||
, _timerBonjourResolver()
|
, _timerBonjourResolver()
|
||||||
, _log(CORE_LOGGER)
|
, _log(CORE_LOGGER)
|
||||||
@ -468,9 +469,6 @@ Hyperion::Hyperion(const QJsonObject &qjsonConfig, const QString configFile)
|
|||||||
_fsi_timer.setSingleShot(true);
|
_fsi_timer.setSingleShot(true);
|
||||||
_fsi_blockTimer.setSingleShot(true);
|
_fsi_blockTimer.setSingleShot(true);
|
||||||
|
|
||||||
const QJsonObject & generalConfig = qjsonConfig["general"].toObject();
|
|
||||||
_configVersionId = generalConfig["configVersion"].toInt(-1);
|
|
||||||
|
|
||||||
// initialize the leds
|
// initialize the leds
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
"type" : "integer",
|
"type" : "integer",
|
||||||
"required" : true,
|
"required" : true,
|
||||||
"title" : "edt_conf_general_port_title",
|
"title" : "edt_conf_general_port_title",
|
||||||
"minimum" : 0,
|
"minimum" : 1024,
|
||||||
"maximum" : 65535,
|
"maximum" : 65535,
|
||||||
"propertyOrder" : 2
|
"propertyOrder" : 2
|
||||||
},
|
},
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
{
|
{
|
||||||
"type" : "array",
|
"type" : "array",
|
||||||
"title" : "edt_conf_effp_paths_title",
|
"title" : "edt_conf_effp_paths_title",
|
||||||
"default" : ["../custom-effects"],
|
"default" : ["$ROOT/custom-effects"],
|
||||||
"items" : {
|
"items" : {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"title" : "edt_conf_effp_paths_itemtitle"
|
"title" : "edt_conf_effp_paths_itemtitle"
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
"type" : "integer",
|
"type" : "integer",
|
||||||
"required" : true,
|
"required" : true,
|
||||||
"title" : "edt_conf_general_port_title",
|
"title" : "edt_conf_general_port_title",
|
||||||
"minimum" : 0,
|
"minimum" : 1024,
|
||||||
"maximum" : 65535,
|
"maximum" : 65535,
|
||||||
"default" : 19444
|
"default" : 19444
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
{
|
{
|
||||||
"type":"array",
|
"type":"array",
|
||||||
"required":true,
|
"required":true,
|
||||||
|
"minItems":1,
|
||||||
"items":
|
"items":
|
||||||
{
|
{
|
||||||
"type":"object",
|
"type":"object",
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
"type" : "integer",
|
"type" : "integer",
|
||||||
"required" : true,
|
"required" : true,
|
||||||
"title" : "edt_conf_general_port_title",
|
"title" : "edt_conf_general_port_title",
|
||||||
"minimum" : 0,
|
"minimum" : 1024,
|
||||||
"maximum" : 65535,
|
"maximum" : 65535,
|
||||||
"default" : 19445
|
"default" : 19445
|
||||||
}
|
}
|
||||||
|
@ -22,9 +22,9 @@
|
|||||||
{
|
{
|
||||||
"type" : "integer",
|
"type" : "integer",
|
||||||
"title" : "edt_conf_general_port_title",
|
"title" : "edt_conf_general_port_title",
|
||||||
"minimum" : 0,
|
"minimum" : 80,
|
||||||
"maximum" : 65535,
|
"maximum" : 65535,
|
||||||
"default" : 8099,
|
"default" : 8090,
|
||||||
"access" : "expert",
|
"access" : "expert",
|
||||||
"propertyOrder" : 3
|
"propertyOrder" : 3
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,7 @@ JsonClientConnection::JsonClientConnection(QTcpSocket *socket)
|
|||||||
connect(_socket, SIGNAL(readyRead()), this, SLOT(readData()));
|
connect(_socket, SIGNAL(readyRead()), this, SLOT(readData()));
|
||||||
|
|
||||||
// create a new instance of JsonProcessor
|
// create a new instance of JsonProcessor
|
||||||
_jsonProcessor = new JsonProcessor(_clientAddress.toString());
|
_jsonProcessor = new JsonProcessor(_clientAddress.toString(), _log);
|
||||||
// get the callback messages from JsonProcessor and send it to the client
|
// get the callback messages from JsonProcessor and send it to the client
|
||||||
connect(_jsonProcessor,SIGNAL(callbackMessage(QJsonObject)),this,SLOT(sendMessage(QJsonObject)));
|
connect(_jsonProcessor,SIGNAL(callbackMessage(QJsonObject)),this,SLOT(sendMessage(QJsonObject)));
|
||||||
}
|
}
|
||||||
@ -378,6 +378,7 @@ qint64 JsonClientConnection::sendMessage_Websockets(QByteArray &data)
|
|||||||
|
|
||||||
QByteArray buf = makeFrameHeader(OPCODE::TEXT, frameSize, isLastFrame);
|
QByteArray buf = makeFrameHeader(OPCODE::TEXT, frameSize, isLastFrame);
|
||||||
sendMessage_Raw(buf);
|
sendMessage_Raw(buf);
|
||||||
|
|
||||||
qint64 written = sendMessage_Raw(payload+position,frameSize);
|
qint64 written = sendMessage_Raw(payload+position,frameSize);
|
||||||
if (written > 0)
|
if (written > 0)
|
||||||
{
|
{
|
||||||
@ -419,4 +420,3 @@ void JsonClientConnection::handleBinaryMessage(QByteArray &data)
|
|||||||
memcpy(image.memptr(), data.data()+4, imgSize);
|
memcpy(image.memptr(), data.data()+4, imgSize);
|
||||||
_hyperion->setImage(priority, image, duration_s*1000);
|
_hyperion->setImage(priority, image, duration_s*1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
|
|
||||||
#include "hyperion/Hyperion.h"
|
#include "hyperion/Hyperion.h"
|
||||||
|
#include <utils/JsonUtils.h>
|
||||||
|
|
||||||
LedDeviceRegistry LedDevice::_ledDeviceMap = LedDeviceRegistry();
|
LedDeviceRegistry LedDevice::_ledDeviceMap = LedDeviceRegistry();
|
||||||
QString LedDevice::_activeDevice = "";
|
QString LedDevice::_activeDevice = "";
|
||||||
@ -91,7 +92,6 @@ QJsonObject LedDevice::getLedDeviceSchemas()
|
|||||||
{
|
{
|
||||||
// make sure the resources are loaded (they may be left out after static linking)
|
// make sure the resources are loaded (they may be left out after static linking)
|
||||||
Q_INIT_RESOURCE(LedDeviceSchemas);
|
Q_INIT_RESOURCE(LedDeviceSchemas);
|
||||||
QJsonParseError error;
|
|
||||||
|
|
||||||
// read the json schema from the resource
|
// read the json schema from the resource
|
||||||
QDir d(":/leddevices/");
|
QDir d(":/leddevices/");
|
||||||
@ -100,40 +100,22 @@ QJsonObject LedDevice::getLedDeviceSchemas()
|
|||||||
|
|
||||||
for(QString &item : l)
|
for(QString &item : l)
|
||||||
{
|
{
|
||||||
QFile schemaData(QString(":/leddevices/")+item);
|
QString schemaPath(QString(":/leddevices/")+item);
|
||||||
QString devName = item.remove("schema-");
|
QString devName = item.remove("schema-");
|
||||||
|
|
||||||
if (!schemaData.open(QIODevice::ReadOnly))
|
QString data;
|
||||||
|
if(!FileUtils::readFile(schemaPath, data, Logger::getInstance("LedDevice")))
|
||||||
{
|
{
|
||||||
Error(Logger::getInstance("LedDevice"), "Schema not found: %s", QSTRING_CSTR(item));
|
|
||||||
throw std::runtime_error("ERROR: Schema not found: " + item.toStdString());
|
throw std::runtime_error("ERROR: Schema not found: " + item.toStdString());
|
||||||
}
|
}
|
||||||
|
|
||||||
QByteArray schema = schemaData.readAll();
|
QJsonObject schema;
|
||||||
QJsonDocument doc = QJsonDocument::fromJson(schema, &error);
|
if(!JsonUtils::parse(schemaPath, data, schema, Logger::getInstance("LedDevice")))
|
||||||
schemaData.close();
|
|
||||||
|
|
||||||
if (error.error != QJsonParseError::NoError)
|
|
||||||
{
|
{
|
||||||
// report to the user the failure and their locations in the document.
|
throw std::runtime_error("ERROR: Json schema wrong of file: " + item.toStdString());
|
||||||
int errorLine(0), errorColumn(0);
|
|
||||||
|
|
||||||
for( int i=0, count=qMin( error.offset,schema.size()); i<count; ++i )
|
|
||||||
{
|
|
||||||
++errorColumn;
|
|
||||||
if(schema.at(i) == '\n' )
|
|
||||||
{
|
|
||||||
errorColumn = 0;
|
|
||||||
++errorLine;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString errorMsg = error.errorString() + " at Line: " + QString::number(errorLine) + ", Column: " + QString::number(errorColumn);
|
schemaJson = schema;
|
||||||
Error(Logger::getInstance("LedDevice"), "LedDevice JSON schema error in %s (%s)", QSTRING_CSTR(item), QSTRING_CSTR(errorMsg));
|
|
||||||
throw std::runtime_error("ERROR: Json schema wrong: " + errorMsg.toStdString());
|
|
||||||
}
|
|
||||||
|
|
||||||
schemaJson = doc.object();
|
|
||||||
schemaJson["title"] = QString("edt_dev_spec_header_title");
|
schemaJson["title"] = QString("edt_dev_spec_header_title");
|
||||||
|
|
||||||
result[devName] = schemaJson;
|
result[devName] = schemaJson;
|
||||||
|
@ -1,6 +1,11 @@
|
|||||||
#include <utils/FileUtils.h>
|
#include <utils/FileUtils.h>
|
||||||
|
|
||||||
|
// qt incl
|
||||||
#include <QFileInfo>
|
#include <QFileInfo>
|
||||||
|
#include <QDebug>
|
||||||
|
|
||||||
|
// hyperion include
|
||||||
|
#include <hyperion/Hyperion.h>
|
||||||
|
|
||||||
namespace FileUtils {
|
namespace FileUtils {
|
||||||
|
|
||||||
@ -16,5 +21,126 @@ QString getDirName( QString sourceFile)
|
|||||||
return fi.path();
|
return fi.path();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool fileExists(const QString& path, Logger* log, bool ignError)
|
||||||
|
{
|
||||||
|
QFile file(path);
|
||||||
|
if(!file.exists())
|
||||||
|
{
|
||||||
|
ErrorIf((!ignError), log,"File does not exist: %s",QSTRING_CSTR(path));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool readFile(const QString& path, QString& data, Logger* log, bool ignError)
|
||||||
|
{
|
||||||
|
QFile file(path);
|
||||||
|
if(!fileExists(path, log, ignError))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!file.open(QFile::ReadOnly | QFile::Text))
|
||||||
|
{
|
||||||
|
if(!ignError)
|
||||||
|
resolveFileError(file,log);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
data = QString(file.readAll());
|
||||||
|
file.close();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool writeFile(const QString& path, const QByteArray& data, Logger* log)
|
||||||
|
{
|
||||||
|
QFile file(path);
|
||||||
|
if (!file.open(QFile::WriteOnly | QFile::Truncate))
|
||||||
|
{
|
||||||
|
resolveFileError(file,log);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(file.write(data) == -1)
|
||||||
|
{
|
||||||
|
resolveFileError(file,log);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
file.close();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool removeFile(const QString& path, Logger* log)
|
||||||
|
{
|
||||||
|
QFile file(path);
|
||||||
|
if(!file.remove())
|
||||||
|
{
|
||||||
|
resolveFileError(file,log);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString convertPath(const QString path)
|
||||||
|
{
|
||||||
|
QString p = path;
|
||||||
|
return p.replace(QString("$ROOT"), Hyperion::getInstance()->getRootPath());
|
||||||
|
}
|
||||||
|
|
||||||
|
void resolveFileError(const QFile& file, Logger* log)
|
||||||
|
{
|
||||||
|
QFile::FileError error = file.error();
|
||||||
|
const char* fn = QSTRING_CSTR(file.fileName());
|
||||||
|
switch(error)
|
||||||
|
{
|
||||||
|
case QFileDevice::NoError:
|
||||||
|
Debug(log,"No error occurred while procesing file: %s",fn);
|
||||||
|
break;
|
||||||
|
case QFileDevice::ReadError:
|
||||||
|
Error(log,"Can't read file: %s",fn);
|
||||||
|
break;
|
||||||
|
case QFileDevice::WriteError:
|
||||||
|
Error(log,"Can't write file: %s",fn);
|
||||||
|
break;
|
||||||
|
case QFileDevice::FatalError:
|
||||||
|
Error(log,"Fatal error while processing file: %s",fn);
|
||||||
|
break;
|
||||||
|
case QFileDevice::ResourceError:
|
||||||
|
Error(log,"Resource Error while processing file: %s",fn);
|
||||||
|
break;
|
||||||
|
case QFileDevice::OpenError:
|
||||||
|
Error(log,"Can't open file: %s",fn);
|
||||||
|
break;
|
||||||
|
case QFileDevice::AbortError:
|
||||||
|
Error(log,"Abort Error while processing file: %s",fn);
|
||||||
|
break;
|
||||||
|
case QFileDevice::TimeOutError:
|
||||||
|
Error(log,"Timeout Error while processing file: %s",fn);
|
||||||
|
break;
|
||||||
|
case QFileDevice::UnspecifiedError:
|
||||||
|
Error(log,"Unspecified Error while processing file: %s",fn);
|
||||||
|
break;
|
||||||
|
case QFileDevice::RemoveError:
|
||||||
|
Error(log,"Failed to remove file: %s",fn);
|
||||||
|
break;
|
||||||
|
case QFileDevice::RenameError:
|
||||||
|
Error(log,"Failed to rename file: %s",fn);
|
||||||
|
break;
|
||||||
|
case QFileDevice::PositionError:
|
||||||
|
Error(log,"Position Error while processing file: %s",fn);
|
||||||
|
break;
|
||||||
|
case QFileDevice::ResizeError:
|
||||||
|
Error(log,"Resize Error while processing file: %s",fn);
|
||||||
|
break;
|
||||||
|
case QFileDevice::PermissionsError:
|
||||||
|
Error(log,"Permission Error at file: %s",fn);
|
||||||
|
break;
|
||||||
|
case QFileDevice::CopyError:
|
||||||
|
Error(log,"Error during file copy of file: %s",fn);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
// project includes
|
// project includes
|
||||||
#include <utils/JsonProcessor.h>
|
#include <utils/JsonProcessor.h>
|
||||||
|
|
||||||
@ -12,13 +11,11 @@
|
|||||||
#include <QResource>
|
#include <QResource>
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
#include <QCryptographicHash>
|
#include <QCryptographicHash>
|
||||||
#include <QFile>
|
|
||||||
#include <QFileInfo>
|
|
||||||
#include <QJsonDocument>
|
|
||||||
#include <QDir>
|
|
||||||
#include <QImage>
|
#include <QImage>
|
||||||
#include <QBuffer>
|
#include <QBuffer>
|
||||||
#include <QByteArray>
|
#include <QByteArray>
|
||||||
|
#include <QFileInfo>
|
||||||
|
#include <QDir>
|
||||||
#include <QIODevice>
|
#include <QIODevice>
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
|
|
||||||
@ -32,15 +29,16 @@
|
|||||||
#include <leddevice/LedDevice.h>
|
#include <leddevice/LedDevice.h>
|
||||||
#include <hyperion/GrabberWrapper.h>
|
#include <hyperion/GrabberWrapper.h>
|
||||||
#include <utils/Process.h>
|
#include <utils/Process.h>
|
||||||
|
#include <utils/JsonUtils.h>
|
||||||
|
|
||||||
using namespace hyperion;
|
using namespace hyperion;
|
||||||
|
|
||||||
std::map<hyperion::Components, bool> JsonProcessor::_componentsPrevState;
|
std::map<hyperion::Components, bool> JsonProcessor::_componentsPrevState;
|
||||||
|
|
||||||
JsonProcessor::JsonProcessor(QString peerAddress, bool noListener)
|
JsonProcessor::JsonProcessor(QString peerAddress, Logger* log, bool noListener)
|
||||||
: QObject()
|
: QObject()
|
||||||
, _peerAddress(peerAddress)
|
, _peerAddress(peerAddress)
|
||||||
, _log(Logger::getInstance("JSONRPCPROCESSOR"))
|
, _log(log)
|
||||||
, _hyperion(Hyperion::getInstance())
|
, _hyperion(Hyperion::getInstance())
|
||||||
, _imageProcessor(ImageProcessorFactory::getInstance().newImageProcessor())
|
, _imageProcessor(ImageProcessorFactory::getInstance().newImageProcessor())
|
||||||
, _streaming_logging_activated(false)
|
, _streaming_logging_activated(false)
|
||||||
@ -48,7 +46,7 @@ JsonProcessor::JsonProcessor(QString peerAddress, bool noListener)
|
|||||||
{
|
{
|
||||||
// notify hyperion about a jsonMessageForward
|
// notify hyperion about a jsonMessageForward
|
||||||
connect(this, &JsonProcessor::forwardJsonMessage, _hyperion, &Hyperion::forwardJsonMessage);
|
connect(this, &JsonProcessor::forwardJsonMessage, _hyperion, &Hyperion::forwardJsonMessage);
|
||||||
// notify hyperion about a push emit
|
// notify hyperion about a push emit TODO: Remove! Make sure that the target of the commands trigger this (less error margin) instead this instance
|
||||||
connect(this, &JsonProcessor::pushReq, _hyperion, &Hyperion::hyperionStateChanged);
|
connect(this, &JsonProcessor::pushReq, _hyperion, &Hyperion::hyperionStateChanged);
|
||||||
|
|
||||||
if(!noListener)
|
if(!noListener)
|
||||||
@ -73,52 +71,32 @@ void JsonProcessor::handleMessage(const QString& messageString, const QString pe
|
|||||||
if(!peerAddress.isNull())
|
if(!peerAddress.isNull())
|
||||||
_peerAddress = peerAddress;
|
_peerAddress = peerAddress;
|
||||||
|
|
||||||
QString errors;
|
const QString ident = "JsonRpc@"+_peerAddress;
|
||||||
|
Q_INIT_RESOURCE(JSONRPC_schemas);
|
||||||
try
|
QJsonObject message;
|
||||||
|
// parse the message
|
||||||
|
if(!JsonUtils::parse(ident, messageString, message, _log))
|
||||||
{
|
{
|
||||||
QJsonParseError error;
|
sendErrorReply("Errors during message parsing, please consult the Hyperion Log. Data:"+messageString);
|
||||||
QJsonDocument doc = QJsonDocument::fromJson(messageString.toUtf8(), &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,messageString.size()); i<count; ++i )
|
|
||||||
{
|
|
||||||
++errorColumn;
|
|
||||||
if(messageString.at(i) == '\n' )
|
|
||||||
{
|
|
||||||
errorColumn = 0;
|
|
||||||
++errorLine;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::stringstream sstream;
|
|
||||||
sstream << "Error while parsing json: " << error.errorString().toStdString() << " at Line: " << errorLine << ", Column: " << errorColumn;
|
|
||||||
sendErrorReply(QString::fromStdString(sstream.str()));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const QJsonObject message = doc.object();
|
|
||||||
|
|
||||||
// check basic message
|
// check basic message
|
||||||
if (!checkJson(message, ":schema", errors))
|
if(!JsonUtils::validate(ident, message, ":schema", _log))
|
||||||
{
|
{
|
||||||
sendErrorReply("Error while validating json: " + errors);
|
sendErrorReply("Errors during message validation, please consult the Hyperion Log.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// check specific message
|
// check specific message
|
||||||
const QString command = message["command"].toString();
|
const QString command = message["command"].toString();
|
||||||
if (!checkJson(message, QString(":schema-%1").arg(command), errors))
|
if(!JsonUtils::validate(ident, message, QString(":schema-%1").arg(command), _log))
|
||||||
{
|
{
|
||||||
sendErrorReply("Error while validating json: " + errors);
|
sendErrorReply("Errors during specific message validation, please consult the Hyperion Log");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int tan = message["tan"].toInt(0);
|
int tan = message["tan"].toInt();
|
||||||
// switch over all possible commands and handle them
|
// switch over all possible commands and handle them
|
||||||
if (command == "color") handleColorCommand (message, command, tan);
|
if (command == "color") handleColorCommand (message, command, tan);
|
||||||
else if (command == "image") handleImageCommand (message, command, tan);
|
else if (command == "image") handleImageCommand (message, command, tan);
|
||||||
@ -139,12 +117,6 @@ void JsonProcessor::handleMessage(const QString& messageString, const QString pe
|
|||||||
else if (command == "videomode") handleVideoModeCommand (message, command, tan);
|
else if (command == "videomode") handleVideoModeCommand (message, command, tan);
|
||||||
else handleNotImplemented ();
|
else handleNotImplemented ();
|
||||||
}
|
}
|
||||||
catch (std::exception& e)
|
|
||||||
{
|
|
||||||
sendErrorReply("Error while processing incoming json message: " + QString(e.what()) + " " + errors );
|
|
||||||
Warning(_log, "Error while processing incoming json message: %s (%s)", e.what(), errors.toStdString().c_str());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void JsonProcessor::handleColorCommand(const QJsonObject& message, const QString& command, const int tan)
|
void JsonProcessor::handleColorCommand(const QJsonObject& message, const QString& command, const int tan)
|
||||||
{
|
{
|
||||||
@ -246,8 +218,6 @@ void JsonProcessor::handleEffectCommand(const QJsonObject& message, const QStrin
|
|||||||
}
|
}
|
||||||
|
|
||||||
void JsonProcessor::handleCreateEffectCommand(const QJsonObject& message, const QString &command, const int tan)
|
void JsonProcessor::handleCreateEffectCommand(const QJsonObject& message, const QString &command, const int tan)
|
||||||
{
|
|
||||||
if(message.size() > 0)
|
|
||||||
{
|
{
|
||||||
if (!message["args"].toObject().isEmpty())
|
if (!message["args"].toObject().isEmpty())
|
||||||
{
|
{
|
||||||
@ -261,11 +231,9 @@ void JsonProcessor::handleCreateEffectCommand(const QJsonObject& message, const
|
|||||||
|
|
||||||
if (it != effectsSchemas.end())
|
if (it != effectsSchemas.end())
|
||||||
{
|
{
|
||||||
QString errors;
|
if(!JsonUtils::validate("JsonRpc@"+_peerAddress, message["args"].toObject(), it->schemaFile, _log))
|
||||||
|
|
||||||
if (!checkJson(message["args"].toObject(), it->schemaFile, errors))
|
|
||||||
{
|
{
|
||||||
sendErrorReply("Error while validating json: " + errors, command, tan);
|
sendErrorReply("Error during arg validation against schema, please consult the Hyperion Log", command, tan);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -299,15 +267,16 @@ void JsonProcessor::handleCreateEffectCommand(const QJsonObject& message, const
|
|||||||
}
|
}
|
||||||
} else
|
} else
|
||||||
{
|
{
|
||||||
newFileName.setFile(effectArray[0].toString() + QDir::separator() + message["name"].toString().replace(QString(" "), QString("")) + QString(".json"));
|
QString f = FileUtils::convertPath(effectArray[0].toString() + "/" + message["name"].toString().replace(QString(" "), QString("")) + QString(".json"));
|
||||||
|
newFileName.setFile(f);
|
||||||
|
}
|
||||||
|
|
||||||
while(newFileName.exists())
|
if(!JsonUtils::write(newFileName.absoluteFilePath(), effectJson, _log))
|
||||||
{
|
{
|
||||||
newFileName.setFile(effectArray[0].toString() + QDir::separator() + newFileName.baseName() + QString::number(qrand() % ((10) - 0) + 0) + QString(".json"));
|
sendErrorReply("Error while saving effect, please check the Hyperion Log", command, tan);
|
||||||
}
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
QJsonFactory::writeJson(newFileName.absoluteFilePath(), effectJson);
|
|
||||||
Info(_log, "Reload effect list");
|
Info(_log, "Reload effect list");
|
||||||
_hyperion->reloadEffects();
|
_hyperion->reloadEffects();
|
||||||
sendSuccessReply(command, tan);
|
sendSuccessReply(command, tan);
|
||||||
@ -320,13 +289,9 @@ void JsonProcessor::handleCreateEffectCommand(const QJsonObject& message, const
|
|||||||
sendErrorReply("Missing schema file for Python script " + message["script"].toString(), command, tan);
|
sendErrorReply("Missing schema file for Python script " + message["script"].toString(), command, tan);
|
||||||
} else
|
} else
|
||||||
sendErrorReply("Missing or empty Object 'args'", command, tan);
|
sendErrorReply("Missing or empty Object 'args'", command, tan);
|
||||||
} else
|
|
||||||
sendErrorReply("Error while parsing json: Message size " + QString(message.size()), command, tan);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void JsonProcessor::handleDeleteEffectCommand(const QJsonObject& message, const QString& command, const int tan)
|
void JsonProcessor::handleDeleteEffectCommand(const QJsonObject& message, const QString& command, const int tan)
|
||||||
{
|
|
||||||
if(message.size() > 0)
|
|
||||||
{
|
{
|
||||||
QString effectName = message["name"].toString();
|
QString effectName = message["name"].toString();
|
||||||
std::list<EffectDefinition> effectsDefinition = _hyperion->getEffects();
|
std::list<EffectDefinition> effectsDefinition = _hyperion->getEffects();
|
||||||
@ -353,8 +318,6 @@ void JsonProcessor::handleDeleteEffectCommand(const QJsonObject& message, const
|
|||||||
sendErrorReply("Can't delete internal effect: " + message["name"].toString(), command, tan);
|
sendErrorReply("Can't delete internal effect: " + message["name"].toString(), command, tan);
|
||||||
} else
|
} else
|
||||||
sendErrorReply("Effect " + message["name"].toString() + " not found", command, tan);
|
sendErrorReply("Effect " + message["name"].toString() + " not found", command, tan);
|
||||||
} else
|
|
||||||
sendErrorReply("Error while parsing json: Message size " + QString(message.size()), command, tan);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void JsonProcessor::handleSysInfoCommand(const QJsonObject&, const QString& command, const int tan)
|
void JsonProcessor::handleSysInfoCommand(const QJsonObject&, const QString& command, const int tan)
|
||||||
@ -889,14 +852,7 @@ void JsonProcessor::handleConfigGetCommand(const QJsonObject& message, const QSt
|
|||||||
result["command"] = command;
|
result["command"] = command;
|
||||||
result["tan"] = tan;
|
result["tan"] = tan;
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
result["result"] = QJsonFactory::readConfig(_hyperion->getConfigFileName());
|
|
||||||
}
|
|
||||||
catch(...)
|
|
||||||
{
|
|
||||||
result["result"] = _hyperion->getQJsonConfig();
|
result["result"] = _hyperion->getQJsonConfig();
|
||||||
}
|
|
||||||
|
|
||||||
// send the result
|
// send the result
|
||||||
emit callbackMessage(result);
|
emit callbackMessage(result);
|
||||||
@ -1146,62 +1102,6 @@ void JsonProcessor::sendErrorReply(const QString &error, const QString &command,
|
|||||||
emit callbackMessage(reply);
|
emit callbackMessage(reply);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool JsonProcessor::checkJson(const QJsonObject& message, const QString& schemaResource, QString& errorMessage, bool ignoreRequired)
|
|
||||||
{
|
|
||||||
// make sure the resources are loaded (they may be left out after static linking)
|
|
||||||
Q_INIT_RESOURCE(JSONRPC_schemas);
|
|
||||||
QJsonParseError error;
|
|
||||||
|
|
||||||
// read the json schema from the resource
|
|
||||||
QFile schemaData(schemaResource);
|
|
||||||
if (!schemaData.open(QIODevice::ReadOnly))
|
|
||||||
{
|
|
||||||
errorMessage = "Schema error: " + schemaData.errorString();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// create schema checker
|
|
||||||
QByteArray schema = schemaData.readAll();
|
|
||||||
QJsonDocument schemaJson = QJsonDocument::fromJson(schema, &error);
|
|
||||||
schemaData.close();
|
|
||||||
|
|
||||||
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,schema.size()); i<count; ++i )
|
|
||||||
{
|
|
||||||
++errorColumn;
|
|
||||||
if(schema.at(i) == '\n' )
|
|
||||||
{
|
|
||||||
errorColumn = 0;
|
|
||||||
++errorLine;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
errorMessage = "Schema error: " + error.errorString() + " at Line: " + QString::number(errorLine) + ", Column: " + QString::number(errorColumn);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
QJsonSchemaChecker schemaChecker;
|
|
||||||
schemaChecker.setSchema(schemaJson.object());
|
|
||||||
|
|
||||||
// check the message
|
|
||||||
if (!schemaChecker.validate(message, ignoreRequired).first)
|
|
||||||
{
|
|
||||||
const QStringList & errors = schemaChecker.getMessages();
|
|
||||||
errorMessage = "{";
|
|
||||||
foreach (auto & error, errors)
|
|
||||||
{
|
|
||||||
errorMessage += error + " ";
|
|
||||||
}
|
|
||||||
errorMessage += "}";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void JsonProcessor::streamLedcolorsUpdate()
|
void JsonProcessor::streamLedcolorsUpdate()
|
||||||
{
|
{
|
||||||
|
129
libsrc/utils/JsonUtils.cpp
Normal file
129
libsrc/utils/JsonUtils.cpp
Normal file
@ -0,0 +1,129 @@
|
|||||||
|
//project include
|
||||||
|
#include <utils/JsonUtils.h>
|
||||||
|
|
||||||
|
// util includes
|
||||||
|
#include <utils/jsonschema/QJsonSchemaChecker.h>
|
||||||
|
|
||||||
|
//qt includes
|
||||||
|
#include <QRegularExpression>
|
||||||
|
#include <QJsonObject>
|
||||||
|
#include <QJsonParseError>
|
||||||
|
|
||||||
|
#include <QDebug>
|
||||||
|
|
||||||
|
namespace JsonUtils {
|
||||||
|
|
||||||
|
bool readFile(const QString& path, QJsonObject& obj, Logger* log, bool ignError)
|
||||||
|
{
|
||||||
|
QString data;
|
||||||
|
if(!FileUtils::readFile(path, data, log, ignError))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if(!parse(path, data, obj, log))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool readSchema(const QString& path, QJsonObject& obj, Logger* log)
|
||||||
|
{
|
||||||
|
QJsonObject schema;
|
||||||
|
if(!readFile(path, schema, log))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if(!resolveRefs(schema, obj, log))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool parse(const QString& path, const QString& data, QJsonObject& obj, Logger* log)
|
||||||
|
{
|
||||||
|
//remove Comments in data
|
||||||
|
QString cleanData = data;
|
||||||
|
cleanData.remove(QRegularExpression("([^:]?\\/\\/.*)"));
|
||||||
|
|
||||||
|
QJsonParseError error;
|
||||||
|
QJsonDocument doc = QJsonDocument::fromJson(cleanData.toUtf8(), &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,cleanData.size()); i<count; ++i )
|
||||||
|
{
|
||||||
|
++errorColumn;
|
||||||
|
if(data.at(i) == '\n' )
|
||||||
|
{
|
||||||
|
errorColumn = 0;
|
||||||
|
++errorLine;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Error(log,"Failed to parse json data from %s: Error: %s at Line: %i, Column: %i", QSTRING_CSTR(path), QSTRING_CSTR(error.errorString()), errorLine, errorColumn);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
obj = doc.object();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool validate(const QString& file, const QJsonObject& json, const QString& schemaPath, Logger* log)
|
||||||
|
{
|
||||||
|
// get the schema data
|
||||||
|
QJsonObject schema;
|
||||||
|
if(!readFile(schemaPath, schema, log))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
QJsonSchemaChecker schemaChecker;
|
||||||
|
schemaChecker.setSchema(schema);
|
||||||
|
if (!schemaChecker.validate(json).first)
|
||||||
|
{
|
||||||
|
const QStringList & errors = schemaChecker.getMessages();
|
||||||
|
for (auto & error : errors)
|
||||||
|
{
|
||||||
|
Error(log, "While validating schema against json data of '%s':%s", QSTRING_CSTR(file), QSTRING_CSTR(error));
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool write(const QString& filename, const QJsonObject& json, Logger* log)
|
||||||
|
{
|
||||||
|
QJsonDocument doc;
|
||||||
|
|
||||||
|
doc.setObject(json);
|
||||||
|
QByteArray data = doc.toJson(QJsonDocument::Indented);
|
||||||
|
|
||||||
|
if(!FileUtils::writeFile(filename, data, log))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool resolveRefs(const QJsonObject& schema, QJsonObject& obj, Logger* log)
|
||||||
|
{
|
||||||
|
for (QJsonObject::const_iterator i = schema.begin(); i != schema.end(); ++i)
|
||||||
|
{
|
||||||
|
QString attribute = i.key();
|
||||||
|
const QJsonValue & attributeValue = *i;
|
||||||
|
|
||||||
|
if (attribute == "$ref" && attributeValue.isString())
|
||||||
|
{
|
||||||
|
if(!readSchema(":/" + attributeValue.toString(), obj, log))
|
||||||
|
{
|
||||||
|
Error(log,"Error while getting schema ref: %s",QSTRING_CSTR(QString(":/" + attributeValue.toString())));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (attributeValue.isObject())
|
||||||
|
obj.insert(attribute, resolveRefs(attributeValue.toObject(), obj, log));
|
||||||
|
else
|
||||||
|
{
|
||||||
|
qDebug() <<"ADD ATTR:VALUE"<<attribute<<attributeValue;
|
||||||
|
obj.insert(attribute, attributeValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
@ -111,7 +111,7 @@ Logger::Logger ( QString name, LogLevel minLevel )
|
|||||||
|
|
||||||
Logger::~Logger()
|
Logger::~Logger()
|
||||||
{
|
{
|
||||||
Debug(this, "logger '%s' destroyed", QSTRING_CSTR(_name) );
|
//Debug(this, "logger '%s' destroyed", QSTRING_CSTR(_name) );
|
||||||
loggerCount--;
|
loggerCount--;
|
||||||
if ( loggerCount == 0 )
|
if ( loggerCount == 0 )
|
||||||
closelog();
|
closelog();
|
||||||
|
@ -120,7 +120,7 @@ void Stats::resolveReply(QNetworkReply *reply)
|
|||||||
|
|
||||||
bool Stats::trigger(bool set)
|
bool Stats::trigger(bool set)
|
||||||
{
|
{
|
||||||
QString path = QDir::homePath()+"/.hyperion/misc/";
|
QString path = _hyperion->getRootPath()+"/misc/";
|
||||||
QDir dir;
|
QDir dir;
|
||||||
QFile file(path + _hyperion->id);
|
QFile file(path + _hyperion->id);
|
||||||
|
|
||||||
|
@ -197,6 +197,7 @@ void QJsonSchemaChecker::checkType(const QJsonValue & value, const QJsonValue &
|
|||||||
if (_correct == "modify")
|
if (_correct == "modify")
|
||||||
QJsonUtils::modify(_autoCorrected, _currentPath, defaultValue);
|
QJsonUtils::modify(_autoCorrected, _currentPath, defaultValue);
|
||||||
|
|
||||||
|
|
||||||
if (_correct == "")
|
if (_correct == "")
|
||||||
setMessage(type + " expected");
|
setMessage(type + " expected");
|
||||||
}
|
}
|
||||||
@ -222,7 +223,10 @@ void QJsonSchemaChecker::checkProperties(const QJsonObject & value, const QJsonO
|
|||||||
_error = true;
|
_error = true;
|
||||||
|
|
||||||
if (_correct == "create")
|
if (_correct == "create")
|
||||||
|
{
|
||||||
QJsonUtils::modify(_autoCorrected, _currentPath, QJsonUtils::create(propertyValue, _ignoreRequired), property);
|
QJsonUtils::modify(_autoCorrected, _currentPath, QJsonUtils::create(propertyValue, _ignoreRequired), property);
|
||||||
|
setMessage("Create property: "+property+" with value: "+propertyValue.toObject().find("default").value().toString());
|
||||||
|
}
|
||||||
|
|
||||||
if (_correct == "")
|
if (_correct == "")
|
||||||
setMessage("missing member");
|
setMessage("missing member");
|
||||||
@ -250,7 +254,10 @@ void QJsonSchemaChecker::checkAdditionalProperties(const QJsonObject & value, co
|
|||||||
_error = true;
|
_error = true;
|
||||||
|
|
||||||
if (_correct == "remove")
|
if (_correct == "remove")
|
||||||
|
{
|
||||||
QJsonUtils::modify(_autoCorrected, _currentPath);
|
QJsonUtils::modify(_autoCorrected, _currentPath);
|
||||||
|
setMessage("Removed property: "+property);
|
||||||
|
}
|
||||||
|
|
||||||
if (_correct == "")
|
if (_correct == "")
|
||||||
setMessage("no schema definition");
|
setMessage("no schema definition");
|
||||||
@ -280,9 +287,12 @@ void QJsonSchemaChecker::checkMinimum(const QJsonValue & value, const QJsonValue
|
|||||||
_error = true;
|
_error = true;
|
||||||
|
|
||||||
if (_correct == "modify")
|
if (_correct == "modify")
|
||||||
|
{
|
||||||
(defaultValue != QJsonValue::Null) ?
|
(defaultValue != QJsonValue::Null) ?
|
||||||
QJsonUtils::modify(_autoCorrected, _currentPath, defaultValue) :
|
QJsonUtils::modify(_autoCorrected, _currentPath, defaultValue) :
|
||||||
QJsonUtils::modify(_autoCorrected, _currentPath, schema);
|
QJsonUtils::modify(_autoCorrected, _currentPath, schema);
|
||||||
|
setMessage("Correct too small value: "+QString::number(value.toDouble())+" to: "+QString::number(defaultValue.toDouble()));
|
||||||
|
}
|
||||||
|
|
||||||
if (_correct == "")
|
if (_correct == "")
|
||||||
setMessage("value is too small (minimum=" + QString::number(schema.toDouble()) + ")");
|
setMessage("value is too small (minimum=" + QString::number(schema.toDouble()) + ")");
|
||||||
@ -304,9 +314,12 @@ void QJsonSchemaChecker::checkMaximum(const QJsonValue & value, const QJsonValue
|
|||||||
_error = true;
|
_error = true;
|
||||||
|
|
||||||
if (_correct == "modify")
|
if (_correct == "modify")
|
||||||
|
{
|
||||||
(defaultValue != QJsonValue::Null) ?
|
(defaultValue != QJsonValue::Null) ?
|
||||||
QJsonUtils::modify(_autoCorrected, _currentPath, defaultValue) :
|
QJsonUtils::modify(_autoCorrected, _currentPath, defaultValue) :
|
||||||
QJsonUtils::modify(_autoCorrected, _currentPath, schema);
|
QJsonUtils::modify(_autoCorrected, _currentPath, schema);
|
||||||
|
setMessage("Correct too large value: "+QString::number(value.toDouble())+" to: "+QString::number(defaultValue.toDouble()));
|
||||||
|
}
|
||||||
|
|
||||||
if (_correct == "")
|
if (_correct == "")
|
||||||
setMessage("value is too large (maximum=" + QString::number(schema.toDouble()) + ")");
|
setMessage("value is too large (maximum=" + QString::number(schema.toDouble()) + ")");
|
||||||
@ -328,10 +341,12 @@ void QJsonSchemaChecker::checkMinLength(const QJsonValue & value, const QJsonVal
|
|||||||
_error = true;
|
_error = true;
|
||||||
|
|
||||||
if (_correct == "modify")
|
if (_correct == "modify")
|
||||||
|
{
|
||||||
(defaultValue != QJsonValue::Null) ?
|
(defaultValue != QJsonValue::Null) ?
|
||||||
QJsonUtils::modify(_autoCorrected, _currentPath, defaultValue) :
|
QJsonUtils::modify(_autoCorrected, _currentPath, defaultValue) :
|
||||||
QJsonUtils::modify(_autoCorrected, _currentPath, schema);
|
QJsonUtils::modify(_autoCorrected, _currentPath, schema);
|
||||||
|
setMessage("Correct too short value: "+value.toString()+" to: "+defaultValue.toString());
|
||||||
|
}
|
||||||
if (_correct == "")
|
if (_correct == "")
|
||||||
setMessage("value is too short (minLength=" + QString::number(schema.toInt()) + ")");
|
setMessage("value is too short (minLength=" + QString::number(schema.toInt()) + ")");
|
||||||
}
|
}
|
||||||
@ -352,10 +367,12 @@ void QJsonSchemaChecker::checkMaxLength(const QJsonValue & value, const QJsonVal
|
|||||||
_error = true;
|
_error = true;
|
||||||
|
|
||||||
if (_correct == "modify")
|
if (_correct == "modify")
|
||||||
|
{
|
||||||
(defaultValue != QJsonValue::Null) ?
|
(defaultValue != QJsonValue::Null) ?
|
||||||
QJsonUtils::modify(_autoCorrected, _currentPath, defaultValue) :
|
QJsonUtils::modify(_autoCorrected, _currentPath, defaultValue) :
|
||||||
QJsonUtils::modify(_autoCorrected, _currentPath, schema);
|
QJsonUtils::modify(_autoCorrected, _currentPath, schema);
|
||||||
|
setMessage("Correct too long value: "+value.toString()+" to: "+defaultValue.toString());
|
||||||
|
}
|
||||||
if (_correct == "")
|
if (_correct == "")
|
||||||
setMessage("value is too long (maxLength=" + QString::number(schema.toInt()) + ")");
|
setMessage("value is too long (maxLength=" + QString::number(schema.toInt()) + ")");
|
||||||
}
|
}
|
||||||
@ -375,7 +392,10 @@ void QJsonSchemaChecker::checkItems(const QJsonValue & value, const QJsonObject
|
|||||||
|
|
||||||
if (_correct == "remove")
|
if (_correct == "remove")
|
||||||
if (jArray.isEmpty())
|
if (jArray.isEmpty())
|
||||||
|
{
|
||||||
QJsonUtils::modify(_autoCorrected, _currentPath);
|
QJsonUtils::modify(_autoCorrected, _currentPath);
|
||||||
|
setMessage("Remove empty array");
|
||||||
|
}
|
||||||
|
|
||||||
for(int i = 0; i < jArray.size(); ++i)
|
for(int i = 0; i < jArray.size(); ++i)
|
||||||
{
|
{
|
||||||
@ -402,9 +422,12 @@ void QJsonSchemaChecker::checkMinItems(const QJsonValue & value, const QJsonValu
|
|||||||
_error = true;
|
_error = true;
|
||||||
|
|
||||||
if (_correct == "modify")
|
if (_correct == "modify")
|
||||||
|
{
|
||||||
(defaultValue != QJsonValue::Null) ?
|
(defaultValue != QJsonValue::Null) ?
|
||||||
QJsonUtils::modify(_autoCorrected, _currentPath, defaultValue) :
|
QJsonUtils::modify(_autoCorrected, _currentPath, defaultValue) :
|
||||||
QJsonUtils::modify(_autoCorrected, _currentPath, schema);
|
QJsonUtils::modify(_autoCorrected, _currentPath, schema);
|
||||||
|
setMessage("Correct minItems: "+QString::number(jArray.size())+" to: "+QString::number(defaultValue.toArray().size()));
|
||||||
|
}
|
||||||
|
|
||||||
if (_correct == "")
|
if (_correct == "")
|
||||||
setMessage("array is too small (minimum=" + QString::number(schema.toInt()) + ")");
|
setMessage("array is too small (minimum=" + QString::number(schema.toInt()) + ")");
|
||||||
@ -427,9 +450,12 @@ void QJsonSchemaChecker::checkMaxItems(const QJsonValue & value, const QJsonValu
|
|||||||
_error = true;
|
_error = true;
|
||||||
|
|
||||||
if (_correct == "modify")
|
if (_correct == "modify")
|
||||||
|
{
|
||||||
(defaultValue != QJsonValue::Null) ?
|
(defaultValue != QJsonValue::Null) ?
|
||||||
QJsonUtils::modify(_autoCorrected, _currentPath, defaultValue) :
|
QJsonUtils::modify(_autoCorrected, _currentPath, defaultValue) :
|
||||||
QJsonUtils::modify(_autoCorrected, _currentPath, schema);
|
QJsonUtils::modify(_autoCorrected, _currentPath, schema);
|
||||||
|
setMessage("Correct maxItems: "+QString::number(jArray.size())+" to: "+QString::number(defaultValue.toArray().size()));
|
||||||
|
}
|
||||||
|
|
||||||
if (_correct == "")
|
if (_correct == "")
|
||||||
setMessage("array is too large (maximum=" + QString::number(schema.toInt()) + ")");
|
setMessage("array is too large (maximum=" + QString::number(schema.toInt()) + ")");
|
||||||
@ -501,9 +527,12 @@ void QJsonSchemaChecker::checkEnum(const QJsonValue & value, const QJsonValue &
|
|||||||
_error = true;
|
_error = true;
|
||||||
|
|
||||||
if (_correct == "modify")
|
if (_correct == "modify")
|
||||||
|
{
|
||||||
(defaultValue != QJsonValue::Null) ?
|
(defaultValue != QJsonValue::Null) ?
|
||||||
QJsonUtils::modify(_autoCorrected, _currentPath, defaultValue) :
|
QJsonUtils::modify(_autoCorrected, _currentPath, defaultValue) :
|
||||||
QJsonUtils::modify(_autoCorrected, _currentPath, schema.toArray().first());
|
QJsonUtils::modify(_autoCorrected, _currentPath, schema.toArray().first());
|
||||||
|
setMessage("Correct unknown enum value: "+value.toString()+" to: "+defaultValue.toString());
|
||||||
|
}
|
||||||
|
|
||||||
if (_correct == "")
|
if (_correct == "")
|
||||||
{
|
{
|
||||||
|
@ -62,7 +62,7 @@ void StaticFileServing::onServerStarted (quint16 port)
|
|||||||
Debug(_log, "Web Config mDNS responder started");
|
Debug(_log, "Web Config mDNS responder started");
|
||||||
|
|
||||||
// json-rpc for http
|
// json-rpc for http
|
||||||
_jsonProcessor = new JsonProcessor(QString("HTTP-API"),true);
|
_jsonProcessor = new JsonProcessor(QString("HTTP-API"), _log, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void StaticFileServing::onServerStopped () {
|
void StaticFileServing::onServerStopped () {
|
||||||
@ -200,4 +200,3 @@ void StaticFileServing::onRequestNeedsReply (QtHttpRequest * request, QtHttpRepl
|
|||||||
printErrorToReply (reply, QtHttpReply::MethodNotAllowed,"Unhandled HTTP/1.1 method " % command);
|
printErrorToReply (reply, QtHttpReply::MethodNotAllowed,"Unhandled HTTP/1.1 method " % command);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,6 +24,7 @@ add_executable(${PROJECT_NAME}
|
|||||||
target_link_libraries(${PROJECT_NAME}
|
target_link_libraries(${PROJECT_NAME}
|
||||||
effectengine
|
effectengine
|
||||||
commandline
|
commandline
|
||||||
|
hyperion-utils
|
||||||
Qt5::Gui
|
Qt5::Gui
|
||||||
Qt5::Core
|
Qt5::Core
|
||||||
Qt5::Network)
|
Qt5::Network)
|
||||||
|
@ -14,8 +14,12 @@
|
|||||||
// hyperion-remote includes
|
// hyperion-remote includes
|
||||||
#include "JsonConnection.h"
|
#include "JsonConnection.h"
|
||||||
|
|
||||||
|
// util includes
|
||||||
|
#include <utils/JsonUtils.h>
|
||||||
|
|
||||||
JsonConnection::JsonConnection(const QString & address, bool printJson)
|
JsonConnection::JsonConnection(const QString & address, bool printJson)
|
||||||
: _printJson(printJson)
|
: _printJson(printJson)
|
||||||
|
, _log(Logger::getInstance("REMOTE"))
|
||||||
, _socket()
|
, _socket()
|
||||||
{
|
{
|
||||||
QStringList parts = address.split(":");
|
QStringList parts = address.split(":");
|
||||||
@ -128,30 +132,13 @@ void JsonConnection::setEffect(const QString &effectName, const QString & effect
|
|||||||
|
|
||||||
if (effectArgs.size() > 0)
|
if (effectArgs.size() > 0)
|
||||||
{
|
{
|
||||||
QJsonParseError error;
|
QJsonObject effObj;
|
||||||
QJsonDocument doc = QJsonDocument::fromJson(effectArgs.toUtf8() ,&error);
|
if(!JsonUtils::parse("hyperion-remote-args", effectArgs, effObj, _log))
|
||||||
|
|
||||||
if (error.error != QJsonParseError::NoError)
|
|
||||||
{
|
{
|
||||||
// report to the user the failure and their locations in the document.
|
throw std::runtime_error("Error in effect arguments, abort");
|
||||||
int errorLine(0), errorColumn(0);
|
|
||||||
|
|
||||||
for( int i=0, count=qMin( error.offset,effectArgs.size()); i<count; ++i )
|
|
||||||
{
|
|
||||||
++errorColumn;
|
|
||||||
if(effectArgs.at(i) == '\n' )
|
|
||||||
{
|
|
||||||
errorColumn = 0;
|
|
||||||
++errorLine;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::stringstream sstream;
|
effect["args"] = effObj;
|
||||||
sstream << "Error in effect arguments: " << error.errorString().toStdString() << " at Line: " << errorLine << ", Column: " << errorColumn;
|
|
||||||
throw std::runtime_error(sstream.str());
|
|
||||||
}
|
|
||||||
|
|
||||||
effect["args"] = doc.object();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
command["effect"] = effect;
|
command["effect"] = effect;
|
||||||
@ -180,30 +167,13 @@ void JsonConnection::createEffect(const QString &effectName, const QString &effe
|
|||||||
|
|
||||||
if (effectArgs.size() > 0)
|
if (effectArgs.size() > 0)
|
||||||
{
|
{
|
||||||
QJsonParseError error;
|
QJsonObject effObj;
|
||||||
QJsonDocument doc = QJsonDocument::fromJson(effectArgs.toUtf8() ,&error);
|
if(!JsonUtils::parse("hyperion-remote-args", effectScript, effObj, _log))
|
||||||
|
|
||||||
if (error.error != QJsonParseError::NoError)
|
|
||||||
{
|
{
|
||||||
// report to the user the failure and their locations in the document.
|
throw std::runtime_error("Error in effect arguments, abort");
|
||||||
int errorLine(0), errorColumn(0);
|
|
||||||
|
|
||||||
for( int i=0, count=qMin( error.offset,effectArgs.size()); i<count; ++i )
|
|
||||||
{
|
|
||||||
++errorColumn;
|
|
||||||
if(effectArgs.at(i) == '\n' )
|
|
||||||
{
|
|
||||||
errorColumn = 0;
|
|
||||||
++errorLine;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::stringstream sstream;
|
effect["args"] = effObj;
|
||||||
sstream << "Error in effect arguments: " << error.errorString().toStdString() << " at Line: " << errorLine << ", Column: " << errorColumn;
|
|
||||||
throw std::runtime_error(sstream.str());
|
|
||||||
}
|
|
||||||
|
|
||||||
effect["args"] = doc.object();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// send command message
|
// send command message
|
||||||
@ -399,31 +369,17 @@ void JsonConnection::setConfig(const QString &jsonString)
|
|||||||
|
|
||||||
if (jsonString.size() > 0)
|
if (jsonString.size() > 0)
|
||||||
{
|
{
|
||||||
QJsonParseError error;
|
QJsonObject configObj;
|
||||||
QJsonDocument doc = QJsonDocument::fromJson(jsonString.toUtf8() ,&error);
|
if(!JsonUtils::parse("hyperion-remote-args", jsonString, configObj, _log))
|
||||||
|
|
||||||
if (error.error != QJsonParseError::NoError)
|
|
||||||
{
|
{
|
||||||
// report to the user the failure and their locations in the document.
|
throw std::runtime_error("Error in configset arguments, abort");
|
||||||
int errorLine(0), errorColumn(0);
|
|
||||||
|
|
||||||
for( int i=0, count=qMin( error.offset,jsonString.size()); i<count; ++i )
|
|
||||||
{
|
|
||||||
++errorColumn;
|
|
||||||
if(jsonString.at(i) == '\n' )
|
|
||||||
{
|
|
||||||
errorColumn = 0;
|
|
||||||
++errorLine;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::stringstream sstream;
|
command["config"] = configObj;
|
||||||
sstream << "Error in configset arguments: " << error.errorString().toStdString() << " at Line: " << errorLine << ", Column: " << errorColumn;
|
|
||||||
throw std::runtime_error(sstream.str());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
command["config"] = doc.object();
|
|
||||||
}
|
|
||||||
|
|
||||||
// send command message
|
// send command message
|
||||||
QJsonObject reply = sendMessage(command);
|
QJsonObject reply = sendMessage(command);
|
||||||
|
@ -6,6 +6,9 @@
|
|||||||
#include <QTcpSocket>
|
#include <QTcpSocket>
|
||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
|
|
||||||
|
//forward class decl
|
||||||
|
class Logger;
|
||||||
|
|
||||||
///
|
///
|
||||||
/// Connection class to setup an connection to the hyperion server and execute commands
|
/// Connection class to setup an connection to the hyperion server and execute commands
|
||||||
///
|
///
|
||||||
@ -188,10 +191,13 @@ private:
|
|||||||
///
|
///
|
||||||
bool parseReply(const QJsonObject & reply);
|
bool parseReply(const QJsonObject & reply);
|
||||||
|
|
||||||
private:
|
|
||||||
/// Flag for printing all send and received json-messages to the standard out
|
/// Flag for printing all send and received json-messages to the standard out
|
||||||
bool _printJson;
|
bool _printJson;
|
||||||
|
|
||||||
|
// Logger class
|
||||||
|
Logger* _log;
|
||||||
|
|
||||||
/// The TCP-Socket with the connection to the server
|
/// The TCP-Socket with the connection to the server
|
||||||
QTcpSocket _socket;
|
QTcpSocket _socket;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
|
|
||||||
#include <utils/jsonschema/QJsonFactory.h>
|
#include <utils/jsonschema/QJsonFactory.h>
|
||||||
#include <utils/Components.h>
|
#include <utils/Components.h>
|
||||||
|
#include <utils/JsonUtils.h>
|
||||||
|
|
||||||
#include <hyperion/Hyperion.h>
|
#include <hyperion/Hyperion.h>
|
||||||
#include <hyperion/PriorityMuxer.h>
|
#include <hyperion/PriorityMuxer.h>
|
||||||
@ -33,7 +34,9 @@
|
|||||||
|
|
||||||
#include "hyperiond.h"
|
#include "hyperiond.h"
|
||||||
|
|
||||||
HyperionDaemon::HyperionDaemon(QString configFile, QObject *parent)
|
#include <QDebug>
|
||||||
|
|
||||||
|
HyperionDaemon::HyperionDaemon(QString configFile, const QString rootPath, QObject *parent)
|
||||||
: QObject(parent)
|
: QObject(parent)
|
||||||
, _log(Logger::getInstance("MAIN"))
|
, _log(Logger::getInstance("MAIN"))
|
||||||
, _kodiVideoChecker(nullptr)
|
, _kodiVideoChecker(nullptr)
|
||||||
@ -73,7 +76,7 @@ HyperionDaemon::HyperionDaemon(QString configFile, QObject *parent)
|
|||||||
WarningIf(_qconfig.contains("logger"), Logger::getInstance("LOGGER"), "Logger settings overridden by command line argument");
|
WarningIf(_qconfig.contains("logger"), Logger::getInstance("LOGGER"), "Logger settings overridden by command line argument");
|
||||||
}
|
}
|
||||||
|
|
||||||
_hyperion = Hyperion::initInstance(_qconfig, configFile);
|
_hyperion = Hyperion::initInstance(_qconfig, configFile, rootPath);
|
||||||
|
|
||||||
Info(_log, "Hyperion initialized");
|
Info(_log, "Hyperion initialized");
|
||||||
}
|
}
|
||||||
@ -145,11 +148,12 @@ void HyperionDaemon::loadConfig(const QString & configFile)
|
|||||||
Q_INIT_RESOURCE(resource);
|
Q_INIT_RESOURCE(resource);
|
||||||
|
|
||||||
// read the json schema from the resource
|
// read the json schema from the resource
|
||||||
|
|
||||||
QString schemaFile = ":/hyperion-schema";
|
QString schemaFile = ":/hyperion-schema";
|
||||||
QJsonObject schemaJson;
|
QJsonObject schemaJson;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
//QJsonObject obj;
|
||||||
|
//JsonUtils::readSchema(schemaFile, obj, _log);
|
||||||
schemaJson = QJsonFactory::readSchema(schemaFile);
|
schemaJson = QJsonFactory::readSchema(schemaFile);
|
||||||
}
|
}
|
||||||
catch(const std::runtime_error& error)
|
catch(const std::runtime_error& error)
|
||||||
@ -160,25 +164,31 @@ void HyperionDaemon::loadConfig(const QString & configFile)
|
|||||||
QJsonSchemaChecker schemaChecker;
|
QJsonSchemaChecker schemaChecker;
|
||||||
schemaChecker.setSchema(schemaJson);
|
schemaChecker.setSchema(schemaJson);
|
||||||
|
|
||||||
_qconfig = QJsonFactory::readConfig(configFile);
|
if(!JsonUtils::readFile(configFile, _qconfig, _log))
|
||||||
|
throw std::runtime_error("Failed to load config!");
|
||||||
|
|
||||||
|
// validate config with schema and correct it if required
|
||||||
QPair<bool, bool> validate = schemaChecker.validate(_qconfig);
|
QPair<bool, bool> validate = schemaChecker.validate(_qconfig);
|
||||||
|
|
||||||
if (!validate.first && validate.second)
|
// errors in schema syntax, abort
|
||||||
|
if (!validate.second)
|
||||||
{
|
{
|
||||||
Warning(_log,"Errors have been found in the configuration file. Automatic correction is applied");
|
foreach (auto & schemaError, schemaChecker.getMessages())
|
||||||
|
Error(_log, "Schema Syntax Error: %s", QSTRING_CSTR(schemaError));
|
||||||
|
|
||||||
|
throw std::runtime_error("ERROR: Hyperion schema has errors!");
|
||||||
|
}
|
||||||
|
// errors in configuration, correct it!
|
||||||
|
if (!validate.first)
|
||||||
|
{
|
||||||
|
Warning(_log,"Errors have been found in the configuration file. Automatic correction has been applied");
|
||||||
_qconfig = schemaChecker.getAutoCorrectedConfig(_qconfig);
|
_qconfig = schemaChecker.getAutoCorrectedConfig(_qconfig);
|
||||||
|
|
||||||
if (!QJsonFactory::writeJson(configFile, _qconfig))
|
foreach (auto & schemaError, schemaChecker.getMessages())
|
||||||
throw std::runtime_error("ERROR: can not save configuration file, aborting ");
|
Warning(_log, "Config Fix: %s", QSTRING_CSTR(schemaError));
|
||||||
}
|
|
||||||
else if (validate.first && !validate.second) //Error in Schema
|
|
||||||
{
|
|
||||||
QStringList schemaErrors = schemaChecker.getMessages();
|
|
||||||
foreach (auto & schemaError, schemaErrors)
|
|
||||||
std::cout << schemaError.toStdString() << std::endl;
|
|
||||||
|
|
||||||
throw std::runtime_error("ERROR: Json validation failed");
|
if (!JsonUtils::write(configFile, _qconfig, _log))
|
||||||
|
throw std::runtime_error("ERROR: Can't save configuration file, aborting");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,7 +58,7 @@ class HyperionDaemon : public QObject
|
|||||||
friend SysTray;
|
friend SysTray;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
HyperionDaemon(QString configFile, QObject *parent=nullptr);
|
HyperionDaemon(QString configFile, QString rootPath, QObject *parent=nullptr);
|
||||||
~HyperionDaemon();
|
~HyperionDaemon();
|
||||||
|
|
||||||
void loadConfig(const QString & configFile);
|
void loadConfig(const QString & configFile);
|
||||||
|
@ -140,6 +140,7 @@ int main(int argc, char** argv)
|
|||||||
parser.addHelpOption();
|
parser.addHelpOption();
|
||||||
|
|
||||||
BooleanOption & versionOption = parser.add<BooleanOption>(0x0, "version", "Show version information");
|
BooleanOption & versionOption = parser.add<BooleanOption>(0x0, "version", "Show version information");
|
||||||
|
Option & rootPathOption = parser.add<Option> (0x0, "rootPath", "Overwrite root path for all hyperion user files, defaults to home directory of current user");
|
||||||
IntOption & parentOption = parser.add<IntOption> ('p', "parent", "pid of parent hyperiond"); // 2^22 is the max for Linux
|
IntOption & parentOption = parser.add<IntOption> ('p', "parent", "pid of parent hyperiond"); // 2^22 is the max for Linux
|
||||||
BooleanOption & silentOption = parser.add<BooleanOption>('s', "silent", "do not print any outputs");
|
BooleanOption & silentOption = parser.add<BooleanOption>('s', "silent", "do not print any outputs");
|
||||||
BooleanOption & verboseOption = parser.add<BooleanOption>('v', "verbose", "Increase verbosity");
|
BooleanOption & verboseOption = parser.add<BooleanOption>('v', "verbose", "Increase verbosity");
|
||||||
@ -227,21 +228,48 @@ int main(int argc, char** argv)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle default config file
|
// handle rootPath for user data, default path is home directory + /.hyperion
|
||||||
if (configFiles.size() == 0)
|
QString rootPath = QDir::homePath() + "/.hyperion";
|
||||||
|
if (parser.isSet(rootPathOption))
|
||||||
{
|
{
|
||||||
QString hyperiond_path = QDir::homePath();
|
QDir rDir(rootPathOption.value(parser));
|
||||||
QString hyperiond_config = hyperiond_path+"/.hyperion.config.json";
|
if(!rDir.mkpath(rootPathOption.value(parser)))
|
||||||
QFileInfo hyperiond_pathinfo(hyperiond_path);
|
|
||||||
|
|
||||||
if ( ! hyperiond_pathinfo.isWritable() && ! QFile::exists(hyperiond_config) )
|
|
||||||
{
|
{
|
||||||
QFileInfo hyperiond_fileinfo(argv[0]);
|
Error(log, "Can't create root path '%s', falling back to home directory", QSTRING_CSTR(rDir.absolutePath()));
|
||||||
hyperiond_config = hyperiond_fileinfo.absolutePath()+"/hyperion.config.json";
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
rootPath = rDir.absolutePath();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// create /.hyperion folder for default path, check if the directory is read/writeable
|
||||||
|
// Note: No further checks inside Hyperion. FileUtils::writeFile() will resolve permission errors and others that occur during runtime
|
||||||
|
QDir mDir(rootPath);
|
||||||
|
QFileInfo mFi(rootPath);
|
||||||
|
if(!mDir.mkpath(rootPath) || !mFi.isWritable() || !mDir.isReadable())
|
||||||
|
{
|
||||||
|
throw std::runtime_error("The specified root path can't be created or isn't read/writeable. Please setup the permissions correctly!");
|
||||||
}
|
}
|
||||||
|
|
||||||
configFiles.append(hyperiond_config);
|
// determine name of config file, defaults to hyperion_main.json
|
||||||
Info(log, "No config file given. Standard config file used: %s", QSTRING_CSTR(configFiles[0]));
|
// create config folder
|
||||||
|
QString cPath(rootPath+"/config");
|
||||||
|
QDir().mkpath(rootPath+"/config");
|
||||||
|
if (configFiles.size() > 0)
|
||||||
|
{
|
||||||
|
// use argument config file
|
||||||
|
// check if file has a path and ends with .json
|
||||||
|
if(configFiles[0].contains("/"))
|
||||||
|
throw std::runtime_error("Don't provide a path to config file, just a config name is allowed!");
|
||||||
|
if(!configFiles[0].endsWith(".json"))
|
||||||
|
configFiles[0].append(".json");
|
||||||
|
|
||||||
|
configFiles.prepend(cPath+"/"+configFiles[0]);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// use default config file
|
||||||
|
configFiles.append(cPath+"/hyperion_main.json");
|
||||||
}
|
}
|
||||||
|
|
||||||
bool exportDefaultConfig = false;
|
bool exportDefaultConfig = false;
|
||||||
@ -257,7 +285,7 @@ int main(int argc, char** argv)
|
|||||||
{
|
{
|
||||||
exportDefaultConfig = true;
|
exportDefaultConfig = true;
|
||||||
exportConfigFileTarget = configFiles[0];
|
exportConfigFileTarget = configFiles[0];
|
||||||
Warning(log, "Your configuration file does not exist. hyperion writes default config");
|
Warning(log, "Create new config file (%s)",QSTRING_CSTR(configFiles[0]));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (exportDefaultConfig)
|
if (exportDefaultConfig)
|
||||||
@ -277,11 +305,6 @@ int main(int argc, char** argv)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (configFiles.size() > 1)
|
|
||||||
{
|
|
||||||
Warning(log, "You provided more than one config file. Hyperion will use only the first one");
|
|
||||||
}
|
|
||||||
|
|
||||||
int parentPid = parser.value(parentOption).toInt();
|
int parentPid = parser.value(parentOption).toInt();
|
||||||
if (parentPid > 0 )
|
if (parentPid > 0 )
|
||||||
{
|
{
|
||||||
@ -295,7 +318,7 @@ int main(int argc, char** argv)
|
|||||||
HyperionDaemon* hyperiond = nullptr;
|
HyperionDaemon* hyperiond = nullptr;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
hyperiond = new HyperionDaemon(configFiles[0], qApp);
|
hyperiond = new HyperionDaemon(configFiles[0], rootPath, qApp);
|
||||||
hyperiond->run();
|
hyperiond->run();
|
||||||
}
|
}
|
||||||
catch (std::exception& e)
|
catch (std::exception& e)
|
||||||
|
Loading…
Reference in New Issue
Block a user