nearly functional bluemonkey plugin
authorKevron Rees <tripzero.kev@gmail.com>
Thu, 6 Jun 2013 05:33:16 +0000 (22:33 -0700)
committerKevron Rees <tripzero.kev@gmail.com>
Thu, 6 Jun 2013 05:33:16 +0000 (22:33 -0700)
examples/bluemonkey/authSettings.js [new file with mode: 0644]
examples/bluemonkey/bluemonkeyconfig [moved from examples/bluemonkeyconfig with 100% similarity]
examples/bluemonkey/config.js [new file with mode: 0644]
lib/abstractsink.h
plugins/bluemonkey/CMakeLists.txt
plugins/bluemonkey/bluemonkey.cpp
plugins/bluemonkey/bluemonkey.h

diff --git a/examples/bluemonkey/authSettings.js b/examples/bluemonkey/authSettings.js
new file mode 100644 (file)
index 0000000..a678486
--- /dev/null
@@ -0,0 +1 @@
+auth.pin = "000"
diff --git a/examples/bluemonkey/config.js b/examples/bluemonkey/config.js
new file mode 100644 (file)
index 0000000..6a77033
--- /dev/null
@@ -0,0 +1,4 @@
+var speedProperty = bluemonkey.subscribeTo("VehicleSpeed");
+
+
+       
index 110c72e..77ca6ec 100644 (file)
@@ -54,7 +54,8 @@ public:
          * @see AbstractRoutingEngine::subscribeToPropertyChanges()
          * @param property name that changed
          * @param value value of the property that changed. this is a temporary pointer that will be destroyed.
-         * Do not destroy it.  If you need to store the value use value.anyValue() or value.value<T>() to copy.
+         * Do not destroy it.  If you need to store the value use value.anyValue(), value.value<T>() or
+         * value->copy() to copy.
          * @param uuid Unique identifier representing the source
          */
        virtual void propertyChanged(VehicleProperty::Property property, AbstractPropertyType* value, string  uuid) = 0;
@@ -68,6 +69,8 @@ protected:
        map<string, string> configuration;
 };
 
+
+/// TODO:  this class actually serves no purpose.
 class AbstractSinkManager
 {
 public:
index 2024828..8ec0840 100644 (file)
@@ -21,8 +21,8 @@ add_definitions(-DCOMMUNI_SHARED)
 
 include_directories(${CMAKE_SOURCE_DIR}/lib ${include_dirs} ${communi_INCLUDE_DIRS} ${QT_INCLUDE_DIRS})
 
-set(bluemonkeyplugin_headers bluemonkey.h irccoms.h)
-set(bluemonkeyplugin_sources bluemonkey.cpp irccoms.cpp)
+set(bluemonkeyplugin_headers bluemonkey.h irccoms.h authenticate.h)
+set(bluemonkeyplugin_sources bluemonkey.cpp irccoms.cpp authenticate.cpp)
 
 add_library(bluemonkeyplugin MODULE ${bluemonkeyplugin_sources})
 set_target_properties(bluemonkeyplugin PROPERTIES PREFIX "")
index c28d267..e8cfb0a 100644 (file)
 #include "irccoms.h"
 
 #include <QJsonDocument>
-
+#include <QScriptEngine>
+#include <QString>
+#include <QFile>
 
 extern "C" AbstractSinkManager * create(AbstractRoutingEngine* routingengine, map<string, string> config)
 {
        return new BluemonkeySinkManager(routingengine, config);
 }
 
-BluemonkeySink::BluemonkeySink(AbstractRoutingEngine* engine, map<string, string> config): QObject(0),AbstractSink(engine, config)
+QVariant gvariantToQVariant(GVariant *value)
+{
+       switch (g_variant_classify(value)) {
+               case G_VARIANT_CLASS_BOOLEAN:
+                       return QVariant((bool) g_variant_get_boolean(value));
+
+               case G_VARIANT_CLASS_BYTE:
+                       return QVariant((char) g_variant_get_byte(value));
+
+               case G_VARIANT_CLASS_INT16:
+                       return QVariant((int) g_variant_get_int16(value));
+
+               case G_VARIANT_CLASS_UINT16:
+                       return QVariant((unsigned int) g_variant_get_uint16(value));
+
+               case G_VARIANT_CLASS_INT32:
+                       return QVariant((int) g_variant_get_int32(value));
+
+               case G_VARIANT_CLASS_UINT32:
+                       return QVariant((unsigned int) g_variant_get_uint32(value));
+
+               case G_VARIANT_CLASS_INT64:
+                       return QVariant((long long) g_variant_get_int64(value));
+
+               case G_VARIANT_CLASS_UINT64:
+                       return QVariant((unsigned long long) g_variant_get_uint64(value));
+
+               case G_VARIANT_CLASS_DOUBLE:
+                       return QVariant(g_variant_get_double(value));
+
+               case G_VARIANT_CLASS_STRING:
+                       return QVariant(g_variant_get_string(value, NULL));
+
+               default:
+                       return QVariant::Invalid;
+       }
+}
+
+BluemonkeySink::BluemonkeySink(AbstractRoutingEngine* e, map<string, string> config): QObject(0), AbstractSink(e, config)
 {
        irc = new IrcCommunication(this);
-       irc->connect("chat.freenode.com",8001,"","tripzero","bluemoney","");
+       irc->connect("chat.freenode.com",8001,"","tripzero","bluemonkey","");
        connect(irc,&IrcCommunication::connected, [&]() {
                irc->join("#linuxice");
        });
 
+       engine = new QScriptEngine(this);
+
+       auth = new Authenticate(this);
+
+       QScriptValue value = engine->newQObject(this);
+       engine->globalObject().setProperty("bluemonkey", value);
+
+       connect(irc, &IrcCommunication::message, [&](QString sender, QString prefix, QString codes ) {
+
+               if(codes.contains("authenticate"))
+               {
+
+                       int i = codes.indexOf("authenticate");
+                       QString pin = codes.mid(i+10);
+                       pin = pin.trimmed();
+
+
+                       if(!auth->authorize(prefix, pin))
+                               irc->respond(sender,"failed");
+
+               }
+               else if(codes.startsWith("bluemonkey"))
+               {
+                       if(!auth->isAuthorized(prefix))
+                       {
+                               irc->respond(sender, "denied");
+                               return;
+                       }
+                       irc->respond(sender, engine->evaluate(codes).toString());
+               }
+       });
+
+       loadConfig("config.js");
+
 }
 
 
@@ -66,14 +140,31 @@ QObject *BluemonkeySink::subscribeTo(QString str)
        return new Property(str.toStdString(), routingEngine, this);
 }
 
