OpcUaCanOpen
CANopen OPC-UA server
UaControlNode.h
Go to the documentation of this file.
1 #ifndef __UACONTROLNODE_H__
2 #define __UACONTROLNODE_H__
3 
4 #include "uaobjecttypes.h"
5 #include "nodemanagerbase.h"
6 #include "CANOpenServerConfig.h"
8 #include "opcua_propertytype.h"
9 #include "UaDeviceMethod.h"
10 #include "UaControlDeviceItem.h"
11 
12 //#include "boost/shared_ptr.hpp"
13 
14 namespace AddressSpace
15 {
28 
29  {
30  // UA_DISABLE_COPY(UaControlNode);
31 
32  public:
34  virtual ~UaControlNode(void) {};
35 
44  template<class T>
45  static UaStatus UaControlNodeCreateByType(T *cur, UaNode* instance,
46  NmBuildingAutomation* pNodeManager,
48  pUserDeviceStruct* interf,
49  UaMutexRefCounted* m)
50  {
51  UaControlDeviceGeneric* pDeclarationObject = NULL;
52  OpcUa::BaseMethod* pMethodDeclaration = NULL;
53  OpcUa::BaseDataVariableType* pDataVariable = NULL;
54  OpcUa::PropertyType *pProperty;
55  UaStatus addStatus;
56  UaNodeId nTypeId; // ID of node type
57  UaNodeId uaId; //new id
58  UaVariant defaultValue;
59  UaNode *ptNode;
60  UaString newName;
61  defaultValue.clear();
62 
63 
64  /**************************************************************
65  * Create the Device components
66  **************************************************************/
67 
68  UaReferenceLists *uaList = instance->getUaReferenceLists(); // take the list children
69  for (UaReference *pRef = (UaReference *)uaList->pTargetNodes(); pRef != 0; pRef = pRef->pNextForwardReference()) {
70  OpcUa_UInt32 refType = pRef->referenceTypeId().identifierNumeric();
71 
72  ptNode = pRef->pTargetNode(); // take child node
73  // nTypeId = ptNode->nodeId();
74  newName = ptNode->browseName().toString();
75  uaId = pNodeManager->getNewNodeId(cur, newName);
76  // cout << "New Name " << newName.toUtf8() << " new key " << uaId.toString().toUtf8() << endl;
77  // printf("Node Class %d %s %d %s\n", ptNode->nodeClass(), ptNode->browseName().toString().toUtf8(),refType, cur->displayName(0).toString().toUtf8());
78  switch (refType) {
79  case OpcUaId_HasComponent:
80  switch (ptNode->nodeClass()) {
81  case OpcUa_NodeClass_VariableType:
82  pDataVariable = new UaControlDeviceItem(newName, uaId, pNodeManager, defaultValue, static_cast<UaVariableType*>(ptNode), conf, interf, m);
83  addStatus = pNodeManager->addNodeAndReference(cur, pDataVariable, OpcUaId_HasComponent);
84  UA_ASSERT(addStatus.isGood());
85  break;
86  case OpcUa_NodeClass_Variable:
87  pDataVariable = new UaControlDeviceItem(cur, pNodeManager, static_cast<UaVariable*>(ptNode), conf, interf, m);
88  addStatus = pNodeManager->addNodeAndReference(cur, pDataVariable, OpcUaId_HasComponent);
89  UA_ASSERT(addStatus.isGood());
90  break;
91  case OpcUa_NodeClass_ObjectType:
92  pDeclarationObject = new UaControlDeviceGeneric(newName, uaId, pNodeManager, static_cast<UaObjectType *>(ptNode), conf, interf);
93  addStatus = pNodeManager->addNodeAndReference(cur, pDeclarationObject, OpcUaId_HasComponent);
94  UA_ASSERT(addStatus.isGood());
95  break;
96  case OpcUa_NodeClass_Object:
97  pDeclarationObject = new UaControlDeviceGeneric(newName, uaId, pNodeManager, static_cast<UaObject *>(ptNode), conf, interf);
98  addStatus = pNodeManager->addNodeAndReference(cur, pDeclarationObject, OpcUaId_HasComponent);
99  UA_ASSERT(addStatus.isGood());
100  break;
101  case OpcUa_NodeClass_Method:
102  pMethodDeclaration = new UaDeviceMethod<T>(cur, pNodeManager, static_cast<UaMethod *>(ptNode), m);
103  addStatus = pNodeManager->addNodeAndReference(cur, pMethodDeclaration, OpcUaId_HasComponent);
104  UA_ASSERT(addStatus.isGood());
105  cur->addMethod(pMethodDeclaration);
106  break;
107  }
108 
109  break;
110  case OpcUaId_HasSubtype:
111  addStatus = UaControlNodeCreateByType(cur, ptNode, pNodeManager, conf, interf, m);
112  UA_ASSERT(addStatus.isGood());
113  break;
114  case OpcUaId_HasProperty:
115  {
116  UaDataValue udt;
117  UaDateTime sdt = UaDateTime::now();
118  pUserDeviceStruct* dev = static_cast<pUserDeviceStruct*>(cur->getUserData());
119  defaultValue = static_cast<InterfaceUserDataBase*>(dev->getDevice())->getPropertyValue(ptNode->nodeId().identifierNumeric(), conf);
120  udt.setDataValue(defaultValue, OpcUa_False, OpcUa_Good, sdt, sdt);
121 
122  pProperty = new OpcUa::PropertyType(uaId, newName, pNodeManager->getNameSpaceIndex(), defaultValue, Ua_AccessLevel_CurrentRead, pNodeManager, m);
123  pProperty->setValue(0, udt, OpcUa_False);
124  addStatus = pNodeManager->addNodeAndReference(cur, pProperty, OpcUaId_HasProperty);
125  if (!addStatus.isGood())
126  {
127  UA_ASSERT(addStatus.isGood());
128  return addStatus;
129  }
130  }
131  break;
132  }
133  }
134  return addStatus;
135  }
136  };
137 
138 }
139 #endif