Beispiel #1
0
 private void CallBack_Write(IAsyncResult ar)
 {
     if (ar.IsCompleted)
     {
         TagItem                  tag            = null;
         string                   tagServerIndex = string.Empty;
         DataValueCollection      results;
         DiagnosticInfoCollection diagnosticinfos;
         WriteValueCollection     tmpReadIds     = ar.AsyncState as WriteValueCollection;
         ResponseHeader           responseHeader = uAClient.Session.EndRead(ar, out results, out diagnosticinfos);
         ClientBase.ValidateResponse(results, tmpReadIds);
         ClientBase.ValidateDiagnosticInfos(diagnosticinfos, tmpReadIds);
         for (int i = 0; i < results.Count; i++)
         {
             tagServerIndex = tmpReadIds[i].NodeId.Identifier.ToString();
             if (!_AsyncWriteTags.ContainsKey(tagServerIndex))
             {
                 continue;
             }
             tag           = _AsyncWriteTags[tmpReadIds[i].NodeId.Identifier.ToString()];
             tag.CBType    = TagItem.CallBackType.AsyncRead;
             tag.Quality   = results[i].StatusCode.ToString();
             tag.Value     = results[i].Value;
             tag.Timestamp = results[i].SourceTimestamp;
             tag.CallBack?.Invoke(tag);
             _AsyncWriteTags.Remove(tmpReadIds[i].NodeId.Identifier.ToString());
         }
     }
 }
Beispiel #2
0
        private void ItemChanged(MonitoredItem monitoreditem, MonitoredItemNotificationEventArgs e)
        {
            MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification;

            if (notification != null)
            {
                string nodeid = monitoreditem.StartNodeId.Identifier.ToString();
                if (!_SubscriptionTags.ContainsKey(nodeid))
                {
                    throw (new Exception(string.Format("订阅回调触发,但是_SubscriptionTags不包含键;key:{0}", nodeid)));
                }
                TagItem item = _SubscriptionTags[nodeid];
                if (item == null)
                {
                    throw (new Exception(string.Format("订阅回调触发,但是未找到键对应TagItem对象;key:{0},value:null", nodeid)));
                }
                if (item.CallBack == null)
                {
                    return;
                }
                item.CBType    = TagItem.CallBackType.Subscription;
                item.Value     = notification.Value.Value;
                item.Timestamp = notification.Value.SourceTimestamp;
                item.Quality   = notification.Value.StatusCode.ToString();
                item.CallBack(item);
            }
        }
Beispiel #3
0
        /// <summary>
        /// 异步读
        /// </summary>
        /// <param name="tag"></param>
        public void AsyncRead(TagItem tag)
        {
            List <TagItem> tags = new List <TagItem>();

            tags.Add(tag);
            AsyncRead(tags);
        }
Beispiel #4
0
 /// <inheritdoc />
 public bool AsyncWrite(TagItem tag)
 {
     AsyncWrite(new List <TagItem>()
     {
         tag
     });
     return(true);
 }
Beispiel #5
0
        /// <summary>
        /// 读一个变量
        /// </summary>
        /// <param name="tag"></param>
        /// <returns></returns>
        public TagItem Read(TagItem tag)
        {
            DataValue value = uAClient.ReadValue(tag.GetNodeId());

            tag.Value     = value.Value;
            tag.Quality   = value.StatusCode.ToString();
            tag.Timestamp = value.SourceTimestamp;
            return(tag);
        }
Beispiel #6
0
 /// <summary>
 /// 写变量
 /// </summary>
 /// <param name="tag"></param>
 /// <returns></returns>
 public bool Write(TagItem tag)
 {
     uAClient.WriteValues(new List <string>()
     {
         tag.ValueToWrite?.ToString()
     }, new List <string>()
     {
         tag.GetNodeId()
     });
     return(true);
 }
Beispiel #7
0
 /// <summary>
 /// 添加订阅
 /// </summary>
 /// <param name="tag"></param>
 /// <returns></returns>
 public bool AddSubscription(TagItem tag)
 {
     if (_TagItems.ContainsKey(tag.ServerId))
     {
         return(true);                                     //重复添加订阅,认为成功
     }
     _TagItems.Add(tag.ServerId, tag);
     return(_OpcDaGroup.AddItem(new Item()
     {
         Name = tag.ServerId
     }));
 }
