wip replace jsoncpp by json-c

This commit is contained in:
2018-04-04 23:07:32 +02:00
parent a7b3344363
commit 84b00beeb2
13 changed files with 141 additions and 117 deletions

View File

@@ -153,30 +153,28 @@ std::string DevicesManager::get(const std::string &a_capability)
* *
* @brief get the device of a specific capabilities, with the ID. * @brief get the device of a specific capabilities, with the ID.
*/ */
#if 0 int DevicesManager::set(const std::string &a_capability, struct json_object *a_node)
int Devices::set (const std::string &aCapability, Json::Value anElement)
{ {
int theResult = -1; int the_result = -1;
if (aCapability == "Lights") { if (a_capability == kLightEntry)
{
theResult = mLights.set (anElement); the_result = m_lights.set(a_node);
} }
else if (aCapability == "Shutters") { else if (a_capability == kShutterEntry)
{
theResult = mShutters.set (anElement); the_result = m_shutters.set(a_node);
} }
else if (aCapability == "Sprinklers") { else if (a_capability == kSprinklerEntry)
{
theResult = mSprinklers.set (anElement); the_result = m_sprinklers.set(a_node);
} }
if (theResult == 0) if (the_result == 0)
save(); save();
return theResult; return the_result;
} }
#endif
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn set * @fn set
@@ -200,7 +198,8 @@ int DevicesManager::set(const std::string &a_capability, int an_id, bool a_state
the_result = m_sprinklers.set(an_id, a_state); the_result = m_sprinklers.set(an_id, a_state);
} }
if (the_result == 0) { if (the_result == 0)
{
save(); save();
} }

View File

@@ -56,7 +56,7 @@ class DevicesManager
int save(void); int save(void);
std::string get(const std::string &a_capability); std::string get(const std::string &a_capability);
//int set (const std::string &aCapability, Json::Value anElement); 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(const std::string &a_capability, int an_id, bool a_state);
private: private:

View File

@@ -110,6 +110,32 @@ struct json_object *LightDevice::to_json(void)
return the_root_node; return the_root_node;
} }
/*! ----------------------------------------------------------------------------
* @fn set
*
* @brief set the new light state.
*/
int LightDevice::set(struct json_object *a_node)
{
int the_id;
bool the_state;
struct json_object *the_value_node;
// id
if (json_object_object_get_ex(a_node, k_entry_id, &the_value_node))
{
the_id = json_object_get_int(the_value_node);
}
// state
if (json_object_object_get_ex(a_node, k_entry_state, &the_value_node))
{
the_state = json_object_get_boolean(the_value_node);
}
return set(the_id, the_state);
}
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn set * @fn set
* *
@@ -128,21 +154,3 @@ int LightDevice::set(int an_id, bool a_state)
} }
return -1; return -1;
} }
/*! ----------------------------------------------------------------------------
* @fn set
*
* @brief set the new light state.
*/
#if 0
int LightDevice::set(struct json_object *a_node)
{
int theID;
bool theState;
theID = anElement["id"].asInt();
theState = anElement["state"].asBool();
return set (theID, theState);
}
#endif

View File

@@ -46,7 +46,7 @@ class LightDevice : public Device
int from_json(struct json_object *a_node); int from_json(struct json_object *a_node);
struct json_object *to_json(void); struct json_object *to_json(void);
//int set(struct json_object *a_node); int set(struct json_object *a_node);
int set(int anID, bool aState); int set(int anID, bool aState);
private: private:

View File

@@ -33,15 +33,6 @@
#include "devices/light.h" #include "devices/light.h"
/*------------------------------- DEFINES ----------------------------------*/
#define k_entry_id "id"
#define k_entry_name "name"
#define k_entry_speach_name "speach_name"
#define k_entry_zone "zone"
#define k_entry_state "state"
#define k_entry_sender "sender"
#define k_entry_interruptor "interruptor"
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn Light * @fn Light

View File

@@ -32,6 +32,16 @@
#include <string> #include <string>
/*------------------------------- DEFINES ----------------------------------*/
#define k_entry_id "id"
#define k_entry_name "name"
#define k_entry_speach_name "speach_name"
#define k_entry_zone "zone"
#define k_entry_state "state"
#define k_entry_sender "sender"
#define k_entry_interruptor "interruptor"
/*---------------------------------- Deps -----------------------------------*/ /*---------------------------------- Deps -----------------------------------*/
struct json_object; struct json_object;

View File

@@ -33,17 +33,6 @@
#include "devices/shutter.h" #include "devices/shutter.h"
/*------------------------------- DEFINES ----------------------------------*/
#define k_entry_id "id"
#define k_entry_name "name"
#define k_entry_speach_name "speach_name"
#define k_entry_zone "zone"
#define k_entry_state "state"
#define k_entry_sender "sender"
#define k_entry_interruptor "interruptor"
#define k_entry_speed_up "speed_up"
#define k_entry_speed_down "speed_down"
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn Shutter * @fn Shutter

View File

@@ -32,6 +32,18 @@
#include <string> #include <string>
/*------------------------------- DEFINES ----------------------------------*/
#define k_entry_id "id"
#define k_entry_name "name"
#define k_entry_speach_name "speach_name"
#define k_entry_zone "zone"
#define k_entry_state "state"
#define k_entry_sender "sender"
#define k_entry_interruptor "interruptor"
#define k_entry_speed_up "speed_up"
#define k_entry_speed_down "speed_down"
/*---------------------------------- Deps -----------------------------------*/ /*---------------------------------- Deps -----------------------------------*/
struct json_object; struct json_object;

