trikRuntime
pythonEngineWorker.h
Go to the documentation of this file.
1 /* Copyright 2018 Iakov Kirilenko, 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 <QtCore/QString>
18 #include <QtCore/QThread>
19 #include <QMutex>
20 
23 
24 #include "PythonQt_QtAll.h"
25 #include "PyTrikControl0.h"
26 
27 void PythonQt_init_PyTrikControl(PyObject* module);
28 
29 namespace trikScriptRunner
30 {
31 
33 class PythonEngineWorker : public QObject
34 {
35  Q_OBJECT
36 
37 public:
43  );
44 
46 
49  void resetBrick();
50 
54  void stopScript();
55 
57  QStringList knownNames() const;
58 
59 signals:
63  void completed(const QString &error, int scriptId);
64 
67  void startedScript(const QString &fileName, int scriptId);
68 
71  void startedDirectScript(int scriptId);
72 
73 
75  void inited();
76 
78  void sendMessage(const QString&);
79 
80 public slots:
87  void run(const QString &script);
88 
94  void runDirect(const QString &command);
95 
99  void init();
100 
102  bool recreateContext();
103 
106  void brickBeep();
107 
109  void sendStdOutMessage(const QString &text);
110 
111 private slots:
113  void onScriptRequestingToQuit();
114 
116  bool initTrik();
117 
119  void doRun(const QString &script);
120 
122  void doRunDirect(const QString &command);
123 
125  void updateErrorMessage(const QString &err);
126 
127 private:
133  enum State {
134  ready
135  , starting
136  , stopping
137  , running
138  };
139 
141  bool evalSystemPy();
142 
144  void startScriptEvaluation(int scriptId);
145 
147  trikNetwork::MailboxInterface * const mMailbox; // Does not have ownership.
148 
149  State mState = State::ready;
150 
153  QMutex mScriptStateMutex;
154 
155  PythonQtObjectPtr mMainContext;
156 
157  PyThreadState * mPyInterpreter { nullptr };
158 
159  QString mErrorMessage;
160 
161  wchar_t *mProgramName { nullptr };
162  wchar_t *mPythonPath { nullptr };
163 
164  static QAtomicInt initCounter;
165 };
166 
167 }
void sendMessage(const QString &)
Some message to send, for example, from stdout.
bool recreateContext()
Recreates Main Context made by init, returns true when were errors.
Definition: pythonEngineWorker.cpp:135
void resetBrick()
Clears execution state and stops robot.
Definition: pythonEngineWorker.cpp:171
~PythonEngineWorker()
Definition: pythonEngineWorker.cpp:54
Definition: trikCommunicator.h:23
void inited()
When engine was inited.
void startedDirectScript(int scriptId)
Emitted when new direct script is started.
void run(const QString &script)
Starts script evaluation, emits startedScript() signal and returns.
Definition: pythonEngineWorker.cpp:241
void PythonQt_init_PyTrikControl(PyObject *module)
void startedScript(const QString &fileName, int scriptId)
Emitted when new script is started.
void sendStdOutMessage(const QString &text)
Sends message to listeners.
Definition: pythonEngineWorker.cpp:188
void stopScript()
Stops script execution and resets script engine.
Definition: pythonEngineWorker.cpp:193
QStringList knownNames() const
Report known objects and methods for autocompletion.
Definition: pythonEngineWorker.cpp:228
void completed(const QString &error, int scriptId)
Emitted when current script execution is completed or is aborted by reset() call. ...
void runDirect(const QString &command)
Runs a command in a current context.
Definition: pythonEngineWorker.cpp:274
void init()
Initializes PythonQt and creates new main module, which will be used by user Must be invoked (called ...
Definition: pythonEngineWorker.cpp:80
Worker object to be run in a separate thread for Python execution.
Definition: pythonEngineWorker.h:33
Allows communication between robots.
Definition: mailboxInterface.h:31
void brickBeep()
Plays "beep" sound.
Definition: pythonEngineWorker.cpp:183
script
Definition: system.py:83
PythonEngineWorker(trikControl::BrickInterface &brick, trikNetwork::MailboxInterface *mailbox)
Constructor.
Definition: pythonEngineWorker.cpp:46
Interface representing TRIK controller board and devices installed on it, also provides access to per...
Definition: brickInterface.h:46