UniSet 2.32.1
OPCUAServer.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 _OPCUAServer_H_
18#define _OPCUAServer_H_
19// -----------------------------------------------------------------------------
20#include <memory>
21#include <atomic>
22#include <regex>
23#include <optional>
24#include <unordered_map>
25#include "open62541pp/open62541pp.h"
26#include "UObject_SK.h"
27#include "SMInterface.h"
28#include "SharedMemory.h"
29#include "ThreadCreator.h"
30#include "Extensions.h"
31// --------------------------------------------------------------------------
32namespace uniset
33{
34 // -----------------------------------------------------------------------------
164 // -----------------------------------------------------------------------------
167 public UObject_SK
168 {
169 public:
170 OPCUAServer(uniset::ObjectId objId, xmlNode* cnode, uniset::ObjectId shmID,
171 const std::shared_ptr<SharedMemory>& ic = nullptr,
172 const std::string& prefix = "opcua");
173
174 virtual ~OPCUAServer();
175
176 virtual CORBA::Boolean exist() override;
177
179 static std::shared_ptr<OPCUAServer> init_opcua_server(int argc, const char* const* argv,
180 uniset::ObjectId shmID,
181 const std::shared_ptr<SharedMemory>& ic = nullptr,
182 const std::string& prefix = "opcua");
183
185 static void help_print();
186
187 using DefaultValueType = int32_t;
188 using DefaultValueUType = uint32_t;
189 static const opcua::Type DefaultVariableType = { opcua::Type::Int32 };
190
191 static uint8_t firstBit( DefaultValueUType mask );
192 // offset = firstBit(mask)
193 static DefaultValueUType getBits( DefaultValueUType value, DefaultValueUType mask, uint8_t offset );
194 // if mask = 0 return value
195 static DefaultValueUType setBits( DefaultValueUType value, DefaultValueUType set, DefaultValueUType mask, uint8_t offset );
196 // if mask=0 return set
197 static DefaultValueUType forceSetBits( DefaultValueUType value, DefaultValueUType set, DefaultValueUType mask, uint8_t offset );
198
199 static UA_StatusCode UA_setValueMethod(UA_Server* server, const UA_NodeId* sessionId, void* sessionHandle,
200 const UA_NodeId* methodId, void* methodContext, const UA_NodeId* objectId,
201 void* objectContext, size_t inputSize, const UA_Variant* input, size_t outputSize, UA_Variant* output);
202
203 protected:
204 OPCUAServer();
205
206 virtual void callback() noexcept override;
207 virtual void sysCommand(const uniset::SystemMessage* sm) override;
208 virtual bool deactivateObject() override;
209 virtual void askSensors(UniversalIO::UIOCommand cmd) override;
210 virtual void sensorInfo(const uniset::SensorMessage* sm) override;
211 virtual std::string getMonitInfo() const override;
212 void serverLoopTerminate();
213 void serverLoop();
214 void updateLoop();
215 void update();
216
217 bool initVariable(UniXML::iterator& it);
218 bool readItem(const std::shared_ptr<UniXML>& xml, UniXML::iterator& it, xmlNode* sec);
219 void readConfiguration();
220
221 std::shared_ptr<SMInterface> shm;
222 std::unique_ptr<ThreadCreator<OPCUAServer>> serverThread;
223 std::unique_ptr<ThreadCreator<OPCUAServer>> updateThread;
224
226 {
227 IOVariable(const opcua::Node<opcua::Server>& n) : node(n) {};
228 opcua::Node<opcua::Server> node;
229 IOController::IOStateList::iterator it;
230 UniversalIO::IOType stype = { UniversalIO::AO };
231
233 DefaultValueType value = { 0 };
234 bool state = { false };
235 DefaultValueUType mask = { 0 };
236 uint8_t offset = { 0 };
237 opcua::Type vtype = { DefaultVariableType };
238 uint8_t precision = { 0 }; // only for float
239 };
240
241 std::unordered_map<ObjectId, IOVariable> variables;
242 size_t writeCount = { 0 };
243
244 struct IONode
245 {
246 opcua::Node<opcua::Server> node;
247 IONode( const opcua::Node<opcua::Server>& n ): node(n) {};
248 };
249
250 struct IOMethod
251 {
252 IOMethod( uniset::ObjectId _sid) : sid(_sid) {};
253
254 IOController::IOStateList::iterator it;
256 uint8_t precision = { 0 }; // only for float
257 };
258
259 std::unordered_map<uint32_t, IOMethod> methods;
260 size_t methodCount = { 0 };
261
262 private:
263 std::unique_ptr<opcua::Server> opcServer = { nullptr };
264 std::unique_ptr<IONode> ioNode = { nullptr };
265 std::string prefix;
266 std::string propPrefix;
267 std::string s_field;
268 std::string s_fvalue;
269 std::optional<std::regex> s_fvalue_re;
270 std::string namePrefix;
271 uniset::timeout_t updateTime_msec = { 100 };
272 std::atomic_bool firstUpdate = false;
273
274 using folderMap = std::unordered_map<std::string, std::unique_ptr<IONode>>;
275 folderMap foldermap; // список тегов
276 void initFolderMap( uniset::UniXML::iterator it, const std::string& parent_name, std::unique_ptr<IONode>& parent );
277 };
278 // --------------------------------------------------------------------------
279} // end of namespace uniset
280// -----------------------------------------------------------------------------
281#endif // _OPCUAServer_H_
282// -----------------------------------------------------------------------------
Definition UObject_SK.h:30
Definition OPCUAServer.h:168
virtual void callback() noexcept override
Definition OPCUAServer.cc:502
virtual std::string getMonitInfo() const override
Definition OPCUAServer.cc:862
virtual bool deactivateObject() override
Деактивация объекта (переопределяется для необходимых действий при завершении работы)
Definition OPCUAServer.cc:565
static void help_print()
Definition OPCUAServer.cc:587
static std::shared_ptr< OPCUAServer > init_opcua_server(int argc, const char *const *argv, uniset::ObjectId shmID, const std::shared_ptr< SharedMemory > &ic=nullptr, const std::string &prefix="opcua")
Definition OPCUAServer.cc:620
Definition MessageType.h:127
Definition MessageType.h:171
Definition UniXML.h:44
Definition Mutex.h:32
Definition Calibration.h:27
const ObjectId DefaultObjectId
Definition UniSetTypes.h:71
long ObjectId
Definition UniSetTypes_i.idl:30
Definition OPCUAServer.h:251
Definition OPCUAServer.h:245
Definition OPCUAServer.h:226