add timer manager

This commit is contained in:
2018-04-07 11:55:09 +02:00
parent b5f6a813d3
commit 83396e3d20
2 changed files with 64 additions and 66 deletions

View File

@@ -34,7 +34,7 @@ file(
# Timers # Timers
${workspaceRoot}/src/prog/domod/src/timers/timers-manager.cpp ${workspaceRoot}/src/prog/domod/src/timers/timers-manager.cpp
${workspaceRoot}/src/prog/domod/src/timers/event.cpp ${workspaceRoot}/src/prog/domod/src/timers/event.cpp
${workspaceRoot}/src/prog/domod/src/timers/Clock.cpp ${workspaceRoot}/src/prog/domod/src/timers/clock.cpp
) )
# $ENV{SRC_DIR}/src/prog/domod/src/ubus/speach_command.cpp # $ENV{SRC_DIR}/src/prog/domod/src/ubus/speach_command.cpp

View File

@@ -35,7 +35,6 @@
#include "devices/devices-manager.h" #include "devices/devices-manager.h"
#include "timers/clock.h" #include "timers/clock.h"
#include "timers/timers-manager.h" #include "timers/timers-manager.h"
@@ -49,13 +48,11 @@
* *
* @brief Constructor of the Timers Managagers. * @brief Constructor of the Timers Managagers.
*/ */
TimerManager::TimerManager (const std::string &a_path, DevicesManager *a_device_manager): TimerManager::TimerManager(const std::string &a_path, DevicesManager *a_device_manager) : m_devices_manager(a_device_manager),
m_devices_manager (a_device_manager),
m_timers_path(a_path) m_timers_path(a_path)
{ {
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn ~TimerManager * @fn ~TimerManager
* *
@@ -65,7 +62,6 @@ TimerManager::~TimerManager (void)
{ {
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn load * @fn load
* *
@@ -106,7 +102,6 @@ int TimerManager::load (void)
return 0; return 0;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn save * @fn save
* *
@@ -114,17 +109,19 @@ int TimerManager::load (void)
*/ */
int TimerManager::save(void) int TimerManager::save(void)
{ {
#if 0 int the_result;
Json::StyledStreamWriter theWriter; struct json_object *the_root_node;
/* Save to the File. */ the_root_node = to_json();
std::ofstream theOutput (mTimersPath.c_str());
theWriter.write (theOutput, to_json()); the_result = json_object_to_file(m_timers_path.c_str(), the_root_node);
#endif
return 0; /* Clean the json object. */
json_object_put(the_root_node);
return the_result;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn get * @fn get
* *
@@ -134,14 +131,14 @@ const Event &TimerManager::get (uint16_t an_id)
{ {
std::vector<Event>::iterator the_timer_evt; std::vector<Event>::iterator the_timer_evt;
for (the_timer_evt = m_timers.begin(); the_timer_evt != m_timers.end(); the_timer_evt++) { for (the_timer_evt = m_timers.begin(); the_timer_evt != m_timers.end(); the_timer_evt++)
{
if ((*the_timer_evt).get_id() == an_id) if ((*the_timer_evt).get_id() == an_id)
{
return (*the_timer_evt); return (*the_timer_evt);
} }
} }
}
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn remove * @fn remove
@@ -152,18 +149,18 @@ int TimerManager::remove (uint16_t an_id)
{ {
std::vector<Event>::iterator the_timer_evt; std::vector<Event>::iterator the_timer_evt;
for (the_timer_evt = m_timers.begin(); the_timer_evt != m_timers.end(); the_timer_evt++) { for (the_timer_evt = m_timers.begin(); the_timer_evt != m_timers.end(); the_timer_evt++)
{
if ((*the_timer_evt).get_id() == an_id) if ((*the_timer_evt).get_id() == an_id)
{
m_timers.erase(the_timer_evt); m_timers.erase(the_timer_evt);
return 0; return 0;
} }
}
return -1; return -1;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn to_json * @fn to_json
* *
@@ -171,23 +168,22 @@ int TimerManager::remove (uint16_t an_id)
*/ */
struct json_object *TimerManager::to_json(void) struct json_object *TimerManager::to_json(void)
{ {
#if 0 std::vector<Event>::iterator the_timer_evt;
std::vector<Event>::iterator timer_evt; struct json_object *the_root_node, *the_timer_list_node;
Json::Value theRoot(Json::objectValue), timers_json(Json::arrayValue);
for (timer_evt = mTimers.begin(); timer_evt != mTimers.end(); timer_evt++) { the_root_node = json_object_new_object();
timers_json.append ((*timer_evt).to_json()); the_timer_list_node = json_object_new_array();
json_object_object_add(the_root_node, k_entry_timers, the_timer_list_node);
for (the_timer_evt = m_timers.begin(); the_timer_evt != m_timers.end(); the_timer_evt++)
{
json_object_array_add(the_timer_list_node, (*the_timer_evt).to_json());
} }
theRoot["timers"] = timers_json; return the_root_node;
return theRoot;
#endif
return NULL;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn Expire * @fn Expire
* *
@@ -195,39 +191,41 @@ struct json_object *TimerManager::to_json (void)
*/ */
int TimerManager::expire(void) int TimerManager::expire(void)
{ {
#if 0 std::vector<Event>::iterator the_timer_evt;
std::vector<Event>::iterator timer_evt; Clock the_clock;
Clock theClock;
fprintf(stderr, "**** >> Manage timers....\n"); fprintf(stderr, "**** >> Manage timers....\n");
for(timer_evt = mTimers.begin(); timer_evt != mTimers.end(); timer_evt++) { for (the_timer_evt = m_timers.begin(); the_timer_evt != m_timers.end(); the_timer_evt++)
{
if ((*timer_evt).isActive()) { if ((*the_timer_evt).is_active())
{
theClock.set ((*timer_evt)); the_clock.set((*the_timer_evt));
if ((*timer_evt).isInProgress()) {
if (theClock.isExpired()) {
if ((*the_timer_evt).is_in_progress())
{
if (the_clock.is_expired())
{
// Action Stop. // Action Stop.
fprintf(stdout, "Timer Stop\n"); fprintf(stdout, "Timer Stop\n");
(*timer_evt).stop (mDevices); (*the_timer_evt).stop(m_devices_manager);
} }
else { else
{
fprintf(stdout, "Not expired yet.\n"); fprintf(stdout, "Not expired yet.\n");
} }
} }
else { else
{
if (theClock.isEqualToCurrentTime ()) {
if (the_clock.is_equal_to_current_time())
{
// Action START. // Action START.
fprintf(stdout, "Timer Start\n"); fprintf(stdout, "Timer Start\n");
(*timer_evt).start (mDevices); (*the_timer_evt).start(m_devices_manager);
} }
} }
} }
} }
#endif
return 0; return 0;
} }