Beispiel #1
0
        private void BtnGetRemoteConfiguration_Click(object sender, EventArgs e)
        {
            TL.LogMessage("GetConfiguration", "Start of btnGetRemoteConfgiuration_Click");
            try
            {
                int clientNumber = 0;
                TL.LogMessage("GetConfiguration", "Connecting to device: " + ipAddressString + ":" + portNumber.ToString());

                string clientHostAddress = string.Format("{0}://{1}:{2}", serviceType, ipAddressString, portNumber.ToString());
                TL.LogMessage("GetConfiguration", "Client host address: " + clientHostAddress);

                RestClient client = new RestClient(clientHostAddress)
                {
                    PreAuthenticate = true
                };
                TL.LogMessage("GetConfiguration", "Creating Authenticator");
                client.Authenticator = new HttpBasicAuthenticator(userName, password);
                TL.LogMessage("GetConfiguration", "Setting timeout");
                RemoteClientDriver.SetClientTimeout(client, 10);

                string      managementUri = string.Format("{0}{1}/{2}", SharedConstants.MANAGEMENT_URL_BASE, SharedConstants.API_VERSION_V1, SharedConstants.MANGEMENT_CONFIGURATION);
                RestRequest request       = new RestRequest(managementUri.ToLowerInvariant(), Method.GET)
                {
                    RequestFormat = DataFormat.Json
                };

                request.AddParameter(SharedConstants.CLIENTID_PARAMETER_NAME, clientNumber.ToString());
                uint transaction = RemoteClientDriver.TransactionNumber();
                request.AddParameter(SharedConstants.CLIENTTRANSACTION_PARAMETER_NAME, transaction.ToString());

                TL.LogMessage("GetConfiguration", "Client Txn ID: " + transaction.ToString() + ", Sending command to remote server");
                IRestResponse response = client.Execute(request);
                string        responseContent;
                if (response.Content.Length > 100)
                {
                    responseContent = response.Content.Substring(0, 100);
                }
                else
                {
                    responseContent = response.Content;
                }
                TL.LogMessage("GetConfiguration", string.Format("Response Status: '{0}', Response: {1}", response.StatusDescription, responseContent));

                if ((response.ResponseStatus == ResponseStatus.Completed) & (response.StatusCode == System.Net.HttpStatusCode.OK))
                {
                    ConfigurationResponse configurationResponse = JsonConvert.DeserializeObject <ConfigurationResponse>(response.Content);
                    ConcurrentDictionary <string, ConfiguredDevice> configuration = configurationResponse.Value;
                    TL.LogMessage("GetConfiguration", "Number of device records: " + configuration.Count);

                    using (Profile profile = new Profile())
                    {
                        foreach (string deviceType in profile.RegisteredDeviceTypes)
                        {
                            TL.LogMessage("GetConfiguration", "Adding item: " + deviceType);
                            registeredDeviceTypes.Add(deviceType); // Remember the device types on this system
                        }

                        foreach (ServedDeviceClient item in this.Controls.OfType <ServedDeviceClient>())
                        {
                            TL.LogMessage(0, 0, 0, "GetConfiguration", "Starting Init");
                            item.InitUI(this, TL);
                            TL.LogMessage(0, 0, 0, "GetConfiguration", "Completed Init");
                            item.DeviceType   = configuration[item.Name].DeviceType;
                            item.ProgID       = configuration[item.Name].ProgID;
                            item.DeviceNumber = configuration[item.Name].DeviceNumber;
                            TL.LogMessage("GetConfiguration", "Completed");
                        }
                        TL.LogMessage("GetConfiguration", "Before RecalculateDevice Numbers");

                        RecalculateDeviceNumbers();
                        TL.LogMessage("GetConfiguration", "After RecalculateDevice Numbers");
                    }

                    // Handle exceptions received from the driver by the remote server
                    if (configurationResponse.DriverException != null)
                    {
                        TL.LogMessageCrLf("GetConfiguration", string.Format("Exception Message: {0}, Exception Number: 0x{1}", configurationResponse.ErrorMessage, configurationResponse.ErrorNumber.ToString("X8")));
                    }
                }
                else
                {
                    if (response.ErrorException != null)
                    {
                        TL.LogMessageCrLf("GetConfiguration", "RestClient exception: " + response.ErrorMessage + "\r\n " + response.ErrorException.ToString());
                        // throw new ASCOM.DriverException(string.Format("Communications exception: {0} - {1}", response.ErrorMessage, response.ResponseStatus), response.ErrorException);
                    }
                    else
                    {
                        TL.LogMessage("GetConfiguration" + " Error", string.Format("RestRequest response status: {0}, HTTP response code: {1}, HTTP response description: {2}", response.ResponseStatus.ToString(), response.StatusCode, response.StatusDescription));
                        // throw new ASCOM.DriverException("ServerConfigurationForm Error - Status: " + response.ResponseStatus + " " + response.StatusDescription);
                    }
                }
            }
            catch (Exception ex)
            {
                TL.LogMessage("GetConfiguration", "Exception: " + ex.ToString());
            }

            TL.LogMessage("GetConfiguration", "End of btnGetRemoteConfgiuration_Click");
        }