trikRuntime
scriptEngineWorker.h
Go to the documentation of this file.
1 /* Copyright 2013 - 2015 Yurii Litvinov and CyberTech Labs Ltd.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License. */
14 
15 #pragma once
16 
17 #include <functional>
18 
19 #include <QtCore/QString>
20 #include <QtCore/QThread>
21 #include <QtScript/QScriptEngine>
22 
25 
26 #include "scriptExecutionControl.h"
27 #include "threading.h"
28 
29 namespace trikScriptRunner
30 {
31 
35 class ScriptEngineWorker : public QObject
36 {
37  Q_OBJECT
38 
39 public:
46  , ScriptExecutionControl &scriptControl
47  );
48 
52  QScriptEngine *createScriptEngine(bool supportThreads = true);
53 
58  QScriptEngine *copyScriptEngine(const QScriptEngine * original);
59 
62  void registerUserFunction(const QString &name, QScriptEngine::FunctionSignature function);
63 
65  void addCustomEngineInitStep(const std::function<void (QScriptEngine *)> &step);
66 
69  void resetBrick();
70 
74  void stopScript();
75 
76 
79  QStringList knownMethodNames() const;
80 
81 signals:
85  void completed(const QString &error, int scriptId);
86 
89  void startedScript(int scriptId);
90 
93  void getVariables(const QString &propertyName);
94 
97  void variablesReady(const QJsonObject &data);
98 
99 public slots:
107  void run(const QString &script, int scriptId);
108 
114  void runDirect(const QString &command, int scriptId);
115 
118  void brickBeep();
119 
120 private slots:
122  void onScriptRequestingToQuit();
123 
125  void doRun(const QString &script);
126 
128  void doRunDirect(const QString &command, int scriptId);
129 
130 private:
136  enum State {
137  ready
138  , starting
139  , stopping
140  , running
141  };
142 
144  void startScriptEvaluation(int scriptId);
145 
147  void evalSystemJs(QScriptEngine * engine) const;
148 
150  trikNetwork::MailboxInterface * const mMailbox; // Does not have ownership.
151  ScriptExecutionControl &mScriptControl;
152  Threading mThreading;
153  QScriptEngine *mDirectScriptsEngine = nullptr; // Has ownership.
154  int mScriptId = 0;
155  State mState = State::ready;
156  QHash<QString, QScriptEngine::FunctionSignature> mRegisteredUserFunctions;
157  QVector<std::function<void (QScriptEngine *)>> mCustomInitSteps;
158 
161  QMutex mScriptStateMutex;
162 };
163 
164 }
Definition: trikCommunicator.h:23
void brickBeep()
Plays "beep" sound.
Definition: scriptEngineWorker.cpp:183
Designed to support OS threads from a Qt Script.
Definition: threading.h:34
void getVariables(const QString &propertyName)
Emitted when there is a request for variables values.
void resetBrick()
Clears execution state and stops robot.
Definition: scriptEngineWorker.cpp:242
QScriptEngine * copyScriptEngine(const QScriptEngine *original)
Copies given script engine creating a new one with the same context as existing one.
Definition: scriptEngineWorker.cpp:382
void addCustomEngineInitStep(const std::function< void(QScriptEngine *)> &step)
Helper for adding custom initialization steps when creating script engine from outside of the TrikRun...
Definition: scriptEngineWorker.cpp:402
void completed(const QString &error, int scriptId)
Emitted when current script execution is completed or is aborted by reset() call. ...
Worker object to be run in a separate thread for Qt Script execution.
Definition: scriptEngineWorker.h:35
void runDirect(const QString &command, int scriptId)
Runs a command in a current context.
Definition: scriptEngineWorker.cpp:274
void variablesReady(const QJsonObject &data)
Emitted when variables values are ready.
void startedScript(int scriptId)
Emitted when new script is started.
ScriptEngineWorker(trikControl::BrickInterface &brick, trikNetwork::MailboxInterface *mailbox, ScriptExecutionControl &scriptControl)
Constructor.
Definition: scriptEngineWorker.cpp:162
Script execution controller, provides related functions to scripts.
Definition: scriptExecutionControl.h:24
void stopScript()
Stops script execution and resets script engine.
Definition: scriptEngineWorker.cpp:188
QStringList knownMethodNames() const
Gets all method names from executive objects (brick, script, etc.) from ScriptEngineWorker (useful wh...
Definition: scriptEngineWorker.cpp:427
void run(const QString &script, int scriptId)
Starts script evaluation, emits startedScript() signal and returns.
Definition: scriptEngineWorker.cpp:254
Allows communication between robots.
Definition: mailboxInterface.h:31
QScriptEngine * createScriptEngine(bool supportThreads=true)
Create and initialize a new script engine.
Definition: scriptEngineWorker.cpp:341
script
Definition: system.py:83
void registerUserFunction(const QString &name, QScriptEngine::FunctionSignature function)
Registers given C++ function as callable from script, with given name.
Definition: scriptEngineWorker.cpp:397
Interface representing TRIK controller board and devices installed on it, also provides access to per...
Definition: brickInterface.h:46