UniSet 2.44.3
JScript/tests/OPCUATestServer.h
1/*
2 * Simplified OPC UA test server for unit tests.
3 */
4#ifndef TESTS_OPCUA_TEST_SERVER_H_
5#define TESTS_OPCUA_TEST_SERVER_H_
6// --------------------------------------------------------------------------
7#include <memory>
8#include <string>
9#include <unordered_map>
10#include <thread>
11#include <atomic>
12#include <functional>
13#include <optional>
14
15#include "open62541pp/open62541pp.hpp"
16// --------------------------------------------------------------------------
17static const char* kEndpointUrl = "opc.tcp://127.0.0.1:15480";
18static const char* kNodeInt = "TestInt";
19static const char* kNodeFloat = "TestFloat";
20static const char* kNodeBool = "TestBool";
21// --------------------------------------------------------------------------
22class OPCUATestServer
23{
24 public:
25 OPCUATestServer( const std::string& host, uint16_t port = 15480 );
26 ~OPCUATestServer();
27
28 bool start();
29 void stop();
30 bool isRunning() const noexcept;
31
32 void setInt( const std::string& name, int32_t value );
33 void setFloat( const std::string& name, float value );
34 void setBool( const std::string& name, bool value );
35
36 int32_t getInt( const std::string& name ) const;
37 float getFloat( const std::string& name ) const;
38 bool getBool( const std::string& name ) const;
39
40 std::string nodeIdString( const std::string& name ) const;
41
42 private:
43 void ensureNode( const std::string& name, opcua::DataTypeId type );
44 void writeValue( const std::string& name, opcua::DataTypeId type, const std::function<void(opcua::Node<opcua::Server>&)>& fn );
45 void run();
46
47 struct NodeEntry
48 {
49 opcua::Node<opcua::Server> node;
50 opcua::DataTypeId type;
51
52 NodeEntry(const opcua::Node<opcua::Server>& n, opcua::DataTypeId t):
53 node(n), type(t) {}
54 };
55
56 std::string host;
57 uint16_t port;
58 std::unique_ptr<opcua::Server> server;
59 std::optional<opcua::Node<opcua::Server>> ioNode;
60 mutable std::unordered_map<std::string, std::unique_ptr<NodeEntry>> nodes;
61 std::thread worker;
62 std::atomic_bool running { false };
63};
64
65#endif // TESTS_OPCUA_TEST_SERVER_H_