123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- #include <open62541/plugin/log_stdout.h>
- #include <open62541/server.h>
- #include <open62541/server_config_default.h>
- #include <signal.h>
- #include <stdlib.h>
- UA_Boolean running = true;
- static void stopHandler(int sig) {
- running = false;
- }
- static void createMammals(UA_Server *server) {
- UA_ObjectTypeAttributes otAttr = UA_ObjectTypeAttributes_default;
- otAttr.description = UA_LOCALIZEDTEXT("en-US", "A mammal");
- otAttr.displayName = UA_LOCALIZEDTEXT("en-US", "MammalType");
- UA_Server_addObjectTypeNode(server, UA_NODEID_NUMERIC(1, 10000),
- UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
- UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
- UA_QUALIFIEDNAME(1, "MammalType"), otAttr, NULL, NULL);
- UA_VariableAttributes vAttr = UA_VariableAttributes_default;
- vAttr.description = UA_LOCALIZEDTEXT("en-US", "This mammals class");
- vAttr.displayName = UA_LOCALIZEDTEXT("en-US", "Class");
- UA_String classVar = UA_STRING("mamalia");
- UA_Variant_setScalar(&vAttr.value, &classVar, &UA_TYPES[UA_TYPES_STRING]);
- UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, 10001),
- UA_NODEID_NUMERIC(1, 10000),
- UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
- UA_QUALIFIEDNAME(1, "Class"), UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
- vAttr, NULL, NULL);
- vAttr = UA_VariableAttributes_default;
- vAttr.description = UA_LOCALIZEDTEXT("en-US", "This mammals species");
- vAttr.displayName = UA_LOCALIZEDTEXT("en-US", "Species");
- UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, 10002),
- UA_NODEID_NUMERIC(1, 10000),
- UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
- UA_QUALIFIEDNAME(1, "Species"), UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
- vAttr, NULL, NULL);
- otAttr = UA_ObjectTypeAttributes_default;
- otAttr.description = UA_LOCALIZEDTEXT("en-US", "A dog, subtype of mammal");
- otAttr.displayName = UA_LOCALIZEDTEXT("en-US", "DogType");
- UA_Server_addObjectTypeNode(server, UA_NODEID_NUMERIC(1, 20000),
- UA_NODEID_NUMERIC(1, 10000),
- UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
- UA_QUALIFIEDNAME(1, "DogType"), otAttr, NULL, NULL);
- vAttr = UA_VariableAttributes_default;
- vAttr.description = UA_LOCALIZEDTEXT("en-US", "This dogs species");
- vAttr.displayName = UA_LOCALIZEDTEXT("en-US", "Species");
- UA_String defaultSpecies = UA_STRING("Canis");
- UA_Variant_setScalar(&vAttr.value, &defaultSpecies, &UA_TYPES[UA_TYPES_STRING]);
- UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, 20001),
- UA_NODEID_NUMERIC(1, 20000),
- UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
- UA_QUALIFIEDNAME(1, "Species"), UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
- vAttr, NULL, NULL);
- vAttr = UA_VariableAttributes_default;
- vAttr.description = UA_LOCALIZEDTEXT("en-US", "This dogs name");
- vAttr.displayName = UA_LOCALIZEDTEXT("en-US", "Name");
- UA_String defaultName = UA_STRING("unnamed dog");
- UA_Variant_setScalar(&vAttr.value, &defaultName, &UA_TYPES[UA_TYPES_STRING]);
- UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, 20002),
- UA_NODEID_NUMERIC(1, 20000),
- UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
- UA_QUALIFIEDNAME(1, "Name"), UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
- vAttr, NULL, NULL);
-
- UA_ObjectAttributes oAttr = UA_ObjectAttributes_default;
- oAttr.description = UA_LOCALIZEDTEXT("en-US", "A dog named Bello");
- oAttr.displayName = UA_LOCALIZEDTEXT("en-US", "Bello");
- UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, 0),
- UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
- UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
- UA_QUALIFIEDNAME(1, "Bello"), UA_NODEID_NUMERIC(1, 20000),
- oAttr, NULL, NULL);
- oAttr = UA_ObjectAttributes_default;
- oAttr.description = UA_LOCALIZEDTEXT("en-US", "Another dog");
- oAttr.displayName = UA_LOCALIZEDTEXT("en-US", "Dog2");
- UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, 0),
- UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
- UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
- UA_QUALIFIEDNAME(1, "Dog2"), UA_NODEID_NUMERIC(1, 20000),
- oAttr, NULL, NULL);
- oAttr = UA_ObjectAttributes_default;
- oAttr.description = UA_LOCALIZEDTEXT("en-US", "A mmamal");
- oAttr.displayName = UA_LOCALIZEDTEXT("en-US", "Mmamal1");
- UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, 0),
- UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
- UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
- UA_QUALIFIEDNAME(1, "Mammal1"), UA_NODEID_NUMERIC(1, 10000),
- oAttr, NULL, NULL);
- }
- static void createCustomInheritance(UA_Server *server) {
-
- UA_ObjectTypeAttributes otAttr = UA_ObjectTypeAttributes_default;
- otAttr.description = UA_LOCALIZEDTEXT("en-US", "A Lamp");
- otAttr.displayName = UA_LOCALIZEDTEXT("en-US", "LampType");
- UA_Server_addObjectTypeNode(server, UA_NODEID_NUMERIC(1, 30000),
- UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
- UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
- UA_QUALIFIEDNAME(1, "LampType"), otAttr, NULL, NULL);
-
- UA_VariableAttributes vAttr = UA_VariableAttributes_default;
- vAttr.description = UA_LOCALIZEDTEXT("en-US", "Switch lamp on/off");
- vAttr.displayName = UA_LOCALIZEDTEXT("en-US", "IsOn");
- UA_Boolean isOn = UA_FALSE;
- UA_Variant_setScalar(&vAttr.value, &isOn, &UA_TYPES[UA_TYPES_BOOLEAN]);
- UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, 30001),
- UA_NODEID_NUMERIC(1, 30000),
- UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
- UA_QUALIFIEDNAME(1, "IsOn"), UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
- vAttr, NULL, NULL);
- UA_Server_addReference(server, UA_NODEID_NUMERIC(1, 30001),
- UA_NODEID_NUMERIC(0, UA_NS0ID_HASMODELLINGRULE),
- UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_MODELLINGRULE_MANDATORY), true);
- vAttr = UA_VariableAttributes_default;
- vAttr.description = UA_LOCALIZEDTEXT("en-US", "Lamp brightness");
- vAttr.displayName = UA_LOCALIZEDTEXT("en-US", "Brightness");
- UA_UInt16 brightness = 142;
- UA_Variant_setScalar(&vAttr.value, &brightness, &UA_TYPES[UA_TYPES_UINT16]);
- UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, 30002),
- UA_NODEID_NUMERIC(1, 30000),
- UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
- UA_QUALIFIEDNAME(1, "Brightness"), UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
- vAttr, NULL, NULL);
- UA_Server_addReference(server, UA_NODEID_NUMERIC(1, 30002),
- UA_NODEID_NUMERIC(0, UA_NS0ID_HASMODELLINGRULE),
- UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_MODELLINGRULE_MANDATORY), true);
-
-
- UA_ObjectAttributes oAttr = UA_ObjectAttributes_default;
- oAttr.description = UA_LOCALIZEDTEXT("en-US", "A green lamp");
- oAttr.displayName = UA_LOCALIZEDTEXT("en-US", "LampGreen");
- UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, 0),
- UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
- UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
- UA_QUALIFIEDNAME(1, "LampGreen"), UA_NODEID_NUMERIC(1, 30000),
- oAttr, NULL, NULL);
-
-
- oAttr = UA_ObjectAttributes_default;
- oAttr.description = UA_LOCALIZEDTEXT("en-US", "A red lamp");
- oAttr.displayName = UA_LOCALIZEDTEXT("en-US", "LampRed");
- UA_Server_addNode_begin(server, UA_NODECLASS_OBJECT,
- UA_NODEID_NUMERIC(1, 30100),
- UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
- UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
- UA_QUALIFIEDNAME(1, "LampRed"),
- UA_NODEID_NUMERIC(1, 30000),
- (const UA_NodeAttributes*)&oAttr, &UA_TYPES[UA_TYPES_OBJECTATTRIBUTES],
- NULL, NULL);
-
- vAttr = UA_VariableAttributes_default;
- vAttr.description = UA_LOCALIZEDTEXT("en-US", "Switch lamp on/off");
- vAttr.displayName = UA_LOCALIZEDTEXT("en-US", "IsOn");
- isOn = UA_FALSE;
- UA_Variant_setScalar(&vAttr.value, &isOn, &UA_TYPES[UA_TYPES_BOOLEAN]);
- UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, 30101),
- UA_NODEID_NUMERIC(1, 30100),
- UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
- UA_QUALIFIEDNAME(1, "IsOn"), UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
- vAttr, NULL, NULL);
-
- UA_Server_addNode_finish(server, UA_NODEID_NUMERIC(1, 30100));
- }
- int main(void) {
- signal(SIGINT, stopHandler);
- signal(SIGTERM, stopHandler);
- UA_Server *server = UA_Server_new();
- UA_ServerConfig_setDefault(UA_Server_getConfig(server));
- createMammals(server);
- createCustomInheritance(server);
-
- UA_StatusCode retval = UA_Server_run(server, &running);
- UA_Server_delete(server);
- return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
- }
|