Esempio n. 1
0
 public void SubscribeTag(ITag TagToSubscribe, int EventMaxFrequency = 0, int MaxDelayToFireEvent = 0, Action <ITag> OnTagValueChangeAction = null)
 {
     if (!TagSubscriptionDictionary.ContainsKey(TagToSubscribe.Name))
     {
         OPCTag T = TagToSubscribe as OPCTag;
         TagSubscriptionDictionary.Add(TagToSubscribe.Name, TagToSubscribe as OPCTag);
         T.SetToSubscription(EventMaxFrequency, MaxDelayToFireEvent, OnTagValueChangeAction);
     }
     CreateSubscription();
 }
Esempio n. 2
0
        public bool WriteTag(string tagName, object tagValue)
        {
            OPCTag T = (OPCTag)GetTag(tagName);

            if (T != null)
            {
                return(T.WriteItem(tagValue));
            }
            else
            {
                return(false);
            }
        }
Esempio n. 3
0
        public object ReadTag(string tagName)
        {
            OPCTag T = (OPCTag)GetTag(tagName);

            if (T != null)
            {
                if (T.ReadItem())
                {
                    return(T.Value);
                }
            }
            return(null);
        }
Esempio n. 4
0
 public ITag AddTag(string TagName, string TagAddress, Type TagType, int?TagTypeSize = null, int ElementN = 1)
 {
     if (!_TagDictionary.ContainsKey(TagName))
     {
         OPCTag T = new OPCTag(this, TagName, TagAddress, TagType);
         _TagDictionary.Add(TagName, T);
         return(T);
     }
     else
     {
         return(_TagDictionary[TagName]);
     }
 }
Esempio n. 5
0
        public void StartCyclicalRefresh(List <string> TagsToReadName, int Interval = 100, Action <List <ITag> > OnTagCyclicalRefreshAction = null)
        {
            List <ITag> TagsToRead = new List <ITag>();

            foreach (string item in TagsToReadName)
            {
                OPCTag T = (OPCTag)GetTag(item);
                if (T != null)
                {
                    TagsToRead.Add(T);
                }
            }
            StartCyclicalRefresh(TagsToRead, Interval, OnTagCyclicalRefreshAction);
        }
Esempio n. 6
0
        public void UnsubscribeTags(List <ITag> TagsToUnSubscribe)
        {
            if (TagsToUnSubscribe.Count > 0)
            {
                RemoveSubscription(TagsToUnSubscribe);

                Logger?.LogDebug($"OPC - Tag Subscription - Removed from subscription {TagsToUnSubscribe.Count()} Tags. Total tags in subscription: {TagSubscriptionDictionary.Count()}");

                foreach (var item in TagsToUnSubscribe)
                {
                    OPCTag T = item.Value as OPCTag;
                    TagSubscriptionDictionary.Remove(T.Name);
                    T.RemoveFromSubscription();
                }
            }
        }
Esempio n. 7
0
 public void SubscribeTags(List <string> TagsToSubscribeNames, int EventMaxFrequency = 0, int MaxDelayToFireEvent = 0, Action <ITag> OnTagValueChangeAction = null)
 {
     foreach (string item in TagsToSubscribeNames)
     {
         if (!TagSubscriptionDictionary.ContainsKey(item))
         {
             OPCTag T = (OPCTag)GetTag(item);
             if (T != null)
             {
                 TagSubscriptionDictionary.Add(T.Name, T);
                 T.SetToSubscription(EventMaxFrequency, MaxDelayToFireEvent, OnTagValueChangeAction);
             }
         }
     }
     CreateSubscription();
 }
Esempio n. 8
0
        public bool WriteTags(List <string> tagNameList, List <object> objectList)
        {
            List <ITag> tagList = new List <ITag>();

            foreach (var item in tagNameList)
            {
                OPCTag T = (OPCTag)GetTag(item);
                if (T == null)
                {
                    Logger?.LogException($"OPC - Multiple write error. Tag: {item} not found in dictionary.");
                    return(false);
                }
                tagList.Add(T);
            }
            return(WriteTags(tagList, objectList));
        }
