server_object.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 "server_object.h"
00021 
00022 #include <boost/chrono.hpp>
00023 #include <opc/ua/node.h>
00024 #include <opc/ua/server/addons/services_registry.h>
00025 #include <functional>
00026 
00027 #ifdef WIN32
00028 #undef GetObject
00029 #endif
00030 
00031 namespace
00032 {
00033   OpcUa::RelativePathElement GetHierarchicalElement(const std::string& browseName)
00034   {
00035     OpcUa::RelativePathElement element;
00036     element.ReferenceTypeId = OpcUa::ObjectId::HierarchicalReferences;
00037     element.IncludeSubtypes = true;
00038     element.TargetName.Name = browseName;
00039     return element;
00040   }
00041 
00042 
00043   OpcUa::RelativePath GetCurrentTimeRelativepath()
00044   {
00045     OpcUa::RelativePath path;
00046     path.Elements.push_back(GetHierarchicalElement(OpcUa::Names::ServerStatus));
00047     path.Elements.push_back(GetHierarchicalElement(OpcUa::Names::CurrentTime));
00048     return path;
00049   }
00050 }
00051 
00052 namespace OpcUa
00053 {
00054   namespace Server
00055   {
00056 
00057     ServerObject::ServerObject(Services::SharedPtr services, boost::asio::io_service& io, bool debug)
00058       : Server(services)
00059       , Io(io)
00060       , Debug(debug)
00061       , Instance(std::move(CreateServerObject(services)))
00062       , ServerTime(Instance.GetVariable(GetCurrentTimeRelativepath()))
00063       , Timer(io)
00064     {
00065       Timer.Start(boost::posix_time::seconds(1), [this](){
00066         UpdateTime();
00067       });
00068       //Set many values in address space which are expected by clients
00069       std::vector<std::string> uris;
00070       uris.push_back("http://opcfoundation.org/UA/");
00071       uris.push_back("http://freeopcua.github.io");
00072       Node node = Node(Server, ObjectId::Server_NamespaceArray);
00073       node.SetValue(uris);
00074       node = Node(Server, ObjectId::Server_ServerStatus_BuildInfo_ManufacturerName);
00075       node.SetValue(std::string("FreeOpcUa"));
00076       node = Node(Server, ObjectId::Server_ServerCapabilities_LocaleIdArray);
00077       node.SetValue(std::vector<std::string>({ "en" }));
00078       node = Node(Server, ObjectId::Server_ServerStatus_BuildInfo_BuildNumber);
00079       node.SetValue(std::string("0.8"));
00080       node = Node(Server, ObjectId::Server_ServerStatus_BuildInfo_ProductName);
00081       node.SetValue(std::string("FreeOpcUa"));
00082       node = Node(Server, ObjectId::Server_ServerStatus_BuildInfo_ProductUri);
00083       node.SetValue(std::string("https://freeopcua.github.io"));
00084       node = Node(Server, ObjectId::Server_ServerStatus_State);
00085       node.SetValue((int32_t) 0);
00086       node = Node(Server, ObjectId::Server_ServerStatus_CurrentTime);
00087       node.SetValue(DateTime::Current());//Set time before the thread does it
00088 
00089     }
00090 
00091     ServerObject::~ServerObject()
00092     {
00093       if (Debug) std::clog << "server_object| canceling timer..." << std::endl;
00094       Timer.Cancel();
00095       if (Debug) std::clog << "server_object| timer stopped." << std::endl;
00096     }
00097 
00098     Model::Object ServerObject::CreateServerObject(const Services::SharedPtr& services) const
00099     {
00100       Model::Server server(services);
00101       Model::Object serverObject = server.GetObject(ObjectId::Server);
00102       return serverObject;
00103     }
00104 
00105     void ServerObject::UpdateTime()
00106     {
00107       try
00108       {
00109         DateTime t = OpcUa::DateTime::Current();
00110         DataValue timeData(t);
00111         timeData.SetSourceTimestamp(t);
00112         timeData.SetServerTimestamp(t);
00113 
00114         if (Debug) std::clog << "server_object| Updating server time: " << t << std::endl;
00115         ServerTime.SetValue(timeData);
00116       }
00117       catch (std::exception& ex)
00118       {
00119         std::cerr << "Failed to update time at server object: " << ex.what() << std::endl;
00120       }
00121 
00122     }
00123 
00124   } // namespace UaServer
00125 } // namespace OpcUa


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