示例#1
0
        /// <summary>
        /// Creates a subscription and associate it with the current session.
        /// </summary>
        /// <param name="publishingInterval">The requested publishing interval.</param>
        /// <returns>Instance of class Subscription.</returns>
        /// <exception cref="Exception">Throws and forwards any exception with short error description.</exception>
        public Subscription AddSubscription(int publishingInterval)
        {
            Subscription newSubscription = null;

            try
            {
                Opc.Ua.Client.Subscription innerSubscription = new Opc.Ua.Client.Subscription(m_Session.DefaultSubscription);

                innerSubscription.DisplayName                = "My Subscription Name";
                innerSubscription.PublishingEnabled          = true;
                innerSubscription.PublishingInterval         = publishingInterval; // in milliseconds.
                innerSubscription.KeepAliveCount             = 10;                 // 10*UaRefreshRate  = 5s if UaRefreshRate = 500
                innerSubscription.LifetimeCount              = 100;                // UaRefreshRate*100 = 50s if UaRefreshRate = 500;
                innerSubscription.MaxNotificationsPerPublish = 100;

                // Associate the subscription with the session.
                m_Session.AddSubscription(innerSubscription);

                // Call the server and create the subscription.
                innerSubscription.Create();

                // At this point the subscription is sending publish requests at the keep alive rate.
                // Use the Notification event the session to receive updates when a publish completes.
                //m_Session.Notification += new NotificationEventHandler(Session_Notification);
                m_Session.Notification += new NotificationEventHandler(DataChangedNotification);

                newSubscription                   = new Subscription();
                newSubscription.Session           = m_Session;
                newSubscription.innerSubscription = innerSubscription;
            }
            catch (Exception e)
            {
                // Console.WriteLine(e.Message);
                throw e;
            }

            return(newSubscription);
        }
示例#2
0
        /// <summary>
        /// Добавление тегов к по подписке
        /// </summary>
        public override void AddTags(List <TagId> taglist)
        {
            TagListBackup = taglist;
            if (Activated)
            {
                try
                {
                    var subscription = new Subscription(session.DefaultSubscription)
                    {
                        PublishingInterval = 1
                    };

                    int tag_counter = 0;
                    var list        = new List <MonitoredItem>();

                    //foreach (TagId tag in taglist)
                    //пока конфигуратор пуст поиск делаем по всей сфере
                    foreach (var tag in discoveredTags)
                    {
                        string identifier = "";
                        //if (discoveredTags.ContainsKey(tag.TagName))
                        //пока конфигуратор пуст поиск делаем по всей сфере
                        if (tag.Key.Contains("Sfera"))
                        {
                            //logger.Logged("Info", "#" + PollerId + ": добавляем тег '" + tag.TagName + "' в подписку",
                            logger.Logged("Info", "#" + PollerId + ": добавляем тег '" + tag.Key + "' в подписку",
                                          "OpcUaPoller", "AddTags");
                            //identifier = discoveredTags[tag.TagName];
                            //пока конфигуратор пуст поиск делаем по всей сфере
                            identifier = discoveredTags[tag.Key];
                            tag_counter++;
                            var item = new MonitoredItem(subscription.DefaultItem)
                            {
                                // DisplayName = tag.TagName,
                                DisplayName = tag.Key,
                                StartNodeId = identifier
                            };
                            list.Add(item);
                        }
                        else
                        {
                            logger.Logged("Error",
                                          //"#" + PollerId + ": тег '" + tag.TagName + "' не обнаружен на сервере", "OpcUaPoller",
                                          "#" + PollerId + ": тег '" + tag.Key + "' не обнаружен на сервере", "OpcUaPoller",
                                          "AddTags");
                        }
                    }

                    if (tag_counter > 0)
                    {
                        list.ForEach(i => i.Notification += OnNotification);
                        subscription.AddItems(list);

                        session.AddSubscription(subscription);
                        subscription.Create();

                        logger.Logged("Info",
                                      "Добавлено " + tag_counter + " тегов для контроля с OPC UA сервера #" + PollerId + "",
                                      "OpcUaPoller", "AddTags");
                    }
                    else
                    {
                        logger.Logged("Error", "Не найдено ни одного тега для контроля OPC UA сервера #" + PollerId + "",
                                      "OpcUaPoller", "AddTags");
                    }
                }
                catch (Exception ex)
                {
                    logger.Logged("Error",
                                  "Не удалось добавить теги для контроля OPC UA сервером #" + PollerId + ":" + ex.Message,
                                  "OpcUaPoller", "AddTags");
                }
            }
        }
