From 84b00beeb233b9de0ec17830ab47e98f4e82f635 Mon Sep 17 00:00:00 2001 From: NADAL Jean-Baptiste Date: Wed, 4 Apr 2018 23:07:32 +0200 Subject: [PATCH] wip replace jsoncpp by json-c --- .../domod/src/devices/devices-manager.cpp | 33 ++++++------ src/prog/domod/src/devices/devices-manager.h | 2 +- src/prog/domod/src/devices/light-device.cpp | 44 ++++++++------- src/prog/domod/src/devices/light-device.h | 2 +- src/prog/domod/src/devices/light.cpp | 9 ---- src/prog/domod/src/devices/light.h | 10 ++++ src/prog/domod/src/devices/shutter.cpp | 11 ---- src/prog/domod/src/devices/shutter.h | 12 +++++ .../domod/src/devices/sprinkler-device.cpp | 44 ++++++++------- src/prog/domod/src/devices/sprinkler-device.h | 2 +- src/prog/domod/src/devices/sprinkler.cpp | 29 +++++----- src/prog/domod/src/devices/sprinkler.h | 7 +++ .../domod/src/ubus/capabilities_lights.cpp | 53 ++++++++++--------- 13 files changed, 141 insertions(+), 117 deletions(-) diff --git a/src/prog/domod/src/devices/devices-manager.cpp b/src/prog/domod/src/devices/devices-manager.cpp index 5df6e6a6..22fbdbd4 100644 --- a/src/prog/domod/src/devices/devices-manager.cpp +++ b/src/prog/domod/src/devices/devices-manager.cpp @@ -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(); } diff --git a/src/prog/domod/src/devices/devices-manager.h b/src/prog/domod/src/devices/devices-manager.h index e0d66732..59d9ab7f 100644 --- a/src/prog/domod/src/devices/devices-manager.h +++ b/src/prog/domod/src/devices/devices-manager.h @@ -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: diff --git a/src/prog/domod/src/devices/light-device.cpp b/src/prog/domod/src/devices/light-device.cpp index 0be6fd37..621d328b 100644 --- a/src/prog/domod/src/devices/light-device.cpp +++ b/src/prog/domod/src/devices/light-device.cpp @@ -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 diff --git a/src/prog/domod/src/devices/light-device.h b/src/prog/domod/src/devices/light-device.h index 818ef836..a7a9e7a8 100644 --- a/src/prog/domod/src/devices/light-device.h +++ b/src/prog/domod/src/devices/light-device.h @@ -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: diff --git a/src/prog/domod/src/devices/light.cpp b/src/prog/domod/src/devices/light.cpp index 077accae..30add525 100644 --- a/src/prog/domod/src/devices/light.cpp +++ b/src/prog/domod/src/devices/light.cpp @@ -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 diff --git a/src/prog/domod/src/devices/light.h b/src/prog/domod/src/devices/light.h index dd1e5afa..fdda6047 100644 --- a/src/prog/domod/src/devices/light.h +++ b/src/prog/domod/src/devices/light.h @@ -32,6 +32,16 @@ #include +/*------------------------------- 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; diff --git a/src/prog/domod/src/devices/shutter.cpp b/src/prog/domod/src/devices/shutter.cpp index c9bce4ee..4061a00c 100644 --- a/src/prog/domod/src/devices/shutter.cpp +++ b/src/prog/domod/src/devices/shutter.cpp @@ -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 diff --git a/src/prog/domod/src/devices/shutter.h b/src/prog/domod/src/devices/shutter.h index 8e24f3bc..52e42e5f 100644 --- a/src/prog/domod/src/devices/shutter.h +++ b/src/prog/domod/src/devices/shutter.h @@ -32,6 +32,18 @@ #include +/*------------------------------- 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; diff --git a/src/prog/domod/src/devices/sprinkler-device.cpp b/src/prog/domod/src/devices/sprinkler-device.cpp index bc2af3ff..829940b5 100644 --- a/src/prog/domod/src/devices/sprinkler-device.cpp +++ b/src/prog/domod/src/devices/sprinkler-device.cpp @@ -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 diff --git a/src/prog/domod/src/devices/sprinkler-device.h b/src/prog/domod/src/devices/sprinkler-device.h index 1a5843e9..d0b973a8 100644 --- a/src/prog/domod/src/devices/sprinkler-device.h +++ b/src/prog/domod/src/devices/sprinkler-device.h @@ -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: diff --git a/src/prog/domod/src/devices/sprinkler.cpp b/src/prog/domod/src/devices/sprinkler.cpp index 929559cd..5d9de596 100644 --- a/src/prog/domod/src/devices/sprinkler.cpp +++ b/src/prog/domod/src/devices/sprinkler.cpp @@ -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; - - theParam["station"] = mID; - theParam["state"] = mState; + int the_result; + std::string the_output_result; + struct json_object *the_parameter_doc; + UBusCall the_cmd; - return theCmd.Exec ("sprinklers", "set", theWriter.write(theParam).c_str(),theResult); -#endif + 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)); + + 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; } diff --git a/src/prog/domod/src/devices/sprinkler.h b/src/prog/domod/src/devices/sprinkler.h index dc3d32ce..6219b6cb 100644 --- a/src/prog/domod/src/devices/sprinkler.h +++ b/src/prog/domod/src/devices/sprinkler.h @@ -32,6 +32,13 @@ #include +/*------------------------------- 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; diff --git a/src/prog/domod/src/ubus/capabilities_lights.cpp b/src/prog/domod/src/ubus/capabilities_lights.cpp index 1c5bd7d1..83efa682 100644 --- a/src/prog/domod/src/ubus/capabilities_lights.cpp +++ b/src/prog/domod/src/ubus/capabilities_lights.cpp @@ -53,9 +53,8 @@ static ObjectType gCapabilitiesLightsUbus_types( * * @brief Constructor of the UBus Mixer Volume. */ -CapabilitiesLightsModel::CapabilitiesLightsModel(DevicesManager *a_device_manager) : - UBusObject(gCapabilitiesLightsUbus_types, "domo.capabilities.lights"), - m_devices_manager(a_device_manager) +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); @@ -86,7 +85,7 @@ int CapabilitiesLightsModel::get(struct ubus_context *a_ctx, struct ubus_request blob_buf_free(&the_buf); - return the_result; + return the_result; } /*! ---------------------------------------------------------------------------- @@ -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; }