qavrg 0.0.28
|
00001 #include "qavrgscriptingengine.h" 00002 00003 #include "qavrgapplication.h" 00004 #include "qavrgwindow.h" 00005 #include "qavrgacquisition.h" 00006 #include "qavrgfitter.h" 00007 #include <QThread> 00008 #include <QMutexLocker> 00009 #include <stdio.h> 00010 00011 static QavrgScriptingEngine *g_ScriptingEngine; 00012 static QavrgAcquisition *g_Acquisition; 00013 static QavrgApplication *g_Application; 00014 00015 QavrgScriptingEngine::QavrgScriptingEngine(QavrgApplication *app, QavrgWindow *win, QavrgAcquisition *acq) 00016 : QObject(), 00017 m_Mutex(QMutex::Recursive), 00018 m_ScriptEngine(NULL), 00019 m_Application(app), 00020 m_Window(win), 00021 m_Acquisition(acq) 00022 { 00023 g_ScriptingEngine = this; 00024 g_Acquisition = acq; 00025 g_Application = app; 00026 } 00027 00028 void QavrgScriptingEngine::initialize() 00029 { 00030 // printf("enter QavrgScriptingEngine::initialize\n"); 00031 00032 m_ScriptEngine = new QScriptEngine(this); 00033 00034 qScriptRegisterSequenceMetaType< QList<int> >(m_ScriptEngine); 00035 qScriptRegisterSequenceMetaType< QList<bool> >(m_ScriptEngine); 00036 qScriptRegisterSequenceMetaType< QList<double> >(m_ScriptEngine); 00037 qScriptRegisterSequenceMetaType< QList<QString> >(m_ScriptEngine); 00038 qScriptRegisterSequenceMetaType< QVector<int> >(m_ScriptEngine); 00039 qScriptRegisterSequenceMetaType< QVector<bool> >(m_ScriptEngine); 00040 qScriptRegisterSequenceMetaType< QVector<double> >(m_ScriptEngine); 00041 qScriptRegisterSequenceMetaType< QVector<QString> >(m_ScriptEngine); 00042 00043 m_ScriptEngine -> globalObject().setProperty("acquireData",m_ScriptEngine -> newFunction(acquireDataFunc)); 00044 m_ScriptEngine -> globalObject().setProperty("acquireReference",m_ScriptEngine -> newFunction(acquireReferenceFunc)); 00045 m_ScriptEngine -> globalObject().setProperty("acquireDark",m_ScriptEngine -> newFunction(acquireDarkFunc)); 00046 m_ScriptEngine -> globalObject().setProperty("acquireScope",m_ScriptEngine -> newFunction(acquireScopeFunc)); 00047 m_ScriptEngine -> globalObject().setProperty("acquireOnce",m_ScriptEngine -> newFunction(acquireOnceFunc)); 00048 m_ScriptEngine -> globalObject().setProperty("acquireCancel",m_ScriptEngine -> newFunction(acquireCancelFunc)); 00049 m_ScriptEngine -> globalObject().setProperty("status",m_ScriptEngine -> newFunction(statusFunc)); 00050 m_ScriptEngine -> globalObject().setProperty("newFile",m_ScriptEngine -> newFunction(newFileFunc)); 00051 m_ScriptEngine -> globalObject().setProperty("closeFile",m_ScriptEngine -> newFunction(closeFileFunc)); 00052 m_ScriptEngine -> globalObject().setProperty("newScan",m_ScriptEngine -> newFunction(newScanFunc)); 00053 m_ScriptEngine -> globalObject().setProperty("saveData",m_ScriptEngine -> newFunction(saveDataFunc)); 00054 // m_ScriptEngine -> globalObject().setProperty("readData",m_ScriptEngine -> newFunction(readDataFunc)); 00055 m_ScriptEngine -> globalObject().setProperty("channel",m_ScriptEngine -> newFunction(fitterFunc)); 00056 m_ScriptEngine -> globalObject().setProperty("print",m_ScriptEngine -> newFunction(printFunc)); 00057 00058 m_ScriptEngine -> globalObject().setProperty("application",m_ScriptEngine -> newQObject(m_Application)); 00059 m_ScriptEngine -> globalObject().setProperty("acquisition",m_ScriptEngine -> newQObject(m_Acquisition)); 00060 m_ScriptEngine -> globalObject().setProperty("window",m_ScriptEngine -> newQObject(m_Window)); 00061 00062 // printf("leave QavrgScriptingEngine::initialize\n"); 00063 } 00064 00065 void QavrgScriptingEngine::evaluateAppCommand(QString expr) 00066 { 00067 QMutexLocker lock(&m_Mutex); 00068 00069 // printf("QavrgScriptingEngine::evaluateAppCommand(%s)\n", qPrintable(expr)); 00070 00071 QMetaObject::invokeMethod(this, "evaluate", Qt::QueuedConnection, Q_ARG(int, 0), Q_ARG(QString, expr)); 00072 } 00073 00074 void QavrgScriptingEngine::evaluateServerCommand(QString expr) 00075 { 00076 QMutexLocker lock(&m_Mutex); 00077 00078 // printf("QavrgScriptingEngine::evaluateServerCommand(%s)\n", qPrintable(expr)); 00079 00080 QMetaObject::invokeMethod(this, "evaluate", Qt::QueuedConnection, Q_ARG(int, 1), Q_ARG(QString, expr)); 00081 } 00082 00083 void QavrgScriptingEngine::evaluateSpecCommand(QString expr) 00084 { 00085 QMutexLocker lock(&m_Mutex); 00086 00087 // printf("QavrgScriptingEngine::evaluateSpecCommand(%s)\n", qPrintable(expr)); 00088 00089 QMetaObject::invokeMethod(this, "evaluate", Qt::QueuedConnection, Q_ARG(int, 2), Q_ARG(QString, expr)); 00090 } 00091 00092 void QavrgScriptingEngine::evaluate(int src, QString expr) 00093 { 00094 THREAD_CHECK; 00095 00096 // printf("QavrgScriptingEngine::evaluate(%s)\n", qPrintable(expr)); 00097 00098 QScriptValue result = m_ScriptEngine -> evaluate(expr); 00099 00100 switch (src) { 00101 case 0: 00102 emit appResultAvailable(result); 00103 break; 00104 case 1: 00105 emit serverResultAvailable(result); 00106 break; 00107 case 2: 00108 emit specResultAvailable(result); 00109 break; 00110 } 00111 } 00112 00113 void QavrgScriptingEngine::cancelCommand() 00114 { 00115 m_ScriptEngine -> abortEvaluation(); 00116 } 00117 00118 bool QavrgScriptingEngine::hasUncaughtException() const 00119 { 00120 QMutexLocker lock(&m_Mutex); 00121 00122 return m_ScriptEngine -> hasUncaughtException(); 00123 } 00124 00125 int QavrgScriptingEngine::uncaughtExceptionLineNumber() const 00126 { 00127 QMutexLocker lock(&m_Mutex); 00128 00129 return m_ScriptEngine -> uncaughtExceptionLineNumber(); 00130 } 00131 00132 QString QavrgScriptingEngine::uncaughtExceptionString() const 00133 { 00134 QMutexLocker lock(&m_Mutex); 00135 00136 return m_ScriptEngine -> uncaughtException().toString(); 00137 } 00138 00139 QScriptValue QavrgScriptingEngine::printFunc(QScriptContext *context, QScriptEngine *engine) 00140 { 00141 int nargs = context->argumentCount(); 00142 QString result; 00143 00144 for (int i=0; i<nargs; i++) { 00145 if (i>0) { 00146 result += " "; 00147 } 00148 00149 result += context->argument(i).toString(); 00150 } 00151 00152 g_ScriptingEngine -> emit printMessage(result); 00153 00154 return QScriptValue(engine, 0); 00155 } 00156 00157 QScriptValue QavrgScriptingEngine::newFileFunc(QScriptContext *context, QScriptEngine *engine) 00158 { 00159 int nargs = context->argumentCount(); 00160 int nscan = 0; 00161 QString name; 00162 00163 switch (nargs) { 00164 case 2: 00165 nscan = context->argument(1).toInteger(); 00166 case 1: 00167 name = context->argument(0).toString(); 00168 g_ScriptingEngine -> emit printMessage(tr("newFile(\"%1\".%2)").arg(name).arg(nscan)); 00169 00170 QMetaObject::invokeMethod(g_Acquisition, "newSaveFile", Q_ARG(QString, name), Q_ARG(int, nscan)); 00171 break; 00172 00173 default: 00174 return context -> throwError("newFile(QString,int=0) takes one or two arguments"); 00175 } 00176 00177 return QScriptValue(engine, nargs); 00178 } 00179 00180 QScriptValue QavrgScriptingEngine::closeFileFunc(QScriptContext *context, QScriptEngine *engine) 00181 { 00182 00183 int nargs = context->argumentCount(); 00184 00185 if (nargs == 0) { 00186 g_ScriptingEngine -> emit printMessage("closeFile()"); 00187 QMetaObject::invokeMethod(g_Acquisition, "closeSaveFile"); 00188 } else { 00189 return context -> throwError("closeFile() takes no arguments"); 00190 } 00191 00192 return QScriptValue(engine, nargs); 00193 } 00194 00195 static int s_ScanNArgs = 0; 00196 00197 QScriptValue QavrgScriptingEngine::newScanFunc(QScriptContext *context, QScriptEngine *engine) 00198 { 00199 QString cmd = "newScan("; 00200 00201 QVector<QString> hdr; 00202 00203 int nargs = context->argumentCount(); 00204 00205 s_ScanNArgs = nargs; 00206 00207 for (int i = 0; i<nargs; i++) { 00208 if (i != 0) { 00209 cmd += ", "; 00210 } 00211 cmd += "\""+context->argument(i).toString()+"\""; 00212 00213 hdr.append(context->argument(i).toString()); 00214 } 00215 cmd += ")"; 00216 00217 g_ScriptingEngine -> emit printMessage(cmd); 00218 00219 bool ok = QMetaObject::invokeMethod(g_Acquisition, "startNewScan", Q_ARG(QcepStringVector, hdr)); 00220 00221 if (!ok) { 00222 printf("Problem with QMetaObject::invokeMethod startNewScan\n"); 00223 } 00224 00225 return QScriptValue(engine, nargs); 00226 } 00227 00228 00229 QScriptValue QavrgScriptingEngine::acquireDataFunc(QScriptContext *context, QScriptEngine *engine) 00230 { 00231 if (g_Acquisition -> getAcquisitionLock() == 0) { 00232 return context -> throwError("Attempt to start new acquire operation before previous acquire has completed"); 00233 } 00234 00235 switch (context->argumentCount()) { 00236 case 2: 00237 g_ScriptingEngine -> emit printMessage(tr("acquireData(%1,%2)") 00238 .arg(context->argument(0).toNumber()) 00239 .arg(context->argument(1).toInteger())); 00240 00241 g_Acquisition -> set_NRepeats(context->argument(1).toInteger()); 00242 g_Acquisition -> set_IntegrationTime(context->argument(0).toNumber()); 00243 break; 00244 00245 case 1: 00246 g_ScriptingEngine -> emit printMessage(tr("acquireData(%1)") 00247 .arg(context->argument(0).toNumber())); 00248 00249 g_ScriptingEngine -> emit printMessage("acquireData()"); 00250 g_Acquisition -> set_IntegrationTime(context->argument(0).toNumber()); 00251 break; 00252 00253 case 0: 00254 g_ScriptingEngine -> emit printMessage("acquireData()"); 00255 break; 00256 00257 default: 00258 return context -> throwError("acquireData() takes zero, one or two arguments"); 00259 } 00260 00261 QMetaObject::invokeMethod(g_Acquisition, "acquireData"); 00262 00263 return QScriptValue(engine,0); 00264 } 00265 00266 QScriptValue QavrgScriptingEngine::acquireReferenceFunc(QScriptContext *context, QScriptEngine *engine) 00267 { 00268 if (g_Acquisition -> getAcquisitionLock() == 0) { 00269 return context -> throwError("Attempt to start new acquire operation before previous acquire has completed"); 00270 } 00271 00272 switch (context->argumentCount()) { 00273 case 2: 00274 g_ScriptingEngine -> emit printMessage(tr("acquireReference(%1,%2)") 00275 .arg(context->argument(0).toNumber()) 00276 .arg(context->argument(1).toInteger())); 00277 00278 g_Acquisition -> set_ReferenceNRepeats(context->argument(1).toInteger()); 00279 g_Acquisition -> set_IntegrationTime(context->argument(0).toNumber()); 00280 break; 00281 00282 case 1: 00283 g_ScriptingEngine -> emit printMessage(tr("acquireReference(%1)") 00284 .arg(context->argument(0).toNumber())); 00285 00286 g_Acquisition -> set_IntegrationTime(context->argument(0).toNumber()); 00287 break; 00288 00289 case 0: 00290 g_ScriptingEngine -> emit printMessage("acquireReference()"); 00291 break; 00292 00293 default: 00294 return context -> throwError("acquireReference() takes zero, one or two arguments"); 00295 } 00296 00297 QMetaObject::invokeMethod(g_Acquisition, "acquireReference"); 00298 00299 return QScriptValue(engine,0); 00300 } 00301 00302 QScriptValue QavrgScriptingEngine::acquireDarkFunc(QScriptContext *context, QScriptEngine *engine) 00303 { 00304 if (g_Acquisition -> getAcquisitionLock() == 0) { 00305 return context -> throwError("Attempt to start new acquire operation before previous acquire has completed"); 00306 } 00307 00308 switch (context->argumentCount()) { 00309 case 2: 00310 g_ScriptingEngine -> emit printMessage(tr("acquireDark(%1,%2)") 00311 .arg(context->argument(0).toNumber()) 00312 .arg(context->argument(1).toInteger())); 00313 00314 g_Acquisition -> set_DarkNRepeats(context->argument(1).toInteger()); 00315 g_Acquisition -> set_IntegrationTime(context->argument(0).toNumber()); 00316 break; 00317 00318 case 1: 00319 g_ScriptingEngine -> emit printMessage(tr("acquireDark(%1)") 00320 .arg(context->argument(0).toNumber())); 00321 00322 g_Acquisition -> set_IntegrationTime(context->argument(0).toNumber()); 00323 break; 00324 00325 case 0: 00326 g_ScriptingEngine -> emit printMessage("acquireDark()"); 00327 break; 00328 00329 default: 00330 return context -> throwError("acquireDark() takes zero, one or two arguments"); 00331 } 00332 00333 QMetaObject::invokeMethod(g_Acquisition, "acquireDark"); 00334 00335 return QScriptValue(engine,0); 00336 } 00337 00338 QScriptValue QavrgScriptingEngine::acquireScopeFunc(QScriptContext *context, QScriptEngine *engine) 00339 { 00340 if (g_Acquisition -> getAcquisitionLock() == 0) { 00341 return context -> throwError("Attempt to start new acquire operation before previous acquire has completed"); 00342 } 00343 00344 switch (context->argumentCount()) { 00345 case 1: 00346 g_ScriptingEngine -> emit printMessage(tr("acquireScope(%1)") 00347 .arg(context->argument(0).toNumber())); 00348 00349 g_Acquisition -> set_ScopeIntegrationTime(context->argument(0).toNumber()); 00350 break; 00351 00352 case 0: 00353 g_ScriptingEngine -> emit printMessage("acquireScope()"); 00354 break; 00355 00356 default: 00357 return context -> throwError("acquireScope() takes zero or one arguments"); 00358 } 00359 00360 QMetaObject::invokeMethod(g_Acquisition, "acquireScope"); 00361 00362 return QScriptValue(engine,0); 00363 } 00364 00365 QScriptValue QavrgScriptingEngine::acquireOnceFunc(QScriptContext *context, QScriptEngine *engine) 00366 { 00367 if (g_Acquisition -> getAcquisitionLock() == 0) { 00368 return context -> throwError("Attempt to start new acquire operation before previous acquire has completed"); 00369 } 00370 00371 switch (context->argumentCount()) { 00372 case 1: 00373 g_ScriptingEngine -> emit printMessage(tr("acquireOnce(%1)") 00374 .arg(context->argument(0).toNumber())); 00375 00376 g_Acquisition -> set_IntegrationTime(context->argument(0).toNumber()); 00377 break; 00378 00379 case 0: 00380 g_ScriptingEngine -> emit printMessage("acquireOnce()"); 00381 break; 00382 00383 default: 00384 return context -> throwError("acquireOnce() takes zero or one arguments"); 00385 } 00386 00387 QMetaObject::invokeMethod(g_Acquisition, "acquireOnce"); 00388 00389 return QScriptValue(engine,0); 00390 } 00391 00392 QScriptValue QavrgScriptingEngine::acquireCancelFunc(QScriptContext *context, QScriptEngine *engine) 00393 { 00394 if (context->argumentCount() == 0) { 00395 g_ScriptingEngine -> emit printMessage("acquireCancel()"); 00396 00397 g_Acquisition -> acquireCancel(); 00398 } else { 00399 return context -> throwError("acquireCancel() takes no arguments"); 00400 } 00401 00402 return QScriptValue(engine,0); 00403 } 00404 00405 QScriptValue QavrgScriptingEngine::statusFunc(QScriptContext *context, QScriptEngine *engine) 00406 { 00407 int status = 0; 00408 00409 switch (context->argumentCount()) { 00410 case 1: 00411 status = g_Acquisition -> acquireWait(context->argument(0).toNumber()); 00412 break; 00413 00414 case 0: 00415 status = g_Acquisition -> get_Status(); 00416 break; 00417 00418 default: 00419 return context -> throwError("status() takes zero or one arguments"); 00420 } 00421 00422 return QScriptValue(engine, status); 00423 } 00424 00425 QScriptValue QavrgScriptingEngine::saveDataFunc(QScriptContext *context, QScriptEngine *engine) 00426 { 00427 int nargs = context->argumentCount(); 00428 00429 if (nargs != s_ScanNArgs) { 00430 return context -> throwError(tr("saveData() called with %1 arguments, newScan() was called with %2") 00431 .arg(nargs).arg(s_ScanNArgs)); 00432 } 00433 00434 QString cmd = "saveData("; 00435 00436 QVector<double> vals; 00437 00438 for (int i = 0; i<nargs; i++) { 00439 if (i != 0) { 00440 cmd += ", "; 00441 } 00442 cmd += tr("%1").arg(context->argument(i).toNumber()); 00443 00444 vals.append(context->argument(i).toNumber()); 00445 } 00446 00447 cmd += ")"; 00448 00449 g_ScriptingEngine -> emit printMessage(cmd); 00450 00451 bool ok = QMetaObject::invokeMethod(g_Acquisition, "saveAcquisitionResult", Q_ARG(QcepDoubleVector, vals)); 00452 00453 if (!ok) { 00454 printf("Problem with QMetaObject::invokeMethod\n"); 00455 } 00456 00457 return QScriptValue(engine, nargs); 00458 } 00459 00460 //QScriptValue QavrgScriptingEngine::readDataFunc(QScriptContext *context, QScriptEngine *engine) 00461 //{ 00462 // int nargs = context->argumentCount(); 00463 // 00464 // switch (nargs) { 00465 // case 1: 00466 // { 00467 // int n1 = context->argument(0).toInteger(); 00468 // 00469 // QVector <double> res = g_Acquisition -> fitter(n1get_RawData().value(n1); 00470 // return qScriptValueFromSequence(engine, res); 00471 // } 00472 // case 2: 00473 // { 00474 // int n1 = context->argument(0).toInteger(); 00475 // int n2 = context->argument(1).toInteger(); 00476 // double res = g_Acquisition -> get_RawData().value(n1).value(n2); 00477 // return QScriptValue(engine, res); 00478 // } 00479 // case 3: 00480 // { 00481 // int n1 = context->argument(0).toInteger(); 00482 // int n2 = context->argument(1).toInteger(); 00483 // int n3 = context->argument(2).toInteger(); 00484 // QVector <double> res = g_Acquisition -> get_RawData().value(n1).mid(n2,n3); 00485 // return qScriptValueFromSequence(engine, res); 00486 // } 00487 // } 00488 // 00489 // return QScriptValue(engine, 0); 00490 //} 00491 00492 QScriptValue QavrgScriptingEngine::fitterFunc(QScriptContext *context, QScriptEngine *engine) 00493 { 00494 QavrgFitter *f = NULL; 00495 00496 int nargs = context->argumentCount(); 00497 00498 if (nargs == 1) { 00499 int n = context->argument(0).toInteger(); 00500 00501 f = g_Acquisition -> fitter(n); 00502 } 00503 00504 return engine->newQObject(f); 00505 }