1 #include "databasesink.h"
2 #include "abstractroutingengine.h"
4 extern "C" AbstractSinkManager * create(AbstractRoutingEngine* routingengine, map<string, string> config)
6 return new DatabaseSinkManager(routingengine, config);
9 DatabaseSink::DatabaseSink(AbstractRoutingEngine *engine, map<std::string, std::string> config)
10 :AbstractSource(engine,config)
12 databaseName = "storage";
14 tablecreate = "CREATE TABLE IF NOT EXISTS data (key TEXT, value BLOB, source TEXT, time REAL, sequence REAL)";
16 shared->db->init(databaseName, tablename, tablecreate);
18 engine->subscribeToProperty(VehicleProperty::EngineSpeed, this);
19 engine->subscribeToProperty(VehicleProperty::VehicleSpeed, this);
22 props.push_back(VehicleProperty::EngineSpeed);
23 props.push_back(VehicleProperty::VehicleSpeed);
25 engine->setSupported(supported(),this);
27 auto cb = [](gpointer data)
29 Shared *shared = (Shared*)data;
33 DBObject* obj = shared->queue.pop();
40 DictionaryList<string> dict;
42 NameValuePair<string> one("key", obj->key);
43 NameValuePair<string> two("value", obj->value);
44 NameValuePair<string> three("source", obj->source);
45 NameValuePair<string> four("time", boost::lexical_cast<string>(obj->time));
46 NameValuePair<string> five("sequence", boost::lexical_cast<string>(obj->sequence));
50 dict.push_back(three);
54 shared->db->insert(dict);
62 thread = g_thread_new("dbthread", cb, shared);
66 DatabaseSink::~DatabaseSink()
68 DBObject* obj = new DBObject();
71 shared->queue.append(obj);
73 g_thread_join(thread);
79 void DatabaseSink::supportedChanged(PropertyList supportedProperties)
84 PropertyList DatabaseSink::supported()
88 props.push_back(VehicleProperty::EngineSpeed);
89 props.push_back(VehicleProperty::VehicleSpeed);
94 void DatabaseSink::propertyChanged(VehicleProperty::Property property, AbstractPropertyType *value, std::string uuid)
96 DBObject* obj = new DBObject;
98 obj->value = value->toString();
100 obj->time = value->timestamp;
101 obj->sequence = value->sequence;
103 shared->queue.append(obj);
107 std::string DatabaseSink::uuid()
109 return "9f88156e-cb92-4472-8775-9c08addf50d3";
112 void DatabaseSink::getPropertyAsync(AsyncPropertyReply *reply)
117 void DatabaseSink::getRangePropertyAsync(AsyncRangePropertyReply *reply)
119 BaseDB * db = new BaseDB();
120 db->init(databaseName, tablename, tablecreate);
125 query<<"SELECT * from "<<tablename<<" WHERE ";
127 if(reply->timeBegin && reply->timeEnd)
129 query<<" time BETWEEN "<<reply->timeBegin<<" AND "<<reply->timeEnd;
132 if(reply->sequenceBegin >= 0 && reply->sequenceEnd >=0)
134 query<<" AND sequence BETWEEN "<<reply->sequenceBegin<<" AND "<<reply->sequenceEnd;
137 std::vector<std::vector<string>> data = db->select(query.str());
139 std::list<AbstractPropertyType*> cleanup;
141 for(auto i=0;i<data.size();i++)
143 if(data[i].size() != 5)
147 dbobj.key = data[i][0];
148 dbobj.value = data[i][1];
149 dbobj.source = data[i][2];
150 dbobj.time = boost::lexical_cast<double>(data[i][3]);
151 dbobj.sequence = boost::lexical_cast<double>(data[i][4]);
153 AbstractPropertyType* property = VehicleProperty::getPropertyTypeForPropertyNameValue(dbobj.key,dbobj.value);
156 property->timestamp = dbobj.time;
157 property->sequence = dbobj.sequence;
159 reply->values.push_back(property);
160 cleanup.push_back(property);
164 reply->success = true;
165 reply->completed(reply);
167 /// reply is owned by the requester of this call. we own the data:
168 for(auto itr = cleanup.begin(); itr != cleanup.end(); itr++)
176 AsyncPropertyReply *DatabaseSink::setProperty(AsyncSetPropertyRequest request)
178 AsyncPropertyReply* reply = new AsyncPropertyReply(request);
179 reply->success = false;
183 void DatabaseSink::subscribeToPropertyChanges(VehicleProperty::Property )
188 void DatabaseSink::unsubscribeToPropertyChanges(VehicleProperty::Property )