fixed up example
[profile/ivi/automotive-message-broker.git] / plugins / database / databasesink.cpp
1 #include "databasesink.h"
2 #include "abstractroutingengine.h"
3 #include "listplusplus.h"
4
5 extern "C" AbstractSinkManager * create(AbstractRoutingEngine* routingengine, map<string, string> config)
6 {
7         return new DatabaseSinkManager(routingengine, config);
8 }
9
10 void * cbFunc(gpointer data)
11 {
12         Shared *shared = static_cast<Shared*>(data);
13
14         if(!shared)
15         {
16                 throw std::runtime_error("Could not cast shared object.");
17         }
18
19         while(1)
20         {
21                 DBObject* obj = shared->queue.pop();
22
23                 if( obj->quit )
24                 {
25                         delete obj;
26                         break;
27                 }
28
29                 DictionaryList<string> dict;
30
31                 NameValuePair<string> one("key", obj->key);
32                 NameValuePair<string> two("value", obj->value);
33                 NameValuePair<string> three("source", obj->source);
34                 NameValuePair<string> four("time", boost::lexical_cast<string>(obj->time));
35                 NameValuePair<string> five("sequence", boost::lexical_cast<string>(obj->sequence));
36
37                 dict.push_back(one);
38                 dict.push_back(two);
39                 dict.push_back(three);
40                 dict.push_back(four);
41                 dict.push_back(five);
42
43                 shared->db->insert(dict);
44                 delete obj;
45         }
46
47         return NULL;
48 }
49
50 int getNextEvent(gpointer data)
51 {
52         PlaybackShared* pbshared = static_cast<PlaybackShared*>(data);
53
54         if(!pbshared)
55                 throw std::runtime_error("failed to cast PlaybackShared object");
56
57         auto itr = pbshared->playbackQueue.begin();
58
59         if(itr == pbshared->playbackQueue.end())
60         {
61                 return 0;
62         }
63
64         DBObject* obj = *itr;
65
66         AbstractPropertyType* value = VehicleProperty::getPropertyTypeForPropertyNameValue(obj->key,obj->value);
67
68         if(value)
69         {
70                 pbshared->routingEngine->updateProperty(obj->key, value, pbshared->uuid);
71                 value->timestamp = obj->time;
72                 value->sequence = obj->sequence;
73         }
74
75         if(++itr != pbshared->playbackQueue.end())
76         {
77                 DBObject *o2 = *itr;
78                 double t = o2->time - obj->time;
79
80                 if(t > 0)
81                         g_timeout_add((t*1000) / pbshared->playBackMultiplier, getNextEvent, pbshared);
82                 else
83                         g_timeout_add(t, getNextEvent, pbshared);
84         }
85
86         pbshared->playbackQueue.remove(obj);
87         delete obj;
88
89         return 0;
90 }
91
92 DatabaseSink::DatabaseSink(AbstractRoutingEngine *engine, map<std::string, std::string> config)
93         :AbstractSource(engine,config),thread(NULL),shared(NULL),playback(false),playbackShared(NULL), playbackMultiplier(1)
94 {
95         databaseName = "storage";
96         tablename = "data";
97         tablecreate = "CREATE TABLE IF NOT EXISTS data (key TEXT, value BLOB, source TEXT, time REAL, sequence REAL)";
98
99         //startDb();
100
101         if(config.find("startOnLoad")!= config.end())
102         {
103                 startDb();
104         }
105
106         if(config.find("playbackMultiplier")!= config.end())
107         {
108                 playbackMultiplier = boost::lexical_cast<uint>(config["playbackMultiplier"]);
109         }
110
111         if(config.find("playbackOnLoad")!= config.end())
112         {
113                 startPlayback();
114         }
115
116         if(config.find("databaseFile") != config.end())
117         {
118                 databaseName = config["databaseFile"];
119         }
120
121         if(config.find("properties") != config.end())
122         {
123                 parseConfig();
124         }
125
126         for(auto itr=propertiesToSubscribeTo.begin();itr!=propertiesToSubscribeTo.end();itr++)
127         {
128                 engine->subscribeToProperty(*itr,this);
129         }
130
131         mSupported.push_back(DatabaseFileProperty);
132         mSupported.push_back(DatabaseLoggingProperty);
133         mSupported.push_back(DatabasePlaybackProperty);
134
135         routingEngine->setSupported(mSupported,this);
136
137 }
138
139 DatabaseSink::~DatabaseSink()
140 {
141         if(shared)
142         {
143                 DBObject* obj = new DBObject();
144                 obj->quit = true;
145
146                 shared->queue.append(obj);
147
148                 g_thread_join(thread);
149                 g_thread_unref(thread);
150                 delete shared;
151         }
152
153         if(playbackShared)
154         {
155                 delete playbackShared;
156         }
157 }
158
159
160 void DatabaseSink::supportedChanged(PropertyList supportedProperties)
161 {
162
163 }
164
165 PropertyList DatabaseSink::supported()
166 {
167         return mSupported;
168 }
169
170 void DatabaseSink::parseConfig()
171 {
172         json_object *rootobject;
173         json_tokener *tokener = json_tokener_new();
174         enum json_tokener_error err;
175         do
176         {
177                 rootobject = json_tokener_parse_ex(tokener, configuration["properties"].c_str(),configuration["properties"].size());
178         } while ((err = json_tokener_get_error(tokener)) == json_tokener_continue);
179         if (err != json_tokener_success)
180         {
181                 fprintf(stderr, "Error: %s\n", json_tokener_error_desc(err));
182         }
183         if (tokener->char_offset < configuration["properties"].size()) // XXX shouldn't access internal fields
184         {
185                 //Should handle the extra data here sometime...
186         }
187         
188         json_object *propobject = json_object_object_get(rootobject,"properties");
189         
190         g_assert(json_object_get_type(propobject) == json_type_array);
191
192         array_list *proplist = json_object_get_array(propobject);
193         
194         for(int i=0; i < array_list_length(proplist); i++)
195         {
196                 json_object *idxobj = (json_object*)array_list_get_idx(proplist,i);
197                 std::string prop = json_object_get_string(idxobj);
198                 propertiesToSubscribeTo.push_back(prop);
199
200                 DebugOut()<<"DatabaseSink logging: "<<prop<<endl;
201         }
202
203         json_object_put(propobject);
204         json_object_put(rootobject);
205 }
206
207 void DatabaseSink::stopDb()
208 {
209         if(!shared)
210                 return;
211
212         DBObject *obj = new DBObject();
213         obj->quit = true;
214         shared->queue.append(obj);
215
216         g_thread_join(thread);
217
218         delete shared;
219         shared = NULL;
220 }
221
222 void DatabaseSink::startDb()
223 {
224         if(playback)
225         {
226                 DebugOut(0)<<"ERROR: tried to start logging during playback.  Only logging or playback can be used at one time"<<endl;
227                 return;
228         }
229
230         if(shared)
231         {
232                 DebugOut(0)<<"WARNING: logging already started.  doing nothing."<<endl;
233                 return;
234         }
235
236         initDb();
237
238 //      thread = g_thread_new("dbthread", cbFunc, shared);
239 }
240
241 void DatabaseSink::startPlayback()
242 {
243         if(playback)
244                 return;
245
246         playback = true;
247
248         initDb();
249
250         /// get supported:
251
252         vector<vector<string> > supportedStr = shared->db->select("SELECT DISTINCT key FROM "+tablename);
253
254         for(int i=0; i < supportedStr.size(); i++)
255         {
256                 if(!ListPlusPlus<VehicleProperty::Property>(&mSupported).contains(supportedStr[i][0]))
257                         mSupported.push_back(supportedStr[i][0]);
258         }
259
260         routingEngine->setSupported(supported(), this);
261
262         /// populate playback queue:
263
264         vector<vector<string> > results = shared->db->select("SELECT * FROM "+tablename);
265
266         if(playbackShared)
267         {
268                 delete playbackShared;
269         }
270
271         playbackShared = new PlaybackShared(routingEngine, uuid(), playbackMultiplier);
272
273         for(int i=0;i<results.size();i++)
274         {
275                 if(results[i].size() < 5)
276                 {
277                         throw std::runtime_error("column mismatch in query");
278                 }
279
280                 DBObject* obj = new DBObject();
281
282                 obj->key = results[i][0];
283                 obj->value = results[i][1];
284                 obj->source = results[i][2];
285                 obj->time = boost::lexical_cast<double>(results[i][3]);
286 //              obj->sequence = boost::lexical_cast<int>(results[i][4]);
287
288                 playbackShared->playbackQueue.push_back(obj);
289         }
290
291         g_timeout_add(0,getNextEvent,playbackShared);
292 }
293
294 void DatabaseSink::initDb()
295 {
296         if(shared) delete shared;
297
298         shared = new Shared;
299         shared->db->init(databaseName, tablename, tablecreate);
300 }
301
302 void DatabaseSink::propertyChanged(VehicleProperty::Property property, AbstractPropertyType *value, std::string uuid)
303 {
304         if(!shared)
305                 return;
306
307         DBObject* obj = new DBObject;
308         obj->key = property;
309         obj->value = value->toString();
310         obj->source = uuid;
311         obj->time = value->timestamp;
312         obj->sequence = value->sequence;
313
314         shared->queue.append(obj);
315 }
316
317
318 std::string DatabaseSink::uuid()
319 {
320         return "9f88156e-cb92-4472-8775-9c08addf50d3";
321 }
322
323 void DatabaseSink::getPropertyAsync(AsyncPropertyReply *reply)
324 {
325         reply->success = false;
326
327         if(reply->property == DatabaseFileProperty)
328         {
329                 StringPropertyType temp(databaseName);
330                 reply->value = &temp;
331
332                 reply->success = true;
333                 reply->completed(reply);
334
335                 return;
336         }
337         else if(reply->property == DatabaseLoggingProperty)
338         {
339                 BasicPropertyType<bool> temp = shared;
340
341                 reply->value = &temp;
342                 reply->success = true;
343                 reply->completed(reply);
344
345                 return;
346         }
347
348         else if(reply->property == DatabasePlaybackProperty)
349         {
350                 BasicPropertyType<bool> temp = playback;
351                 reply->value = &temp;
352                 reply->success = true;
353                 reply->completed(reply);
354
355                 return;
356         }
357
358         reply->completed(reply);
359 }
360
361 void DatabaseSink::getRangePropertyAsync(AsyncRangePropertyReply *reply)
362 {
363         BaseDB * db = new BaseDB();
364         db->init(databaseName, tablename, tablecreate);
365
366         ostringstream query;
367         query.precision(15);
368
369         query<<"SELECT * from "<<tablename<<" WHERE ";
370
371         if(reply->timeBegin && reply->timeEnd)
372         {
373                 query<<" time BETWEEN "<<reply->timeBegin<<" AND "<<reply->timeEnd;
374         }
375
376         if(reply->sequenceBegin >= 0 && reply->sequenceEnd >=0)
377         {
378                 query<<" AND sequence BETWEEN "<<reply->sequenceBegin<<" AND "<<reply->sequenceEnd;
379         }
380
381         std::vector<std::vector<string>> data = db->select(query.str());
382
383         std::list<AbstractPropertyType*> cleanup;
384
385         for(auto i=0;i<data.size();i++)
386         {
387                 if(data[i].size() != 5)
388                         continue;
389
390                 DBObject dbobj;
391                 dbobj.key = data[i][0];
392                 dbobj.value = data[i][1];
393                 dbobj.source = data[i][2];
394                 dbobj.time = boost::lexical_cast<double>(data[i][3]);
395                 dbobj.sequence = boost::lexical_cast<double>(data[i][4]);
396
397                 AbstractPropertyType* property = VehicleProperty::getPropertyTypeForPropertyNameValue(dbobj.key, dbobj.value);
398                 if(property)
399                 {
400                         property->timestamp = dbobj.time;
401                         property->sequence = dbobj.sequence;
402
403                         reply->values.push_back(property);
404                         cleanup.push_back(property);
405                 }
406         }
407
408         reply->success = true;
409         reply->completed(reply);
410
411         /// reply is owned by the requester of this call.  we own the data:
412         for(auto itr = cleanup.begin(); itr != cleanup.end(); itr++)
413         {
414                 delete *itr;
415         }
416
417         delete db;
418 }
419
420 AsyncPropertyReply *DatabaseSink::setProperty(AsyncSetPropertyRequest request)
421 {
422         AsyncPropertyReply* reply = new AsyncPropertyReply(request);
423         reply->success = false;
424
425         if(request.property == DatabaseLoggingProperty)
426         {
427                 if(request.value->value<bool>())
428                 {
429                         ///TODO: start or stop logging thread
430                         startDb();
431                         reply->success = true;
432                         BasicPropertyType<bool> temp(true);
433                         routingEngine->updateProperty(DatabaseLoggingProperty,&temp,uuid());
434                 }
435                 else
436                 {
437                         stopDb();
438                         reply->success = true;
439                         BasicPropertyType<bool> temp(false);
440                         routingEngine->updateProperty(DatabaseLoggingProperty,&temp,uuid());
441                 }
442         }
443
444         else if(request.property == DatabaseFileProperty)
445         {
446                 std::string fname = request.value->toString();
447
448                 databaseName = fname;
449
450                 StringPropertyType temp(databaseName);
451
452                 routingEngine->updateProperty(DatabaseFileProperty,&temp,uuid());
453
454                 reply->success = true;
455         }
456         else if( request.property == DatabasePlaybackProperty)
457         {
458                 if(request.value->value<bool>())
459                 {
460                         startPlayback();
461
462                         BasicPropertyType<bool> temp(true);
463
464                         routingEngine->updateProperty(DatabasePlaybackProperty,&temp,uuid());
465                 }
466                 else
467                 {
468                         /// TODO: stop playback
469
470                         BasicPropertyType<bool> temp(true);
471
472                         routingEngine->updateProperty(DatabasePlaybackProperty,&temp,uuid());
473                 }
474
475                 reply->success = true;
476         }
477
478         return reply;
479 }
480
481 void DatabaseSink::subscribeToPropertyChanges(VehicleProperty::Property )
482 {
483
484 }
485
486 void DatabaseSink::unsubscribeToPropertyChanges(VehicleProperty::Property )
487 {
488 }