1 #include "databasesink.h"
2 #include "abstractroutingengine.h"
4 #include <json-glib/json-glib.h>
6 extern "C" AbstractSinkManager * create(AbstractRoutingEngine* routingengine, map<string, string> config)
8 return new DatabaseSinkManager(routingengine, config);
11 DatabaseSink::DatabaseSink(AbstractRoutingEngine *engine, map<std::string, std::string> config)
12 :AbstractSource(engine,config)
14 databaseName = "storage";
16 tablecreate = "CREATE TABLE IF NOT EXISTS data (key TEXT, value BLOB, source TEXT, time REAL, sequence REAL)";
18 shared->db->init(databaseName, tablename, tablecreate);
20 auto cb = [](gpointer data)
22 Shared *shared = (Shared*)data;
26 DBObject* obj = shared->queue.pop();
33 DictionaryList<string> dict;
35 NameValuePair<string> one("key", obj->key);
36 NameValuePair<string> two("value", obj->value);
37 NameValuePair<string> three("source", obj->source);
38 NameValuePair<string> four("time", boost::lexical_cast<string>(obj->time));
39 NameValuePair<string> five("sequence", boost::lexical_cast<string>(obj->sequence));
43 dict.push_back(three);
47 shared->db->insert(dict);
55 thread = g_thread_new("dbthread", cb, shared);
59 for(auto itr=propertiesToSubscribeTo.begin();itr!=propertiesToSubscribeTo.end();itr++)
61 engine->subscribeToProperty(*itr,this);
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::parseConfig()
96 JsonParser* parser = json_parser_new();
97 GError* error = nullptr;
98 if(!json_parser_load_from_data(parser, configuration["properties"].c_str(),configuration["properties"].size(), &error))
100 DebugOut()<<"Failed to load config: "<<error->message;
101 throw std::runtime_error("Failed to load config");
104 JsonNode* node = json_parser_get_root(parser);
107 throw std::runtime_error("Unable to get JSON root object");
109 JsonReader* reader = json_reader_new(node);
111 if(reader == nullptr)
112 throw std::runtime_error("Unable to create JSON reader");
114 json_reader_read_member(reader,"properties");
116 g_assert(json_reader_is_array(reader));
118 for(int i=0; i < json_reader_count_elements(reader); i++)
120 json_reader_read_element(reader, i);
121 std::string prop = json_reader_get_string_value(reader);
122 propertiesToSubscribeTo.push_back(prop);
123 json_reader_end_element(reader);
125 DebugOut()<<"DatabaseSink logging: "<<prop<<endl;
128 if(error) g_error_free(error);
130 g_object_unref(reader);
131 g_object_unref(parser);
134 void DatabaseSink::propertyChanged(VehicleProperty::Property property, AbstractPropertyType *value, std::string uuid)
136 DBObject* obj = new DBObject;
138 obj->value = value->toString();
140 obj->time = value->timestamp;
141 obj->sequence = value->sequence;
143 shared->queue.append(obj);
147 std::string DatabaseSink::uuid()
149 return "9f88156e-cb92-4472-8775-9c08addf50d3";
152 void DatabaseSink::getPropertyAsync(AsyncPropertyReply *reply)
157 void DatabaseSink::getRangePropertyAsync(AsyncRangePropertyReply *reply)
159 BaseDB * db = new BaseDB();
160 db->init(databaseName, tablename, tablecreate);
165 query<<"SELECT * from "<<tablename<<" WHERE ";
167 if(reply->timeBegin && reply->timeEnd)
169 query<<" time BETWEEN "<<reply->timeBegin<<" AND "<<reply->timeEnd;
172 if(reply->sequenceBegin >= 0 && reply->sequenceEnd >=0)
174 query<<" AND sequence BETWEEN "<<reply->sequenceBegin<<" AND "<<reply->sequenceEnd;
177 std::vector<std::vector<string>> data = db->select(query.str());
179 std::list<AbstractPropertyType*> cleanup;
181 for(auto i=0;i<data.size();i++)
183 if(data[i].size() != 5)
187 dbobj.key = data[i][0];
188 dbobj.value = data[i][1];
189 dbobj.source = data[i][2];
190 dbobj.time = boost::lexical_cast<double>(data[i][3]);
191 dbobj.sequence = boost::lexical_cast<double>(data[i][4]);
193 AbstractPropertyType* property = VehicleProperty::getPropertyTypeForPropertyNameValue(dbobj.key, dbobj.value);
196 property->timestamp = dbobj.time;
197 property->sequence = dbobj.sequence;
199 reply->values.push_back(property);
200 cleanup.push_back(property);
204 reply->success = true;
205 reply->completed(reply);
207 /// reply is owned by the requester of this call. we own the data:
208 for(auto itr = cleanup.begin(); itr != cleanup.end(); itr++)
216 AsyncPropertyReply *DatabaseSink::setProperty(AsyncSetPropertyRequest request)
218 AsyncPropertyReply* reply = new AsyncPropertyReply(request);
219 reply->success = false;
223 void DatabaseSink::subscribeToPropertyChanges(VehicleProperty::Property )
228 void DatabaseSink::unsubscribeToPropertyChanges(VehicleProperty::Property )