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 }
master
OpenGnSys Support Team 2023-11-30 20:06:07 +01:00
parent 1ce5624032
commit edac32bb98
4 changed files with 116 additions and 0 deletions

View File

@ -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 */

View File

@ -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);

View File

@ -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, &params);
} 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, &params, buf_reply);
} else if (!strncmp(cmd, "procedure/add", strlen("procedure/add"))) {
if (method != OG_METHOD_POST) {
err = og_client_method_not_found(cli);

View File

@ -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,