Ejemplo n.º 1
0
 private TypeLibrary()
 {
     _typeByDataTypeId = new Dictionary <ExpandedNodeId, Type>()
     {
         [ExpandedNodeId.Parse(DataTypeIds.Boolean)]        = typeof(bool),
         [ExpandedNodeId.Parse(DataTypeIds.SByte)]          = typeof(sbyte),
         [ExpandedNodeId.Parse(DataTypeIds.Byte)]           = typeof(byte),
         [ExpandedNodeId.Parse(DataTypeIds.Int16)]          = typeof(short),
         [ExpandedNodeId.Parse(DataTypeIds.UInt16)]         = typeof(ushort),
         [ExpandedNodeId.Parse(DataTypeIds.Int32)]          = typeof(int),
         [ExpandedNodeId.Parse(DataTypeIds.UInt32)]         = typeof(uint),
         [ExpandedNodeId.Parse(DataTypeIds.Int64)]          = typeof(long),
         [ExpandedNodeId.Parse(DataTypeIds.UInt64)]         = typeof(ulong),
         [ExpandedNodeId.Parse(DataTypeIds.Float)]          = typeof(float),
         [ExpandedNodeId.Parse(DataTypeIds.Double)]         = typeof(double),
         [ExpandedNodeId.Parse(DataTypeIds.String)]         = typeof(string),
         [ExpandedNodeId.Parse(DataTypeIds.DateTime)]       = typeof(DateTime),
         [ExpandedNodeId.Parse(DataTypeIds.Guid)]           = typeof(Guid),
         [ExpandedNodeId.Parse(DataTypeIds.ByteString)]     = typeof(byte[]),
         [ExpandedNodeId.Parse(DataTypeIds.XmlElement)]     = typeof(XElement),
         [ExpandedNodeId.Parse(DataTypeIds.NodeId)]         = typeof(NodeId),
         [ExpandedNodeId.Parse(DataTypeIds.ExpandedNodeId)] = typeof(ExpandedNodeId),
         [ExpandedNodeId.Parse(DataTypeIds.StatusCode)]     = typeof(StatusCode),
         [ExpandedNodeId.Parse(DataTypeIds.QualifiedName)]  = typeof(QualifiedName),
         [ExpandedNodeId.Parse(DataTypeIds.LocalizedText)]  = typeof(LocalizedText),
         [ExpandedNodeId.Parse(DataTypeIds.Structure)]      = typeof(ExtensionObject),
         [ExpandedNodeId.Parse(DataTypeIds.DataValue)]      = typeof(DataValue),
         [ExpandedNodeId.Parse(DataTypeIds.BaseDataType)]   = typeof(Variant),
         [ExpandedNodeId.Parse(DataTypeIds.DiagnosticInfo)] = typeof(DiagnosticInfo),
     };
     _dataTypeIdByType       = _typeByDataTypeId.ToDictionary(x => x.Value, x => x.Key);
     _binaryEncodingIdByType = new Dictionary <Type, ExpandedNodeId>();
     _typeByBinaryEncodingId = new Dictionary <ExpandedNodeId, Type>();
     foreach (var assembly in from assembly in AppDomain.CurrentDomain.GetAssemblies()
              where assembly.IsDefined(typeof(TypeLibraryAttribute), false)
              select assembly)
     {
         try
         {
             AddTypesToLibrary(assembly);
         }
         catch
         {
             continue;
         }
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SubscriptionBase"/> class.
        /// </summary>
        /// <param name="application">The UaApplication.</param>
        public SubscriptionBase(UaApplication application)
        {
            this.application = application ?? throw new ArgumentNullException(nameof(application));
            this.application.Completion.ContinueWith(t => this.stateMachineCts?.Cancel());
            this.logger           = this.application.LoggerFactory?.CreateLogger(this.GetType());
            this.errors           = new ErrorsContainer <string>(p => this.ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(p)));
            this.progress         = new Progress <CommunicationState>(s => this.State = s);
            this.propertyChanged += this.OnPropertyChanged;
            this.whenSubscribed   = new TaskCompletionSource <bool>();
            this.whenUnsubscribed = new TaskCompletionSource <bool>();
            this.whenUnsubscribed.TrySetResult(true);

            // register the action to be run on the ui thread, if there is one.
            if (SynchronizationContext.Current != null)
            {
                this.actionBlock = new ActionBlock <PublishResponse>(pr => this.OnPublishResponse(pr), new ExecutionDataflowBlockOptions {
                    SingleProducerConstrained = true, TaskScheduler = TaskScheduler.FromCurrentSynchronizationContext()
                });
            }
            else
            {
                this.actionBlock = new ActionBlock <PublishResponse>(pr => this.OnPublishResponse(pr), new ExecutionDataflowBlockOptions {
                    SingleProducerConstrained = true
                });
            }

            // read [Subscription] attribute.
            var typeInfo = this.GetType().GetTypeInfo();
            var sa       = typeInfo.GetCustomAttribute <SubscriptionAttribute>();

            if (sa != null)
            {
                this.endpointUrl        = sa.EndpointUrl;
                this.publishingInterval = sa.PublishingInterval;
                this.keepAliveCount     = sa.KeepAliveCount;
                this.lifetimeCount      = sa.LifetimeCount;
            }

            // read [MonitoredItem] attributes.
            foreach (var propertyInfo in typeInfo.DeclaredProperties)
            {
                var mia = propertyInfo.GetCustomAttribute <MonitoredItemAttribute>();
                if (mia == null || string.IsNullOrEmpty(mia.NodeId))
                {
                    continue;
                }

                MonitoringFilter filter = null;
                if (mia.AttributeId == AttributeIds.Value && (mia.DataChangeTrigger != DataChangeTrigger.StatusValue || mia.DeadbandType != DeadbandType.None))
                {
                    filter = new DataChangeFilter()
                    {
                        Trigger = mia.DataChangeTrigger, DeadbandType = (uint)mia.DeadbandType, DeadbandValue = mia.DeadbandValue
                    };
                }

                var propType = propertyInfo.PropertyType;
                if (propType == typeof(DataValue))
                {
                    this.monitoredItems.Add(new DataValueMonitoredItem(
                                                target: this,
                                                property: propertyInfo,
                                                nodeId: ExpandedNodeId.Parse(mia.NodeId),
                                                indexRange: mia.IndexRange,
                                                attributeId: mia.AttributeId,
                                                samplingInterval: mia.SamplingInterval,
                                                filter: filter,
                                                queueSize: mia.QueueSize,
                                                discardOldest: mia.DiscardOldest));
                    continue;
                }

                if (propType == typeof(BaseEvent) || propType.GetTypeInfo().IsSubclassOf(typeof(BaseEvent)))
                {
                    this.monitoredItems.Add(new EventMonitoredItem(
                                                target: this,
                                                property: propertyInfo,
                                                nodeId: ExpandedNodeId.Parse(mia.NodeId),
                                                indexRange: mia.IndexRange,
                                                attributeId: mia.AttributeId,
                                                samplingInterval: mia.SamplingInterval,
                                                filter: new EventFilter()
                    {
                        SelectClauses = EventHelper.GetSelectClauses(propType)
                    },
                                                queueSize: mia.QueueSize,
                                                discardOldest: mia.DiscardOldest));
                    continue;
                }

                if (propType == typeof(ObservableQueue <DataValue>))
                {
                    this.monitoredItems.Add(new DataValueQueueMonitoredItem(
                                                target: this,
                                                property: propertyInfo,
                                                nodeId: ExpandedNodeId.Parse(mia.NodeId),
                                                indexRange: mia.IndexRange,
                                                attributeId: mia.AttributeId,
                                                samplingInterval: mia.SamplingInterval,
                                                filter: filter,
                                                queueSize: mia.QueueSize,
                                                discardOldest: mia.DiscardOldest));
                    continue;
                }

                if (propType.IsConstructedGenericType && propType.GetGenericTypeDefinition() == typeof(ObservableQueue <>))
                {
                    var elemType = propType.GenericTypeArguments[0];
                    if (elemType == typeof(BaseEvent) || elemType.GetTypeInfo().IsSubclassOf(typeof(BaseEvent)))
                    {
                        this.monitoredItems.Add((MonitoredItemBase)Activator.CreateInstance(
                                                    typeof(EventQueueMonitoredItem <>).MakeGenericType(elemType),
                                                    this,
                                                    propertyInfo,
                                                    ExpandedNodeId.Parse(mia.NodeId),
                                                    mia.AttributeId,
                                                    mia.IndexRange,
                                                    MonitoringMode.Reporting,
                                                    mia.SamplingInterval,
                                                    new EventFilter()
                        {
                            SelectClauses = EventHelper.GetSelectClauses(elemType)
                        },
                                                    mia.QueueSize,
                                                    mia.DiscardOldest));
                        continue;
                    }
                }

                this.monitoredItems.Add(new ValueMonitoredItem(
                                            target: this,
                                            property: propertyInfo,
                                            nodeId: ExpandedNodeId.Parse(mia.NodeId),
                                            indexRange: mia.IndexRange,
                                            attributeId: mia.AttributeId,
                                            samplingInterval: mia.SamplingInterval,
                                            filter: filter,
                                            queueSize: mia.QueueSize,
                                            discardOldest: mia.DiscardOldest));
            }

            this.stateMachineCts  = new CancellationTokenSource();
            this.stateMachineTask = Task.Run(() => this.StateMachineAsync(this.stateMachineCts.Token));
        }
Ejemplo n.º 3
0
 public DataTypeIdAttribute(string s)
 {
     this.NodeId = ExpandedNodeId.Parse(s);
 }
 public BinaryEncodingIdAttribute(string s)
 {
     this.NodeId = ExpandedNodeId.Parse(s);
 }