implemented new abstractrouting things in core
[profile/ivi/automotive-message-broker.git] / plugins / database / databasesink.cpp
index fa28f2b..423e043 100644 (file)
@@ -2,6 +2,8 @@
 #include "abstractroutingengine.h"
 #include "listplusplus.h"
 
+int bufferLength = 100;
+
 extern "C" AbstractSinkManager * create(AbstractRoutingEngine* routingengine, map<string, string> config)
 {
        return new DatabaseSinkManager(routingengine, config);
@@ -44,7 +46,7 @@ void * cbFunc(gpointer data)
 
                insertList.push_back(dict);
 
-               if(insertList.size() > 100)
+               if(insertList.size() > bufferLength)
                {
                        shared->db->exec("BEGIN IMMEDIATE TRANSACTION");
                        for(int i=0; i< insertList.size(); i++)
@@ -126,7 +128,12 @@ DatabaseSink::DatabaseSink(AbstractRoutingEngine *engine, map<std::string, std::
 
        if(config.find("databaseFile") != config.end())
        {
-               databaseName = config["databaseFile"];
+               setDatabaseFileName(config["databaseFile"]);
+       }
+
+       if(config.find("bufferLength") != config.end())
+       {
+               bufferLength = atoi(config["bufferLength"].c_str());
        }
 
        if(config.find("properties") != config.end())
@@ -139,34 +146,15 @@ DatabaseSink::DatabaseSink(AbstractRoutingEngine *engine, map<std::string, std::
                engine->subscribeToProperty(*itr,this);
        }
 
-       mSupported.push_back(DatabaseFileProperty);
-       mSupported.push_back(DatabaseLoggingProperty);
-       mSupported.push_back(DatabasePlaybackProperty);
-
-       /// get supported:
-
-       initDb();
-
-       vector<vector<string> > supportedStr = shared->db->select("SELECT DISTINCT key FROM "+tablename);
-
-       for(int i=0; i < supportedStr.size(); i++)
-       {
-               if(!ListPlusPlus<VehicleProperty::Property>(&mSupported).contains(supportedStr[i][0]))
-                       mSupported.push_back(supportedStr[i][0]);
-       }
-
-       delete shared;
-       shared = NULL;
+       mSupported.push_back(DatabaseFile);
+       mSupported.push_back(DatabaseLogging);
+       mSupported.push_back(DatabasePlayback);
 
        routingEngine->setSupported(supported(), this);
 
        if(config.find("startOnLoad")!= config.end())
        {
-               AsyncSetPropertyRequest request;
-               request.property = DatabaseLoggingProperty;
-               request.value = new DatabaseLoggingType(true);
-
-               setProperty(request);
+               setLogging(true);
        }
 
        if(config.find("playbackMultiplier")!= config.end())
@@ -176,11 +164,7 @@ DatabaseSink::DatabaseSink(AbstractRoutingEngine *engine, map<std::string, std::
 
        if(config.find("playbackOnLoad")!= config.end())
        {
-               AsyncSetPropertyRequest request;
-               request.property = DatabasePlaybackProperty;
-               request.value = new DatabasePlaybackType(true);
-
-               setProperty(request);
+               setPlayback(true);
        }
 
 
@@ -217,6 +201,12 @@ PropertyList DatabaseSink::supported()
        return mSupported;
 }
 
+PropertyInfo DatabaseSink::getPropertyInfo(VehicleProperty::Property property)
+{
+       /// TODO: Compute update frequency for properties in the database
+       return PropertyInfo();
+}
+
 void DatabaseSink::parseConfig()
 {
        json_object *rootobject;
@@ -347,7 +337,7 @@ void DatabaseSink::initDb()
 void DatabaseSink::setPlayback(bool v)
 {
        AsyncSetPropertyRequest request;
-       request.property = DatabasePlaybackProperty;
+       request.property = DatabasePlayback;
        request.value = new DatabasePlaybackType(v);
 
        setProperty(request);
@@ -356,17 +346,43 @@ void DatabaseSink::setPlayback(bool v)
 void DatabaseSink::setLogging(bool b)
 {
        AsyncSetPropertyRequest request;
-       request.property = DatabaseLoggingProperty;
+       request.property = DatabaseLogging;
        request.value = new DatabaseLoggingType(b);
 
        setProperty(request);
 }
 
+void DatabaseSink::setDatabaseFileName(string filename)
+{
+       databaseName = filename;
+
+       initDb();
+
+       vector<vector<string> > supportedStr = shared->db->select("SELECT DISTINCT key FROM "+tablename);
+
+       for(int i=0; i < supportedStr.size(); i++)
+       {
+               if(!ListPlusPlus<VehicleProperty::Property>(&mSupported).contains(supportedStr[i][0]))
+                       mSupported.push_back(supportedStr[i][0]);
+       }
+
+       delete shared;
+       shared = NULL;
+
+       routingEngine->setSupported(mSupported, this);
+}
+
 void DatabaseSink::propertyChanged(VehicleProperty::Property property, AbstractPropertyType *value, std::string uuid)
 {
        if(!shared)
                return;
 
+       if(!ListPlusPlus<VehicleProperty::Property>(&mSupported).contains(property))
+       {
+               mSupported.push_back(property);
+               routingEngine->setSupported(mSupported, this);
+       }
+
        DBObject* obj = new DBObject;
        obj->key = property;
        obj->value = value->toString();
@@ -387,9 +403,9 @@ void DatabaseSink::getPropertyAsync(AsyncPropertyReply *reply)
 {
        reply->success = false;
 
-       if(reply->property == DatabaseFileProperty)
+       if(reply->property == DatabaseFile)
        {
-               DatabaseFilePropertyType temp(databaseName);
+               DatabaseFileType temp(databaseName);
                reply->value = &temp;
 
                reply->success = true;
@@ -397,9 +413,9 @@ void DatabaseSink::getPropertyAsync(AsyncPropertyReply *reply)
 
                return;
        }
-       else if(reply->property == DatabaseLoggingProperty)
+       else if(reply->property == DatabaseLogging)
        {
-               BasicPropertyType<bool> temp = shared;
+               DatabaseLoggingType temp = shared;
 
                reply->value = &temp;
                reply->success = true;
@@ -408,9 +424,9 @@ void DatabaseSink::getPropertyAsync(AsyncPropertyReply *reply)
                return;
        }
 
-       else if(reply->property == DatabasePlaybackProperty)
+       else if(reply->property == DatabasePlayback)
        {
-               BasicPropertyType<bool> temp = playback;
+               DatabasePlaybackType temp = playback;
                reply->value = &temp;
                reply->success = true;
                reply->completed(reply);
@@ -429,7 +445,7 @@ void DatabaseSink::getRangePropertyAsync(AsyncRangePropertyReply *reply)
        ostringstream query;
        query.precision(15);
 
-       query<<"SELECT * from "<<tablename<<" WHERE ";
+       query<<"SELECT * from "<<tablename<<" WHERE key='"<<reply->property<<"' AND";
 
        if(reply->timeBegin && reply->timeEnd)
        {
@@ -479,47 +495,47 @@ AsyncPropertyReply *DatabaseSink::setProperty(AsyncSetPropertyRequest request)
        AsyncPropertyReply* reply = new AsyncPropertyReply(request);
        reply->success = false;
 
-       if(request.property == DatabaseLoggingProperty)
+       if(request.property == DatabaseLogging)
        {
                if(request.value->value<bool>())
                {
                        setPlayback(false);
                        startDb();
                        reply->success = true;
-                       BasicPropertyType<bool> temp(true);
-                       routingEngine->updateProperty(DatabaseLoggingProperty,&temp,uuid());
+                       DatabaseLoggingType temp(true);
+                       routingEngine->updateProperty(DatabaseLogging,&temp,uuid());
                }
                else
                {
                        stopDb();
                        reply->success = true;
-                       BasicPropertyType<bool> temp(false);
-                       routingEngine->updateProperty(DatabaseLoggingProperty,&temp,uuid());
+                       DatabaseLoggingType temp(false);
+                       routingEngine->updateProperty(DatabaseLogging,&temp,uuid());
                }
        }
 
-       else if(request.property == DatabaseFileProperty)
+       else if(request.property == DatabaseFile)
        {
                std::string fname = request.value->toString();
 
                databaseName = fname;
 
-               StringPropertyType temp(databaseName);
+               DatabaseFileType temp(databaseName);
 
-               routingEngine->updateProperty(DatabaseFileProperty,&temp,uuid());
+               routingEngine->updateProperty(DatabaseFile,&temp,uuid());
 
                reply->success = true;
        }
-       else if( request.property == DatabasePlaybackProperty)
+       else if( request.property == DatabasePlayback)
        {
                if(request.value->value<bool>())
                {
                        setLogging(false);
                        startPlayback();
 
-                       BasicPropertyType<bool> temp(playback);
+                       DatabasePlaybackType temp(playback);
 
-                       routingEngine->updateProperty(DatabasePlaybackProperty,&temp,uuid());
+                       routingEngine->updateProperty(DatabasePlayback,&temp,uuid());
                }
                else
                {
@@ -528,9 +544,9 @@ AsyncPropertyReply *DatabaseSink::setProperty(AsyncSetPropertyRequest request)
 
                        playback = false;
 
-                       BasicPropertyType<bool> temp(playback);
+                       DatabasePlaybackType temp(playback);
 
-                       routingEngine->updateProperty(DatabasePlaybackProperty, &temp, uuid());
+                       routingEngine->updateProperty(DatabasePlayback, &temp, uuid());
                }
 
                reply->success = true;