device manager now load data.
This commit is contained in:
@@ -29,6 +29,7 @@
|
||||
/*-------------------------------- INCLUDES ---------------------------------*/
|
||||
|
||||
#include <strings.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <json.h>
|
||||
#include <json_util.h>
|
||||
@@ -36,11 +37,14 @@
|
||||
#include "macro.h"
|
||||
#include "domo.h"
|
||||
|
||||
#include "outlet_dio.h"
|
||||
|
||||
#include "devices_manager.h"
|
||||
|
||||
/*----------------------------- LOCAL FUNCTIONS ----------------------------*/
|
||||
|
||||
int load_outlets(devices_manager_t *dm, struct json_object *node);
|
||||
struct json_object *save_outlets(devices_manager_t *dm);
|
||||
int create_outlet(struct json_object *node);
|
||||
|
||||
int load_shutters(devices_manager_t *dm, struct json_object *node);
|
||||
@@ -64,7 +68,7 @@ devices_manager_t *devices_manager_new(void)
|
||||
bzero((void *)devices_manager, sizeof(devices_manager_t));
|
||||
|
||||
// Initialize instance.
|
||||
devices_manager->outlets = qhashtbl(0, 0);
|
||||
devices_manager->outlets = qlist(0);
|
||||
devices_manager->shutters = qhashtbl(0, 0);
|
||||
devices_manager->sprinklers = qhashtbl(0, 0);
|
||||
|
||||
@@ -90,15 +94,28 @@ void devices_manager_free(devices_manager_t *devices_manager)
|
||||
{
|
||||
devices_manager->sprinklers->free(devices_manager->sprinklers);
|
||||
}
|
||||
|
||||
if (devices_manager->file_path != NULL)
|
||||
{
|
||||
free(devices_manager->file_path);
|
||||
}
|
||||
|
||||
free(devices_manager);
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
int devices_manager_load(devices_manager_t *dm)
|
||||
int devices_manager_load(devices_manager_t *dm, char *config_path)
|
||||
{
|
||||
struct json_object *the_root_node, *the_value_node;
|
||||
DEBUG("Devices::load...\n");
|
||||
DEBUG("Devices load...\n");
|
||||
|
||||
if (config_path == NULL)
|
||||
{
|
||||
fprintf(stderr, "Couldn't found the device config file.\n");
|
||||
return -1;
|
||||
}
|
||||
dm->file_path = config_path;
|
||||
|
||||
the_root_node = json_object_from_file(dm->file_path);
|
||||
if (the_root_node == NULL)
|
||||
@@ -132,8 +149,25 @@ int devices_manager_load(devices_manager_t *dm)
|
||||
|
||||
int devices_manager_save(devices_manager_t *dm)
|
||||
{
|
||||
DEBUG("TODO");
|
||||
return 0;
|
||||
int result;
|
||||
struct json_object *root_node;
|
||||
DEBUG("Devices save...\n");
|
||||
|
||||
root_node = json_object_new_object();
|
||||
|
||||
// Outlets
|
||||
json_object_object_add(root_node, kOutletEntry, save_outlets(dm));
|
||||
// Shutters
|
||||
//json_object_object_add(the_root_node, kShutterEntry, m_shutters.to_json());
|
||||
// Sprinklers
|
||||
//json_object_object_add(the_root_node, kSprinklerEntry, m_sprinklers.to_json());
|
||||
|
||||
result = json_object_to_file_ext(dm->file_path, root_node, JSON_C_TO_STRING_PRETTY);
|
||||
|
||||
/* Clean the json object. */
|
||||
json_object_put(root_node);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
@@ -212,13 +246,12 @@ int load_outlets(devices_manager_t *dm, struct json_object *node)
|
||||
length = json_object_array_length(node);
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
/*
|
||||
OutletDio* the_outlet = new OutletDio;
|
||||
the_outlet_node = json_object_array_get_idx(a_node, i);
|
||||
the_outlet->from_json(the_outlet_node);
|
||||
|
||||
m_outlets.add(the_outlet);
|
||||
*/
|
||||
outlet_dio_t *outlet = outlet_dio_new();
|
||||
outlet_node = json_object_array_get_idx(node, i);
|
||||
if (outlet_dio_from_json(outlet, outlet_node) == 0)
|
||||
{
|
||||
qlist_addlast(dm->outlets, outlet, sizeof(outlet_dio_t));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -227,6 +260,26 @@ int load_outlets(devices_manager_t *dm, struct json_object *node)
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
struct json_object *save_outlets(devices_manager_t *dm)
|
||||
{
|
||||
struct json_object *root_node;
|
||||
outlet_dio_t *outlet;
|
||||
qlist_obj_t obj;
|
||||
|
||||
memset((void *) &obj, 0, sizeof(obj));
|
||||
root_node = json_object_new_array();
|
||||
|
||||
while (qlist_getnext(dm->outlets, &obj, true))
|
||||
{
|
||||
outlet = obj.data;
|
||||
json_object_array_add(root_node, outlet_dio_to_json_object(outlet));
|
||||
}
|
||||
|
||||
return root_node;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
int load_shutters(devices_manager_t *dm, struct json_object *node)
|
||||
{
|
||||
DEBUG("TODO");
|
||||
|
||||
Reference in New Issue
Block a user