trikRuntime
|
Worker object to be run in a separate thread for Qt Script execution. More...
#include <scriptEngineWorker.h>
Public Slots | |
void | run (const QString &script, int scriptId) |
Starts script evaluation, emits startedScript() signal and returns. More... | |
void | runDirect (const QString &command, int scriptId) |
Runs a command in a current context. More... | |
void | brickBeep () |
Plays "beep" sound. More... | |
Signals | |
void | completed (const QString &error, int scriptId) |
Emitted when current script execution is completed or is aborted by reset() call. More... | |
void | startedScript (int scriptId) |
Emitted when new script is started. More... | |
void | getVariables (const QString &propertyName) |
Emitted when there is a request for variables values. More... | |
void | variablesReady (const QJsonObject &data) |
Emitted when variables values are ready. More... | |
Public Member Functions | |
ScriptEngineWorker (trikControl::BrickInterface &brick, trikNetwork::MailboxInterface *mailbox, ScriptExecutionControl &scriptControl) | |
Constructor. More... | |
QScriptEngine * | createScriptEngine (bool supportThreads=true) |
Create and initialize a new script engine. More... | |
QScriptEngine * | copyScriptEngine (const QScriptEngine *original) |
Copies given script engine creating a new one with the same context as existing one. More... | |
void | registerUserFunction (const QString &name, QScriptEngine::FunctionSignature function) |
Registers given C++ function as callable from script, with given name. More... | |
void | addCustomEngineInitStep (const std::function< void(QScriptEngine *)> &step) |
Helper for adding custom initialization steps when creating script engine from outside of the TrikRuntime. More... | |
void | resetBrick () |
Clears execution state and stops robot. More... | |
void | stopScript () |
Stops script execution and resets script engine. More... | |
QStringList | knownMethodNames () const |
Gets all method names from executive objects (brick, script, etc.) from ScriptEngineWorker (useful when used from outside of the TrikRuntime). More... | |
Worker object to be run in a separate thread for Qt Script execution.
QScriptEngine calls ProcessEvents too infrequently even when ProcessEventsInterval is set to 1 ms, so there is a need for separate threads to run a script and listen for incoming connections.
ScriptEngineWorker::ScriptEngineWorker | ( | trikControl::BrickInterface & | brick, |
trikNetwork::MailboxInterface * | mailbox, | ||
ScriptExecutionControl & | scriptControl | ||
) |
Constructor.
brick | - reference to trikControl::Brick instance. |
mailbox | - mailbox object used to communicate with other robots. |
scriptControl | - reference to script execution control object. |
void ScriptEngineWorker::addCustomEngineInitStep | ( | const std::function< void(QScriptEngine *)> & | step | ) |
Helper for adding custom initialization steps when creating script engine from outside of the TrikRuntime.
|
slot |
Plays "beep" sound.
Can be safely called from other threads.
|
signal |
Emitted when current script execution is completed or is aborted by reset() call.
error | - localized error message or empty string. |
scriptId | - unique identifier of a script completed |
QScriptEngine * ScriptEngineWorker::copyScriptEngine | ( | const QScriptEngine * | original | ) |
Copies given script engine creating a new one with the same context as existing one.
Note that functions will not be copied to a new engine due to limitations of Qt Scripting engine, they need to be re-evaluated manually. Can be safely called from other threads (to some extent. Original engine shall not simultaneously evaluate).
QScriptEngine * ScriptEngineWorker::createScriptEngine | ( | bool | supportThreads = true | ) |
Create and initialize a new script engine.
Can be safely called from other threads.
supportThreads | - true if created engine should support creation of threads. |
|
signal |
Emitted when there is a request for variables values.
propertyName | - name of variables prefix, i.e prefix "web" for variable "web.light" |
QStringList ScriptEngineWorker::knownMethodNames | ( | ) | const |
Gets all method names from executive objects (brick, script, etc.) from ScriptEngineWorker (useful when used from outside of the TrikRuntime).
void ScriptEngineWorker::registerUserFunction | ( | const QString & | name, |
QScriptEngine::FunctionSignature | function | ||
) |
Registers given C++ function as callable from script, with given name.
Can be safely called from other threads (but it shall not be called simultaneously with engine creation).
void ScriptEngineWorker::resetBrick | ( | ) |
Clears execution state and stops robot.
Can be safely called from other threads.
|
slot |
Starts script evaluation, emits startedScript() signal and returns.
Script will be executed asynchronously. completed() signal is emitted upon script abortion or completion. It is a caller's responsibility to ensure that ScriptEngineWorker is in ready state before a call to run() by calling reset() first.
script | - QtScript code to evaluate |
scriptId | - an id of a script, used to distinguish between different scripts run by a worker Can be safely called from other threads. |
|
slot |
Runs a command in a current
context.
Permits to run a script line by line. The command will be executed asynchronously. If called when an ordinary script is running, that script would be aborted before evaluation of a command. In this mode, threads and messages are unsupported. Can be safely called from other threads.
|
signal |
Emitted when new script is started.
scriptId | - unique identifier assigned to a newly started script. |
void ScriptEngineWorker::stopScript | ( | ) |
Stops script execution and resets script engine.
Can be called from another thread. By the end of call the worker would be in a ready state. Can be safely called from other threads.
|
signal |
Emitted when variables values are ready.
json | - JSON container for variables values |