subscription.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (C) 2014-2014 Olivier Roulet-Dubonnet *
3  * olivier.roulet@gmail.com *
4  * *
5  * This library is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU Lesser General Public License as *
7  * published by the Free Software Foundation; version 3 of the License. *
8  * *
9  * This library is distributed in the hope that it will be useful, *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12  * GNU Lesser General Public License for more details. *
13  * *
14  * You should have received a copy of the GNU Lesser General Public License *
15  * along with this library; if not, write to the *
16  * Free Software Foundation, Inc., *
17  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18  ******************************************************************************/
19 
20 #pragma once
21 
22 #include <opc/common/logger.h>
23 #include <opc/ua/global.h>
24 #include <opc/ua/node.h>
25 #include <opc/ua/event.h>
27 
28 #include <sstream>
29 #include <map>
30 #include <mutex>
31 
32 #include <iostream> //debug
33 
34 namespace OpcUa
35 {
36 // the better void pointer
37 struct UserData
38 {
39 };
40 
42 {
48 };
49 
50 typedef std::map<uint32_t, MonitoredItemData> AttValMap;
51 typedef std::map<uint32_t, EventFilter> SimpleAttOpMap;
52 
54 {
55 public:
56  virtual ~SubscriptionHandler() {}
57  //Called for each datachange events
58  virtual void DataChange(uint32_t handle, const Node & node, const Variant & val, AttributeId attribute)
59  {
60  OPCUA_UNUSED(handle);
61  OPCUA_UNUSED(node);
62  OPCUA_UNUSED(val);
63  OPCUA_UNUSED(attribute);
64  std::cout << "default dc" << std::endl;
65  }
66  //Called for each datachange events
67  // Same as DataChange(), but it provides whole DataValue type with aditional fields like time stamps
68  virtual void DataValueChange(uint32_t handle, const Node & node, const DataValue & val, AttributeId attribute)
69  {
70  OPCUA_UNUSED(handle);
71  OPCUA_UNUSED(node);
72  OPCUA_UNUSED(val);
73  OPCUA_UNUSED(attribute);
74  }
75  //Called for every events receive from server
76  virtual void Event(uint32_t handle, const Event & event)
77  {
78  OPCUA_UNUSED(handle);
79  OPCUA_UNUSED(event);
80  std::cout << "default c++ event callback has been called" << std::endl;
81  }
82  //Called at server state changed
83  virtual void StatusChange(StatusCode status)
84  {
85  OPCUA_UNUSED(status);
86  }
87 };
88 
89 
91 {
92 public:
94 
95 public:
96  //Create a new subscription on server
97  //methods of callback object will be called everytime an event is received from the server
98  //FIXME: should we use interface or std::function for callback???? std::function syntax is ugly but is more flexible
99  //Alternative could be
100  //AddDataChangeCallback(std::function<const Node&, const Variuant& val, AttributeId> callback);
101  //AddEventCallback(std::function<std::vector<Variant>> callback);
102  Subscription(Services::SharedPtr server, const CreateSubscriptionParameters & params, SubscriptionHandler & callback, const Common::Logger::SharedPtr & logger = nullptr);
103  virtual ~Subscription() {}
104 
105  //Delete the subscription from server
106  void Delete();
107 
108  //Get information about the subscription
110  uint32_t GetId() const { return Data.SubscriptionId; }
111  double GetPeriode() const { return Data.RevisedPublishingInterval; }
112 
113  //Subscribe to a Node attribute for its value to change
114  // Subscribe to nodes for specified attribute change
115  uint32_t SubscribeDataChange(const Node & node, AttributeId attr = AttributeId::Value);
116  std::vector<uint32_t> SubscribeDataChange(const std::vector<ReadValueId> & attributes);
117 
118  // UserData pointer to have acces to user data in callback functions DataChange(),DataValueChange()
119  void setUsrPtr(uint32_t handle, UserData * usr);
120  UserData * getUsrPtr(uint32_t handle);
121 
122  //Unsubscribe to datachange or events
123  void UnSubscribe(uint32_t handle);
124  void UnSubscribe(std::vector<uint32_t> handles);
125 
126  //Subscribe to Events for given node
127  //As far as I remember the only allowed node is Server in most SDKs
128  uint32_t SubscribeEvents(const Node & node, const EventFilter & eventfilter);
129  uint32_t SubscribeEvents(const Node & node, const Node & eventType); //subscribe to all variables og given event type
130  uint32_t SubscribeEvents(); //subscribe to variables of baseEventTypes and ServerNode
131 
132  // Subscribe using a MonitoredItemCreateRequest
133  // This method allow to fully customize the subscription
134  std::vector<MonitoredItemCreateResult> Subscribe(std::vector<MonitoredItemCreateRequest> request);
135 
136  // Override this method if you want raw publish results from server
137  // for example if you want to make sure you do not miss any packets, etc, ...
138  virtual void PublishCallback(Services::SharedPtr serverLocalPtr, const PublishResult result);
139 
140  //Request republish of a notification from server
141  //SequenceNumber are send by server in PublishResult struct
142  RepublishResponse Republish(uint32_t sequenceNumber);
143 
144 
145 
146 private:
147  void CallDataChangeCallback(const NotificationData & data);
148  void CallEventCallback(const NotificationData & data);
149  void CallStatusChangeCallback(const NotificationData & data);
150 
151  Services::SharedPtr Server;
154  uint32_t LastMonitoredItemHandle = 1;
155  AttValMap AttributeValueMap;
156  SimpleAttOpMap SimpleAttributeOperandMap; //Not used currently
157  std::mutex Mutex;
158  Common::Logger::SharedPtr Logger;
159 };
160 }
161 
162 
MonitoringFilter Filter
Definition: subscription.h:46
std::map< uint32_t, MonitoredItemData > AttValMap
Definition: subscription.h:50
virtual void DataChange(uint32_t handle, const Node &node, const Variant &val, AttributeId attribute)
Definition: subscription.h:58
uint32_t GetId() const
Definition: subscription.h:110
virtual void Event(uint32_t handle, const Event &event)
Definition: subscription.h:76
virtual void DataValueChange(uint32_t handle, const Node &node, const DataValue &val, AttributeId attribute)
Definition: subscription.h:68
std::map< uint32_t, EventFilter > SimpleAttOpMap
Definition: subscription.h:51
#define OPCUA_UNUSED(x)
Definition: global.h:22
double GetPeriode() const
Definition: subscription.h:111
Services::SharedPtr Server
Definition: subscription.h:151
SimpleAttOpMap SimpleAttributeOperandMap
Definition: subscription.h:156
virtual ~Subscription()
Definition: subscription.h:103
handle
Definition: client.py:58
AttValMap AttributeValueMap
Definition: subscription.h:155
OPC UA Address space part. GNU LGPL.
SubscriptionHandler & Client
Definition: subscription.h:153
Common::Logger::SharedPtr Logger
Definition: subscription.h:158
A Node object represent an OPC-UA node. It is high level object intended for developper who want to e...
Definition: node.h:42
#define DEFINE_CLASS_POINTERS(ClassName)
Exception declarations GNU LGPL.
BasicData Data
Definition: format.h:993
virtual void StatusChange(StatusCode status)
Definition: subscription.h:83
SubscriptionData GetData()
Definition: subscription.h:109
SubscriptionData Data
Definition: subscription.h:152
Definition: server.py:1


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:12:08