UniSet 2.32.1
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 <variant>
23#include <open62541/client_config_default.h>
24#include "Exceptions.h"
25//--------------------------------------------------------------------------
26namespace uniset
27{
28 // -----------------------------------------------------------------------------
31 {
32 public:
34 virtual ~OPCUAClient();
35
36 bool connect( const std::string& addr );
37 bool connect( const std::string& addr, const std::string& user, const std::string& pass );
38
39 // supported types (other types are converted to these if possible)
40 enum class VarType : int
41 {
42 Int32 = 0,
43 Float = 1
44 };
45 static VarType str2vtype( std::string_view s );
46
47 struct ResultVar
48 {
49 ResultVar() {}
50 std::variant<int32_t, float> value = { 0 };
51 UA_StatusCode status;
52 VarType type = { VarType::Int32 }; // by default
53
54 bool statusOk();
55
56 // get as int32_t (cast to int32_t if possible)
57 int32_t get();
58
59 template<class VType>
60 VType as()
61 {
62 try
63 {
64 return std::get<VType>(value);
65 }
66 catch(const std::bad_variant_access&) {}
67
68 return {};
69 }
70 };
71
72 using ErrorCode = int;
73
74 ErrorCode read( std::vector<UA_ReadValueId>& attrs, std::vector<ResultVar>& result );
75 ErrorCode write32( std::vector<UA_WriteValue>& values );
76 ErrorCode write32( const std::string& attr, int32_t value );
77 ErrorCode set( const std::string& attr, bool set );
78 ErrorCode write( const UA_WriteValue& val );
79 static UA_WriteValue makeWriteValue32( const std::string& name, int32_t val );
80 static UA_ReadValueId makeReadValue32( const std::string& name );
81
82 protected:
83 UA_Client* client = { nullptr };
84 UA_Variant* val = { nullptr };
85 UA_SessionState ss;
86
87 private:
88 };
89 // --------------------------------------------------------------------------
90} // end of namespace uniset
91// -----------------------------------------------------------------------------
92#endif // OPCUAClient_H_
93// -----------------------------------------------------------------------------
Definition OPCUAClient.h:31
Definition Calibration.h:27
Definition OPCUAClient.h:48