General Purpose Geodetic Library
NsScrSupport.cpp
Go to the documentation of this file.
1 /*
2  *
3  * This file is a part of nuSolve. nuSolve is a part of CALC/SOLVE system
4  * and is designed to perform data analyis of a geodetic VLBI session.
5  * Copyright (C) 2010-2020 Sergei Bolotin.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 #include "nuSolve.h"
22 #include "NsScrSupport.h"
23 
24 
25 #include <iostream>
26 
27 
28 
29 #include <QtCore/QFile>
30 
31 
32 #if QT_VERSION >= 0x050000
33 # include <QtWidgets/QApplication>
34 # include <QtWidgets/QMessageBox>
35 # include <QtWidgets/QPushButton>
36 #else
37 # include <QtGui/QApplication>
38 # include <QtGui/QMessageBox>
39 # include <QtGui/QPushButton>
40 #endif
41 
42 #include <SgVlbiSession.h>
43 
44 #if 0 < HAVE_SCRIPTS
45 
46 #include "NsScrPrx4Logger.h"
47 #include "NsScrPrx4Observation.h"
49 #include "NsScrPrx4Session.h"
51 #include "NsScrPrx4Setup.h"
52 #include "NsScrPrx4TaskConfig.h"
53 
54 #endif
55 
56 
57 /*=======================================================================================================
58 *
59 * METHODS:
60 *
61 *======================================================================================================*/
62 
63 
64 
65 
66 
67 /*=======================================================================================================
68 *
69 * FRIENDS:
70 *
71 *======================================================================================================*/
72 //
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 
85 
86 
87 /*=====================================================================================================*/
88 //
89 // constants:
90 //
91 
92 
93 /*=====================================================================================================*/
94 QScriptValue myPrintFunction(QScriptContext *context, QScriptEngine *engine)
95 {
96  QString result;
97  for (int i = 0; i < context->argumentCount(); ++i)
98  {
99  if (i > 0)
100  result.append(" ");
101  result.append(context->argument(i).toString());
102  };
103  std::cout << qPrintable(result) << std::endl;
104  return engine->undefinedValue();
105 };
106 
107 
108 
109 
110 
111 
112 //
113 int executeScript(const QString& scriptFileName, const QList<QString> args, bool hasDisplay)
114 {
115  QString str("");
116  int rc;
117  rc = 0;
118 
119 #if 0 < HAVE_SCRIPTS
120 
121  QFile file(scriptFileName);
122 
123  if (!file.exists())
124  {
125 #ifdef SWDATA
126  str = SWDATA;
127 #endif
128  if (str.size())
129  {
130  file.setFileName(str + "/scripts/" + scriptFileName);
131  if (!file.exists())
132  {
133  std::cout << "Error: cannot not find the file " << qPrintable(scriptFileName)
134  << " neither in the current directory nor in the system one, "
135  << qPrintable(str + "/scripts/") << "\n";
136  return 1;
137  };
138  }
139  else
140  {
141  std::cout << "Error: cannot not find the file " << qPrintable(scriptFileName) << "\n";
142  return 1;
143  };
144  };
145 
146  if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
147  {
148  std::cout << "Error: cannot not open the file " << qPrintable(scriptFileName) << "\n";
149  return 2;
150  };
151 
152 
154  std::cout << "executing " << qPrintable(file.fileName()) << " script\n";
155 
156 
157  QString strProgram(file.readAll());
158  file.close();
159 
160  QScriptEngine interpreter;
161 
162 
163  // do static check so far of code:
164  if (QScriptSyntaxCheckResult::Valid != interpreter.checkSyntax(strProgram).state())
165  {
166  std::cout << "Error: bad syntax of the file " << qPrintable(scriptFileName) << "\n";
167  return 3;
168  };
169 
170  // actually do the eval:
171 //std::cout << "program: \n" << qPrintable(strProgram) << "\n";
172 
173 //- QPushButton button;
174 //- QScriptValue scriptButton = interpreter.newQObject(&button);
175 //- interpreter.globalObject().setProperty("button", scriptButton);
176 
177  /*
178  NsVlbiSessionProxy session;
179  QScriptValue scriptSession = scriptEngine.newQObject(&session);
180  scriptEngine.globalObject().setProperty("session", scriptSession);
181  */
182 
183 
184  qScriptRegisterMetaType(&interpreter, toScriptValue4ParIdx, fromScriptValue4ParIdx);
185  qScriptRegisterMetaType(&interpreter, toScriptValue4ParMode, fromScriptValue4ParMode);
186  qScriptRegisterMetaType(&interpreter, toScriptValue4ConfigDtype, fromScriptValue4ConfigDtype);
187  qScriptRegisterMetaType(&interpreter, toScriptValue4ConfigRtype, fromScriptValue4ConfigRtype);
188  qScriptRegisterMetaType(&interpreter, toScriptValue4ConfigWrMode, fromScriptValue4ConfigWrMode);
189  qScriptRegisterMetaType(&interpreter, toScriptValue4ConfigOpMode, fromScriptValue4ConfigOpMode);
190  qScriptRegisterMetaType(&interpreter, toScriptValue4ConfigOpAction, fromScriptValue4ConfigOpAction);
191  qScriptRegisterMetaType(&interpreter, toScriptValue4ConfigEpwlMode, fromScriptValue4ConfigEpwlMode);
192  qScriptRegisterMetaType(&interpreter, toScriptValue4ConfigTropZenithMap,
193  fromScriptValue4ConfigTropZenithMap);
194  qScriptRegisterMetaType(&interpreter, toScriptValue4ConfigCableCalSource,
195  fromScriptValue4ConfigCableCalSource);
196 
197  qScriptRegisterMetaType(&interpreter, toScriptValue4Band, fromScriptValue4Band);
198  qScriptRegisterMetaType(&interpreter, toScriptValue4Stn, fromScriptValue4Stn);
199  qScriptRegisterMetaType(&interpreter, toScriptValue4Bln, fromScriptValue4Bln);
200  qScriptRegisterMetaType(&interpreter, toScriptValue4Src, fromScriptValue4Src);
201  qScriptRegisterMetaType(&interpreter, toScriptValue4VlbiObs, fromScriptValue4VlbiObs);
202  qScriptRegisterMetaType(&interpreter, toScriptValue4VlbiAuxObs, fromScriptValue4VlbiAuxObs);
203  qScriptRegisterMetaType(&interpreter, toScriptValue4LogLevel, fromScriptValue4LogLevel);
204  qScriptRegisterMetaType(&interpreter, toScriptValue4LogFacility, fromScriptValue4LogFacility);
205 
206  qScriptRegisterSequenceMetaType< QList<NsScrPrx4ParametersDescriptor::ParIdx> >(&interpreter);
207  qScriptRegisterSequenceMetaType< QList<NsScrPrx4Band*> >(&interpreter);
208  qScriptRegisterSequenceMetaType< QList<NsScrPrx4Station*> >(&interpreter);
209  qScriptRegisterSequenceMetaType< QList<NsScrPrx4Baseline*> >(&interpreter);
210  qScriptRegisterSequenceMetaType< QList<NsScrPrx4Source*> >(&interpreter);
211  qScriptRegisterSequenceMetaType< QList<NsScrPrx4VlbiObservation*> >(&interpreter);
212  qScriptRegisterSequenceMetaType< QList<NsScrPrx4VlbiAuxObservation*> >(&interpreter);
213 
214 
215  // args:
216  QScriptValue args4Script=qScriptValueFromSequence(&interpreter, args);
217  interpreter.globalObject().setProperty("args", args4Script);
218 
219 
220  // handler:
221  NsSessionHandler *handler;
222  handler = new NsSessionHandler(&config, &parametersDescriptor);
223  NsScrPrx4SessionHandler handlerProxy(*handler);
224  QScriptValue handlerScript=interpreter.newQObject(&handlerProxy);
225  interpreter.globalObject().setProperty("handler", handlerScript);
226 
227  // config:
228  NsScrPrx4TaskConfig config4Scr(*handler->cfg());
229  QScriptValue scriptConfig=interpreter.newQObject(&config4Scr);
230  interpreter.globalObject().setProperty("config", scriptConfig);
231 
232  QScriptValue metaScrConfig=interpreter.newQMetaObject(config4Scr.metaObject());
233  interpreter.globalObject().setProperty("CFG", metaScrConfig);
234 
235  // setup:
236  NsScrPrx4Setup setup4Scr(setup);
237  QScriptValue scriptSetup=interpreter.newQObject(&setup4Scr);
238  interpreter.globalObject().setProperty("setup", scriptSetup);
239 
240  // parameters descriptor:
241  NsScrPrx4ParametersDescriptor parsDescript4Scr(*handler->parametersDescriptor());
242  QScriptValue scriptParsDescript=interpreter.newQObject(&parsDescript4Scr);
243  interpreter.globalObject().setProperty("parsDescript", scriptParsDescript);
244 
245  QScriptValue metaScrParsDescript=
246  interpreter.newQMetaObject(parsDescript4Scr.metaObject());
247  interpreter.globalObject().setProperty("Parameters", metaScrParsDescript);
248 
249  // session:
250  NsScrPrx4Session session4Scr(*handler->session());
251  QScriptValue scriptSession=interpreter.newQObject(&session4Scr);
252  interpreter.globalObject().setProperty("session", scriptSession);
253 
254  QScriptValue metaScrSession=
255  interpreter.newQMetaObject(session4Scr.metaObject());
256  interpreter.globalObject().setProperty("Session", metaScrSession);
257  handlerProxy.setSessionProxy(&session4Scr);
258 
259  // logger:
260  NsScrPrx4Logger logger4Scr(logger);
261  QScriptValue scriptLogger=interpreter.newQObject(&logger4Scr);
262  interpreter.globalObject().setProperty("logger", scriptLogger);
263 
264  QScriptValue metaScrLogger=interpreter.newQMetaObject(logger4Scr.metaObject());
265  interpreter.globalObject().setProperty("Log", metaScrLogger);
266 
267 
268  QObject::connect(&parsDescript4Scr, SIGNAL(clocksParModeChanged(SgParameterCfg::PMode)),
269  &session4Scr, SLOT(dispatchChangeOfClocksParameterModel(SgParameterCfg::PMode)));
270  QObject::connect(&parsDescript4Scr, SIGNAL(zenithParModeChanged(SgParameterCfg::PMode)),
271  &session4Scr, SLOT(dispatchChangeOfZenithParameterModel(SgParameterCfg::PMode)));
272 
273 
274  interpreter.globalObject().setProperty("nsPrint", interpreter.newFunction(myPrintFunction));
275 
276 
277  //
278  //
279  //
280  QScriptValue result=interpreter.evaluate(strProgram, scriptFileName);
281  rc = logger4Scr.getReturnCode(); // a script can provide its return code through logger's proxy
282 
283  if (result.isError())
284  {
285  QString strError(QString::fromLatin1("%0:%1: %2")
286  .arg(scriptFileName)
287  .arg(result.property("lineNumber").toInt32())
288  .arg(result.toString()));
289  if (hasDisplay)
290  {
291  QMessageBox::critical(0, "Error running script", strError);
292  }
293  else
294  {
295  std::cerr << "ERROR: " << qPrintable(strError) << "\n";
296  };
297  return -1;
298  };
299  //
300  delete handler;
301  //
302 #else
303  rc = -1;
304  std::cout << "script mode is not supported\n";
305 #endif
306  //
307  return rc;
308 };
309 /*=====================================================================================================*/
310 
311 
312 
313 
314 
315 
316 
317 
318 
319 
320 
321 
322 
323 /*=====================================================================================================*/
324 
QScriptValue myPrintFunction(QScriptContext *context, QScriptEngine *engine)
int executeScript(const QString &scriptFileName, const QList< QString > args, bool hasDisplay)
SgLogger * logger
Definition: SgLogger.cpp:231
int getReturnCode() const
void setSessionProxy(NsScrPrx4Session *sProxy)
const SgTaskConfig * cfg() const
const SgParametersDescriptor * parametersDescriptor() const
const SgVlbiSession * session() const
@ IO_TXT
Definition: SgLogger.h:65
bool isEligible(LogLevel lvl, quint32 f) const
Definition: SgLogger.h:151
NsSetup setup
Definition: nuSolve.cpp:61
SgTaskConfig config
Definition: nuSolve.cpp:59
SgParametersDescriptor parametersDescriptor
Definition: nuSolve.cpp:60