UniSet 2.35.2
OPCUAClient.h
1/*
2 * Copyright (c) 2023 Pavel Vainerman.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation, version 2.1.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Lesser Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16// -----------------------------------------------------------------------------
17#ifndef OPCUAClient_H_
18#define OPCUAClient_H_
19// -----------------------------------------------------------------------------
20#include <string>
21#include <vector>
22#include <unordered_map>
23#include <map>
24#include <variant>
25
26#include "open62541pp/open62541pp.h"
27#include "open62541pp/detail/ExceptionCatcher.h"
28
29#include <open62541/client_config_default.h>
30#include "Exceptions.h"
31//--------------------------------------------------------------------------
32namespace uniset
33{
35 {
36 opcua::ReadValueId itemToMonitor;
37 opcua::services::DataChangeNotificationCallback dataChangeCallback;
38 opcua::services::DeleteMonitoredItemCallback deleteCallback;
39 };
40
41 using DataChangeCallback =
42 std::function<void(const uniset::MonitoredItem& item, const opcua::DataValue& value)>;
43
44 using DeleteMonitoredItemCallback = std::function<void(uint32_t subId, uint32_t monId)>;
45
46 // -----------------------------------------------------------------------------
49 {
50 public:
52 virtual ~OPCUAClient();
53
54 bool connect( const std::string& addr );
55 bool connect( const std::string& addr, const std::string& user, const std::string& pass );
56 void disconnect() noexcept;
57
58 // supported types (other types are converted to these if possible)
59 enum class VarType : int
60 {
61 Int32 = 0,
62 Float = 1
63 };
64 static VarType str2vtype( std::string_view s );
65
66 struct ResultVar
67 {
68 ResultVar() {}
69 std::variant<int32_t, float> value = { 0 };
70 UA_StatusCode status;
71 VarType type = { VarType::Int32 }; // by default
72
73 bool statusOk();
74
75 // get as int32_t (cast to int32_t if possible)
76 int32_t get();
77
78 template<class VType>
79 VType as()
80 {
81 try
82 {
83 return std::get<VType>(value);
84 }
85 catch(const std::bad_variant_access&) {}
86
87 return {};
88 }
89 };
90
91 using ErrorCode = unsigned int;
92
93 ErrorCode read( std::vector<UA_ReadValueId>& attrs, std::vector<ResultVar>& result );
94 ErrorCode write32( std::vector<UA_WriteValue>& values );
95 ErrorCode write32( const std::string& attr, int32_t value );
96 ErrorCode set( const std::string& attr, bool set );
97 ErrorCode write( const UA_WriteValue& val );
98 static UA_WriteValue makeWriteValue32( const std::string& name, int32_t val );
99 static UA_ReadValueId makeReadValue32( const std::string& name );
100
101 void onSessionActivated(opcua::StateCallback callback)
102 {
103 auto& exceptionCatcher = opcua::detail::getContext(client).exceptionCatcher;
104 client.onSessionActivated(exceptionCatcher.wrapCallback(std::move(callback)));
105 }
106
107 void runIterate(uint16_t timeoutMilliseconds)
108 {
109 client.runIterate(timeoutMilliseconds);
110 }
111
112 opcua::Subscription<opcua::Client> createSubscription()
113 {
114 return client.createSubscription();
115 }
116
117 void rethrowException()
118 {
119 auto& exceptionCatcher = opcua::detail::getContext(client).exceptionCatcher;
120 exceptionCatcher.rethrow(); // Работает только один раз, после повторной отправки удаляется!
121 }
122
123 std::vector<opcua::MonitoredItem<opcua::Client>> subscribeDataChanges(
124 opcua::Subscription<opcua::Client>& sub,
125 std::vector<UA_ReadValueId>& attrs,
126 std::vector<uniset::DataChangeCallback>& callbacks,
127 std::vector<uniset::DeleteMonitoredItemCallback>& delete_callbacks,
128 bool stop);
129
130 inline size_t getSubscriptionSize()
131 {
132 return monitoredItems.size();
133 }
134
135 protected:
136
137 opcua::Client client;
138 UA_Variant* val = { nullptr };
139
140 using SubMonId = std::pair<uint32_t, uint32_t>;
141
142 std::map<SubMonId, std::unique_ptr<uniset::MonitoredItem>> monitoredItems;
143
144 private:
145 };
146 // --------------------------------------------------------------------------
147} // end of namespace uniset
148// -----------------------------------------------------------------------------
149#endif // OPCUAClient_H_
150// -----------------------------------------------------------------------------
Definition OPCUAClient.h:49
Definition Calibration.h:27
Definition OPCUAClient.h:35
Definition OPCUAClient.h:67