subscription.h
Go to the documentation of this file.
00001 /******************************************************************************
00002  *   Copyright (C) 2014-2014 Olivier Roulet-Dubonnet          *
00003  *   olivier.roulet@gmail.com          *
00004  *                      *
00005  *   This library is free software; you can redistribute it and/or modify   *
00006  *   it under the terms of the GNU Lesser General Public License as      *
00007  *   published by the Free Software Foundation; version 3 of the License.   *
00008  *                      *
00009  *   This library is distributed in the hope that it will be useful,      *
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of      *
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the      *
00012  *   GNU Lesser General Public License for more details.        *
00013  *                      *
00014  *   You should have received a copy of the GNU Lesser General Public License *
00015  *   along with this library; if not, write to the          *
00016  *   Free Software Foundation, Inc.,              *
00017  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.        *
00018  ******************************************************************************/
00019 
00020 #pragma once
00021 
00022 #include <opc/ua/global.h>
00023 #include <opc/ua/node.h>
00024 #include <opc/ua/event.h>
00025 #include <opc/ua/services/subscriptions.h>
00026 
00027 #include <sstream>
00028 #include <map>
00029 #include <mutex>
00030 
00031 #include <iostream> //debug
00032 
00033 namespace OpcUa
00034 {
00035   // the better void pointer
00036   struct UserData
00037   {
00038   };
00039   
00040   struct MonitoredItemData
00041   {
00042     IntegerId MonitoredItemId;
00043     Node TargetNode;
00044     AttributeId Attribute;
00045     MonitoringFilter Filter;
00046     UserData *usrVar;
00047   };
00048 
00049   typedef std::map<uint32_t, MonitoredItemData> AttValMap;
00050   typedef std::map<uint32_t, EventFilter> SimpleAttOpMap;
00051 
00052   class SubscriptionHandler
00053   {
00054     public:
00055       virtual ~SubscriptionHandler() {}
00056       //Called for each datachange events
00057       virtual void DataChange(uint32_t handle, const Node& node, const Variant& val, AttributeId attribute)
00058       {
00059         OPCUA_UNUSED(handle);
00060         OPCUA_UNUSED(node);
00061         OPCUA_UNUSED(val);
00062         OPCUA_UNUSED(attribute);
00063         std::cout << "default dc" << std::endl;
00064       }
00065       //Called for each datachange events
00066       // Same as DataChange(), but it provides whole DataValue type with aditional fields like time stamps
00067       virtual void DataValueChange(uint32_t handle, const Node& node, const DataValue& val, AttributeId attribute)
00068       {
00069         OPCUA_UNUSED(handle);
00070         OPCUA_UNUSED(node);
00071         OPCUA_UNUSED(val);
00072         OPCUA_UNUSED(attribute);
00073       }
00074       //Called for every events receive from server
00075       virtual void Event(uint32_t handle, const Event& event)
00076       {
00077         OPCUA_UNUSED(handle);
00078         OPCUA_UNUSED(event);
00079         std::cout << "default c++ event callback has been called" << std::endl;
00080       }
00081       //Called at server state changed
00082       virtual void StatusChange(StatusCode status)
00083       {
00084         OPCUA_UNUSED(status);
00085       }
00086   };
00087 
00088 
00089   class Subscription
00090   {
00091     public:
00092       //Create a new subscription on server
00093       //methods of callback object will be called everytime an event is received from the server
00094       //FIXME: should we use interface or std::function for callback???? std::function syntax is ugly but is more flexible
00095       //Alternative could be
00096       //AddDataChangeCallback(std::function<const Node&, const Variuant& val, AttributeId> callback);
00097       //AddEventCallback(std::function<std::vector<Variant>> callback);
00098       Subscription(Services::SharedPtr server, const CreateSubscriptionParameters& params, SubscriptionHandler& callback, bool debug=false);
00099       virtual ~Subscription() {}
00100 
00101       //Delete the subscription from server
00102       void Delete();
00103 
00104       //Get information about the subscription
00105       SubscriptionData GetData() {return Data; }
00106       uint32_t GetId() const { return Data.SubscriptionId; }
00107       double GetPeriode() const { return Data.RevisedPublishingInterval; } 
00108 
00109       //Subscribe to a Node attribute for its value to change
00110       // Subscribe to nodes for specified attribute change
00111       uint32_t SubscribeDataChange(const Node& node, AttributeId attr=AttributeId::Value);
00112       std::vector<uint32_t> SubscribeDataChange(const std::vector<ReadValueId>& attributes);
00113       
00114       // UserData pointer to have acces to user data in callback functions DataChange(),DataValueChange()
00115       void setUsrPtr(uint32_t handle,UserData *usr);
00116       UserData* getUsrPtr(uint32_t handle);
00117 
00118       //Unsubscribe to datachange or events
00119       void UnSubscribe(uint32_t handle); 
00120       void UnSubscribe(std::vector<uint32_t> handles); 
00121 
00122       //Subscribe to Events for given node
00123       //As far as I remember the only allowed node is Server in most SDKs
00124       uint32_t SubscribeEvents(const Node& node, const EventFilter& eventfilter); 
00125       uint32_t SubscribeEvents(const Node& node, const Node& eventType); //subscribe to all variables og given event type 
00126       uint32_t SubscribeEvents(); //subscribe to variables of baseEventTypes and ServerNode 
00127 
00128       // Subscribe using a MonitoredItemCreateRequest
00129       // This method allow to fully customize the subscription
00130       std::vector<MonitoredItemCreateResult> Subscribe(std::vector<MonitoredItemCreateRequest> request);
00131       
00132       // Override this method if you want raw publish results from server
00133       // for example if you want to make sure you do not miss any packets, etc, ...
00134       virtual void PublishCallback( Services::SharedPtr serverLocalPtr, const PublishResult result); 
00135 
00136       //Request republish of a notification from server
00137       //SequenceNumber are send by server in PublishResult struct
00138       RepublishResponse Republish(uint32_t sequenceNumber);
00139 
00140 
00141 
00142     private:
00143       void CallDataChangeCallback(const NotificationData& data);
00144       void CallEventCallback(const NotificationData& data);
00145       void CallStatusChangeCallback(const NotificationData& data);
00146 
00147       Services::SharedPtr Server;
00148       SubscriptionData Data;
00149       SubscriptionHandler& Client;
00150       uint32_t LastMonitoredItemHandle = 1;
00151       AttValMap AttributeValueMap; 
00152       SimpleAttOpMap SimpleAttributeOperandMap; //Not used currently
00153       std::mutex Mutex;
00154       bool Debug;
00155   };
00156 }
00157 
00158 


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Sat Jun 8 2019 18:24:57