/// <summary> /// Initializes a new instance of the <see cref="TelescopeBaseClass"/> class. /// Must be public for COM registration. /// </summary> public TelescopeBaseClass(string RequiredDriverNumber, string RequiredDriverDisplayName, string RequiredProgId) { try { // Initialise variables unique to this particular driver with values passed from the calling class DriverNumber = RequiredDriverNumber; DriverDisplayName = RequiredDriverDisplayName; // Driver description that displays in the ASCOM Chooser. DriverProgId = RequiredProgId; if (TL == null) { TL = new TraceLoggerPlus("", string.Format(SharedConstants.TRACELOGGER_NAME_FORMAT_STRING, DriverNumber, DEVICE_TYPE)); } RemoteClientDriver.ReadProfile(clientNumber, TL, DEVICE_TYPE, DriverProgId, ref traceState, ref debugTraceState, ref ipAddressString, ref portNumber, ref remoteDeviceNumber, ref serviceType, ref establishConnectionTimeout, ref standardServerResponseTimeout, ref longServerResponseTimeout, ref userName, ref password, ref manageConnectLocally, ref imageArrayTransferType, ref imageArrayCompression); Version version = Assembly.GetEntryAssembly().GetName().Version; TL.LogMessage(clientNumber, DEVICE_TYPE, "Starting initialisation, Version: " + version.ToString()); clientNumber = RemoteClientDriver.GetUniqueClientNumber(); TL.LogMessage(clientNumber, DEVICE_TYPE, "This instance's unique client number: " + clientNumber); RemoteClientDriver.ConnectToRemoteServer(ref client, ipAddressString, portNumber, serviceType, TL, clientNumber, DEVICE_TYPE, standardServerResponseTimeout, userName, password); URIBase = string.Format("{0}{1}/{2}/{3}/", SharedConstants.API_URL_BASE, SharedConstants.API_VERSION_V1, DEVICE_TYPE, remoteDeviceNumber.ToString()); TL.LogMessage(clientNumber, DEVICE_TYPE, "This devices's base URI: " + URIBase); TL.LogMessage(clientNumber, DEVICE_TYPE, "Establish communications timeout: " + establishConnectionTimeout.ToString()); TL.LogMessage(clientNumber, DEVICE_TYPE, "Standard server response timeout: " + standardServerResponseTimeout.ToString()); TL.LogMessage(clientNumber, DEVICE_TYPE, "Long server response timeout: " + longServerResponseTimeout.ToString()); TL.LogMessage(clientNumber, DEVICE_TYPE, string.Format("User name is Null or Empty: {0}, User name is Null or White Space: {1}", string.IsNullOrEmpty(userName), string.IsNullOrWhiteSpace(userName))); TL.LogMessage(clientNumber, DEVICE_TYPE, string.Format("User name length: {0}", password.Length)); TL.LogMessage(clientNumber, DEVICE_TYPE, string.Format("Password is Null or Empty: {0}, Password is Null or White Space: {1}", string.IsNullOrEmpty(password), string.IsNullOrWhiteSpace(password))); TL.LogMessage(clientNumber, DEVICE_TYPE, string.Format("Password length: {0}", password.Length)); TL.LogMessage(clientNumber, DEVICE_TYPE, "Completed initialisation"); } catch (Exception ex) { TL.LogMessageCrLf(clientNumber, DEVICE_TYPE, ex.ToString()); } }
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"); }