123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- #include <open62541/plugin/log_stdout.h>
- #include <open62541/server.h>
- #include <open62541/server_config_default.h>
- #include <signal.h>
- static void
- manuallyDefinePump(UA_Server *server) {
- UA_NodeId pumpId;
- UA_ObjectAttributes oAttr = UA_ObjectAttributes_default;
- oAttr.displayName = UA_LOCALIZEDTEXT("en-US", "Pump (Manual)");
- UA_Server_addObjectNode(server, UA_NODEID_NULL,
- UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
- UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
- UA_QUALIFIEDNAME(1, "Pump (Manual)"), UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
- oAttr, NULL, &pumpId);
- UA_VariableAttributes mnAttr = UA_VariableAttributes_default;
- UA_String manufacturerName = UA_STRING("Pump King Ltd.");
- UA_Variant_setScalar(&mnAttr.value, &manufacturerName, &UA_TYPES[UA_TYPES_STRING]);
- mnAttr.displayName = UA_LOCALIZEDTEXT("en-US", "ManufacturerName");
- UA_Server_addVariableNode(server, UA_NODEID_NULL, pumpId,
- UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
- UA_QUALIFIEDNAME(1, "ManufacturerName"),
- UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), mnAttr, NULL, NULL);
- UA_VariableAttributes modelAttr = UA_VariableAttributes_default;
- UA_String modelName = UA_STRING("Mega Pump 3000");
- UA_Variant_setScalar(&modelAttr.value, &modelName, &UA_TYPES[UA_TYPES_STRING]);
- modelAttr.displayName = UA_LOCALIZEDTEXT("en-US", "ModelName");
- UA_Server_addVariableNode(server, UA_NODEID_NULL, pumpId,
- UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
- UA_QUALIFIEDNAME(1, "ModelName"),
- UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), modelAttr, NULL, NULL);
- UA_VariableAttributes statusAttr = UA_VariableAttributes_default;
- UA_Boolean status = true;
- UA_Variant_setScalar(&statusAttr.value, &status, &UA_TYPES[UA_TYPES_BOOLEAN]);
- statusAttr.displayName = UA_LOCALIZEDTEXT("en-US", "Status");
- UA_Server_addVariableNode(server, UA_NODEID_NULL, pumpId,
- UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
- UA_QUALIFIEDNAME(1, "Status"),
- UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), statusAttr, NULL, NULL);
- UA_VariableAttributes rpmAttr = UA_VariableAttributes_default;
- UA_Double rpm = 50.0;
- UA_Variant_setScalar(&rpmAttr.value, &rpm, &UA_TYPES[UA_TYPES_DOUBLE]);
- rpmAttr.displayName = UA_LOCALIZEDTEXT("en-US", "MotorRPM");
- UA_Server_addVariableNode(server, UA_NODEID_NULL, pumpId,
- UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
- UA_QUALIFIEDNAME(1, "MotorRPMs"),
- UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), rpmAttr, NULL, NULL);
- }
- UA_NodeId pumpTypeId = {1, UA_NODEIDTYPE_NUMERIC, {1001}};
- static void
- defineObjectTypes(UA_Server *server) {
-
- UA_NodeId deviceTypeId;
- UA_ObjectTypeAttributes dtAttr = UA_ObjectTypeAttributes_default;
- dtAttr.displayName = UA_LOCALIZEDTEXT("en-US", "DeviceType");
- UA_Server_addObjectTypeNode(server, UA_NODEID_NULL,
- UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
- UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
- UA_QUALIFIEDNAME(1, "DeviceType"), dtAttr,
- NULL, &deviceTypeId);
- UA_VariableAttributes mnAttr = UA_VariableAttributes_default;
- mnAttr.displayName = UA_LOCALIZEDTEXT("en-US", "ManufacturerName");
- UA_NodeId manufacturerNameId;
- UA_Server_addVariableNode(server, UA_NODEID_NULL, deviceTypeId,
- UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
- UA_QUALIFIEDNAME(1, "ManufacturerName"),
- UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), mnAttr, NULL, &manufacturerNameId);
-
- UA_Server_addReference(server, manufacturerNameId,
- UA_NODEID_NUMERIC(0, UA_NS0ID_HASMODELLINGRULE),
- UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_MODELLINGRULE_MANDATORY), true);
- UA_VariableAttributes modelAttr = UA_VariableAttributes_default;
- modelAttr.displayName = UA_LOCALIZEDTEXT("en-US", "ModelName");
- UA_Server_addVariableNode(server, UA_NODEID_NULL, deviceTypeId,
- UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
- UA_QUALIFIEDNAME(1, "ModelName"),
- UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), modelAttr, NULL, NULL);
-
- UA_ObjectTypeAttributes ptAttr = UA_ObjectTypeAttributes_default;
- ptAttr.displayName = UA_LOCALIZEDTEXT("en-US", "PumpType");
- UA_Server_addObjectTypeNode(server, pumpTypeId,
- deviceTypeId, UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
- UA_QUALIFIEDNAME(1, "PumpType"), ptAttr,
- NULL, NULL);
- UA_VariableAttributes statusAttr = UA_VariableAttributes_default;
- statusAttr.displayName = UA_LOCALIZEDTEXT("en-US", "Status");
- statusAttr.valueRank = UA_VALUERANK_SCALAR;
- UA_NodeId statusId;
- UA_Server_addVariableNode(server, UA_NODEID_NULL, pumpTypeId,
- UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
- UA_QUALIFIEDNAME(1, "Status"),
- UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), statusAttr, NULL, &statusId);
-
- UA_Server_addReference(server, statusId,
- UA_NODEID_NUMERIC(0, UA_NS0ID_HASMODELLINGRULE),
- UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_MODELLINGRULE_MANDATORY), true);
- UA_VariableAttributes rpmAttr = UA_VariableAttributes_default;
- rpmAttr.displayName = UA_LOCALIZEDTEXT("en-US", "MotorRPM");
- rpmAttr.valueRank = UA_VALUERANK_SCALAR;
- UA_Server_addVariableNode(server, UA_NODEID_NULL, pumpTypeId,
- UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
- UA_QUALIFIEDNAME(1, "MotorRPMs"),
- UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), rpmAttr, NULL, NULL);
- }
- static void
- addPumpObjectInstance(UA_Server *server, char *name) {
- UA_ObjectAttributes oAttr = UA_ObjectAttributes_default;
- oAttr.displayName = UA_LOCALIZEDTEXT("en-US", name);
- UA_Server_addObjectNode(server, UA_NODEID_NULL,
- UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
- UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
- UA_QUALIFIEDNAME(1, name),
- pumpTypeId,
- oAttr, NULL, NULL);
- }
- static UA_StatusCode
- pumpTypeConstructor(UA_Server *server,
- const UA_NodeId *sessionId, void *sessionContext,
- const UA_NodeId *typeId, void *typeContext,
- const UA_NodeId *nodeId, void **nodeContext) {
- UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "New pump created");
-
- UA_RelativePathElement rpe;
- UA_RelativePathElement_init(&rpe);
- rpe.referenceTypeId = UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT);
- rpe.isInverse = false;
- rpe.includeSubtypes = false;
- rpe.targetName = UA_QUALIFIEDNAME(1, "Status");
- UA_BrowsePath bp;
- UA_BrowsePath_init(&bp);
- bp.startingNode = *nodeId;
- bp.relativePath.elementsSize = 1;
- bp.relativePath.elements = &rpe;
- UA_BrowsePathResult bpr =
- UA_Server_translateBrowsePathToNodeIds(server, &bp);
- if(bpr.statusCode != UA_STATUSCODE_GOOD ||
- bpr.targetsSize < 1)
- return bpr.statusCode;
-
- UA_Boolean status = true;
- UA_Variant value;
- UA_Variant_setScalar(&value, &status, &UA_TYPES[UA_TYPES_BOOLEAN]);
- UA_Server_writeValue(server, bpr.targets[0].targetId.nodeId, value);
- UA_BrowsePathResult_clear(&bpr);
-
- return UA_STATUSCODE_GOOD;
- }
- static void
- addPumpTypeConstructor(UA_Server *server) {
- UA_NodeTypeLifecycle lifecycle;
- lifecycle.constructor = pumpTypeConstructor;
- lifecycle.destructor = NULL;
- UA_Server_setNodeTypeLifecycle(server, pumpTypeId, lifecycle);
- }
- static volatile 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_Server *server = UA_Server_new();
- UA_ServerConfig_setDefault(UA_Server_getConfig(server));
- manuallyDefinePump(server);
- defineObjectTypes(server);
- addPumpObjectInstance(server, "pump2");
- addPumpObjectInstance(server, "pump3");
- addPumpTypeConstructor(server);
- addPumpObjectInstance(server, "pump4");
- addPumpObjectInstance(server, "pump5");
- UA_StatusCode retval = UA_Server_run(server, &running);
- UA_Server_delete(server);
- return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
- }
|