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.
*/
#if 0
int Devices::set (const std::string &aCapability, Json::Value anElement)
int DevicesManager::set(const std::string &a_capability, struct json_object *a_node)
{
int theResult = -1;
int the_result = -1;
if (aCapability == "Lights") {
theResult = mLights.set (anElement);
if (a_capability == kLightEntry)
{
the_result = m_lights.set(a_node);
}
else if (aCapability == "Shutters") {
theResult = mShutters.set (anElement);
else if (a_capability == kShutterEntry)
{
the_result = m_shutters.set(a_node);
}
else if (aCapability == "Sprinklers") {
theResult = mSprinklers.set (anElement);
else if (a_capability == kSprinklerEntry)
{
the_result = m_sprinklers.set(a_node);
}
if (theResult == 0)
save ();
if (the_result == 0)
save();
return theResult;
return the_result;
}
#endif
/*! ----------------------------------------------------------------------------
* @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);
}
if (the_result == 0) {
if (the_result == 0)
{
save();
}

View File

@@ -56,7 +56,7 @@ class DevicesManager
int save(void);
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);
private:

View File

@@ -110,6 +110,32 @@ struct json_object *LightDevice::to_json(void)
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
*
@@ -128,21 +154,3 @@ int LightDevice::set(int an_id, bool a_state)
}
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);
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);
private:

View File

@@ -33,15 +33,6 @@
#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

View File

@@ -32,6 +32,16 @@
#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 -----------------------------------*/
struct json_object;

View File

@@ -33,17 +33,6 @@
#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

View File

@@ -32,6 +32,18 @@
#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 -----------------------------------*/
struct json_object;

View File

@@ -110,6 +110,32 @@ struct json_object *SprinklerDevice::to_json(void)
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
*
@@ -129,21 +155,3 @@ int SprinklerDevice::set(int an_id, bool a_state)
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);
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);
private:

View File

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

View File

@@ -32,6 +32,13 @@
#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 -----------------------------------*/
struct json_object;

View File

@@ -53,8 +53,7 @@ static ObjectType gCapabilitiesLightsUbus_types(
*
* @brief Constructor of the UBus Mixer Volume.
*/
CapabilitiesLightsModel::CapabilitiesLightsModel(DevicesManager *a_device_manager) :
UBusObject(gCapabilitiesLightsUbus_types, "domo.capabilities.lights"),
CapabilitiesLightsModel::CapabilitiesLightsModel(DevicesManager *a_device_manager) : UBusObject(gCapabilitiesLightsUbus_types, "domo.capabilities.lights"),
m_devices_manager(a_device_manager)
{
}
@@ -76,7 +75,7 @@ CapabilitiesLightsModel::~CapabilitiesLightsModel(void)
int CapabilitiesLightsModel::get(struct ubus_context *a_ctx, struct ubus_request_data *a_req, struct blob_attr *a_msg)
{
int the_result;
struct blob_buf the_buf = { 0 };
struct blob_buf the_buf = {0};
blob_buf_init(&the_buf, 0);
@@ -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)
{
#if 0
int theResult = 0;
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 the_result = 0;
struct blob_buf the_buf = {0};
char *the_string;
struct json_object *the_root_node, *the_output_node;
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");
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");
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;
#endif
json_object_put(the_root_node);
json_object_put(the_output_node);
return the_result;
}