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