123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
- * See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
- /**
- * Connecting a Variable with a Physical Process
- * ---------------------------------------------
- *
- * In OPC UA-based architectures, servers are typically situated near the source
- * of information. In an industrial context, this translates into servers being
- * near the physical process and clients consuming the data at runtime. In the
- * previous tutorial, we saw how to add variables to an OPC UA information
- * model. This tutorial shows how to connect a variable to runtime information,
- * for example from measurements of a physical process. For simplicty, we take
- * the system clock as the underlying "process".
- *
- * The following code snippets are each concerned with a different way of
- * updating variable values at runtime. Taken together, the code snippets define
- * a compilable source file.
- *
- * Updating variables manually
- * ^^^^^^^^^^^^^^^^^^^^^^^^^^^
- * As a starting point, assume that a variable for a value of type
- * :ref:`datetime` has been created in the server with the identifier
- * "ns=1,s=current-time". Assuming that our applications gets triggered when a
- * new value arrives from the underlying process, we can just write into the
- * variable. */
- #include <signal.h>
- #include "open62541.h"
- static void
- updateCurrentTime(UA_Server *server) {
- UA_DateTime now = UA_DateTime_now();
- UA_Variant value;
- UA_Variant_setScalar(&value, &now, &UA_TYPES[UA_TYPES_DATETIME]);
- UA_NodeId currentNodeId = UA_NODEID_STRING(1, "current-time");
- UA_Server_writeValue(server, currentNodeId, value);
- }
- static void
- addCurrentTimeVariable(UA_Server *server) {
- UA_DateTime now = 0;
- UA_VariableAttributes attr;
- UA_VariableAttributes_init(&attr);
- attr.displayName = UA_LOCALIZEDTEXT("en_US", "Current time");
- UA_Variant_setScalar(&attr.value, &now, &UA_TYPES[UA_TYPES_DATETIME]);
- UA_NodeId currentNodeId = UA_NODEID_STRING(1, "current-time");
- UA_QualifiedName currentName = UA_QUALIFIEDNAME(1, "current-time");
- UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
- UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
- UA_NodeId variableTypeNodeId = UA_NODEID_NULL;
- UA_Server_addVariableNode(server, currentNodeId, parentNodeId,
- parentReferenceNodeId, currentName,
- variableTypeNodeId, attr, NULL, NULL);
- updateCurrentTime(server);
- }
- /**
- * Variable Value Callback
- * ^^^^^^^^^^^^^^^^^^^^^^^
- *
- * When a value changes continuously, such as the system time, updating the
- * value in a tight loop would take up a lot of resources. Value callbacks allow
- * to synchronize a variable value with an external representation. They attach
- * callbacks to the variable that are executed before every read and after every
- * write operation. */
- static void
- beforeReadTime(void *handle, const UA_NodeId nodeid, const UA_Variant *data,
- const UA_NumericRange *range) {
- UA_Server *server = (UA_Server*)handle;
- UA_DateTime now = UA_DateTime_now();
- UA_Variant value;
- UA_Variant_setScalar(&value, &now, &UA_TYPES[UA_TYPES_DATETIME]);
- UA_NodeId currentNodeId = UA_NODEID_STRING(1, "current-time");
- UA_Server_writeValue(server, currentNodeId, value);
- }
- static void
- afterWriteTime(void *handle, const UA_NodeId nodeid, const UA_Variant *data,
- const UA_NumericRange *range) {
- UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
- "The variable was updated");
- }
- static void
- addValueCallbackToCurrentTimeVariable(UA_Server *server) {
- UA_NodeId currentNodeId = UA_NODEID_STRING(1, "current-time");
- UA_ValueCallback callback ;
- callback.handle = server;
- callback.onRead = beforeReadTime;
- callback.onWrite = afterWriteTime;
- UA_Server_setVariableNode_valueCallback(server, currentNodeId, callback);
- }
- /**
- * Variable Data Sources
- * ^^^^^^^^^^^^^^^^^^^^^
- *
- * With value callbacks, the value is still stored in the variable node.
- * So-called data sources go one step further. The server redirects every read
- * and write request to a callback function. Upon reading, the callback provides
- * copy of the current value. Internally, the data source needs to implement its
- * own memory management. */
- static UA_StatusCode
- readCurrentTime(void *handle, const UA_NodeId nodeid, UA_Boolean sourceTimeStamp,
- const UA_NumericRange *range, UA_DataValue *dataValue) {
- UA_DateTime now = UA_DateTime_now();
- UA_Variant_setScalarCopy(&dataValue->value, &now,
- &UA_TYPES[UA_TYPES_DATETIME]);
- dataValue->hasValue = true;
- return UA_STATUSCODE_GOOD;
- }
- static UA_StatusCode
- writeCurrentTime(void *handle, const UA_NodeId nodeid, const UA_Variant *data,
- const UA_NumericRange *range) {
- UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
- "Changing the system time is not implemented");
- return UA_STATUSCODE_BADINTERNALERROR;
- }
- static void
- addCurrentTimeDataSourceVariable(UA_Server *server) {
- UA_VariableAttributes attr;
- UA_VariableAttributes_init(&attr);
- attr.displayName = UA_LOCALIZEDTEXT("en_US", "Current time - data source");
- UA_NodeId currentNodeId = UA_NODEID_STRING(1, "current-time-datasource");
- UA_QualifiedName currentName = UA_QUALIFIEDNAME(1, "current-time-datasource");
- UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
- UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
- UA_NodeId variableTypeNodeId = UA_NODEID_NULL;
- UA_DataSource timeDataSource;
- timeDataSource.handle = NULL;
- timeDataSource.read = readCurrentTime;
- timeDataSource.write = writeCurrentTime;
- UA_Server_addDataSourceVariableNode(server, currentNodeId, parentNodeId,
- parentReferenceNodeId, currentName,
- variableTypeNodeId, attr,
- timeDataSource, NULL);
- }
- /** It follows the main server code, making use of the above definitions. */
- UA_Boolean running = true;
- static void stopHandler(int sign) {
- UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
- running = false;
- }
- int main(void) {
- signal(SIGINT, stopHandler);
- signal(SIGTERM, stopHandler);
- UA_ServerConfig config = UA_ServerConfig_standard;
- UA_ServerNetworkLayer nl =
- UA_ServerNetworkLayerTCP(UA_ConnectionConfig_standard, 16664);
- config.networkLayers = &nl;
- config.networkLayersSize = 1;
- UA_Server *server = UA_Server_new(config);
- addCurrentTimeVariable(server);
- addValueCallbackToCurrentTimeVariable(server);
- addCurrentTimeDataSourceVariable(server);
- UA_Server_run(server, &running);
- UA_Server_delete(server);
- nl.deleteMembers(&nl);
- return 0;
- }
- /**
- * DataChange Notifications
- * ^^^^^^^^^^^^^^^^^^^^^^^^
- * A client that is interested in the current value of a variable does not need
- * to regularly poll the variable. Instead, he can use the Subscription
- * mechanism to be notified about changes.
- *
- * Within a Subscription, the client adds so-called MonitoredItems. A DataChange
- * MonitoredItem defines a node attribute (usually the value attribute) that is
- * monitored for changes. The server internally reads the value in the defined
- * interval and generates the appropriate notifications. The three ways of
- * updating node values discussed above are all usable in combination with
- * notifications. That is because notifications use the standard *Read* service
- * to look for value changes. */
|