Add remove method.

This commit is contained in:
jbnadal
2016-07-06 19:05:49 +02:00
parent 5cbe69d986
commit 01738a7684
4 changed files with 70 additions and 10 deletions

View File

@@ -131,6 +131,27 @@ Event Timers::get (uint16_t anID)
} }
/*! ----------------------------------------------------------------------------
* @fn remove
*
* @brief Method to remove a specific Timers Object.
*/
int Timers::remove (uint16_t anID)
{
std::vector<Event>::iterator timer_evt;
for (timer_evt = mTimers.begin(); timer_evt != mTimers.end(); timer_evt++) {
if ((*timer_evt).getID() == anID)
mTimers.erase (timer_evt);
return 0;
}
return -1;
}
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn to_json * @fn to_json
* *

View File

@@ -54,6 +54,7 @@ public:
int save (void); int save (void);
Event get (uint16_t anID); Event get (uint16_t anID);
int remove (uint16_t anID);
Json::Value to_json (void); Json::Value to_json (void);

View File

@@ -44,7 +44,8 @@ using namespace UBus;
static ObjectType gTimersModelUbus_types( static ObjectType gTimersModelUbus_types(
"TimersModel", "TimersModel",
Method("get", UBUS_CPP(TimersModel, Get)) Method("get", UBUS_CPP(TimersModel, Get)),
Method("delete", UBUS_CPP(TimersModel, Delete))
); );
} }
@@ -52,7 +53,7 @@ static ObjectType gTimersModelUbus_types(
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn TimersModel * @fn TimersModel
* *
* @brief Constructor of the UBus Mixer Volume. * @brief Constructor of the Timer Objects.
*/ */
TimersModel::TimersModel (Timers *aTimers) : TimersModel::TimersModel (Timers *aTimers) :
UBusObject (gTimersModelUbus_types, "domo.timers"), UBusObject (gTimersModelUbus_types, "domo.timers"),
@@ -64,7 +65,7 @@ TimersModel::TimersModel (Timers *aTimers) :
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn ~TimersModel * @fn ~TimersModel
* *
* @brief Destructor of the UBus Mixer Volume. * @brief Destructor of the Timer Objects.
*/ */
TimersModel::~TimersModel (void) TimersModel::~TimersModel (void)
{ {
@@ -75,7 +76,7 @@ TimersModel::~TimersModel (void)
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn Get * @fn Get
* *
* @brief Get the List of the Capabilities. * @brief Get the List of timer objects.
*/ */
int TimersModel::Get (struct ubus_context *aCtx, struct ubus_request_data *aReq, int TimersModel::Get (struct ubus_context *aCtx, struct ubus_request_data *aReq,
struct blob_attr *aMsg) struct blob_attr *aMsg)
@@ -113,3 +114,39 @@ int TimersModel::Get (struct ubus_context *aCtx, struct ubus_request_data *aReq,
return theResult; return theResult;
} }
/*! ----------------------------------------------------------------------------
* @fn Delete
*
* @brief Delete a Timer Object.
*/
int TimersModel::Delete (struct ubus_context *aCtx, struct ubus_request_data *aReq,
struct blob_attr *aMsg)
{
int theResult = 0;
Json::Reader theReader;
Json::Value theRoot;
int theID = -1;
char *theString = blobmsg_format_json (aMsg, true);
if (!theReader.parse (theString, theRoot)) {
fprintf (stderr, "Failed parse the parameters.\n");
return UBUS_STATUS_INVALID_ARGUMENT;
}
theID = theRoot["id"].asInt();
printf ("try to delete id: %d\n", theID);
theResult = mTimers->remove (theID);
if (theResult == 0) {
printf ("Successfully removed.\n");
//TODO SAVE and return OK.
}
return theResult;
}

View File

@@ -45,6 +45,7 @@ public:
~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*);
private: private:
Timers *mTimers; Timers *mTimers;