From dae9f9def4ac424bdd59bd959c36e9c4e76a696c Mon Sep 17 00:00:00 2001 From: gc87 Date: Thu, 16 Sep 2021 13:34:39 +0800 Subject: [PATCH] Read configuration file --- neuron.yaml | 18 ++++++++++++++++- plugins/mqtt/CMakeLists.txt | 7 +++---- plugins/mqtt/plugin_mqtt.c | 39 +++++++++++++++++++++++++----------- plugins/opcua/CMakeLists.txt | 5 +++-- plugins/opcua/plugin_opcua.c | 33 ++++++++++++++++++++++-------- 5 files changed, 75 insertions(+), 27 deletions(-) diff --git a/neuron.yaml b/neuron.yaml index f4d6a258..c3141f34 100644 --- a/neuron.yaml +++ b/neuron.yaml @@ -1,7 +1,14 @@ lib_path: ./ mqtt: + id: neuron-lite-mqtt-plugin-10012 + mqtt_version: 4 + topic: neuronlite/response + qos: 1 + keepalive_interval: 20 + clean_session: 1 broker: - host: 127.0.0.1 + connection: tcp:// + host: localhost port: 1883 web: host: 0.0.0.0 @@ -9,3 +16,12 @@ web: api: host: 0.0.0.0 port: 7001 +opcua: + default_cert_file: ./opcua-default-cert.der + default_key_file: ./opcua-default-key.der + host: localhost + port: 4840 + username: gaoc01 + password: 123456 + cert_file: "" + key_file: "" \ No newline at end of file diff --git a/plugins/mqtt/CMakeLists.txt b/plugins/mqtt/CMakeLists.txt index aca0004b..98c9d5db 100644 --- a/plugins/mqtt/CMakeLists.txt +++ b/plugins/mqtt/CMakeLists.txt @@ -1,10 +1,9 @@ find_package(eclipse-paho-mqtt-c 1.3.9 REQUIRED) -# include_directories(../../include/) - file(GLOB PARSER_SRC "${CMAKE_SOURCE_DIR}/src/parser/*.c") +file(GLOB CONFIG_SRC "${CMAKE_SOURCE_DIR}/src/config.c") set(LIBRARY_OUTPUT_PATH "${CMAKE_BINARY_DIR}") -set(src ${PARSER_SRC} plugin_mqtt.c paho_client.c message_handle.c) +set(src ${PARSER_SRC} ${CONFIG_SRC} plugin_mqtt.c paho_client.c message_handle.c) add_library(plugin-mqtt SHARED ${src}) target_include_directories(plugin-mqtt PRIVATE @@ -12,5 +11,5 @@ target_include_directories(plugin-mqtt PRIVATE ${CMAKE_SOURCE_DIR}/include ) -target_link_libraries(plugin-mqtt neuron-base paho-mqtt3as ssl crypto) +target_link_libraries(plugin-mqtt neuron-base paho-mqtt3as ssl crypto yaml) target_link_libraries(plugin-mqtt ${CMAKE_THREAD_LIBS_INIT}) diff --git a/plugins/mqtt/plugin_mqtt.c b/plugins/mqtt/plugin_mqtt.c index 336a2081..51db60c5 100644 --- a/plugins/mqtt/plugin_mqtt.c +++ b/plugins/mqtt/plugin_mqtt.c @@ -21,6 +21,7 @@ #include #include +#include #include #include "message_handle.h" @@ -36,7 +37,6 @@ struct neu_plugin { neu_plugin_common_t common; option_t option; paho_client_t * paho; - uint32_t new_event_id; neu_list context_list; }; @@ -163,19 +163,34 @@ static int mqtt_plugin_close(neu_plugin_t *plugin) static int mqtt_plugin_init(neu_plugin_t *plugin) { - plugin->new_event_id = 1; + const char *clientid = + neu_config_get_value("./neuron.yaml", 2, "mqtt", "id"); + const char *mqtt_version = + neu_config_get_value("./neuron.yaml", 2, "mqtt", "mqtt_version"); + const char *topic = + neu_config_get_value("./neuron.yaml", 2, "mqtt", "topic"); + const char *qos = neu_config_get_value("./neuron.yaml", 2, "mqtt", "qos"); + const char *keepalive_interval = + neu_config_get_value("./neuron.yaml", 2, "mqtt", "keepalive_interval"); + const char *clean_session = + neu_config_get_value("./neuron.yaml", 2, "mqtt", "clean_session"); + const char *connection = neu_config_get_value("./neuron.yaml", 3, "mqtt", + "broker", "connection"); + const char *host = + neu_config_get_value("./neuron.yaml", 3, "mqtt", "broker", "host"); + const char *port = + neu_config_get_value("./neuron.yaml", 3, "mqtt", "broker", "port"); // MQTT option - plugin->option.clientid = "neuron-lite-mqtt-plugin-10012"; - plugin->option.MQTT_version = 4; - plugin->option.topic = "neuronlite/response"; - plugin->option.qos = 1; - plugin->option.connection = "tcp://"; - // plugin->option.host = "broker.emqx.io"; - plugin->option.host = "localhost"; - plugin->option.port = "1883"; - plugin->option.keepalive_interval = 20; - plugin->option.clean_session = 1; + plugin->option.clientid = strdup(clientid); + plugin->option.MQTT_version = atoi(mqtt_version); + plugin->option.topic = strdup(topic); + plugin->option.qos = atoi(qos); + plugin->option.connection = strdup(connection); + plugin->option.host = strdup(host); + plugin->option.port = strdup(port); + plugin->option.keepalive_interval = atoi(keepalive_interval); + plugin->option.clean_session = atoi(clean_session); // Paho mqtt client setup client_error error = diff --git a/plugins/opcua/CMakeLists.txt b/plugins/opcua/CMakeLists.txt index 53c01725..ccd4b8eb 100644 --- a/plugins/opcua/CMakeLists.txt +++ b/plugins/opcua/CMakeLists.txt @@ -1,8 +1,9 @@ find_package(open62541 1.2.2 REQUIRED) file(GLOB PARSER_SRC "${CMAKE_SOURCE_DIR}/src/parser/*.c") +file(GLOB CONFIG_SRC "${CMAKE_SOURCE_DIR}/src/config.c") set(LIBRARY_OUTPUT_PATH "${CMAKE_BINARY_DIR}") -set(src ${PARSER_SRC} plugin_opcua.c open62541_client.h open62541_client.c plugin_handle.h plugin_handle.c) +set(src ${PARSER_SRC} ${CONFIG_SRC} plugin_opcua.c open62541_client.h open62541_client.c plugin_handle.h plugin_handle.c) add_library(plugin-opcua SHARED ${src}) target_include_directories(plugin-opcua PRIVATE @@ -10,5 +11,5 @@ target_include_directories(plugin-opcua PRIVATE ${CMAKE_SOURCE_DIR}/include ) -target_link_libraries(plugin-opcua neuron-base open62541 ssl crypto) +target_link_libraries(plugin-opcua neuron-base open62541 ssl crypto yaml) target_link_libraries(plugin-opcua ${CMAKE_THREAD_LIBS_INIT}) diff --git a/plugins/opcua/plugin_opcua.c b/plugins/opcua/plugin_opcua.c index f7aca9e3..573dd8d2 100644 --- a/plugins/opcua/plugin_opcua.c +++ b/plugins/opcua/plugin_opcua.c @@ -21,6 +21,7 @@ #include #include +#include #include #include "option.h" @@ -73,15 +74,31 @@ static int opcua_plugin_close(neu_plugin_t *plugin) static int opcua_plugin_init(neu_plugin_t *plugin) { + const char *default_cert_file = + neu_config_get_value("./neuron.yaml", 2, "opcua", "default_cert_file"); + const char *default_key_file = + neu_config_get_value("./neuron.yaml", 2, "opcua", "default_key_file"); + const char *host = + neu_config_get_value("./neuron.yaml", 2, "opcua", "host"); + const char *port = neu_config_get_value("neuron.yaml", 2, "opcua", "port"); + const char *username = + neu_config_get_value("./neuron.yaml", 2, "opcua", "username"); + const char *password = + neu_config_get_value("./neuron.yaml", 2, "opcua", "password"); + const char *cert_file = + neu_config_get_value("./neuron.yaml", 2, "opcua", "cert_file"); + const char *key_file = + neu_config_get_value("./neuron.yaml", 2, "opcua", "key_file"); + // OPC-UA option - plugin->option.default_cert_file = "./opcua-default-cert.der"; - plugin->option.default_key_file = "./opcua-default-key.der"; - plugin->option.host = "localhost"; - plugin->option.port = 4840; - plugin->option.username = "gaoc01"; - plugin->option.password = "123456"; - plugin->option.cert_file = ""; - plugin->option.key_file = ""; + plugin->option.default_cert_file = strdup(default_cert_file); + plugin->option.default_key_file = strdup(default_key_file); + plugin->option.host = strdup(host); + plugin->option.port = atoi(port); + plugin->option.username = strdup(username); + plugin->option.password = strdup(password); + plugin->option.cert_file = strdup(cert_file); + plugin->option.key_file = strdup(key_file); int rc = open62541_client_open(&plugin->option, plugin, &plugin->client); if (0 != rc) {