123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- #include <open62541/plugin/log_stdout.h>
- #include <open62541/server.h>
- #include <open62541/server_config_default.h>
- #include <signal.h>
- #include <stdlib.h>
- static UA_NodeId eventType;
- static UA_StatusCode
- addNewEventType(UA_Server *server) {
- UA_ObjectTypeAttributes attr = UA_ObjectTypeAttributes_default;
- attr.displayName = UA_LOCALIZEDTEXT("en-US", "SimpleEventType");
- attr.description = UA_LOCALIZEDTEXT("en-US", "The simple event type we created");
- return UA_Server_addObjectTypeNode(server, UA_NODEID_NULL,
- UA_NODEID_NUMERIC(0, UA_NS0ID_BASEEVENTTYPE),
- UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
- UA_QUALIFIEDNAME(0, "SimpleEventType"),
- attr, NULL, &eventType);
- }
- static UA_StatusCode
- setUpEvent(UA_Server *server, UA_NodeId *outId) {
- UA_StatusCode retval = UA_Server_createEvent(server, eventType, outId);
- if (retval != UA_STATUSCODE_GOOD) {
- UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
- "createEvent failed. StatusCode %s", UA_StatusCode_name(retval));
- return retval;
- }
-
-
- UA_DateTime eventTime = UA_DateTime_now();
- UA_Server_writeObjectProperty_scalar(server, *outId, UA_QUALIFIEDNAME(0, "Time"),
- &eventTime, &UA_TYPES[UA_TYPES_DATETIME]);
- UA_UInt16 eventSeverity = 100;
- UA_Server_writeObjectProperty_scalar(server, *outId, UA_QUALIFIEDNAME(0, "Severity"),
- &eventSeverity, &UA_TYPES[UA_TYPES_UINT16]);
- UA_LocalizedText eventMessage = UA_LOCALIZEDTEXT("en-US", "An event has been generated.");
- UA_Server_writeObjectProperty_scalar(server, *outId, UA_QUALIFIEDNAME(0, "Message"),
- &eventMessage, &UA_TYPES[UA_TYPES_LOCALIZEDTEXT]);
- UA_String eventSourceName = UA_STRING("Server");
- UA_Server_writeObjectProperty_scalar(server, *outId, UA_QUALIFIEDNAME(0, "SourceName"),
- &eventSourceName, &UA_TYPES[UA_TYPES_STRING]);
- return UA_STATUSCODE_GOOD;
- }
- static UA_StatusCode
- generateEventMethodCallback(UA_Server *server,
- const UA_NodeId *sessionId, void *sessionHandle,
- const UA_NodeId *methodId, void *methodContext,
- const UA_NodeId *objectId, void *objectContext,
- size_t inputSize, const UA_Variant *input,
- size_t outputSize, UA_Variant *output) {
- UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Creating event");
-
- UA_NodeId eventNodeId;
- UA_StatusCode retval = setUpEvent(server, &eventNodeId);
- if(retval != UA_STATUSCODE_GOOD) {
- UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
- "Creating event failed. StatusCode %s", UA_StatusCode_name(retval));
- return retval;
- }
- retval = UA_Server_triggerEvent(server, eventNodeId,
- UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER),
- NULL, UA_TRUE);
- if(retval != UA_STATUSCODE_GOOD)
- UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
- "Triggering event failed. StatusCode %s", UA_StatusCode_name(retval));
- return retval;
- }
- static void
- addGenerateEventMethod(UA_Server *server) {
- UA_MethodAttributes generateAttr = UA_MethodAttributes_default;
- generateAttr.description = UA_LOCALIZEDTEXT("en-US","Generate an event.");
- generateAttr.displayName = UA_LOCALIZEDTEXT("en-US","Generate Event");
- generateAttr.executable = true;
- generateAttr.userExecutable = true;
- UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1, 62541),
- UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
- UA_NODEID_NUMERIC(0, UA_NS0ID_HASORDEREDCOMPONENT),
- UA_QUALIFIEDNAME(1, "Generate Event"),
- generateAttr, &generateEventMethodCallback,
- 0, NULL, 0, NULL, NULL, NULL);
- }
- static volatile UA_Boolean running = true;
- static void stopHandler(int sig) {
- running = false;
- }
- int main (void) {
-
- signal(SIGINT, stopHandler);
- signal(SIGTERM, stopHandler);
- UA_Server *server = UA_Server_new();
- UA_ServerConfig_setDefault(UA_Server_getConfig(server));
- addNewEventType(server);
- addGenerateEventMethod(server);
- UA_StatusCode retval = UA_Server_run(server, &running);
- UA_Server_delete(server);
- return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
- }
|