test domo: add capabilities
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -82,6 +82,7 @@ target_link_libraries (test_device
|
||||
add_custom_command(TARGET test_device POST_BUILD
|
||||
COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/tests/index.html ${CMAKE_BINARY_DIR}/bin
|
||||
COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/tests/devices.json ${CMAKE_BINARY_DIR}/bin
|
||||
COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/../share/domo/capabilities.json ${CMAKE_BINARY_DIR}/bin
|
||||
COMMENT "Install test Files"
|
||||
)
|
||||
|
||||
|
||||
@@ -116,6 +116,11 @@ void devices_manager_free(devices_manager_t *devices_manager)
|
||||
free(devices_manager->file_path);
|
||||
}
|
||||
|
||||
if (devices_manager->capabilities_path != NULL)
|
||||
{
|
||||
free(devices_manager->capabilities_path);
|
||||
}
|
||||
|
||||
free(devices_manager);
|
||||
}
|
||||
|
||||
@@ -188,6 +193,13 @@ int devices_manager_save(devices_manager_t *dm)
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
int devices_manager_set_capabilities_path(devices_manager_t *dm, char *path)
|
||||
{
|
||||
dm->capabilities_path = strdup(path);
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
char *devices_manager_get(devices_manager_t *dm, const char *capability)
|
||||
{
|
||||
json_object *output_node;
|
||||
|
||||
@@ -47,6 +47,7 @@ typedef struct devices_manager_s devices_manager_t;
|
||||
struct devices_manager_s
|
||||
{
|
||||
char *file_path;
|
||||
char *capabilities_path;
|
||||
|
||||
qlist_t *outlets;
|
||||
uint16_t outlets_max_id;
|
||||
@@ -64,6 +65,8 @@ extern void devices_manager_free(devices_manager_t *devices_manager);
|
||||
extern int devices_manager_load(devices_manager_t *dm, char *config_path);
|
||||
extern int devices_manager_save(devices_manager_t *dm);
|
||||
|
||||
extern int devices_manager_set_capabilities_path(devices_manager_t *dm, char *path);
|
||||
|
||||
extern char *devices_manager_get(devices_manager_t *dm, const char *capability);
|
||||
extern char *devices_manager_get_by_id(devices_manager_t *dm, const char *capability, uint32_t id);
|
||||
|
||||
|
||||
@@ -45,6 +45,7 @@
|
||||
#define k_max_path_len 200 /* make this larger if you need to. */
|
||||
|
||||
#define k_devices_file_path "share/domo/devices.json"
|
||||
#define k_capabilities_file_path "share/domo/capabilities.json"
|
||||
#define k_sequences_file_path "share/domo/sequences.json"
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
@@ -132,6 +133,8 @@ int main(int argc, char **argv)
|
||||
return -1;
|
||||
}
|
||||
|
||||
devices_manager_set_capabilities_path(device_manager, get_config_path(k_capabilities_file_path));
|
||||
|
||||
#if 0
|
||||
devices_manager_save(device_manager);
|
||||
/* Setup the Sequences Manager. */
|
||||
|
||||
@@ -37,6 +37,32 @@
|
||||
// 204 Success. No content.
|
||||
// 400 Bad request
|
||||
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
int capabilities_handler(restd_resp_t *response, void *arg)
|
||||
{
|
||||
FILE *f;
|
||||
char buf[1024];
|
||||
devices_manager_t *dm = (devices_manager_t *)arg;
|
||||
|
||||
f = fopen(dm->capabilities_path, "rb");
|
||||
if (f == NULL)
|
||||
restd_http_response(response, 500, "application/json", kempty_body);
|
||||
|
||||
int s = fread(buf, 1, sizeof(buf), f);
|
||||
fclose(f);
|
||||
|
||||
if (s == 0)
|
||||
restd_http_response(response, 500, "application/json", kempty_body);
|
||||
|
||||
buf[s]='\0';
|
||||
|
||||
restd_http_response(response, 200, "application/json", buf);
|
||||
|
||||
return RESTD_OK;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
int outlet_create_handler(restd_resp_t *response, void *arg)
|
||||
|
||||
@@ -34,6 +34,9 @@
|
||||
#define kerror_body "{ \"status\": \"error\" }"
|
||||
#define kempty_body "{}"
|
||||
|
||||
|
||||
extern int capabilities_handler(restd_resp_t *response, void *arg);
|
||||
|
||||
// Outlets
|
||||
extern int outlet_create_handler(restd_resp_t *response, void *arg);
|
||||
extern int outlet_list_handler(restd_resp_t *response, void *arg);
|
||||
|
||||
@@ -57,12 +57,11 @@ int setup_rest_server(restd_server_t *rest_server, const char *port, const char
|
||||
restd_server_set_option(rest_server, "server.port", port);
|
||||
restd_server_set_option(rest_server, "server.root_path", root_path);
|
||||
|
||||
printf (">>>>>>>>>>\n");
|
||||
|
||||
// Error.
|
||||
restd_server_register_error_handler(rest_server, my_error_handler, NULL);
|
||||
|
||||
// Capabilities.
|
||||
restd_server_register_hook_on_path(rest_server, EVHTTP_REQ_GET, "/api/v1/capabilities", NULL, dm);
|
||||
restd_server_register_hook_on_path(rest_server, EVHTTP_REQ_GET, "/api/v1/capabilities", capabilities_handler, dm);
|
||||
|
||||
// Outlets
|
||||
restd_server_register_hook_on_path(rest_server, EVHTTP_REQ_POST, "/api/v1/outlets", outlet_create_handler, dm);
|
||||
|
||||
@@ -25,7 +25,24 @@
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
char *load_capabilities(devices_manager_t *dm)
|
||||
{
|
||||
int s;
|
||||
FILE *f;
|
||||
char buf[1024];
|
||||
char *capabilities;
|
||||
|
||||
f = fopen(dm->capabilities_path, "rb");
|
||||
if (f == NULL)
|
||||
return NULL;
|
||||
|
||||
s = fread(buf, 1, sizeof(buf), f);
|
||||
fclose(f);
|
||||
buf[s] = '\0';
|
||||
capabilities = strdup(buf);
|
||||
|
||||
return capabilities;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
@@ -46,8 +63,9 @@ TEST("Domo - Test API correct access\t")
|
||||
struct event_base *ev_base;
|
||||
restd_server_t *rest_server;
|
||||
devices_manager_t *dm;
|
||||
char *config_path;
|
||||
char *config_path, *capabilities_path;
|
||||
int ret;
|
||||
char *capabilities;
|
||||
|
||||
dm = devices_manager_new();
|
||||
ASSERT_NOT_NULL(dm);
|
||||
@@ -64,8 +82,26 @@ TEST("Domo - Test API correct access\t")
|
||||
config_path = get_config_path();
|
||||
ASSERT_NOT_NULL(config_path);
|
||||
|
||||
capabilities_path = qstrdupf("%s/capabilities.json", config_path);
|
||||
|
||||
devices_manager_set_capabilities_path(dm, capabilities_path);
|
||||
free(capabilities_path);
|
||||
|
||||
ret = setup_rest_server(rest_server, kserver_port, config_path, dm);
|
||||
ASSERT_EQUAL_INT(ret, 0);
|
||||
|
||||
restd_server_set_option(rest_server, "server.thread", "1");
|
||||
|
||||
ret = restd_server_start(rest_server);
|
||||
ASSERT_EQUAL_INT(ret, 0);
|
||||
|
||||
sleep(1);
|
||||
|
||||
capabilities = load_capabilities(dm);
|
||||
ASSERT_NOT_NULL(capabilities);
|
||||
ret = exec_request(kget_method, "http://localhost:" kserver_port "/api/v1/capabilities", 200, "", capabilities, false);
|
||||
ASSERT_EQUAL_INT(ret, 0);
|
||||
free(capabilities);
|
||||
|
||||
// TODO
|
||||
|
||||
@@ -73,3 +109,4 @@ TEST("Domo - Test API correct access\t")
|
||||
devices_manager_free(dm);
|
||||
free(config_path);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user