public ActionResult <IntArray1DResponse> Get(int ClientID, int ClientTransactionID) { try { int[] focusOffsets = Program.Simulator.FocusOffsets; string focusOffsetsList = string.Join(" ", focusOffsets); Program.TraceLogger.LogMessage(methodName + " Get", focusOffsetsList); return(new IntArray1DResponse(ClientTransactionID, ClientID, methodName, focusOffsets)); } catch (Exception ex) { Program.TraceLogger.LogMessage(methodName + " Get", string.Format("Exception: {0}", ex.ToString())); IntArray1DResponse response = new IntArray1DResponse(ClientTransactionID, ClientID, methodName, new int[1]); response.ErrorMessage = ex.Message; response.ErrorNumber = ex.HResult - Program.ASCOM_ERROR_NUMBER_OFFSET; return(response); } }
/// <summary> /// Get Alpaca device information from the management API /// </summary> /// <param name="deviceIpEndPointObject"></param> private async void GetAlpacaDeviceInformation(object deviceIpEndPointObject) { IPEndPoint deviceIpEndPoint = deviceIpEndPointObject as IPEndPoint; string hostIpAndPort; // Create a text version of the host IP address and port switch (deviceIpEndPoint.AddressFamily) { case AddressFamily.InterNetwork: hostIpAndPort = $"{serviceType.ToString().ToLowerInvariant()}://{deviceIpEndPoint}"; break; case AddressFamily.InterNetworkV6: string scopeId = $"%{deviceIpEndPoint.Address.ScopeId}"; // Obtain the IPv6 scope ID in text form (if present) hostIpAndPort = $"{serviceType.ToString().ToLowerInvariant()}://{deviceIpEndPoint.ToString().Replace(scopeId, string.Empty)}"; // Create the overall URI break; default: hostIpAndPort = $"{serviceType.ToString().ToLowerInvariant()}://{deviceIpEndPoint}"; break; } try { LogMessage("GetAlpacaDeviceInformation", $"Host URL: {hostIpAndPort} DISCOVERY TIMEOUT: {discoveryTime} ({discoveryTime * 1000d})"); // Wait for API version result and process it LogMessage("GetAlpacaDeviceInformation", $"About to get version information from {hostIpAndPort}/management/apiversions at IP endpoint {deviceIpEndPoint.Address} {deviceIpEndPoint.AddressFamily}"); string apiVersionsJsonResponse = await httpClient.GetStringAsync($"{hostIpAndPort}/management/apiversions"); LogMessage("GetAlpacaDeviceInformation", $"Received JSON response from {hostIpAndPort}: {apiVersionsJsonResponse}"); IntArray1DResponse apiVersionsResponse = JsonSerializer.Deserialize <IntArray1DResponse>(apiVersionsJsonResponse, new JsonSerializerOptions { PropertyNameCaseInsensitive = strictCasing }); lock (deviceListLockObject) // Make sure that only one thread can update the device list dictionary at a time { alpacaDeviceList[deviceIpEndPoint].SupportedInterfaceVersions = apiVersionsResponse.Value; alpacaDeviceList[deviceIpEndPoint].StatusMessage = ""; // Clear the status field to indicate that this first call was successful } RaiseAnAlpacaDevicesChangedEvent(); // Device list was changed so set the changed flag // Wait for device description result and process it string deviceDescriptionJsonResponse = await httpClient.GetStringAsync($"{hostIpAndPort}/management/v1/description"); LogMessage("GetAlpacaDeviceInformation", $"Received JSON response from {hostIpAndPort}: {deviceDescriptionJsonResponse}"); var deviceDescriptionResponse = JsonSerializer.Deserialize <AlpacaDescriptionResponse>(deviceDescriptionJsonResponse, new JsonSerializerOptions { PropertyNameCaseInsensitive = strictCasing }); lock (deviceListLockObject) // Make sure that only one thread can update the device list dictionary at a time { alpacaDeviceList[deviceIpEndPoint].ServerName = deviceDescriptionResponse.Value.ServerName; alpacaDeviceList[deviceIpEndPoint].Manufacturer = deviceDescriptionResponse.Value.Manufacturer; alpacaDeviceList[deviceIpEndPoint].ManufacturerVersion = deviceDescriptionResponse.Value.ManufacturerVersion; alpacaDeviceList[deviceIpEndPoint].Location = deviceDescriptionResponse.Value.Location; } RaiseAnAlpacaDevicesChangedEvent(); // Device list was changed so set the changed flag // Wait for configured devices result and process it string configuredDevicesJsonResponse = await httpClient.GetStringAsync($"{hostIpAndPort}/management/v1/configureddevices"); LogMessage("GetAlpacaDeviceInformation", $"Received JSON response from {hostIpAndPort}: {configuredDevicesJsonResponse}"); AlpacaConfiguredDevicesResponse configuredDevicesResponse = JsonSerializer.Deserialize <AlpacaConfiguredDevicesResponse>(configuredDevicesJsonResponse, new JsonSerializerOptions { PropertyNameCaseInsensitive = strictCasing }); lock (deviceListLockObject) // Make sure that only one thread can update the device list dictionary at a time { alpacaDeviceList[deviceIpEndPoint].ConfiguredDevices = configuredDevicesResponse.Value; LogMessage("GetAlpacaDeviceInformation", $"Listing configured devices"); foreach (AlpacaConfiguredDevice configuredDevce in alpacaDeviceList[deviceIpEndPoint].ConfiguredDevices) { LogMessage("GetAlpacaDeviceInformation", $"Found configured device: {configuredDevce.DeviceName} {configuredDevce.DeviceType} {configuredDevce.UniqueID}"); } LogMessage("GetAlpacaDeviceInformation", $"Completed list of configured devices"); } RaiseAnAlpacaDevicesChangedEvent(); // Device list was changed so set the changed flag LogMessage("GetAlpacaDeviceInformation", $"COMPLETED API tasks for {hostIpAndPort}"); } catch (TaskCanceledException) { LogMessage("GetAlpacaDeviceInformation", $"Task cancelled while getting information from {hostIpAndPort}"); } catch (TimeoutException) { LogMessage("GetAlpacaDeviceInformation", $"Timed out getting information from {hostIpAndPort}"); } catch (Exception ex) { // Something went wrong so log the issue and sent a message to the user LogMessage("GetAlpacaDeviceInformation", $"GetAlpacaDescriptions exception: {ex}"); lock (deviceListLockObject) // Make sure that only one thread can update the device list dictionary at a time { alpacaDeviceList[deviceIpEndPoint].StatusMessage = ex.Message; RaiseAnAlpacaDevicesChangedEvent(); // Device list was changed so set the changed flag } } }