Beispiel #8
0
        /// <summary>
        /// 添加订阅
        /// </summary>
        /// <param name="tag"></param>
        /// <returns></returns>
        public bool AddSubscription(TagItem tag)
        {
            MonitoredItem item = uAClient.AddMonitoredItem(_Subscription, tag.GetNodeId(), tag.Name, tag.SampleRate);

            _SubscriptionTags.Add(tag.ServerId, tag);
            _MonitorItem.Add(tag.ServerId, item);
            if (item.Created)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Beispiel #9
0
        /// <summary>
        /// 订阅内移除变量
        /// </summary>
        /// <param name="tag">要移除的变量</param>
        /// <returns></returns>
        public bool RemoveSubscription(TagItem tag)
        {
            MonitoredItem monitoredItem = uAClient.RemoveMonitoredItem(_Subscription, _MonitorItem[tag.ServerId]);

            if (_MonitorItem[tag.ServerId].Created == false)
            {
                _SubscriptionTags.Remove(tag.ServerId);
                _MonitorItem.Remove(tag.ServerId);
                return(true);
            }
            else
            {
                return(false);
            }
        }
Beispiel #10
0
        private void _OpcDaGroup_AsyncReadCompleteEvent(Item item)
        {
            TagItem tag = _TagItems[item.Name];

            if (tag == null)
            {
                return;
            }
            if (tag.CallBack == null)
            {
                return;
            }
            tag.Value     = item.Value;
            tag.Quality   = item.Quality.ToString();
            tag.Timestamp = DateTime.Parse(item.Timesnamp.ToString());
            tag.CBType    = TagItem.CallBackType.AsyncRead;
            tag.CallBack(tag);
        }
Beispiel #11
0
        private void _OpcDaGroup_DataChangeEvent(Item item)
        {
            TagItem tag = _TagItems[item.Name];

            if (tag == null)
            {
                return;
            }
            if (tag.CallBack == null)
            {
                return;
            }
            tag.Value     = item.Value;
            tag.Quality   = item.Quality.ToString();
            tag.Timestamp = DateTime.Parse(item.Timesnamp.ToString());
            tag.CBType    = TagItem.CallBackType.Subscription;
            tag.CallBack(tag);
        }
Beispiel #12
0
        /// <summary>
        /// 同步写
        /// </summary>
        /// <param name="tag"></param>
        /// <returns></returns>
        public bool Write(TagItem tag)
        {
            TagItem tagItem = _TagItems[tag.ServerId];
            Item    item    = _OpcDaGroup.ItemsEx[tag.ServerId];

            if (item == null)
            {
                AddSubscription(tag);
                item          = _OpcDaGroup.ItemsEx[tag.ServerId];
                item.ObjValue = tag.ValueToWrite;
                item.Write_NoRes();
                RemoveSubscription(tag);
            }
            else
            {
                item.ObjValue = tag.ValueToWrite;
                item.Write_NoRes();
            }
            return(true);
        }
Beispiel #13
0
        /// <summary>
        /// 同步读
        /// </summary>
        /// <param name="tag"></param>
        /// <returns></returns>
        public TagItem Read(TagItem tag)
        {
            TagItem tagItem = _TagItems[tag.ServerId];
            Item    item    = _OpcDaGroup.ItemsEx[tag.ServerId];

            if (item == null)
            {
                AddSubscription(tag);
                item = _OpcDaGroup.ItemsEx[tag.ServerId];
                item.Read();
                tagItem.Value = item.Value;
                RemoveSubscription(tag);
            }
            else
            {
                item.Read();
                tagItem.Value = item.Value;
            }

            return(tagItem);
        }
Beispiel #14
0
 /// <inheritdoc />
 public bool RemoveTag(TagItem tag)
 {
     _Tags.Remove(tag.ServerId);
     return(true);
 }
Beispiel #15
0
 /// <summary>
 /// 等同于移除订阅
 /// </summary>
 /// <param name="tag"></param>
 /// <returns></returns>
 public bool RemoveTag(TagItem tag)
 {
     return(RemoveSubscription(tag));
 }
Beispiel #16
0
 /// <summary>
 /// 等同于添加订阅
 /// </summary>
 /// <param name="tag"></param>
 /// <returns></returns>
 public bool AddTag(TagItem tag)
 {
     return(AddSubscription(tag));
 }
Beispiel #17
0
 /// <summary>
 /// 异步读
 /// </summary>
 /// <param name="tag"></param>
 public void AsyncRead(TagItem tag)
 {
     AsyncRead(new List <TagItem> {
         tag
     });
 }
Beispiel #18
0
 /// <summary>
 /// 取消订阅
 /// </summary>
 /// <param name="tag"></param>
 /// <returns></returns>
 public bool RemoveSubscription(TagItem tag)
 {
     _TagItems.Remove(tag.ServerId);
     return(_OpcDaGroup.RemoveItem(_OpcDaGroup.ItemsEx[tag.ServerId]).Count == 1);
 }
Beispiel #19
0
 /// <summary>
 /// 异步写
 /// </summary>
 /// <param name="tag"></param>
 /// <returns></returns>
 public bool AsyncWrite(TagItem tag)
 {
     return(false);
 }
Beispiel #20
0
 /// <inheritdoc />
 public bool AddTag(TagItem tag)
 {
     _Tags.Add(tag.ServerId, tag);
     return(true);
 }