From 23fed47639dd4dffa477b226cc9a28075511c925 Mon Sep 17 00:00:00 2001 From: OpenGnSys Support Team Date: Wed, 29 May 2019 12:10:19 +0200 Subject: #915 add POST poweroff command to REST API in ogAdmServer Power off a client (through POST method): curl -X POST http://127.0.0.1:8888/poweroff -d @poweroff.json Request POST /poweroff {"clients": [ "192.168.2.1" ] } Reply: 200 OK --- sources/ogAdmServer.cpp | 29 +++++++++++++++++++++++++++++ tests/poweroff.json | 1 + tests/run-tests.sh | 1 + 3 files changed, 31 insertions(+) create mode 100644 tests/poweroff.json diff --git a/sources/ogAdmServer.cpp b/sources/ogAdmServer.cpp index 970cf2b..a86655d 100644 --- a/sources/ogAdmServer.cpp +++ b/sources/ogAdmServer.cpp @@ -3876,6 +3876,26 @@ static int og_cmd_session(json_t *element, struct og_msg_params *params) return 0; } +static int og_cmd_poweroff(json_t *element, struct og_msg_params *params) +{ + const char *key; + json_t *value; + int err = 0; + + if (json_typeof(element) != JSON_OBJECT) + return -1; + + json_object_foreach(element, key, value) { + if (!strcmp(key, "clients")) + err = og_json_parse_clients(value, params); + + if (err < 0) + break; + } + + return og_cmd_legacy_send(params, "Apagar", CLIENTE_OCUPADO); +} + static int og_client_not_found(struct og_client *cli) { char buf[] = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n"; @@ -3991,6 +4011,15 @@ static int og_client_state_process_payload_rest(struct og_client *cli) return og_client_not_found(cli); } err = og_cmd_session(root, ¶ms); + } else if (!strncmp(cmd, "poweroff", strlen("poweroff"))) { + if (method != OG_METHOD_POST) + return -1; + + if (!root) { + syslog(LOG_ERR, "command poweroff with no payload\n"); + return og_client_not_found(cli); + } + err = og_cmd_poweroff(root, ¶ms); } else { syslog(LOG_ERR, "unknown command %s\n", cmd); err = og_client_not_found(cli); diff --git a/tests/poweroff.json b/tests/poweroff.json new file mode 100644 index 0000000..4667303 --- /dev/null +++ b/tests/poweroff.json @@ -0,0 +1 @@ +{ "clients" : [ "192.168.2.1", "192.168.2.2" ] } diff --git a/tests/run-tests.sh b/tests/run-tests.sh index 27c7bc6..8736859 100755 --- a/tests/run-tests.sh +++ b/tests/run-tests.sh @@ -4,3 +4,4 @@ curl -X POST http://127.0.0.1:8888/wol -d @wol.json curl -X POST http://127.0.0.1:8888/shell/run -d @post_shell_run.json curl -X POST http://127.0.0.1:8888/shell/output -d @post_shell_output.json curl -X POST http://127.0.0.1:8888/session -d @session.json +curl -X POST http://127.0.0.1:8888/poweroff -d @poweroff.json -- cgit v1.2.3-18-g5258