END_TEST static void handler_events_propagate(UA_Client *lclient, UA_UInt32 subId, void *subContext, UA_UInt32 monId, void *monContext, size_t nEventFields, UA_Variant *eventFields) { UA_Boolean foundSeverity = UA_FALSE; UA_Boolean foundMessage = UA_FALSE; UA_Boolean foundType = UA_FALSE; UA_Boolean foundSource = UA_FALSE; ck_assert_uint_eq(*(UA_UInt32 *) monContext, monitoredItemId); ck_assert_uint_eq(nEventFields, nSelectClauses); // check all event fields for (unsigned int i = 0; i < nEventFields; i++) { // find out which attribute of the event is being looked at if (UA_Variant_hasScalarType(&eventFields[i], &UA_TYPES[UA_TYPES_UINT16])) { // Severity ck_assert_uint_eq(*((UA_UInt16 *) (eventFields[i].data)), 1000); foundSeverity = UA_TRUE; } else if (UA_Variant_hasScalarType(&eventFields[i], &UA_TYPES[UA_TYPES_LOCALIZEDTEXT])) { // Message UA_LocalizedText comp = UA_LOCALIZEDTEXT("en-US", "Generated Event"); ck_assert(UA_String_equal(&((UA_LocalizedText *) eventFields[i].data)->locale, &comp.locale)); ck_assert(UA_String_equal(&((UA_LocalizedText *) eventFields[i].data)->text, &comp.text)); foundMessage = UA_TRUE; } else if (UA_Variant_hasScalarType(&eventFields[i], &UA_TYPES[UA_TYPES_NODEID])) { // either SourceNode or EventType UA_NodeId serverNameSpaceId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_NAMESPACES); if (UA_NodeId_equal((UA_NodeId *)eventFields[i].data, &eventType)) { // EventType foundType = UA_TRUE; } else if (UA_NodeId_equal((UA_NodeId *)eventFields[i].data, &serverNameSpaceId)) { // SourceNode foundSource = UA_TRUE; } else { ck_assert_msg(UA_FALSE, "NodeId doesn't match"); } } else { ck_assert_msg(UA_FALSE, "Field doesn't match"); } } ck_assert_uint_eq(foundMessage, UA_TRUE); ck_assert_uint_eq(foundSeverity, UA_TRUE); ck_assert_uint_eq(foundType, UA_TRUE); ck_assert_uint_eq(foundSource, UA_TRUE); notificationReceived = true; }
int main(void) { signal(SIGINT, stopHandler); /* catches ctrl-c */ UA_ClientConfig config = UA_ClientConfig_default; /* default timeout is 5 seconds. Set it to 1 second here for demo */ config.timeout = 1000; UA_Client *client = UA_Client_new(config); /* 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; /* Variants can hold scalar values and arrays of any type */ UA_Variant_init(&value); /* Endless loop reading the server time */ while (running) { /* if already connected, this will return GOOD and do nothing */ /* if the connection is closed/errored, the connection will be reset and then reconnected */ /* Alternatively you can also use UA_Client_getState to get the current state */ UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840"); if (retval != UA_STATUSCODE_GOOD) { UA_LOG_ERROR(logger, UA_LOGCATEGORY_CLIENT, "Not connected. Retrying to connect in 1 second"); /* The connect may timeout after 1 second (see above) or it may fail immediately on network errors */ /* E.g. name resolution errors or unreachable network. Thus there should be a small sleep here */ UA_sleep_ms(1000); continue; } /* NodeId of the variable holding the current time */ const UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME); retval = UA_Client_readValueAttribute(client, nodeId, &value); if (retval == UA_STATUSCODE_BADCONNECTIONCLOSED) { UA_LOG_ERROR(logger, UA_LOGCATEGORY_CLIENT, "Connection was closed. Reconnecting ..."); continue; } if (retval == UA_STATUSCODE_GOOD && UA_Variant_hasScalarType(&value, &UA_TYPES[UA_TYPES_DATETIME])) { UA_DateTime raw_date = *(UA_DateTime *) value.data; UA_String string_date = UA_DateTime_toString(raw_date); UA_LOG_INFO(logger, UA_LOGCATEGORY_CLIENT, "string date is: %.*s", (int) string_date.length, string_date.data); UA_String_deleteMembers(&string_date); } UA_sleep_ms(1000); }; /* Clean up */ UA_Variant_deleteMembers(&value); UA_Client_delete(client); /* Disconnects the client internally */ return UA_STATUSCODE_GOOD; }