Esempio n. 9
0
 public void UnsubscribeTag(ITag TagToUnSubscribe)
 {
     if (TagSubscriptionDictionary.ContainsKey(TagToUnSubscribe.Name))
     {
         ////Remove tag from dictionary
         List <ITag> TagsToUnSubscribe = new List <ITag>()
         {
             TagToUnSubscribe
         };
         RemoveSubscription(TagsToUnSubscribe);
         OPCTag T = TagToUnSubscribe as OPCTag;
         T.RemoveFromSubscription();
         Logger?.LogDebug($"OPC - Tag Subscription - Removed from subscription Tag: {TagToUnSubscribe.Name}. Total tags in subscription: {TagSubscriptionDictionary.Count()}");
         TagSubscriptionDictionary.Remove(TagToUnSubscribe.Name);
     }
 }
Esempio n. 10
0
        private void CreateSubscription()
        {
            if (IsAvailable)
            {
                if (ClientSubscription == null)
                {
                    // initialize subscription.
                    ClientSubscription = new Subscription(ClientSession.DefaultSubscription)
                    {
                        PublishingInterval = 100
                    };
                    //ClientSubscription.MaxNotificationsPerPublish = 0;
                    //ClientSubscription.Priority = 0;
                    //ClientSubscription.PublishingEnabled = true;
                }
                List <MonitoredItem> MonitoredItem_List = new List <MonitoredItem>();
                foreach (var item in TagSubscriptionDictionary)
                {
                    OPCTag MyTag = item.Value as OPCTag;

                    MonitoredItem MyMonitoredItem = new MonitoredItem(ClientSubscription.DefaultItem)
                    {
                        DisplayName = MyTag.Name,
                        StartNodeId = MyTag.Address
                    };
                    MyMonitoredItem.Notification += MyTag.OnSubscriptionNotification;
                    MonitoredItem_List.Add(MyMonitoredItem);
                }

                ClientSubscription.AddItems(MonitoredItem_List);

                if (!IsSubscriptionActive)
                {
                    ClientSession.AddSubscription(ClientSubscription);
                    ClientSubscription.Create();
                    IsSubscriptionActive = true;
                }
                else
                {
                    ClientSubscription.Modify();
                }
            }
            else
            {
                Logger?.LogWarning($"OPC - Tag Subscription - Client current state is not available. Subscription will be created on next connection.");
            }
        }
Esempio n. 11
0
        public void UnsubscribeTags()
        {
            if (TagSubscriptionDictionary.Count > 0)
            {
                List <ITag> Tag_List = new List <ITag>();
                foreach (var item in TagSubscriptionDictionary)
                {
                    OPCTag T = item.Value as OPCTag;
                    Tag_List.Add(T);
                    T.RemoveFromSubscription();
                }
                RemoveSubscription(Tag_List);

                Logger?.LogDebug($"OPC - Tag Subscription - Removed all subscription Tags: {TagSubscriptionDictionary.Count()}.");
                TagSubscriptionDictionary.Clear();
            }
        }
Esempio n. 12
0
        public void SubscribeTags(List <ITag> TagsToSubscribe, int EventMaxFrequency = 0, int MaxDelayToFireEvent = 0, Action <ITag> OnTagValueChangeAction = null)
        {
            int SubscribedTagsN = 0;

            if (TagsToSubscribe.Count > 0)
            {
                foreach (var item in TagsToSubscribe)
                {
                    if (!TagSubscriptionDictionary.ContainsKey(item.Name))
                    {
                        OPCTag T = item as OPCTag;
                        TagSubscriptionDictionary.Add(T.Name, T);
                        T.SetToSubscription(EventMaxFrequency, MaxDelayToFireEvent, OnTagValueChangeAction);
                    }
                }
                CreateSubscription();
                Logger?.LogDebug($"OPC - Tag Subscription - Added to subscription: {SubscribedTagsN} Tags. Event max frequency:{EventMaxFrequency}. Total tags in subscription: {TagSubscriptionDictionary.Count()}");
            }
        }