示例#3
0
        /// <summary>
        /// Called when a session is created with a server.
        /// </summary>
        private void OnSessionCreated(Session session)
        {
            string commonAppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
            string configFileName = Utils.Format(@"{0}\OPC Foundation\ComPseudoServers\{1}.internal.xml", commonAppDataPath, m_clsid);

            lock (m_lock)
            {
                try
                {
                    m_session = null;
                    m_Subscription = null;
                    m_AreaNodes.Clear();

                    m_session = session;

                    // load the config file.
                    if (File.Exists(configFileName))
                    {
                        XmlSerializer ser = new XmlSerializer(typeof(Configuration));
                        TextReader reader = new StreamReader(configFileName);
                        m_configFile = (Configuration)ser.Deserialize(reader);
                        reader.Close();

                        // set the ActualDataType property
                        for (int ii = 0; ii < m_configFile.Attributes.Length - 1; ii++)
                        {
                            NodeId nodeid = new NodeId(m_configFile.Attributes[ii].strDataTypeNodeId);
                            m_configFile.Attributes[ii].ActualDataType = DataTypes.GetSystemType(nodeid, EncodeableFactory.GlobalFactory);
                        }
                    }
                    else
                    {
                        InitConfigInfo(configFileName);
                    }

                    // Obtain the current server table, generate index mapping tables (client->server, server->client)
                    // and update the client side namespace table if necessary due to server changes
                    GenerateNamespaceIndexMappings(m_clsid);

                    // The client side namespace table may have been updated if the server namespace table
                    // has changed therefore save the updated client table.
                    SaveConfigInfo(configFileName);
                    
                    // fetch type tree.
                    m_session.FetchTypeTree(Opc.Ua.ObjectTypeIds.BaseEventType);
                    m_session.FetchTypeTree(Opc.Ua.ReferenceTypeIds.References);
                    
                    //Create UA Event Subscription if none configured in the registry
                    m_Subscription = new Opc.Ua.Client.Subscription(m_session.DefaultSubscription);
                    m_Subscription.PublishingEnabled = true;
                    m_Subscription.PublishingInterval = m_configFile.ProxySubscriptionSettings.PublishingInterval;
                    m_Subscription.KeepAliveCount = m_configFile.ProxySubscriptionSettings.KeepAliveCount;
                    m_Subscription.LifetimeCount = m_configFile.ProxySubscriptionSettings.LifetimeCount;
                    m_Subscription.Priority = m_configFile.ProxySubscriptionSettings.Priority;
                    m_Subscription.MaxNotificationsPerPublish = m_configFile.ProxySubscriptionSettings.MaxNotificationsPerPublish;

                    m_session.AddSubscription(m_Subscription);
                    m_Subscription.Create();
                    m_KeepAliveInterval = (int)(m_Subscription.CurrentPublishingInterval * m_Subscription.CurrentKeepAliveCount);

                    // Add Server object as the only monitored item to this subscription

                    NodeId nodeId_Server = new NodeId(Opc.Ua.Objects.Server);

                    MonitoredItem monitoredItem = CreateMonitoredItemForEvents(nodeId_Server);
                    m_Subscription.AddItem(monitoredItem);
                    m_Subscription.ApplyChanges();

                    AreaNode areaNode = new AreaNode();
                    areaNode.AreaName = "/";
                    ++areaNode.RefCount;
                    areaNode.MonitoredItem = monitoredItem;
                    m_notifiers.Add(monitoredItem.ClientHandle, areaNode);
                    m_AreaNodes.Add("/", areaNode);

                    m_Subscription.Session.Call(
                        Opc.Ua.ObjectTypes.ConditionType,
                        Methods.ConditionType_ConditionRefresh,
                        m_Subscription.Id);
                }
                catch (Exception e)
                {
                    Utils.Trace(e, "Initializing server after create.");
                    throw ComUtils.CreateComException(e);
                }
            }
        }
