timer wip

This commit is contained in:
jbnadal
2018-04-06 18:12:42 +02:00
parent 62c1b6ba32
commit b5f6a813d3
14 changed files with 332 additions and 315 deletions

View File

@@ -36,11 +36,10 @@ extern "C" {
/*----------------------------- Dependencies --------------------------------*/ /*----------------------------- Dependencies --------------------------------*/
/*--------------------------------- CLASS ----------------------------------*/ /*--------------------------------- CLASS ----------------------------------*/
class ULoopTimer : public uloop_timeout { class ULoopTimer : public uloop_timeout
{
// Disable copy construction and copy assignement // Disable copy construction and copy assignement
ULoopTimer(const ULoopTimer &); ULoopTimer(const ULoopTimer &);
ULoopTimer &operator=(const ULoopTimer &); ULoopTimer &operator=(const ULoopTimer &);
@@ -49,7 +48,8 @@ public:
ULoopTimer(void); ULoopTimer(void);
virtual ~ULoopTimer(void); virtual ~ULoopTimer(void);
virtual int expire (void); virtual int expire(void) = 0;
int start(unsigned int a_timer_value, bool a_is_repeated = false); int start(unsigned int a_timer_value, bool a_is_repeated = false);
int stop(void); int stop(void);

View File

@@ -29,7 +29,6 @@
#include "ubus-cpp/uloop-timer.h" #include "ubus-cpp/uloop-timer.h"
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn timer_callback * @fn timer_callback
* *
@@ -42,7 +41,6 @@ static void timer_callback (struct uloop_timeout *a_timeout)
the_timer->tick(); the_timer->tick();
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn ULoopTimer * @fn ULoopTimer
* *
@@ -59,7 +57,6 @@ ULoopTimer::ULoopTimer (void)
m_timer_value = 0; m_timer_value = 0;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn ~ULoopTimer * @fn ~ULoopTimer
* *
@@ -69,7 +66,6 @@ ULoopTimer::~ULoopTimer (void)
{ {
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn start * @fn start
* @param aTimerValue is the amount of time (in milliseconds) before the timer * @param aTimerValue is the amount of time (in milliseconds) before the timer
@@ -84,7 +80,6 @@ int ULoopTimer::start (unsigned int a_timer_value, bool a_is_repeated)
return uloop_timeout_set(this, m_timer_value); return uloop_timeout_set(this, m_timer_value);
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn stop * @fn stop
* *
@@ -95,19 +90,6 @@ 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 * @fn tick
* *

View File

@@ -29,13 +29,14 @@ file(
${workspaceRoot}/src/prog/domod/src/ubus/capabilities_lights.cpp ${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_shutters.cpp
${workspaceRoot}/src/prog/domod/src/ubus/capabilities_sprinklers.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 #${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/ubus/speach_command.cpp
# $ENV{SRC_DIR}/src/prog/domod/src/helpers/Tokenizer.cpp # $ENV{SRC_DIR}/src/prog/domod/src/helpers/Tokenizer.cpp
# $ENV{SRC_DIR}/src/prog/domod/src/helpers/Strings.cpp # $ENV{SRC_DIR}/src/prog/domod/src/helpers/Strings.cpp

View File

@@ -157,7 +157,7 @@ std::string DevicesManager::get(const std::string &a_capability)
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn set * @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) 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; int the_result = -1;

View File

@@ -57,7 +57,7 @@ class DevicesManager
std::string get(const std::string &a_capability); 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, 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: private:
std::string m_file_path; std::string m_file_path;

View File

@@ -37,7 +37,7 @@ extern "C" {
#include <libubus.h> #include <libubus.h>
} }
#include "timers/timers.h" #include "timers/timers-manager.h"
#include "devices/devices-manager.h" #include "devices/devices-manager.h"
#include "ubus/capabilities.h" #include "ubus/capabilities.h"
@@ -140,13 +140,12 @@ int main(int argc, char *argv[])
} }
/* Setup the Timers. */ /* Setup the Timers. */
#if 0 TimerManager the_timers_manager(the_config_path + "/Timers.json", &the_devices_manager);
Timers the_timers(the_config_path + "/Timers.json", &the_devices_manager); if (the_timers_manager.load() != 0)
if (the_timers.load() != 0) { {
fprintf(stderr, "Failed to load timers.\n"); fprintf(stderr, "Failed to load timers.\n");
return -2; return -2;
} }
#endif
/* Setup the UBus Models. */ /* Setup the UBus Models. */
CapabilitiesModel the_capabilities(the_config_path + "./capabilities.json"); CapabilitiesModel the_capabilities(the_config_path + "./capabilities.json");

View File

@@ -1,5 +1,5 @@
/*! /*!
* Clock.cpp * clock.cpp
* *
* Copyright (c) 2016-2018, NADAL Jean-Baptiste. All rights reserved. * Copyright (c) 2016-2018, NADAL Jean-Baptiste. All rights reserved.
* *
@@ -26,8 +26,7 @@
#include <cstdio> #include <cstdio>
#include "timers/Clock.h" #include "timers/clock.h"
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn Clock * @fn Clock
@@ -38,7 +37,6 @@ Clock::Clock (void)
{ {
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn ~Clock * @fn ~Clock
* *
@@ -48,71 +46,68 @@ Clock::~Clock (void)
{ {
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn isEqual * @fn isEqual
* *
* @brief Set an Event to the Clock. * @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; return 0;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn isEqualToCurrentTime * @fn is_equal_to_current_time
* *
* @brief return true if the clock 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; struct tm the_compared_time;
double theDiff; double the_diff;
theComparedTime = *localtime (&mNow); the_compared_time = *localtime(&m_now);
theComparedTime.tm_hour = mEvent.getHour(); the_compared_time.tm_hour = m_event.get_hour();
theComparedTime.tm_min = mEvent.getMinute(); 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); //printf ("%.f seconds of diff between current time and Timer time.\n", theDiff);
if (theDiff == 0) if (the_diff == 0)
return true; return true;
return false; return false;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn isExpired * @fn is_expired
* *
* @brief Return true if the curent time has expired. * @brief Return true if the curent time has expired.
*/ */
bool Clock::isExpired (void) bool Clock::is_expired(void)
{ {
time_t theExpired; time_t the_expired;
struct tm theComparedTime; struct tm the_compared_time;
double theDiff; double the_diff;
theComparedTime = *localtime (&mNow); the_compared_time = *localtime(&m_now);
theComparedTime.tm_hour = mEvent.getHour(); the_compared_time.tm_hour = m_event.get_hour();
theComparedTime.tm_min = mEvent.getMinute(); 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); //printf ("%.f seconds of diff between %lld and %lld.\n", theDiff, mNow, theExpired);
if (theDiff == 0) if (the_diff == 0)
return true; return true;
return false; return false;

View File

@@ -1,5 +1,5 @@
/*! /*!
* Clock.h * clock.h
* *
* Copyright (c) 2016-2018, NADAL Jean-Baptiste. All rights reserved. * Copyright (c) 2016-2018, NADAL Jean-Baptiste. All rights reserved.
* *
@@ -30,25 +30,25 @@
#include <ctime> #include <ctime>
#include "timers/Event.h" #include "timers/event.h"
/*---------------------------------- Deps -----------------------------------*/ /*---------------------------------- Deps -----------------------------------*/
/*--------------------------------- CLASS ----------------------------------*/ /*--------------------------------- CLASS ----------------------------------*/
class Clock { class Clock
{
public: public:
Clock(void); Clock(void);
~Clock(void); ~Clock(void);
int set (const Event &anEvent); int set(const Event &an_event);
bool isEqualToCurrentTime (void); bool is_equal_to_current_time(void);
bool isExpired (void); bool is_expired(void);
private: private:
Event mEvent; Event m_event;
time_t mNow; time_t m_now;
}; };
#endif /* _CLOCK_H */ #endif /* _CLOCK_H */

View File

@@ -23,32 +23,40 @@
* *
*/ */
/*------------------------------- INCLUDES ----------------------------------*/ /*------------------------------- INCLUDES ----------------------------------*/
#include <json-c/json.h>
#include "devices/devices-manager.h" #include "devices/devices-manager.h"
#include "timers/event.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 * @fn Event
* *
* @brief Constructor of Timer Event Object. * @brief Constructor of Timer Event Object.
*/ */
Event::Event (void): Event::Event(void) : m_active(false),
m_id(0),
mActive(false), m_device_id(0),
mID(0), m_hour(0),
mDeviceID(0), m_minute(0),
mHour(0), m_recurrence(0),
mMinute(0), m_duration(0),
mRecurrence(0), m_in_progress(false)
mDuration(0),
mInProgress (false)
{ {
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn ~Event * @fn ~Event
* *
@@ -58,49 +66,77 @@ Event::~Event (void)
{ {
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn load_from_json * @fn from_json
* *
* @brief Load Event Object. * @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; struct json_object *the_value_node;
std::string::size_type sz; // alias of size_t
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(); m_hour = std::stoi(the_start_time.substr(0, the_separator));
theSeparator = theIDElem.find_last_of("/"); m_minute = std::stoi(the_start_time.substr(the_separator + 1));
}
mCapability = theIDElem.substr(0, theSeparator); // recurrence
mDeviceID = std::stoi (theIDElem.substr (theSeparator + 1), &sz); if (json_object_object_get_ex(a_node, k_entry_recurrence, &the_value_node))
{
m_recurrence = json_object_get_int(the_value_node);
}
std::string theStarTime = anElem["start_time"].asString(); // duration
theSeparator = theStarTime.find_last_of(":"); if (json_object_object_get_ex(a_node, k_entry_duration, &the_value_node))
{
m_duration = json_object_get_int(the_value_node);
}
mHour = std::stoi (theStarTime.substr (0, theSeparator), &sz); // action
mMinute = std::stoi (theStarTime.substr (theSeparator + 1), &sz); if (json_object_object_get_ex(a_node, k_entry_action, &the_value_node))
{
mRecurrence = anElem["recurrence"].asInt(); m_action = json_object_get_string(the_value_node);
mDuration = anElem["duration"].asInt(); }
mAction = anElem["action"].asString();
return 0; return 0;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn to_json * @fn to_json
* *
* @brief Export Event Object as JSON Object. * @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); Json::Value aResult(Json::objectValue);
aResult ["id"] = mID; aResult ["id"] = mID;
@@ -115,35 +151,33 @@ Json::Value Event::to_json (void) const
aResult["start_time"] = std::to_string(mHour) + ":" + std::to_string(mMinute); aResult["start_time"] = std::to_string(mHour) + ":" + std::to_string(mMinute);
return aResult; return aResult;
#endif
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn start * @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 * @fn stop
* *
* @brief Stop the Event. * @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 * @fn dump
* *
@@ -153,133 +187,122 @@ void Event::dump (void)
{ {
fprintf(stdout, "Timer Event:\n"); fprintf(stdout, "Timer Event:\n");
fprintf(stdout, "=========== \n"); fprintf(stdout, "=========== \n");
fprintf (stdout, " - active: %s\n", (mActive?"true":"false")); fprintf(stdout, " - active: %s\n", (m_active ? "true" : "false"));
fprintf (stdout, " - capability: %s\n", mCapability.c_str()); fprintf(stdout, " - capability: %s\n", m_capability.c_str());
fprintf (stdout, " - id: %d\n", mID); fprintf(stdout, " - id: %d\n", m_id);
fprintf (stdout, " - device_id: %d\n", mDeviceID); fprintf(stdout, " - device_id: %d\n", m_device_id);
fprintf (stdout, " - Time: %2d:%2d\n", mHour, mMinute); fprintf(stdout, " - Time: %2d:%2d\n", m_hour, m_minute);
fprintf (stdout, " - recurrence: %d\n", mRecurrence); fprintf(stdout, " - recurrence: %d\n", m_recurrence);
fprintf (stdout, " - duration: %d\n", mDuration); fprintf(stdout, " - duration: %d\n", m_duration);
fprintf (stdout, " - action: %s\n", mAction.c_str()); fprintf(stdout, " - action: %s\n", m_action.c_str());
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn isActive * @fn is_active
* *
* @brief return true if the Timer event 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. * @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. * @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. * @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. * @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. * @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 * @fn getRecurrence
* *
* @brief Return the Recurrency of the Event. * @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. * @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. * @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. * @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. * @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;
} }

View File

@@ -1,5 +1,5 @@
/*! /*!
* Event.h * event.h
* *
* Copyright (c) 2016-2018, NADAL Jean-Baptiste. All rights reserved. * Copyright (c) 2016-2018, NADAL Jean-Baptiste. All rights reserved.
* *
@@ -37,46 +37,46 @@ class DevicesManager;
/*--------------------------------- CLASS ----------------------------------*/ /*--------------------------------- CLASS ----------------------------------*/
class Event { class Event
{
public: public:
Event(void); Event(void);
~Event(void); ~Event(void);
//int load_from_json (Json::Value anElem); int from_json(struct json_object *a_node);
//Json::Value to_json (void) const; struct json_object *to_json(void) const;
int start (DevicesManager *aDevices); int start(DevicesManager *a_device_manager);
int stop (DevicesManager *aDevices); int stop(DevicesManager *a_device_manager);
void dump(void); void dump(void);
/* Getter */ /* Getter */
bool isActive (void) const; bool is_active(void) const;
std::string getCapability (void) const; const std::string &get_capability(void) const;
uint16_t getID (void) const; uint16_t get_id(void) const;
uint16_t getDeviceID (void) const; uint16_t get_device_id(void) const;
uint8_t getHour (void) const; uint8_t get_hour(void) const;
uint8_t getMinute (void) const; uint8_t get_minute(void) const;
uint16_t getRecurrence (void) const; uint16_t get_recurrence(void) const;
uint16_t getDuration (void) const; uint16_t get_duration(void) const;
std::string getAction (void) const; const std::string &get_action(void) const;
bool isInProgress (void) const; bool is_in_progress(void) const;
/* Setter */ /* Setter */
void setInProgress (bool aState); void set_in_progress(bool a_state);
private: private:
bool mActive; bool m_active;
std::string mCapability; std::string m_capability;
uint16_t mID; uint16_t m_id;
uint16_t mDeviceID; uint16_t m_device_id;
uint8_t mHour; uint8_t m_hour;
uint8_t mMinute; uint8_t m_minute;
uint16_t mRecurrence; uint16_t m_recurrence;
uint16_t mDuration; uint16_t m_duration;
std::string mAction; std::string m_action;
bool mInProgress; bool m_in_progress;
}; };
#endif /* _EVENT_H */ #endif /* _EVENT_H */

View File

@@ -28,36 +28,40 @@
#include <cstdio> #include <cstdio>
#include <ctime> #include <ctime>
#include <iostream> #include <json-c/json.h>
#include <fstream>
#include "devices/Devices.h" //#include <iostream>
//#include <fstream>
#include "timers/Timers.h" #include "devices/devices-manager.h"
#include "timers/Clock.h"
#include "timers/clock.h"
#include "timers/timers-manager.h"
//#define k60s 60000 //#define k60s 60000
#define k60s 2000 #define k60s 2000
#define k_entry_timers "timers"
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn Timers * @fn TimerManager
* *
* @brief Constructor of the Timers Managagers. * @brief Constructor of the Timers Managagers.
*/ */
Timers::Timers (std::string aPath, Devices *aDevice): TimerManager::TimerManager (const std::string &a_path, DevicesManager *a_device_manager):
mDevices (aDevice), m_devices_manager (a_device_manager),
mTimersPath (aPath) m_timers_path (a_path)
{ {
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn ~Timers * @fn ~TimerManager
* *
* @brief Destructors of the Timer Managers. * @brief Destructors of the Timer Managers.
*/ */
Timers::~Timers (void) TimerManager::~TimerManager (void)
{ {
} }
@@ -67,30 +71,37 @@ Timers::~Timers (void)
* *
* @brief Load the Timer Managers. * @brief Load the Timer Managers.
*/ */
int Timers::load (void) int TimerManager::load (void)
{ {
Json::Reader theReader; struct json_object *the_root_node, *the_data_node;
Json::Value theRoot;
std::ifstream theTimerFile (mTimersPath.c_str());
/* Read and Parse the Json file. */ the_root_node = json_object_from_file(m_timers_path.c_str());
if (!theReader.parse (theTimerFile, theRoot)) { if (the_root_node == NULL)
{
fprintf (stderr, "Failed to parse the Timers File (%s).\n", fprintf(stderr, "Failed to parse the Timers File (%s).\n", m_timers_path.c_str());
mTimersPath.c_str());
return -1; return -1;
} }
/* Create the Objects.. */ 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))
for (Json::Value& theElement : theRoot["timers"]) { {
Event theEvent; int the_len;
if (theEvent.load_from_json (theElement) == 0) { 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; return 0;
} }
@@ -101,14 +112,15 @@ int Timers::load (void)
* *
* @brief Method to save the Timers Object into a Json File. * @brief Method to save the Timers Object into a Json File.
*/ */
int Timers::save (void) int TimerManager::save (void)
{ {
#if 0
Json::StyledStreamWriter theWriter; Json::StyledStreamWriter theWriter;
/* Save to the File. */ /* Save to the File. */
std::ofstream theOutput (mTimersPath.c_str()); std::ofstream theOutput (mTimersPath.c_str());
theWriter.write (theOutput, to_json()); theWriter.write (theOutput, to_json());
#endif
return 0; return 0;
} }
@@ -118,15 +130,15 @@ int Timers::save (void)
* *
* @brief Method to get a specific Timers Object. * @brief Method to get a specific Timers Object.
*/ */
Event Timers::get (uint16_t anID) const Event &TimerManager::get (uint16_t an_id)
{ {
std::vector<Event>::iterator timer_evt; std::vector<Event>::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. * @brief Method to remove a specific Timers Object.
*/ */
int Timers::remove (uint16_t anID) int TimerManager::remove (uint16_t an_id)
{ {
std::vector<Event>::iterator timer_evt; std::vector<Event>::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; 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. * @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<Event>::iterator timer_evt; std::vector<Event>::iterator timer_evt;
Json::Value theRoot(Json::objectValue), timers_json(Json::arrayValue); Json::Value theRoot(Json::objectValue), timers_json(Json::arrayValue);
@@ -170,6 +183,8 @@ Json::Value Timers::to_json (void)
theRoot["timers"] = timers_json; theRoot["timers"] = timers_json;
return theRoot; return theRoot;
#endif
return NULL;
} }
@@ -178,8 +193,9 @@ Json::Value Timers::to_json (void)
* *
* @brief Method Call when the Timer has expired. * @brief Method Call when the Timer has expired.
*/ */
int Timers::Expire (void) int TimerManager::expire (void)
{ {
#if 0
std::vector<Event>::iterator timer_evt; std::vector<Event>::iterator timer_evt;
Clock theClock; Clock theClock;
fprintf (stderr, "**** >> Manage timers....\n"); fprintf (stderr, "**** >> Manage timers....\n");
@@ -212,6 +228,6 @@ int Timers::Expire (void)
} }
} }
} }
#endif
return 0; return 0;
} }

View File

@@ -1,5 +1,5 @@
/*! /*!
* Timers.h * timers-manager.h
* *
* Copyright (c) 2016-2018, NADAL Jean-Baptiste. All rights reserved. * Copyright (c) 2016-2018, NADAL Jean-Baptiste. All rights reserved.
* *
@@ -23,8 +23,8 @@
* *
*/ */
#ifndef _TIMERS_H #ifndef _TIMERS_MANAGER_H
#define _TIMERS_H #define _TIMERS_MANAGER_H
/*------------------------------- INCLUDES ----------------------------------*/ /*------------------------------- INCLUDES ----------------------------------*/
@@ -38,30 +38,30 @@
/*---------------------------------- Deps -----------------------------------*/ /*---------------------------------- Deps -----------------------------------*/
class DevicesManager; class DevicesManager;
struct json_object;
/*--------------------------------- CLASS ----------------------------------*/ /*--------------------------------- CLASS ----------------------------------*/
class Timers : public ULoopTimer { class TimerManager : public ULoopTimer
{
public: public:
Timers (const std::string &a_path, DevicesManager *a_device); TimerManager(const std::string &a_path, DevicesManager *a_device_manager);
~Timers (void); ~TimerManager(void);
int expire(void); int expire(void);
int load(void); int load(void);
int save(void); int save(void);
Event get (uint16_t anID); const Event &get(uint16_t an_id);
int remove (uint16_t anID); int remove(uint16_t an_id);
//Json::Value to_json (void); struct json_object *to_json(void);
private: private:
DevicesManager *mDevices; DevicesManager *m_devices_manager;
std::string mTimersPath; std::string m_timers_path;
std::vector <Event> mTimers; std::vector<Event> m_timers;
}; };
#endif /* _TIMERS_MANAGER_H */
#endif /* _TIMERS_H */

View File

@@ -31,10 +31,10 @@ extern "C" {
#include <libubox/blobmsg_json.h> #include <libubox/blobmsg_json.h>
} }
#include <jsoncpp/json.h> #include <json-c/json.h>
#include "timers/Timers.h" #include "timers/timers.h"
#include "timers.h" #include "timers.h"

View File

@@ -30,7 +30,7 @@
#include <stdint.h> #include <stdint.h>
#include <ubus-cpp/UBusObject.h> #include <ubus-cpp/ubus-object.h>
/*--------------------------------- Define ----------------------------------*/ /*--------------------------------- Define ----------------------------------*/
@@ -38,17 +38,18 @@ class Timers;
/*--------------------------------- CLASS ----------------------------------*/ /*--------------------------------- CLASS ----------------------------------*/
class TimersModel : public UBusObject { class TimersModel : public UBusObject
{
public: public:
TimersModel(Timers *aTimers); TimersModel(Timers *aTimers);
~TimersModel(void); ~TimersModel(void);
int Get (struct ubus_context*, struct ubus_request_data*, struct blob_attr *); int get(struct ubus_context *, struct ubus_request_data *, struct blob_attr *);
int Delete (struct ubus_context*, struct ubus_request_data*, struct blob_attr *); int del(struct ubus_context *, struct ubus_request_data *, struct blob_attr *);
private: private:
Timers *mTimers; Timers *m_timers;
}; };
#endif /* _UBUS_TIMERS_H */ #endif /* _UBUS_TIMERS_H */