Skip to content

Commit 04ab5d6

Browse files
committed
[centralsystem] Add possibility to start a central system without database and without timers
1 parent 74516c9 commit 04ab5d6

File tree

2 files changed

+75
-54
lines changed

2 files changed

+75
-54
lines changed

src/centralsystem/CentralSystem.cpp

Lines changed: 72 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -67,34 +67,43 @@ CentralSystem::CentralSystem(const ocpp::config::ICentralSystemConfig& st
6767
m_timer_pool(timer_pool),
6868
m_worker_pool(worker_pool),
6969
m_database(),
70-
m_internal_config(m_database),
70+
m_internal_config(),
7171
m_messages_converter(),
7272
m_messages_validator(),
7373
m_ws_server(),
7474
m_rpc_server(),
75-
m_uptime_timer(*m_timer_pool.get(), "Uptime timer"),
75+
m_uptime_timer(),
7676
m_uptime(0),
7777
m_total_uptime(0)
7878
{
7979
// Open database
80-
if (m_database.open(m_stack_config.databasePath()))
80+
if (!m_stack_config.databasePath().empty())
8181
{
82-
// Register logger
83-
if (m_stack_config.logMaxEntriesCount() != 0)
82+
m_database = std::make_unique<ocpp::database::Database>();
83+
m_internal_config = std::make_unique<ocpp::config::InternalConfigManager>(*m_database);
84+
if (m_database->open(m_stack_config.databasePath()))
8485
{
85-
ocpp::log::Logger::registerDefaultLogger(m_database, m_stack_config.logMaxEntriesCount());
86-
}
86+
// Register logger
87+
if (m_stack_config.logMaxEntriesCount() != 0)
88+
{
89+
ocpp::log::Logger::registerDefaultLogger(*m_database, m_stack_config.logMaxEntriesCount());
90+
}
8791

88-
// Initialize the database
89-
initDatabase();
90-
}
91-
else
92-
{
93-
LOG_ERROR << "Unable to open database";
92+
// Initialize the database
93+
initDatabase();
94+
}
95+
else
96+
{
97+
LOG_ERROR << "Unable to open database";
98+
}
9499
}
95100

96101
// Uptime timer
97-
m_uptime_timer.setCallback(std::bind(&CentralSystem::processUptime, this));
102+
if (m_timer_pool && m_worker_pool && m_internal_config)
103+
{
104+
m_uptime_timer = std::make_unique<ocpp::helpers::Timer>(*m_timer_pool, "Uptime timer");
105+
m_uptime_timer->setCallback(std::bind(&CentralSystem::processUptime, this));
106+
}
98107

99108
// Random numbers
100109
std::srand(static_cast<unsigned int>(time(nullptr)));
@@ -123,33 +132,36 @@ bool CentralSystem::resetData()
123132
}
124133

125134
// Close database to invalid existing connexions
126-
m_database.close();
127-
128-
// Delete database
129-
if (std::filesystem::remove(m_stack_config.databasePath()))
135+
if (m_database)
130136
{
131-
// Open database
132-
if (m_database.open(m_stack_config.databasePath()))
137+
m_database->close();
138+
139+
// Delete database
140+
if (std::filesystem::remove(m_stack_config.databasePath()))
133141
{
134-
// Register logger
135-
if (m_stack_config.logMaxEntriesCount() != 0)
142+
// Open database
143+
if (m_database->open(m_stack_config.databasePath()))
136144
{
137-
ocpp::log::Logger::registerDefaultLogger(m_database, m_stack_config.logMaxEntriesCount());
145+
// Register logger
146+
if (m_stack_config.logMaxEntriesCount() != 0)
147+
{
148+
ocpp::log::Logger::registerDefaultLogger(*m_database, m_stack_config.logMaxEntriesCount());
149+
}
150+
151+
// Re-initialize with default values
152+
m_total_uptime = 0;
153+
initDatabase();
154+
}
155+
else
156+
{
157+
LOG_ERROR << "Unable to open database";
138158
}
139-
140-
// Re-initialize with default values
141-
m_total_uptime = 0;
142-
initDatabase();
143159
}
144160
else
145161
{
146-
LOG_ERROR << "Unable to open database";
162+
LOG_ERROR << "Unable to delete database";
147163
}
148164
}
149-
else
150-
{
151-
LOG_ERROR << "Unable to delete database";
152-
}
153165
}
154166

