Fix New device format.

This commit is contained in:
jbnadal
2018-04-30 15:48:20 +02:00
parent 8411d9dba8
commit 93206103a3
3 changed files with 20 additions and 52 deletions

View File

@@ -58,23 +58,19 @@ LightDevice::~LightDevice(void)
*/ */
int LightDevice::from_json(struct json_object *a_node) int LightDevice::from_json(struct json_object *a_node)
{ {
struct json_object *the_devices_node;
fprintf(stderr, "LightDevice::load_from_json\n"); fprintf(stderr, "LightDevice::load_from_json\n");
// speach name // Get Lights
Device::from_json(a_node); if (json_object_get_type(a_node) == json_type_array)
// Get Light
if (json_object_object_get_ex(a_node, k_entry_devices, &the_devices_node) && (json_object_get_type(the_devices_node) == json_type_array))
{ {
int the_len; int the_len;
struct json_object *the_light_node; struct json_object *the_light_node;
the_len = json_object_array_length(the_devices_node); the_len = json_object_array_length(a_node);
for (int i = 0; i < the_len; i++) for (int i = 0; i < the_len; i++)
{ {
Light the_light; Light the_light;
the_light_node = json_object_array_get_idx(the_devices_node, i); the_light_node = json_object_array_get_idx(a_node, i);
the_light.from_json(the_light_node); the_light.from_json(the_light_node);
m_lights.push_back(the_light); m_lights.push_back(the_light);
@@ -91,20 +87,14 @@ int LightDevice::from_json(struct json_object *a_node)
*/ */
struct json_object *LightDevice::to_json(void) struct json_object *LightDevice::to_json(void)
{ {
struct json_object *the_root_node, *the_devices_node; struct json_object *the_root_node;
std::vector<Light>::iterator the_light_it; std::vector<Light>::iterator the_light_it;
the_root_node = json_object_new_object(); the_root_node = json_object_new_array();
the_devices_node = json_object_new_array();
// 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_devices, the_devices_node);
for (the_light_it = m_lights.begin(); the_light_it != m_lights.end(); the_light_it++) for (the_light_it = m_lights.begin(); the_light_it != m_lights.end(); the_light_it++)
{ {
json_object_array_add(the_devices_node, (*the_light_it).to_json()); json_object_array_add(the_root_node, (*the_light_it).to_json());
} }
return the_root_node; return the_root_node;

View File

@@ -58,24 +58,19 @@ ShutterDevice::~ShutterDevice(void)
*/ */
int ShutterDevice::from_json(struct json_object *a_node) int ShutterDevice::from_json(struct json_object *a_node)
{ {
struct json_object *the_devices_node;
fprintf(stderr, "ShutterDevice::load_from_json\n"); fprintf(stderr, "ShutterDevice::load_from_json\n");
// speach name
Device::from_json(a_node);
// Get Shutter // Get Shutter
if (json_object_object_get_ex(a_node, k_entry_devices, &the_devices_node) && (json_object_get_type(the_devices_node) == json_type_array)) if (json_object_get_type(a_node) == json_type_array)
{ {
int the_len; int the_len;
struct json_object *the_shutter_node; struct json_object *the_shutter_node;
the_len = json_object_array_length(the_devices_node); the_len = json_object_array_length(a_node);
for (int i = 0; i < the_len; i++) for (int i = 0; i < the_len; i++)
{ {
Shutter the_shutter; Shutter the_shutter;
the_shutter_node = json_object_array_get_idx(the_devices_node, i); the_shutter_node = json_object_array_get_idx(a_node, i);
the_shutter.from_json(the_shutter_node); the_shutter.from_json(the_shutter_node);
m_shutters.push_back(the_shutter); m_shutters.push_back(the_shutter);
@@ -92,21 +87,15 @@ int ShutterDevice::from_json(struct json_object *a_node)
*/ */
struct json_object *ShutterDevice::to_json(void) struct json_object *ShutterDevice::to_json(void)
{ {
struct json_object *the_root_node, *the_devices_node; struct json_object *the_root_node;
std::vector<Shutter>::iterator the_shutter_it; 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_root_node = json_object_new_array();
the_devices_node = json_object_new_array();
// speach_name
json_object_object_add(the_root_node, k_entry_speach_name, json_object_new_string(m_speach_name.c_str()));
// devices
json_object_object_add(the_root_node, k_entry_devices, the_devices_node);
for (the_shutter_it = m_shutters.begin(); the_shutter_it != m_shutters.end(); the_shutter_it++) for (the_shutter_it = m_shutters.begin(); the_shutter_it != m_shutters.end(); the_shutter_it++)
{ {
json_object_array_add(the_devices_node, (*the_shutter_it).to_json()); json_object_array_add(the_root_node, (*the_shutter_it).to_json());
} }
return the_root_node; return the_root_node;

View File

@@ -58,24 +58,19 @@ SprinklerDevice::~SprinklerDevice(void)
*/ */
int SprinklerDevice::from_json(struct json_object *a_node) int SprinklerDevice::from_json(struct json_object *a_node)
{ {
struct json_object *the_devices_node;
fprintf(stderr, "SprinklerDevice::load_from_json\n"); fprintf(stderr, "SprinklerDevice::load_from_json\n");
// speach name // Get Sprinklers
Device::from_json(a_node); if (json_object_get_type(a_node) == json_type_array)
// Get Sprinkler
if (json_object_object_get_ex(a_node, k_entry_devices, &the_devices_node) && (json_object_get_type(the_devices_node) == json_type_array))
{ {
int the_len; int the_len;
struct json_object *the_sprinkler_node; struct json_object *the_sprinkler_node;
the_len = json_object_array_length(the_devices_node); the_len = json_object_array_length(a_node);
for (int i = 0; i < the_len; i++) for (int i = 0; i < the_len; i++)
{ {
Sprinkler the_sprinkler; Sprinkler the_sprinkler;
the_sprinkler_node = json_object_array_get_idx(the_devices_node, i); the_sprinkler_node = json_object_array_get_idx(a_node, i);
the_sprinkler.from_json(the_sprinkler_node); the_sprinkler.from_json(the_sprinkler_node);
m_sprinklers.push_back(the_sprinkler); m_sprinklers.push_back(the_sprinkler);
@@ -92,21 +87,15 @@ int SprinklerDevice::from_json(struct json_object *a_node)
*/ */
struct json_object *SprinklerDevice::to_json(void) struct json_object *SprinklerDevice::to_json(void)
{ {
struct json_object *the_root_node, *the_devices_node; struct json_object *the_root_node;
std::vector<Sprinkler>::iterator the_sprintkler_it; 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_root_node = json_object_new_array();
the_devices_node = json_object_new_array();
// 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_devices, the_devices_node);
for (the_sprintkler_it = m_sprinklers.begin(); the_sprintkler_it != m_sprinklers.end(); the_sprintkler_it++) for (the_sprintkler_it = m_sprinklers.begin(); the_sprintkler_it != m_sprinklers.end(); the_sprintkler_it++)
{ {
json_object_array_add(the_devices_node, (*the_sprintkler_it).to_json()); json_object_array_add(the_root_node, (*the_sprintkler_it).to_json());
} }
return the_root_node; return the_root_node;