using ZB.MOM.WW.MxGateway.Client; using ZB.MOM.WW.MxGateway.Contracts.Proto.Galaxy; namespace ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Browse; /// /// Default : a platform-only DiscoverHierarchy sweep /// over the gateway's — one sweep per platform template /// ($WinPlatform, then $AppEngine), each filtered on that single /// template_chain_contains token with no attributes, so it is a few rows per galaxy rather /// than the full object + attribute dump the address-space discovery pulls. Paged at the /// configured Repository.DiscoverPageSize. /// /// /// ⚠️ One template per request (#864). The gateway's shared /// GalaxyHierarchyProjector.MatchesFilters ANDs the template_chain_contains entries — /// an object must match every token — so a single request carrying both templates matches no /// platform (a $WinPlatform's chain has no $AppEngine, and vice versa) and the probe /// watcher seeded 0 platforms in production. The sweeps are unioned and de-duplicated by tag name. /// public sealed class GatewayGalaxyPlatformSource : IGalaxyPlatformSource { /// Template-chain tokens that identify a platform / engine object. public static readonly IReadOnlyList PlatformTemplates = ["$WinPlatform", "$AppEngine"]; private readonly GalaxyRepositoryClient _client; private readonly int _pageSize; /// Initializes a new instance of the class. /// The gateway's Galaxy repository client. /// Objects per page (Repository.DiscoverPageSize); non-positive → the client default. public GatewayGalaxyPlatformSource(GalaxyRepositoryClient client, int pageSize = GatewayGalaxyHierarchySource.DefaultPageSize) { _client = client ?? throw new ArgumentNullException(nameof(client)); _pageSize = GatewayGalaxyHierarchySource.EffectivePageSize(pageSize); } /// public Task> GetPlatformTagNamesAsync(CancellationToken cancellationToken) => QueryAsync(_client.DiscoverHierarchyRawAsync, _pageSize, cancellationToken); /// /// The platform-only sweeps behind a delegate so it is unit-testable without a channel. One /// paged sweep per entry of ; every request carries exactly that /// one template token and include_attributes = false; the reply is /// re-filtered on the returned template chain (a gateway that ignores the filter still /// yields only platforms) and reduced to non-empty tag names. /// /// The raw DiscoverHierarchy RPC. /// Objects per page. /// Cancellation token for the walk. /// Distinct platform tag names: $WinPlatform sweep first, then $AppEngine, each in gateway order. internal static async Task> QueryAsync( Func> discover, int pageSize, CancellationToken cancellationToken) { ArgumentNullException.ThrowIfNull(discover); var effective = GatewayGalaxyHierarchySource.EffectivePageSize(pageSize); var names = new List(); var seen = new HashSet(StringComparer.OrdinalIgnoreCase); foreach (var template in PlatformTemplates) { var seenPageTokens = new HashSet(StringComparer.Ordinal); var pageToken = string.Empty; do { var request = new DiscoverHierarchyRequest { PageSize = effective, PageToken = pageToken, IncludeAttributes = false, }; // Exactly ONE token per request: the gateway ANDs template filters (#864). request.TemplateChainContains.Add(template); var reply = await discover(request, cancellationToken).ConfigureAwait(false); foreach (var obj in reply.Objects) { if (string.IsNullOrEmpty(obj.TagName) || !IsPlatform(obj)) continue; if (seen.Add(obj.TagName)) names.Add(obj.TagName); } pageToken = reply.NextPageToken ?? string.Empty; if (!string.IsNullOrWhiteSpace(pageToken) && !seenPageTokens.Add(pageToken)) { throw new InvalidOperationException( $"Galaxy DiscoverHierarchy (platform query, {template}) returned a repeated page token '{pageToken}' — refusing to loop."); } } while (!string.IsNullOrWhiteSpace(pageToken)); } return names; } private static bool IsPlatform(GalaxyObject obj) => obj.TemplateChain.Count == 0 // a gateway that strips the chain already filtered server-side || obj.TemplateChain.Any(t => PlatformTemplates.Contains(t, StringComparer.OrdinalIgnoreCase)); }