7#ifndef CCFCOMMON_DB_HPP_
8#define CCFCOMMON_DB_HPP_
13#include <mal/utility/LoadMal.hpp>
15#include <rad/topicPub.hpp>
16#include <rad/mal/publisher.hpp>
17#include <rad/dbAdapterRedis.hpp>
18#include <rad/dbAdapter.hpp>
19#include <rad/smAdapter.hpp>
23#include <ifw/core/utils/bat/dbInterface.hpp>
47 template<
typename TYPE>
48 void Get(
const std::string& db_key, TYPE& value) {
49 LOG4CPLUS_TRACE_METHOD(
Logger(), __PRETTY_FUNCTION__);
50 auto complete_key = ifw::core::utils::string::Lower(m_prefix + db_key);
51 if (m_db.find(complete_key) == m_db.end()) {
52 CCFTHROW(fmt::format(
"Key given not defined in the OLDB: {}", db_key));
54 ifw::core::utils::conversion::Convert(m_db[complete_key], value);
57 template<
typename TYPE>
58 void Set(
const std::string& db_key,
60 LOG4CPLUS_TRACE_METHOD(
Logger(), __PRETTY_FUNCTION__);
61 LOG4CPLUS_DEBUG(
Logger(), fmt::format(
"Writing key: {}={} in internal DB",
62 m_prefix + db_key, value));
63 auto complete_key = ifw::core::utils::string::Lower(m_prefix + db_key);
64 m_db[complete_key] = boost::lexical_cast<std::string>(value);
67 std::map<std::string, std::string>
Scan(
const std::string& key,
const std::string& pattern);
74 std::map<std::string, std::string> m_db;
101 std::string
GenDbPath(
const std::vector<std::string>& db_key_els);
104 std::map<std::string, std::string>
Scan(
const std::string& key,
const std::string& pattern);
129 template <
class TYPE>
130 TYPE
Get(
const std::string& db_key) {
131 LOG4CPLUS_TRACE_METHOD(
Logger(), __PRETTY_FUNCTION__);
133 if (db_key.find(m_prefix) != std::string::npos) {
134 CCFTHROW(fmt::format(
"OLDB keys shall be given without prefix: {}", db_key));
136 BEGIN_CRIT_SEC(
"ifw::ccf::common::Db") {
138 m_cii_db.get()->Get(db_key, tmp_value);
140 m_internal_db.get()->Get(db_key, tmp_value);
147 template <
class TYPE>
148 TYPE
Get2(
const std::vector<std::string>& db_key_els) {
149 LOG4CPLUS_TRACE_METHOD(
Logger(), __PRETTY_FUNCTION__);
154 std::string
GetAsStr(
const std::string& db_key);
157 template <
class TYPE>
158 void Set(
const std::string& db_key,
160 const bool force_update =
false) {
161 LOG4CPLUS_TRACE_METHOD(
Logger(), __PRETTY_FUNCTION__);
162 if (db_key.find(m_prefix) != std::string::npos) {
163 CCFTHROW(fmt::format(
"OLDB keys shall be given without prefix: {}", db_key));
166 auto it = m_last_update_map.find(db_key);
167 if (it == m_last_update_map.end()) {
168 m_last_update_map[db_key] = 0;
170 it = m_last_update_map.find(db_key);
172 double time_now = ifw::core::utils::time::Time();
173 if (!force_update && (time_now - it->second) < m_max_update_period) {
177 BEGIN_CRIT_SEC(
"ifw::ccf::common::Db") {
179 m_cii_db.get()->Set(db_key, value);
181 m_internal_db.get()->Set(db_key, value);
185 m_last_update_map[db_key] = time_now;
189 template <
class TYPE>
190 void Set2(
const std::vector<std::string>& db_key_els,
192 const bool force_update =
false) {
193 LOG4CPLUS_TRACE_METHOD(
Logger(), __PRETTY_FUNCTION__);
199 const std::string& par_name,
200 const std::string& value,
201 const bool force_update =
false);
204 void SetFromStr2(
const std::vector<std::string>& db_key_els,
205 const std::string& par_name,
206 const std::string& value,
207 const bool force_update =
false);
211 const std::string& value,
212 const ifw::fnd::datatype::DataType data_type,
213 const bool force_update =
false);
216 void SetFromStr2(
const std::vector<std::string>& db_key_els,
217 const std::string& value,
218 const ifw::fnd::datatype::DataType data_type,
219 const bool force_update =
false);
228 ifw::core::utils::bat::DbInterface&
GetBatDb();
236 static bool s_simulation;
238 std::string m_prefix;
239 std::unique_ptr<ifw::core::utils::bat::DbInterface> m_cii_db;
240 std::unique_ptr<InternalDb> m_internal_db;
242 double m_max_update_period{0.5};
243 std::map<std::string, double> m_last_update_map;
246 std::unique_ptr<rad::cii::Publisher<stdif::Status>> m_status_publisher;
247 std::unique_ptr<rad::cii::Publisher<recif::RecStatus>> m_rec_status_publisher;
249 std::string m_current_status;
250 std::string m_sm_state;
251 std::string m_sm_status_state;
252 std::string m_sm_status_substate;
254 std::string m_server_id;
#define CCFTHROW(msg)
Definition base.hpp:399
Interface to the OLDB and Pub/Sub.
Definition db.hpp:81
static void Simulate()
Put the object in simulation, using an internal map as DB.
Definition db.cpp:230
void PublishRecStatus(const recif::RecStatus &rec_status)
Update the current Recording Status in Pub/Sub.
Definition db.cpp:214
std::string GetAsStr(const std::string &db_key)
Get the attribute as string, in case the type is not know.
Definition db.cpp:383
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:190
virtual ~Db()
Definition db.cpp:109
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:364
Db & operator=(const Db &)=delete
Disable copy constructor.
static Db * s_instance
Singleton instance.
Definition db.hpp:85
void TestConnection()
Test if the connection to the OLDB is working properly.
Definition db.cpp:240
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:318
static bool IsSimulated()
Definition db.cpp:235
void UpdateStagingSetupInDb()
Update the current Staging Setup buffered in the DB.
Definition db.cpp:372
std::string GenDbPath(const std::vector< std::string > &db_key_els)
Generate proper DB name.
Definition db.cpp:128
void UpdateSmStatus(std::list< scxml4cpp::State * > &status)
Update the state/substate/status in the OLDB/PubSub.
Definition db.cpp:164
std::string GetSmStatusSubstate()
Definition db.cpp:123
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:153
std::string GetSmState()
Definition db.cpp:113
ifw::core::utils::bat::DbInterface & GetBatDb()
Get reference to BAT DB handle.
Definition db.cpp:447
static Db & Instance()
Return reference to Singleton instance of the application class.
Definition db.cpp:41
bool IsConnected()
Check if connected to the associated database(s).
Definition db.cpp:221
TYPE Get2(const std::vector< std::string > &db_key_els)
Get value for the given key.
Definition db.hpp:148
TYPE Get(const std::string &db_key)
Get value for the given key.
Definition db.hpp:130
Db()
Constructor. The prefix indicates location in database of this application.
Definition db.cpp:60
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:158
std::string GetSmStatusState()
Definition db.cpp:118
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:58
bool IsConnected()
Definition db.hpp:43
~InternalDb()
Definition db.hpp:41
std::map< std::string, std::string > Scan(const std::string &key, const std::string &pattern)
Definition db.cpp:457
void Get(const std::string &db_key, TYPE &value)
Definition db.hpp:48
std::string GetPrefix()
Definition db.hpp:45
InternalDb(const std::string &prefix)
Definition db.cpp:452
Definition appBase.cpp:10
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:10
log4cplus::Logger & Logger()
Definition base.cpp:24
ifw::ccf::common::Db & GetDb()
Return the reference to the CCF DB Singleton instance.
Definition db.hpp:262