From b5f199ddbf6e25972cc2f4eeea7d3cfb7bf90614 Mon Sep 17 00:00:00 2001 From: gc87 Date: Fri, 22 Apr 2022 18:13:50 +0800 Subject: [PATCH] refactor(mqtt): remove code of mqtt cmd --- include/neuron/json/neu_json_mqtt.h | 7 - plugins/mqtt/CMakeLists.txt | 8 +- plugins/mqtt/command/command.c | 427 ---------------------------- plugins/mqtt/command/datatag.c | 330 --------------------- plugins/mqtt/command/datatag.h | 43 --- plugins/mqtt/command/group_config.c | 227 --------------- plugins/mqtt/command/group_config.h | 52 ---- plugins/mqtt/command/node.c | 161 ----------- plugins/mqtt/command/node.h | 50 ---- plugins/mqtt/command/plugin.c | 91 ------ plugins/mqtt/command/plugin.h | 41 --- plugins/mqtt/command/schema.c | 69 ----- plugins/mqtt/command/schema.h | 36 --- plugins/mqtt/command/tty.c | 51 ---- plugins/mqtt/command/tty.h | 36 --- plugins/mqtt/mqtt.c | 55 +--- plugins/mqtt/mqtt.h | 20 -- tests/CMakeLists.txt | 3 +- tests/mqtt_test.cc | 1 - 19 files changed, 17 insertions(+), 1691 deletions(-) delete mode 100644 plugins/mqtt/command/datatag.c delete mode 100644 plugins/mqtt/command/datatag.h delete mode 100644 plugins/mqtt/command/group_config.c delete mode 100644 plugins/mqtt/command/group_config.h delete mode 100644 plugins/mqtt/command/node.c delete mode 100644 plugins/mqtt/command/node.h delete mode 100644 plugins/mqtt/command/plugin.c delete mode 100644 plugins/mqtt/command/plugin.h delete mode 100644 plugins/mqtt/command/schema.c delete mode 100644 plugins/mqtt/command/schema.h delete mode 100644 plugins/mqtt/command/tty.c delete mode 100644 plugins/mqtt/command/tty.h diff --git a/include/neuron/json/neu_json_mqtt.h b/include/neuron/json/neu_json_mqtt.h index bd21a497..f799a3c2 100644 --- a/include/neuron/json/neu_json_mqtt.h +++ b/include/neuron/json/neu_json_mqtt.h @@ -31,13 +31,6 @@ extern "C" { #include -#define NEU_MQTT_CMD_GET "get" -#define NEU_MQTT_CMD_SET "set" -#define NEU_MQTT_CMD_ADD "add" -#define NEU_MQTT_CMD_UPDATE "update" -#define NEU_MQTT_CMD_DELETE "delete" -#define NEU_MQTT_CMD_NONE "" - typedef struct { char *uuid; char *command; diff --git a/plugins/mqtt/CMakeLists.txt b/plugins/mqtt/CMakeLists.txt index a4bffe4a..16c75790 100644 --- a/plugins/mqtt/CMakeLists.txt +++ b/plugins/mqtt/CMakeLists.txt @@ -4,14 +4,8 @@ set(LIBRARY_OUTPUT_PATH "${CMAKE_BINARY_DIR}/plugins") set(src mqtt.c util.c - command/read_write.c - command/group_config.c - command/datatag.c - command/node.c command/ping.c - command/plugin.c - command/tty.c - command/schema.c + command/read_write.c command/command.c) add_library(plugin-mqtt SHARED ${src}) diff --git a/plugins/mqtt/command/command.c b/plugins/mqtt/command/command.c index 22e7c55d..634b9f22 100644 --- a/plugins/mqtt/command/command.c +++ b/plugins/mqtt/command/command.c @@ -1,14 +1,8 @@ #include #include -#include "datatag.h" -#include "group_config.h" -#include "node.h" #include "ping.h" -#include "plugin.h" #include "read_write.h" -#include "schema.h" -#include "tty.h" #include "command.h" @@ -20,238 +14,6 @@ static int ping_response(char **output_str, neu_plugin_t *plugin, return 0; } -// Maniplate node -static int node_response_get(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_get_nodes_req_t *req = NULL; - int rc = neu_json_decode_get_nodes_req(json_str, &req); - if (0 == rc) { - *output_str = command_node_get(plugin, mqtt, req); - neu_json_decode_get_nodes_req_free(req); - } - - return rc; -} - -static int node_response_add(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_add_node_req_t *req = NULL; - int rc = neu_json_decode_add_node_req(json_str, &req); - if (0 == rc) { - *output_str = command_node_add(plugin, mqtt, req); - neu_json_decode_add_node_req_free(req); - } - - return rc; -} - -static int node_response_delete(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_del_node_req_t *req = NULL; - int rc = neu_json_decode_del_node_req(json_str, &req); - if (0 == rc) { - *output_str = command_node_delete(plugin, mqtt, req); - neu_json_decode_del_node_req_free(req); - } - - return rc; -} - -// Manipulate group config -static int group_config_response_get(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_get_group_config_req_t *req = NULL; - int rc = neu_json_decode_get_group_config_req(json_str, &req); - if (0 == rc) { - *output_str = command_group_config_get(plugin, mqtt, req); - neu_json_decode_get_group_config_req_free(req); - } - - return rc; -} - -static int group_config_response_add(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_add_group_config_req_t *req = NULL; - int rc = neu_json_decode_add_group_config_req(json_str, &req); - if (0 == rc) { - *output_str = command_group_config_add(plugin, mqtt, req); - neu_json_decode_add_group_config_req_free(req); - } - - return rc; -} - -static int group_config_response_update(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_update_group_config_req_t *req = NULL; - int rc = neu_json_decode_update_group_config_req(json_str, &req); - if (0 == rc) { - *output_str = command_group_config_update(plugin, mqtt, req); - neu_json_decode_update_group_config_req_free(req); - } - - return rc; -} - -static int group_config_response_delete(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_del_group_config_req_t *req = NULL; - int rc = neu_json_decode_del_group_config_req(json_str, &req); - if (0 == rc) { - *output_str = command_group_config_delete(plugin, mqtt, req); - neu_json_decode_del_group_config_req_free(req); - } - - return rc; -} - -// Manipulate data tags -static int datatag_response_get(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_get_tags_req_t *req = NULL; - int rc = neu_json_decode_get_tags_req(json_str, &req); - *output_str = command_get_tags(plugin, mqtt, req); - if (0 == rc) { - neu_json_decode_get_tags_req_free(req); - } - - return rc; -} - -static int datatag_response_add(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_add_tags_req_t *req = NULL; - int rc = neu_json_decode_add_tags_req(json_str, &req); - *output_str = command_add_tags(plugin, mqtt, req); - if (0 == rc) { - neu_json_decode_add_tags_req_free(req); - } - - return rc; -} - -static int datatag_response_update(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_update_tags_req_t *req = NULL; - int rc = neu_json_decode_update_tags_req(json_str, &req); - *output_str = command_update_tags(plugin, mqtt, req); - if (0 == rc) { - neu_json_decode_update_tags_req_free(req); - } - - return rc; -} - -static int datatag_response_delete(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_del_tags_req_t *req = NULL; - int rc = neu_json_decode_del_tags_req(json_str, &req); - *output_str = command_delete_tags(plugin, mqtt, req); - if (0 == rc) { - neu_json_decode_del_tags_req_free(req); - } - - return rc; -} - -// Manipulate plugin libs -static int plugin_response_get(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_get_plugin_req_t *req = NULL; - int rc = neu_json_decode_get_plugin_req(json_str, &req); - if (0 == rc) { - *output_str = command_plugin_get(plugin, mqtt, req); - neu_json_decode_get_plugin_req_free(req); - } - - return rc; -} - -static int plugin_response_add(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_add_plugin_req_t *req = NULL; - int rc = neu_json_decode_add_plugin_req(json_str, &req); - if (0 == rc) { - *output_str = command_plugin_add(plugin, mqtt, req); - neu_json_decode_add_plugin_req_free(req); - } - - return rc; -} - -// static int plugin_response_update(char **output_str, neu_plugin_t *plugin, -// neu_json_mqtt_t *mqtt, char *json_str) -// { -// return 0; -// } - -static int plugin_response_delete(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_del_plugin_req_t *req = NULL; - int rc = neu_json_decode_del_plugin_req(json_str, &req); - if (0 == rc) { - *output_str = command_plugin_delete(plugin, mqtt, req); - neu_json_decode_del_plugin_req_free(req); - } - - return rc; -} - -// Subscribe group config -static int subscribe_response_get(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_get_group_config_req_t *req = NULL; - int rc = neu_json_decode_get_group_config_req(json_str, &req); - if (0 == rc) { - *output_str = command_get_group_config_subscribes(plugin, mqtt, req); - neu_json_decode_get_group_config_req_free(req); - } - - return rc; -} - -static int subscribe_response_add(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_subscribe_req_t *req = NULL; - int rc = neu_json_decode_subscribe_req(json_str, &req); - if (0 == rc) { - *output_str = command_group_config_subscribe(plugin, mqtt, req); - neu_json_decode_subscribe_req_free(req); - } - - return rc; -} - -static int subscribe_response_delete(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_unsubscribe_req_t *req = NULL; - int rc = neu_json_decode_unsubscribe_req(json_str, &req); - if (0 == rc) { - *output_str = command_group_config_unsubscribe(plugin, mqtt, req); - neu_json_decode_unsubscribe_req_free(req); - } - - return rc; -} - // Read tags with group config static int read_response(mqtt_response_t *response, neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, char *json_str) @@ -293,79 +55,6 @@ static int write_response(mqtt_response_t *response, neu_plugin_t *plugin, return rc; } -// Manipulate TTY -static int tty_response_get(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - UNUSED(json_str); - *output_str = command_tty_get(plugin, mqtt); - return 0; -} - -// Manipulate plugin schema -static int schema_response_get(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - UNUSED(json_str); - *output_str = command_schema_get(plugin, mqtt); - return 0; -} - -// Manipulate node setting -static int setting_response_set(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_node_setting_req_t *req = NULL; - int rc = neu_json_decode_node_setting_req(json_str, &req); - if (0 == rc) { - *output_str = command_node_setting_set(plugin, mqtt, req, json_str); - neu_json_decode_node_setting_req_free(req); - } - - return rc; -} - -static int setting_response_get(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_node_setting_req_t *req = NULL; - int rc = neu_json_decode_node_setting_req(json_str, &req); - if (0 == rc) { - *output_str = command_node_setting_get(plugin, mqtt, req); - neu_json_decode_node_setting_req_free(req); - } - - return rc; -} - -// Control the node -static int control_response(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_node_ctl_req_t *req = NULL; - int rc = neu_json_decode_node_ctl_req(json_str, &req); - if (0 == rc) { - *output_str = command_node_control(plugin, mqtt, req); - neu_json_decode_node_ctl_req_free(req); - } - - return rc; -} - -// Manipulate node state -static int state_response(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_node_setting_req_t *req = NULL; - int rc = neu_json_decode_node_setting_req(json_str, &req); - if (0 == rc) { - *output_str = command_node_state_get(plugin, mqtt, req); - neu_json_decode_node_setting_req_free(req); - } - - return rc; -} - void command_response_handle(mqtt_response_t *response) { neu_plugin_t *plugin = (neu_plugin_t *) response->plugin; @@ -391,16 +80,6 @@ void command_response_handle(mqtt_response_t *response) return; } - if (0 != strcmp(mqtt->command, NEU_MQTT_CMD_GET) && - 0 != strcmp(mqtt->command, NEU_MQTT_CMD_SET) && - 0 != strcmp(mqtt->command, NEU_MQTT_CMD_ADD) && - 0 != strcmp(mqtt->command, NEU_MQTT_CMD_UPDATE) && - 0 != strcmp(mqtt->command, NEU_MQTT_CMD_DELETE) && - 0 != strcmp(mqtt->command, NEU_MQTT_CMD_NONE)) { - log_error("Unsupported command"); - return; - } - switch (response->type) { case TOPIC_TYPE_PING: { // Ping @@ -408,75 +87,6 @@ void command_response_handle(mqtt_response_t *response) break; } - case TOPIC_TYPE_NODE: { - // Node GET/ADD/UPDATE/DELETE - if (0 == strcmp(NEU_MQTT_CMD_GET, mqtt->command)) { - rc = node_response_get(&ret_str, plugin, mqtt, json_str); - } else if (0 == strcmp(NEU_MQTT_CMD_ADD, mqtt->command)) { - rc = node_response_add(&ret_str, plugin, mqtt, json_str); - } else if (0 == strcmp(NEU_MQTT_CMD_DELETE, mqtt->command)) { - rc = node_response_delete(&ret_str, plugin, mqtt, json_str); - } - - break; - } - - case TOPIC_TYPE_GCONFIG: { - // Group Config GET/ADD/UPDATE/DELETE - if (0 == strcmp(NEU_MQTT_CMD_GET, mqtt->command)) { - rc = group_config_response_get(&ret_str, plugin, mqtt, json_str); - } else if (0 == strcmp(NEU_MQTT_CMD_ADD, mqtt->command)) { - rc = group_config_response_add(&ret_str, plugin, mqtt, json_str); - } else if (0 == strcmp(NEU_MQTT_CMD_UPDATE, mqtt->command)) { - rc = group_config_response_update(&ret_str, plugin, mqtt, json_str); - } else if (0 == strcmp(NEU_MQTT_CMD_DELETE, mqtt->command)) { - rc = group_config_response_delete(&ret_str, plugin, mqtt, json_str); - } - - break; - } - - case TOPIC_TYPE_TAGS: { - // Tag GET/ADD/UPDATE/DELETE - if (0 == strcmp(NEU_MQTT_CMD_GET, mqtt->command)) { - rc = datatag_response_get(&ret_str, plugin, mqtt, json_str); - } else if (0 == strcmp(NEU_MQTT_CMD_ADD, mqtt->command)) { - rc = datatag_response_add(&ret_str, plugin, mqtt, json_str); - } else if (0 == strcmp(NEU_MQTT_CMD_UPDATE, mqtt->command)) { - rc = datatag_response_update(&ret_str, plugin, mqtt, json_str); - } else if (0 == strcmp(NEU_MQTT_CMD_DELETE, mqtt->command)) { - rc = datatag_response_delete(&ret_str, plugin, mqtt, json_str); - } - - break; - } - - case TOPIC_TYPE_PLUGIN: { - // Plugin GET/ADD/UPDATE/DELETE - if (0 == strcmp(NEU_MQTT_CMD_GET, mqtt->command)) { - rc = plugin_response_get(&ret_str, plugin, mqtt, json_str); - } else if (0 == strcmp(NEU_MQTT_CMD_ADD, mqtt->command)) { - rc = plugin_response_add(&ret_str, plugin, mqtt, json_str); - } else if (0 == strcmp(NEU_MQTT_CMD_DELETE, mqtt->command)) { - rc = plugin_response_delete(&ret_str, plugin, mqtt, json_str); - } - - break; - } - - case TOPIC_TYPE_SUBSCRIBE: { - // Subscribe GET/ADD/DELETE - if (0 == strcmp(NEU_MQTT_CMD_ADD, mqtt->command)) { - rc = subscribe_response_add(&ret_str, plugin, mqtt, json_str); - } else if (0 == strcmp(NEU_MQTT_CMD_DELETE, mqtt->command)) { - rc = subscribe_response_delete(&ret_str, plugin, mqtt, json_str); - } else if (0 == strcmp(NEU_MQTT_CMD_GET, mqtt->command)) { - rc = subscribe_response_get(&ret_str, plugin, mqtt, json_str); - } - - break; - } - case TOPIC_TYPE_READ: { rc = read_response(response, plugin, mqtt, json_str); break; @@ -486,43 +96,6 @@ void command_response_handle(mqtt_response_t *response) rc = write_response(response, plugin, mqtt, json_str); break; } - - case TOPIC_TYPE_TTYS: { - // TTY GET - if (0 == strcmp(NEU_MQTT_CMD_GET, mqtt->command)) { - rc = tty_response_get(&ret_str, plugin, mqtt, json_str); - } - break; - } - - case TOPIC_TYPE_SCHEMA: { - // Schema GET - if (0 == strcmp(NEU_MQTT_CMD_GET, mqtt->command)) { - rc = schema_response_get(&ret_str, plugin, mqtt, json_str); - } - break; - } - - case TOPIC_TYPE_SETTING: { - // Setting SET/GET - if (0 == strcmp(NEU_MQTT_CMD_SET, mqtt->command)) { - rc = setting_response_set(&ret_str, plugin, mqtt, json_str); - } else if (0 == strcmp(NEU_MQTT_CMD_GET, mqtt->command)) { - rc = setting_response_get(&ret_str, plugin, mqtt, json_str); - } - - break; - } - - case TOPIC_TYPE_CTR: { - rc = control_response(&ret_str, plugin, mqtt, json_str); - break; - } - - case TOPIC_TYPE_STATE: { - rc = state_response(&ret_str, plugin, mqtt, json_str); - break; - } default: assert(1 == 0); break; diff --git a/plugins/mqtt/command/datatag.c b/plugins/mqtt/command/datatag.c deleted file mode 100644 index ade16bbe..00000000 --- a/plugins/mqtt/command/datatag.c +++ /dev/null @@ -1,330 +0,0 @@ -/** - * NEURON IIoT System for Industry 4.0 - * Copyright (C) 2020-2021 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. - **/ - -#include - -#include "datatag.h" -#include "json/neu_json_error.h" - -char *command_get_tags(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_get_tags_req_t *req) -{ - neu_json_error_resp_t error = { 0 }; - char * result = NULL; - if (NULL == req) { - error.error = NEU_ERR_PARAM_IS_WRONG; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; - } - - log_info("Get tag list uuid:%s, node id:%d", mqtt->uuid, req->node_id); - - neu_datatag_table_t *table = - neu_system_get_datatags_table(plugin, req->node_id); - if (NULL == table) { - error.error = NEU_ERR_NODE_NOT_EXIST; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; - } - - vector_t grp_configs = neu_system_get_group_configs(plugin, req->node_id); - neu_json_get_tags_resp_t res = { 0 }; - int index = 0; - - VECTOR_FOR_EACH(&grp_configs, iter) - { - neu_taggrp_config_t *config = - *(neu_taggrp_config_t **) iterator_get(&iter); - vector_t *ids = neu_taggrp_cfg_get_datatag_ids(config); - if (NULL != req->group_config_name && - strncmp(neu_taggrp_cfg_get_name(config), req->group_config_name, - strlen(req->group_config_name)) != 0) { - continue; - } - - res.n_tag += ids->size; - } - - res.tags = calloc(res.n_tag, sizeof(neu_json_get_tags_resp_tag_t)); - - VECTOR_FOR_EACH(&grp_configs, iter) - { - neu_taggrp_config_t *config = - *(neu_taggrp_config_t **) iterator_get(&iter); - const char *group_name = neu_taggrp_cfg_get_name(config); - - if (NULL != req->group_config_name && - strncmp(group_name, req->group_config_name, - strlen(req->group_config_name)) != 0) { - continue; - } - - vector_t *ids = neu_taggrp_cfg_get_datatag_ids(config); - - VECTOR_FOR_EACH(ids, iter_id) - { - neu_datatag_id_t *id = (neu_datatag_id_t *) iterator_get(&iter_id); - neu_datatag_t * tag = neu_datatag_tbl_get(table, *id); - - res.tags[index].id = *id; - res.tags[index].name = tag->name; - res.tags[index].group_config_name = (char *) group_name; - res.tags[index].type = tag->type; - res.tags[index].attribute = tag->attribute; - res.tags[index].address = tag->addr_str; - - index += 1; - } - } - - neu_json_encode_with_mqtt(&res, neu_json_encode_get_tags_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - vector_uninit(&grp_configs); - free(res.tags); - return result; -} - -char *command_add_tags(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_add_tags_req_t *req) -{ - neu_json_error_resp_t error = { 0 }; - char * result = NULL; - if (NULL == req) { - error.error = NEU_ERR_PARAM_IS_WRONG; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; - } - - log_info("Add tags uuid:%s, node id:%d, group config name:%s", mqtt->uuid, - req->node_id, req->group_config_name); - - neu_taggrp_config_t *new_config = NULL; - neu_taggrp_config_t *config = neu_system_find_group_config( - plugin, req->node_id, req->group_config_name); - neu_datatag_table_t *table = - neu_system_get_datatags_table(plugin, req->node_id); - - if (NULL == table) { - error.error = NEU_ERR_NODE_NOT_EXIST; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; - } - - if (NULL == config) { - new_config = neu_taggrp_cfg_new(req->group_config_name); - neu_taggrp_cfg_set_interval(new_config, 10000); - } else { - new_config = neu_taggrp_cfg_clone(config); - } - - vector_t * ids = neu_taggrp_cfg_get_datatag_ids(new_config); - neu_err_code_e code = { 0 }; - bool added = false; - - for (int i = 0; i < req->n_tag; i++) { - if (!neu_tag_check_attribute(req->tags[i].attribute)) { - code = NEU_ERR_TAG_ATTRIBUTE_NOT_SUPPORT; - continue; - } - - neu_datatag_t *tag = - neu_datatag_alloc(req->tags[i].attribute, req->tags[i].type, - req->tags[i].address, req->tags[i].name); - - if (NULL == tag) { - code = NEU_ERR_ENOMEM; - continue; - } - - if (neu_datatag_tbl_add(table, tag)) { - vector_push_back(ids, &tag->id); - added = true; - } else { - log_warn_node(plugin, - "Add failed, tag name exists, uuid:%s, " - "node_id:%d, req_tag_name:%s", - mqtt->uuid, req->node_id, req->tags[i].name); - code = NEU_ERR_TAG_NAME_CONFLICT; - free(tag); - } - } - - int rv = 0; - if (config != NULL) { - rv = neu_system_update_group_config(plugin, req->node_id, new_config); - } else { - rv = neu_system_add_group_config(plugin, req->node_id, new_config); - } - - if (0 != rv) { - code = rv; - added = false; - } - - if (added) { - uint32_t event_id = neu_plugin_get_event_id(plugin); - neu_plugin_notify_event_add_tags(plugin, event_id, req->node_id, - req->group_config_name); - } - - error.error = code; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; -} - -char *command_update_tags(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_update_tags_req_t *req) -{ - neu_json_error_resp_t error = { 0 }; - char * result = NULL; - if (NULL == req) { - error.error = NEU_ERR_PARAM_IS_WRONG; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; - } - - log_info("Update tags uuid:%s, node id:%d", mqtt->uuid, req->node_id); - - neu_datatag_table_t *table = - neu_system_get_datatags_table(plugin, req->node_id); - if (NULL == table) { - error.error = NEU_ERR_NODE_NOT_EXIST; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; - } - - bool updated = false; - neu_err_code_e code = { 0 }; - for (int i = 0; i < req->n_tag; i++) { - neu_datatag_t *tag = neu_datatag_tbl_get(table, req->tags[i].id); - - if (tag == NULL) { - code = NEU_ERR_TAG_NOT_EXIST; - continue; - } - - if (!neu_tag_check_attribute(req->tags[i].attribute)) { - code = NEU_ERR_TAG_ATTRIBUTE_NOT_SUPPORT; - continue; - } - - tag = neu_datatag_alloc(req->tags[i].attribute, req->tags[i].type, - req->tags[i].address, req->tags[i].name); - - if (NULL == tag) { - code = NEU_ERR_ENOMEM; - continue; - } - - if (0 != neu_datatag_tbl_update(table, req->tags[i].id, tag)) { - log_warn_node(plugin, - "Update failed, tag name exists, uuid:%s, " - "node_id:%d, tag_id:%d, req_tag_name:%s", - mqtt->uuid, req->node_id, req->tags[i].id, - req->tags[i].name); - code = NEU_ERR_TAG_NAME_CONFLICT; - free(tag); - } else { - updated = true; - } - } - - if (updated) { - uint32_t event_id = neu_plugin_get_event_id(plugin); - neu_plugin_notify_event_update_tags(plugin, event_id, req->node_id, - req->group_config_name); - } - - error.error = code; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; -} - -char *command_delete_tags(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_del_tags_req_t *req) -{ - neu_json_error_resp_t error = { 0 }; - char * result = NULL; - if (NULL == req) { - error.error = NEU_ERR_PARAM_IS_WRONG; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; - } - - log_info("Delete tags uuid:%s, node id:%d, group config:%s", mqtt->uuid, - req->node_id, req->group_config_name); - - neu_taggrp_config_t *new_config = NULL; - neu_taggrp_config_t *config = neu_system_find_group_config( - plugin, req->node_id, req->group_config_name); - neu_datatag_table_t *table = - neu_system_get_datatags_table(plugin, req->node_id); - - if (NULL == config || NULL == table) { - error.error = NEU_ERR_NODE_NOT_EXIST; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; - } - - new_config = neu_taggrp_cfg_clone(config); - bool deleted = false; - - for (int i = 0; i < req->n_id; i++) { - if (neu_datatag_tbl_remove(table, req->ids[i]) == 0) { - vector_t *ids = neu_taggrp_cfg_get_datatag_ids(new_config); - - VECTOR_FOR_EACH(ids, iter) - { - neu_datatag_id_t *id = (neu_datatag_id_t *) iterator_get(&iter); - if (*id == req->ids[i]) { - iterator_erase(ids, &iter); - break; - } - } - - deleted = true; - } - } - - int rv = neu_system_update_group_config(plugin, req->node_id, new_config); - if (0 != rv) { - error.error = rv; - deleted = false; - } - - if (deleted) { - uint32_t event_id = neu_plugin_get_event_id(plugin); - neu_plugin_notify_event_del_tags(plugin, event_id, req->node_id, - req->group_config_name); - } - - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; -} diff --git a/plugins/mqtt/command/datatag.h b/plugins/mqtt/command/datatag.h deleted file mode 100644 index baff3b82..00000000 --- a/plugins/mqtt/command/datatag.h +++ /dev/null @@ -1,43 +0,0 @@ -/** - * NEURON IIoT System for Industry 4.0 - * Copyright (C) 2020-2021 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 NEURON_PLUGIN_MQTT_COMMAND_DATATAG -#define NEURON_PLUGIN_MQTT_COMMAND_DATATAG - -#ifdef __cplusplus -extern "C" { -#endif - -#include - -#include "common.h" - -char *command_get_tags(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_get_tags_req_t *req); -char *command_add_tags(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_add_tags_req_t *req); -char *command_update_tags(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_update_tags_req_t *req); -char *command_delete_tags(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_del_tags_req_t *req); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/plugins/mqtt/command/group_config.c b/plugins/mqtt/command/group_config.c deleted file mode 100644 index 7071baab..00000000 --- a/plugins/mqtt/command/group_config.c +++ /dev/null @@ -1,227 +0,0 @@ -/** - * NEURON IIoT System for Industry 4.0 - * Copyright (C) 2020-2021 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. - **/ - -#include - -#include "group_config.h" -#include "subscribe.h" -#include "json/neu_json_error.h" - -char *command_group_config_get(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_get_group_config_req_t *req) -{ - log_debug("Get group config uuid:%s, node id:%d", mqtt->uuid, req->node_id); - neu_json_get_group_config_resp_t res = { 0 }; - int index = 0; - vector_t configs = neu_system_get_group_configs(plugin, req->node_id); - - res.n_group_config = configs.size; - res.group_configs = - calloc(res.n_group_config, - sizeof(neu_json_get_group_config_resp_group_config_t)); - - VECTOR_FOR_EACH(&configs, iter) - { - neu_taggrp_config_t *config = - *(neu_taggrp_config_t **) iterator_get(&iter); - - res.group_configs[index].name = - (char *) neu_taggrp_cfg_get_name(config); - res.group_configs[index].interval = neu_taggrp_cfg_get_interval(config); - res.group_configs[index].tag_count = - neu_taggrp_cfg_get_datatag_ids(config)->size; - res.group_configs[index].pipe_count = - neu_taggrp_cfg_get_subpipes(config)->size; - - index += 1; - } - - char *result = NULL; - neu_json_encode_with_mqtt(&res, neu_json_encode_get_group_config_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - - vector_uninit(&configs); - free(res.group_configs); - return result; -} - -char *command_group_config_add(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_add_group_config_req_t *req) -{ - log_debug("Add group config uuid:%s, config:%s, node id:%d, " - "read interval:%d", - mqtt->uuid, req->name, req->node_id, req->interval); - neu_taggrp_config_t *config = neu_taggrp_cfg_new(req->name); - neu_taggrp_cfg_set_interval(config, req->interval); - neu_json_error_resp_t error = { 0 }; - error.error = neu_system_add_group_config(plugin, req->node_id, config); - - if (NEU_ERR_SUCCESS != error.error) { - neu_taggrp_cfg_free(config); - } - - char *result = NULL; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; -} - -char *command_group_config_update(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_update_group_config_req_t *req) -{ - log_debug("Update group config uuid:%s, config:%s, src node id:%d," - "read interval:%d", - mqtt->uuid, req->name, req->node_id, req->interval); - neu_json_error_resp_t error = { 0 }; - char * result = NULL; - neu_taggrp_config_t * config = - neu_system_find_group_config(plugin, req->node_id, req->name); - if (NULL == config) { - error.error = NEU_ERR_GRP_CONFIG_NOT_EXIST; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; - } - - neu_taggrp_cfg_set_interval(config, req->interval); - error.error = neu_system_update_group_config(plugin, req->node_id, config); - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; -} - -char *command_group_config_delete(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_del_group_config_req_t *req) -{ - log_debug("Delete group config uuid:%s, config:%s, src node id:%d", - mqtt->uuid, req->name, req->node_id); - neu_json_error_resp_t error = { 0 }; - char * result = NULL; - neu_taggrp_config_t * config = - neu_system_find_group_config(plugin, req->node_id, req->name); - neu_datatag_table_t *table = - neu_system_get_datatags_table(plugin, req->node_id); - - if (NULL == config || NULL == table) { - error.error = NEU_ERR_GRP_CONFIG_NOT_EXIST; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; - } - - if (neu_taggrp_cfg_get_subpipes(config)->size != 0) { - error.error = NEU_ERR_GRP_CONFIG_IN_USE; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; - } - - vector_t *ids = neu_taggrp_cfg_get_datatag_ids(config); - - VECTOR_FOR_EACH(ids, iter) - { - neu_datatag_id_t *id = (neu_datatag_id_t *) iterator_get(&iter); - neu_datatag_tbl_remove(table, *id); - } - - error.error = neu_system_del_group_config(plugin, req->node_id, req->name); - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; -} - -char *command_group_config_subscribe(neu_plugin_t * plugin, - neu_json_mqtt_t * mqtt, - neu_json_subscribe_req_t *req) -{ - log_info("Subscribe uuid:%s, config name:%s", mqtt->uuid, req->name); - neu_json_error_resp_t error = { 0 }; - neu_taggrp_config_t * config = - neu_system_find_group_config(plugin, req->src_node_id, req->name); - if (NULL != config) { - neu_plugin_send_subscribe_cmd(plugin, req->src_node_id, - req->dst_node_id, config); - error.error = NEU_ERR_SUCCESS; - } else { - error.error = NEU_ERR_GRP_CONFIG_NOT_EXIST; - } - - char *result = NULL; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; -} - -char *command_group_config_unsubscribe(neu_plugin_t * plugin, - neu_json_mqtt_t * mqtt, - neu_json_unsubscribe_req_t *req) -{ - log_info("Unsubscribe uuid:%s, config name:%s", mqtt->uuid, req->name); - neu_json_error_resp_t error = { 0 }; - neu_taggrp_config_t * config = - neu_system_find_group_config(plugin, req->src_node_id, req->name); - if (NULL != config) { - neu_plugin_send_unsubscribe_cmd(plugin, req->src_node_id, - req->dst_node_id, config); - error.error = NEU_ERR_SUCCESS; - } else { - error.error = NEU_ERR_GRP_CONFIG_NOT_EXIST; - } - - char *result = NULL; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; -} - -char *command_get_group_config_subscribes(neu_plugin_t * plugin, - neu_json_mqtt_t *mqtt, - neu_json_get_group_config_req_t *req) -{ - neu_json_get_subscribe_resp_t sub_grp_configs = { 0 }; - int index = 0; - vector_t *configs = neu_system_get_sub_group_configs(plugin, req->node_id); - - sub_grp_configs.n_group = configs->size; - sub_grp_configs.groups = - calloc(configs->size, sizeof(neu_json_get_subscribe_resp_group_t)); - - VECTOR_FOR_EACH(configs, iter) - { - neu_sub_grp_config_t *sgc = - (neu_sub_grp_config_t *) iterator_get(&iter); - - sub_grp_configs.groups[index].node_id = sgc->node_id; - sub_grp_configs.groups[index].group_config_name = - sgc->group_config_name; - - index += 1; - } - - vector_free(configs); - - char *result = NULL; - neu_json_encode_with_mqtt(&sub_grp_configs, - neu_json_encode_get_subscribe_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - - free(sub_grp_configs.groups); - - return result; -} diff --git a/plugins/mqtt/command/group_config.h b/plugins/mqtt/command/group_config.h deleted file mode 100644 index e1126a63..00000000 --- a/plugins/mqtt/command/group_config.h +++ /dev/null @@ -1,52 +0,0 @@ -/** - * NEURON IIoT System for Industry 4.0 - * Copyright (C) 2020-2021 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 NEURON_PLUGIN_MQTT_COMMAND_GROUP_CONFIG -#define NEURON_PLUGIN_MQTT_COMMAND_GROUP_CONFIG - -#ifdef __cplusplus -extern "C" { -#endif - -#include - -#include "common.h" - -char *command_group_config_get(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_get_group_config_req_t *req); -char *command_group_config_add(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_add_group_config_req_t *req); -char *command_group_config_update(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_update_group_config_req_t *req); -char *command_group_config_delete(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_del_group_config_req_t *req); -char *command_group_config_subscribe(neu_plugin_t * plugin, - neu_json_mqtt_t * mqtt, - neu_json_subscribe_req_t *req); -char *command_group_config_unsubscribe(neu_plugin_t * plugin, - neu_json_mqtt_t * mqtt, - neu_json_unsubscribe_req_t *req); -char *command_get_group_config_subscribes(neu_plugin_t * plugin, - neu_json_mqtt_t *mqtt, - neu_json_get_group_config_req_t *req); - -#ifdef __cplusplus -} -#endif -#endif \ No newline at end of file diff --git a/plugins/mqtt/command/node.c b/plugins/mqtt/command/node.c deleted file mode 100644 index 617b10d1..00000000 --- a/plugins/mqtt/command/node.c +++ /dev/null @@ -1,161 +0,0 @@ -/** - * NEURON IIoT System for Industry 4.0 - * Copyright (C) 2020-2021 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. - **/ - -#include - -#include "node.h" - -char *command_node_get(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_get_nodes_req_t *req) -{ - log_info("Get node list uuid:%s, node type:%d", mqtt->uuid, req->node_type); - neu_node_type_e node_type = req->node_type; - char * result = NULL; - neu_json_error_resp_t error = { 0 }; - if (NEU_NODE_TYPE_MAX <= req->node_type) { - error.error = NEU_ERR_PARAM_IS_WRONG; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; - } - - neu_json_get_nodes_resp_t res = { 0 }; - int index = 0; - vector_t nodes = neu_system_get_nodes(plugin, node_type); - - res.n_node = nodes.size; - res.nodes = malloc(res.n_node * sizeof(neu_json_get_nodes_resp_node_t)); - memset(res.nodes, 0, res.n_node * sizeof(neu_json_get_nodes_resp_node_t)); - - VECTOR_FOR_EACH(&nodes, iter) - { - neu_node_info_t *info = (neu_node_info_t *) iterator_get(&iter); - - res.nodes[index].id = info->node_id; - res.nodes[index].name = info->node_name; - index += 1; - } - - vector_uninit(&nodes); - - neu_json_encode_with_mqtt(&res, neu_json_encode_get_nodes_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; -} - -char *command_node_add(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_add_node_req_t *req) -{ - log_info("Add node uuid:%s, node type:%d", mqtt->uuid, req->type); - neu_json_error_resp_t error = { 0 }; - if (NEU_NODE_TYPE_MAX <= req->type || NEU_NODE_TYPE_UNKNOW >= req->type) { - error.error = NEU_ERR_NODE_TYPE_INVALID; - } else { - error.error = - neu_system_add_node(plugin, req->type, req->name, req->plugin_name); - } - - char *result = NULL; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; -} - -char *command_node_delete(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_del_node_req_t *req) -{ - log_info("Delete node uuid:%s, node id:%d", mqtt->uuid, req->id); - neu_json_error_resp_t error = { 0 }; - error.error = neu_system_del_node(plugin, req->id); - char *result = NULL; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; -} - -char *command_node_setting_set(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_node_setting_req_t *req, - const char * json_str) -{ - log_info("Set node setting uuid:%s, node id:%d", mqtt->uuid, req->node_id); - neu_json_error_resp_t error = { 0 }; - char * result = NULL; - error.error = neu_plugin_set_node_setting(plugin, req->node_id, json_str); - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; -} - -char *command_node_setting_get(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_node_setting_req_t *req) -{ - log_info("Get node setting uuid:%s, node id:%d", mqtt->uuid, req->node_id); - neu_json_error_resp_t error = { 0 }; - char * settings = NULL; - char * result = NULL; - error.error = neu_plugin_get_node_setting(plugin, req->node_id, &settings); - if (0 != error.error) { - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; - } - if (0 != - neu_json_encode_setting_with_mqtt(req->node_id, settings, mqtt, - &result)) { - error.error = NEU_ERR_PARAM_IS_WRONG; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - } - free(settings); - return result; -} - -char *command_node_state_get(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_node_setting_req_t *req) -{ - log_info("Get node state uuid:%s, node id:%d", mqtt->uuid, req->node_id); - neu_plugin_state_t state = { 0 }; - neu_json_get_node_state_resp_t res = { 0 }; - neu_json_error_resp_t error = { 0 }; - char * result = NULL; - error.error = neu_plugin_get_node_state(plugin, req->node_id, &state); - if (0 != error.error) { - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; - } - - res.running = state.running; - res.link = state.link; - neu_json_encode_with_mqtt(&res, neu_json_encode_get_node_state_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; -} - -char *command_node_control(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_node_ctl_req_t *req) -{ - log_info("Node control uuid:%s, node id:%d", mqtt->uuid, req->id); - char * result = NULL; - neu_json_error_resp_t error = { 0 }; - error.error = neu_plugin_node_ctl(plugin, req->id, req->cmd); - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; -} diff --git a/plugins/mqtt/command/node.h b/plugins/mqtt/command/node.h deleted file mode 100644 index c19f529e..00000000 --- a/plugins/mqtt/command/node.h +++ /dev/null @@ -1,50 +0,0 @@ -/** - * NEURON IIoT System for Industry 4.0 - * Copyright (C) 2020-2021 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 NEURON_PLUGIN_MQTT_COMMAND_NODE -#define NEURON_PLUGIN_MQTT_COMMAND_NODE - -#ifdef __cplusplus -extern "C" { -#endif - -#include - -#include "common.h" - -char *command_node_get(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_get_nodes_req_t *req); -char *command_node_add(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_add_node_req_t *req); -char *command_node_delete(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_del_node_req_t *req); -char *command_node_setting_set(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_node_setting_req_t *req, - const char * json_str); -char *command_node_setting_get(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_node_setting_req_t *req); -char *command_node_state_get(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_node_setting_req_t *req); -char *command_node_control(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_node_ctl_req_t *req); - -#ifdef __cplusplus -} -#endif -#endif \ No newline at end of file diff --git a/plugins/mqtt/command/plugin.c b/plugins/mqtt/command/plugin.c deleted file mode 100644 index f83bb556..00000000 --- a/plugins/mqtt/command/plugin.c +++ /dev/null @@ -1,91 +0,0 @@ -/** - * NEURON IIoT System for Industry 4.0 - * Copyright (C) 2020-2021 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. - **/ - -#include - -#include "plugin.h" - -char *command_plugin_get(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_get_plugin_req_t *req) -{ - log_info("Get plugin list uuid:%s, plugin id:%d", mqtt->uuid, req->id); - neu_json_get_plugin_resp_t res = { 0 }; - int index = 0; - vector_t plugin_libs = neu_system_get_plugin(plugin); - - res.n_plugin_lib = plugin_libs.size; - if (0 < req->id) { - res.n_plugin_lib = 1; - } - - res.plugin_libs = - calloc(res.n_plugin_lib, sizeof(neu_json_get_plugin_resp_plugin_lib_t)); - - VECTOR_FOR_EACH(&plugin_libs, iter) - { - plugin_lib_info_t *info = (plugin_lib_info_t *) iterator_get(&iter); - - if (0 < req->id && (req->id != info->plugin_id.id_val)) { - continue; - } - - res.plugin_libs[index].node_type = info->node_type; - res.plugin_libs[index].id = info->plugin_id.id_val; - res.plugin_libs[index].kind = info->plugin_kind; - res.plugin_libs[index].name = (char *) info->plugin_name; - res.plugin_libs[index].lib_name = (char *) info->plugin_lib_name; - - index += 1; - } - - vector_uninit(&plugin_libs); - - char *result = NULL; - neu_json_encode_with_mqtt(&res, neu_json_encode_get_plugin_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; -} - -char *command_plugin_add(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_add_plugin_req_t *req) -{ - log_info("Add plugin uuid:%s, node name:%s", mqtt->uuid, req->lib_name); - neu_json_error_resp_t error = { 0 }; - - error.error = neu_system_add_plugin(plugin, req->lib_name); - - char *result = NULL; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; -} - -char *command_plugin_delete(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_del_plugin_req_t *req) -{ - log_info("Delete plugin uuid:%s, plugin id:%d", mqtt->uuid, req->id); - neu_json_error_resp_t error = { 0 }; - plugin_id_t id = { 0 }; - id.id_val = req->id; - error.error = neu_system_del_plugin(plugin, id); - char *result = NULL; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; -} \ No newline at end of file diff --git a/plugins/mqtt/command/plugin.h b/plugins/mqtt/command/plugin.h deleted file mode 100644 index a1a15b88..00000000 --- a/plugins/mqtt/command/plugin.h +++ /dev/null @@ -1,41 +0,0 @@ -/** - * NEURON IIoT System for Industry 4.0 - * Copyright (C) 2020-2021 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 NEURON_PLUGIN_MQTT_COMMAND_PLUGIN -#define NEURON_PLUGIN_MQTT_COMMAND_PLUGIN - -#ifdef __cplusplus -extern "C" { -#endif - -#include - -#include "common.h" - -char *command_plugin_get(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_get_plugin_req_t *req); -char *command_plugin_add(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_add_plugin_req_t *req); -char *command_plugin_delete(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_del_plugin_req_t *req); - -#ifdef __cplusplus -} -#endif -#endif \ No newline at end of file diff --git a/plugins/mqtt/command/schema.c b/plugins/mqtt/command/schema.c deleted file mode 100644 index bc8af7b3..00000000 --- a/plugins/mqtt/command/schema.c +++ /dev/null @@ -1,69 +0,0 @@ -/** - * NEURON IIoT System for Industry 4.0 - * Copyright (C) 2020-2021 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. - **/ - -#include -#include -#include - -#include "schema.h" - -#define SCHEMA_PATH "./plugin_param_schema.json" - -static char *schema_inject(char *buf, neu_json_mqtt_t *mqtt) -{ - json_error_t error; - json_t * schema = json_loads(buf, 0, &error); - - if (NULL == schema) { - log_debug("json error, column:%d, line:%d, pos:%d, %s, %s", - error.column, error.line, error.position, error.source, - error.text); - return NULL; - } - - json_t *root = json_object(); - json_object_set(root, "schema", schema); - json_object_set(root, "uuid", json_string(mqtt->uuid)); - json_object_set(root, "command", json_string(mqtt->command)); - char *ret_str = json_dumps(root, 4096); - return ret_str; -} - -char *command_schema_get(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt) -{ - UNUSED(plugin); - - size_t len = 0; - char * buf = NULL; - buf = file_string_read(&len, SCHEMA_PATH); - if (buf == NULL) { - log_info("open %s error: %d", SCHEMA_PATH, errno); - - char * result = NULL; - neu_json_error_resp_t error = { 0 }; - error.error = NEU_ERR_FAILURE; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; - } - - char *ret_str = schema_inject(buf, mqtt); - free(buf); - return ret_str; -} diff --git a/plugins/mqtt/command/schema.h b/plugins/mqtt/command/schema.h deleted file mode 100644 index 223f68ba..00000000 --- a/plugins/mqtt/command/schema.h +++ /dev/null @@ -1,36 +0,0 @@ -/** - * NEURON IIoT System for Industry 4.0 - * Copyright (C) 2020-2021 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 NEURON_PLUGIN_MQTT_COMMAND_SCHEMA -#define NEURON_PLUGIN_MQTT_COMMAND_SCHEMA - -#ifdef __cplusplus -extern "C" { -#endif - -#include - -#include "common.h" - -char *command_schema_get(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt); - -#ifdef __cplusplus -} -#endif -#endif \ No newline at end of file diff --git a/plugins/mqtt/command/tty.c b/plugins/mqtt/command/tty.c deleted file mode 100644 index 31d6e074..00000000 --- a/plugins/mqtt/command/tty.c +++ /dev/null @@ -1,51 +0,0 @@ -/** - * NEURON IIoT System for Industry 4.0 - * Copyright (C) 2020-2021 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. - **/ - -#include -#include -#include - -#include "tty.h" - -char *command_tty_get(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt) -{ - log_info("Get tty uuid:%s", mqtt->uuid); - - UNUSED(plugin); - neu_json_get_tty_resp_t ttys_res = { 0 }; - char * result = NULL; - ttys_res.n_tty = tty_file_list_get(&ttys_res.ttys); - if (ttys_res.n_tty == -1) { - neu_json_error_resp_t error = { 0 }; - error.error = NEU_ERR_FAILURE; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - } else { - neu_json_encode_with_mqtt(&ttys_res, neu_json_encode_get_tty_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - - for (int i = 0; i < ttys_res.n_tty; i++) { - free(ttys_res.ttys[i]); - } - - free(ttys_res.ttys); - } - - return result; -} diff --git a/plugins/mqtt/command/tty.h b/plugins/mqtt/command/tty.h deleted file mode 100644 index bca62495..00000000 --- a/plugins/mqtt/command/tty.h +++ /dev/null @@ -1,36 +0,0 @@ -/** - * NEURON IIoT System for Industry 4.0 - * Copyright (C) 2020-2021 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 NEURON_PLUGIN_MQTT_COMMAND_TTY -#define NEURON_PLUGIN_MQTT_COMMAND_TTY - -#ifdef __cplusplus -extern "C" { -#endif - -#include - -#include "common.h" - -char *command_tty_get(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt); - -#ifdef __cplusplus -} -#endif -#endif \ No newline at end of file diff --git a/plugins/mqtt/mqtt.c b/plugins/mqtt/mqtt.c index dddfa89d..860cd2e2 100644 --- a/plugins/mqtt/mqtt.c +++ b/plugins/mqtt/mqtt.c @@ -131,46 +131,21 @@ static void topics_cleanup(vector_t *topics) static void topics_generate(vector_t *topics, char *name) { - topics_add(topics, real_topic_generate(TOPIC_PING_REQ, name), QOS0, - real_topic_generate(TOPIC_STATUS_RES, name), QOS0, - TOPIC_TYPE_PING); - topics_add(topics, real_topic_generate(TOPIC_NODE_REQ, name), QOS0, - real_topic_generate(TOPIC_NODE_RES, name), QOS0, - TOPIC_TYPE_NODE); - topics_add(topics, real_topic_generate(TOPIC_GCONFIG_REQ, name), QOS0, - real_topic_generate(TOPIC_GCONFIG_RES, name), QOS0, - TOPIC_TYPE_GCONFIG); - topics_add(topics, real_topic_generate(TOPIC_TAGS_REQ, name), QOS0, - real_topic_generate(TOPIC_TAGS_RES, name), QOS0, - TOPIC_TYPE_TAGS); - topics_add(topics, real_topic_generate(TOPIC_PLUGIN_REQ, name), QOS0, - real_topic_generate(TOPIC_PLUGIN_RES, name), QOS0, - TOPIC_TYPE_PLUGIN); - topics_add(topics, real_topic_generate(TOPIC_SUBSCRIBE_REQ, name), QOS0, - real_topic_generate(TOPIC_SUBSCRIBE_RES, name), QOS0, - TOPIC_TYPE_SUBSCRIBE); - topics_add(topics, real_topic_generate(TOPIC_READ_REQ, name), QOS0, - real_topic_generate(TOPIC_READ_RES, name), QOS0, - TOPIC_TYPE_READ); - topics_add(topics, real_topic_generate(TOPIC_WRITE_REQ, name), QOS0, - real_topic_generate(TOPIC_WRITE_RES, name), QOS0, - TOPIC_TYPE_WRITE); - topics_add(topics, real_topic_generate(TOPIC_TTYS_REQ, name), QOS0, - real_topic_generate(TOPIC_TTYS_RES, name), QOS0, - TOPIC_TYPE_TTYS); - topics_add(topics, real_topic_generate(TOPIC_SCHEMA_REQ, name), QOS0, - real_topic_generate(TOPIC_SCHEMA_RES, name), QOS0, - TOPIC_TYPE_SCHEMA); - topics_add(topics, real_topic_generate(TOPIC_SETTING_REQ, name), QOS0, - real_topic_generate(TOPIC_SETTING_RES, name), QOS0, - TOPIC_TYPE_SETTING); - topics_add(topics, real_topic_generate(TOPIC_CTR_REQ, name), QOS0, - real_topic_generate(TOPIC_CTR_RES, name), QOS0, TOPIC_TYPE_CTR); - topics_add(topics, real_topic_generate(TOPIC_STATE_REQ, name), QOS0, - real_topic_generate(TOPIC_STATE_RES, name), QOS0, - TOPIC_TYPE_STATE); - topics_add(topics, NULL, QOS0, real_topic_generate(TOPIC_UPLOAD_RES, name), - QOS0, TOPIC_TYPE_UPLOAD); + char *ping_req = real_topic_generate(TOPIC_PING_REQ, name); + char *ping_res = real_topic_generate(TOPIC_STATUS_RES, name); + topics_add(topics, ping_req, QOS0, ping_res, QOS0, TOPIC_TYPE_PING); + + char *read_req = real_topic_generate(TOPIC_READ_REQ, name); + char *read_res = real_topic_generate(TOPIC_READ_RES, name); + topics_add(topics, read_req, QOS0, read_res, QOS0, TOPIC_TYPE_READ); + + char *write_req = real_topic_generate(TOPIC_WRITE_REQ, name); + char *write_res = real_topic_generate(TOPIC_WRITE_RES, name); + topics_add(topics, write_req, QOS0, write_res, QOS0, TOPIC_TYPE_WRITE); + + char *upload_req = NULL; + char *upload_res = real_topic_generate(TOPIC_UPLOAD_RES, name); + topics_add(topics, upload_req, QOS0, upload_res, QOS0, TOPIC_TYPE_UPLOAD); } static bool topic_match(const void *key, const void *item) diff --git a/plugins/mqtt/mqtt.h b/plugins/mqtt/mqtt.h index cfec5c98..d875d3e3 100644 --- a/plugins/mqtt/mqtt.h +++ b/plugins/mqtt/mqtt.h @@ -31,32 +31,12 @@ extern "C" { #define TIMEOUT 3000U #define TOPIC_PING_REQ "neuron/%s/ping" -#define TOPIC_NODE_REQ "neuron/%s/node/req" -#define TOPIC_GCONFIG_REQ "neuron/%s/gconfig/req" -#define TOPIC_TAGS_REQ "neuron/%s/tags/req" -#define TOPIC_PLUGIN_REQ "neuron/%s/plugin/req" -#define TOPIC_SUBSCRIBE_REQ "neuron/%s/subscribe/req" #define TOPIC_READ_REQ "neuron/%s/read/req" #define TOPIC_WRITE_REQ "neuron/%s/write/req" -#define TOPIC_TTYS_REQ "neuron/%s/ttys/req" -#define TOPIC_SCHEMA_REQ "neuron/%s/schema/plugin/req" -#define TOPIC_SETTING_REQ "neuron/%s/node/setting/req" -#define TOPIC_CTR_REQ "neuron/%s/node/ctl/req" -#define TOPIC_STATE_REQ "neuron/%s/node/state/req" #define TOPIC_STATUS_RES "neuron/%s/status" -#define TOPIC_NODE_RES "neuron/%s/node/resp" -#define TOPIC_GCONFIG_RES "neuron/%s/gconfig/resp" -#define TOPIC_TAGS_RES "neuron/%s/tags/resp" -#define TOPIC_PLUGIN_RES "neuron/%s/plugin/resp" -#define TOPIC_SUBSCRIBE_RES "neuron/%s/subscribe/resp" #define TOPIC_READ_RES "neuron/%s/read/resp" #define TOPIC_WRITE_RES "neuron/%s/write/resp" -#define TOPIC_TTYS_RES "neuron/%s/ttys/resp" -#define TOPIC_SCHEMA_RES "neuron/%s/schema/plugin/resp" -#define TOPIC_SETTING_RES "neuron/%s/node/setting/resp" -#define TOPIC_CTR_RES "neuron/%s/node/ctl/resp" -#define TOPIC_STATE_RES "neuron/%s/node/state/resp" #define TOPIC_UPLOAD_RES "neuron/%s/upload" #define QOS0 0 diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index c64a8bdb..0124ba76 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -115,8 +115,7 @@ target_link_libraries(http_test neuron-base gtest_main gtest yaml jansson nng) #file(COPY ${CMAKE_SOURCE_DIR}/plugin_param_schema.json DESTINATION ${CMAKE_BINARY_DIR}/tests) add_executable(mqtt_test mqtt_test.cc ${CMAKE_SOURCE_DIR}/src/config.c - ${CMAKE_SOURCE_DIR}/plugins/mqtt/util.c - ${CMAKE_SOURCE_DIR}/plugins/mqtt/command/node.c) + ${CMAKE_SOURCE_DIR}/plugins/mqtt/util.c) target_include_directories(mqtt_test PRIVATE ${CMAKE_SOURCE_DIR}/src ${CMAKE_SOURCE_DIR}/include diff --git a/tests/mqtt_test.cc b/tests/mqtt_test.cc index e816cc81..02a214d9 100644 --- a/tests/mqtt_test.cc +++ b/tests/mqtt_test.cc @@ -1,6 +1,5 @@ #include -#include "command/node.h" #include "util.h" #include