convert bluemonkey script to be qml based engine
authorKevron Rees <kevron.m.rees@intel.com>
Fri, 12 Sep 2014 16:49:45 +0000 (09:49 -0700)
committerKevron Rees <kevron.m.rees@intel.com>
Fri, 12 Sep 2014 16:49:45 +0000 (09:49 -0700)
examples/databasesource [deleted file]
plugins/bluemonkey/CMakeLists.txt
plugins/bluemonkey/authenticate.cpp
plugins/bluemonkey/bluemonkey.cpp
plugins/bluemonkey/bluemonkey.h

diff --git a/examples/databasesource b/examples/databasesource
deleted file mode 100644 (file)
index 15a039c..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-{
-  "sources" : [
-    {
-      "name" : "Database Source",
-      "path" : "/usr/lib/x86_64-linux-gnu/automotive-message-broker/databasesinkplugin.so",
-      "playbackOnLoad" : "true",
-      "databaseFile" : "generated.db"
-    }
-  ],
-  "sinks": [
-    {
-      "path" : "/usr/lib/x86_64-linux-gnu/automotive-message-broker/dbussinkplugin.so"
-    }
-  ]
-}
-
index 3482d48..0e710e2 100644 (file)
@@ -2,12 +2,12 @@ if(bluemonkey_plugin)
 
 find_package(Qt5Core REQUIRED)
 find_package(Qt5Network REQUIRED)
-find_package(Qt5Script REQUIRED)
+find_package(Qt5Qml REQUIRED)
 if(Qt5Core_FOUND)
   message(STATUS "using Qt5")
 
-  set(QT_INCLUDE_DIRS ${Qt5Core_INCLUDE_DIRS} ${Qt5Network_INCLUDE_DIRS} ${Qt5Script_INCLUDE_DIRS})
-  set(QT_LIBRARIES ${Qt5Core_LIBRARIES} ${Qt5Network_LIBRARIES} ${Qt5Script_LIBRARIES})
+  set(QT_INCLUDE_DIRS ${Qt5Core_INCLUDE_DIRS} ${Qt5Network_INCLUDE_DIRS} ${Qt5Qml_INCLUDE_DIRS})
+  set(QT_LIBRARIES ${Qt5Core_LIBRARIES} ${Qt5Network_LIBRARIES} ${Qt5Qml_LIBRARIES})
   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Qt5Core_EXECUTABLE_COMPILE_FLAGS}")
   add_definitions(${Qt5Core_DEFINITIONS})
 
@@ -20,8 +20,8 @@ set(CMAKE_AUTOMOC ON)
 
 include_directories(${CMAKE_SOURCE_DIR}/lib ${include_dirs} ${communi_INCLUDE_DIRS} ${QT_INCLUDE_DIRS} ${CMAKE_SOURCE_DIR}/plugins/common)
 
-set(bluemonkeyplugin_headers bluemonkey.h authenticate.h agent.h)
-set(bluemonkeyplugin_sources bluemonkey.cpp authenticate.cpp agent.cpp)
+set(bluemonkeyplugin_headers bluemonkey.h authenticate.h)
+set(bluemonkeyplugin_sources bluemonkey.cpp authenticate.cpp)
 
 add_library(bluemonkeyplugin MODULE ${bluemonkeyplugin_sources})
 set_target_properties(bluemonkeyplugin PROPERTIES PREFIX "")
index 0097456..81a540f 100644 (file)
@@ -1,16 +1,18 @@
 #include "authenticate.h"
 #include <QSettings>
-#include <QtScript>
+#include <QJSEngine>
+#include <QFile>
+#include <QtDebug>
 
 Authenticate::Authenticate(std::map<std::string, std::string> config, QObject *parent) :
-    QObject(parent)
+       QObject(parent)
 {
        QSettings settings;
        authorized = settings.value("authorized").toStringList();
 
-       QScriptEngine engine;
+       QJSEngine engine;
 
-       QScriptValue eventEngineValue = engine.newQObject(this);
+       QJSValue eventEngineValue = engine.newQObject(this);
        engine.globalObject().setProperty("auth", eventEngineValue);
 
        QString str = config["authSettings"].c_str();
index 014540b..11b12af 100644 (file)
 #include "debugout.h"
 
 #include <QJsonDocument>
-#include <QScriptEngine>
+#include <QJSEngine>
 #include <QDateTime>
 #include <QString>
 #include <QFile>
 #include <QTimer>
-
-Q_SCRIPT_DECLARE_QMETAOBJECT(QTimer, QObject*)
+#include <QtQml>
 
 #define foreach Q_FOREACH
 
@@ -92,12 +91,14 @@ QVariant gvariantToQVariant(GVariant *value)
 
 }
 
