wip continue to replace jsoncpp to json-c

This commit is contained in:
jbnadal
2018-04-04 10:58:28 +02:00
parent 5220d2b0d8
commit 98e209ef09
7 changed files with 113 additions and 75 deletions

View File

@@ -34,6 +34,8 @@
/*---------------------------------- Deps -----------------------------------*/
struct json_object;
/*--------------------------------- CLASS ----------------------------------*/
class Light

View File

@@ -90,26 +90,24 @@ int ShutterDevice::from_json(struct json_object *a_node)
*/
struct json_object *ShutterDevice::to_json(void)
{
#if 0
Json::Value theResult;
Json::Value data_json(Json::arrayValue);
std::vector<Shutter>::iterator theShutter_it;
struct json_object *the_root_node, *the_data_node;
std::vector<Shutter>::iterator the_shutter_it;
fprintf(stderr, "ShutterDevice::to_json\n");
fprintf (stderr, "ShutterDevice::to_json\n");
the_root_node = json_object_new_object();
the_data_node = json_object_new_array();
for (theShutter_it = mShutter.begin(); theShutter_it != mShutter.end(); theShutter_it++) {
// speach_name
json_object_object_add(the_root_node, k_entry_speach_name, json_object_new_string(m_speach_name.c_str()));
// data
json_object_object_add(the_root_node, k_entry_data, the_data_node);
data_json.append ((*theShutter_it).to_json());
for (the_shutter_it = m_shutters.begin(); the_shutter_it != m_shutters.end(); the_shutter_it++)
{
json_object_array_add(the_data_node, (*the_shutter_it).to_json());
}
theResult["speach_name"] = mspeach_name;
theResult["data"] = data_json;
if (bDataOnly == true)
return data_json;
return theResult;
#endif
return the_root_node;
}
/*! ----------------------------------------------------------------------------

View File

@@ -34,6 +34,8 @@
/*---------------------------------- Deps -----------------------------------*/
struct json_object;
/*--------------------------------- CLASS ----------------------------------*/
class Shutter

View File

@@ -23,94 +23,99 @@
*
*/
/*------------------------------- INCLUDES ----------------------------------*/
#include <cstdio>
#include "devices/sprinkler-device.h"
#include <json-c/json.h>
#include "devices/sprinkler-device.h"
/*! ----------------------------------------------------------------------------
* @fn SprinklerDevice
*
* @brief Constructor of the SprinklerDevice Object.
*/
SprinklerDevice::SprinklerDevice (void) : Device()
SprinklerDevice::SprinklerDevice(void) : Device()
{
}
/*! ----------------------------------------------------------------------------
* @fn ~SprinklerDevice
*
* @brief Destructor of the Device Object.
*/
SprinklerDevice::~SprinklerDevice (void)
SprinklerDevice::~SprinklerDevice(void)
{
}
/*! ----------------------------------------------------------------------------
* @fn from_json
*
* @brief Load SprinklerDevice Object From a Json Object.
*/
int SprinklerDevice::from_json (struct json_object *a_node)
int SprinklerDevice::from_json(struct json_object *a_node)
{
fprintf (stderr, "SprinklerDevice::load_from_json\n");
#if 0
mspeach_name = anElem["speach_name"].asString();
struct json_object *the_data_node;
fprintf(stderr, "SprinklerDevice::load_from_json\n");
for (Json::Value& theElement : anElem["data"]) {
Sprinkler theSprinkler;
if (theSprinkler.load_from_json (theElement) == 0) {
// speach name
Device::from_json(a_node);
mSprinkler.push_back (theSprinkler);
// Get Sprinkler
if (json_object_object_get_ex(a_node, k_entry_data, &the_data_node) && (json_object_get_type(the_data_node) == json_type_array))
{
int the_len;
struct json_object *the_sprinkler_node;
the_len = json_object_array_length(the_data_node);
for (int i = 0; i < the_len; i++)
{
Sprinkler the_sprinkler;
the_sprinkler_node = json_object_array_get_idx(the_data_node, i);
the_sprinkler.from_json(the_sprinkler_node);
m_sprinklers.push_back(the_sprinkler);
}
}
#endif
return 0;
}
/*! ----------------------------------------------------------------------------
* @fn to_json
*
* @brief Export SprinklerDevice Object as JSON Object.
*/
struct json_object *SprinklerDevice::to_json (void)
struct json_object *SprinklerDevice::to_json(void)
{
#if 0
Json::Value theResult;
Json::Value data_json(Json::arrayValue);
std::vector<Sprinkler>::iterator theSprinkler_it;
struct json_object *the_root_node, *the_data_node;
std::vector<Sprinkler>::iterator the_sprintkler_it;
fprintf(stderr, "SprinklerDevice::to_json\n");
fprintf (stderr, "SprinklerDevice::to_json\n");
the_root_node = json_object_new_object();
the_data_node = json_object_new_array();
for (theSprinkler_it = mSprinkler.begin(); theSprinkler_it != mSprinkler.end(); theSprinkler_it++) {
// speach_name
json_object_object_add(the_root_node, k_entry_speach_name, json_object_new_string(m_speach_name.c_str()));
// data
json_object_object_add(the_root_node, k_entry_data, the_data_node);
data_json.append ((*theSprinkler_it).to_json());
for (the_sprintkler_it = m_sprinklers.begin(); the_sprintkler_it != m_sprinklers.end(); the_sprintkler_it++)
{
json_object_array_add(the_data_node, (*the_sprintkler_it).to_json());
}
theResult["speach_name"] = mspeach_name;
theResult["data"] = data_json;
if (bDataOnly == true)
return data_json;
return theResult;
#endif
return the_root_node;
}
/*! ----------------------------------------------------------------------------
* @fn set
*
* @brief set the new Sprinkler state.
*/
int SprinklerDevice::set (struct json_object *a_node)
int SprinklerDevice::set(struct json_object *a_node)
{
#if 0
int theID;
@@ -128,7 +133,7 @@ int SprinklerDevice::set (struct json_object *a_node)
*
* @brief set the new Sprinkler state.
*/
int SprinklerDevice::set (int anID, bool aState)
int SprinklerDevice::set(int anID, bool aState)
{
#if 0
std::vector<Sprinkler>::iterator theSprinkler_it;

View File

@@ -51,7 +51,7 @@ class SprinklerDevice : public Device
int set(int an_id, bool a_state);
private:
std::vector<Sprinkler> m_sprinkler;
std::vector<Sprinkler> m_sprinklers;
};
#endif /* _SPRINKLER_DEVICE_H */

View File

@@ -27,10 +27,19 @@
#include <cstdio>
#include <json-c/json.h>
#include <ubus-cpp/ubus-call.h>
#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"
/*! ----------------------------------------------------------------------------
* @fn Sprinkler
*
@@ -56,12 +65,29 @@ Sprinkler::~Sprinkler(void)
*/
int Sprinkler::from_json(struct json_object *a_node)
{
#if 0
mID = anElem["id"].asInt();
mName = anElem["name"].asString();
mSpeach_name = anElem["speach_name"].asString();
mState = anElem["state"].asBool();
#endif
struct json_object *the_value_node;
// TODO: Should shared with Light.
// id
if (json_object_object_get_ex(a_node, k_entry_id, &the_value_node))
{
m_id = json_object_get_int(the_value_node);
}
// name
if (json_object_object_get_ex(a_node, k_entry_name, &the_value_node))
{
m_name = json_object_get_string(the_value_node);
}
// speach_name
if (json_object_object_get_ex(a_node, k_entry_speach_name, &the_value_node))
{
m_speach_name = json_object_get_string(the_value_node);
}
// state
if (json_object_object_get_ex(a_node, k_entry_state, &the_value_node))
{
m_state = json_object_get_boolean(the_value_node);
}
return 0;
}
@@ -72,16 +98,20 @@ int Sprinkler::from_json(struct json_object *a_node)
*/
struct json_object *Sprinkler::to_json(void) const
{
#if 0
Json::Value aResult(Json::objectValue);
fprintf (stderr, "Sprinkler::to_json\n");
aResult["id"] = mID;
aResult["name"] = mName;
aResult["speach_name"] = mSpeach_name;
aResult["state"] = mState;
struct json_object *the_root_node;
return aResult;
#endif
the_root_node = json_object_new_object();
// id
json_object_object_add(the_root_node, k_entry_id, json_object_new_int(m_id));
// name
json_object_object_add(the_root_node, k_entry_name, json_object_new_string(m_name.c_str()));
// speach_name
json_object_object_add(the_root_node, k_entry_speach_name, json_object_new_string(m_speach_name.c_str()));
// state
json_object_object_add(the_root_node, k_entry_state, json_object_new_boolean(m_state));
return the_root_node;
}
/*! ----------------------------------------------------------------------------

View File

@@ -34,17 +34,18 @@
/*---------------------------------- Deps -----------------------------------*/
struct json_object;
/*--------------------------------- CLASS ----------------------------------*/
class Sprinkler
{
public:
Sprinkler(void);
~Sprinkler(void);
int from_json(struct json_object *a_node);
struct json_object *to_json (void) const;
struct json_object *to_json(void) const;
int update(bool aState);
@@ -55,8 +56,8 @@ class Sprinkler
private:
uint16_t m_id;
std::string mName;
std::string mSpeach_name;
std::string m_name;
std::string m_speach_name;
bool m_state;
};