namespace ScadaLink.SiteRuntime.Scripts;
///
/// Scope-aware view onto the instance's attributes, anchored at a path prefix.
/// Attributes["X"] on the root scope resolves to canonical name "X";
/// on a composition with prefix "TempSensor" it resolves to "TempSensor.X".
/// Reads block on the actor Ask; async variants are provided for callers
/// that prefer to await explicitly.
///
public class AttributeAccessor
{
private readonly ScriptRuntimeContext _ctx;
/// Canonical-name prefix, e.g. "" for root or "TempSensor" for a composition.
public string ScopePrefix { get; }
public AttributeAccessor(ScriptRuntimeContext ctx, string prefix)
{
_ctx = ctx;
ScopePrefix = prefix;
}
public string Resolve(string key) =>
ScopePrefix.Length == 0 ? key : ScopePrefix + "." + key;
public object? this[string key]
{
get => _ctx.GetAttribute(Resolve(key)).GetAwaiter().GetResult();
set => _ctx.SetAttribute(Resolve(key), value?.ToString() ?? string.Empty);
}
public Task