-BluemonkeySink::BluemonkeySink(AbstractRoutingEngine* e, map<string, string> config, AbstractSource &parent): QObject(0), AmbPluginImpl(e, config, parent), agent(nullptr), engine(nullptr), mSilentMode(false)
+BluemonkeySink::BluemonkeySink(AbstractRoutingEngine* e, map<string, string> config, AbstractSource &parent): QObject(0), AmbPluginImpl(e, config, parent), engine(nullptr), mSilentMode(false)
 {
        QTimer::singleShot(1,this,SLOT(reloadEngine()));
 
        auth = new Authenticate(config, this);
 
+       qmlRegisterType<QTimer>("", 1, 0, "QTimer");
+
 /*     connect(irc, &IrcCommunication::message, [&](QString sender, QString prefix, QString codes ) {
 
                if(codes.startsWith("authenticate"))
@@ -166,7 +167,7 @@ QObject *BluemonkeySink::subscribeTo(QString str)
        return new Property(str.toStdString(), "", routingEngine, this);
 }
 
-QObject *BluemonkeySink::subscribeTo(QString str, QString srcFilter)
+QObject *BluemonkeySink::subscribeToSource(QString str, QString srcFilter)
 {
        return new Property(str.toStdString(), srcFilter, routingEngine, this);
 }
@@ -216,7 +217,7 @@ void BluemonkeySink::loadConfig(QString str)
 
        DebugOut()<<"evaluating script: "<<script.toStdString()<<endl;
 
-       QScriptValue val = engine->evaluate(script);
+       QJSValue val = engine->evaluate(script);
 
        DebugOut()<<val.toString().toStdString()<<endl;
 }
@@ -226,32 +227,22 @@ void BluemonkeySink::reloadEngine()
        if(engine)
                engine->deleteLater();
 
-       engine = new QScriptEngine(this);
-
-       if(agent) delete agent;
-
-       agent = new BluemonkeyAgent(engine);
+       engine = new QJSEngine(this);
 
-       //engine->setAgent(agent);
-
-       QScriptValue value = engine->newQObject(this);
+       QJSValue value = engine->newQObject(this);
        engine->globalObject().setProperty("bluemonkey", value);
 
-       QScriptValue qtimerClass = engine->scriptValueFromQMetaObject<QTimer>();
-       engine->globalObject().setProperty("QTimer", qtimerClass);
-
-//     QScriptValue ircValue = engine->newQObject(irc);
-//     engine->globalObject().setProperty("irc", ircValue);
-
        loadConfig(configuration["config"].c_str());
 }
 
 void BluemonkeySink::writeProgram(QString program)
 {
-       QScriptSyntaxCheckResult result = QScriptEngine::checkSyntax(program);
-       if(result.state() != QScriptSyntaxCheckResult::Valid)
+
+       QJSEngine temp;
+       QJSValue result = temp.evaluate(program);
+       if(result.isError())
        {
-               DebugOut(DebugOut::Error)<<"Syntax error in program: "<<result.errorMessage().toStdString()<<endl;
+               DebugOut(DebugOut::Error)<<"Syntax error in program: "<<result.toString().toStdString()<<endl;
                return;
        }
 
@@ -274,7 +265,12 @@ void BluemonkeySink::log(QString str)
        DebugOut()<<str.toStdString()<<endl;
 }
 
-void BluemonkeySink::getHistory(QStringList properties, QDateTime begin, QDateTime end, QScriptValue cbFunction)
+QObject *BluemonkeySink::createTimer()
+{
+       return new QTimer(this);
+}
+
+void BluemonkeySink::getHistory(QStringList properties, QDateTime begin, QDateTime end, QJSValue cbFunction)
 {
        double b = (double)begin.toMSecsSinceEpoch() / 1000.0;
        double e = (double)end.toMSecsSinceEpoch() / 1000.0;
@@ -298,7 +294,7 @@ void BluemonkeySink::getHistory(QStringList properties, QDateTime begin, QDateTi
                        return;
                }
 
-               if(cbFunction.isFunction())
+               if(cbFunction.isCallable())
                {
                        QVariantList list;
 
@@ -309,7 +305,9 @@ void BluemonkeySink::getHistory(QStringList properties, QDateTime begin, QDateTi
                                list.append(gvariantToQVariant(val->toVariant()));
                        }
 
-                       cbFunction.call(QScriptValue(),cbFunction.engine()->newVariant(list));
+                       QJSValue val = cbFunction.engine()->toScriptValue<QVariantList>(list);
+
+                       cbFunction.call(QJSValueList()<<val);
 
                }
 
@@ -319,7 +317,7 @@ void BluemonkeySink::getHistory(QStringList properties, QDateTime begin, QDateTi
        routingEngine->getRangePropertyAsync(request);
 }
 
-void BluemonkeySink::createCustomProperty(QString name, QScriptValue defaultValue)
+void BluemonkeySink::createCustomProperty(QString name, QJSValue defaultValue)
 {
 
        auto create = [defaultValue, name]() -> AbstractPropertyType*
@@ -386,7 +384,7 @@ void Property::setValue(QVariant v)
        routingEngine->setProperty(request);
 }
 
-void Property::getHistory(QDateTime begin, QDateTime end, QScriptValue cbFunction)
+void Property::getHistory(QDateTime begin, QDateTime end, QJSValue cbFunction)
 {
        double b = (double)begin.toMSecsSinceEpoch() / 1000.0;
        double e = (double)end.toMSecsSinceEpoch() / 1000.0;
@@ -406,7 +404,7 @@ void Property::getHistory(QDateTime begin, QDateTime end, QScriptValue cbFunctio
                        return;
                }
 
-               if(cbFunction.isFunction())
+               if(cbFunction.isCallable())
                {
                        QVariantList list;
 
@@ -416,8 +414,8 @@ void Property::getHistory(QDateTime begin, QDateTime end, QScriptValue cbFunctio
 
                                list.append(gvariantToQVariant(val->toVariant()));
                        }
-
-                       cbFunction.call(QScriptValue(),cbFunction.engine()->newVariant(list));
+                       QJSValue val = cbFunction.engine()->toScriptValue<QVariantList>(list);
+                       cbFunction.call(QJSValueList()<<val);
 
                }
 
index 34c7f29..a8df6ba 100644 (file)
 #include <QVariant>
 #include <QJsonDocument>
 #include <QDateTime>
-#include <QScriptValue>
+#include <QJSValue>
 #include "uuidhelper.h"
 
 #include "authenticate.h"
-#include "agent.h"
 
-class QScriptEngine;
+class QJSEngine;
 
 class ModuleInterface
 {
@@ -70,7 +69,7 @@ public:
        QVariant value();
        void setValue(QVariant v);
 
-       void getHistory(QDateTime begin, QDateTime end, QScriptValue cbFunction);
+       void getHistory(QDateTime begin, QDateTime end, QJSValue cbFunction);
 Q_SIGNALS:
 
        void changed(QVariant val);
@@ -91,7 +90,7 @@ public:
        virtual void propertyChanged(AbstractPropertyType* value);
        virtual const std::string uuid() const;
 
-       QScriptEngine* engine;
+       QJSEngine* engine;
 
        virtual int supportedOperations();
 
@@ -104,7 +103,7 @@ private: //source privates
 public Q_SLOTS:
 
        QObject* subscribeTo(QString str);
-       QObject* subscribeTo(QString str, QString srcFilter);
+       QObject* subscribeToSource(QString str, QString srcFilter);
 
        QStringList sourcesForProperty(QString property);
 
@@ -122,20 +121,21 @@ public Q_SLOTS:
 
        void log(QString str);
 
-       void getHistory(QStringList properties, QDateTime begin, QDateTime end, QScriptValue cbFunction);
+       QObject* createTimer();
+
+       void getHistory(QStringList properties, QDateTime begin, QDateTime end, QJSValue cbFunction);
 
        void setSilentMode(bool m)
        {
                mSilentMode = m;
        }
 
-       void createCustomProperty(QString name, QScriptValue defaultValue);
+       void createCustomProperty(QString name, QJSValue defaultValue);
 
 private:
        QStringList configsToLoad;
 
        Authenticate* auth;
-       BluemonkeyAgent* agent;
        bool mSilentMode;
 };