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.cpp
${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/timers/event.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
) )
# $ENV{SRC_DIR}/src/prog/domod/src/timers/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/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/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

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

View File

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

View File

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