Example #1
0
        /// <summary>
        /// Assigns a locally unique numeric id to each event type.
        /// </summary>
        private void UpdateEventAttributeMappings(NodeIdMappingSet mappingSet)
        {
            NodeIdMappingCollection mappingsToKeep = new NodeIdMappingCollection();

            // collect all unique declarations.
            List<AeEventAttribute> list = new List<AeEventAttribute>();

            foreach (AeEventCategory type in m_eventTypes.Values)
            {
                for (int ii = 0; ii < type.Attributes.Count; ii++)
                {
                    AeEventAttribute declaration = type.Attributes[ii];

                    // only variables can be attributes.
                    if (declaration.NodeClass != NodeClass.Variable)
                    {
                        continue;
                    }

                    // need to link attributes to any attributes that they override.
                    AeEventCategory subType = type;
                    AeEventCategory superType = null;

                    while (subType != null)
                    {
                        if (NodeId.IsNull(subType.SuperTypeId) || subType.TypeId == subType.SuperTypeId || subType.SuperTypeId == Opc.Ua.ObjectTypeIds.BaseObjectType)
                        {
                            list.Add(declaration);
                            break;
                        }

                        if (!m_eventTypes.TryGetValue(subType.SuperTypeId, out superType))
                        {
                            break;
                        }

                        for (int jj = 0; jj < superType.Attributes.Count; jj++)
                        {
                            if (superType.Attributes[jj].BrowsePathDisplayText == declaration.BrowsePathDisplayText)
                            {
                                declaration.OverriddenDeclaration = superType.Attributes[jj];
                                declaration = declaration.OverriddenDeclaration;
                                break;
                            }
                        }

                        subType = superType;
                    }
                }
            }

            // look up ids for all attributes in master list.
            Dictionary<uint, AeEventAttribute> attributes = new Dictionary<uint, AeEventAttribute>();

            for (int ii = 0; ii < list.Count; ii++)
            {
                AeEventAttribute declaration = list[ii];

                for (int jj = 0; jj < mappingSet.Mappings.Count; jj++)
                {
                    NodeIdMapping mapping = mappingSet.Mappings[jj];

                    try
                    {
                        // browse display paths always use local namespa indexes.
                        if (declaration.BrowsePathDisplayText != mapping.BrowePath)
                        {
                            continue;
                        }

                        // need to convert the cached type id to a remote id.
                        NodeId localId = NodeId.Parse(mapping.NodeId);
                        NodeId remoteId = this.GetRemoteNodeId(localId);

                        if (declaration.RootTypeId != remoteId)
                        {
                            continue;
                        }

                        // must update the saved integer id.
                        if (!attributes.ContainsKey(mapping.IntegerId))
                        {
                            declaration.LocalId = mapping.IntegerId;
                            attributes[declaration.LocalId] = declaration;
                            mappingsToKeep.Add(mapping);
                        }

                        // must assign a new one if a duplicate found.
                        else
                        {
                            declaration.LocalId = 0;
                        }
                    }
                    catch (Exception)
                    {
                        // ignore invalid mappings.
                    }
                }
            }

            // assign new ids.
            uint nextId = 1;

            for (int ii = 0; ii < list.Count; ii++)
            {
                AeEventAttribute declaration = list[ii];

                if (declaration.LocalId == 0)
                {
                    // find a unique id.
                    while (attributes.ContainsKey(nextId)) nextId++;

                    // assign the id.
                    declaration.LocalId = nextId;
                    attributes[declaration.LocalId] = declaration;

                    // save the mapping.
                    NodeIdMapping mapping = new NodeIdMapping();
                    mapping.IntegerId = nextId;
                    mapping.NodeId = this.GetLocalNodeId(declaration.RootTypeId).ToString();
                    mapping.BrowePath = declaration.BrowsePathDisplayText;

                    mappingsToKeep.Add(mapping);
                }
            }

            // update mapping set.
            mappingSet.Mappings = mappingsToKeep;
            m_attributes = attributes;
        }
