server_object.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 "server_object.h"
21 
22 #include <boost/chrono.hpp>
23 #include <opc/ua/node.h>
25 #include <functional>
26 #include <iostream>
27 
28 #ifdef WIN32
29 #undef GetObject
30 #endif
31 
32 namespace
33 {
35 {
38  element.IncludeSubtypes = true;
39  element.TargetName.Name = browseName;
40  return element;
41 }
42 
43 
44 OpcUa::RelativePath GetCurrentTimeRelativepath()
45 {
49  return path;
50 }
51 }
52 
53 namespace OpcUa
54 {
55 namespace Server
56 {
57 
58 ServerObject::ServerObject(Services::SharedPtr services, boost::asio::io_service & io, bool debug)
59  : Server(services)
60 // , Io(io)
61  , Debug(debug)
62  , Instance(CreateServerObject(services))
63  , ServerTime(Instance.GetVariable(GetCurrentTimeRelativepath()))
64  , Timer(io)
65 {
66  Timer.Start(boost::posix_time::seconds(1), [this]()
67  {
68  UpdateTime();
69  });
70  //Set many values in address space which are expected by clients
71  std::vector<std::string> uris;
72  uris.push_back("http://opcfoundation.org/UA/");
73  uris.push_back("http://freeopcua.github.io");
75  node.SetValue(uris);
77  node.SetValue(std::string("FreeOpcUa"));
79  node.SetValue(std::vector<std::string>({ "en" }));
81  node.SetValue(std::string("0.8"));
83  node.SetValue(std::string("FreeOpcUa"));
85  node.SetValue(std::string("https://freeopcua.github.io"));
87  node.SetValue((int32_t) 0);
89  node.SetValue(DateTime::Current());//Set time before the thread does it
90 
91 }
92 
94 {
95  if (Debug) { std::clog << "server_object| canceling timer..." << std::endl; }
96 
97  Timer.Cancel();
98 
99  if (Debug) { std::clog << "server_object| timer stopped." << std::endl; }
100 }
101 
102 Model::Object ServerObject::CreateServerObject(const Services::SharedPtr & services) const
103 {
104  Model::Server server(services);
105  Model::Object serverObject = server.GetObject(ObjectId::Server);
106  return serverObject;
107 }
108 
110 {
111  try
112  {
114  DataValue timeData(t);
115  timeData.SetSourceTimestamp(t);
116  timeData.SetServerTimestamp(t);
117 
118  if (Debug) { std::clog << "server_object| Updating server time: " << t << std::endl; }
119 
120  ServerTime.SetValue(timeData);
121  }
122 
123  catch (std::exception & ex)
124  {
125  std::cerr << "Failed to update time at server object: " << ex.what() << std::endl;
126  }
127 
128 }
129 
130 } // namespace UaServer
131 } // namespace OpcUa
const char ServerStatus[]
Definition: strings.h:162
const char CurrentTime[]
Definition: strings.h:39
Object GetObject(const NodeId &id) const
ServerObject(Services::SharedPtr services, boost::asio::io_service &io, bool debug)
Services::SharedPtr Server
Definition: server_object.h:46
Model::Variable ServerTime
Definition: server_object.h:50
OpcUa::RelativePathElement GetHierarchicalElement(const std::string &browseName)
void SetSourceTimestamp(const DateTime &t)
Definition: data_value.h:110
void SetValue(const Variant &value)
Debug
std::vector< RelativePathElement > Elements
Definition: types.h:125
OPC UA Address space part. GNU LGPL.
A Node object represent an OPC-UA node. It is high level object intended for developper who want to e...
Definition: node.h:42
std::string Name
Definition: types.h:74
static DateTime Current()
void SetServerTimestamp(const DateTime &t)
Definition: data_value.h:116
void SetValue(const Variant &val) const
Definition: node.cpp:135
Model::Object CreateServerObject(const Services::SharedPtr &services) const
const char Server[]
Definition: strings.h:121
server
Definition: server.py:19
QualifiedName TargetName
Definition: types.h:120
void Start(const boost::asio::deadline_timer::duration_type &t, std::function< void()> handler)
Definition: timer.h:45


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