CVB++ 14.0
OpcUa/BareboneServer
1// ----------------------------------------------------------------------------
17// ----------------------------------------------------------------------------
18
19
20#include <iostream>
21#include <memory>
22#include <locale>
23#include <codecvt>
24#include <thread>
25
26#include <cvb/opcua/server.hpp>
27
28int main(int, char*[])
29{
30 try
31 {
32 // create server object
33 auto server = Cvb::OpcUa::Server::Create(4840);
34
35 // add a namespace to the server (this is not mandatory)
36 auto nsIndex = server->AddNameSpace("TestNamespace");
37
38 // get the "objectsfolder" node id (we will insert a folder to it)
39 auto root = Cvb::OpcUa::NodeID::Create(Cvb::OpcUa::Namespace0NodeID::OBJECTSFOLDER);
40
41 // get the type for a folder
42 auto type = Cvb::OpcUa::NodeID::Create(Cvb::OpcUa::Namespace0NodeID::FOLDERTYPE);
43
44 // create a folder object
45 auto folder = Cvb::OpcUa::ObjectNode::CreateFromType(nsIndex, "TestFolder", *root, *type);
46
47 // add the created folder to the server
48 server->AddNode(*folder);
49
50 // creating a FloatNode (as a child of the previously created folder)
51 auto node = Cvb::OpcUa::FloatNode::Create(nsIndex, "TestFloat", *folder->NodeID(), 42.7);
52
53 // add the float node to the server
54 server->AddNode(*node);
55
56 // add a callback to the float node
57
58 auto callbackCookie = node->RegisterWriteCallback([&]()
59 {
60 std::cout << "write was triggerd" << std::endl;
61 });
62
63 // start, the server is now ready
64 server->Start();
65
66 // wait until "enter" is pressed.
67 std::cout << "Press enter to stop the server" << std::endl;
68
69 std::cin.get();
70
71 // remove the callback
72 node->UnregisterCallback(callbackCookie);
73
74 // stop
75 server->Stop();
76 }
77 catch (const Cvb::OpcUa::OpcUaException & e1)
78 {
79 std::cout << e1.what() << std::endl;
80 }
81 catch (const std::exception & ex)
82 {
83 std::cout << ex.what() << std::endl;
84 }
85}
static FloatNodePtr Create(std::uint16_t namespaceIndex, const Cvb::String &name, const OpcUa::NodeID &parentNodeID, double value)
Creates an OPCUA Variable with the specified parameter, data type and set its value as double.
Definition: float_node.hpp:45
static NodeIDPtr Create(Namespace0NodeID id)
Creates an id based on a predefined Namespace0 node id.
Definition: node_id.hpp:48
static ObjectNodePtr CreateFromType(const std::uint16_t NameSpaceIndex, const Cvb::String &Name, const OpcUa::NodeID &Parent, const OpcUa::NodeID &TypeDefinition)
Creates a OPCUA object node with a given type definition.
Definition: object_node.hpp:49
Special runtime exception to carry a native error code.
Definition: exception.hpp:25
static ServerPtr Create(const std::uint16_t port)
Creates an OPCUA server with the specified port number.
Definition: detail_server.hpp:23