diff --git a/src/prog/domod/src/devices/device.cpp b/src/prog/domod/src/devices/device.cpp index 98750453..1f17aacc 100644 --- a/src/prog/domod/src/devices/device.cpp +++ b/src/prog/domod/src/devices/device.cpp @@ -57,7 +57,7 @@ Device::~Device (void) * * @brief Load Device Object From a Json Object stringified. */ -int Device::from_json (const std::string &a_json) +int Device::from_json (struct json_object *a_node) { fprintf(stderr, "Device::load_from_json...\n"); return 0; diff --git a/src/prog/domod/src/devices/device.h b/src/prog/domod/src/devices/device.h index 5dd1460e..8ea27417 100644 --- a/src/prog/domod/src/devices/device.h +++ b/src/prog/domod/src/devices/device.h @@ -42,7 +42,7 @@ class Device Device(void); virtual ~Device(void); - virtual int from_json(const std::string &a_json); + virtual int from_json(struct json_object *a_node); virtual struct json_object *to_json(void) const; protected: diff --git a/src/prog/domod/src/devices/light-device.cpp b/src/prog/domod/src/devices/light-device.cpp index 64c93913..cf397fe0 100644 --- a/src/prog/domod/src/devices/light-device.cpp +++ b/src/prog/domod/src/devices/light-device.cpp @@ -27,10 +27,15 @@ #include +#include + #include "devices/light.h" #include "devices/light-device.h" +#define k_entry_speach_name "speach_name" +#define k_entry_data "data" + /*! ---------------------------------------------------------------------------- * @fn LightDevice * @@ -56,18 +61,31 @@ LightDevice::~LightDevice(void) */ int LightDevice::from_json(struct json_object *a_node) { + struct json_object *the_value_node, *the_data_node; fprintf(stderr, "LightDevice::load_from_json\n"); -#if 0 - mspeach_name = anElem["speach_name"].asString(); - for (Json::Value& theElement : anElem["data"]) { - Light theLight; - if (theLight.load_from_json (theElement) == 0) { + // speach name + if (json_object_object_get_ex(a_node, k_entry_speach_name, &the_value_node)) { - mLights.push_back (theLight); + m_speach_name = json_object_get_string(the_value_node); + } + // Get Light + else 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_light_node; + + the_len = json_object_array_length(the_data_node); + for (int i = 0; i < the_len; i++) + { + Light the_light; + the_light_node = json_object_array_get_idx(the_data_node, i); + the_light.from_json(the_light_node); + + m_lights.push_back (the_light); } } -#endif + return 0; } @@ -78,26 +96,23 @@ int LightDevice::from_json(struct json_object *a_node) */ struct json_object *LightDevice::to_json(void) { -#if 0 - Json::Value theResult; - Json::Value data_json(Json::arrayValue); - std::vector::iterator theLight_it; + struct json_object *the_root_node, *the_data_node; + std::vector::iterator the_light_it; - fprintf (stderr, "LightDevice::to_json\n"); + the_root_node = json_object_new_object(); + the_data_node = json_object_new_array(); - for (theLight_it = mLights.begin(); theLight_it != mLights.end(); theLight_it++) { - - data_json.append ((*theLight_it).to_json()); + // 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); + + + for (the_light_it = m_lights.begin(); the_light_it != m_lights.end(); the_light_it++) { + json_object_array_add(the_data_node, (*the_light_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; } /*! ---------------------------------------------------------------------------- diff --git a/src/prog/domod/src/devices/light-device.h b/src/prog/domod/src/devices/light-device.h index 4a72ddd5..a7a9e7a8 100644 --- a/src/prog/domod/src/devices/light-device.h +++ b/src/prog/domod/src/devices/light-device.h @@ -50,7 +50,7 @@ class LightDevice : public Device int set(int anID, bool aState); private: - std::vector mLights; + std::vector m_lights; }; #endif /* _LIGHT_DEVICE_H */ diff --git a/src/prog/domod/src/devices/light.cpp b/src/prog/domod/src/devices/light.cpp index aed145b3..5890f03f 100644 --- a/src/prog/domod/src/devices/light.cpp +++ b/src/prog/domod/src/devices/light.cpp @@ -27,11 +27,23 @@ #include +#include + #include #include "devices/light.h" +#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 * @@ -59,15 +71,37 @@ Light::~Light (void) */ int Light::from_json (struct json_object *a_node) { -#if 0 - mID = anElem["id"].asInt(); - mName = anElem["name"].asString(); - mSpeach_name = anElem["speach_name"].asString(); - mZone = anElem["zone"].asString(); - mState = anElem["state"].asBool(); - mSender = anElem["sender"].asInt(); - mInterruptor = anElem["interruptor"].asInt(); -#endif + struct json_object *the_value_node; + + // 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); + } + // zone + if (json_object_object_get_ex(a_node, k_entry_zone, &the_value_node)) { + m_zone = 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); + } + // sender + if (json_object_object_get_ex(a_node, k_entry_sender, &the_value_node)) { + m_sender = json_object_get_int(the_value_node); + } + // interruptor + if (json_object_object_get_ex(a_node, k_entry_interruptor, &the_value_node)) { + m_interruptor = json_object_get_int(the_value_node); + } + return 0; } @@ -79,19 +113,26 @@ int Light::from_json (struct json_object *a_node) */ struct json_object *Light::to_json (void) const { -#if 0 - Json::Value aResult(Json::objectValue); - fprintf (stderr, "Light::to_json\n"); - aResult["id"] = mID; - aResult["name"] = mName; - aResult["speach_name"] = mSpeach_name; - aResult["zone"] = mZone; - aResult["state"] = mState; - aResult["sender"] = mSender; - aResult["interruptor"] = mInterruptor; + 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())); + // zone + json_object_object_add(the_root_node, k_entry_zone, json_object_new_string(m_zone.c_str())); + // state + json_object_object_add(the_root_node, k_entry_state, json_object_new_boolean(m_state)); + // sender + json_object_object_add(the_root_node, k_entry_sender, json_object_new_int(m_sender)); + // interruptor + json_object_object_add(the_root_node, k_entry_interruptor, json_object_new_int(m_interruptor)); + + return the_root_node; }