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 
120 #if 0 < HAVE_SCRIPTS
121 
122  QFile file(scriptFileName);
123 
124  if (!file.exists())
125  {
126 #ifdef SWDATA
127  str = SWDATA;
128 #endif
129  if (str.size())
130  {
131  file.setFileName(str + "/scripts/" + scriptFileName);
132  if (!file.exists())
133  {
134  std::cout << "Error: cannot not find the file " << qPrintable(scriptFileName)
135  << " neither in the current directory nor in the system one, "
136  << qPrintable(str + "/scripts/") << "\n";
137  return 1;
138  };
139  }
140  else
141  {
142  std::cout << "Error: cannot not find the file " << qPrintable(scriptFileName) << "\n";
143  return 1;
144  };
145  };
146 
147  if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
148  {
149  std::cout << "Error: cannot not open the file " << qPrintable(scriptFileName) << "\n";
150  return 2;
151  };
152 
153  std::cout << "executing " << qPrintable(file.fileName()) << " script\n";
154 
155  QString strProgram(file.readAll());
156  file.close();
157 
158  QScriptEngine interpreter;
159 
160 
161  // do static check so far of code:
162  if (QScriptSyntaxCheckResult::Valid != interpreter.checkSyntax(strProgram).state())
163  {
164  std::cout << "Error: bad syntax of the file " << qPrintable(scriptFileName) << "\n";
165  return 3;
166  };
167 
168  // actually do the eval:
169 //std::cout << "program: \n" << qPrintable(strProgram) << "\n";
170 
171 //- QPushButton button;
172 //- QScriptValue scriptButton = interpreter.newQObject(&button);
173 //- interpreter.globalObject().setProperty("button", scriptButton);
174 
175  /*
176  NsVlbiSessionProxy session;
177  QScriptValue scriptSession = scriptEngine.newQObject(&session);
178  scriptEngine.globalObject().setProperty("session", scriptSession);
179  */
180 
181 
182  qScriptRegisterMetaType(&interpreter, toScriptValue4ParIdx, fromScriptValue4ParIdx);
183  qScriptRegisterMetaType(&interpreter, toScriptValue4ParMode, fromScriptValue4ParMode);
184  qScriptRegisterMetaType(&interpreter, toScriptValue4ConfigDtype, fromScriptValue4ConfigDtype);
185  qScriptRegisterMetaType(&interpreter, toScriptValue4ConfigRtype, fromScriptValue4ConfigRtype);
186  qScriptRegisterMetaType(&interpreter, toScriptValue4ConfigWrMode, fromScriptValue4ConfigWrMode);
187  qScriptRegisterMetaType(&interpreter, toScriptValue4ConfigOpMode, fromScriptValue4ConfigOpMode);
188  qScriptRegisterMetaType(&interpreter, toScriptValue4ConfigOpAction, fromScriptValue4ConfigOpAction);
189  qScriptRegisterMetaType(&interpreter, toScriptValue4ConfigEpwlMode, fromScriptValue4ConfigEpwlMode);
190  qScriptRegisterMetaType(&interpreter, toScriptValue4ConfigTropZenithMap, fromScriptValue4ConfigTropZenithMap);
191 
192 
193  qScriptRegisterMetaType(&interpreter, toScriptValue4Band, fromScriptValue4Band);
194  qScriptRegisterMetaType(&interpreter, toScriptValue4Stn, fromScriptValue4Stn);
195  qScriptRegisterMetaType(&interpreter, toScriptValue4Bln, fromScriptValue4Bln);
196  qScriptRegisterMetaType(&interpreter, toScriptValue4Src, fromScriptValue4Src);
197  qScriptRegisterMetaType(&interpreter, toScriptValue4VlbiObs, fromScriptValue4VlbiObs);
198  qScriptRegisterMetaType(&interpreter, toScriptValue4VlbiAuxObs, fromScriptValue4VlbiAuxObs);
199  qScriptRegisterMetaType(&interpreter, toScriptValue4LogLevel, fromScriptValue4LogLevel);
200  qScriptRegisterMetaType(&interpreter, toScriptValue4LogFacility, fromScriptValue4LogFacility);
201 
202  qScriptRegisterSequenceMetaType< QList<NsScrPrx4ParametersDescriptor::ParIdx> >(&interpreter);
203  qScriptRegisterSequenceMetaType< QList<NsScrPrx4Band*> >(&interpreter);
204  qScriptRegisterSequenceMetaType< QList<NsScrPrx4Station*> >(&interpreter);
205  qScriptRegisterSequenceMetaType< QList<NsScrPrx4Baseline*> >(&interpreter);
206  qScriptRegisterSequenceMetaType< QList<NsScrPrx4Source*> >(&interpreter);
207  qScriptRegisterSequenceMetaType< QList<NsScrPrx4VlbiObservation*> >(&interpreter);
208  qScriptRegisterSequenceMetaType< QList<NsScrPrx4VlbiAuxObservation*> >(&interpreter);
209 
210 
211  // args:
212  QScriptValue args4Script=qScriptValueFromSequence(&interpreter, args);
213  interpreter.globalObject().setProperty("args", args4Script);
214 
215 
216  // handler:
217  NsSessionHandler *handler;
218  handler = new NsSessionHandler(&config, &parametersDescriptor);
219  NsScrPrx4SessionHandler handlerProxy(*handler);
220  QScriptValue handlerScript=interpreter.newQObject(&handlerProxy);
221  interpreter.globalObject().setProperty("handler", handlerScript);
222 
223  // config:
224  NsScrPrx4TaskConfig config4Scr(*handler->cfg());
225  QScriptValue scriptConfig=interpreter.newQObject(&config4Scr);
226  interpreter.globalObject().setProperty("config", scriptConfig);
227 
228  QScriptValue metaScrConfig=interpreter.newQMetaObject(config4Scr.metaObject());
229  interpreter.globalObject().setProperty("CFG", metaScrConfig);
230 
231  // setup:
232  NsScrPrx4Setup setup4Scr(setup);
233  QScriptValue scriptSetup=interpreter.newQObject(&setup4Scr);
234  interpreter.globalObject().setProperty("setup", scriptSetup);
235 
236  // parameters descriptor:
237  NsScrPrx4ParametersDescriptor parsDescript4Scr(*handler->parametersDescriptor());
238  QScriptValue scriptParsDescript=interpreter.newQObject(&parsDescript4Scr);
239  interpreter.globalObject().setProperty("parsDescript", scriptParsDescript);
240 
241  QScriptValue metaScrParsDescript=
242  interpreter.newQMetaObject(parsDescript4Scr.metaObject());
243  interpreter.globalObject().setProperty("Parameters", metaScrParsDescript);
244 
245  // session:
246  NsScrPrx4Session session4Scr(*handler->session());
247  QScriptValue scriptSession=interpreter.newQObject(&session4Scr);
248  interpreter.globalObject().setProperty("session", scriptSession);
249 
250  QScriptValue metaScrSession=
251  interpreter.newQMetaObject(session4Scr.metaObject());
252  interpreter.globalObject().setProperty("Session", metaScrSession);
253  handlerProxy.setSessionProxy(&session4Scr);
254 
255  // logger:
256  NsScrPrx4Logger logger4Scr(logger);
257  QScriptValue scriptLogger=interpreter.newQObject(&logger4Scr);
258  interpreter.globalObject().setProperty("logger", scriptLogger);
259 
260  QScriptValue metaScrLogger=interpreter.newQMetaObject(logger4Scr.metaObject());
261  interpreter.globalObject().setProperty("Log", metaScrLogger);
262 
263 
264  QObject::connect(&parsDescript4Scr, SIGNAL(clocksParModeChanged(SgParameterCfg::PMode)),
265  &session4Scr, SLOT(dispatchChangeOfClocksParameterModel(SgParameterCfg::PMode)));
266  QObject::connect(&parsDescript4Scr, SIGNAL(zenithParModeChanged(SgParameterCfg::PMode)),
267  &session4Scr, SLOT(dispatchChangeOfZenithParameterModel(SgParameterCfg::PMode)));
268 
269 
270  interpreter.globalObject().setProperty("nsPrint", interpreter.newFunction(myPrintFunction));
271 
272 
273  //
274  //
275  //
276  QScriptValue result=interpreter.evaluate(strProgram, scriptFileName);
277 
278  if (result.isError())
279  {
280  QString strError(QString::fromLatin1("%0:%1: %2")
281  .arg(scriptFileName)
282  .arg(result.property("lineNumber").toInt32())
283  .arg(result.toString()));
284  if (hasDisplay)
285  {
286  QMessageBox::critical(0, "Error running script", strError);
287  }
288  else
289  {
290  std::cerr << "ERROR: " << qPrintable(strError) << "\n";
291  };
292  return -1;
293  };
294 
295 //app.exec();
296 
297  delete handler;
298 
299 
300 
301 #else
302  rc = -1;
303  std::cout << "script mode is not supported\n";
304 
305 #endif
306 
307 //#endif
308 
309 
310  return rc;
311 };
312 /*=====================================================================================================*/
313 
314 
315 
316 
317 
318 
319 
320 
321 
322 
323 
324 
325 
326 /*=====================================================================================================*/
327 
QScriptValue myPrintFunction(QScriptContext *context, QScriptEngine *engine)
int executeScript(const QString &scriptFileName, const QList< QString > args, bool hasDisplay)
SgLogger * logger
Definition: SgLogger.cpp:231
void setSessionProxy(NsScrPrx4Session *sProxy)
const SgTaskConfig * cfg() const
const SgParametersDescriptor * parametersDescriptor() const
const SgVlbiSession * session() const
NsSetup setup
Definition: nuSolve.cpp:61
SgTaskConfig config
Definition: nuSolve.cpp:59
SgParametersDescriptor parametersDescriptor
Definition: nuSolve.cpp:60