40 QObject(parent), m_initReady(false), m_threadCompleted(false)
42 zDebug() <<
"ZScriptThread::ZScriptThread";
44 m_thread =
new QThread(
this);
48 m_worker->moveToThread(m_thread);
53 connect(m_thread, &QThread::started, m_worker,
56 connect(m_thread, &QThread::started,
this,
59 connect(m_thread, &QThread::finished,
this,
66 qDebug(
"ZScriptThread::~ZScriptThread");
73 qDebug(
"ZScriptThread::destroyThread");
109 zWarning() <<
"WARNING - programming error: initScript " 110 "can't be set after initizlization";
134 zWarning() <<
"Programming error; Thread already destroyed.";
147 static int snipLength = 150;
151 zDebug() <<
"Loading init script: " << snipText;
159 zDebug() <<
"No init script";
175 zDebug() <<
"execScript ID=" << requestID <<
", script = " << script.left(150) <<
", userData = " << userData;
179 zWarning() <<
"Programming error: Thread already destroyed.";
183 QVariant data(userData);
186 QMetaObject::invokeMethod(
m_worker,
"execScript",
187 Qt::QueuedConnection,
188 Q_ARG(qulonglong, requestID),
189 Q_ARG(
const QString&, script),
190 Q_ARG(QString, jsonUserData)
219 zDebug() <<
"ZScriptThread ALERT id=" << signalID <<
", payload=" << payload;
227 var = QVariant(payload.toInt());
233 var = QVariant(payload.toDouble());
239 var = QVariant(payload);
251 zWarning() <<
"ZScriptThread::onThreadAlert received UNDEFINED conversion value: " << conversion;
257 zWarning() <<
"ZScriptThread::onThreadAlert received unrecognized conversion value: " << conversion;
263 emit
alert(signalID, var);
276 zDebug() <<
"ZScriptThread::readyChanged";
308 return m_thread->isInterruptionRequested();
324 unsigned long waitTime = 25;
325 unsigned long count = time > waitTime ? time / waitTime : 1;
326 QAbstractEventDispatcher* d = QThread::currentThread()->eventDispatcher();
328 for(
unsigned long i=0; i < count; i++)
336 d->processEvents(QEventLoop::AllEvents);
340 zWarning() <<
"THREAD WAIT TIMED OUT";
349 qmlRegisterType<ZScriptThread>(
"org.zuble.qml", 1, 0,
"ZScriptThread");
354 QJsonDocument doc = QJsonDocument::fromVariant(var);
355 QByteArray jsonText = doc.toJson(QJsonDocument::Compact);
356 return QString(jsonText);
365 QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8().constData());
366 retVal = doc.toVariant();
void execComplete(qulonglong requestID, QVariant result, QVariant userData)
Sent when an asynchronous background script execution has completed.
void finished()
Sent from the associated thread right before it finishes executing.
QString ptrToString(void *ptr)
QVariant m_initError
The error message generated when running the initScript, or an empty string if initScript was success...
static void registerType()
Registers ZScriptThread as a QML type.
void setInitScript(const QString &script)
void onWorkerExecComplete(qulonglong requestID, QString result, QString userData)
Receives and propagates the ZScriptWorker::execComplete signal.
void execScript(qulonglong requestID, const QString &script, QVariantMap userData=QVariantMap())
Execute the specified javascript program fragment asynchronously in the background thread's javascrip...
void onFinished()
Called when QThread event loop has exited.
#define ZBL_REGISTER_LOGGED_OBJECT
Q_INVOKABLE bool isInterrupted() const
Calls the QThread::isInterruptionRequested() method for this thread. Can be used to determine if this...
A thread class to support Zuble's background Javascript processing.
A class for executing javascript programs in a background worker thread.
void quit()
Causes the thread to stop processing events and exit the event loop.
static QString variantToJson(QVariant &var)
Converts QVariant to json string.
Q_INVOKABLE void onThreadAlert(QString signalID, QString payload, int conversion)
Called by ZblApp in background thread to trigger ZScriptThread::alert signal in foreground thread...
ZScriptWorker * m_worker
Script worker object maintains this thread's javascript context.
#define ZBL_DEFINE_LOGGED_OBJECT(class_name)
void execComplete(qulonglong requestID, QString result, QString userData)
Sent when the a javascript program execution started with execScript() has been completed.
void onStarted()
Called when QThread has started running.
void onStarted()
Called when QThread has started running.
void readyChanged()
Sent when ready property value changes.
static QVariant jsonToVariant(const QString &json)
Converts json string to QVariant.
void sendInterruptRequest()
Calls the QThread::requestInterruption() method for this thread. If the thread is checking Zbl...
bool m_initReady
true if thread initialization is complete, false otherwise
void completeChanged()
Sent when complete property value changes.
void destroyThread()
Stops the QThread, waits for it to exit and then deletes it and the ZScriptWorker object...
void alert(const QString signalID, QVariant payload)
Background scripts send this signal by calling the ZblApp::alert() method.
bool waitThreadComplete(unsigned long time=ULONG_MAX)
Blocks the current thread until the background thread finishes running its event loop or a timeout oc...
ZBL_DECLARE_LOGGED_OBJECT QThread * m_thread
QThread object in which ZScriptWorker object will run.
QString m_initScript
Thread's javascript context will be initialized by running this script.
void initComplete(QVariant status)
Sent when the javascript execution environment has completed initialization and is ready for use...
bool m_threadCompleted
true if thread has been shut down, false otherwise
void start()
Causes the tread to start execution.