///////////////////////////////////////////////////////////////////////
        #region Public Methods

        /// <summary>
        /// Connects to the server with the specified OpcUrl and credentials.
        /// </summary>
        public override void Connect(OpcUrl url, OpcConnectData connectData)
        {
            if (_factory == null)
            {
                _factory = new Com.Factory();
            }
            // connect to server.
            base.Connect(url, connectData);

            // all done if no subscriptions.
            if (_subscriptions.Count == 0)
            {
                return;
            }

            // create subscriptions (should only happen if server has been deserialized).
            SubscriptionCollection subscriptions = new SubscriptionCollection();

            foreach (Ae.TsCAeSubscription template in _subscriptions)
            {
                // create subscription for template.
                try { subscriptions.Add(EstablishSubscription(template)); }
                catch { }
            }

            // save new set of subscriptions.
            _subscriptions = subscriptions;
        }
        /// <summary>
        /// Looks up the CLSID for the specified prog id on a remote host.
        /// </summary>
        public Guid CLSIDFromProgID(string progID, string host, OpcConnectData connectData)
        {
            lock (this)
            {
                NetworkCredential credentials = (connectData != null)?connectData.GetCredential(null, null):null;

                // connect to the server.
                m_server = (IOPCServerList2)Interop.CreateInstance(CLSID, host, credentials);
                m_host   = host;

                // lookup prog id.
                Guid clsid;

                try
                {
                    m_server.CLSIDFromProgID(progID, out clsid);
                }
                catch
                {
                    clsid = Guid.Empty;
                }
                finally
                {
                    Interop.ReleaseServer(m_server);
                    m_server = null;
                }

                // return empty guid if prog id not found.
                return(clsid);
            }
        }
        /// <summary>
        /// Connects to the server with the specified OpcUrl and credentials.
        /// </summary>
        public override void Connect(OpcUrl url, OpcConnectData connectData)
        {
            LicenseHandler.ValidateFeatures(LicenseHandler.ProductFeature.AlarmsConditions);
            if (Factory == null)
            {
                Factory = new Factory();
            }
            // connect to server.
            base.Connect(url, connectData);

            // all done if no subscriptions.
            if (subscriptions_.Count == 0)
            {
                return;
            }

            // create subscriptions (should only happen if server has been deserialized).
            SubscriptionCollection subscriptions = new SubscriptionCollection();

            foreach (TsCAeSubscription template in subscriptions_)
            {
                // create subscription for template.
                try { subscriptions.Add(EstablishSubscription(template)); }
                catch
                {
                    // ignored
                }
            }

            // save new set of subscriptions.
            subscriptions_ = subscriptions;
        }
        /// <summary>
        /// Connects to the server with the specified URL and credentials.
        /// </summary>
        public virtual void Initialize(OpcUrl url, OpcConnectData connectData)
        {
            if (url == null)
            {
                throw new ArgumentNullException(nameof(url));
            }

            lock (lock_)
            {
                // re-connect only if the url has changed or has not been initialized.
                if (url_ == null || !url_.Equals(url))
                {
                    // release the current server.
                    if (server_ != null)
                    {
                        Uninitialize();
                    }

                    // instantiate a new server.
                    server_ = (IOPCCommon)Factory.Connect(url, connectData);
                }

                // save url.
                url_ = (OpcUrl)url.Clone();
            }
        }
        /// <summary>
        /// Connects to the server with the specified URL and credentials.
        /// </summary>
        public virtual void Initialize(OpcUrl url, OpcConnectData connectData)
        {
            if (url == null)
            {
                throw new ArgumentNullException("url");
            }

            lock (lock_)
            {
                // re-connect only if the url has changed or has not been initialized.
                if (_url == null || !_url.Equals(url))
                {
                    // release the current server.
                    if (m_server != null)
                    {
                        Uninitialize();
                    }

                    // instantiate a new server.
                    m_server = (IOPCCommon)Technosoftware.DaAeHdaClient.Com.Factory.Connect(url, connectData);
                }

                // save url.
                _url = (OpcUrl)url.Clone();
            }
        }
        /// <summary>
        /// Browses for servers a computer.
        /// </summary>
        private void BrowseServers(TreeNode node)
        {
            try
            {
                // clear current contents.
                node.Nodes.Clear();

                // get the host name.
                string host = null;

                if (node != m_localServers)
                {
                    host = node.Text;
                }

                // get default login information.
                OpcConnectData connectData = FindConnectData(node);

                // find the servers.
                List <OpcServer> servers;

                OpcUserIdentity userIdentity;
                if (connectData == null || connectData.UserIdentity == null)
                {
                    userIdentity = null;
                }
                else
                {
                    userIdentity = connectData.UserIdentity;
                }
                // find the servers.
                IOpcDiscovery discovery = new OpcClientSdk.Com.ServerEnumerator();
                servers = discovery.GetAvailableServers(m_specification, host, connectData).ToList();
                // add children.
                if (servers != null)
                {
                    foreach (OpcServer server in servers)
                    {
                        TreeNode child = new TreeNode(server.ServerName);
                        child.ImageIndex = child.SelectedImageIndex = Resources.IMAGE_LOCAL_SERVER;
                        child.Tag        = server;

                        node.Nodes.Add(child);
                    }

                    node.Expand();
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
        /// <summary>
        /// Prompts the user to set the network connectData for the node.
        /// </summary>
        private void SetLoginMI_Click(object sender, System.EventArgs e)
        {
            TreeNode node = BrowseTV.SelectedNode;

            if (node == m_localNetwork | IsHostNode(node))
            {
                OpcConnectData connectData = (OpcConnectData)node.Tag;

                if (connectData == null)
                {
                    node.Tag = connectData = new OpcConnectData(null);
                }

                connectData.UserIdentity = new NetworkCredentialsDlg().ShowDialog(connectData.UserIdentity);
            }
        }
        /// <summary>
        /// Connects to the server with the specified OpcUrl and credentials.
        /// </summary>
        public override void Connect(OpcUrl url, OpcConnectData connectData)
        {
            LicenseHandler.ValidateFeatures(LicenseHandler.ProductFeature.HistoricalAccess);
            if (Factory == null)
            {
                Factory = new Com.Factory();
            }
            // connect to server.
            base.Connect(url, connectData);

            // fetch supported attributes.
            GetAttributes();

            // fetch supported aggregates.
            GetAggregates();

            // create items for trends.
            foreach (TsCHdaTrend trend in trends_)
            {
                ArrayList itemIDs = new ArrayList();

                foreach (TsCHdaItem item in trend.Items)
                {
                    itemIDs.Add(new OpcItem(item));
                }

                // save server handles for each item.
                OpcItemResult[] results = CreateItems((OpcItem[])itemIDs.ToArray(typeof(OpcItem)));

                if (results != null)
                {
                    for (int ii = 0; ii < results.Length; ii++)
                    {
                        trend.Items[ii].ServerHandle = null;

                        if (results[ii].Result.Succeeded())
                        {
                            trend.Items[ii].ServerHandle = results[ii].ServerHandle;
                        }
                    }
                }
            }
        }
示例#9
0
        /// <summary>Connects to the server with the specified OpcUrl and credentials.</summary>
        /// <exception caption="OpcResultException Class" cref="OpcResultException">If an OPC specific error occur this exception is raised. The Result field includes then the OPC specific code.</exception>
        /// <param name="url">The network address of the remote server.</param>
        /// <param name="connectData">Any protocol configuration or user authentication information.</param>
        public override void Connect(OpcUrl url, OpcConnectData connectData)
        {
            LicenseHandler.ValidateFeatures(LicenseHandler.ProductFeature.DataAccess);
            if (Factory == null)
            {
                if (Factory == null)
                {
                    Factory = new Com.Factory();
                }
            }
            // connect to server.
            base.Connect(url, connectData);

            // all done if no subscriptions.
            if (subscriptions_ == null)
            {
                return;
            }

            // create subscriptions (should only happen if server has been deserialized).
            TsCDaSubscriptionCollection subscriptions = new TsCDaSubscriptionCollection();

            foreach (TsCDaSubscription template in subscriptions_)
            {
                // create subscription for template.
                try
                {
                    subscriptions.Add(EstablishSubscription(template));
                }
                catch
                {
                    // Ignore exceptions here
                }
            }

            // save new set of subscriptions.
            subscriptions_ = subscriptions;
        }
示例#10
0
        //======================================================================
        // IOpcFactory

        /// <summary>
        /// Creates a new instance of the server.
        /// </summary>
        public override IOpcServer CreateInstance(OpcUrl url, OpcConnectData connectData)
        {
            object comServer = Connect(url, connectData);

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

            Server server        = null;
            Type   interfaceType = null;

            try
            {
                if (String.IsNullOrEmpty(url.Scheme))
                {
                    throw new NotSupportedException(String.Format("The URL scheme '{0}' is not supported.", url.Scheme));
                }

                // DA
                else if (url.Scheme == OpcUrlScheme.DA)
                {
                    // Verify that it is a DA server.
                    if (!typeof(OpcRcw.Da.IOPCServer).IsInstanceOfType(comServer))
                    {
                        interfaceType = typeof(OpcRcw.Da.IOPCServer);
                        throw new NotSupportedException();
                    }

                    // DA 3.00
                    if (!ForceDa20Usage && typeof(OpcRcw.Da.IOPCBrowse).IsInstanceOfType(comServer) && typeof(OpcRcw.Da.IOPCItemIO).IsInstanceOfType(comServer))
                    {
                        server = new Technosoftware.DaAeHdaClient.Com.Da.Server(url, comServer);
                    }

                    // DA 2.XX
                    else if (typeof(OpcRcw.Da.IOPCItemProperties).IsInstanceOfType(comServer))
                    {
                        server = new Technosoftware.DaAeHdaClient.Com.Da20.Server(url, comServer);
                    }

                    else
                    {
                        interfaceType = typeof(OpcRcw.Da.IOPCItemProperties);
                        throw new NotSupportedException();
                    }
                }

                // AE
                else if (url.Scheme == OpcUrlScheme.AE)
                {
                    // Verify that it is a AE server.
                    if (!typeof(OpcRcw.Ae.IOPCEventServer).IsInstanceOfType(comServer))
                    {
                        interfaceType = typeof(OpcRcw.Ae.IOPCEventServer);
                        throw new NotSupportedException();
                    }

                    server = new Technosoftware.DaAeHdaClient.Com.Ae.Server(url, comServer);
                }

                // HDA
                else if (url.Scheme == OpcUrlScheme.HDA)
                {
                    // Verify that it is a HDA server.
                    if (!typeof(OpcRcw.Hda.IOPCHDA_Server).IsInstanceOfType(comServer))
                    {
                        interfaceType = typeof(OpcRcw.Hda.IOPCHDA_Server);
                        throw new NotSupportedException();
                    }

                    server = new Technosoftware.DaAeHdaClient.Com.Hda.Server(url, comServer);
                }

                // All other specifications not supported yet.
                else
                {
                    throw new NotSupportedException(String.Format("The URL scheme '{0}' is not supported.", url.Scheme));
                }
            }
            catch (NotSupportedException e)
            {
                Utilities.Interop.ReleaseServer(server);
                server = null;

                if (interfaceType != null)
                {
                    StringBuilder message = new StringBuilder();

                    message.AppendFormat("The COM server does not support the interface ");
                    message.AppendFormat("'{0}'.", interfaceType.FullName);
                    message.Append("\r\n\r\nThis problem could be caused by:\r\n");
                    message.Append("- incorrectly installed proxy/stubs.\r\n");
                    message.Append("- problems with the DCOM security settings.\r\n");
                    message.Append("- a personal firewall (sometimes activated by default).\r\n");

                    throw new NotSupportedException(message.ToString());
                }

                throw e;
            }
            catch (Exception e)
            {
                Utilities.Interop.ReleaseServer(server);
                server = null;

                throw e;
            }

            // initialize the wrapper object.
            if (server != null)
            {
                server.Initialize(url, connectData);
            }

            return(server);
        }
示例#11
0
        /// <summary>
        /// Connects to the specified COM server.
        /// </summary>
        public static object Connect(OpcUrl url, OpcConnectData connectData)
        {
            // parse path to find prog id and clsid.
            string progID = url.Path;
            string clsid  = null;

            int index = url.Path.IndexOf('/');

            if (index >= 0)
            {
                progID = url.Path.Substring(0, index);
                clsid  = url.Path.Substring(index + 1);
            }

            // look up prog id if clsid not specified in the url.
            Guid guid;

            if (clsid == null)
            {
                // use OpcEnum to lookup the prog id.
                guid = new ServerEnumerator().CLSIDFromProgID(progID, url.HostName, connectData);

                // check if prog id is actually a clsid string.
                if (guid == Guid.Empty)
                {
                    try
                    {
                        guid = new Guid(progID);
                    }
                    catch
                    {
                        throw new OpcResultException(new OpcResult((int)OpcResult.CO_E_CLASSSTRING.Code, OpcResult.FuncCallType.SysFuncCall, null), String.Format("Could not connect to server {0}", progID));
                    }
                }
            }

            // convert clsid string to a guid.
            else
            {
                try
                {
                    guid = new Guid(clsid);
                }
                catch
                {
                    throw new OpcResultException(new OpcResult((int)OpcResult.CO_E_CLASSSTRING.Code, OpcResult.FuncCallType.SysFuncCall, null), String.Format("Could not connect to server {0}", progID));
                }
            }

            // get the credentials.
            OpcUserIdentity credentials = (connectData != null) ? connectData.UserIdentity : null;

            // instantiate the server using CoCreateInstanceEx.
            if (connectData == null || connectData.LicenseKey == null)
            {
                try
                {
                    return(Utilities.Interop.CreateInstance(guid, url.HostName, credentials));
                }
                catch (Exception e)
                {
                    throw new OpcResultException(OpcResult.CO_E_CLASSSTRING, e.Message, e);
                }
            }

            // instantiate the server using IClassFactory2.
            else
            {
                try
                {
                    return(null);
                    //return Technosoftware.DaAeHdaClient.Utilities.Interop.CreateInstanceWithLicenseKey(guid, url.HostName, credentials, connectData.LicenseKey);
                }
                catch (Exception e)
                {
                    throw new OpcResultException(OpcResult.CO_E_CLASSSTRING, e.Message, e);
                }
            }
        }
        /// <summary>
        /// Returns a list of servers that support the specified specification on the specified host.
        /// </summary>
        public OpcServer[] GetAvailableServers(OpcSpecification specification, string host, OpcConnectData connectData)
        {
            lock (this)
            {
                NetworkCredential credentials = (connectData != null)?connectData.GetCredential(null, null):null;

                // connect to the server.
                m_server = (IOPCServerList2)Interop.CreateInstance(CLSID, host, credentials);
                m_host   = host;

                try
                {
                    ArrayList servers = new ArrayList();

                    // convert the interface version to a guid.
                    Guid catid = new Guid(specification.Id);

                    // get list of servers in the specified specification.
                    IOPCEnumGUID enumerator = null;

                    m_server.EnumClassesOfCategories(
                        1,
                        new Guid[] { catid },
                        0,
                        null,
                        out enumerator);

                    // read clsids.
                    Guid[] clsids = ReadClasses(enumerator);

                    // release enumerator object.
                    Interop.ReleaseServer(enumerator);
                    enumerator = null;

                    // fetch class descriptions.
                    foreach (Guid clsid in clsids)
                    {
                        Factory factory = new Factory();

                        try
                        {
                            OpcUrl url = CreateUrl(specification, clsid);

                            OpcServer server = null;

                            if (specification == OpcSpecification.OPC_DA_30)
                            {
                                server = new TsCDaServer(factory, url);
                            }

                            else if (specification == OpcSpecification.OPC_DA_20)
                            {
                                server = new TsCDaServer(factory, url);
                            }

                            else if (specification == OpcSpecification.OPC_AE_10)
                            {
                                server = new TsCAeServer(factory, url);
                            }

                            else if (specification == OpcSpecification.OPC_HDA_10)
                            {
                                server = new TsCHdaServer(factory, url);
                            }

                            servers.Add(server);
                        }
                        catch (Exception)
                        {
                            // ignore bad clsids.
                        }
                    }

                    return((OpcServer[])servers.ToArray(typeof(OpcServer)));
                }
                finally
                {
                    // free the server.
                    Interop.ReleaseServer(m_server);
                    m_server = null;
                }
            }
        }