示例#1
0
        /// <summary>
        /// Open connection to Spark API
        /// </summary>
        /// <returns></returns>
        internal bool Connect()
        {
            bool result = true;

            lock (_syncLock)
            {
                try
                {
                    if (ConnectionStatus == SparkConnectionStatus.Disconnected)
                    {
                        //Instance Spark object
                        Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + "\t" + "Initiating Spark connection...");
                        Initialise();

                        //Connect to Spark server
                        ApiCredentials credentials = ApiCredentials.LoadFromFile();
                        if (credentials == null)
                        {
                            throw new ApplicationException("Spark API security credentials not available");
                        }
                        Spark.SetLogin(credentials.Username, credentials.Password);
                        if (!Spark.Connect())
                        {
                            Debug.WriteLine("Can't connect: {0}", Spark.DescribeError(Spark.GetLastError()));
                            result = false;
                        }
                        Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + "\t" + "Spark connection initiated");

                        //Wait to allow Spark API to data access
                        System.Threading.Thread.Sleep(1000);
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("ERROR: APIControl - ", ex.Message);
                    result = false;
                }
            }
            return(result);
        }