View File

@@ -110,6 +110,32 @@ struct json_object *SprinklerDevice::to_json(void)
return the_root_node; return the_root_node;
} }
/*! ----------------------------------------------------------------------------
* @fn set
*
* @brief set the new Sprinkler state.
*/
int SprinklerDevice::set(struct json_object *a_node)
{
int the_id;
bool the_state;
struct json_object *the_value_node;
// id
if (json_object_object_get_ex(a_node, k_entry_id, &the_value_node))
{
the_id = json_object_get_int(the_value_node);
}
// state
if (json_object_object_get_ex(a_node, k_entry_state, &the_value_node))
{
the_state = json_object_get_boolean(the_value_node);
}
return set(the_id, the_state);
}
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn set * @fn set
* *
@@ -129,21 +155,3 @@ int SprinklerDevice::set(int an_id, bool a_state)
return -1; return -1;
} }
/*! ----------------------------------------------------------------------------
* @fn set
*
* @brief set the new Sprinkler state.
*/
#if 0
int SprinklerDevice::set(struct json_object *a_node)
{
int theID;
bool theState;
theID = anElement["id"].asInt();
theState = anElement["state"].asBool();
return set (theID, theState);
}
#endif

View File

@@ -47,7 +47,7 @@ class SprinklerDevice : public Device
int from_json(struct json_object *a_node); int from_json(struct json_object *a_node);
struct json_object *to_json(void); struct json_object *to_json(void);
//int set(struct json_object *a_node); int set(struct json_object *a_node);
int set(int an_id, bool a_state); int set(int an_id, bool a_state);
private: private:

View File

@@ -33,12 +33,8 @@
#include "devices/sprinkler.h" #include "devices/sprinkler.h"
/*------------------------------- DEFINES ----------------------------------*/
#define k_entry_id "id" #define k_entry_station "station"
#define k_entry_name "name"
#define k_entry_speach_name "speach_name"
#define k_entry_state "state"
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn Sprinkler * @fn Sprinkler
@@ -146,15 +142,18 @@ uint16_t Sprinkler::get_id(void)
*/ */
int Sprinkler::send_state(void) int Sprinkler::send_state(void)
{ {
#if 0 int the_result;
UBusCall theCmd; std::string the_output_result;
Json::Value theParam; struct json_object *the_parameter_doc;
Json::StyledWriter theWriter; UBusCall the_cmd;
std::string theResult;
theParam["station"] = mID; the_parameter_doc = json_object_new_object();
theParam["state"] = mState; json_object_object_add(the_parameter_doc, k_entry_station, json_object_new_int(m_id));
json_object_object_add(the_parameter_doc, k_entry_state, json_object_new_boolean(m_state));
return theCmd.Exec ("sprinklers", "set", theWriter.write(theParam).c_str(),theResult); the_result = the_cmd.exec("sprinklers", "set", json_object_to_json_string(the_parameter_doc), the_output_result);
#endif
json_object_put(the_parameter_doc);
return the_result;
} }

View File

@@ -32,6 +32,13 @@
#include <string> #include <string>
/*------------------------------- DEFINES ----------------------------------*/
#define k_entry_id "id"
#define k_entry_name "name"
#define k_entry_speach_name "speach_name"
#define k_entry_state "state"
/*---------------------------------- Deps -----------------------------------*/ /*---------------------------------- Deps -----------------------------------*/
struct json_object; struct json_object;

View File

@@ -53,8 +53,7 @@ static ObjectType gCapabilitiesLightsUbus_types(
* *
* @brief Constructor of the UBus Mixer Volume. * @brief Constructor of the UBus Mixer Volume.
*/ */
CapabilitiesLightsModel::CapabilitiesLightsModel(DevicesManager *a_device_manager) : CapabilitiesLightsModel::CapabilitiesLightsModel(DevicesManager *a_device_manager) : UBusObject(gCapabilitiesLightsUbus_types, "domo.capabilities.lights"),
UBusObject(gCapabilitiesLightsUbus_types, "domo.capabilities.lights"),
m_devices_manager(a_device_manager) m_devices_manager(a_device_manager)
{ {
} }
@@ -120,41 +119,43 @@ int CapabilitiesLightsModel::put(struct ubus_context *, struct ubus_request_data
*/ */
int CapabilitiesLightsModel::post(struct ubus_context *a_ctx, struct ubus_request_data *a_req, struct blob_attr *a_msg) int CapabilitiesLightsModel::post(struct ubus_context *a_ctx, struct ubus_request_data *a_req, struct blob_attr *a_msg)
{ {
#if 0 int the_result = 0;
int theResult = 0; struct blob_buf the_buf = {0};
struct blob_buf theBuf = {0}; char *the_string;
char *theString = blobmsg_format_json(aMsg, true); struct json_object *the_root_node, *the_output_node;
Json::Reader theReader;
Json::StyledWriter theWriter;
Json::Value theRoot;
Json::Value theOutput;
Json::Value theElement;
fprintf(stderr, "CapabilitiesLights::Post \n"); fprintf(stderr, "CapabilitiesLights::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("Lights", theRoot) < 0) if (m_devices_manager->set(kLightEntry, 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["Lights"] = theElement; // Create the output node.
the_output_node = json_object_new_object();
json_object_object_add(the_output_node, kLightEntry, 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);
#endif json_object_put(the_output_node);
return the_result;
} }