155167
return ret;
@@ -170,9 +182,12 @@ bool CentralSystem::start()
170182
if (ret)
171183
{
172184
// Start uptime counter
173-
m_uptime = 0;
174-
m_internal_config.setKey(START_DATE_KEY, DateTime::now().str());
175-
m_uptime_timer.start(std::chrono::seconds(1u));
185+
if (m_uptime_timer)
186+
{
187+
m_uptime = 0;
188+
m_internal_config->setKey(START_DATE_KEY, DateTime::now().str());
189+
m_uptime_timer->start(std::chrono::seconds(1u));
190+
}
176191

177192
// Allocate resources
178193
m_ws_server = std::unique_ptr<ocpp::websockets::IWebsocketServer>(ocpp::websockets::WebsocketFactory::newServer());
@@ -222,8 +237,11 @@ bool CentralSystem::stop()
222237
LOG_INFO << "Stopping OCPP stack";
223238

224239
// Stop uptime counter
225-
m_uptime_timer.stop();
226-
saveUptime();
240+
if (m_uptime_timer)
241+
{
242+
m_uptime_timer->stop();
243+
saveUptime();
244+
}
227245

228246
// Stop connection
229247
ret = m_rpc_server->stop();
@@ -233,7 +251,10 @@ bool CentralSystem::stop()
233251
m_rpc_server.reset();
234252

235253
// Close database
236-
m_database.close();
254+
if (m_database)
255+
{
256+
m_database->close();
257+
}
237258
}
238259
else
239260
{
@@ -289,33 +310,33 @@ void CentralSystem::rpcServerError()
289310
void CentralSystem::initDatabase()
290311
{
291312
// Initialize internal configuration
292-
m_internal_config.initDatabaseTable();
313+
m_internal_config->initDatabaseTable();
293314

294315
// Internal keys
295-
if (!m_internal_config.keyExist(STACK_VERSION_KEY))
316+
if (!m_internal_config->keyExist(STACK_VERSION_KEY))
296317
{
297-
m_internal_config.createKey(STACK_VERSION_KEY, OPEN_OCPP_VERSION);
318+
m_internal_config->createKey(STACK_VERSION_KEY, OPEN_OCPP_VERSION);
298319
}
299320
else
300321
{
301-
m_internal_config.setKey(STACK_VERSION_KEY, OPEN_OCPP_VERSION);
322+
m_internal_config->setKey(STACK_VERSION_KEY, OPEN_OCPP_VERSION);
302323
}
303-
if (!m_internal_config.keyExist(START_DATE_KEY))
324+
if (!m_internal_config->keyExist(START_DATE_KEY))
304325
{
305-
m_internal_config.createKey(START_DATE_KEY, "");
326+
m_internal_config->createKey(START_DATE_KEY, "");
306327
}
307-
if (!m_internal_config.keyExist(UPTIME_KEY))
328+
if (!m_internal_config->keyExist(UPTIME_KEY))
308329
{
309-
m_internal_config.createKey(UPTIME_KEY, "0");
330+
m_internal_config->createKey(UPTIME_KEY, "0");
310331
}
311-
if (!m_internal_config.keyExist(TOTAL_UPTIME_KEY))
332+
if (!m_internal_config->keyExist(TOTAL_UPTIME_KEY))
312333
{
313-
m_internal_config.createKey(TOTAL_UPTIME_KEY, "0");
334+
m_internal_config->createKey(TOTAL_UPTIME_KEY, "0");
314335
}
315336
else
316337
{
317338
std::string value;
318-
m_internal_config.getKey(TOTAL_UPTIME_KEY, value);
339+
m_internal_config->getKey(TOTAL_UPTIME_KEY, value);
319340
m_total_uptime = static_cast<unsigned int>(std::atoi(value.c_str()));
320341
}
321342
}
@@ -337,8 +358,8 @@ void CentralSystem::processUptime()
337358
/** @brief Save the uptime counter in database */
338359
void CentralSystem::saveUptime()
339360
{
340-
m_internal_config.setKey(UPTIME_KEY, std::to_string(m_uptime));
341-
m_internal_config.setKey(TOTAL_UPTIME_KEY, std::to_string(m_total_uptime));
361+
m_internal_config->setKey(UPTIME_KEY, std::to_string(m_uptime));
362+
m_internal_config->setKey(TOTAL_UPTIME_KEY, std::to_string(m_total_uptime));
342363
}
343364

344365
} // namespace centralsystem

src/centralsystem/CentralSystem.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ class CentralSystem : public ICentralSystem, public ocpp::rpc::RpcServer::IListe
9393
std::shared_ptr<ocpp::helpers::WorkerThreadPool> m_worker_pool;
9494

9595
/** @brief Database */
96-
ocpp::database::Database m_database;
96+
std::unique_ptr<ocpp::database::Database> m_database;
9797
/** @brief Internal configuration manager */
98-
ocpp::config::InternalConfigManager m_internal_config;
98+
std::unique_ptr<ocpp::config::InternalConfigManager> m_internal_config;
9999

100100
/** @brief Messages converter */
101101
ocpp::messages::MessagesConverter m_messages_converter;
@@ -108,7 +108,7 @@ class CentralSystem : public ICentralSystem, public ocpp::rpc::RpcServer::IListe
108108
std::unique_ptr<ocpp::rpc::RpcServer> m_rpc_server;
109109

110110
/** @brief Uptime timer */
111-
ocpp::helpers::Timer m_uptime_timer;
111+
std::unique_ptr<ocpp::helpers::Timer> m_uptime_timer;
112112
/** @brief Uptime in seconds */
113113
unsigned int m_uptime;
114114
/** @brief Total uptime in seconds */

0 commit comments

Comments
 (0)