ifw-ccf 5.0.2
Loading...
Searching...
No Matches
db.hpp
Go to the documentation of this file.
1
5#ifndef CCFCOMMON_DB_HPP_
6#define CCFCOMMON_DB_HPP_
7
8#include <string>
9
10#include <mal/Cii.hpp>
11#include <mal/utility/LoadMal.hpp>
12
13#include <rad/topicPub.hpp>
14#include <rad/mal/publisher.hpp>
15#include <rad/dbAdapterRedis.hpp>
16#include <rad/dbAdapter.hpp>
17#include <rad/smAdapter.hpp>
18
19#include <Stdif.hpp>
20
21#include <ifw/core/utils/bat/dbInterface.hpp>
22
26
27
28namespace ifw::ccf::common {
29
31 ifw::fnd::datatype::DataType ParToIfwDataType(const std::string& par);
32
34 class InternalDb {
35 public:
36
37 InternalDb(const std::string& prefix);
38
40
41 inline bool IsConnected() { return m_connected; }
42
43 inline std::string GetPrefix() { return m_prefix; }
44
45 template<typename TYPE>
46 void Get(const std::string& db_key, TYPE& value) {
47 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
48 auto complete_key = ifw::core::utils::string::Lower(m_prefix + db_key);
49 if (m_db.find(complete_key) == m_db.end()) {
50 CCFTHROW(fmt::format("Key given not defined in the OLDB: {}", db_key));
51 }
52 ifw::core::utils::conversion::Convert(m_db[complete_key], value);
53 }
54
55 template<typename TYPE>
56 void Set(const std::string& db_key,
57 const TYPE& value) {
58 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
59 LOG4CPLUS_DEBUG(Logger(), fmt::format("Writing key: {}={} in internal DB",
60 m_prefix + db_key, value));
61 auto complete_key = ifw::core::utils::string::Lower(m_prefix + db_key);
62 m_db[complete_key] = boost::lexical_cast<std::string>(value);
63 }
64
65 std::map<std::string, std::string> Scan(const std::string& key, const std::string& pattern);
66
67 InternalDb(const InternalDb&) = delete;
68 InternalDb& operator=(const InternalDb&) = delete;
69
70 protected:
71 private:
72 std::map<std::string, std::string> m_db;
73 std::string m_prefix;
74 bool m_connected;
75 };
76
77
79 class Db {
80 public:
81
83 static Db* s_instance;
84
86 static Db& Instance();
87
89 static void Simulate();
90
91 static bool IsSimulated();
92
94 Db();
95
96 virtual ~Db();
97
99 std::string GenDbPath(const std::vector<std::string>& db_key_els);
100
102 std::map<std::string, std::string> Scan(const std::string& key, const std::string& pattern);
103
105 bool IsConnected();
106
108 std::string GetSmState();
109
111 std::string GetSmStatusState();
112
114 std::string GetSmStatusSubstate();
115
116 // TODO: Check if possible to use the one from util::bat::DbInterface.
118 void UpdateSmStatus(std::list<scxml4cpp::State*>& status);
119
121 void UpdateSmStatus(const std::string& status);
122
124 void PublishRecStatus(const recif::RecStatus& rec_status);
125
127 template <class TYPE>
128 TYPE Get(const std::string& db_key) {
129 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
130 TYPE tmp_value;
131 if (db_key.find(m_prefix) != std::string::npos) {
132 CCFTHROW(fmt::format("OLDB keys shall be given without prefix: {}", db_key));
133 }
134 BEGIN_CRIT_SEC("ifw::ccf::common::Db") {
135 if (!s_simulation) {
136 m_cii_db.get()->Get(db_key, tmp_value);
137 } else {
138 m_internal_db.get()->Get(db_key, tmp_value);
139 }
140 } END_CRIT_SEC();
141 return tmp_value;
142 }
143
145 template <class TYPE>
146 TYPE Get2(const std::vector<std::string>& db_key_els) {
147 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
149 }
150
152 std::string GetAsStr(const std::string& db_key);
153
155 template <class TYPE>
156 void Set(const std::string& db_key,
157 const TYPE value,
158 const bool force_update = false) {
160 if (db_key.find(m_prefix) != std::string::npos) {
161 CCFTHROW(fmt::format("OLDB keys shall be given without prefix: {}", db_key));
162 }
163
164 auto it = m_last_update_map.find(db_key);
165 if (it == m_last_update_map.end()) {
166 m_last_update_map[db_key] = 0;
167 }
168 it = m_last_update_map.find(db_key);
169
170 double time_now = ifw::core::utils::time::Time();
171 if (!force_update && (time_now - it->second) < m_max_update_period) {
172 return;
173 }
174
175 BEGIN_CRIT_SEC("ifw::ccf::common::Db") {
176 if (!s_simulation) {
177 m_cii_db.get()->Set(db_key, value);
178 } else {
179 m_internal_db.get()->Set(db_key, value);
180 }
181 } END_CRIT_SEC();
182
183 m_last_update_map[db_key] = time_now;
184 }
185
187 template <class TYPE>
188 void Set2(const std::vector<std::string>& db_key_els,
189 const TYPE value,
190 const bool force_update = false) {
193 }
194
196 void SetFromStr(const std::string& db_key,
197 const std::string& par_name,
198 const std::string& value,
199 const bool force_update = false);
200
202 void SetFromStr2(const std::vector<std::string>& db_key_els,
203 const std::string& par_name,
204 const std::string& value,
205 const bool force_update = false);
206
208 void SetFromStr(const std::string& db_key,
209 const std::string& value,
210 const ifw::fnd::datatype::DataType data_type,
211 const bool force_update = false);
212
214 void SetFromStr2(const std::vector<std::string>& db_key_els,
215 const std::string& value,
216 const ifw::fnd::datatype::DataType data_type,
217 const bool force_update = false);
218
220 void TestConnection();
221
224
226 ifw::core::utils::bat::DbInterface& GetBatDb();
227
228 Db(const Db&) = delete;
229 Db& operator=(const Db&) = delete;
230
231 protected:
232
233 private:
234 static bool s_simulation;
235
236 std::string m_prefix;
237 std::unique_ptr<ifw::core::utils::bat::DbInterface> m_cii_db;
238 std::unique_ptr<InternalDb> m_internal_db;
239
240 double m_max_update_period{0.5};
241 std::map<std::string, double> m_last_update_map;
242
243 bool m_with_pubsub;
244 std::unique_ptr<rad::cii::Publisher<stdif::Status>> m_status_publisher;
245 std::unique_ptr<rad::cii::Publisher<recif::RecStatus>> m_rec_status_publisher;
246
247 std::string m_current_status; // Current state + substate.
248 std::string m_sm_state;
249 std::string m_sm_status_state;
250 std::string m_sm_status_substate;
251
252 std::string m_server_id;
253 };
254
255} // namespace ifw::ccf::common
256
257
258namespace ifw::ccf {
263}
264
265#endif // CCFCOMMON_DB_HPP_
#define CCFTHROW(msg)
Definition base.hpp:366
Interface to the OLDB and Pub/Sub.
Definition db.hpp:79
Db(const Db &)=delete
static void Simulate()
Put the object in simulation, using an internal map as DB.
Definition db.cpp:229
void PublishRecStatus(const recif::RecStatus &rec_status)
Update the current Recording Status in Pub/Sub.
Definition db.cpp:213
std::string GetAsStr(const std::string &db_key)
Get the attribute as string, in case the type is not know.
Definition db.cpp:382
void Set2(const std::vector< std::string > &db_key_els, const TYPE value, const bool force_update=false)
Set the referenced key to the given value.
Definition db.hpp:188
virtual ~Db()
Definition db.cpp:108
void SetFromStr2(const std::vector< std::string > &db_key_els, const std::string &par_name, const std::string &value, const bool force_update=false)
Write the parameter, which values is provided as a string, to the OLDB.
Definition db.cpp:363
Db & operator=(const Db &)=delete
Disable copy constructor.
static Db * s_instance
Singleton instance.
Definition db.hpp:83
void TestConnection()
Test if the connection to the OLDB is working properly.
Definition db.cpp:239
void SetFromStr(const std::string &db_key, const std::string &par_name, const std::string &value, const bool force_update=false)
Write the parameter, which values is provided as a string, to the OLDB.
Definition db.cpp:317
static bool IsSimulated()
Definition db.cpp:234
void UpdateStagingSetupInDb()
Update the current Staging Setup buffered in the DB.
Definition db.cpp:371
std::string GenDbPath(const std::vector< std::string > &db_key_els)
Generate proper DB name.
Definition db.cpp:127
void UpdateSmStatus(std::list< scxml4cpp::State * > &status)
Update the state/substate/status in the OLDB/PubSub.
Definition db.cpp:163
std::string GetSmStatusSubstate()
Definition db.cpp:122
std::map< std::string, std::string > Scan(const std::string &key, const std::string &pattern)
Scan the namespace of the associated DB, applying the key and pattern.
Definition db.cpp:152
std::string GetSmState()
Definition db.cpp:112
ifw::core::utils::bat::DbInterface & GetBatDb()
Get reference to BAT DB handle.
Definition db.cpp:446
static Db & Instance()
Return reference to Singleton instance of the application class.
Definition db.cpp:40
bool IsConnected()
Check if connected to the associated database(s).
Definition db.cpp:220
TYPE Get2(const std::vector< std::string > &db_key_els)
Get value for the given key.
Definition db.hpp:146
TYPE Get(const std::string &db_key)
Get value for the given key.
Definition db.hpp:128
Db()
Constructor. The prefix indicates location in database of this application.
Definition db.cpp:59
void Set(const std::string &db_key, const TYPE value, const bool force_update=false)
Set the referenced key to the given value.
Definition db.hpp:156
std::string GetSmStatusState()
Definition db.cpp:117
DB implementing the ifw::core::utils::bat::DbInterface; used for internal/test purposes.
Definition db.hpp:34
InternalDb & operator=(const InternalDb &)=delete
Disable copy constructor.
InternalDb(const InternalDb &)=delete
void Set(const std::string &db_key, const TYPE &value)
Definition db.hpp:56
bool IsConnected()
Definition db.hpp:41
~InternalDb()
Definition db.hpp:39
std::map< std::string, std::string > Scan(const std::string &key, const std::string &pattern)
Definition db.cpp:456
void Get(const std::string &db_key, TYPE &value)
Definition db.hpp:46
std::string GetPrefix()
Definition db.hpp:43
InternalDb(const std::string &prefix)
Definition db.cpp:451
Definition appBase.cpp:9
ifw::fnd::datatype::DataType ParToIfwDataType(const std::string &par)
Attempt to determine the native type of the parameter contained in the parameter object.
Definition appBase.cpp:9
log4cplus::Logger & Logger()
Definition base.cpp:23
ifw::ccf::common::Db & GetDb()
Return the reference to the CCF DB Singleton instance.
Definition db.hpp:260