5#ifndef CCFCOMMON_DB_HPP_
6#define CCFCOMMON_DB_HPP_
11#include <mal/utility/LoadMal.hpp>
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>
21#include <ifw/core/utils/bat/dbInterface.hpp>
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));
52 ifw::core::utils::conversion::Convert(m_db[complete_key], value);
55 template<
typename TYPE>
56 void Set(
const std::string& db_key,
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);
65 std::map<std::string, std::string>
Scan(
const std::string& key,
const std::string& pattern);
72 std::map<std::string, std::string> m_db;
99 std::string
GenDbPath(
const std::vector<std::string>& db_key_els);
102 std::map<std::string, std::string>
Scan(
const std::string& key,
const std::string& pattern);
127 template <
class TYPE>
128 TYPE
Get(
const std::string& db_key) {
129 LOG4CPLUS_TRACE_METHOD(
Logger(), __PRETTY_FUNCTION__);
131 if (db_key.find(m_prefix) != std::string::npos) {
132 CCFTHROW(fmt::format(
"OLDB keys shall be given without prefix: {}", db_key));
134 BEGIN_CRIT_SEC(
"ifw::ccf::common::Db") {
136 m_cii_db.get()->Get(db_key, tmp_value);
138 m_internal_db.get()->Get(db_key, tmp_value);
145 template <
class TYPE>
146 TYPE
Get2(
const std::vector<std::string>& db_key_els) {
147 LOG4CPLUS_TRACE_METHOD(
Logger(), __PRETTY_FUNCTION__);
155 template <
class TYPE>
160 if (
db_key.find(m_prefix) != std::string::npos) {
161 CCFTHROW(fmt::format(
"OLDB keys shall be given without prefix: {}",
db_key));
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;
168 it = m_last_update_map.find(
db_key);
170 double time_now = ifw::core::utils::time::Time();
187 template <
class TYPE>
198 const std::string&
value,
204 const std::string&
value,
209 const std::string&
value,
210 const ifw::fnd::datatype::DataType data_type,
215 const std::string&
value,
216 const ifw::fnd::datatype::DataType data_type,
226 ifw::core::utils::bat::DbInterface&
GetBatDb();
234 static bool s_simulation;
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;
240 double m_max_update_period{0.5};
241 std::map<std::string, double> m_last_update_map;
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;
247 std::string m_current_status;
248 std::string m_sm_state;
249 std::string m_sm_status_state;
250 std::string m_sm_status_substate;
252 std::string m_server_id;
#define CCFTHROW(msg)
Definition base.hpp:366
Interface to the OLDB and Pub/Sub.
Definition db.hpp:79
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
ifw::fnd::datatype::DataType ParToIfwDataType(const std::string &par)
Attempt to determine the native type of the parameter contained in the parameter object.
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