using System.Reflection; using System.Security.Claims; using Microsoft.AspNetCore.Components.Authorization; using Microsoft.EntityFrameworkCore; using ZB.MOM.WW.OtOpcUa.AdminUI.Browsing; using ZB.MOM.WW.OtOpcUa.Commons.Browsing; using ZB.MOM.WW.OtOpcUa.Configuration; using RawWorkflowCanary; using RawWorkflowCanary.Components; using ZB.MOM.WW.OtOpcUa.AdminUI.Uns; var builder=WebApplication.CreateBuilder(args); builder.WebHost.UseStaticWebAssets(); builder.Services.AddRazorComponents().AddInteractiveServerComponents(); builder.Services.AddScoped(); builder.Services.AddAuthorizationCore(o=>o.AddPolicy(ZB.MOM.WW.OtOpcUa.Security.Auth.AdminUiPolicies.DriverOperator,p=>p.RequireRole("Operator","Administrator"))); builder.Services.AddScoped(); builder.Services.AddScoped(sp=>FixtureBrowser.Create(sp.GetRequiredService())); builder.Services.AddDbContextFactory(o=>o.UseInMemoryDatabase("raw-workflow-local")); builder.Services.AddScoped(); builder.Services.AddScoped(sp=>WorkflowProxy.Create(sp.GetRequiredService())); var app=builder.Build();app.UseStaticFiles();app.UseAntiforgery();app.MapRazorComponents().AddInteractiveServerRenderMode();app.Run(); namespace RawWorkflowCanary { public sealed class WorkflowState {public int Requests {get;set;}public int RebirthRequests {get;set;} public int OpenRequests {get;set;} public int ClosedSessions {get;set;} public event Action? Changed; public void Notify()=>Changed?.Invoke(); public string DriverType {get;set;}="AbCip";} public class WorkflowProxy:DispatchProxy { private WorkflowState _state=null!; public static IRawTreeService Create(WorkflowState state){var proxy=Create();((WorkflowProxy)proxy)._state=state;return proxy;} protected override object? Invoke(MethodInfo? method,object?[]? args)=>method!.Name switch { "LoadMergedProbeConfigAsync"=>Task.FromResult<(string DriverType,string MergedConfigJson)?>((_state.DriverType,"{}")), "LoadDeviceDriverInstanceIdAsync"=>Task.FromResult("synthetic-owner"), "ImportTagsAsync"=>Import((IReadOnlyList)args![1]!), "CreateTagAsync"=>CreateTag(), _=>throw new InvalidOperationException("Operational service refused by local fixture: "+method.Name) }; private async Task Import(IReadOnlyList rows){_state.Requests++;_state.Notify();await Task.Delay(900);return new(rows.Count,[]);} private async Task CreateTag(){_state.Requests++;_state.Notify();await Task.Delay(900);return new(true,null);} } public sealed class FixturePrincipal:AuthenticationStateProvider {public override Task GetAuthenticationStateAsync()=>Task.FromResult(new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity([new(ClaimTypes.Name,"local-fixture"),new(ClaimTypes.Role,"Operator")],"fixture"))));} public class FixtureBrowser:DispatchProxy { private WorkflowState _state=null!; public static IBrowserSessionService Create(WorkflowState state){var p=Create();((FixtureBrowser)p)._state=state;return p;} protected override object? Invoke(MethodInfo? m,object?[]? args)=>m!.Name switch { "CanBrowse"=>true, "OpenAsync"=>Open(), "CloseAsync"=>Close(), "CanRequestRebirth"=>_state.DriverType=="Mqtt", "RootAsync"=>Task.FromResult>(_state.DriverType=="Mqtt"?[]:[new("SyntheticTag1","Synthetic tag one",BrowseNodeKind.Leaf,false),new("SyntheticTag2","Synthetic tag two",BrowseNodeKind.Leaf,false)]), "AttributesAsync"=>Task.FromResult>([]), "RequestRebirthAsync"=>Rebirth(), _=>throw new InvalidOperationException("Unexpected fixture operation: "+m.Name) }; private async Task Open(){_state.OpenRequests++;_state.Notify();await Task.Delay(700);return new(true,null,Guid.NewGuid());} private Task Close(){_state.ClosedSessions++;_state.Notify();return Task.CompletedTask;} private async Task Rebirth(){_state.RebirthRequests++;_state.Notify();await Task.Delay(900);return 1;} } }