From b5f6a813d30e4518c8d53318b199724746aaa51e Mon Sep 17 00:00:00 2001 From: jbnadal Date: Fri, 6 Apr 2018 18:12:42 +0200 Subject: [PATCH] timer wip --- .../include/ubus-cpp/uloop-timer.h | 32 +-- src/lib/libubus-cpp/src/uloop-timer.cpp | 42 +--- src/prog/domod/builders/cmake/CMakeLists.txt | 11 +- .../domod/src/devices/devices-manager.cpp | 8 +- src/prog/domod/src/devices/devices-manager.h | 2 +- src/prog/domod/src/main.cpp | 11 +- .../domod/src/timers/{Clock.cpp => clock.cpp} | 59 +++-- .../domod/src/timers/{Clock.h => clock.h} | 26 +-- src/prog/domod/src/timers/event.cpp | 221 ++++++++++-------- src/prog/domod/src/timers/event.h | 66 +++--- .../timers/{timers.cpp => timers-manager.cpp} | 100 ++++---- .../src/timers/{timers.h => timers-manager.h} | 40 ++-- src/prog/domod/src/ubus/timers.cpp | 4 +- src/prog/domod/src/ubus/timers.h | 25 +- 14 files changed, 332 insertions(+), 315 deletions(-) rename src/prog/domod/src/timers/{Clock.cpp => clock.cpp} (69%) rename src/prog/domod/src/timers/{Clock.h => clock.h} (84%) rename src/prog/domod/src/timers/{timers.cpp => timers-manager.cpp} (65%) rename src/prog/domod/src/timers/{timers.h => timers-manager.h} (70%) diff --git a/src/lib/libubus-cpp/include/ubus-cpp/uloop-timer.h b/src/lib/libubus-cpp/include/ubus-cpp/uloop-timer.h index 19131fb6..2beafda7 100644 --- a/src/lib/libubus-cpp/include/ubus-cpp/uloop-timer.h +++ b/src/lib/libubus-cpp/include/ubus-cpp/uloop-timer.h @@ -36,26 +36,26 @@ extern "C" { /*----------------------------- Dependencies --------------------------------*/ - /*--------------------------------- CLASS ----------------------------------*/ -class ULoopTimer : public uloop_timeout { - - // Disable copy construction and copy assignement - ULoopTimer (const ULoopTimer&); - ULoopTimer &operator=(const ULoopTimer&); - -public: - ULoopTimer (void); - virtual ~ULoopTimer (void); +class ULoopTimer : public uloop_timeout +{ + // Disable copy construction and copy assignement + ULoopTimer(const ULoopTimer &); + ULoopTimer &operator=(const ULoopTimer &); - virtual int expire (void); - int start (unsigned int a_timer_value, bool a_is_repeated = false); - int stop (void); - - int tick (void); + public: + ULoopTimer(void); + virtual ~ULoopTimer(void); -private: + virtual int expire(void) = 0; + + int start(unsigned int a_timer_value, bool a_is_repeated = false); + int stop(void); + + int tick(void); + + private: bool m_is_repeated; uint16_t m_timer_value; }; diff --git a/src/lib/libubus-cpp/src/uloop-timer.cpp b/src/lib/libubus-cpp/src/uloop-timer.cpp index e5c915bf..99b85ec6 100644 --- a/src/lib/libubus-cpp/src/uloop-timer.cpp +++ b/src/lib/libubus-cpp/src/uloop-timer.cpp @@ -29,26 +29,24 @@ #include "ubus-cpp/uloop-timer.h" - /*! ---------------------------------------------------------------------------- * @fn timer_callback * * @brief Callback used when a Timer has expired. */ -static void timer_callback (struct uloop_timeout *a_timeout) +static void timer_callback(struct uloop_timeout *a_timeout) { ULoopTimer *the_timer = static_cast(a_timeout); - + the_timer->tick(); } - /*! ---------------------------------------------------------------------------- * @fn ULoopTimer * * @brief Constructor of the Uloop Timer Object. */ -ULoopTimer::ULoopTimer (void) +ULoopTimer::ULoopTimer(void) { // Uloop Timer Init. cb = timer_callback; @@ -59,66 +57,50 @@ ULoopTimer::ULoopTimer (void) m_timer_value = 0; } - /*! ---------------------------------------------------------------------------- * @fn ~ULoopTimer * * @brief Destructor of the Ullop Timer Object. */ -ULoopTimer::~ULoopTimer (void) +ULoopTimer::~ULoopTimer(void) { } - /*! ---------------------------------------------------------------------------- * @fn start * @param aTimerValue is the amount of time (in milliseconds) before the timer * expires. * @brief Start the Timer object. */ -int ULoopTimer::start (unsigned int a_timer_value, bool a_is_repeated) +int ULoopTimer::start(unsigned int a_timer_value, bool a_is_repeated) { m_timer_value = a_timer_value; m_is_repeated = a_is_repeated; - return uloop_timeout_set (this, m_timer_value); + return uloop_timeout_set(this, m_timer_value); } - /*! ---------------------------------------------------------------------------- * @fn stop * * @brief Stop the Timer object. */ -int ULoopTimer::stop (void) +int ULoopTimer::stop(void) { - return uloop_timeout_cancel (this); + return uloop_timeout_cancel(this); } - -/*! ---------------------------------------------------------------------------- - * @fn expire - * - * @brief Method Should be overrided. - */ -int ULoopTimer::expire (void) -{ - //fprintf (stderr, "UBusTimer::Expire\n"); - return 0; -} - - /*! ---------------------------------------------------------------------------- * @fn tick * * @brief Method called when the Timer has expired. */ -int ULoopTimer::tick (void) +int ULoopTimer::tick(void) { // fprintf (stderr, "UBusTimer::Tick\n"); if (m_is_repeated == true) - uloop_timeout_set (this, m_timer_value); - - return expire (); + uloop_timeout_set(this, m_timer_value); + + return expire(); } diff --git a/src/prog/domod/builders/cmake/CMakeLists.txt b/src/prog/domod/builders/cmake/CMakeLists.txt index b19419f7..c6ad6a19 100644 --- a/src/prog/domod/builders/cmake/CMakeLists.txt +++ b/src/prog/domod/builders/cmake/CMakeLists.txt @@ -29,13 +29,14 @@ file( ${workspaceRoot}/src/prog/domod/src/ubus/capabilities_lights.cpp ${workspaceRoot}/src/prog/domod/src/ubus/capabilities_shutters.cpp ${workspaceRoot}/src/prog/domod/src/ubus/capabilities_sprinklers.cpp - - #${workspaceRoot}/src/prog/domod/src/timers/event.cpp #${workspaceRoot}/src/prog/domod/src/ubus/timers.cpp + + # Timers + ${workspaceRoot}/src/prog/domod/src/timers/timers-manager.cpp + ${workspaceRoot}/src/prog/domod/src/timers/event.cpp + ${workspaceRoot}/src/prog/domod/src/timers/Clock.cpp ) - # $ENV{SRC_DIR}/src/prog/domod/src/timers/Timers.cpp -# $ENV{SRC_DIR}/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/helpers/Tokenizer.cpp # $ENV{SRC_DIR}/src/prog/domod/src/helpers/Strings.cpp diff --git a/src/prog/domod/src/devices/devices-manager.cpp b/src/prog/domod/src/devices/devices-manager.cpp index d454d403..f2e5fac4 100644 --- a/src/prog/domod/src/devices/devices-manager.cpp +++ b/src/prog/domod/src/devices/devices-manager.cpp @@ -157,7 +157,7 @@ std::string DevicesManager::get(const std::string &a_capability) /*! ---------------------------------------------------------------------------- * @fn set * - * @brief get the device of a specific capabilities, with the ID. + * @brief set the device from a specific capabilities, with the given json. */ int DevicesManager::set(const std::string &a_capability, struct json_object *a_node) { @@ -183,11 +183,11 @@ int DevicesManager::set(const std::string &a_capability, struct json_object *a_n } /*! ---------------------------------------------------------------------------- - * @fn set + * @fn set_state * - * @brief get the device of a specific capabilities, with the ID. + * @brief set the state for device with the id from a specific capabilitiy. */ -int DevicesManager::set(const std::string &a_capability, int an_id, bool a_state) +int DevicesManager::set_state(const std::string &a_capability, int an_id, bool a_state) { int the_result = -1; diff --git a/src/prog/domod/src/devices/devices-manager.h b/src/prog/domod/src/devices/devices-manager.h index 59d9ab7f..223371bb 100644 --- a/src/prog/domod/src/devices/devices-manager.h +++ b/src/prog/domod/src/devices/devices-manager.h @@ -57,7 +57,7 @@ class DevicesManager std::string get(const std::string &a_capability); int set(const std::string &a_capability, struct json_object *a_node); - int set(const std::string &a_capability, int an_id, bool a_state); + int set_state(const std::string &a_capability, int an_id, bool a_state); private: std::string m_file_path; diff --git a/src/prog/domod/src/main.cpp b/src/prog/domod/src/main.cpp index 4bb0e582..1dfc3772 100644 --- a/src/prog/domod/src/main.cpp +++ b/src/prog/domod/src/main.cpp @@ -37,7 +37,7 @@ extern "C" { #include } -#include "timers/timers.h" +#include "timers/timers-manager.h" #include "devices/devices-manager.h" #include "ubus/capabilities.h" @@ -139,14 +139,13 @@ int main(int argc, char *argv[]) return -1; } - /* Setup the Timers. */ -#if 0 - Timers the_timers(the_config_path + "/Timers.json", &the_devices_manager); - if (the_timers.load() != 0) { + /* Setup the Timers. */ + TimerManager the_timers_manager(the_config_path + "/Timers.json", &the_devices_manager); + if (the_timers_manager.load() != 0) + { fprintf(stderr, "Failed to load timers.\n"); return -2; } -#endif /* Setup the UBus Models. */ CapabilitiesModel the_capabilities(the_config_path + "./capabilities.json"); diff --git a/src/prog/domod/src/timers/Clock.cpp b/src/prog/domod/src/timers/clock.cpp similarity index 69% rename from src/prog/domod/src/timers/Clock.cpp rename to src/prog/domod/src/timers/clock.cpp index f916a8e3..8ceca85a 100644 --- a/src/prog/domod/src/timers/Clock.cpp +++ b/src/prog/domod/src/timers/clock.cpp @@ -1,5 +1,5 @@ /*! - * Clock.cpp + * clock.cpp * * Copyright (c) 2016-2018, NADAL Jean-Baptiste. All rights reserved. * @@ -26,93 +26,88 @@ #include -#include "timers/Clock.h" - +#include "timers/clock.h" /*! ---------------------------------------------------------------------------- * @fn Clock * * @brief Constructor of the Clock Object. */ -Clock::Clock (void) +Clock::Clock(void) { } - /*! ---------------------------------------------------------------------------- * @fn ~Clock * * @brief Destructor of the Clock Object. */ -Clock::~Clock (void) +Clock::~Clock(void) { } - /*! ---------------------------------------------------------------------------- * @fn isEqual * * @brief Set an Event to the Clock. */ -int Clock::set (const Event &anEvent) +int Clock::set(const Event &an_event) { - mEvent = anEvent; + m_event = an_event; - mNow = time (NULL); + m_now = time(NULL); return 0; } - /*! ---------------------------------------------------------------------------- - * @fn isEqualToCurrentTime + * @fn is_equal_to_current_time * * @brief return true if the clock is equal to current time. */ -bool Clock::isEqualToCurrentTime (void) +bool Clock::is_equal_to_current_time(void) { - struct tm theComparedTime; - double theDiff; + struct tm the_compared_time; + double the_diff; - theComparedTime = *localtime (&mNow); + the_compared_time = *localtime(&m_now); - theComparedTime.tm_hour = mEvent.getHour(); - theComparedTime.tm_min = mEvent.getMinute(); + the_compared_time.tm_hour = m_event.get_hour(); + the_compared_time.tm_min = m_event.get_minute(); - theDiff = difftime (mNow, mktime (&theComparedTime)); + the_diff = difftime(m_now, mktime(&the_compared_time)); //printf ("%.f seconds of diff between current time and Timer time.\n", theDiff); - if (theDiff == 0) + if (the_diff == 0) return true; return false; } - /*! ---------------------------------------------------------------------------- - * @fn isExpired + * @fn is_expired * * @brief Return true if the curent time has expired. */ -bool Clock::isExpired (void) +bool Clock::is_expired(void) { - time_t theExpired; - struct tm theComparedTime; - double theDiff; + time_t the_expired; + struct tm the_compared_time; + double the_diff; - theComparedTime = *localtime (&mNow); + the_compared_time = *localtime(&m_now); - theComparedTime.tm_hour = mEvent.getHour(); - theComparedTime.tm_min = mEvent.getMinute(); + the_compared_time.tm_hour = m_event.get_hour(); + the_compared_time.tm_min = m_event.get_minute(); - theExpired = mktime (&theComparedTime) + (mEvent.getDuration() * 60); + the_expired = mktime(&the_compared_time) + (m_event.get_duration() * 60); - theDiff = difftime (mNow, theExpired); + the_diff = difftime(m_now, the_expired); //printf ("%.f seconds of diff between %lld and %lld.\n", theDiff, mNow, theExpired); - if (theDiff == 0) + if (the_diff == 0) return true; return false; diff --git a/src/prog/domod/src/timers/Clock.h b/src/prog/domod/src/timers/clock.h similarity index 84% rename from src/prog/domod/src/timers/Clock.h rename to src/prog/domod/src/timers/clock.h index 824ffb18..ed77973a 100644 --- a/src/prog/domod/src/timers/Clock.h +++ b/src/prog/domod/src/timers/clock.h @@ -1,5 +1,5 @@ /*! - * Clock.h + * clock.h * * Copyright (c) 2016-2018, NADAL Jean-Baptiste. All rights reserved. * @@ -30,25 +30,25 @@ #include -#include "timers/Event.h" +#include "timers/event.h" /*---------------------------------- Deps -----------------------------------*/ - /*--------------------------------- CLASS ----------------------------------*/ -class Clock { -public: - Clock (void); - ~Clock (void); +class Clock +{ + public: + Clock(void); + ~Clock(void); - int set (const Event &anEvent); - bool isEqualToCurrentTime (void); - bool isExpired (void); + int set(const Event &an_event); + bool is_equal_to_current_time(void); + bool is_expired(void); -private: - Event mEvent; - time_t mNow; + private: + Event m_event; + time_t m_now; }; #endif /* _CLOCK_H */ diff --git a/src/prog/domod/src/timers/event.cpp b/src/prog/domod/src/timers/event.cpp index ae03fd73..e2b1a3d4 100644 --- a/src/prog/domod/src/timers/event.cpp +++ b/src/prog/domod/src/timers/event.cpp @@ -23,84 +23,120 @@ * */ - /*------------------------------- INCLUDES ----------------------------------*/ +#include + #include "devices/devices-manager.h" #include "timers/event.h" +/*------------------------------- DEFINES ----------------------------------*/ + +#define k_entry_active "active" +#define k_entry_device_id "device_id" +#define k_entry_start_time "start_time" + +#define k_entry_recurrence "recurrence" +#define k_entry_duration "duration" +#define k_entry_action "action" + /*! ---------------------------------------------------------------------------- * @fn Event * * @brief Constructor of Timer Event Object. */ -Event::Event (void): - - mActive(false), - mID(0), - mDeviceID(0), - mHour(0), - mMinute(0), - mRecurrence(0), - mDuration(0), - mInProgress (false) +Event::Event(void) : m_active(false), + m_id(0), + m_device_id(0), + m_hour(0), + m_minute(0), + m_recurrence(0), + m_duration(0), + m_in_progress(false) { } - /*! ---------------------------------------------------------------------------- * @fn ~Event * * @brief Destructor of Timer Event Object. */ -Event::~Event (void) +Event::~Event(void) { } - /*! ---------------------------------------------------------------------------- - * @fn load_from_json + * @fn from_json * * @brief Load Event Object. */ -int Event::load_from_json (Json::Value anElem) +int Event::from_json(struct json_object *a_node) { - std::size_t theSeparator; - std::string::size_type sz; // alias of size_t + struct json_object *the_value_node; - mID = anElem["id"].asInt(); + // id + if (json_object_object_get_ex(a_node, k_entry_id, &the_value_node)) + { + m_id = json_object_get_int(the_value_node); + } + // active + if (json_object_object_get_ex(a_node, k_entry_active, &the_value_node)) + { + m_active = json_object_get_boolean(the_value_node); + } + // device_id + if (json_object_object_get_ex(a_node, k_entry_device_id, &the_value_node)) + { + std::size_t the_separator; + std::string the_id_elem; + the_id_elem = json_object_get_string(the_value_node); + the_separator = the_id_elem.find_last_of("/"); - mActive = anElem["active"].asBool(); + m_capability = the_id_elem.substr(0, the_separator); + m_device_id = std::stoi(the_id_elem.substr(the_separator + 1)); + } + // start time + if (json_object_object_get_ex(a_node, k_entry_start_time, &the_value_node)) + { + std::size_t the_separator; + std::string the_start_time; + the_start_time = json_object_get_string(the_value_node); + the_separator = the_start_time.find_last_of(":"); - std::string theIDElem = anElem["id"].asString(); - theSeparator = theIDElem.find_last_of("/"); - - mCapability = theIDElem.substr(0, theSeparator); - mDeviceID = std::stoi (theIDElem.substr (theSeparator + 1), &sz); + m_hour = std::stoi(the_start_time.substr(0, the_separator)); + m_minute = std::stoi(the_start_time.substr(the_separator + 1)); + } - std::string theStarTime = anElem["start_time"].asString(); - theSeparator = theStarTime.find_last_of(":"); + // recurrence + if (json_object_object_get_ex(a_node, k_entry_recurrence, &the_value_node)) + { + m_recurrence = json_object_get_int(the_value_node); + } - mHour = std::stoi (theStarTime.substr (0, theSeparator), &sz); - mMinute = std::stoi (theStarTime.substr (theSeparator + 1), &sz); + // duration + if (json_object_object_get_ex(a_node, k_entry_duration, &the_value_node)) + { + m_duration = json_object_get_int(the_value_node); + } - mRecurrence = anElem["recurrence"].asInt(); - mDuration = anElem["duration"].asInt(); - - mAction = anElem["action"].asString(); + // action + if (json_object_object_get_ex(a_node, k_entry_action, &the_value_node)) + { + m_action = json_object_get_string(the_value_node); + } return 0; } - /*! ---------------------------------------------------------------------------- * @fn to_json * * @brief Export Event Object as JSON Object. */ -Json::Value Event::to_json (void) const +struct json_object *Event::to_json(void) const { +#if 0 Json::Value aResult(Json::objectValue); aResult ["id"] = mID; @@ -115,171 +151,158 @@ Json::Value Event::to_json (void) const aResult["start_time"] = std::to_string(mHour) + ":" + std::to_string(mMinute); return aResult; +#endif } - /*! ---------------------------------------------------------------------------- * @fn start * - * @brief Stop the Event. + * @brief Start the Event. */ -int Event::start (Devices *aDevices) +int Event::start(DevicesManager *a_device_manager) { - setInProgress (true); + set_in_progress(true); - return aDevices->set (mCapability.c_str(), mDeviceID, true); + return a_device_manager->set_state(m_capability, m_device_id, true); } - /*! ---------------------------------------------------------------------------- * @fn stop * * @brief Stop the Event. */ -int Event::stop (Devices *aDevices) +int Event::stop(DevicesManager *a_device_manager) { - setInProgress (false); + set_in_progress(false); - return aDevices->set (mCapability.c_str(), mDeviceID, false); + return a_device_manager->set_state(m_capability, m_device_id, false); } - /*! ---------------------------------------------------------------------------- * @fn dump * * @brief dump the Event Object. */ -void Event::dump (void) +void Event::dump(void) { - fprintf (stdout, "Timer Event:\n"); - fprintf (stdout, "=========== \n"); - fprintf (stdout, " - active: %s\n", (mActive?"true":"false")); - fprintf (stdout, " - capability: %s\n", mCapability.c_str()); - fprintf (stdout, " - id: %d\n", mID); - fprintf (stdout, " - device_id: %d\n", mDeviceID); - fprintf (stdout, " - Time: %2d:%2d\n", mHour, mMinute); - fprintf (stdout, " - recurrence: %d\n", mRecurrence); - fprintf (stdout, " - duration: %d\n", mDuration); - fprintf (stdout, " - action: %s\n", mAction.c_str()); + fprintf(stdout, "Timer Event:\n"); + fprintf(stdout, "=========== \n"); + fprintf(stdout, " - active: %s\n", (m_active ? "true" : "false")); + fprintf(stdout, " - capability: %s\n", m_capability.c_str()); + fprintf(stdout, " - id: %d\n", m_id); + fprintf(stdout, " - device_id: %d\n", m_device_id); + fprintf(stdout, " - Time: %2d:%2d\n", m_hour, m_minute); + fprintf(stdout, " - recurrence: %d\n", m_recurrence); + fprintf(stdout, " - duration: %d\n", m_duration); + fprintf(stdout, " - action: %s\n", m_action.c_str()); } - /*! ---------------------------------------------------------------------------- - * @fn isActive + * @fn is_active * * @brief return true if the Timer event is active. */ -bool Event::isActive (void) const +bool Event::is_active(void) const { - return mActive; + return m_active; } - /*! ---------------------------------------------------------------------------- - * @fn getCapability + * @fn get_capability * * @brief return the capability of the Event. */ -std::string Event::getCapability (void) const +const std::string &Event::get_capability(void) const { - return mCapability; + return m_capability; } - /*! ---------------------------------------------------------------------------- - * @fn getID + * @fn get_id * * @brief return the ID of the Event. */ -uint16_t Event::getID (void) const +uint16_t Event::get_id(void) const { - return mID; + return m_id; } - /*! ---------------------------------------------------------------------------- - * @fn getDeviceID + * @fn get_device_id * * @brief return the Device ID of the Event. */ -uint16_t Event::getDeviceID (void) const +uint16_t Event::get_device_id(void) const { - return mDeviceID; + return m_device_id; } - /*! ---------------------------------------------------------------------------- - * @fn getHour + * @fn get_hour * * @brief Return the Hour of the Event. */ -uint8_t Event::getHour (void) const +uint8_t Event::get_hour(void) const { - return mHour; + return m_hour; } - /*! ---------------------------------------------------------------------------- - * @fn getMinute + * @fn get_minute * * @brief Return the Minute of the Event. */ -uint8_t Event::getMinute (void) const +uint8_t Event::get_minute(void) const { - return mMinute; + return m_minute; } - /*! ---------------------------------------------------------------------------- * @fn getRecurrence * * @brief Return the Recurrency of the Event. */ -uint16_t Event::getRecurrence (void) const +uint16_t Event::get_recurrence(void) const { - return mRecurrence; + return m_recurrence; } - /*! ---------------------------------------------------------------------------- - * @fn getDuration + * @fn get_duration * * @brief Return the Duration of the Event. */ -uint16_t Event::getDuration (void) const +uint16_t Event::get_duration(void) const { - return mDuration; + return m_duration; } - /*! ---------------------------------------------------------------------------- - * @fn getAction + * @fn get_action * * @brief Return the Action of the Event. */ -std::string Event::getAction (void) const +const std::string &Event::get_action(void) const { - return mAction; + return m_action; } - /*! ---------------------------------------------------------------------------- - * @fn isInProgress + * @fn is_in_progress * * @brief Return true if the Event Is in Progress. */ -bool Event::isInProgress (void) const +bool Event::is_in_progress(void) const { - return mInProgress; + return m_in_progress; } - /*! ---------------------------------------------------------------------------- - * @fn setInProgress + * @fn set_in_progress * * @brief Set the InProgress to a new state. */ -void Event::setInProgress (bool aState) +void Event::set_in_progress(bool a_state) { - mInProgress = aState; + m_in_progress = a_state; } diff --git a/src/prog/domod/src/timers/event.h b/src/prog/domod/src/timers/event.h index fd001fa2..3b4d4a02 100644 --- a/src/prog/domod/src/timers/event.h +++ b/src/prog/domod/src/timers/event.h @@ -1,5 +1,5 @@ /*! - * Event.h + * event.h * * Copyright (c) 2016-2018, NADAL Jean-Baptiste. All rights reserved. * @@ -37,46 +37,46 @@ class DevicesManager; /*--------------------------------- CLASS ----------------------------------*/ -class Event { +class Event +{ + public: + Event(void); + ~Event(void); -public: - Event (void); - ~Event (void); + int from_json(struct json_object *a_node); + struct json_object *to_json(void) const; - //int load_from_json (Json::Value anElem); - //Json::Value to_json (void) const; + int start(DevicesManager *a_device_manager); + int stop(DevicesManager *a_device_manager); - int start (DevicesManager *aDevices); - int stop (DevicesManager *aDevices); - - void dump (void); + void dump(void); /* Getter */ - bool isActive (void) const; - std::string getCapability (void) const; - uint16_t getID (void) const; - uint16_t getDeviceID (void) const; - uint8_t getHour (void) const; - uint8_t getMinute (void) const; - uint16_t getRecurrence (void) const; - uint16_t getDuration (void) const; - std::string getAction (void) const; - bool isInProgress (void) const; + bool is_active(void) const; + const std::string &get_capability(void) const; + uint16_t get_id(void) const; + uint16_t get_device_id(void) const; + uint8_t get_hour(void) const; + uint8_t get_minute(void) const; + uint16_t get_recurrence(void) const; + uint16_t get_duration(void) const; + const std::string &get_action(void) const; + bool is_in_progress(void) const; /* Setter */ - void setInProgress (bool aState); + void set_in_progress(bool a_state); -private: - bool mActive; - std::string mCapability; - uint16_t mID; - uint16_t mDeviceID; - uint8_t mHour; - uint8_t mMinute; - uint16_t mRecurrence; - uint16_t mDuration; - std::string mAction; - bool mInProgress; + private: + bool m_active; + std::string m_capability; + uint16_t m_id; + uint16_t m_device_id; + uint8_t m_hour; + uint8_t m_minute; + uint16_t m_recurrence; + uint16_t m_duration; + std::string m_action; + bool m_in_progress; }; #endif /* _EVENT_H */ diff --git a/src/prog/domod/src/timers/timers.cpp b/src/prog/domod/src/timers/timers-manager.cpp similarity index 65% rename from src/prog/domod/src/timers/timers.cpp rename to src/prog/domod/src/timers/timers-manager.cpp index 72cd0970..ae1a9795 100644 --- a/src/prog/domod/src/timers/timers.cpp +++ b/src/prog/domod/src/timers/timers-manager.cpp @@ -28,36 +28,40 @@ #include #include -#include -#include +#include -#include "devices/Devices.h" +//#include +//#include -#include "timers/Timers.h" -#include "timers/Clock.h" +#include "devices/devices-manager.h" + + +#include "timers/clock.h" + +#include "timers/timers-manager.h" //#define k60s 60000 #define k60s 2000 - +#define k_entry_timers "timers" /*! ---------------------------------------------------------------------------- - * @fn Timers + * @fn TimerManager * * @brief Constructor of the Timers Managagers. */ -Timers::Timers (std::string aPath, Devices *aDevice): -mDevices (aDevice), -mTimersPath (aPath) +TimerManager::TimerManager (const std::string &a_path, DevicesManager *a_device_manager): +m_devices_manager (a_device_manager), +m_timers_path (a_path) { } /*! ---------------------------------------------------------------------------- - * @fn ~Timers + * @fn ~TimerManager * * @brief Destructors of the Timer Managers. */ -Timers::~Timers (void) +TimerManager::~TimerManager (void) { } @@ -67,30 +71,37 @@ Timers::~Timers (void) * * @brief Load the Timer Managers. */ -int Timers::load (void) +int TimerManager::load (void) { - Json::Reader theReader; - Json::Value theRoot; - std::ifstream theTimerFile (mTimersPath.c_str()); + struct json_object *the_root_node, *the_data_node; - /* Read and Parse the Json file. */ - if (!theReader.parse (theTimerFile, theRoot)) { - - fprintf (stderr, "Failed to parse the Timers File (%s).\n", - mTimersPath.c_str()); + the_root_node = json_object_from_file(m_timers_path.c_str()); + if (the_root_node == NULL) + { + fprintf(stderr, "Failed to parse the Timers File (%s).\n", m_timers_path.c_str()); return -1; } - /* Create the Objects.. */ - for (Json::Value& theElement : theRoot["timers"]) { - Event theEvent; - if (theEvent.load_from_json (theElement) == 0) { + if (json_object_object_get_ex(the_root_node, k_entry_timers, &the_data_node) && (json_object_get_type(the_data_node) == json_type_array)) + { + int the_len; + struct json_object *the_timer_node; - mTimers.push_back (theEvent); + the_len = json_object_array_length(the_data_node); + for (int i = 0; i < the_len; i++) + { + Event the_event; + the_timer_node = json_object_array_get_idx(the_data_node, i); + the_event.from_json(the_timer_node); + + m_timers.push_back(the_event); } } - Start (k60s, true); + /* Clean the json object. */ + json_object_put(the_root_node); + + start (k60s, true); return 0; } @@ -101,14 +112,15 @@ int Timers::load (void) * * @brief Method to save the Timers Object into a Json File. */ -int Timers::save (void) +int TimerManager::save (void) { +#if 0 Json::StyledStreamWriter theWriter; /* Save to the File. */ std::ofstream theOutput (mTimersPath.c_str()); theWriter.write (theOutput, to_json()); - +#endif return 0; } @@ -118,15 +130,15 @@ int Timers::save (void) * * @brief Method to get a specific Timers Object. */ -Event Timers::get (uint16_t anID) +const Event &TimerManager::get (uint16_t an_id) { - std::vector::iterator timer_evt; + std::vector::iterator the_timer_evt; - 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).getID() == anID) + if ((*the_timer_evt).get_id() == an_id) - return (*timer_evt); + return (*the_timer_evt); } } @@ -136,15 +148,15 @@ Event Timers::get (uint16_t anID) * * @brief Method to remove a specific Timers Object. */ -int Timers::remove (uint16_t anID) +int TimerManager::remove (uint16_t an_id) { - std::vector::iterator timer_evt; + std::vector::iterator the_timer_evt; - 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).getID() == anID) + if ((*the_timer_evt).get_id() == an_id) - mTimers.erase (timer_evt); + m_timers.erase (the_timer_evt); return 0; } @@ -157,8 +169,9 @@ int Timers::remove (uint16_t anID) * * @brief Method to return the Timers Object into a Json formar into a Json Object. */ -Json::Value Timers::to_json (void) +struct json_object *TimerManager::to_json (void) { +#if 0 std::vector::iterator timer_evt; Json::Value theRoot(Json::objectValue), timers_json(Json::arrayValue); @@ -170,6 +183,8 @@ Json::Value Timers::to_json (void) theRoot["timers"] = timers_json; return theRoot; +#endif + return NULL; } @@ -178,8 +193,9 @@ Json::Value Timers::to_json (void) * * @brief Method Call when the Timer has expired. */ -int Timers::Expire (void) +int TimerManager::expire (void) { +#if 0 std::vector::iterator timer_evt; Clock theClock; fprintf (stderr, "**** >> Manage timers....\n"); @@ -212,6 +228,6 @@ int Timers::Expire (void) } } } - +#endif return 0; } diff --git a/src/prog/domod/src/timers/timers.h b/src/prog/domod/src/timers/timers-manager.h similarity index 70% rename from src/prog/domod/src/timers/timers.h rename to src/prog/domod/src/timers/timers-manager.h index 8781e881..514c085d 100644 --- a/src/prog/domod/src/timers/timers.h +++ b/src/prog/domod/src/timers/timers-manager.h @@ -1,5 +1,5 @@ /*! - * Timers.h + * timers-manager.h * * Copyright (c) 2016-2018, NADAL Jean-Baptiste. All rights reserved. * @@ -23,8 +23,8 @@ * */ -#ifndef _TIMERS_H -#define _TIMERS_H +#ifndef _TIMERS_MANAGER_H +#define _TIMERS_MANAGER_H /*------------------------------- INCLUDES ----------------------------------*/ @@ -38,30 +38,30 @@ /*---------------------------------- Deps -----------------------------------*/ class DevicesManager; +struct json_object; /*--------------------------------- CLASS ----------------------------------*/ -class Timers : public ULoopTimer { +class TimerManager : public ULoopTimer +{ + public: + TimerManager(const std::string &a_path, DevicesManager *a_device_manager); + ~TimerManager(void); -public: - Timers (const std::string &a_path, DevicesManager *a_device); - ~Timers (void); + int expire(void); - int expire (void); + int load(void); + int save(void); - int load (void); - int save (void); - - Event get (uint16_t anID); - int remove (uint16_t anID); + const Event &get(uint16_t an_id); + int remove(uint16_t an_id); - //Json::Value to_json (void); + struct json_object *to_json(void); -private: - DevicesManager *mDevices; - std::string mTimersPath; - std::vector mTimers; + private: + DevicesManager *m_devices_manager; + std::string m_timers_path; + std::vector m_timers; }; - -#endif /* _TIMERS_H */ +#endif /* _TIMERS_MANAGER_H */ diff --git a/src/prog/domod/src/ubus/timers.cpp b/src/prog/domod/src/ubus/timers.cpp index a4079616..8834d1a2 100644 --- a/src/prog/domod/src/ubus/timers.cpp +++ b/src/prog/domod/src/ubus/timers.cpp @@ -31,10 +31,10 @@ extern "C" { #include } -#include +#include -#include "timers/Timers.h" +#include "timers/timers.h" #include "timers.h" diff --git a/src/prog/domod/src/ubus/timers.h b/src/prog/domod/src/ubus/timers.h index 143480b9..91987d81 100644 --- a/src/prog/domod/src/ubus/timers.h +++ b/src/prog/domod/src/ubus/timers.h @@ -29,8 +29,8 @@ /*------------------------------- INCLUDES ----------------------------------*/ #include - -#include + +#include /*--------------------------------- Define ----------------------------------*/ @@ -38,17 +38,18 @@ class Timers; /*--------------------------------- CLASS ----------------------------------*/ -class TimersModel : public UBusObject { +class TimersModel : public UBusObject +{ -public: - TimersModel (Timers *aTimers); - ~TimersModel (void); - - int Get (struct ubus_context*, struct ubus_request_data*, struct blob_attr *); - int Delete (struct ubus_context*, struct ubus_request_data*, struct blob_attr *); - -private: - Timers *mTimers; + public: + TimersModel(Timers *aTimers); + ~TimersModel(void); + + int get(struct ubus_context *, struct ubus_request_data *, struct blob_attr *); + int del(struct ubus_context *, struct ubus_request_data *, struct blob_attr *); + + private: + Timers *m_timers; }; #endif /* _UBUS_TIMERS_H */