using Microsoft.Extensions.DependencyInjection; using RobotNet10.Script; using RobotNet10.Script.IO; using RobotNet10.ScriptEngine.IO; using RobotNet10.ScriptEngine.Shared; namespace RobotNet10.ScriptEngine.Models; public class ScriptEngineGlobals(ILogger logger, IServiceScopeFactory scopeFactory) : IScriptGlobals { public RobotNet10.Script.ILogger Logger => logger; public Guid CreateMission(string name, params object[] args) { using var scope = scopeFactory.CreateScope(); var missionManager = scope.ServiceProvider.GetRequiredService(); var result = missionManager.CreateMission(name, args); if (result.IsSuccess) { return result.Data; } throw new InvalidOperationException($"Failed to create mission '{name}': {result.Message}"); } public bool CancelMission(Guid id, string reason) { using var scope = scopeFactory.CreateScope(); var missionManager = scope.ServiceProvider.GetRequiredService(); var mission = missionManager.GetMission(id); if (mission == null) { return false; } try { mission.Cancel(reason); return true; } catch { return false; } } public void DisableTask(string name) { using var scope = scopeFactory.CreateScope(); var taskManager = scope.ServiceProvider.GetRequiredService(); var result = taskManager.DisableTask(name); if (!result.IsSuccess) { throw new InvalidOperationException($"Failed to disable task '{name}': {result.Message}"); } } public void EnableTask(string name) { using var scope = scopeFactory.CreateScope(); var taskManager = scope.ServiceProvider.GetRequiredService(); var result = taskManager.EnableTask(name); if (!result.IsSuccess) { throw new InvalidOperationException($"Failed to enable task '{name}': {result.Message}"); } } // IO Connection Factory Methods public IHttpConnection CreateHttpConnection(string baseUrl, int timeoutSeconds = 30) { return new HttpConnection(baseUrl, TimeSpan.FromSeconds(timeoutSeconds)); } public IModbusTcpConnection CreateModbusTcpConnection(string ipAddress, int port = 502, byte slaveId = 1) { return new ModbusTcpConnection(ipAddress, port, slaveId); } public IProfiNetConnection CreateProfiNetConnection(string ipAddress, int slot = 1, int subslot = 1) { return new ProfiNetConnection(ipAddress, slot, subslot); } public ICcLinkIeConnection CreateCcLinkIeConnection(string ipAddress, int stationNumber = 1) { return new CcLinkIeConnection(ipAddress, stationNumber); } public IOpcUaConnection CreateOpcUaConnection(string endpointUrl) { return new OpcUaConnection(endpointUrl); } }