21 using namespace OpcUa;
27 std::cout <<
"Received DataChange event, value of Node " << node <<
" is now: " << val.
ToString() << std::endl;
31 int main(
int argc,
char ** argv)
39 std::string endpoint =
"opc.tcp://127.0.0.1:4840/freeopcua/server/";
44 { endpoint = argv[1]; }
46 logger->info(
"Connecting to: {}", endpoint);
53 logger->info(
"Root node is: {}", root);
56 logger->info(
"Child of objects node are:");
60 { logger->info(
" {}", node); }
63 logger->info(
"NamespaceArray is:");
68 { logger->info(
" {}",
d); }
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));
96 std::vector<std::string> varpath{
"Objects",
"Server",
"ServerStatus",
"CurrentTime" };
99 logger->info(
"got node: {}", myvar);
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));
108 logger->info(
"Disconnecting");
114 catch (
const std::exception & exc)
116 logger->error(
"Error: {}", exc.what());
121 logger->error(
"Unknown error.");
Node GetNode(const NodeId &nodeid) const
Get a specific node by nodeid.
Subscription client class handles subscription callbacks.
void Disconnect()
Disconnect from server.
Node GetRootNode() const
helper methods for node you will probably want to access
int main(int argc, char **argv)
void Connect(const std::string &endpoint)
connect to a server, specify endpoint as string
uint32_t GetNamespaceIndex(std::string uri)
OPC UA Address space part. GNU LGPL.
Node GetObjectsNode() const
Node GetChild(const std::vector< OpcUa::QualifiedName > &path) const
std::vector< Variant > CallMethod(NodeId methodId, std::vector< Variant > inputArguments) const
Subscription::SharedPtr CreateSubscription(unsigned int period, SubscriptionHandler &client)
Create a subscription objects.
A Node object represent an OPC-UA node. It is high level object intended for developper who want to e...
std::vector< Node > GetChildren(const OpcUa::ReferenceId &refid) const
List child nodes by specified reference.
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)
std::string ToString() const