From 1e27430f88fad3ad8367acacf9741904588dfa1c Mon Sep 17 00:00:00 2001 From: "xiang.zhou" Date: Fri, 29 Nov 2024 09:46:11 +0800 Subject: [PATCH] api:add new api about system exit --- CMakeLists.txt | 3 +- plugins/restful/handle.c | 10 +++++ plugins/restful/system_handle.c | 32 ++++++++++++++ plugins/restful/system_handle.h | 27 ++++++++++++ src/parser/neu_json_system.c | 75 +++++++++++++++++++++++++++++++++ src/parser/neu_json_system.h | 43 +++++++++++++++++++ tests/ut/json_test.cc | 13 ++++++ 7 files changed, 202 insertions(+), 1 deletion(-) create mode 100644 plugins/restful/system_handle.c create mode 100644 plugins/restful/system_handle.h create mode 100644 src/parser/neu_json_system.c create mode 100644 src/parser/neu_json_system.h diff --git a/CMakeLists.txt b/CMakeLists.txt index f5b9e6e5..ea45ddfd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -160,7 +160,8 @@ set(NEURON_SOURCES plugins/restful/version_handle.c plugins/restful/scan_handle.c plugins/restful/otel_handle.c - plugins/restful/cid_handle.c + plugins/restful/cid_handle.c + plugins/restful/system_handle.c plugins/restful/rest.c plugins/restful/user.c) diff --git a/plugins/restful/handle.c b/plugins/restful/handle.c index 69cea3e1..482ce779 100644 --- a/plugins/restful/handle.c +++ b/plugins/restful/handle.c @@ -36,6 +36,7 @@ #include "plugin_handle.h" #include "rw_handle.h" #include "scan_handle.h" +#include "system_handle.h" #include "utils/http.h" #include "version_handle.h" @@ -135,6 +136,9 @@ static struct neu_http_handler cors_handler[] = { { .url = "/api/v2/cid", }, + { + .url = "/api/v2/system/ctl", + }, }; static struct neu_http_handler rest_handlers[] = { @@ -439,6 +443,12 @@ static struct neu_http_handler rest_handlers[] = { .url = "/api/v2/otel", .value.handler = handle_otel_get, }, + { + .method = NEU_HTTP_METHOD_POST, + .type = NEU_HTTP_HANDLER_FUNCTION, + .url = "/api/v2/system/ctl", + .value.handler = handle_system_ctl, + }, }; void neu_rest_handler(const struct neu_http_handler **handlers, uint32_t *size) diff --git a/plugins/restful/system_handle.c b/plugins/restful/system_handle.c new file mode 100644 index 00000000..388fd540 --- /dev/null +++ b/plugins/restful/system_handle.c @@ -0,0 +1,32 @@ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "json/neu_json_fn.h" + +#include "handle.h" +#include "utils/http.h" +#include "utils/neu_jwt.h" + +#include "parser/neu_json_system.h" +#include "system_handle.h" + +void handle_system_ctl(nng_aio *aio) +{ + NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT( + aio, neu_json_system_ctl_req_t, neu_json_decode_system_ctl_req, { + if (req->action == 1) { + neu_http_ok(aio, "{\"error\": 0 }"); + exit(-2); + } + }) +} \ No newline at end of file diff --git a/plugins/restful/system_handle.h b/plugins/restful/system_handle.h new file mode 100644 index 00000000..c453a138 --- /dev/null +++ b/plugins/restful/system_handle.h @@ -0,0 +1,27 @@ +/** + * NEURON IIoT System for Industry 4.0 + * Copyright (C) 2020-2022 EMQ Technologies Co., Ltd All rights reserved. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + **/ + +#ifndef _NEU_SYSTEM_HANDLE_H_ +#define _NEU_SYSTEM_HANDLE_H_ + +#include + +void handle_system_ctl(nng_aio *aio); + +#endif \ No newline at end of file diff --git a/src/parser/neu_json_system.c b/src/parser/neu_json_system.c new file mode 100644 index 00000000..d9e7dbcb --- /dev/null +++ b/src/parser/neu_json_system.c @@ -0,0 +1,75 @@ +/** + * NEURON IIoT System for Industry 4.0 + * Copyright (C) 2020-2022 EMQ Technologies Co., Ltd All rights reserved. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + **/ + +/* + * DO NOT EDIT THIS FILE MANUALLY! + * It was automatically generated by `json-autotype`. + */ + +#include +#include + +#include "json/json.h" + +#include "neu_json_system.h" + +void neu_json_decode_system_ctl_req_free(neu_json_system_ctl_req_t *req) +{ + if (req != NULL) { + free(req); + } +} + +int neu_json_decode_system_ctl_req(char * buf, + neu_json_system_ctl_req_t **result) +{ + int ret = 0; + void *json_obj = neu_json_decode_new(buf); + + neu_json_system_ctl_req_t *req = + calloc(1, sizeof(neu_json_system_ctl_req_t)); + if (req == NULL) { + return -1; + } + + neu_json_elem_t req_elems[] = { + { + .name = "action", + .t = NEU_JSON_INT, + }, + }; + + ret = neu_json_decode_by_json(json_obj, NEU_JSON_ELEM_SIZE(req_elems), + req_elems); + if (ret != 0) { + free(req); + if (json_obj != NULL) { + neu_json_decode_free(json_obj); + } + return -1; + } + + req->action = req_elems[0].v.val_int; + *result = req; + + if (json_obj != NULL) { + neu_json_decode_free(json_obj); + } + return ret; +} \ No newline at end of file diff --git a/src/parser/neu_json_system.h b/src/parser/neu_json_system.h new file mode 100644 index 00000000..3299870e --- /dev/null +++ b/src/parser/neu_json_system.h @@ -0,0 +1,43 @@ +/** + * NEURON IIoT System for Industry 4.0 + * Copyright (C) 2020-2022 EMQ Technologies Co., Ltd All rights reserved. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + **/ + +/* + * DO NOT EDIT THIS FILE MANUALLY! + * It was automatically generated by `json-autotype`. + */ + +#ifndef _NEU_JSON_API_NEU_JSON_SYSTEM_H_ +#define _NEU_JSON_API_NEU_JSON_SYSTEM_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct { + int action; +} neu_json_system_ctl_req_t; + +void neu_json_decode_system_ctl_req_free(neu_json_system_ctl_req_t *req); +int neu_json_decode_system_ctl_req(char * buf, + neu_json_system_ctl_req_t **result); +#ifdef __cplusplus +} +#endif + +#endif diff --git a/tests/ut/json_test.cc b/tests/ut/json_test.cc index aa64ca8e..989591b8 100644 --- a/tests/ut/json_test.cc +++ b/tests/ut/json_test.cc @@ -4,6 +4,8 @@ #include "utils/log.h" +#include "parser/neu_json_system.h" + zlog_category_t *neuron = NULL; TEST(JsonTest, DecodeField) { @@ -329,6 +331,17 @@ TEST(JsonTest, EncodeArray) neu_json_decode_free(ob); } +TEST(JsonTest, System_ctl) +{ + char * buf = (char *) "{\"action\": 1}"; + char * buf1 = (char *) "{\"action1\": 1}"; + neu_json_system_ctl_req_t *req = NULL; + EXPECT_EQ(-1, neu_json_decode_system_ctl_req(buf1, &req)); + EXPECT_EQ(0, neu_json_decode_system_ctl_req(buf, &req)); + EXPECT_EQ(1, req->action); + free(req); +} + int main(int argc, char **argv) { zlog_init("./config/dev.conf");