ifw-fcf  4.0.0
dataContext.hpp
Go to the documentation of this file.
1 
9 #ifndef FCF_DEVMGR_COMMON_DATA_CONTEXT_HPP_
10 #define FCF_DEVMGR_COMMON_DATA_CONTEXT_HPP_
11 
12 // System headers
13 #include <unordered_map>
14 
15 // Local headers
16 //#include <rad/topicPub.hpp>
17 #include <mal/Cii.hpp>
18 #include <mal/utility/LoadMal.hpp>
19 #include <rad/topicPub.hpp>
20 #include <rad/mal/publisher.hpp>
21 #include <boost/asio.hpp>
22 #include <Stdif.hpp>
24 #include <utils/bat/dbInterfaceRedis.hpp>
25 
26 namespace fcf::devmgr::common {
27 
28  // Constants for state values
29  const short SRV_STATE_NOT_OPERATIONAL = 1;
30  const short SRV_STATE_OPERATIONAL = 2;
31 
32  // Constants for substate values
33  const short SRV_SUBSTATE_NOTREADY = 1;
34  const short SRV_SUBSTATE_READY = 2;
35  const short SRV_SUBSTATE_IDLE = 3;
36  const short SRV_SUBSTATE_INITIALISING = 4;
37  const short SRV_SUBSTATE_ENABLING = 5;
38  const short SRV_SUBSTATE_SETTING_UP = 6;
39  const short SRV_SUBSTATE_ERROR = 10;
40 
41 
42  const std::string SRV_STATE_NOT_OPERATIONAL_STR = "NotOperational";
43  const std::string SRV_STATE_OPERATIONAL_STR = "Operational";
44  const std::string SRV_STATE_UNAVAILABLE_STR = "Unavailable";
45 
46  const std::string SRV_SUBSTATE_NOTREADY_STR = "NotReady";
47  const std::string SRV_SUBSTATE_READY_STR = "Ready";
48  const std::string SRV_SUBSTATE_IDLE_STR = "Idle";
49  const std::string SRV_SUBSTATE_INITIALISING_STR = "Initialising";
50  const std::string SRV_SUBSTATE_ENABLING_STR = "Enabling";
51  const std::string SRV_SUBSTATE_ERROR_STR = "Error";
52  const std::string SRV_SUBSTATE_SETTING_UP_STR = "SettingUp";
53 
54 
55 
56  const std::unordered_map<short, std::string> SrvStateMap = {
59  };
60 
61  const std::unordered_map<std::string, short> SrvStateStrMap = {
64  };
65 
66  const std::unordered_map<short, std::string> SrvSubstateMap = {
74  };
75 
76  const std::unordered_map<std::string, short> SrvSubstateStrMap = {
84  };
85 
90  class DataContext {
91  public:
103  DataContext(Config& config,
104  boost::asio::io_service& ios,
105  bool with_publisher = true,
106  bool with_oldb = true);
107 
111  virtual ~DataContext();
112 
116  void ReloadConfig();
117 
121  void UpdateDb();
122  void UpdateDb(utils::bat::DbVector& vec);
123 
124 
128  Config& GetConfig();
129 
130  short GetState();
131  std::string GetStateStr();
132  void SetState(const short state);
133  void SetState(const std::string state);
134  short GetSubstate();
135  std::string GetSubstateStr();
136  void SetSubstate(const short state);
137  void SetSubstate(const std::string state);
138  void SetSettingState(bool new_state);
139  bool GetSettingState();
140 
141  void PublishStates();
142 
143  template<typename T>
144  void Set(const std::string& key,
145  const T& value);
146 
147 
148  DataContext(const DataContext&) = delete;
149  DataContext& operator=(const DataContext&) = delete;
150 
151  private:
152  Config& m_config;
153  std::unique_ptr<utils::bat::DbInterfaceRedis> m_db_interface;
154 
156  boost::asio::io_service& m_ios;
160  std::unique_ptr<rad::cii::Publisher<stdif::Status>> m_publisher;
161 
162  short m_state;
163  short m_substate;
164  std::string m_state_str;
165  std::string m_substate_str;
166 
170  bool m_setting_state {false};
171  log4cplus::Logger m_logger;
172  };
173 
174  template<typename T>
175  void DataContext::Set(const std::string& key, const T& value) {
176  if (m_db_interface) {
177  m_db_interface->Set<T>(key, value);
178  }
179  }
180 
181 } // namespace devmgr
182 
183 #endif // DEVMGR_COMMON_DATA_CONTEXT_HPP_
fcf::devmgr::common::SrvSubstateStrMap
const std::unordered_map< std::string, short > SrvSubstateStrMap
Definition: dataContext.hpp:76
fcf::devmgr::common::SRV_SUBSTATE_IDLE_STR
const std::string SRV_SUBSTATE_IDLE_STR
Definition: dataContext.hpp:48
fcf::devmgr::common::SRV_STATE_NOT_OPERATIONAL
const short SRV_STATE_NOT_OPERATIONAL
Definition: dataContext.hpp:29
fcf::devmgr::common::DataContext::DataContext
DataContext(const DataContext &)=delete
Disable copy constructor.
fcf::devmgr::common::DataContext::PublishStates
void PublishStates()
Definition: dataContext.cpp:162
fcf::devmgr::common::DataContext::SetSettingState
void SetSettingState(bool new_state)
Definition: dataContext.cpp:188
fcf::devmgr::common::DataContext::Set
void Set(const std::string &key, const T &value)
Definition: dataContext.hpp:175
config.hpp
Config class header file.
fcf::devmgr::common::SRV_SUBSTATE_ERROR_STR
const std::string SRV_SUBSTATE_ERROR_STR
Definition: dataContext.hpp:51
fcf::devmgr::common::DataContext::GetState
short GetState()
Definition: dataContext.cpp:107
fcf::devmgr::common::SRV_SUBSTATE_INITIALISING_STR
const std::string SRV_SUBSTATE_INITIALISING_STR
Definition: dataContext.hpp:49
fcf::devmgr::common::SRV_SUBSTATE_NOTREADY_STR
const std::string SRV_SUBSTATE_NOTREADY_STR
Definition: dataContext.hpp:46
fcf::devmgr::common::SRV_SUBSTATE_READY_STR
const std::string SRV_SUBSTATE_READY_STR
Definition: dataContext.hpp:47
fcf::devmgr::common
Definition: actionMgr.cpp:29
fcf::devmgr::common::SRV_SUBSTATE_ENABLING
const short SRV_SUBSTATE_ENABLING
Definition: dataContext.hpp:37
fcf::devmgr::common::SRV_SUBSTATE_READY
const short SRV_SUBSTATE_READY
Definition: dataContext.hpp:34
fcf::devmgr::common::SRV_STATE_OPERATIONAL
const short SRV_STATE_OPERATIONAL
Definition: dataContext.hpp:30
fcf::devmgr::common::DataContext::SetSubstate
void SetSubstate(const short state)
Definition: dataContext.cpp:144
fcf::devmgr::common::SRV_SUBSTATE_IDLE
const short SRV_SUBSTATE_IDLE
Definition: dataContext.hpp:35
fcf::devmgr::common::SrvStateMap
const std::unordered_map< short, std::string > SrvStateMap
Definition: dataContext.hpp:56
fcf::devmgr::common::SRV_SUBSTATE_SETTING_UP_STR
const std::string SRV_SUBSTATE_SETTING_UP_STR
Definition: dataContext.hpp:52
fcf::devmgr::common::DataContext::GetConfig
Config & GetConfig()
Definition: dataContext.cpp:100
fcf::devmgr::common::DataContext::~DataContext
virtual ~DataContext()
Definition: dataContext.cpp:54
fcf::devmgr::common::DataContext::operator=
DataContext & operator=(const DataContext &)=delete
Disable assignment operator.
fcf::devmgr::common::DataContext
Definition: dataContext.hpp:90
fcf::devmgr::common::SRV_SUBSTATE_ENABLING_STR
const std::string SRV_SUBSTATE_ENABLING_STR
Definition: dataContext.hpp:50
fcf::devmgr::common::DataContext::GetSettingState
bool GetSettingState()
Definition: dataContext.cpp:193
fcf::devmgr::common::Config
Definition: config.hpp:56
fcf::devmgr::common::DataContext::UpdateDb
void UpdateDb()
Definition: dataContext.cpp:67
fcf::devmgr::common::SRV_SUBSTATE_SETTING_UP
const short SRV_SUBSTATE_SETTING_UP
Definition: dataContext.hpp:38
fcf::devmgr::common::DataContext::ReloadConfig
void ReloadConfig()
Definition: dataContext.cpp:60
fcf::devmgr::common::DataContext::SetState
void SetState(const short state)
Definition: dataContext.cpp:117
fcf::devmgr::common::SRV_STATE_NOT_OPERATIONAL_STR
const std::string SRV_STATE_NOT_OPERATIONAL_STR
Definition: dataContext.hpp:42
fcf::devmgr::common::SRV_SUBSTATE_NOTREADY
const short SRV_SUBSTATE_NOTREADY
Definition: dataContext.hpp:33
fcf::devmgr::common::DataContext::GetSubstate
short GetSubstate()
Definition: dataContext.cpp:134
fcf::devmgr::common::SRV_SUBSTATE_ERROR
const short SRV_SUBSTATE_ERROR
Definition: dataContext.hpp:39
fcf::devmgr::common::SRV_SUBSTATE_INITIALISING
const short SRV_SUBSTATE_INITIALISING
Definition: dataContext.hpp:36
fcf::devmgr::common::SrvStateStrMap
const std::unordered_map< std::string, short > SrvStateStrMap
Definition: dataContext.hpp:61
fcf::devmgr::common::SRV_STATE_OPERATIONAL_STR
const std::string SRV_STATE_OPERATIONAL_STR
Definition: dataContext.hpp:43
fcf::devmgr::common::DataContext::GetSubstateStr
std::string GetSubstateStr()
Definition: dataContext.cpp:139
fcf::devmgr::common::DataContext::GetStateStr
std::string GetStateStr()
Definition: dataContext.cpp:112
fcf::devmgr::common::DataContext::DataContext
DataContext(Config &config, boost::asio::io_service &ios, bool with_publisher=true, bool with_oldb=true)
Definition: dataContext.cpp:22
fcf::devmgr::common::SrvSubstateMap
const std::unordered_map< short, std::string > SrvSubstateMap
Definition: dataContext.hpp:66
fcf::devmgr::common::SRV_STATE_UNAVAILABLE_STR
const std::string SRV_STATE_UNAVAILABLE_STR
Definition: dataContext.hpp:44