123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- /* 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 <signal.h>
- #include <stdio.h>
- #include "open62541.h"
- static UA_Boolean running = true;
- static void stopHandler(int sig) {
- UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "received ctrl-c");
- running = false;
- }
- #ifdef UA_ENABLE_SUBSCRIPTIONS
- static void
- handler_events(const UA_UInt32 monId, const size_t nEventFields,
- const UA_Variant *eventFields, void *context) {
- UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Notification");
- /* The context should point to the monId on the stack */
- UA_assert(*(UA_UInt32*)context == monId);
- for(size_t i = 0; i < nEventFields; ++i) {
- if (UA_Variant_hasScalarType(&eventFields[i], &UA_TYPES[UA_TYPES_UINT16])) {
- UA_UInt16 severity = *(UA_UInt16 *)eventFields[i].data;
- UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Severity: %u", severity);
- } else if (UA_Variant_hasScalarType(&eventFields[i], &UA_TYPES[UA_TYPES_LOCALIZEDTEXT])) {
- UA_LocalizedText *lt = (UA_LocalizedText *)eventFields[i].data;
- UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
- "Message: '%.*s'", (int)lt->text.length, lt->text.data);
- }
- else {
- UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
- "Don't know how to handle type: '%s'", eventFields[i].type->typeName);
- }
- }
- }
- const size_t nSelectClauses = 2;
- static UA_SimpleAttributeOperand *
- setupSelectClauses(void) {
- UA_SimpleAttributeOperand *selectClauses = (UA_SimpleAttributeOperand*)
- UA_Array_new(nSelectClauses, &UA_TYPES[UA_TYPES_SIMPLEATTRIBUTEOPERAND]);
- if(!selectClauses)
- return NULL;
- for(size_t i =0; i<nSelectClauses; ++i) {
- UA_SimpleAttributeOperand_init(&selectClauses[i]);
- }
- selectClauses[0].typeDefinitionId = UA_NODEID_NUMERIC(0, UA_NS0ID_BASEEVENTTYPE);
- selectClauses[0].browsePathSize = 1;
- selectClauses[0].browsePath = (UA_QualifiedName*)
- UA_Array_new(selectClauses[0].browsePathSize, &UA_TYPES[UA_TYPES_QUALIFIEDNAME]);
- if(!selectClauses[0].browsePath) {
- UA_SimpleAttributeOperand_delete(selectClauses);
- return NULL;
- }
- selectClauses[0].attributeId = UA_ATTRIBUTEID_VALUE;
- selectClauses[0].browsePath[0] = UA_QUALIFIEDNAME_ALLOC(0, "Message");
- selectClauses[1].typeDefinitionId = UA_NODEID_NUMERIC(0, UA_NS0ID_BASEEVENTTYPE);
- selectClauses[1].browsePathSize = 1;
- selectClauses[1].browsePath = (UA_QualifiedName*)
- UA_Array_new(selectClauses[1].browsePathSize, &UA_TYPES[UA_TYPES_QUALIFIEDNAME]);
- if(!selectClauses[1].browsePath) {
- UA_SimpleAttributeOperand_deleteMembers(selectClauses);
- UA_SimpleAttributeOperand_delete(selectClauses);
- return NULL;
- }
- selectClauses[1].attributeId = UA_ATTRIBUTEID_VALUE;
- selectClauses[1].browsePath[0] = UA_QUALIFIEDNAME_ALLOC(0, "Severity");
- return selectClauses;
- }
- #endif
- int main(int argc, char *argv[]) {
- signal(SIGINT, stopHandler);
- signal(SIGTERM, stopHandler);
- if(argc < 2) {
- printf("Usage: tutorial_client_events <opc.tcp://server-url>\n");
- return 1;
- }
- UA_Client *client = UA_Client_new(UA_ClientConfig_default);
- /* opc.tcp://uademo.prosysopc.com:53530/OPCUA/SimulationServer */
- UA_StatusCode retval = UA_Client_connect(client, argv[1]);
- if(retval != UA_STATUSCODE_GOOD) {
- UA_Client_delete(client);
- return (int)retval;
- }
- #ifdef UA_ENABLE_SUBSCRIPTIONS
- /* Create a subscription */
- UA_UInt32 subId = 0;
- retval = UA_Client_Subscriptions_new(client, UA_SubscriptionSettings_default, &subId);
- if(!subId) {
- UA_Client_disconnect(client);
- UA_Client_delete(client);
- return (int)retval;
- }
- UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Create subscription succeeded, id %u", subId);
- /* Add a MonitoredItem */
- UA_NodeId monitorThis = UA_NODEID_NUMERIC(0, 2253); // Root->Objects->Server
- UA_UInt32 monId = 0;
- UA_SimpleAttributeOperand *selectClauses = setupSelectClauses();
- retval = UA_Client_Subscriptions_addMonitoredEvent(client, subId, monitorThis,
- UA_ATTRIBUTEID_EVENTNOTIFIER,
- selectClauses, nSelectClauses,
- NULL, 0, &handler_events, &monId, &monId);
- if(retval != UA_STATUSCODE_GOOD) {
- UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
- "Could not add the MonitoredItem with %s", UA_StatusCode_name(retval));
- goto cleanup;
- } else {
- UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
- "Monitoring 'Root->Objects->Server', id %u", subId);
- }
- while (running)
- UA_Client_Subscriptions_manuallySendPublishRequest(client);
- /* Delete the subscription */
- cleanup:
- UA_Client_Subscriptions_remove(client, subId);
- UA_Array_delete(selectClauses, nSelectClauses, &UA_TYPES[UA_TYPES_SIMPLEATTRIBUTEOPERAND]);
- #endif
- UA_Client_disconnect(client);
- UA_Client_delete(client);
- return (int) retval;
- }
|