model_variable.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002  *   Copyright (C) 2013-2014 by Alexander Rykovanov                        *
00003  *   rykovanov.as@gmail.com                                                   *
00004  *                                                                            *
00005  *   This library is free software; you can redistribute it and/or modify     *
00006  *   it under the terms of the GNU Lesser General Public License as           *
00007  *   published by the Free Software Foundation; version 3 of the License.     *
00008  *                                                                            *
00009  *   This library is distributed in the hope that it will be useful,          *
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00012  *   GNU Lesser General Public License for more details.                      *
00013  *                                                                            *
00014  *   You should have received a copy of the GNU Lesser General Public License *
00015  *   along with this library; if not, write to the                            *
00016  *   Free Software Foundation, Inc.,                                          *
00017  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.                *
00018  ******************************************************************************/
00019 
00020 #include "model_impl.h"
00021 
00022 #include <opc/ua/model.h>
00023 
00024 namespace OpcUa
00025 {
00026   namespace Model
00027   {
00028     Variable::Variable(NodeId variableId, Services::SharedPtr services)
00029       : Node(services)
00030     {
00031       Id = variableId;
00032       ReadParameters attrs;
00033       attrs.AttributesToRead.push_back(ToReadValueId(variableId, AttributeId::DisplayName));
00034       attrs.AttributesToRead.push_back(ToReadValueId(variableId, AttributeId::BrowseName));
00035       attrs.AttributesToRead.push_back(ToReadValueId(variableId, AttributeId::DataType));
00036       std::vector<DataValue> values = services->Attributes()->Read(attrs);
00037       DisplayName = values[0].Value.As<LocalizedText>();
00038       BrowseName = values[1].Value.As<QualifiedName>();
00039       DataType = OpcUa::DataTypeToVariantType(values[2].Value.As<NodeId>());
00040     }
00041 
00042     DataValue Variable::GetValue() const
00043     {
00044       ReadParameters params;
00045       params.AttributesToRead.push_back(ToReadValueId(GetId(), AttributeId::Value));
00046       const std::vector<DataValue> result = GetServices()->Attributes()->Read(params);
00047       if (result.size() != 1)
00048       {
00049         throw std::runtime_error("Cannot read variable value. Server returned invalid number of values.");
00050       }
00051       return result.front();
00052     }
00053 
00054     void Variable::SetValue(const Variant& value)
00055     {
00056       DataValue data(value);
00057       data.SetSourceTimestamp(OpcUa::DateTime::Current());
00058       SetValue(data);
00059     }
00060 
00061     void Variable::SetValue(const DataValue& value)
00062     {
00063       WriteValue writeValue;
00064       writeValue.AttributeId = AttributeId::Value;
00065       writeValue.Value = value;
00066       writeValue.NodeId = Id;
00067       std::vector<StatusCode> result = GetServices()->Attributes()->Write({writeValue});
00068       if (result.size() != 1)
00069       {
00070         throw std::runtime_error("Failed to write data. Server returned wron nunber of status codes.");
00071       }
00072       CheckStatusCode(result[0]);
00073     }
00074 
00075     std::vector<Variable> Variable::Variables() const
00076     {
00077       return Browse<Variable>(GetId(), NodeClass::Variable, GetServices());
00078     }
00079   }
00080 }


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Sat Jun 8 2019 18:24:56