Example #2
0
        /// <summary>
        /// Updates the aggregate mappings.
        /// </summary>
        /// <param name="session">The session.</param>
        private void UpdateAggregateMappings(Session session)
        {
            // get the list of supported aggregates.
            NodeId objectId = GetAggregateFunctionsObjectId(session);

            // create the updated mapping set.
            NodeIdMappingSet mappingSet = new NodeIdMappingSet();
            mappingSet.MappingType = Opc.Ua.BrowseNames.AggregateFunctions;
            mappingSet.Mappings = new NodeIdMappingCollection();

            List<HdaAggregate> aggregates = GetAggregateFunctions(session, objectId);

            // check for unassigned aggregate ids.
            uint maxId = 0x80000000;

            for (int ii = 0; ii < aggregates.Count; ii++)
            {
                if (aggregates[ii].LocalId != 0)
                {
                    if (maxId < aggregates[ii].LocalId)
                    {
                        maxId = aggregates[ii].LocalId;
                    }

                    continue;
                }
            }

            // assign aggregate ids.
            for (int ii = 0; ii < aggregates.Count; ii++)
            {
                // assign a new id.
                if (aggregates[ii].LocalId == 0)
                {
                    aggregates[ii].LocalId = maxId++;
                }

                // do not add mapping for built-in annotations.
                if (aggregates[ii].LocalId <= (uint)OpcRcw.Hda.OPCHDA_AGGREGATE.OPCHDA_ANNOTATIONS)
                {
                    continue;
                }

                NodeIdMapping mapping = new NodeIdMapping();

                mapping.NodeId = m_mapper.GetLocalItemId(aggregates[ii].RemoteId);
                mapping.IntegerId = aggregates[ii].LocalId;

                mappingSet.Mappings.Add(mapping);
            }

            // update descriptions.
            UpdateAggregateDescriptions(session, aggregates);

            // update configuration.
            if (m_configuration.MappingSets == null)
            {
                m_configuration.MappingSets = new NodeIdMappingSetCollection();
            }

            // replace existing set.
            for (int ii = 0; ii <  m_configuration.MappingSets.Count; ii++)
            {
                if (m_configuration.MappingSets[ii].MappingType == Opc.Ua.BrowseNames.AggregateFunctions)
                {
                    m_configuration.MappingSets[ii] = mappingSet;
                    mappingSet = null;
                    break;
                }
            }

            // add a new set.
            if (mappingSet != null)
            {
                // update the configuration.
                m_configuration.MappingSets.Add(mappingSet);

                // update the mapping table.
                m_mapper.UpdateMappingSet(mappingSet);
            }

            m_aggregates = aggregates;
        }
Example #3
0
        /// <summary>
        /// Assigns a locally unique numeric id to each event type.
        /// </summary>
        private void UpdateEventTypeMappings(NodeIdMappingSet mappingSet)
        {
            NodeIdMappingCollection mappingsToKeep = new NodeIdMappingCollection();
            Dictionary<uint, AeEventCategory> categories = new Dictionary<uint, AeEventCategory>();

            for (int ii = 0; ii < mappingSet.Mappings.Count; ii++)
            {
                NodeIdMapping mapping = mappingSet.Mappings[ii];

                try
                {
                    // need to convert the cached type id to a remote id.
                    NodeId localId = NodeId.Parse(mapping.NodeId);
                    NodeId remoteId = this.GetRemoteNodeId(localId);

                    AeEventCategory eventType = null;

                    if (m_eventTypes.TryGetValue(remoteId, out eventType))
                    {
                        // check if the event already has an id.
                        if (eventType.LocalId == 0)
                        {
                            // must update the saved integer id.
                            if (!categories.ContainsKey(mapping.IntegerId))
                            {
                                eventType.LocalId = mapping.IntegerId;
                                categories[eventType.LocalId] = eventType;
                                mappingsToKeep.Add(mapping);
                            }

                            // must assign a new one if a duplicate found.
                            else
                            {
                                eventType.LocalId = 0;
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    // discard invalid mappings.
                }
            }

            // assign ids to any types which do not have mappings.
            uint nextId = 1;

            foreach (AeEventCategory eventType in m_eventTypes.Values)
            {
                if (eventType.LocalId == 0)
                {
                    // find a unique id.
                    while (categories.ContainsKey(nextId)) nextId++;

                    // assign the id.
                    eventType.LocalId = nextId;
                    categories[eventType.LocalId] = eventType;

                    // save the mapping.
                    NodeIdMapping mapping = new NodeIdMapping();
                    mapping.IntegerId = nextId;
                    mapping.NodeId = this.GetLocalNodeId(eventType.TypeId).ToString();

                    mappingsToKeep.Add(mapping);
                }
            }

            // update mappings.
            mappingSet.Mappings = mappingsToKeep;
            m_categories = categories;
        }