OpcUaCanOpen
CANopen OPC-UA server
UaProgramItem.cpp
Go to the documentation of this file.
1 //#include "BuildingAutomationTypeIds.h"
3 #include "UaProgramItem.h"
4 #include "UaProgramMethod.h"
5 #include "CANOpenServerConfig.h"
6 #include "UaControlDeviceItem.h"
7 
8 using namespace UserDevice;
9 
10 namespace AddressSpace
11 {
18 // UaProgramItem::UaProgramItem(PROGRAM *pt, const UaNodeId& newNodeId, NmBuildingAutomation* pNodeManager, UaControlDeviceGeneric* parent,UserDataBase* puds)
19 // : UaControlDeviceGeneric(UaString(pt->name().c_str()), newNodeId, pNodeManager, BA_PROGRAM, pt, puds)
20 // : OpcUa::BaseObjectTypeGeneric(newNodeId, UaString(pt->name().c_str()), pNodeManager->getNameSpaceIndex(), pNodeManager)
21 // UaProgramItem::UaProgramItem(PROGRAM *pt, UaObject *pNodeType, const UaNodeId& newNodeId, NmBuildingAutomation *pNodeManager, UaControlDeviceGeneric* parent, UserDataBase* puds)
22 // : UaControlDeviceGeneric(newNodeId, pNodeManager, pNodeType, pt, puds)
23 
24  UaProgramItem::UaProgramItem(PROGRAM *pt, UaObjectType *pNodeType, const UaNodeId& newNodeId, NmBuildingAutomation *pNodeManager, UaControlDeviceGeneric* parent, pUserDeviceStruct * puds)
25  : UaControlDeviceGeneric(UaString(pt->name().c_str()),newNodeId, pNodeManager, pNodeType, pt, puds)
26 
27  {
28 // OpcUa_UInt16 indx = pNodeManager->m_pControlInterface-> ->getNameSpaceIndex();
29  UaStatus ret = OpcUa_Good;
30  UaProgramStartVariable *pStartProgramItem;
31 
32  UaNodeId uaId, uaMethId;
33  UaVariant defaultValue;
34 
35  defaultValue.setBool(0);
36  uaId = pNodeManager->getNewNodeId(this, "StartProgram");
37 
38  pStartProgramItem = new UaProgramStartVariable("StartProgram", uaId, pNodeManager, defaultValue, this, parent->getSharedMutex());
39 
40  pStartProgramItem->setValueHandling(UaVariable_Value_Cache);
41 
42  ret = pNodeManager->addNodeAndReference(this, pStartProgramItem, OpcUaId_HasComponent);
43 
44  UA_ASSERT(ret.isGood());
45 
46  defaultValue.setStatusCode(OpcUa_Good);
47  uaId = pNodeManager->getNewNodeId(this, "Error");
48 
49  m_pErrorItem = new UaControlVariable("Error", uaId, pNodeManager, defaultValue, parent->getSharedMutex());
50 
51  m_pErrorItem->setValueHandling(UaVariable_Value_Cache);
52 
53  ret = pNodeManager->addNodeAndReference(this, m_pErrorItem, OpcUaId_HasComponent);
54 
55  UA_ASSERT(ret.isGood());
56 
57  PROGRAM::METHOD_sequence mitem1 = pt->METHOD();
58 
59  for (PROGRAM::METHOD_iterator mitem = mitem1.begin(); mitem != mitem1.end(); mitem++)
60  {
61  OpcUa_UInt32 numericIdentifier;
62  string mname = mitem->command().get();
63  if (mname == "read")
64  numericIdentifier = BA_SDOREAD;
65  else if (mname == "write")
66  numericIdentifier = BA_SDOWRITE;
67  else {
68  UaNodeId uaMethId = pNodeManager->getNewNodeId(parent, mname.c_str());
70  ret = pNodeManager->addUaReference(this, methNode, OpcUaId_HasComponent);
71  UA_ASSERT(ret.isGood());
72  continue;
73  }
74 // UaMethod* instance = pNodeManager->getInstanceDeclarationMethod(numericIdentifier); //! get a method based on numerical id ( if not return null)
75 
76  UaString address = mitem->address().get().c_str();
77 
78  UaNodeId adrNodeId = pNodeManager->getNewNodeId(parent, address);
79  UaControlDeviceItem * adrNode = (UaControlDeviceItem*)(pNodeManager->findNode(adrNodeId));
80 
81  UaString name = mname.c_str();
82  UaNodeId uaId = pNodeManager->getNewNodeId(this, name+"_"+address);
83  UaProgramMethod<UaControlDeviceGeneric> *pUaMethod = new UaProgramMethod<UaControlDeviceGeneric>(this, uaId, name + "_" + address, pNodeManager, numericIdentifier, parent->getSharedMutex());
84 
85 // UaDeviceMethod *pUaMethod = new UaDeviceMethod(this, pNodeManager, instance, NULL);
86 
87  ret = pNodeManager->addNodeAndReference(this, pUaMethod, OpcUaId_HasComponent);
88  addMethod(pUaMethod);
89  UA_ASSERT(ret.isGood());
90 
91  ret = pNodeManager->addUaReference(pUaMethod, adrNode, pNodeManager->m_pControlInterface->getTypeNodeId(BA_HASADDRESS)); //UaNodeId(BA_HASADDRESS, indx));
92  UA_ASSERT(ret.isGood());
93 
94  UaString valueMethod = mitem->value().get().c_str();
95  UaNodeId vNodeId = pNodeManager->getNewNodeId(this, valueMethod);
96  defaultValue = *adrNode->value(0).value();
97 // UaControlVariable *ucv = new UaControlVariable(valueMethod, vNodeId, pNodeManager, UaVariant(*adrNode->value(0).value()), NULL);
98  UaControlVariable *ucv = new UaControlVariable(valueMethod, vNodeId, pNodeManager, defaultValue, NULL);
99 
100  ret = pNodeManager->addNodeAndReference(this, ucv, OpcUaId_HasComponent);
101  UA_ASSERT(ret.isGood());
102 
103  ret = pNodeManager->addUaReference(pUaMethod, ucv, pNodeManager->m_pControlInterface->getTypeNodeId(BA_HASVALUE)); //UaNodeId(BA_HASVALUE, indx));
104  UA_ASSERT(ret.isGood());
105  if (numericIdentifier == BA_SDOREAD)
106  ucv->connectItem(adrNode);
107  }
108 
109  }
110 
111 /*
112  UaProgramItem::UaProgramItem(PROGRAM *pt, UaObject *pNodeType, const UaNodeId& newNodeId, NmBuildingAutomation *pNodeManager, UaControlDeviceGeneric* parent, UserDataBase* puds)
113 
114  : UaControlDeviceGeneric(newNodeId, pNodeManager, pNodeType, pt, puds)
115 
116  {
117  UaStatus ret = OpcUa_Good;
118  PROGRAM::METHOD_sequence mitem1 = pt->METHOD();
119 
120  for (PROGRAM::METHOD_iterator mitem = mitem1.begin(); mitem != mitem1.end(); mitem++)
121  {
122  OpcUa_UInt32 numericIdentifier;
123  string mname = mitem->command().get();
124  if (mname == "read")
125  numericIdentifier = BA_SDOREAD;
126  else if (mname == "write")
127  numericIdentifier = BA_SDOWRITE;
128  else {
129  return;
130  }
131  UaMethod* instance = pNodeManager->getInstanceDeclarationMethod(numericIdentifier); //! get a method based on numerical id ( if not return null)
132 
133  UaString address = mitem->address().get().c_str();
134  UaNode * nNode = pNodeManager->getUpNode(this);
135  UaNodeId adrNodeId = pNodeManager->getNewNodeId(nNode, address);
136  UaControlDeviceItem * adrNode = (UaControlDeviceItem*)(pNodeManager->findNode(adrNodeId));
137 
138  UaString name = mname.c_str();
139  UaDeviceMethod *pUaMethod = new UaDeviceMethod(puds, pNodeManager, instance, NULL);
140 
141  ret = pNodeManager->addNodeAndReference(this, pUaMethod, OpcUaId_HasComponent);
142  UA_ASSERT(ret.isGood());
143 
144  ret = pNodeManager->addNodeAndReference(pUaMethod, adrNode, UaNodeId(Ba_ReferenceAddress, pNodeManager->getNameSpaceIndex()));
145  UA_ASSERT(ret.isGood());
146 
147  UaString valueMethod = mitem->value().get().c_str();
148  UaNodeId vNodeId = pNodeManager->getNewNodeId(nNode, valueMethod);
149  UaControlVariable *ucv = new UaControlVariable(valueMethod, vNodeId, pNodeManager, UaVariant(*adrNode->value(0).value()), NULL);
150 
151  ret = pNodeManager->addNodeAndReference(pUaMethod, adrNode, (const UaNodeId)UaNodeId(Ba_ReferenceValue, pNodeManager->getNameSpaceIndex()));
152  UA_ASSERT(ret.isGood());
153  if (numericIdentifier == BA_SDOREAD)
154  ucv->connectItem(adrNode);
155  }
156 
157  }
158  */
159 
161  {
162  UaStatus ret = OpcUa_Good;
163 
164  UaReferenceLists *uaList = getUaReferenceLists(); // take the list children
165  for (UaReference *pRef = (UaReference *)uaList->pTargetNodes(); pRef != 0; pRef = pRef->pNextForwardReference()) {
166  UaNode *ptNode = pRef->pTargetNode();
167  if (ptNode->nodeClass() == OpcUa_NodeClass_Method) {
168  ret = ((UaDeviceMethod<UaControlDeviceGeneric> *)ptNode)->execute();
169 
170  UaDataValue dataValue;
171  UaDateTime udt, sdt;
172  udt = UaDateTime::now();
173  sdt = UaDateTime::now();
174  UaVariant val = ret.statusCode();
175  dataValue.setDataValue(val, OpcUa_False, ret.statusCode(), udt, sdt);
176  m_pErrorItem->setValue(0, dataValue, OpcUa_False);
177  if (ret.isBad())
178  return;
179 
180  }
181  }
182  }
183 }