UniSet 2.41.2
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#include "USingleProcess.h"
32// --------------------------------------------------------------------------
33namespace uniset
34{
35 // -----------------------------------------------------------------------------
165 // -----------------------------------------------------------------------------
167 class OPCUAServer:
168 private USingleProcess,
169 public UObject_SK
170 {
171 public:
172 OPCUAServer(uniset::ObjectId objId, xmlNode* cnode, uniset::ObjectId shmID,
173 const std::shared_ptr<SharedMemory>& ic = nullptr,
174 const std::string& prefix = "opcua");
175
176 virtual ~OPCUAServer();
177
178 virtual CORBA::Boolean exist() override;
179
181 static std::shared_ptr<OPCUAServer> init_opcua_server(int argc, const char* const* argv,
182 uniset::ObjectId shmID,
183 const std::shared_ptr<SharedMemory>& ic = nullptr,
184 const std::string& prefix = "opcua");
185
187 static void help_print();
188
189 using DefaultValueType = int32_t;
190 using DefaultValueUType = uint32_t;
191 static const opcua::DataTypeId DefaultVariableType = { opcua::DataTypeId::Int32 };
192
193 static uint8_t firstBit( DefaultValueUType mask );
194 // offset = firstBit(mask)
195 static DefaultValueUType getBits( DefaultValueUType value, DefaultValueUType mask, uint8_t offset );
196 // if mask = 0 return value
197 static DefaultValueUType setBits( DefaultValueUType value, DefaultValueUType set, DefaultValueUType mask, uint8_t offset );
198 // if mask=0 return set
199 static DefaultValueUType forceSetBits( DefaultValueUType value, DefaultValueUType set, DefaultValueUType mask, uint8_t offset );
200
201 static UA_StatusCode UA_setValueMethod(UA_Server* server, const UA_NodeId* sessionId, void* sessionHandle,
202 const UA_NodeId* methodId, void* methodContext, const UA_NodeId* objectId,
203 void* objectContext, size_t inputSize, const UA_Variant* input, size_t outputSize, UA_Variant* output);
204
205#ifndef DISABLE_REST_API
206 // HTTP API
207 virtual Poco::JSON::Object::Ptr httpHelp( const Poco::URI::QueryParameters& p ) override;
208 virtual Poco::JSON::Object::Ptr httpRequest( const std::string& req, const Poco::URI::QueryParameters& p ) override;
209#endif
210
211 protected:
212 OPCUAServer();
213
214 virtual void callback() noexcept override;
215 virtual void sysCommand(const uniset::SystemMessage* sm) override;
216 virtual bool deactivateObject() override;
217 virtual void askSensors(UniversalIO::UIOCommand cmd) override;
218 virtual void sensorInfo(const uniset::SensorMessage* sm) override;
219 virtual std::string getMonitInfo() const override;
220 void serverLoopTerminate();
221 void serverLoop();
222 void updateLoop();
223 void update();
224
225#ifndef DISABLE_REST_API
226 // params
227 virtual Poco::JSON::Object::Ptr httpGetParam( const Poco::URI::QueryParameters& p );
228 virtual Poco::JSON::Object::Ptr httpSetParam( const Poco::URI::QueryParameters& p );
229
230 // status
231 Poco::JSON::Object::Ptr httpStatus();
232
233 // Защитный флаг: разрешить/запретить /setparam (по аналогии с MBExchange/MBSlave)
234 bool httpEnabledSetParams { true };
235#endif
236 bool initVariable(UniXML::iterator& it);
237 bool readItem(const std::shared_ptr<UniXML>& xml, UniXML::iterator& it, xmlNode* sec);
238 void readConfiguration();
239
240 std::shared_ptr<SMInterface> shm;
241 std::unique_ptr<ThreadCreator<OPCUAServer>> serverThread;
242 std::unique_ptr<ThreadCreator<OPCUAServer>> updateThread;
243
244 struct IOVariable
245 {
246 IOVariable(const opcua::Node<opcua::Server>& n) : node(n) {};
247 opcua::Node<opcua::Server> node;
248 IOController::IOStateList::iterator it;
249 UniversalIO::IOType stype = { UniversalIO::AO };
250
252 DefaultValueType value = { 0 };
253 bool state = { false };
254 DefaultValueUType mask = { 0 };
255 uint8_t offset = { 0 };
256 opcua::DataTypeId vtype = { DefaultVariableType };
257 uint8_t precision = { 0 }; // only for float
258 };
259
260 std::unordered_map<ObjectId, IOVariable> variables;
261 size_t writeCount = { 0 };
262
263 struct IONode
264 {
265 opcua::Node<opcua::Server> node;
266 IONode( const opcua::Node<opcua::Server>& n ): node(n) {};
267 };
268
269 struct IOMethod
270 {
271 IOMethod( uniset::ObjectId _sid) : sid(_sid) {};
272
273 IOController::IOStateList::iterator it;
275 uint8_t precision = { 0 }; // only for float
276 };
277
278 std::unordered_map<uint32_t, IOMethod> methods;
279 size_t methodCount = { 0 };
280
281 private:
282 std::unique_ptr<opcua::Server> opcServer = { nullptr };
283 std::unique_ptr<IONode> ioNode = { nullptr };
284 std::string prefix;
285 std::string propPrefix;
286 std::string s_field;
287 std::string s_fvalue;
288 std::optional<std::regex> s_fvalue_re;
289 std::string namePrefix;
290 uniset::timeout_t updateTime_msec = { 100 };
291 std::atomic_bool firstUpdate = false;
292
293 using folderMap = std::unordered_map<std::string, std::unique_ptr<IONode>>;
294 folderMap foldermap; // список тегов
295 void initFolderMap( uniset::UniXML::iterator it, const std::string& parent_name, std::unique_ptr<IONode>& parent );
296 };
297 // --------------------------------------------------------------------------
299 constexpr std::string_view getLogLevelName(opcua::LogLevel level)
300 {
301 switch (level)
302 {
303 case opcua::LogLevel::Trace:
304 return "trace";
305
306 case opcua::LogLevel::Debug:
307 return "debug";
308
309 case opcua::LogLevel::Info:
310 return "info";
311
312 case opcua::LogLevel::Warning:
313 return "warning";
314
315 case opcua::LogLevel::Error:
316 return "error";
317
318 case opcua::LogLevel::Fatal:
319 return "fatal";
320
321 default:
322 return "unknown";
323 }
324 }
325
327 constexpr std::string_view getLogCategoryName(opcua::LogCategory category)
328 {
329 switch (category)
330 {
331 case opcua::LogCategory::Network:
332 return "network";
333
334 case opcua::LogCategory::SecureChannel:
335 return "channel";
336
337 case opcua::LogCategory::Session:
338 return "session";
339
340 case opcua::LogCategory::Server:
341 return "server";
342
343 case opcua::LogCategory::Client:
344 return "client";
345
346 case opcua::LogCategory::Userland:
347 return "userland";
348
349 case opcua::LogCategory::SecurityPolicy:
350 return "securitypolicy";
351
352 default:
353 return "unknown";
354 }
355 }
356} // end of namespace uniset
357// -----------------------------------------------------------------------------
358#endif // _OPCUAServer_H_
359// -----------------------------------------------------------------------------
virtual void callback() noexcept override
Определения OPCUAServer.cc:508
virtual std::string getMonitInfo() const override
Определения OPCUAServer.cc:872
virtual bool deactivateObject() override
Деактивация объекта (переопределяется для необходимых действий при завершении работы).
Определения OPCUAServer.cc:571
static void help_print()
Определения OPCUAServer.cc:593
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")
Определения OPCUAServer.cc:630
Определения MessageType.h:127
Определения MessageType.h:171
Определения Mutex.h:32
Определения Calibration.h:27
constexpr std::string_view getLogCategoryName(opcua::LogCategory category)
Get name of log category.
Определения OPCUAServer.h:327
constexpr std::string_view getLogLevelName(opcua::LogLevel level)
Get name of log level.
Определения OPCUAServer.h:299
const ObjectId DefaultObjectId
Определения UniSetTypes.h:71
long ObjectId
Определения UniSetTypes_i.idl:30