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