|
| 1 | +using System; |
| 2 | +using System.Collections.Immutable; |
| 3 | +using System.Globalization; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using System.Web; |
| 7 | +using TianWen.Lib.Devices; |
| 8 | +using TianWen.Lib.Sequencing; |
| 9 | + |
| 10 | +namespace TianWen.UI.Abstractions; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// Pure functions for profile/equipment manipulation. Shared between CLI and GUI. |
| 14 | +/// All methods return new ProfileData (immutable record with-expressions). |
| 15 | +/// </summary> |
| 16 | +public static class EquipmentActions |
| 17 | +{ |
| 18 | + public static async Task<Profile> CreateProfileAsync(string name, IExternal external, CancellationToken ct) |
| 19 | + { |
| 20 | + var profile = new Profile(Guid.NewGuid(), name, ProfileData.Empty); |
| 21 | + await profile.SaveAsync(external, ct); |
| 22 | + return profile; |
| 23 | + } |
| 24 | + |
| 25 | + public static ProfileData AssignMount(ProfileData data, Uri mountUri) |
| 26 | + => data with { Mount = mountUri }; |
| 27 | + |
| 28 | + public static ProfileData AssignGuider(ProfileData data, Uri guiderUri) |
| 29 | + => data with { Guider = guiderUri }; |
| 30 | + |
| 31 | + public static ProfileData AssignGuiderCamera(ProfileData data, Uri cameraUri) |
| 32 | + => data with { GuiderCamera = cameraUri }; |
| 33 | + |
| 34 | + public static ProfileData AssignGuiderFocuser(ProfileData data, Uri focuserUri) |
| 35 | + => data with { GuiderFocuser = focuserUri }; |
| 36 | + |
| 37 | + public static ProfileData SetOagOtaIndex(ProfileData data, int otaIndex) |
| 38 | + => data with { OAG_OTA_Index = otaIndex }; |
| 39 | + |
| 40 | + public static ProfileData SetSite(ProfileData data, double lat, double lon, double? elevation = null) |
| 41 | + { |
| 42 | + var query = HttpUtility.ParseQueryString(data.Mount.Query); |
| 43 | + query[DeviceQueryKey.Latitude.Key] = lat.ToString(CultureInfo.InvariantCulture); |
| 44 | + query[DeviceQueryKey.Longitude.Key] = lon.ToString(CultureInfo.InvariantCulture); |
| 45 | + if (elevation.HasValue) |
| 46 | + { |
| 47 | + query[DeviceQueryKey.Elevation.Key] = elevation.Value.ToString(CultureInfo.InvariantCulture); |
| 48 | + } |
| 49 | + var builder = new UriBuilder(data.Mount) { Query = query.ToString() }; |
| 50 | + return data with { Mount = builder.Uri }; |
| 51 | + } |
| 52 | + |
| 53 | + public static ProfileData AddOTA(ProfileData data, OTAData ota) |
| 54 | + => data with { OTAs = data.OTAs.Add(ota) }; |
| 55 | + |
| 56 | + public static ProfileData RemoveOTA(ProfileData data, int index) |
| 57 | + => index >= 0 && index < data.OTAs.Length |
| 58 | + ? data with { OTAs = data.OTAs.RemoveAt(index) } |
| 59 | + : data; |
| 60 | + |
| 61 | + public static ProfileData AssignDeviceToOTA(ProfileData data, int otaIndex, DeviceType deviceType, Uri deviceUri) |
| 62 | + { |
| 63 | + if (otaIndex < 0 || otaIndex >= data.OTAs.Length) |
| 64 | + { |
| 65 | + return data; |
| 66 | + } |
| 67 | + |
| 68 | + var ota = data.OTAs[otaIndex]; |
| 69 | + var updated = deviceType switch |
| 70 | + { |
| 71 | + DeviceType.Camera => ota with { Camera = deviceUri }, |
| 72 | + DeviceType.Focuser => ota with { Focuser = deviceUri }, |
| 73 | + DeviceType.FilterWheel => ota with { FilterWheel = deviceUri }, |
| 74 | + DeviceType.CoverCalibrator => ota with { Cover = deviceUri }, |
| 75 | + _ => ota |
| 76 | + }; |
| 77 | + |
| 78 | + return data with { OTAs = data.OTAs.SetItem(otaIndex, updated) }; |
| 79 | + } |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// Checks if a device URI is assigned anywhere in the profile. |
| 83 | + /// </summary> |
| 84 | + public static bool IsDeviceAssigned(ProfileData data, Uri deviceUri) |
| 85 | + { |
| 86 | + if (data.Mount == deviceUri || data.Guider == deviceUri) |
| 87 | + { |
| 88 | + return true; |
| 89 | + } |
| 90 | + if (data.GuiderCamera == deviceUri || data.GuiderFocuser == deviceUri) |
| 91 | + { |
| 92 | + return true; |
| 93 | + } |
| 94 | + |
| 95 | + foreach (var ota in data.OTAs) |
| 96 | + { |
| 97 | + if (ota.Camera == deviceUri || ota.Focuser == deviceUri || |
| 98 | + ota.FilterWheel == deviceUri || ota.Cover == deviceUri) |
| 99 | + { |
| 100 | + return true; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return false; |
| 105 | + } |
| 106 | + |
| 107 | + /// <summary> |
| 108 | + /// Returns a human-readable label for a device URI, using the registry if available. |
| 109 | + /// </summary> |
| 110 | + public static string DeviceLabel(Uri? uri, IDeviceUriRegistry? registry) |
| 111 | + { |
| 112 | + if (uri is null || uri == NoneDevice.Instance.DeviceUri) |
| 113 | + { |
| 114 | + return "(none)"; |
| 115 | + } |
| 116 | + |
| 117 | + if (registry is not null && registry.TryGetDeviceFromUri(uri, out var device)) |
| 118 | + { |
| 119 | + return device.DisplayName; |
| 120 | + } |
| 121 | + |
| 122 | + // Fallback: extract from URI path |
| 123 | + var path = uri.AbsolutePath.TrimStart('/'); |
| 124 | + return path.Length > 0 ? path : uri.ToString(); |
| 125 | + } |
| 126 | + |
| 127 | + /// <summary> |
| 128 | + /// Extracts site coordinates from a mount URI, if present. |
| 129 | + /// </summary> |
| 130 | + public static (double Lat, double Lon, double? Elev)? GetSiteFromMount(Uri mountUri) |
| 131 | + { |
| 132 | + if (mountUri == NoneDevice.Instance.DeviceUri) |
| 133 | + { |
| 134 | + return null; |
| 135 | + } |
| 136 | + |
| 137 | + var query = HttpUtility.ParseQueryString(mountUri.Query); |
| 138 | + var latStr = query[DeviceQueryKey.Latitude.Key]; |
| 139 | + var lonStr = query[DeviceQueryKey.Longitude.Key]; |
| 140 | + var elevStr = query[DeviceQueryKey.Elevation.Key]; |
| 141 | + |
| 142 | + if (latStr is not null && lonStr is not null && |
| 143 | + double.TryParse(latStr, CultureInfo.InvariantCulture, out var lat) && |
| 144 | + double.TryParse(lonStr, CultureInfo.InvariantCulture, out var lon)) |
| 145 | + { |
| 146 | + double? elev = elevStr is not null && double.TryParse(elevStr, CultureInfo.InvariantCulture, out var e) ? e : null; |
| 147 | + return (lat, lon, elev); |
| 148 | + } |
| 149 | + |
| 150 | + return null; |
| 151 | + } |
| 152 | +} |
0 commit comments