deviceObjectType.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #ifndef OPEN62541_MT_COMMON
  2. #define OPEN62541_MT_COMMON
  3. #include "mt_testing.h"
  4. UA_NodeId pumpTypeId = {1, UA_NODEIDTYPE_NUMERIC, {1001}};
  5. static void
  6. defineObjectTypes(void) {
  7. UA_NodeId deviceTypeId;
  8. UA_ObjectTypeAttributes dtAttr = UA_ObjectTypeAttributes_default;
  9. dtAttr.displayName = UA_LOCALIZEDTEXT("en-US", "DeviceType");
  10. UA_Server_addObjectTypeNode(tc.server, UA_NODEID_NULL,
  11. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  12. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  13. UA_QUALIFIEDNAME(1, "DeviceType"), dtAttr,
  14. NULL, &deviceTypeId);
  15. UA_VariableAttributes mnAttr = UA_VariableAttributes_default;
  16. mnAttr.displayName = UA_LOCALIZEDTEXT("en-US", "ManufacturerName");
  17. UA_NodeId manufacturerNameId;
  18. UA_Server_addVariableNode(tc.server, UA_NODEID_NULL, deviceTypeId,
  19. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  20. UA_QUALIFIEDNAME(1, "ManufacturerName"),
  21. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), mnAttr, NULL, &manufacturerNameId);
  22. UA_Server_addReference(tc.server, manufacturerNameId,
  23. UA_NODEID_NUMERIC(0, UA_NS0ID_HASMODELLINGRULE),
  24. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_MODELLINGRULE_MANDATORY), true);
  25. UA_VariableAttributes modelAttr = UA_VariableAttributes_default;
  26. modelAttr.displayName = UA_LOCALIZEDTEXT("en-US", "ModelName");
  27. UA_Server_addVariableNode(tc.server, UA_NODEID_NULL, deviceTypeId,
  28. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  29. UA_QUALIFIEDNAME(1, "ModelName"),
  30. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), modelAttr, NULL, NULL);
  31. UA_ObjectTypeAttributes ptAttr = UA_ObjectTypeAttributes_default;
  32. ptAttr.displayName = UA_LOCALIZEDTEXT("en-US", "PumpType");
  33. UA_Server_addObjectTypeNode(tc.server, pumpTypeId,
  34. deviceTypeId, UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  35. UA_QUALIFIEDNAME(1, "PumpType"), ptAttr,
  36. NULL, NULL);
  37. UA_VariableAttributes statusAttr = UA_VariableAttributes_default;
  38. statusAttr.displayName = UA_LOCALIZEDTEXT("en-US", "Status");
  39. statusAttr.valueRank = UA_VALUERANK_SCALAR;
  40. UA_NodeId statusId;
  41. UA_Server_addVariableNode(tc.server, UA_NODEID_NULL, pumpTypeId,
  42. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  43. UA_QUALIFIEDNAME(1, "Status"),
  44. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), statusAttr, NULL, &statusId);
  45. UA_Server_addReference(tc.server, statusId,
  46. UA_NODEID_NUMERIC(0, UA_NS0ID_HASMODELLINGRULE),
  47. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_MODELLINGRULE_MANDATORY), true);
  48. UA_VariableAttributes rpmAttr = UA_VariableAttributes_default;
  49. rpmAttr.displayName = UA_LOCALIZEDTEXT("en-US", "MotorRPM");
  50. rpmAttr.valueRank = UA_VALUERANK_SCALAR;
  51. UA_Server_addVariableNode(tc.server, UA_NODEID_NULL, pumpTypeId,
  52. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  53. UA_QUALIFIEDNAME(1, "MotorRPMs"),
  54. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), rpmAttr, NULL, NULL);
  55. }
  56. static UA_StatusCode
  57. pumpTypeConstructor(UA_Server *servertmp,
  58. const UA_NodeId *sessionId, void *sessionContext,
  59. const UA_NodeId *typeId, void *typeContext,
  60. const UA_NodeId *nodeId, void **nodeContext) {
  61. UA_RelativePathElement rpe;
  62. UA_RelativePathElement_init(&rpe);
  63. rpe.referenceTypeId = UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT);
  64. rpe.isInverse = false;
  65. rpe.includeSubtypes = false;
  66. rpe.targetName = UA_QUALIFIEDNAME(1, "Status");
  67. UA_BrowsePath bp;
  68. UA_BrowsePath_init(&bp);
  69. bp.startingNode = *nodeId;
  70. bp.relativePath.elementsSize = 1;
  71. bp.relativePath.elements = &rpe;
  72. UA_BrowsePathResult bpr =
  73. UA_Server_translateBrowsePathToNodeIds(tc.server, &bp);
  74. if(bpr.statusCode != UA_STATUSCODE_GOOD ||
  75. bpr.targetsSize < 1)
  76. return bpr.statusCode;
  77. /* Set the status value */
  78. UA_Boolean status = true;
  79. UA_Variant value;
  80. UA_Variant_setScalar(&value, &status, &UA_TYPES[UA_TYPES_BOOLEAN]);
  81. UA_Server_writeValue(tc.server, bpr.targets[0].targetId.nodeId, value);
  82. UA_BrowsePathResult_clear(&bpr);
  83. return UA_STATUSCODE_GOOD;
  84. }
  85. static void
  86. addPumpTypeConstructor(UA_Server *servertmp) {
  87. UA_NodeTypeLifecycle lifecycle;
  88. lifecycle.constructor = pumpTypeConstructor;
  89. lifecycle.destructor = NULL;
  90. UA_Server_setNodeTypeLifecycle(tc.server, pumpTypeId, lifecycle);
  91. }
  92. #endif //OPEN62541_MT_COMMON