node.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (C) 2014-2014 Olivier Roulet-Dubonnet *
3  * olivier.roulet@gmail.com *
4  * *
5  * This library is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU Lesser General Public License as *
7  * published by the Free Software Foundation; version 3 of the License. *
8  * *
9  * This library is distributed in the hope that it will be useful, *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12  * GNU Lesser General Public License for more details. *
13  * *
14  * You should have received a copy of the GNU Lesser General Public License *
15  * along with this library; if not, write to the *
16  * Free Software Foundation, Inc., *
17  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18  ******************************************************************************/
19 
20 #pragma once
21 
23 
24 #include <sstream>
25 
26 
27 namespace OpcUa
28 {
29 
30 class NodeNotFoundException : public std::runtime_error
31 {
32 public:
33  NodeNotFoundException() : std::runtime_error("NodeNotFoundException") { }
34 };
35 
41 
42 class Node
43 {
44 public:
45  // Creating Root Node.
46  explicit Node(Services::SharedPtr srv);
47  Node(Services::SharedPtr srv, const NodeId & id);
48  Node(const Node & other);
49  Node() {}
50 
51  NodeId GetId() const;
52 
53  QualifiedName GetBrowseName() const;
54  //void SetBrowseName(const QualifiedName& name) const;
55 
56  Node GetParent() const;
57 
60  std::vector<Node> GetChildren(const OpcUa::ReferenceId & refid) const;
61 
64  std::vector<Node> GetChildren() const;
65 
66  //The GetChildNode methods return a node defined by its path from the node. A path is defined by
67  // a sequence of browse name(QualifiedName). A browse name is either defined through a qualifiedname object
68  // or a string of format namespace:browsename. If a namespace is not specified it is assumed to be
69  //the same as the parent
70  Node GetChild(const std::vector<OpcUa::QualifiedName> & path) const;
71  Node GetChild(const std::vector<std::string> & path) const;
72  Node GetChild(const std::string & browsename) const ;
73 
74  //TODO: How to get References?
75  std::vector<Node> GetProperties() const;
76  std::vector<Node> GetVariables() const { return GetChildren(OpcUa::ReferenceId::HasComponent); } //Not correct should filter by variable type
77 
78  bool IsValid() const
79  {
80  try
81  {
82  auto value = GetAttribute(AttributeId::NodeId).Value;
83  if (value.IsNul())
84  {
85  return false;
86  }
87  return !value.As<NodeId>().IsNull();
88  } catch (std::exception& e) {
89  return false;
90  }
91  }
92 
93  //The Read and Write methods read or write attributes of the node
94  //FIXME: add possibility to read and write several nodes at once
95  DataValue GetAttribute(const AttributeId attr) const;
96  void SetAttribute(AttributeId attr, const DataValue & dval) const;
97  //std::vector<StatusCode> WriteAttrs(OpcUa::AttributeId attr, const Variant &val);
98 
99  // convenience methods for attributes
100  void SetAccessLevel(VariableAccessLevel value) { SetAttribute(AttributeId::AccessLevel, DataValue(static_cast<uint8_t>(value))); }
101  VariableAccessLevel GetAccessLevel() const { return static_cast<VariableAccessLevel>(GetAttribute(AttributeId::AccessLevel).Value.As<uint8_t>()); }
102  void SetDescription(const LocalizedText & value) { SetAttribute(AttributeId::Description, DataValue(value)); }
104  void SetNodeClass(NodeClass value) { SetAttribute(AttributeId::NodeClass, DataValue(static_cast<int32_t>(value))); }
105  NodeClass GetNodeClass() const { return static_cast<NodeClass>(GetAttribute(AttributeId::NodeClass).Value.As<int32_t>()); }
106  void SetUserAccessLevel(VariableAccessLevel value) { SetAttribute(AttributeId::UserAccessLevel, DataValue(static_cast<uint8_t>(value))); }
107  VariableAccessLevel GetUserAccessLevel() const { return static_cast<VariableAccessLevel>(GetAttribute(AttributeId::UserAccessLevel).Value.As<uint8_t>()); }
108  void SetUserWriteMask(AttributeWriteMask value) { SetAttribute(AttributeId::UserWriteMask, DataValue(static_cast<uint32_t>(value))); }
109  AttributeWriteMask GetUserWriteMask() const { return static_cast<AttributeWriteMask>(GetAttribute(AttributeId::UserWriteMask).Value.As<uint32_t>()); }
110  void SetWriteMask(AttributeWriteMask value) { SetAttribute(AttributeId::WriteMask, DataValue(static_cast<uint32_t>(value))); }
111  AttributeWriteMask GetWriteMask() const { return static_cast<AttributeWriteMask>(GetAttribute(AttributeId::WriteMask).Value.As<uint32_t>()); }
112 
113  //Helper method to get/set VALUE attribute of a node (Not all nodes support VALUE attribute)
114  Variant GetValue() const;
115  DataValue GetDataValue() const;
116  void SetValue(const Variant & val) const;
117  void SetValue(const DataValue & dval) const;
118 
119  Variant GetDataType() const;
120 
121  // CallMethod
122  std::vector<Variant> CallMethod(NodeId methodId, std::vector<Variant> inputArguments) const;
123  std::vector<std::vector<Variant>> CallMethods(std::vector<NodeId> methodIds, std::vector<std::vector<Variant>> inputArguments) const;
124 
125  //OpcUa low level methods to to modify address space model
126  std::vector<AddNodesResult> AddNodes(std::vector<AddNodesItem> items) const;
127  std::vector<StatusCode> AddReferences(std::vector<AddReferencesItem> items) const;
128 
129 
130  //Helper classes to modify address space model
131  Node AddFolder(const NodeId & folderId, const QualifiedName & browseName) const;
132  Node AddFolder(const std::string & nodeid, const std::string & browseName) const;
133  Node AddFolder(uint32_t namespaceidx, const std::string & browseName) const;
134 
135  Node AddObject(const NodeId & folderId, const QualifiedName & browseName) const;
136  Node AddObject(const std::string & nodeid, const std::string & browseName) const;
137  Node AddObject(uint32_t namespaceidx, const std::string & browseName) const;
138 
139  Node AddVariable(const NodeId & variableId, const QualifiedName & browsename, const Variant & val) const;
140  Node AddVariable(uint32_t namespaceidx, const std::string & BrowseName, const Variant & val) const;
141  Node AddVariable(const std::string & nodeId, const std::string & browseName, const Variant & val) const;
142 
143  Node AddProperty(const NodeId & propertyId, const QualifiedName & browsename, const Variant & val) const;
144  Node AddProperty(const std::string & nodeid, const std::string & browseName, const Variant & val) const;
145  Node AddProperty(uint32_t namespaceidx, const std::string & browseName, const Variant & val) const;
146 
147  Node AddMethod(const NodeId & variableId, const QualifiedName & browsename, std::function<std::vector<OpcUa::Variant> (NodeId context, std::vector<OpcUa::Variant> arguments)> method) const;
148  Node AddMethod(uint32_t namespaceidx, const std::string & BrowseName, std::function<std::vector<OpcUa::Variant> (NodeId context, std::vector<OpcUa::Variant> arguments)> method) const;
149  Node AddMethod(const std::string & nodeId, const std::string & browseName, std::function<std::vector<OpcUa::Variant> (NodeId context, std::vector<OpcUa::Variant> arguments)> method) const;
150 
151  std::string ToString() const;
152 
153  bool operator==(Node const & x) const { return Id == x.Id; }
154  bool operator!=(Node const & x) const { return Id != x.Id; }
155 
156  //FIXME: I need this to create a copy for python binding, another way?
157  OpcUa::Services::SharedPtr GetServices() const {return Server;}
158 
159 protected:
160  // base function for GetChildren(), put found children into given nodes
161  // parameter
162  void _GetChildren(const OpcUa::ReferenceId & refid, std::vector<Node> & nodes) const;
163 
164 protected:
165  OpcUa::Services::SharedPtr Server;
167 };
168 
169 
170 std::ostream & operator<<(std::ostream & os, const Node & node);
171 
172 //FIXME: The following methods should be moved somewhere else!!!
173 
175 
176 
177 } // namespace OpcUa
178 
void SetDescription(const LocalizedText &value)
Definition: node.h:102
LocalizedText GetDescription() const
Definition: node.h:103
bool operator!=(Node const &x) const
Definition: node.h:154
AttributeWriteMask GetWriteMask() const
Definition: node.h:111
AttributeWriteMask
Definition: enums.h:142
OpcUa::Services::SharedPtr GetServices() const
Definition: node.h:157
OpcUa::Services::SharedPtr Server
Definition: node.h:165
void SetNodeClass(NodeClass value)
Definition: node.h:104
ObjectId VariantTypeToDataType(VariantType vt)
VariantType
Definition: variant.h:27
bool IsValid() const
Definition: node.h:78
ObjectId
Definition: object_ids.h:12
Node()
Definition: node.h:49
std::vector< Node > GetVariables() const
Definition: node.h:76
OPC UA Address space part. GNU LGPL.
NodeId Id
Definition: node.h:166
bool operator==(Node const &x) const
Definition: node.h:153
AttributeWriteMask GetUserWriteMask() const
Definition: node.h:109
NodeClass
Definition: enums.h:39
A Node object represent an OPC-UA node. It is high level object intended for developper who want to e...
Definition: node.h:42
void SetAccessLevel(VariableAccessLevel value)
Definition: node.h:100
std::string ToString(const AttributeId &value)
void SetUserAccessLevel(VariableAccessLevel value)
Definition: node.h:106
std::ostream & operator<<(std::ostream &os, const Node &node)
Definition: node.cpp:566
NodeClass GetNodeClass() const
Definition: node.h:105
void SetUserWriteMask(AttributeWriteMask value)
Definition: node.h:108
T As() const
Definition: variant.h:271
VariableAccessLevel GetUserAccessLevel() const
Definition: node.h:107
void SetWriteMask(AttributeWriteMask value)
Definition: node.h:110
VariableAccessLevel GetAccessLevel() const
Definition: node.h:101


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:12:06