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()); } } }
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); } }
/// <summary> /// 异步读 /// </summary> /// <param name="tag"></param> public void AsyncRead(TagItem tag) { List <TagItem> tags = new List <TagItem>(); tags.Add(tag); AsyncRead(tags); }
/// <inheritdoc /> public bool AsyncWrite(TagItem tag) { AsyncWrite(new List <TagItem>() { tag }); return(true); }
/// <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); }
/// <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); }
/// <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 })); }
/// <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); } }
/// <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); } }
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); }
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); }
/// <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); }
/// <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); }
/// <inheritdoc /> public bool RemoveTag(TagItem tag) { _Tags.Remove(tag.ServerId); return(true); }
/// <summary> /// 等同于移除订阅 /// </summary> /// <param name="tag"></param> /// <returns></returns> public bool RemoveTag(TagItem tag) { return(RemoveSubscription(tag)); }
/// <summary> /// 等同于添加订阅 /// </summary> /// <param name="tag"></param> /// <returns></returns> public bool AddTag(TagItem tag) { return(AddSubscription(tag)); }
/// <summary> /// 异步读 /// </summary> /// <param name="tag"></param> public void AsyncRead(TagItem tag) { AsyncRead(new List <TagItem> { tag }); }
/// <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); }
/// <summary> /// 异步写 /// </summary> /// <param name="tag"></param> /// <returns></returns> public bool AsyncWrite(TagItem tag) { return(false); }
/// <inheritdoc /> public bool AddTag(TagItem tag) { _Tags.Add(tag.ServerId, tag); return(true); }