summaryrefslogtreecommitdiff
path: root/plugins/database/databasesink.h
blob: 459ec9288a9e8a4344233ac070740c4fd4b1de57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
	Copyright (C) 2012  Intel Corporation

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation; either
	version 2.1 of the License, or (at your option) any later version.

	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/


#ifndef DATABASESINK_H
#define DATABASESINK_H

#include "abstractsink.h"
#include "abstractsource.h"
#include "basedb.hpp"
#include <asyncqueue.hpp>
#include "listplusplus.h"
#include "ambpluginimpl.h"

#include <glib.h>

#include <functional>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <unordered_map>

const std::string DatabaseLogging = "DatabaseLogging";
const std::string DatabasePlayback = "DatabasePlayback";
const std::string DatabaseFile = "DatabaseFile";

class DBObject {
public:
	DBObject(): zone(0), time(0), sequence(0), quit(false) {}
	std::string key;
	std::string value;
	std::string source;
	int32_t zone;
	double time;
	int32_t sequence;
	std::string tripId;

	bool quit;

	bool operator == (const DBObject & other) const
	{
		return (key == other.key && source == other.source && zone == other.zone &&
				value == other.value && sequence == other.sequence && time == other.time);
	}

	bool operator != (const DBObject & other)
	{
		return (*this == other) == false;
	}
};

namespace amb
{

struct DBObjectCompare
{
	bool operator()(DBObject const & lhs, DBObject & rhs) const
	{
		if (lhs == rhs)
		{
			return true;
		}

		return false;
	}

};

}

namespace std {
  template <> struct hash<DBObject>
  {
	size_t operator()(const DBObject & x) const
	{
	  return x.key.length() * x.value.length() + x.time;
	}
  };
}

class Shared
{
public:
	Shared()
		:queue(true)
	{
		db = new BaseDB;
	}
	~Shared()
	{
		delete db;
	}

	BaseDB * db;
	amb::Queue<DBObject, amb::DBObjectCompare> queue;
	std::string tripId;
};

class PlaybackShared
{
public:
	PlaybackShared(AbstractRoutingEngine* re, std::string u, uint playbackMult)
		:routingEngine(re),uuid(u),playBackMultiplier(playbackMult),stop(false) {}
	~PlaybackShared()
	{
		for(auto itr = playbackQueue.begin(); itr != playbackQueue.end(); itr++)
		{
			DBObject obj = *itr;
		}

		playbackQueue.clear();
	}

	AbstractRoutingEngine* routingEngine;
	std::list<DBObject> playbackQueue;
	uint playBackMultiplier;
	std::string uuid;
	bool stop;
};

PROPERTYTYPEBASIC(DatabaseLogging, bool)
PROPERTYTYPEBASIC(DatabasePlayback, bool)
PROPERTYTYPE(DatabaseFile, DatabaseFileType, StringPropertyType, std::string)

class DatabaseSink : public AmbPluginImpl
{

public:
	DatabaseSink(AbstractRoutingEngine* engine, map<string, string> config, AbstractSource &parent);
	~DatabaseSink();
	virtual void supportedChanged(const PropertyList & supportedProperties);
	virtual void propertyChanged(AbstractPropertyType *value);
	const std::string uuid() const;

	void init();

	///source role:
	virtual void getRangePropertyAsync(AsyncRangePropertyReply *reply);
	virtual AsyncPropertyReply * setProperty(const AsyncSetPropertyRequest & request);
	virtual void subscribeToPropertyChanges(VehicleProperty::Property property);
	virtual void unsubscribeToPropertyChanges(VehicleProperty::Property property);
	int supportedOperations() const { return AbstractSource::GetRanged | AbstractSource::Get | AbstractSource::Set;}

private: //methods:

	void parseConfig();
	void stopDb();
	void startDb();
	void startPlayback();
	void initDb();
		void setDatabaseFileName(std::string filename);

private:
	PropertyList mSubscriptions;
	Shared *shared;
	std::unique_ptr<std::thread> thread;
	std::string tablename;
	std::string tablecreate;
	PropertyList propertiesToSubscribeTo;
	PlaybackShared* playbackShared;
	uint playbackMultiplier;
	std::shared_ptr<AbstractPropertyType> playback;
	std::shared_ptr<AbstractPropertyType> databaseName;
	std::shared_ptr<AbstractPropertyType> databaseLogging;
};

#endif // DATABASESINK_H