Beispiel #1
0
 protected virtual void Dispose(bool disposing)
 {
     if (!disposedValue)
     {
         if (disposing)
         {
             if (HsClient != null)
             {
                 HsClient.Disconnected -= HsClient_Disconnected;
                 HsClient.Dispose();
             }
             hsTraceListener?.Dispose();
             CallbackClient?.Dispose();
             cancellationTokenSource.Dispose();
             shutdownWaitEvent.Dispose();
         }
         disposedValue = true;
     }
 }
Beispiel #2
0
        public void Connect(string serverAddress, int serverPort)
        {
            try
            {
                HsClient = ScsServiceClientBuilder.CreateClient <IHSApplication>(new ScsTcpEndPoint(serverAddress, serverPort), this);
                HsClient.Connect();
                HS = HsClient.ServiceProxy;

                var apiVersion = HS.APIVersion; // just to make sure our connection is valid

                hsTraceListener = new HSTraceListener(this as ILogger);
                Debug.Listeners.Add(hsTraceListener);
            }
            catch (Exception ex)
            {
                throw new HspiConnectionException(Invariant($"Error connecting homeseer SCS client: {ex.Message}"), ex);
            }

            try
            {
                CallbackClient = ScsServiceClientBuilder.CreateClient <IAppCallbackAPI>(new ScsTcpEndPoint(serverAddress, serverPort), this);
                CallbackClient.Connect();
                Callback = CallbackClient.ServiceProxy;

                var apiVersion = Callback.APIVersion; // just to make sure our connection is valid
            }
            catch (Exception ex)
            {
                throw new HspiConnectionException(Invariant($"Error connecting callback SCS client: {ex.Message}"), ex);
            }

            // Establish the reverse connection from homeseer back to our plugin
            try
            {
                HS.Connect(Name, InstanceFriendlyName());
            }
            catch (Exception ex)
            {
                throw new HspiConnectionException(Invariant($"Error connecting homeseer to our plugin: {ex.Message}"), ex);
            }

            HsClient.Disconnected += HsClient_Disconnected;
        }
        public bool Connect(string serverAddress = null, int serverPort = -1)
        {
            if (Shutdown)
            {
                return(true);
            }

            if (serverAddress != null)
            {
                // If passed address/port - store it
                // So later may try to reconnect using stored values
                this.serverAddress = serverAddress;
                this.serverPort    = serverPort;
            }

            // Set HS to HsClient.ServiceProxy only when succsessfully finished connecting
            HS = null;

            // Create our main connection to the HomeSeer TCP communication framework
            // part 1 - hs object Proxy
            try
            {
                HsClient = ScsServiceClientBuilder.CreateClient <IHSApplication>(
                    new ScsTcpEndPoint(this.serverAddress, this.serverPort), this);
                HsClient.Disconnected += HsClient_Disconnected;
                HsClient.Connect();
                // Set HS to HsClient.ServiceProxy only when succsessfully finished connecting
                //HS = HsClient.ServiceProxy;

                // just to make sure our connection is valid
                Console.WriteLine("Host ApiVersion : {0}", HsClient.ServiceProxy.APIVersion);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error connecting HomeSeer SCS client: {ex.Message}");
                return(false);
            }

            // part 2 - callback object Proxy
            try
            {
                CallbackClient = ScsServiceClientBuilder.CreateClient <IAppCallbackAPI>(
                    new ScsTcpEndPoint(this.serverAddress, this.serverPort), this);
                CallbackClient.Connect();
                Callback = CallbackClient.ServiceProxy;

                // just to make sure our connection is valid
                Console.WriteLine("Host CB ApiVersion : {0}", Callback.APIVersion);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error connecting callback SCS client: {ex.Message}");
                return(false);
            }

            // part 3 - Establish the reverse connection from HomeSeer back to our plugin
            try
            {
                HsClient.ServiceProxy.Connect(Name, InstanceFriendlyName());
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error connecting HomeSeer to our plugin: {ex.Message}");
                return(false);
            }

            // All good, call virtual Connected() to inform plugin
            HS = HsClient.ServiceProxy;
            Connected(true);
            return(true);
        }