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
*

View File

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

View File

@@ -44,7 +44,8 @@ using namespace UBus;
static ObjectType gTimersModelUbus_types(
"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
*
* @brief Constructor of the UBus Mixer Volume.
* @brief Constructor of the Timer Objects.
*/
TimersModel::TimersModel (Timers *aTimers) :
UBusObject (gTimersModelUbus_types, "domo.timers"),
@@ -64,7 +65,7 @@ TimersModel::TimersModel (Timers *aTimers) :
/*! ----------------------------------------------------------------------------
* @fn ~TimersModel
*
* @brief Destructor of the UBus Mixer Volume.
* @brief Destructor of the Timer Objects.
*/
TimersModel::~TimersModel (void)
{
@@ -75,10 +76,10 @@ TimersModel::~TimersModel (void)
/*! ----------------------------------------------------------------------------
* @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,
struct blob_attr *aMsg)
struct blob_attr *aMsg)
{
int theResult = 0;
int theID = -1;
@@ -92,12 +93,12 @@ int TimersModel::Get (struct ubus_context *aCtx, struct ubus_request_data *aReq,
if (!theReader.parse (theString, theRoot)) {
fprintf (stderr, "Failed parse the parameters.\n");
return UBUS_STATUS_INVALID_ARGUMENT;
}
fprintf (stderr, "Failed parse the parameters.\n");
return UBUS_STATUS_INVALID_ARGUMENT;
}
theID = theRoot["id"].asInt();
blob_buf_init (&theBuf, 0);
if (theID == 0)
@@ -113,3 +114,39 @@ int TimersModel::Get (struct ubus_context *aCtx, struct ubus_request_data *aReq,
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);
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;