57 lines
4.1 KiB
C#
57 lines
4.1 KiB
C#
using System.Reflection;
|
|
using ZB.MOM.WW.OtOpcUa.AdminUI.Uns;
|
|
|
|
/// <summary>Per-circuit synthetic display state; no external service or database is configured.</summary>
|
|
public sealed class FixtureTreeState
|
|
{
|
|
public bool Fail, Populated, UnsFailAfterWrite;
|
|
public int Reads, Unexpected, CallerMutationRequests, UnsMutationRequests;
|
|
public Dictionary<string,IReadOnlyList<RawNode>> CallerChildren=[];
|
|
public IReadOnlyList<UnsNode>? UnsCallerRoots;
|
|
public void RenameUns(string id,string name)
|
|
{
|
|
UnsNode Clone(UnsNode n)
|
|
{
|
|
var copy=new UnsNode {Kind=n.Kind,Key=n.Key,EntityId=n.EntityId,ClusterId=n.ClusterId,DisplayName=n.EntityId==id?name:n.DisplayName,Expanded=n.Expanded,Loaded=n.Loaded,HasLazyChildren=n.HasLazyChildren,ChildCount=n.ChildCount};
|
|
copy.Children.AddRange(n.Children.Select(Clone));return copy;
|
|
}
|
|
if(UnsCallerRoots is not null)UnsCallerRoots=UnsCallerRoots.Select(Clone).ToArray();
|
|
}
|
|
}
|
|
public class FixtureTreeProxy:DispatchProxy
|
|
{
|
|
private FixtureTreeState _state=null!;
|
|
public static T Create<T>(FixtureTreeState state) where T:class
|
|
{var p=Create<T,FixtureTreeProxy>();((FixtureTreeProxy)(object)p)._state=state;return p;}
|
|
protected override object? Invoke(MethodInfo? m,object?[]? a)
|
|
{
|
|
if(m?.Name=="LoadRootsAsync")
|
|
{_state.Reads++;return _state.Fail?Task.FromException<IReadOnlyList<RawNode>>(new IOException("Synthetic local root read failure <script>ignored()</script>")):Task.FromResult<IReadOnlyList<RawNode>>([]);}
|
|
if(m?.Name=="LoadStructureAsync")
|
|
{
|
|
_state.Reads++;
|
|
return _state.Fail?Task.FromException<IReadOnlyList<UnsNode>>(new IOException("Synthetic local structure read failure <script>ignored()</script>"))
|
|
:Task.FromResult<IReadOnlyList<UnsNode>>(_state.UnsCallerRoots??(_state.Populated?[new UnsNode{Key="eq:local",Kind=UnsNodeKind.Equipment,EntityId="local",DisplayName="Synthetic press",HasLazyChildren=true}]:[]));
|
|
}
|
|
if(m?.Name=="LoadAreaAsync" && a?[0] is string area && area.StartsWith("caller-uns-",StringComparison.Ordinal))
|
|
return Task.FromResult<AreaEditDto?>(new(area,"local-area",null,"caller-uns-cluster",[1]));
|
|
if(m?.Name=="LoadLineAsync" && a?[0] is string line && line.StartsWith("caller-uns-",StringComparison.Ordinal))
|
|
return Task.FromResult<LineEditDto?>(new(line,"caller-uns-area","local-line",null,[2]));
|
|
if((m?.Name=="UpdateAreaAsync"||m?.Name=="UpdateLineAsync") && a?[0] is string update && update.StartsWith("caller-uns-",StringComparison.Ordinal))
|
|
{
|
|
_state.UnsMutationRequests++;_state.RenameUns(update,(string)a[1]!);
|
|
if(_state.UnsFailAfterWrite)_state.Fail=true;
|
|
return Task.FromResult(new UnsMutationResult(true,null));
|
|
}
|
|
if(m?.Name=="DeleteClusterAsync" && a?[0] is string cluster && cluster.StartsWith("caller-uns-",StringComparison.Ordinal))
|
|
{_state.UnsMutationRequests++;return Task.FromResult(new UnsMutationResult(false,"Synthetic current cluster blocker <script>ignored()</script>; no real deletion was attempted."));}
|
|
if(m?.Name=="LoadEquipmentPickContextAsync")return Task.FromResult(new EquipmentPickContext([],[]));
|
|
if(m?.Name=="LoadChildrenAsync" && a?[0] is RawNode node && _state.CallerChildren.TryGetValue(node.Key,out var children))return Task.FromResult(children);
|
|
if(m?.Name=="DeleteFolderAsync" && a?[0] is string deleteId && deleteId.StartsWith("caller-",StringComparison.Ordinal))
|
|
{_state.CallerMutationRequests++;return Task.FromResult(new UnsMutationResult(false,"Synthetic local blocker <script>ignored()</script>: this folder has references. No real deletion was attempted."));}
|
|
if(m?.Name=="RenameFolderAsync" && a?[0] is string renameId && renameId.StartsWith("caller-",StringComparison.Ordinal))
|
|
{_state.CallerMutationRequests++;return Task.FromResult(RawRenameResult.Success(["Synthetic local rename advisory <script>ignored()</script>; no database configuration changed."]));}
|
|
_state.Unexpected++;throw new InvalidOperationException("Tree fixture refuses non-root operation: "+m?.Name);
|
|
}
|
|
}
|