device manager now load data.

This commit is contained in:
NADAL Jean-Baptiste
2019-12-26 16:34:26 +01:00
parent c43fc0b195
commit 4872f71428
9 changed files with 398 additions and 26 deletions

View File

@@ -28,10 +28,70 @@
/*-------------------------------- INCLUDES ---------------------------------*/
#include <stdlib.h>
#include <stdio.h>
#include <libgen.h>
#include <unistd.h>
#include <string.h>
#include <qlibc/qlibc.h>
#include <event2/event.h>
#include "rest/rest_handler.h"
#include "broker/nats_broker.h"
#include "devices/devices_manager.h"
#define k_max_path_len 200 /* make this larger if you need to. */
#define k_devices_file_path "share/domo/devices.json"
#define k_sequences_file_path "share/domo/sequences.json"
/*--------------------------------------------------------------------------*/
char *get_config_path(const char *file_path)
{
int length;
char full_path[k_max_path_len];
char *working_path;
char *last_slash;
length = readlink("/proc/self/exe", full_path, sizeof(full_path));
/* Catch some errors: */
if (length < 0)
{
fprintf(stderr, "Error resolving symlink /proc/self/exe.\n");
return NULL;
}
if (length >= k_max_path_len)
{
fprintf(stderr, "Path too long. Truncated.\n");
return NULL;
}
/* I don't know why, but the string this readlink() function
* returns is appended with a '@'.
*/
full_path[length] = '\0'; /* Strip '@' off the end. */
working_path = dirname(full_path);
last_slash = working_path != NULL ? strrchr(working_path, '/') : NULL;
if (last_slash != NULL)
{
full_path[(last_slash - working_path + 1)] = '\0';
strcat(full_path, file_path);
if (access(full_path, F_OK) != -1)
{
char *to_str = (char *) malloc(strlen(full_path) + 1);
to_str = strdup(full_path);
return to_str;
}
}
return NULL;
}
/*--------------------------------------------------------------------------*/
@@ -48,6 +108,8 @@ int main(int argc, char **argv)
struct event_base *ev_base;
restd_server_t *rest_server;
nats_broker_t *nats_broker;
devices_manager_t *device_manager;
int ret;
if (argc < 2)
{
@@ -57,6 +119,31 @@ int main(int argc, char **argv)
restd_log_level(RESTD_LOG_DEBUG);
// data.
device_manager = devices_manager_new();
if (device_manager == NULL)
{
fprintf(stderr, "Failed to create the device manager.\n");
return -1;
}
if (devices_manager_load(device_manager, get_config_path(k_devices_file_path)) != 0)
{
fprintf(stderr, "Failed to Load the devices.\n");
return -1;
}
devices_manager_save(device_manager);
#if 0
/* Setup the Sequences Manager. */
SequencesManager the_sequences_manager(the_config_path + "/sequences.json", &the_devices_manager);
if (the_sequences_manager.load() != 0)
{
fprintf(stderr, "Failed to load timers.\n");
return -2;
}
the_sequences_manager.save();
#endif
/* event loop */
ev_base = event_base_new();
if (!ev_base)
@@ -93,5 +180,9 @@ int main(int argc, char **argv)
nats_broker_connect(nats_broker, "foo");
/* start the rest server */
return restd_server_start(rest_server);
ret = restd_server_start(rest_server);
/* Clean data. */
return ret;
}