mirror of
https://github.com/open62541/open62541.git
synced 2026-10-16 20:33:18 +08:00
e41e2ca79d
Add UA_Client_newFromFile() and UA_ClientConfig_loadFromFile() to create and configure a client from a JSON5 configuration file.
86 lines
3.2 KiB
C
86 lines
3.2 KiB
C
/* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
|
|
* See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
|
|
|
|
#include <open62541/client.h>
|
|
#include <open62541/client_highlevel.h>
|
|
#include <open62541/client_config_default.h>
|
|
#include <open62541/plugin/log_stdout.h>
|
|
|
|
#include <stdlib.h>
|
|
#include "common.h"
|
|
|
|
int main(int argc, char** argv) {
|
|
UA_ByteString json_config = UA_BYTESTRING_NULL;
|
|
|
|
if(argc >= 2) {
|
|
/* Load client config */
|
|
json_config = loadFile(argv[1]);
|
|
if (UA_String_isEmpty(&json_config)) {
|
|
UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_APPLICATION,
|
|
"Failed to load the client configuration from file '%s'.",
|
|
argv[1]);
|
|
return EXIT_FAILURE;
|
|
}
|
|
} else {
|
|
UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_APPLICATION,
|
|
"Missing argument. Argument are "
|
|
"<client-config.json5>");
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
/*
|
|
* Create the client directly from the JSON file. Alternatively, the configuration
|
|
* can be loaded into a UA_ClientConfig and then used to create the client. The
|
|
* following is identical to the line below:
|
|
*
|
|
* UA_ClientConfig clientConfig;
|
|
* UA_StatusCode res = UA_ClientConfig_loadFromFile(&clientConfig, json_config);
|
|
* UA_Client *client = UA_Client_new(&clientConfig);
|
|
*
|
|
*/
|
|
UA_Client *client = UA_Client_newFromFile(json_config);
|
|
|
|
/* identical to first steps example */
|
|
UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
|
|
if(retval != UA_STATUSCODE_GOOD) {
|
|
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_APPLICATION,
|
|
"The connection failed with status code %s",
|
|
UA_StatusCode_name(retval));
|
|
UA_Client_delete(client);
|
|
UA_ByteString_clear(&json_config);
|
|
return 0;
|
|
}
|
|
|
|
/* Read the value attribute of the node. UA_Client_readValueAttribute is a
|
|
* wrapper for the raw read service available as UA_Client_Service_read. */
|
|
UA_Variant value;
|
|
UA_Variant_init(&value);
|
|
|
|
/* NodeId of the variable holding the current time */
|
|
const UA_NodeId nodeId = UA_NS0ID(SERVER_SERVERSTATUS_CURRENTTIME);
|
|
retval = UA_Client_readValueAttribute(client, nodeId, &value);
|
|
|
|
if(retval == UA_STATUSCODE_GOOD &&
|
|
UA_Variant_hasScalarType(&value, &UA_TYPES[UA_TYPES_DATETIME])) {
|
|
UA_DateTime raw_date = *(UA_DateTime *) value.data;
|
|
UA_DateTimeStruct dts = UA_DateTime_toStruct(raw_date);
|
|
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_APPLICATION,
|
|
"date is: %u-%u-%u %u:%u:%u.%03u",
|
|
dts.day, dts.month, dts.year, dts.hour,
|
|
dts.min, dts.sec, dts.milliSec);
|
|
} else {
|
|
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_APPLICATION,
|
|
"Reading the value failed with status code %s",
|
|
UA_StatusCode_name(retval));
|
|
}
|
|
|
|
/* Clean up */
|
|
UA_Variant_clear(&value);
|
|
UA_Client_delete(client); /* Disconnects the client internally */
|
|
|
|
/* clean up */
|
|
UA_ByteString_clear(&json_config);
|
|
|
|
return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
|
|
}
|