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 -----------------------------------*/ /*---------------------------------- Deps -----------------------------------*/
struct json_object;
/*--------------------------------- CLASS ----------------------------------*/ /*--------------------------------- CLASS ----------------------------------*/
class Light class Light

View File

@@ -90,26 +90,24 @@ int ShutterDevice::from_json(struct json_object *a_node)
*/ */
struct json_object *ShutterDevice::to_json(void) struct json_object *ShutterDevice::to_json(void)
{ {
#if 0 struct json_object *the_root_node, *the_data_node;
Json::Value theResult; std::vector<Shutter>::iterator the_shutter_it;
Json::Value data_json(Json::arrayValue); fprintf(stderr, "ShutterDevice::to_json\n");
std::vector<Shutter>::iterator theShutter_it;
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.append ((*theShutter_it).to_json()); // data
json_object_object_add(the_root_node, k_entry_data, the_data_node);
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; return the_root_node;
theResult["data"] = data_json;
if (bDataOnly == true)
return data_json;
return theResult;
#endif
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------

View File

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

View File

@@ -23,94 +23,99 @@
* *
*/ */
/*------------------------------- INCLUDES ----------------------------------*/ /*------------------------------- INCLUDES ----------------------------------*/
#include <cstdio> #include <cstdio>
#include "devices/sprinkler-device.h" #include <json-c/json.h>
#include "devices/sprinkler-device.h"
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn SprinklerDevice * @fn SprinklerDevice
* *
* @brief Constructor of the SprinklerDevice Object. * @brief Constructor of the SprinklerDevice Object.
*/ */
SprinklerDevice::SprinklerDevice (void) : Device() SprinklerDevice::SprinklerDevice(void) : Device()
{ {
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn ~SprinklerDevice * @fn ~SprinklerDevice
* *
* @brief Destructor of the Device Object. * @brief Destructor of the Device Object.
*/ */
SprinklerDevice::~SprinklerDevice (void) SprinklerDevice::~SprinklerDevice(void)
{ {
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn from_json * @fn from_json
* *
* @brief Load SprinklerDevice Object From a Json Object. * @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"); struct json_object *the_data_node;
#if 0 fprintf(stderr, "SprinklerDevice::load_from_json\n");
mspeach_name = anElem["speach_name"].asString();
for (Json::Value& theElement : anElem["data"]) { // speach name
Sprinkler theSprinkler; Device::from_json(a_node);
if (theSprinkler.load_from_json (theElement) == 0) {
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; return 0;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn to_json * @fn to_json
* *
* @brief Export SprinklerDevice Object as JSON Object. * @brief Export SprinklerDevice Object as JSON Object.
*/ */
struct json_object *SprinklerDevice::to_json (void) struct json_object *SprinklerDevice::to_json(void)
{ {
#if 0 struct json_object *the_root_node, *the_data_node;
Json::Value theResult; std::vector<Sprinkler>::iterator the_sprintkler_it;
Json::Value data_json(Json::arrayValue); fprintf(stderr, "SprinklerDevice::to_json\n");
std::vector<Sprinkler>::iterator theSprinkler_it;
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.append ((*theSprinkler_it).to_json()); // data
json_object_object_add(the_root_node, k_entry_data, the_data_node);
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; return the_root_node;
theResult["data"] = data_json;
if (bDataOnly == true)
return data_json;
return theResult;
#endif
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn set * @fn set
* *
* @brief set the new Sprinkler state. * @brief set the new Sprinkler state.
*/ */
int SprinklerDevice::set (struct json_object *a_node) int SprinklerDevice::set(struct json_object *a_node)
{ {
#if 0 #if 0
int theID; int theID;
@@ -128,7 +133,7 @@ int SprinklerDevice::set (struct json_object *a_node)
* *
* @brief set the new Sprinkler state. * @brief set the new Sprinkler state.
*/ */
int SprinklerDevice::set (int anID, bool aState) int SprinklerDevice::set(int anID, bool aState)
{ {
#if 0 #if 0
std::vector<Sprinkler>::iterator theSprinkler_it; std::vector<Sprinkler>::iterator theSprinkler_it;
@@ -140,6 +145,6 @@ int SprinklerDevice::set (int anID, bool aState)
return (*theSprinkler_it).update (aState); return (*theSprinkler_it).update (aState);
} }
} }
#endif #endif
return -1; return -1;
} }

View File

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

View File

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