Esempio n. 13
0
        public bool ReadTags(List <ITag> tagList)
        {
            try
            {
                if (!IsAvailable || !IsConnected)
                {
                    return(false);
                }


                List <NodeId>        NodeIdList        = new List <NodeId>();
                List <Type>          ExpectedTypesList = new List <Type>();
                List <object>        ObjectList        = new List <object>();
                List <ServiceResult> ServiceResultList = new List <ServiceResult>();
                foreach (var tag in tagList)
                {
                    NodeIdList.Add(new NodeId(tag.Address));
                    ExpectedTypesList.Add(tag.TagType);
                }

                ClientSession.ReadValues(NodeIdList, ExpectedTypesList, out ObjectList, out ServiceResultList);
                int  index   = 0;
                bool Success = true;
                foreach (var MyServiceResult in ServiceResultList)
                {
                    OPCTag tag = tagList[index] as OPCTag;
                    if (!tag.UpdateTag(ServiceResultList[index], ObjectList[index]))
                    {
                        Success = false;
                    }
                    index++;
                }
                return(Success);
            }
            catch (Exception ex)
            {
                Logger?.LogException($"OPC - Multiple read error. Ex: {ex.Message}.");
                return(false);
            }
        }
Esempio n. 14
0
        public void UnsubscribeTags(List <string> TagsToUnSubscribeNames)
        {
            List <ITag> Tag_List = new List <ITag>();

            if (TagsToUnSubscribeNames.Count > 0)
            {
                foreach (var itemName in TagsToUnSubscribeNames)
                {
                    OPCTag T = (OPCTag)this.GetTag(itemName);
                    Tag_List.Add(T);
                    T.RemoveFromSubscription();
                }

                //Remove tag from dictionary
                RemoveSubscription(Tag_List);
                Logger?.LogDebug($"OPC - Tag Subscription - Removed from subscription {TagsToUnSubscribeNames.Count()} Tags. Total tags in subscription: {TagSubscriptionDictionary.Count()}");
                foreach (var item in Tag_List)
                {
                    TagSubscriptionDictionary.Remove(item.Name);
                }
            }
        }
Esempio n. 15
0
        private void RemoveSubscription(List <ITag> TagsToUnSubscribe)
        {
            try
            {
                List <MonitoredItem> MonitoredItem_List = new List <MonitoredItem>();
                foreach (var item in TagsToUnSubscribe)
                {
                    OPCTag        MyTag           = item.Value as OPCTag;
                    MonitoredItem MyMonitoredItem = new MonitoredItem(ClientSubscription.DefaultItem)
                    {
                        DisplayName = MyTag.Name,
                        StartNodeId = MyTag.Address
                    };
                    MonitoredItem_List.Add(MyMonitoredItem);
                }

                ClientSubscription.RemoveItems(MonitoredItem_List);
            }
            catch (Exception ex)
            {
            }
        }
Esempio n. 16
0
        public bool WriteTags(List <ITag> tagList, List <object> objectList)
        {
            try
            {
                if (!IsAvailable || !IsConnected)
                {
                    return(false);
                }



                if (tagList.Count() != objectList.Count())
                {
                    Logger?.LogException($"OPC - WriteTags wrong values. Tags request: {tagList.Count()}. Value given: {objectList.Count()}.");
                    return(false);
                }



                WriteValueCollection nodesToWriteCollection = new WriteValueCollection();
                int       index = 0;
                DataValue val   = new DataValue();
                foreach (var tag in tagList)
                {
                    Type   varType       = tagList[index].TagType;
                    object varValue      = objectList[index];
                    var    MyValueCasted = Convert.ChangeType(varValue, varType);
                    val.Value = MyValueCasted;
                    nodesToWriteCollection.Add(new WriteValue()
                    {
                        NodeId      = new NodeId(tagList[index].Address),
                        AttributeId = Attributes.Value,
                        Value       = val
                    });
                    index++;
                }

                StatusCodeCollection     results;
                DiagnosticInfoCollection diagnosticInfos;

                RequestHeader requestHeader = new RequestHeader();
                //requestHeader.ReturnDiagnostics = 0;

                ResponseHeader RS = ClientSession.Write(
                    requestHeader,
                    nodesToWriteCollection,
                    out results,
                    out diagnosticInfos);

                bool Success = true;

                foreach (var result in results)
                {
                    if (result.Code != StatusCodes.Good)
                    {
                        Success = false;
                    }
                }

                index = 0;
                foreach (var item in results)
                {
                    OPCTag tag = tagList[index] as OPCTag;
                    if (!tag.UpdateTag(item))
                    {
                        Success = false;
                    }
                    index++;
                }
                return(Success);
            }
            catch (Exception ex)
            {
                Logger?.LogException($"OPC - Multiple read error. Ex: {ex.Message}.");
                return(false);
            }
        }