example_client.cpp
Go to the documentation of this file.
1 
11 #include <opc/ua/client/client.h>
12 #include <opc/ua/node.h>
13 #include <opc/ua/subscription.h>
14 
15 #include <opc/common/logger.h>
16 
17 #include <iostream>
18 #include <stdexcept>
19 #include <thread>
20 
21 using namespace OpcUa;
22 
24 {
25  void DataChange(uint32_t handle, const Node & node, const Variant & val, AttributeId attr) override
26  {
27  std::cout << "Received DataChange event, value of Node " << node << " is now: " << val.ToString() << std::endl;
28  }
29 };
30 
31 int main(int argc, char ** argv)
32 {
33  auto logger = spdlog::stderr_color_mt("client");
34  //logger->set_level(spdlog::level::debug);
35  try
36  {
37  //std::string endpoint = "opc.tcp://192.168.56.101:48030";
38  //std::string endpoint = "opc.tcp://user:password@192.168.56.101:48030";
39  std::string endpoint = "opc.tcp://127.0.0.1:4840/freeopcua/server/";
40  //std::string endpoint = "opc.tcp://localhost:53530/OPCUA/SimulationServer/";
41  //std::string endpoint = "opc.tcp://localhost:48010";
42 
43  if (argc > 1)
44  { endpoint = argv[1]; }
45 
46  logger->info("Connecting to: {}", endpoint);
47 
48  OpcUa::UaClient client(logger);
49  client.Connect(endpoint);
50 
51  //get Root node on server
52  OpcUa::Node root = client.GetRootNode();
53  logger->info("Root node is: {}", root);
54 
55  //get and browse Objects node
56  logger->info("Child of objects node are:");
57  Node objects = client.GetObjectsNode();
58 
59  for (OpcUa::Node node : objects.GetChildren())
60  { logger->info(" {}", node); }
61 
62  //get a node from standard namespace using objectId
63  logger->info("NamespaceArray is:");
65  OpcUa::Variant ns = nsnode.GetValue();
66 
67  for (std::string d : ns.As<std::vector<std::string>>())
68  { logger->info(" {}", d); }
69 
72  OpcUa::Node mymethod;
73 
74  //Initialize Node myvar:
75 
76  //Get namespace index we are interested in
77 
78  // From freeOpcUa Server:
79  uint32_t idx = client.GetNamespaceIndex("http://examples.freeopcua.github.io");
81  //std::vector<std::string> varpath({ std::to_string(idx) + ":NewObject", "MyVariable" });
82  //myvar = objects.GetChild(varpath);
83  std::vector<std::string> methodpath({ std::to_string(idx) + ":NewObject" });
84  myobject = objects.GetChild(methodpath);
85  methodpath = { std::to_string(idx) + ":NewObject", "MyMethod" };
86  mymethod = objects.GetChild(methodpath);
87  std::vector<OpcUa::Variant> arguments;
88  arguments.push_back(static_cast<uint8_t>(0));
89  myobject.CallMethod(mymethod.GetId(), arguments);
90 
91  // Example data from Prosys server:
92  //std::vector<std::string> varpath({"Objects", "5:Simulation", "5:Random1"});
93  //myvar = root.GetChild(varpath);
94 
95  // Example from any UA server, standard dynamic variable node:
96  std::vector<std::string> varpath{ "Objects", "Server", "ServerStatus", "CurrentTime" };
97  myvar = root.GetChild(varpath);
98 
99  logger->info("got node: {}", myvar);
100 
101  //Subscription
102  SubClient sclt;
103  Subscription::SharedPtr sub = client.CreateSubscription(100, sclt);
104  uint32_t handle = sub->SubscribeDataChange(myvar);
105  logger->info("Got sub handle: {}, sleeping 5 seconds", handle);
106  std::this_thread::sleep_for(std::chrono::seconds(5));
107 
108  logger->info("Disconnecting");
109  client.Disconnect();
110  logger->flush();
111  return 0;
112  }
113 
114  catch (const std::exception & exc)
115  {
116  logger->error("Error: {}", exc.what());
117  }
118 
119  catch (...)
120  {
121  logger->error("Unknown error.");
122  }
123 
124  return -1;
125 }
126 
d
Node GetNode(const NodeId &nodeid) const
Get a specific node by nodeid.
Definition: client.cpp:403
sclt
Definition: client.py:54
sub
Definition: client.py:55
Subscription client class handles subscription callbacks.
void Disconnect()
Disconnect from server.
Definition: client.cpp:347
Node GetRootNode() const
helper methods for node you will probably want to access
Definition: client.cpp:410
int main(int argc, char **argv)
NodeId GetId() const
Definition: node.cpp:50
myobject
Definition: server.py:36
void Connect(const std::string &endpoint)
connect to a server, specify endpoint as string
Definition: client.cpp:213
objects
Definition: client.py:46
uint32_t GetNamespaceIndex(std::string uri)
Definition: client.cpp:378
handle
Definition: client.py:58
OPC UA Address space part. GNU LGPL.
Node GetObjectsNode() const
Definition: client.cpp:417
myvar
Definition: client.py:50
Node GetChild(const std::vector< OpcUa::QualifiedName > &path) const
Definition: node.cpp:270
Variant GetValue() const
Definition: node.cpp:548
std::vector< Variant > CallMethod(NodeId methodId, std::vector< Variant > inputArguments) const
Definition: node.cpp:80
Subscription::SharedPtr CreateSubscription(unsigned int period, SubscriptionHandler &client)
Create a subscription objects.
Definition: client.cpp:479
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
client
Definition: client.py:25
root
Definition: client.py:39
T As() const
Definition: variant.h:271
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
std::string ToString() const


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