Add ubus sprinkler model

This commit is contained in:
jbnadal
2018-04-06 11:37:11 +02:00
parent e482ca9d71
commit 62c1b6ba32
4 changed files with 94 additions and 94 deletions

View File

@@ -28,13 +28,14 @@ file(
${workspaceRoot}/src/prog/domod/src/ubus/capabilities.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_sprinklers.cpp
#${workspaceRoot}/src/prog/domod/src/timers/event.cpp
#${workspaceRoot}/src/prog/domod/src/ubus/timers.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/capabilities_sprinklers.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

View File

@@ -43,6 +43,7 @@ extern "C" {
#include "ubus/capabilities.h"
#include "ubus/capabilities_lights.h"
#include "ubus/capabilities_shutters.h"
#include "ubus/capabilities_sprinklers.h"
/*-------------------------------- DEFINES ---------------------------------*/
@@ -151,6 +152,8 @@ int main(int argc, char *argv[])
CapabilitiesModel the_capabilities(the_config_path + "./capabilities.json");
CapabilitiesLightsModel the_cap_lights(&the_devices_manager);
CapabilitiesShuttersModel the_cap_shutters(&the_devices_manager);
CapabilitiesSprinklersModel the_cap_sprinklers(&the_devices_manager);
return 0;
/* Setup the Ubus context. */
@@ -164,6 +167,7 @@ int main(int argc, char *argv[])
ubus_add_object(the_ctx, &the_capabilities);
ubus_add_object(the_ctx, &the_cap_lights);
ubus_add_object(the_ctx, &the_cap_shutters);
ubus_add_object(the_ctx, &the_cap_sprinklers);
/* Main Event Loop. */
uloop_run();
@@ -176,7 +180,6 @@ int main(int argc, char *argv[])
#if 0
#include "ubus/capabilities_sprinklers.h"
#include "ubus/speach_command.h"
#include "ubus/timers.h"
@@ -230,7 +233,7 @@ int main (void)
CapabilitiesModel theCapabilities;
CapabilitiesLightsModel theCapLights (&theDevices);
CapabilitiesSprinklersModel theCapSprinklers (&theDevices);
SpeachCommandModel theSpeachCommand (&theDevices);
TimersModel theTimersModel (&theTimers);
@@ -247,7 +250,7 @@ int main (void)
ubus_add_object (theCtx, &theCapabilities);
ubus_add_object (theCtx, &theCapLights);
ubus_add_object (theCtx, &theCapSprinklers);
ubus_add_object (theCtx, &theSpeachCommand);
ubus_add_object (theCtx, &theTimersModel);

View File

@@ -29,26 +29,25 @@ extern "C" {
#include <libubox/blobmsg_json.h>
}
#include <ubus-cpp/UBusCall.h>
#include <ubus-cpp/ubus-call.h>
#include <jsoncpp/json.h>
#include <json-c/json.h>
#include "devices/Devices.h"
#include "devices/devices-manager.h"
#include "capabilities_sprinklers.h"
namespace {
namespace
{
using namespace UBus;
static ObjectType gCapabilitiesSprinklersModelUbus_types(
"sprinklers",
Method("get", UBUS_CPP(CapabilitiesSprinklersModel, Get)),
Method("delete", UBUS_CPP(CapabilitiesSprinklersModel, Delete)),
Method("put", UBUS_CPP(CapabilitiesSprinklersModel, Put)),
Method("post", UBUS_CPP(CapabilitiesSprinklersModel, Post))
);
Method("get", UBUS_CPP(CapabilitiesSprinklersModel, get)),
Method("delete", UBUS_CPP(CapabilitiesSprinklersModel, del)),
Method("put", UBUS_CPP(CapabilitiesSprinklersModel, put)),
Method("post", UBUS_CPP(CapabilitiesSprinklersModel, post)));
}
/*! ----------------------------------------------------------------------------
@@ -56,113 +55,109 @@ static ObjectType gCapabilitiesSprinklersModelUbus_types(
*
* @brief Constructor of the UBus Mixer Volume.
*/
CapabilitiesSprinklersModel::CapabilitiesSprinklersModel (Devices *aDevice) :
UBusObject (gCapabilitiesSprinklersModelUbus_types, "domo.capabilities.sprinklers"),
mDevices (aDevice)
CapabilitiesSprinklersModel::CapabilitiesSprinklersModel(DevicesManager *a_device_manager) : UBusObject(gCapabilitiesSprinklersModelUbus_types, "domo.capabilities.sprinklers"),
m_devices_manager(a_device_manager)
{
}
/*! ----------------------------------------------------------------------------
* @fn ~CapabilitiesSprinklersModel
*
* @brief Destructor of the UBus Mixer Volume.
*/
CapabilitiesSprinklersModel::~CapabilitiesSprinklersModel (void)
CapabilitiesSprinklersModel::~CapabilitiesSprinklersModel(void)
{
}
/*! ----------------------------------------------------------------------------
* @fn Get
* @fn get
*
* @brief Get the List of the Capabilities.
*/
int CapabilitiesSprinklersModel::Get (struct ubus_context *aCtx, struct ubus_request_data *aReq,
struct blob_attr *aMsg)
int CapabilitiesSprinklersModel::get(struct ubus_context *a_ctx, struct ubus_request_data *a_req, struct blob_attr *)
{
int theResult;
struct blob_buf theBuf = {0};
int the_result;
struct blob_buf the_buf = {0};
blob_buf_init (&theBuf, 0);
blobmsg_add_json_from_string (&theBuf, mDevices->get("Sprinklers").c_str());
theResult = ubus_send_reply (aCtx, aReq, theBuf.head);
blob_buf_free (&theBuf);
return theResult;
blob_buf_init(&the_buf, 0);
blobmsg_add_json_from_string(&the_buf, m_devices_manager->get(kSprinklerEntry).c_str());
the_result = ubus_send_reply(a_ctx, a_req, the_buf.head);
blob_buf_free(&the_buf);
return the_result;
}
/*! ----------------------------------------------------------------------------
* @fn Delete
* @fn del
*
* @brief Get the List of the Capabilities.
*/
int CapabilitiesSprinklersModel::Delete (struct ubus_context*, struct ubus_request_data*, struct blob_attr*)
int CapabilitiesSprinklersModel::del(struct ubus_context *, struct ubus_request_data *, struct blob_attr *)
{
int theResult = 0;
return theResult;
int the_result = 0;
return the_result;
}
/*! ----------------------------------------------------------------------------
* @fn Put
* @fn put
*
* @brief Get the List of the Capabilities.
*/
int CapabilitiesSprinklersModel::Put (struct ubus_context*, struct ubus_request_data*, struct blob_attr*)
int CapabilitiesSprinklersModel::put(struct ubus_context *, struct ubus_request_data *, struct blob_attr *)
{
int theResult = 0;
return theResult;
int the_result = 0;
return the_result;
}
/*! ----------------------------------------------------------------------------
* @fn Post
* @fn post
*
* @brief Get the List of the Capabilities.
*/
int CapabilitiesSprinklersModel::Post (struct ubus_context *aCtx, struct ubus_request_data *aReq,
struct blob_attr *aMsg)
int CapabilitiesSprinklersModel::post(struct ubus_context *a_ctx, struct ubus_request_data *a_req, struct blob_attr *a_msg)
{
int theResult;
struct blob_buf theBuf = {0};
char *theString = blobmsg_format_json (aMsg, true);
Json::Reader theReader;
Json::StyledWriter theWriter;
Json::Value theRoot;
Json::Value theOutput;
Json::Value theElement;
int theID;
bool theState;
fprintf (stderr,"CapabilitiesSprinklersModel::Post \n");
if (!theReader.parse (theString, theRoot)) {
int the_result = 0;
struct blob_buf the_buf = {0};
char *the_string;
struct json_object *the_root_node, *the_output_node;
fprintf(stderr, "CapabilitiesSprinklersModel::Post \n");
fprintf (stderr, "Failed parse the parameters.\n");
return UBUS_STATUS_INVALID_ARGUMENT;
}
the_string = blobmsg_format_json(a_msg, true);
if (mDevices->set ("Sprinklers", theRoot) < 0) {
the_root_node = json_tokener_parse(the_string);
free(the_string);
fprintf (stderr, "Failed to set the new state.\n");
return -1;
}
if (the_root_node == NULL)
{
fprintf(stderr, "Failed parse the parameters.\n");
return UBUS_STATUS_INVALID_ARGUMENT;
}
theOutput["Sprinklers"] = theElement;
if (m_devices_manager->set(kSprinklerEntry, the_root_node) < 0)
{
fprintf(stderr, "Failed to set the new state.\n");
return UBUS_STATUS_INVALID_ARGUMENT;
}
blob_buf_init (&theBuf, 0);
// Create the output node.
the_output_node = json_object_new_object();
json_object_object_add(the_output_node, kSprinklerEntry, the_root_node);
blobmsg_add_json_from_string (&theBuf, theWriter.write(theOutput).c_str());
theResult = ubus_send_reply (aCtx, aReq, theBuf.head);
blob_buf_free (&theBuf);
return theResult;
blob_buf_init(&the_buf, 0);
blobmsg_add_json_from_string(&the_buf, json_object_to_json_string(the_output_node));
the_result = ubus_send_reply(a_ctx, a_req, the_buf.head);
blob_buf_free(&the_buf);
json_object_put(the_root_node);
json_object_put(the_output_node);
return the_result;
}

View File

@@ -29,28 +29,29 @@
/*------------------------------- INCLUDES ----------------------------------*/
#include <stdint.h>
#include <ubus-cpp/UBusObject.h>
#include <ubus-cpp/ubus-object.h>
/*--------------------------------- Define ----------------------------------*/
class Devices;
class DevicesManager;
/*--------------------------------- CLASS ----------------------------------*/
class CapabilitiesSprinklersModel : public UBusObject {
class CapabilitiesSprinklersModel : public UBusObject
{
public:
CapabilitiesSprinklersModel (Devices *aDevice);
~CapabilitiesSprinklersModel (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*);
int Put (struct ubus_context*, struct ubus_request_data*, struct blob_attr*);
int Post (struct ubus_context*, struct ubus_request_data*, struct blob_attr*);
private:
Devices *mDevices;
public:
CapabilitiesSprinklersModel(DevicesManager *a_device_manager);
~CapabilitiesSprinklersModel(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 *);
int put(struct ubus_context *, struct ubus_request_data *, struct blob_attr *);
int post(struct ubus_context *, struct ubus_request_data *, struct blob_attr *);
private:
DevicesManager *m_devices_manager;
};
#endif /* _UBUS_CAPABILITIES_SPRINKLERS_MODEL_H */