//======================================================================
        // IFactory

        /// <summary>
        /// Creates a new instance of the server.
        /// </summary>
        public virtual IServer CreateInstance(URL url, ConnectData connectData)
        {
            IServer server = null;

            // instantiate the object locally.
            if (!m_useRemoting)
            {
                server = (IServer)Activator.CreateInstance(m_systemType, new object[] { url, connectData });
            }

            // instantiate the object remotely using .NET remoting.
            else
            {
                server = (IServer)Activator.GetObject(m_systemType, url.ToString());
            }

            return(server);
        }
Exemple #2
0
        // Connect to OPC HDA server
        public bool Connect(string HostName, string ServerName)
        {
            var url = new Opc.URL();

            url.Scheme   = Opc.UrlScheme.HDA;
            url.HostName = HostName;
            url.Path     = ServerName;

            try {
                var fact = new OpcCom.Factory();
                if ((_OPCServer != null) && (!_OPCServer.IsConnected))
                {
                    _trace.TraceEvent(TraceEventType.Verbose, 0, "_OPCServer is disconnected, disposing object");
                    //Unfortunately, in case of lost connection simply calling .Connect() doesn't work :(
                    //Let's try to recreate the object from scratch
                    _OPCServer.Dispose();
                    _OPCServer = null;
                }

                if (_OPCServer == null)
                {
                    _trace.TraceEvent(TraceEventType.Verbose, 0, "_OPCServer is null, creating new object");
                    _OPCServer = new Opc.Hda.Server(fact, null);
                }

                if (!_OPCServer.IsConnected)
                {
                    _trace.TraceEvent(TraceEventType.Verbose, 0, "OPC server is disconnected, trying to connect");
                    _OPCServer.Connect(url, new Opc.ConnectData(new System.Net.NetworkCredential(), null));
                    if (!_OPCServer.IsConnected)
                    {
                        _trace.TraceEvent(TraceEventType.Error, 0, "Connection failed without exception: {0}", url.ToString());
                        return(false);
                    }
                    _trace.TraceEvent(TraceEventType.Verbose, 0, "Succesfully connected to {0}, obj: {1}", url.ToString(), _OPCServer.GetHashCode().ToString());
                }
                try {
                    Status = _OPCServer.GetStatus();
                    _trace.TraceEvent(TraceEventType.Verbose, 0, "OPC server status:\n" +
                                      "\tCurrentTime:     {0}\n" +
                                      "\tMaxReturnValues: {1}\n" +
                                      "\tProductVersion:  {2}\n" +
                                      "\tServerState:     {3}\n" +
                                      "\tStartTime:       {4}\n" +
                                      "\tStatusInfo:      {5}\n" +
                                      "\tVendorInfo:      {6}\n",
                                      Status.CurrentTime,
                                      Status.MaxReturnValues,
                                      Status.ProductVersion,
                                      Status.ServerState,
                                      Status.StartTime,
                                      Status.StatusInfo,
                                      Status.VendorInfo);
                } catch (Exception e) {
                    _trace.TraceEvent(TraceEventType.Warning, 0, "Can't get server status: {0}, {1}", url.ToString(), e.Message);
                }

                try {
                    _trace.TraceEvent(TraceEventType.Verbose, 0, "SupportedAggregates:");
                    SupportedAggregates = _OPCServer.GetAggregates();
                    foreach (Opc.Hda.Aggregate agg in SupportedAggregates)
                    {
                        _trace.TraceEvent(TraceEventType.Verbose, 0, "{0}\t{1}\t{2}", agg.ID, agg.Name, agg.Description);
                    }
                } catch (Exception e) {
                    _trace.TraceEvent(TraceEventType.Warning, 0, "Can't get server supported aggregates: {0}, {1}", url.ToString(), e.Message);
                }
            } catch (Exception e) {
                _trace.TraceEvent(TraceEventType.Error, 0, "Connection failed: {0}, {1}", url.ToString(), e.Message);
                return(false);
            }

            return(true);
        }