+bool BluemonkeySink::authenticate(QString pass)
+{
 
-QVariant Property::value()
+}
+
+void BluemonkeySink::loadConfig(QString str)
 {
-       QJsonDocument doc;
+       QFile file(str);
+       if(!file.open(QIODevice::ReadOnly))
+       {
+               qDebug()<<"failed to open config file: "<<str;
+               return;
+       }
+
+       QString script = file.readAll();
+
+       file.close();
+
+       engine->evaluate(script);
+}
 
-       doc.fromJson(mValue->toString().c_str());
 
-       return doc.toVariant();
+QVariant Property::value()
+{
+       return gvariantToQVariant(mValue->toVariant());
 }
 
 void Property::setValue(QVariant v)
@@ -91,7 +182,7 @@ void Property::setValue(QVariant v)
 }
 
 Property::Property(VehicleProperty::Property prop, AbstractRoutingEngine* re, QObject *parent)
-       :QObject(parent), AbstractSink(re, std::map<std::string,std::string>())
+       :QObject(parent), AbstractSink(re, std::map<std::string,std::string>()),mValue(nullptr)
 {
        setType(prop.c_str());
 }
@@ -103,6 +194,11 @@ QString Property::type()
 
 void Property::setType(QString t)
 {
+       if(mValue && type() != "")
+               routingEngine->unsubscribeToProperty(type().toStdString(),this);
+
+       routingEngine->subscribeToProperty(t.toStdString(),this);
+
        mValue = VehicleProperty::getPropertyTypeForPropertyNameValue(t.toStdString());
 
        AsyncPropertyRequest request;
@@ -112,4 +208,6 @@ void Property::setType(QString t)
                propertyChanged(reply->property, reply->value,uuid());
                delete reply;
        };
+
+       routingEngine->getPropertyAsync(request);
 }
index 1803de7..7ec155e 100644 (file)
 #include "abstractsink.h"
 #include <QObject>
 #include <QVariant>
+#include <QJsonDocument>
+#include "uuidhelper.h"
+
+#include "authenticate.h"
 
 class IrcCommunication;
+class QScriptEngine;
 
 class Property: public QObject, public AbstractSink
 {
@@ -38,20 +43,27 @@ public:
        QString type();
        void setType(QString t);
 
-       virtual PropertyList subscriptions();
-       virtual void supportedChanged(PropertyList supportedProperties);
+       virtual PropertyList subscriptions() { PropertyList list; list.push_back(type().toStdString()); return list; }
+       virtual void supportedChanged(PropertyList ){   }
 
        virtual void propertyChanged(VehicleProperty::Property property, AbstractPropertyType* value, std::string uuid)
        {
                mValue = value->copy();
+
+               QJsonDocument doc;
+
+               doc.fromJson(mValue->toString().c_str());
+
+               changed(doc.toVariant());
        }
 
-       virtual std::string uuid();
+       virtual std::string uuid() { return amb::createUuid(); }
 
        QVariant value();
        void setValue(QVariant v);
 
 Q_SIGNALS:
+
        void changed(QVariant val);
 
 private:
@@ -63,7 +75,7 @@ class BluemonkeySink : public QObject, public AbstractSink
 {
 Q_OBJECT
 public:
-       BluemonkeySink(AbstractRoutingEngine* engine, map<string, string> config);
+       BluemonkeySink(AbstractRoutingEngine* e, map<string, string> config);
        virtual PropertyList subscriptions();
        virtual void supportedChanged(PropertyList supportedProperties);
        virtual void propertyChanged(VehicleProperty::Property property, AbstractPropertyType* value, std::string uuid);
@@ -73,11 +85,17 @@ public Q_SLOTS:
 
        QObject* subscribeTo(QString str);
 
+       bool authenticate(QString pass);
+
+       void loadConfig(QString str);
+
 Q_SIGNALS:
 
 
 private:
        IrcCommunication* irc;
+       QScriptEngine* engine;
+       Authenticate* auth;
 };
 
 class BluemonkeySinkManager: public AbstractSinkManager