example_server.cpp
Go to the documentation of this file.
1 #include <iostream>
9 #include <algorithm>
10 #include <time.h>
11 
12 #include <thread>
13 #include <chrono>
14 
15 #include <opc/ua/node.h>
16 #include <opc/ua/subscription.h>
17 #include <opc/ua/server/server.h>
18 
19 
20 
21 
22 using namespace OpcUa;
23 
24 class SubClient : public SubscriptionHandler
25 {
26  void DataChange(uint32_t handle, const Node & node, const Variant & val, AttributeId attr) override
27  {
28  std::cout << "Received DataChange event for Node " << node << std::endl;
29  }
30 };
31 
32 std::vector<OpcUa::Variant> MyMethod(NodeId context, std::vector<OpcUa::Variant> arguments)
33 {
34  std::cout << "MyMethod called! " << std::endl;
35  std::vector<OpcUa::Variant> result;
36  result.push_back(Variant(static_cast<uint8_t>(0)));
37  return result;
38 }
39 
40 void RunServer()
41 {
42  //First setup our server
43  auto logger = spdlog::stderr_color_mt("server");
44  OpcUa::UaServer server(logger);
45  server.SetEndpoint("opc.tcp://localhost:4840/freeopcua/server");
46  server.SetServerURI("urn://exampleserver.freeopcua.github.io");
47  server.Start();
48 
49  //then register our server namespace and get its index in server
50  uint32_t idx = server.RegisterNamespace("http://examples.freeopcua.github.io");
51 
52  //Create our address space using different methods
53  Node objects = server.GetObjectsNode();
54 
55  //Add a custom object with specific nodeid
56  NodeId nid(99, idx);
57  QualifiedName qn("NewObject", idx);
58  Node newobject = objects.AddObject(nid, qn);
59 
60  //Add a variable and a property with auto-generated nodeid to our custom object
61  Node myvar = newobject.AddVariable(idx, "MyVariable", Variant(8));
62  Node myprop = newobject.AddVariable(idx, "MyProperty", Variant(8.8));
63  Node mymethod = newobject.AddMethod(idx, "MyMethod", MyMethod);
64 
65  //browse root node on server side
66  Node root = server.GetRootNode();
67  logger->info("Root node is: {}", root);
68  logger->info("Children are:");
69 
70  for (Node node : root.GetChildren())
71  {
72  logger->info(" {}", node);
73  }
74 
75 
76  //Uncomment following to subscribe to datachange events inside server
77  /*
78  SubClient clt;
79  std::unique_ptr<Subscription> sub = server.CreateSubscription(100, clt);
80  sub->SubscribeDataChange(myvar);
81  */
82 
83 
84 
85  //Now write values to address space and send events so clients can have some fun
86  uint32_t counter = 0;
87  myvar.SetValue(Variant(counter)); //will change value and trigger datachange event
88 
89  //Create event
90  server.EnableEventNotification();
91  Event ev(ObjectId::BaseEventType); //you should create your own type
92  ev.Severity = 2;
94  ev.SourceName = "Event from FreeOpcUA";
95  ev.Time = DateTime::Current();
96 
97 
98  logger->info("Ctrl-C to exit");
99 
100  for (;;)
101  {
102  myvar.SetValue(Variant(++counter)); //will change value and trigger datachange event
103  std::stringstream ss;
104  ss << "This is event number: " << counter;
105  ev.Message = LocalizedText(ss.str());
106  server.TriggerEvent(ev);
107  std::this_thread::sleep_for(std::chrono::milliseconds(5000));
108  }
109 
110  server.Stop();
111 }
112 
113 int main(int argc, char ** argv)
114 {
115  try
116  {
117  RunServer();
118  }
119 
120  catch (const std::exception & exc)
121  {
122  std::cout << exc.what() << std::endl;
123  }
124 
125  return 0;
126 }
127 
void SetEndpoint(const std::string &endpoint)
set endpoint uri on wich server will listen. opc.tcp://localhost:4841/opcua/server opc...
Definition: server.cpp:56
Node GetRootNode() const
helper methods for node you will probably want to access
Definition: server.cpp:179
int counter
Definition: server.py:47
void SetServerURI(const std::string &uri)
Definition: server.cpp:66
Node AddVariable(const NodeId &variableId, const QualifiedName &browsename, const Variant &val) const
Definition: node.cpp:416
Node AddMethod(const NodeId &variableId, const QualifiedName &browsename, std::function< std::vector< OpcUa::Variant >(NodeId context, std::vector< OpcUa::Variant > arguments)> method) const
Definition: node.cpp:519
LocalizedText Message
Definition: event.h:41
void EnableEventNotification()
Enable event notification on Server node this is necessary if you want to be able to send custom even...
Definition: server.cpp:194
std::string SourceName
Definition: event.h:44
Subscription client class handles subscription callbacks.
NodeId SourceNode
Definition: event.h:43
void RunServer()
uint16_t Severity
Definition: event.h:42
uint32_t RegisterNamespace(std::string uri)
Register your application namespace.
Definition: server.cpp:89
void Start()
Start and Stop server.
Definition: server.cpp:118
int main(int argc, char **argv)
void TriggerEvent(Event event)
Trigger and event.
Definition: server.cpp:220
objects
Definition: client.py:46
myprop
Definition: server.py:38
handle
Definition: client.py:58
std::vector< OpcUa::Variant > MyMethod(NodeId context, std::vector< OpcUa::Variant > arguments)
OPC UA Address space part. GNU LGPL.
Node AddObject(const NodeId &folderId, const QualifiedName &browseName) const
Definition: node.cpp:377
myvar
Definition: client.py:50
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::vector< Node > GetChildren(const OpcUa::ReferenceId &refid) const
List child nodes by specified reference.
Definition: node.cpp:215
static DateTime Current()
ev
Definition: server.py:46
Node GetObjectsNode() const
Definition: server.cpp:184
root
Definition: client.py:39
void SetValue(const Variant &val) const
Definition: node.cpp:135
void DataChange(uint32_t handle, const Node &node, const Variant &val, AttributeId attr) override
std::shared_ptr< logger > stderr_color_mt(const std::string &logger_name)
Definition: spdlog_impl.h:150
idx
Definition: client.py:32
DateTime Time
Definition: event.h:40
server
Definition: server.py:19


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