model_variable.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (C) 2013-2014 by Alexander Rykovanov *
3  * rykovanov.as@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 #include "model_impl.h"
21 
22 #include <opc/ua/model.h>
23 
24 namespace OpcUa
25 {
26 namespace Model
27 {
28 Variable::Variable(NodeId variableId, Services::SharedPtr services)
29  : Node(services)
30 {
31  Id = variableId;
32  ReadParameters attrs;
33  attrs.AttributesToRead.push_back(ToReadValueId(variableId, AttributeId::DisplayName));
34  attrs.AttributesToRead.push_back(ToReadValueId(variableId, AttributeId::BrowseName));
35  attrs.AttributesToRead.push_back(ToReadValueId(variableId, AttributeId::DataType));
36  std::vector<DataValue> values = services->Attributes()->Read(attrs);
37  DisplayName = values[0].Value.As<LocalizedText>();
38  BrowseName = values[1].Value.As<QualifiedName>();
40 }
41 
43 {
44  ReadParameters params;
46  const std::vector<DataValue> result = GetServices()->Attributes()->Read(params);
47 
48  if (result.size() != 1)
49  {
50  throw std::runtime_error("Cannot read variable value. Server returned invalid number of values.");
51  }
52 
53  return result.front();
54 }
55 
56 void Variable::SetValue(const Variant & value)
57 {
58  DataValue data(value);
60  SetValue(data);
61 }
62 
63 void Variable::SetValue(const DataValue & value)
64 {
65  WriteValue writeValue;
66  writeValue.AttributeId = AttributeId::Value;
67  writeValue.Value = value;
68  writeValue.NodeId = Id;
69  std::vector<StatusCode> result = GetServices()->Attributes()->Write({writeValue});
70 
71  if (result.size() != 1)
72  {
73  throw std::runtime_error("Failed to write data. Server returned wron nunber of status codes.");
74  }
75 
76  CheckStatusCode(result[0]);
77 }
78 
79 std::vector<Variable> Variable::Variables() const
80 {
81  return Browse<Variable>(GetId(), NodeClass::Variable, GetServices());
82 }
83 }
84 }
Variable(NodeId variableId, Services::SharedPtr services)
std::vector< OpcUa::ReadValueId > AttributesToRead
void CheckStatusCode(StatusCode code)
OpcUa Error codes. GNU LGPL.
QualifiedName BrowseName
Definition: model.h:57
ReadValueId ToReadValueId(NodeId id, AttributeId attr)
Definition: protocol.h:7
DataValue GetValue() const
void SetSourceTimestamp(const DateTime &t)
Definition: data_value.h:110
void SetValue(const Variant &value)
OPC UA Address space part. GNU LGPL.
VariantType DataTypeToVariantType(const NodeId &dataType)
LocalizedText DisplayName
Definition: model.h:58
static DateTime Current()
OpcUa::DataValue Value
Services::SharedPtr GetServices() const
Definition: model.h:47
OpcUa::NodeId NodeId
NodeId GetId() const
Definition: model_node.cpp:43
std::vector< Variable > Variables() const
OpcUa::AttributeId AttributeId


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