示例#4
0
        public async Task EndpointConnect()
        {
            var endpointCollection = DiscoverEndpoints(Module.Configuration, ServerUrl, 60);
            var selectedEndpoints  = new List <EndpointDescription>();

            // Select endpoints
            foreach (EndpointDescription endpoint in endpointCollection)
            {
                if (endpoint.TransportProfileUri == Profiles.UaTcpTransport &&
                    endpoint.SecurityLevel >= MinimumSecurityLevel &&
                    endpoint.SecurityMode >= MinimumSecurityMode)
                {
                    // patch endpoint to set the original host name we want to connect to.
                    var url = new UriBuilder(endpoint.EndpointUrl);
                    url.Host             = ServerUrl.Host;
                    endpoint.EndpointUrl = url.ToString();
                    selectedEndpoints.Add(endpoint);
                }
            }

            //
            // Sort, but descending with highest level first i.e. return
            // < 0 if x is less than y
            // > 0 if x is greater than y
            //   0 if x and y are equal
            //
            selectedEndpoints.Sort((y, x) => x.SecurityLevel - y.SecurityLevel);

            // Do not emit all exceptions as they occur, only throw them all when no connection can be made.
            var exceptions = new List <Exception>(selectedEndpoints.Count);

            foreach (EndpointDescription endpoint in selectedEndpoints)
            {
                ConfiguredEndpoint configuredEndpoint = new ConfiguredEndpoint(
                    endpoint.Server, EndpointConfiguration.Create(Module.Configuration));
                configuredEndpoint.Update(endpoint);
                try
                {
                    Console.WriteLine($"Opc.Ua.Client.SampleModule: Trying to create session with mode: {endpoint.SecurityMode}, level:{endpoint.SecurityLevel} to {configuredEndpoint.EndpointUrl}...");
                    _session = await Session.Create(
                        Module.Configuration,
                        configuredEndpoint,
                        true,
                        false,
                        Module.Configuration.ApplicationName,
                        60000,
                        // TODO: Make user identity configurable, plus add dedicated security policy
                        new UserIdentity(new AnonymousIdentityToken()),
                        null);

                    if (_session != null)
                    {
                        var subscription = new Subscription(_session.DefaultSubscription);
                        subscription.PublishingInterval = PublishingInterval;

                        // TODO: Make other subscription settings configurable...
                        subscription.AddItems(MonitoredItems);
                        _session.AddSubscription(subscription);
                        subscription.Create();

                        Console.WriteLine($"Opc.Ua.Client.SampleModule: Session with mode: {endpoint.SecurityMode}, level:{endpoint.SecurityLevel} to {configuredEndpoint.EndpointUrl} established!");
                        _session.KeepAlive += new KeepAliveEventHandler(StandardClient_KeepAlive);

                        // Done
                        return;
                    }
                    exceptions.Add(new Exception($"ERROR: Create session to endpoint {endpoint.ToString()} returned null."));
                }
                catch (AggregateException ae)
                {
                    exceptions.AddRange(ae.InnerExceptions);
                }
                catch (Exception ex)
                {
                    exceptions.Add(ex);
                }

                //  ... try another endpoint until we do not have any more...
                Console.WriteLine($"Opc.Ua.Client.SampleModule: WARNING Could not create session to endpoint {endpoint.ToString()}...");
            }
            throw new AggregateException("Failed to find acceptable endpoint to connect to.", exceptions);
        }