From edac32bb986e8dab23a14a79317c2109bc523270 Mon Sep 17 00:00:00 2001 From: OpenGnSys Support Team Date: Thu, 30 Nov 2023 20:06:07 +0100 Subject: rest: add GET /room/info Add GET /room/info to obtain room information, this includes the name, gateway and netmask. curl -X GET -H "Authorization: $API_KEY" http://127.0.0.1:8888/room/info -d '{ "id" : 1 } --- src/dbi.c | 37 +++++++++++++++++++++++++++++++ src/dbi.h | 3 +++ src/rest.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/rest.h | 1 + 4 files changed, 116 insertions(+) diff --git a/src/dbi.c b/src/dbi.c index 7c7a5d0..ba4c113 100644 --- a/src/dbi.c +++ b/src/dbi.c @@ -129,6 +129,43 @@ int og_dbi_get_computer_info(struct og_dbi *dbi, struct og_computer *computer, return 0; } +int og_dbi_get_room_info(struct og_dbi *dbi, struct og_room *room, + uint32_t room_id) +{ + const char *msglog; + dbi_result result; + + result = dbi_conn_queryf(dbi->conn, + "SELECT nombreaula, router, netmask " + "FROM aulas WHERE idaula=%d AND grupoid=0", + room_id); + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", + __func__, __LINE__, msglog); + return -1; + } + + if (!dbi_result_next_row(result)) { + syslog(LOG_ERR, "room does not exist in database (%s:%d)\n", + __func__, __LINE__); + dbi_result_free(result); + return -1; + } + + room->id = room_id; + snprintf(room->name, sizeof(room->name), "%s", + dbi_result_get_string(result, "nombreaula")); + snprintf(room->gateway, sizeof(room->gateway), "%s", + dbi_result_get_string(result, "router")); + snprintf(room->netmask, sizeof(room->netmask), "%s", + dbi_result_get_string(result, "netmask")); + + dbi_result_free(result); + + return 0; +} + #define OG_UNASSIGNED_SW_ID 0 #define OG_IMAGE_DEFAULT_TYPE 1 /* monolithic */ diff --git a/src/dbi.h b/src/dbi.h index 8db3a9c..7e0757c 100644 --- a/src/dbi.h +++ b/src/dbi.h @@ -94,6 +94,7 @@ struct og_computer { }; struct og_room { + uint32_t id; uint32_t center; uint32_t group; char location[OG_DB_ROOM_LOC_MAXLEN + 1]; @@ -114,6 +115,8 @@ struct og_repository { struct in_addr; int og_dbi_get_computer_info(struct og_dbi *dbi, struct og_computer *computer, struct in_addr addr); +int og_dbi_get_room_info(struct og_dbi *dbi, struct og_room *room, + uint32_t room_id); int og_dbi_add_image(struct og_dbi *dbi, const struct og_image *image); int og_dbi_schema_update(void); diff --git a/src/rest.c b/src/rest.c index f177e4c..bd28b67 100644 --- a/src/rest.c +++ b/src/rest.c @@ -2038,6 +2038,67 @@ static int og_cmd_post_client_delete(json_t *element, return 0; } +static int og_cmd_get_room_info(json_t *element, + struct og_msg_params *params, + char *buffer_reply) +{ + struct og_room room = {}; + json_t *value, *root; + struct og_dbi *dbi; + uint32_t room_id; + const char *key; + int err = 0; + + struct og_buffer og_buffer = { + .data = buffer_reply + }; + + json_object_foreach(element, key, value) { + if (!strcmp(key, "id")) { + err = og_json_parse_uint(value, &room_id); + params->flags |= OG_REST_PARAM_ID; + } + + if (err < 0) + return err; + } + + if (!og_msg_params_validate(params, OG_REST_PARAM_ID)) + return -1; + + dbi = og_dbi_open(&ogconfig.db); + if (!dbi) { + syslog(LOG_ERR, "cannot open conection database (%s:%d)\n", + __func__, __LINE__); + return -1; + } + + if (og_dbi_get_room_info(dbi, &room, room_id)) { + og_dbi_close(dbi); + return -1; + } + + og_dbi_close(dbi); + + root = json_object(); + if (!root) + return -1; + + json_object_set_new(root, "id", json_integer(room.id)); + json_object_set_new(root, "name", json_string(room.name)); + json_object_set_new(root, "gateway", json_string(room.gateway)); + json_object_set_new(root, "netmask", json_string(room.netmask)); + + if (json_dump_callback(root, og_json_dump_clients, &og_buffer, 0)) { + json_decref(root); + return -1; + } + + json_decref(root); + return 0; +} + + static int og_cmd_stop(json_t *element, struct og_msg_params *params) { const char *key; @@ -6516,6 +6577,7 @@ struct { [OG_URI_CENTER_DELETE] = { "center/delete", }, [OG_URI_ROOM_ADD] = { "room/add", }, [OG_URI_ROOM_DELETE] = { "room/delete", }, + [OG_URI_ROOM_INFO] = { "room/info", }, [OG_URI_PROC_ADD] = { "procedure/add", }, [OG_URI_PROC_UPDATE] = { "procedure/update", }, [OG_URI_PROC_RUN] = { "procedure/run", }, @@ -7164,6 +7226,19 @@ int og_client_state_process_payload_rest(struct og_client *cli) goto err_process_rest_payload; } err = og_cmd_post_room_delete(root, ¶ms); + } else if (!strncmp(cmd, "room/info", strlen("room/info"))) { + if (method != OG_METHOD_GET) { + err = og_client_method_not_found(cli); + goto err_process_rest_payload; + } + if (!root) { + syslog(LOG_ERR, + "command room info with no payload\n"); + err = og_client_bad_request(cli); + goto err_process_rest_payload; + } + + err = og_cmd_get_room_info(root, ¶ms, buf_reply); } else if (!strncmp(cmd, "procedure/add", strlen("procedure/add"))) { if (method != OG_METHOD_POST) { err = og_client_method_not_found(cli); diff --git a/src/rest.h b/src/rest.h index 6cd69be..3c1cf93 100644 --- a/src/rest.h +++ b/src/rest.h @@ -127,6 +127,7 @@ enum og_rest_uri { OG_URI_CENTER_DELETE, OG_URI_ROOM_ADD, OG_URI_ROOM_DELETE, + OG_URI_ROOM_INFO, OG_URI_PROC_ADD, OG_URI_PROC_UPDATE, OG_URI_PROC_RUN, -- cgit v1.2.3-18-g5258