Provides access to a COM DA server.
Inheritance: ComClient
 /// <summary>
 /// Fetches the event type information from the AE server.
 /// </summary>
 public void LoadEventTypes(ComAeClient client)
 {
     EventTypes = new List <EventType>();
     LoadEventType(client, OpcRcw.Ae.Constants.SIMPLE_EVENT);
     LoadEventType(client, OpcRcw.Ae.Constants.TRACKING_EVENT);
     LoadEventType(client, OpcRcw.Ae.Constants.CONDITION_EVENT);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ComAeBrowserClient"/> class.
 /// </summary>
 /// <param name="client">The client.</param>
 /// <param name="qualifiedName">The qualified area name.</param>
 public ComAeBrowserClient(
     ComAeClient client,
     string qualifiedName)
 {
     m_client = client;
     m_qualifiedName = qualifiedName; 
 }
        /// <summary>
        /// Fetches the attributes for the category from the AE server.
        /// </summary>
        private List <EventAttribute> LoadEventAttributes(ComAeClient client, int eventTypeId, int categoryId)
        {
            List <EventAttribute> attributes = new List <EventAttribute>();

            int[]    ids          = null;
            string[] descriptions = null;
            short[]  dataTypes    = null;

            try
            {
                client.GetEventAttributes(categoryId, out ids, out descriptions, out dataTypes);
            }
            catch (Exception e)
            {
                Utils.Trace(e, "Unexpected error fetching event attributes.");
                ids = null;
            }

            if (ids != null)
            {
                for (int ii = 0; ii < ids.Length; ii++)
                {
                    EventAttribute attribute = new EventAttribute();
                    attribute.Id          = ids[ii];
                    attribute.Description = descriptions[ii];
                    attribute.VarType     = dataTypes[ii];
                    attributes.Add(attribute);
                }
            }

            return(attributes);
        }
Exemple #4
0
        /// <summary>
        /// Creates the subscription.
        /// </summary>
        public void Create()
        {
            ComAeClient client = m_manager.SelectClient(m_defaultContext, false);

            // release existing server.
            if (this.Unknown != null)
            {
                ReleaseServer();
            }

            // create the subscription.
            this.Unknown = client.CreateEventSubscription();

            // select the attributes.
            foreach (KeyValuePair <int, int[]> entry in m_cache.Attributes)
            {
                SelectReturnedAttributes(entry.Key, entry.Value);
            }

            // set the filter.
            SetFilter(m_qualifiedName, m_isSource);

            // set up callback.
            try
            {
                m_callback = new ComAeEventSink(this);
            }
            catch (Exception e)
            {
                Utils.Trace("Could not establish event callback.", e);
            }
        }
Exemple #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ComAeBrowserClient"/> class.
 /// </summary>
 /// <param name="client">The client.</param>
 /// <param name="qualifiedName">The qualified area name.</param>
 public ComAeBrowserClient(
     ComAeClient client,
     string qualifiedName)
 {
     m_client        = client;
     m_qualifiedName = qualifiedName;
 }
        private void LoadConditionEvent(ComAeClient client, int eventTypeId, int categoryId, string description, List <EventAttribute> attributes)
        {
            string[] conditionNames = null;

            try
            {
                client.GetConditionNames(categoryId, out conditionNames);
            }
            catch (Exception e)
            {
                Utils.Trace(e, "Unexpected error fetching condition names.");
                conditionNames = null;
            }

            if (conditionNames != null)
            {
                // create a condition class for the category.
                EventType eventType = new EventType();
                eventType.EventTypeId       = eventTypeId;
                eventType.CategoryId        = categoryId;
                eventType.Description       = description;
                eventType.ConditionName     = null;
                eventType.SubConditionNames = null;
                eventType.Attributes        = new List <EventAttribute>();
                DetermineMapping(eventType);
                EventTypes.Add(eventType);

                // create event types for each condition name.
                for (int ii = 0; ii < conditionNames.Length; ii++)
                {
                    eventType                   = new EventType();
                    eventType.EventTypeId       = eventTypeId;
                    eventType.CategoryId        = categoryId;
                    eventType.Description       = description;
                    eventType.ConditionName     = conditionNames[ii];
                    eventType.SubConditionNames = null;
                    eventType.Attributes        = attributes;
                    DetermineMapping(eventType);

                    string[] subConditionNames = null;

                    try
                    {
                        client.GetSubConditionNames(eventType.ConditionName, out subConditionNames);
                    }
                    catch (Exception e)
                    {
                        Utils.Trace(e, "Unexpected error fetching sub-condition names.");
                        subConditionNames = null;
                    }

                    if (subConditionNames != null)
                    {
                        eventType.SubConditionNames = new List <string>(subConditionNames);
                    }

                    EventTypes.Add(eventType);
                }
            }
        }
        /// <summary>
        /// Updates the type cache.
        /// </summary>
        private void DoMetadataUpdate(object state)
        {
            try
            {
                if (!Server.IsRunning)
                {
                    return;
                }

                ComAeClientManager system = (ComAeClientManager)SystemContext.SystemHandle;
                ComAeClient        client = (ComAeClient)system.SelectClient(SystemContext, true);

                AeTypeCache cache = new AeTypeCache();
                cache.LoadEventTypes(client);

                lock (Lock)
                {
                    if (m_typeCache == null)
                    {
                        m_typeCache = cache;
                    }

                    m_typeCache.EventTypes = cache.EventTypes;
                    m_typeCache.UpdateCache(SystemContext, NamespaceIndex);
                }
            }
            catch (Exception e)
            {
                Utils.Trace(e, "Unexpected error updating event type cache.");
            }
        }
Exemple #8
0
        /// <summary>
        /// Initializes the next stage of browsing.
        /// </summary>
        private void NextStage()
        {
            ComAeClientManager system = (ComAeClientManager)this.SystemContext.SystemHandle;
            ComAeClient        client = system.SelectClient((ServerSystemContext)SystemContext, false);

            // determine which stage is next based on the reference types requested.
            for (Stage next = m_stage + 1; next <= Stage.Done; next++)
            {
                if (next == Stage.Children)
                {
                    if (IsRequired(ReferenceTypeIds.HasNotifier, false))
                    {
                        m_stage = next;
                        break;
                    }
                }

                else if (next == Stage.Parents)
                {
                    if (IsRequired(ReferenceTypeIds.HasNotifier, true))
                    {
                        m_stage = next;
                        break;
                    }
                }

                else if (next == Stage.Done)
                {
                    m_stage = next;
                    break;
                }
            }

            // start enumerating areas.
            if (m_stage == Stage.Children)
            {
                m_browser = new ComAeBrowserClient(client, m_qualifiedName);
                return;
            }

            // start enumerating parents.
            if (m_stage == Stage.Parents)
            {
                return;
            }

            // all done.
        }
        /// <summary>
        /// Called when the alarm is acknowledged.
        /// </summary>
        private ServiceResult OnAcknowledge(
            ISystemContext context,
            MethodState method,
            NodeId objectId,
            byte[] eventId,
            LocalizedText comment)
        {
            ComAeClientManager system = (ComAeClientManager)this.SystemContext.SystemHandle;
            ComAeClient        client = (ComAeClient)system.SelectClient((ServerSystemContext)context, false);

            try
            {
                return(client.Acknowledge((ServerSystemContext)context, eventId, comment));
            }
            catch (Exception e)
            {
                return(ServiceResult.Create(e, StatusCodes.BadUnexpectedError, "Could not acknowledge event."));
            }
        }
        /// <summary>
        /// Fetches the event categories for the specified event type.
        /// </summary>
        public void LoadEventType(ComAeClient client, int eventTypeId)
        {
            int[]    ids          = null;
            string[] descriptions = null;

            try
            {
                client.GetEventCategories(eventTypeId, out ids, out descriptions);
            }
            catch (Exception e)
            {
                Utils.Trace(e, "Unexpected error fetching event categories.");
            }

            if (ids != null)
            {
                for (int ii = 0; ii < ids.Length; ii++)
                {
                    List <EventAttribute> attributes = LoadEventAttributes(client, eventTypeId, ids[ii]);

                    if (eventTypeId == OpcRcw.Ae.Constants.CONDITION_EVENT)
                    {
                        LoadConditionEvent(client, eventTypeId, ids[ii], descriptions[ii], attributes);
                        continue;
                    }

                    EventType eventType = new EventType();
                    eventType.EventTypeId       = eventTypeId;
                    eventType.CategoryId        = ids[ii];
                    eventType.Description       = descriptions[ii];
                    eventType.ConditionName     = null;
                    eventType.SubConditionNames = null;
                    eventType.Attributes        = attributes;
                    DetermineMapping(eventType);
                    EventTypes.Add(eventType);
                }
            }
        }
        /// <summary>
        /// Updates the status node.
        /// </summary>
        protected override bool UpdateStatus()
        {
            // get the status from the server.
            ComAeClient          client = DefaultClient;
            OPCEVENTSERVERSTATUS?status = client.GetStatus();

            // check the client has been abandoned.
            if (!Object.ReferenceEquals(client, DefaultClient))
            {
                return(false);
            }

            // update the server status.
            lock (StatusNodeLock)
            {
                StatusNode.ServerUrl.Value = Configuration.ServerUrl;

                if (status != null)
                {
                    StatusNode.SetStatusCode(DefaultSystemContext, StatusCodes.Good, DateTime.UtcNow);

                    StatusNode.ServerState.Value     = Utils.Format("{0}", status.Value.dwServerState);
                    StatusNode.CurrentTime.Value     = ComUtils.GetDateTime(status.Value.ftCurrentTime);
                    StatusNode.LastUpdateTime.Value  = ComUtils.GetDateTime(status.Value.ftLastUpdateTime);
                    StatusNode.StartTime.Value       = ComUtils.GetDateTime(status.Value.ftStartTime);
                    StatusNode.VendorInfo.Value      = status.Value.szVendorInfo;
                    StatusNode.SoftwareVersion.Value = Utils.Format("{0}.{1}.{2}", status.Value.wMajorVersion, status.Value.wMinorVersion, status.Value.wBuildNumber);
                }
                else
                {
                    StatusNode.SetStatusCode(DefaultSystemContext, StatusCodes.BadOutOfService, DateTime.UtcNow);
                }

                StatusNode.ClearChangeMasks(DefaultSystemContext, true);
                return(status != null);
            }
        }
Exemple #12
0
        /// <summary>
        /// Fetches the attributes for the category from the AE server.
        /// </summary>
        private List<EventAttribute> LoadEventAttributes(ComAeClient client, int eventTypeId, int categoryId)
        {
            List<EventAttribute> attributes = new List<EventAttribute>();

            int[] ids = null;
            string[] descriptions = null;
            short[] dataTypes = null;

            try
            {
                client.GetEventAttributes(categoryId, out ids, out descriptions, out dataTypes);
            }
            catch (Exception e)
            {
                Utils.Trace(e, "Unexpected error fetching event attributes.");
                ids = null;
            }

            if (ids != null)
            {
                for (int ii = 0; ii < ids.Length; ii++)
                {
                    EventAttribute attribute = new EventAttribute();
                    attribute.Id = ids[ii];
                    attribute.Description = descriptions[ii];
                    attribute.VarType = dataTypes[ii];
                    attributes.Add(attribute);
                }
            }

            return attributes;
        }
Exemple #13
0
        private void LoadConditionEvent(ComAeClient client, int eventTypeId, int categoryId, string description, List<EventAttribute> attributes)
        {
            string[] conditionNames = null;

            try
            {
                client.GetConditionNames(categoryId, out conditionNames);
            }
            catch (Exception e)
            {
                Utils.Trace(e, "Unexpected error fetching condition names.");
                conditionNames = null;
            }

            if (conditionNames != null)
            {
                // create a condition class for the category.
                EventType eventType = new EventType();
                eventType.EventTypeId = eventTypeId;
                eventType.CategoryId = categoryId;
                eventType.Description = description;
                eventType.ConditionName = null;
                eventType.SubConditionNames = null;
                eventType.Attributes = new List<EventAttribute>();
                DetermineMapping(eventType);
                EventTypes.Add(eventType);

                // create event types for each condition name.
                for (int ii = 0; ii < conditionNames.Length; ii++)
                {
                    eventType = new EventType();
                    eventType.EventTypeId = eventTypeId;
                    eventType.CategoryId = categoryId;
                    eventType.Description = description;
                    eventType.ConditionName = conditionNames[ii];
                    eventType.SubConditionNames = null;
                    eventType.Attributes = attributes;
                    DetermineMapping(eventType);

                    string[] subConditionNames = null;

                    try
                    {
                        client.GetSubConditionNames(eventType.ConditionName, out subConditionNames);
                    }
                    catch (Exception e)
                    {
                        Utils.Trace(e, "Unexpected error fetching sub-condition names.");
                        subConditionNames = null;
                    }

                    if (subConditionNames != null)
                    {
                        eventType.SubConditionNames = new List<string>(subConditionNames);
                    }

                    EventTypes.Add(eventType);
                }
            }
        }
Exemple #14
0
        /// <summary>
        /// Fetches the event categories for the specified event type.
        /// </summary>
        public void LoadEventType(ComAeClient client, int eventTypeId)
        {
            int[] ids = null;
            string[] descriptions = null;

            try
            {
                client.GetEventCategories(eventTypeId, out ids, out descriptions);
            }
            catch (Exception e)
            {
                Utils.Trace(e, "Unexpected error fetching event categories.");
            }

            if (ids != null)
            {
                for (int ii = 0; ii < ids.Length; ii++)
                {
                    List<EventAttribute> attributes = LoadEventAttributes(client, eventTypeId, ids[ii]);

                    if (eventTypeId == OpcRcw.Ae.Constants.CONDITION_EVENT)
                    {
                        LoadConditionEvent(client, eventTypeId, ids[ii], descriptions[ii], attributes);
                        continue;
                    }

                    EventType eventType = new EventType();
                    eventType.EventTypeId = eventTypeId;
                    eventType.CategoryId = ids[ii];
                    eventType.Description = descriptions[ii];
                    eventType.ConditionName = null;
                    eventType.SubConditionNames = null;
                    eventType.Attributes = attributes;
                    DetermineMapping(eventType);
                    EventTypes.Add(eventType);
                }
            }
        }
Exemple #15
0
 /// <summary>
 /// Fetches the event type information from the AE server.
 /// </summary>
 public void LoadEventTypes(ComAeClient client)
 {
     EventTypes = new List<EventType>();
     LoadEventType(client, OpcRcw.Ae.Constants.SIMPLE_EVENT);
     LoadEventType(client, OpcRcw.Ae.Constants.TRACKING_EVENT);
     LoadEventType(client, OpcRcw.Ae.Constants.CONDITION_EVENT);
 }
        /// <summary>
        /// Subscribes to events.
        /// </summary>
        protected override ServiceResult SubscribeToEvents(
            ServerSystemContext context,
            NodeState source,
            IEventMonitoredItem monitoredItem,
            bool unsubscribe)
        {
            ComAeClientManager system = (ComAeClientManager)this.SystemContext.SystemHandle;
            ComAeClient        client = (ComAeClient)system.SelectClient(context, false);

            // need to wait until the cache is refreshed for the first time.
            if (!WaitForTypeCache())
            {
                return(StatusCodes.BadOutOfService);
            }

            lock (Lock)
            {
                SubscriptionIndex index = new SubscriptionIndex();
                index.NodeId   = source.NodeId;
                index.LocaleId = client.LocaleId;

                if (unsubscribe)
                {
                    ComAeSubscriptionClient subscription = null;

                    if (!m_monitoredItems.TryGetValue(monitoredItem.Id, out subscription))
                    {
                        return(ServiceResult.Good);
                    }

                    m_monitoredItems.Remove(monitoredItem.Id);
                    // Utils.Trace("REMOVED ITEM {0}", monitoredItem.Id);

                    if (subscription.RemoveItem(monitoredItem as MonitoredItem) == 0)
                    {
                        subscription.Delete();
                        m_subscriptions.Remove(index);
                        // Utils.Trace("DELETED SUBSCRIPTION {0}", index.NodeId);
                    }
                }
                else
                {
                    ComAeSubscriptionClient subscription = null;

                    if (!m_subscriptions.TryGetValue(index, out subscription))
                    {
                        subscription = new ComAeSubscriptionClient(context, m_configuration, m_typeCache, NamespaceIndex, system, monitoredItem as MonitoredItem);
                        m_subscriptions.Add(index, subscription);
                        subscription.Create();
                        // Utils.Trace("ADDED NEW SUBSCRIPTION {0}", index.NodeId);
                    }
                    else
                    {
                        subscription.AddItem(monitoredItem as MonitoredItem);
                    }

                    m_monitoredItems[monitoredItem.Id] = subscription;
                    // Utils.Trace("ADDED NEW ITEM {0}", monitoredItem.Id);
                }
            }

            // all done.
            return(ServiceResult.Good);
        }
        /// <summary>
        /// Verifies that the specified node exists.
        /// </summary>
        protected override NodeState ValidateNode(
            ServerSystemContext context,
            NodeHandle handle,
            IDictionary <NodeId, NodeState> cache)
        {
            // not valid if no root.
            if (handle == null)
            {
                return(null);
            }

            // check if previously validated.
            if (handle.Validated)
            {
                return(handle.Node);
            }

            NodeState target = null;

            // check if already in the cache.
            if (cache != null)
            {
                if (cache.TryGetValue(handle.NodeId, out target))
                {
                    // nulls mean a NodeId which was previously found to be invalid has been referenced again.
                    if (target == null)
                    {
                        return(null);
                    }

                    handle.Node      = target;
                    handle.Validated = true;
                    return(handle.Node);
                }

                target = null;
            }

            try
            {
                // check if the node id has been parsed.
                AeParsedNodeId parsedNodeId = handle.ParsedNodeId as AeParsedNodeId;

                if (parsedNodeId == null)
                {
                    return(null);
                }

                ComAeClient client = m_system.SelectClient(context, false);

                switch (parsedNodeId.RootType)
                {
                case AeModelUtils.AeSimpleEventType:
                case AeModelUtils.AeTrackingEventType:
                case AeModelUtils.AeConditionEventType:
                {
                    if (m_typeCache == null)
                    {
                        return(null);
                    }

                    BaseObjectTypeState eventTypeNode = null;
                    NodeId rootId = AeParsedNodeId.Construct(parsedNodeId.RootType, parsedNodeId.CategoryId, parsedNodeId.ConditionName, parsedNodeId.NamespaceIndex);

                    if (!m_typeCache.EventTypeNodes.TryGetValue(rootId, out eventTypeNode))
                    {
                        return(null);
                    }

                    target = eventTypeNode;
                    break;
                }

                case AeModelUtils.AeArea:
                {
                    ComAeBrowserClient browser = new ComAeBrowserClient(client, null);
                    target = browser.FindArea(context, parsedNodeId.RootId, NamespaceIndex);
                    browser.Dispose();

                    handle.Validated = true;
                    handle.Node      = target;
                    return(handle.Node);
                }

                case AeModelUtils.AeSource:
                {
                    ComAeBrowserClient browser = new ComAeBrowserClient(client, null);
                    target = browser.FindSource(context, parsedNodeId.RootId, parsedNodeId.ComponentPath, NamespaceIndex);
                    browser.Dispose();

                    handle.Validated = true;
                    handle.Node      = target;
                    return(handle.Node);
                }

                case AeModelUtils.AeCondition:
                {
                    target = new AeConditionState(context, handle, m_templateAlarm.Acknowledge);
                    break;
                }
                }

                // node does not exist.
                if (target == null)
                {
                    return(null);
                }

                if (!String.IsNullOrEmpty(parsedNodeId.ComponentPath))
                {
                    // validate component.
                    NodeState component = target.FindChildBySymbolicName(context, parsedNodeId.ComponentPath);

                    // component does not exist.
                    if (component == null)
                    {
                        return(null);
                    }

                    target = component;
                }

                // found a valid component.
                handle.Validated = true;
                handle.Node      = target;
                return(handle.Node);
            }
            finally
            {
                // store the node in the cache to optimize subsequent lookups.
                if (cache != null)
                {
                    cache.Add(handle.NodeId, target);
                }
            }
        }