ifw-fcf  5.0.0-pre2
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/dbInterface.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  /*
144  template<typename T>
145  void Set(const std::string& key,
146  const T& value);
147  */
148 
149  template<typename T>
150  void SetAsync(const std::string& key,
151  const T& value);
152 
153 
154  DataContext(const DataContext&) = delete;
155  DataContext& operator=(const DataContext&) = delete;
156 
157  private:
158  Config& m_config;
159  std::unique_ptr<utils::bat::DbInterface> m_db_interface;
160 
162  boost::asio::io_service& m_ios;
166  std::unique_ptr<rad::cii::Publisher<stdif::Status>> m_publisher;
167 
168  short m_state;
169  short m_substate;
170  std::string m_state_str;
171  std::string m_substate_str;
172 
176  bool m_setting_state {false};
177  log4cplus::Logger m_logger;
178  };
179 
180  /*
181  template<typename T>
182  void DataContext::Set(const std::string& key, const T& value) {
183  if (m_db_interface) {
184  m_db_interface->Set<T>(key, value);
185  }
186  }
187  */
188 
189  template<typename T>
190  void DataContext::SetAsync(const std::string& key, const T& value) {
191  if (m_db_interface) {
192  m_db_interface->SetAsync<T>(key, value);
193  }
194  }
195 
196 } // namespace devmgr
197 
198 #endif // DEVMGR_COMMON_DATA_CONTEXT_HPP_
Definition: config.hpp:56
Definition: dataContext.hpp:90
std::string GetStateStr()
Definition: dataContext.cpp:112
void SetSettingState(bool new_state)
Definition: dataContext.cpp:188
DataContext(const DataContext &)=delete
Disable copy constructor.
void ReloadConfig()
Definition: dataContext.cpp:60
std::string GetSubstateStr()
Definition: dataContext.cpp:139
short GetState()
Definition: dataContext.cpp:107
Config & GetConfig()
Definition: dataContext.cpp:100
DataContext & operator=(const DataContext &)=delete
Disable assignment operator.
void UpdateDb()
Definition: dataContext.cpp:67
void SetSubstate(const short state)
Definition: dataContext.cpp:144
void PublishStates()
Definition: dataContext.cpp:162
DataContext(Config &config, boost::asio::io_service &ios, bool with_publisher=true, bool with_oldb=true)
Definition: dataContext.cpp:22
void SetAsync(const std::string &key, const T &value)
Definition: dataContext.hpp:190
short GetSubstate()
Definition: dataContext.cpp:134
virtual ~DataContext()
Definition: dataContext.cpp:54
void SetState(const short state)
Definition: dataContext.cpp:117
bool GetSettingState()
Definition: dataContext.cpp:193
Config class header file.
Definition: actionMgr.cpp:29
const std::string SRV_SUBSTATE_SETTING_UP_STR
Definition: dataContext.hpp:52
const short SRV_SUBSTATE_ERROR
Definition: dataContext.hpp:39
const short SRV_SUBSTATE_SETTING_UP
Definition: dataContext.hpp:38
const short SRV_SUBSTATE_INITIALISING
Definition: dataContext.hpp:36
const short SRV_STATE_NOT_OPERATIONAL
Definition: dataContext.hpp:29
const std::unordered_map< std::string, short > SrvStateStrMap
Definition: dataContext.hpp:61
const std::unordered_map< std::string, short > SrvSubstateStrMap
Definition: dataContext.hpp:76
const std::string SRV_SUBSTATE_NOTREADY_STR
Definition: dataContext.hpp:46
const std::string SRV_SUBSTATE_INITIALISING_STR
Definition: dataContext.hpp:49
const short SRV_SUBSTATE_READY
Definition: dataContext.hpp:34
const std::string SRV_STATE_NOT_OPERATIONAL_STR
Definition: dataContext.hpp:42
const std::unordered_map< short, std::string > SrvSubstateMap
Definition: dataContext.hpp:66
const std::unordered_map< short, std::string > SrvStateMap
Definition: dataContext.hpp:56
const short SRV_STATE_OPERATIONAL
Definition: dataContext.hpp:30
const std::string SRV_SUBSTATE_ENABLING_STR
Definition: dataContext.hpp:50
const std::string SRV_SUBSTATE_IDLE_STR
Definition: dataContext.hpp:48
const short SRV_SUBSTATE_ENABLING
Definition: dataContext.hpp:37
const std::string SRV_SUBSTATE_ERROR_STR
Definition: dataContext.hpp:51
const short SRV_SUBSTATE_IDLE
Definition: dataContext.hpp:35
const std::string SRV_STATE_OPERATIONAL_STR
Definition: dataContext.hpp:43
const std::string SRV_SUBSTATE_READY_STR
Definition: dataContext.hpp:47
const short SRV_SUBSTATE_NOTREADY
Definition: dataContext.hpp:33
const std::string SRV_STATE_UNAVAILABLE_STR
Definition: dataContext.hpp:44