using System.Data.Common; using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Services; using ZB.MOM.WW.ScadaBridge.Commons.Messages.Notification; using ZB.MOM.WW.ScadaBridge.Commons.Types; using ZB.MOM.WW.ScadaBridge.Commons.Types.Scripts; namespace ZB.MOM.WW.ScadaBridge.ScriptAnalysis; /// /// A compile-only globals type that mirrors the real SiteRuntime /// ScriptGlobals (+ ScriptRuntimeContext helper surface) /// member-for-member, so a real instance / shared / on-trigger-handler script /// BINDS against it at design time. It is NEVER executed — every member body /// throws or returns default. The /// design-time deploy gate compiles candidate scripts against this type /// via /// to catch undefined symbols and signature mismatches without touching the /// site runtime. /// /// /// Keeping this surface faithful is enforced by the /// RoslynScriptCompilerTests "representative real script" corpus — if a /// member or signature drifts from the runtime ScriptGlobals, that test /// fails. /// /// public sealed class ScriptCompileSurface { private const string CompileOnly = "compile-only surface"; /// Mirrors ScriptGlobals.Instance. public CompileInstance Instance { get; set; } = null!; /// Mirrors ScriptGlobals.Parameters. public ScriptParameters Parameters { get; set; } = new(); /// Mirrors ScriptGlobals.CancellationToken. public CancellationToken CancellationToken { get; set; } /// Mirrors ScriptGlobals.Alarm. public AlarmContext? Alarm { get; set; } /// Mirrors ScriptGlobals.Scope. public ScriptScope Scope { get; set; } = ScriptScope.Root; /// Mirrors ScriptGlobals.ExternalSystem (delegates to Instance). public CompileExternalSystem ExternalSystem => Instance.ExternalSystem; /// Mirrors ScriptGlobals.Database. public CompileDatabase Database => Instance.Database; /// Mirrors ScriptGlobals.Notify. public CompileNotify Notify => Instance.Notify; /// Mirrors ScriptGlobals.Scripts. public CompileScripts Scripts => Instance.Scripts; /// Mirrors ScriptGlobals.Attributes. public CompileAttributeAccessor Attributes => throw new NotSupportedException(CompileOnly); /// Mirrors ScriptGlobals.Children. public CompileChildrenAccessor Children => throw new NotSupportedException(CompileOnly); /// Mirrors ScriptGlobals.Parent. public CompileCompositionAccessor? Parent => throw new NotSupportedException(CompileOnly); /// Compile-only mirror of ScriptRuntimeContext (the Instance global). public sealed class CompileInstance { /// Mirrors ScriptRuntimeContext.GetAttribute. /// The name of the attribute to read. /// The attribute's current value. public Task GetAttribute(string attributeName) => throw new NotSupportedException(CompileOnly); /// Mirrors ScriptRuntimeContext.SetAttribute. /// The name of the attribute to write. /// The value to write to the attribute. /// A task that represents the asynchronous operation. public Task SetAttribute(string attributeName, string value) => throw new NotSupportedException(CompileOnly); /// Mirrors ScriptRuntimeContext.CallScript. /// The name of the script to invoke. /// Optional parameters passed to the script. /// The result returned by the invoked script. public Task CallScript(string scriptName, object? parameters = null) => throw new NotSupportedException(CompileOnly); /// Mirrors ScriptRuntimeContext.ExternalSystem. public CompileExternalSystem ExternalSystem => throw new NotSupportedException(CompileOnly); /// Mirrors ScriptRuntimeContext.Database. public CompileDatabase Database => throw new NotSupportedException(CompileOnly); /// Mirrors ScriptRuntimeContext.Notify. public CompileNotify Notify => throw new NotSupportedException(CompileOnly); /// Mirrors ScriptRuntimeContext.Scripts. public CompileScripts Scripts => throw new NotSupportedException(CompileOnly); /// Mirrors ScriptRuntimeContext.Tracking. public CompileTracking Tracking => throw new NotSupportedException(CompileOnly); } /// Compile-only mirror of ScriptRuntimeContext.ExternalSystemHelper. public sealed class CompileExternalSystem { /// Mirrors ExternalSystemHelper.Call. /// The name of the external system to call. /// The name of the method to invoke on the external system. /// Optional parameters passed to the call. /// Token used to cancel the call. /// The result of the external system call. public Task Call( string systemName, string methodName, IReadOnlyDictionary? parameters = null, CancellationToken cancellationToken = default) => throw new NotSupportedException(CompileOnly); /// Mirrors ExternalSystemHelper.CachedCall. /// The name of the external system to call. /// The name of the method to invoke on the external system. /// Optional parameters passed to the call. /// Token used to cancel the call. /// A tracking handle for the store-and-forward operation. public Task CachedCall( string systemName, string methodName, IReadOnlyDictionary? parameters = null, CancellationToken cancellationToken = default) => throw new NotSupportedException(CompileOnly); } /// Compile-only mirror of ScriptRuntimeContext.DatabaseHelper. public sealed class CompileDatabase { /// Mirrors DatabaseHelper.Connection. /// The name of the configured database connection. /// Token used to cancel the operation. /// An open connection to the named database. public Task Connection(string name, CancellationToken cancellationToken = default) => throw new NotSupportedException(CompileOnly); /// Mirrors DatabaseHelper.CachedWrite. /// The name of the configured database connection. /// The SQL statement to execute. /// Optional parameters bound to the SQL statement. /// Token used to cancel the operation. /// A tracking handle for the store-and-forward write. public Task CachedWrite( string name, string sql, IReadOnlyDictionary? parameters = null, CancellationToken cancellationToken = default) => throw new NotSupportedException(CompileOnly); } /// Compile-only mirror of ScriptRuntimeContext.NotifyHelper. public sealed class CompileNotify { /// Mirrors NotifyHelper.To. /// The name of the notification list to target. /// A target used to send a notification to the named list. public CompileNotifyTarget To(string listName) => throw new NotSupportedException(CompileOnly); /// Mirrors NotifyHelper.Status. /// The identifier of the notification to look up. /// The delivery status of the notification. public Task Status(string notificationId) => throw new NotSupportedException(CompileOnly); } /// Compile-only mirror of ScriptRuntimeContext.NotifyTarget. public sealed class CompileNotifyTarget { /// Mirrors NotifyTarget.Send. /// The notification subject. /// The notification message body. /// Token used to cancel the operation. /// The identifier of the enqueued notification. public Task Send(string subject, string message, CancellationToken cancellationToken = default) => throw new NotSupportedException(CompileOnly); } /// Compile-only mirror of ScriptRuntimeContext.ScriptCallHelper. public sealed class CompileScripts { /// Mirrors ScriptCallHelper.CallShared. /// The name of the shared script to invoke. /// Optional parameters passed to the script. /// Token used to cancel the operation. /// The result returned by the invoked shared script. public Task CallShared( string scriptName, object? parameters = null, CancellationToken cancellationToken = default) => throw new NotSupportedException(CompileOnly); } /// Compile-only mirror of ScriptRuntimeContext.TrackingHelper. public sealed class CompileTracking { /// Mirrors TrackingHelper.Status. /// The tracking handle returned by a cached call or write. /// Token used to cancel the operation. /// The current tracking status snapshot, or null if not found. public Task Status( TrackedOperationId trackedOperationId, CancellationToken cancellationToken = default) => throw new NotSupportedException(CompileOnly); } /// Compile-only mirror of AttributeAccessor. public sealed class CompileAttributeAccessor { /// Mirrors AttributeAccessor.this[string]. /// The name of the attribute to get or set. public object? this[string key] { get => throw new NotSupportedException(CompileOnly); set => throw new NotSupportedException(CompileOnly); } /// Mirrors AttributeAccessor.GetAsync. /// The name of the attribute to read. /// The attribute's current value. public Task GetAsync(string key) => throw new NotSupportedException(CompileOnly); /// Mirrors AttributeAccessor.SetAsync. /// The name of the attribute to write. /// The value to write to the attribute. /// A task that represents the asynchronous operation. public Task SetAsync(string key, object? value) => throw new NotSupportedException(CompileOnly); /// Mirrors AttributeAccessor.Resolve. /// The name of the attribute to resolve. /// The resolved attribute path. public string Resolve(string key) => throw new NotSupportedException(CompileOnly); /// Mirrors AttributeAccessor.WaitAsync. /// The name of the attribute to wait on. /// The value to wait for. /// The maximum time to wait. /// Whether the attribute must also have good quality before the wait succeeds. /// true if the target value was observed before the timeout elapsed; otherwise false. public Task WaitAsync(string key, object? targetValue, TimeSpan timeout, bool requireGoodQuality = false) => throw new NotSupportedException(CompileOnly); /// Mirrors AttributeAccessor.WaitAsync. /// The name of the attribute to wait on. /// A predicate evaluated against successive attribute values. /// The maximum time to wait. /// Whether the attribute must also have good quality before the wait succeeds. /// true if the predicate was satisfied before the timeout elapsed; otherwise false. public Task WaitAsync(string key, Func predicate, TimeSpan timeout, bool requireGoodQuality = false) => throw new NotSupportedException(CompileOnly); /// Mirrors AttributeAccessor.WaitForAsync. /// The name of the attribute to wait on. /// The value to wait for. /// The maximum time to wait. /// Whether the attribute must also have good quality before the wait succeeds. /// The outcome of the wait, including whether it succeeded or timed out. public Task WaitForAsync(string key, object? targetValue, TimeSpan timeout, bool requireGoodQuality = false) => throw new NotSupportedException(CompileOnly); /// Mirrors AttributeAccessor.WaitForAsync. /// The name of the attribute to wait on. /// A predicate evaluated against successive attribute values. /// The maximum time to wait. /// Whether the attribute must also have good quality before the wait succeeds. /// The outcome of the wait, including whether it succeeded or timed out. public Task WaitForAsync(string key, Func predicate, TimeSpan timeout, bool requireGoodQuality = false) => throw new NotSupportedException(CompileOnly); /// Mirrors AttributeAccessor.WriteBatchAndWaitAsync. /// The set of attribute values to write as a batch. /// The name of the flag attribute signaling the write. /// The value written to the flag attribute. /// The name of the attribute to observe for a response. /// The value to wait for on the response attribute. /// The maximum time to wait for the response. /// true if the response value was observed before the timeout elapsed; otherwise false. public Task WriteBatchAndWaitAsync(IReadOnlyDictionary values, string flagKey, object? flagValue, string responseKey, object? responseValue, TimeSpan timeout) => throw new NotSupportedException(CompileOnly); } /// Compile-only mirror of ChildrenAccessor. public sealed class CompileChildrenAccessor { /// Mirrors ChildrenAccessor.this[string]. /// The name of the composed child to access. public CompileCompositionAccessor this[string compositionName] => throw new NotSupportedException(CompileOnly); } /// Compile-only mirror of CompositionAccessor. public sealed class CompileCompositionAccessor { /// Mirrors CompositionAccessor.Attributes. public CompileAttributeAccessor Attributes => throw new NotSupportedException(CompileOnly); /// Mirrors CompositionAccessor.CallScript. /// The name of the script to invoke. /// Optional parameters passed to the script. /// The result returned by the invoked script. public Task CallScript(string scriptName, object? parameters = null) => throw new NotSupportedException(CompileOnly); /// Mirrors CompositionAccessor.ResolveScript. /// The name of the script to resolve. /// The resolved script path. public string ResolveScript(string scriptName) => throw new NotSupportedException(CompileOnly); /// Mirrors CompositionAccessor.Path. public string Path => throw new NotSupportedException(CompileOnly); } }