コード例 #1
0
    public async Task <object> ExecuteScript(string function_name = "main")
    {
        try
        {
            string script;
            using (var streamReader = new StreamReader(HttpContext.Request.Body))
            {
                script = await streamReader.ReadToEndAsync().ConfigureAwait(false);
            }

            var scriptHost = _pythonScriptHostFactoryService.CreateScriptHost();
            scriptHost.Compile(script);

            if (string.IsNullOrEmpty(function_name))
            {
                return(null);
            }

            return(scriptHost.InvokeFunction(function_name));
        }
        catch (Exception exception)
        {
            return(new ExceptionPythonModel(exception).ToDictionary());
        }
    }
コード例 #2
0
        public object ExecuteTool(string uid, [FromBody] IDictionary parameters = null)
        {
            if (uid == null)
            {
                throw new ArgumentNullException(nameof(uid));
            }

            if (parameters == null)
            {
                parameters = new PythonDictionary();
            }

            Package package;

            try
            {
                package = _packageManagerService.LoadPackage(PackageUid.Parse(uid));
            }
            catch (WirehomePackageNotFoundException)
            {
                HttpContext.Response.StatusCode = (int)HttpStatusCode.NotFound;
                return(null);
            }

            try
            {
                var scriptHost = _pythonScriptHostFactoryService.CreateScriptHost(null);
                scriptHost.Compile(package.Script);
                return(scriptHost.InvokeFunction("main", parameters));
            }
            catch (Exception exception)
            {
                return(new ExceptionPythonModel(exception).ConvertToPythonDictionary());
            }
        }
コード例 #3
0
        ServiceInstance CreateServiceInstance(string id, ServiceConfiguration configuration)
        {
            var packageUid = new PackageUid(id, configuration.Version);
            var package    = _repositoryService.LoadPackage(packageUid);

            var scriptHost = _pythonScriptHostFactoryService.CreateScriptHost();

            scriptHost.Compile(package.Script);

            var context = new PythonDictionary
            {
                ["service_id"]      = id,
                ["service_version"] = configuration.Version,
                ["service_uid"]     = new PackageUid(id, configuration.Version).ToString()
            };

            scriptHost.AddToWirehomeWrapper("context", context);

            var serviceInstance = new ServiceInstance(id, configuration, scriptHost);

            if (configuration.Variables != null)
            {
                foreach (var variable in configuration.Variables)
                {
                    serviceInstance.SetVariable(variable.Key, variable.Value);
                }
            }

            return(serviceInstance);
        }
コード例 #4
0
        public object ExecuteScript(string function_name = "main")
        {
            try
            {
                string script;
                using (var streamReader = new StreamReader(HttpContext.Request.Body))
                {
                    script = streamReader.ReadToEnd();
                }

                var scriptHost = _pythonScriptHostFactoryService.CreateScriptHost(null);
                scriptHost.Compile(script);

                if (string.IsNullOrEmpty(function_name))
                {
                    return(null);
                }

                return(scriptHost.InvokeFunction(function_name));
            }
            catch (Exception exception)
            {
                return(new ExceptionPythonModel(exception).ConvertToPythonDictionary());
            }
        }
コード例 #5
0
        private AutomationInstance CreateAutomation(string uid, AutomationConfiguration configuration, WirehomeDictionary settings)
        {
            var repositoryEntitySource = _repositoryService.LoadEntity(configuration.Logic.Uid);
            var scriptHost             = _pythonScriptHostFactoryService.CreateScriptHost(_logger, new AutomationPythonProxy(uid, this));

            scriptHost.Initialize(repositoryEntitySource.Script);

            var context = new WirehomeDictionary
            {
                ["automation_uid"] = uid,
                ["logic_id"]       = configuration.Logic.Uid.Id,
                ["logic_version"]  = configuration.Logic.Uid.Version
            };

            // TODO: Remove scope as soon as all automations are migrated.
            scriptHost.SetVariable("scope", context);
            scriptHost.SetVariable("context", context);

            foreach (var variable in configuration.Logic.Variables)
            {
                scriptHost.SetVariable(variable.Key, variable.Value);
            }

            var automation = new AutomationInstance(uid, scriptHost);

            foreach (var setting in settings)
            {
                automation.Settings[setting.Key] = setting.Value;
            }

            return(automation);
        }
コード例 #6
0
        public object ExecuteTool(string uid, [FromBody] IDictionary parameters = null)
        {
            if (uid == null)
            {
                throw new ArgumentNullException(nameof(uid));
            }

            if (parameters == null)
            {
                parameters = new PythonDictionary();
            }

            RepositoryEntity repositoryEntity;

            try
            {
                repositoryEntity = _repositoryService.LoadEntity(RepositoryEntityUid.Parse(uid));
            }
            catch (WirehomeRepositoryEntityNotFoundException)
            {
                HttpContext.Response.StatusCode = (int)HttpStatusCode.NotFound;
                return(null);
            }

            try
            {
                var scriptHost = _pythonScriptHostFactoryService.CreateScriptHost(null);
                scriptHost.Initialize(repositoryEntity.Script);
                return(scriptHost.InvokeFunction("main", parameters));
            }
            catch (Exception exception)
            {
                return(new ExceptionPythonModel(exception).ConvertToPythonDictionary());
            }
        }
コード例 #7
0
        StartupScriptInstance CreateStartupScriptInstance(string uid, StartupScriptConfiguration configuration)
        {
            if (!_storageService.TryReadRawText(out var scriptCode, StartupScriptsDirectory, uid, DefaultFileNames.Script))
            {
                throw new InvalidOperationException("Script file not found.");
            }

            var scriptHost = _pythonScriptHostFactoryService.CreateScriptHost();

            scriptHost.Compile(scriptCode);

            return(new StartupScriptInstance(uid, configuration, scriptHost));
        }
コード例 #8
0
        public void Compile(string componentUid, string script)
        {
            if (componentUid == null)
            {
                throw new ArgumentNullException(nameof(componentUid));
            }
            if (script == null)
            {
                throw new ArgumentNullException(nameof(script));
            }

            _scriptHost = _pythonScriptHostFactoryService.CreateScriptHost(_logger, new ComponentPythonProxy(componentUid, _componentRegistryService));
            _scriptHost.SetVariable("publish_adapter_message", (PythonDelegates.CallbackWithResultDelegate)OnMessageReceived);
            _scriptHost.AddToWirehomeWrapper("publish_adapter_message", (PythonDelegates.CallbackWithResultDelegate)OnMessageReceived);

            _scriptHost.Compile(script);
        }
コード例 #9
0
        public void Compile(string componentUid, string script)
        {
            if (componentUid == null)
            {
                throw new ArgumentNullException(nameof(componentUid));
            }
            if (script == null)
            {
                throw new ArgumentNullException(nameof(script));
            }

            _scriptHost = _pythonScriptHostFactoryService.CreateScriptHost(_logger, new ComponentPythonProxy(componentUid, _componentRegistryService));
            _scriptHost.SetVariable("publish_adapter_message", (PythonDelegates.CallbackWithResultDelegate)OnAdapterMessagePublished);
            _scriptHost.AddToWirehomeWrapper("publish_adapter_message", (PythonDelegates.CallbackWithResultDelegate)OnAdapterMessagePublished);

            // TODO: Consider adding debugger here and move enable/disable/getTrace to IComponentLogic and enable via HTTP API.

            _scriptHost.Compile(script);
        }
コード例 #10
0
        private ServiceInstance CreateServiceInstance(string id, ServiceConfiguration configuration)
        {
            var repositoryEntityUid    = new RepositoryEntityUid(id, configuration.Version);
            var repositoryEntitySource = _repositoryService.LoadEntity(repositoryEntityUid);

            var scriptHost = _pythonScriptHostFactoryService.CreateScriptHost(_logger);

            scriptHost.Initialize(repositoryEntitySource.Script);

            var serviceInstance = new ServiceInstance(id, configuration, scriptHost);

            if (configuration.Variables != null)
            {
                foreach (var variable in configuration.Variables)
                {
                    serviceInstance.SetVariable(variable.Key, variable.Value);
                }
            }

            return(serviceInstance);
        }
コード例 #11
0
        private ServiceInstance CreateServiceInstance(string id, ServiceConfiguration configuration)
        {
            var packageUid = new PackageUid(id, configuration.Version);
            var package    = _repositoryService.LoadPackage(packageUid);

            var scriptHost = _pythonScriptHostFactoryService.CreateScriptHost(_logger);

            scriptHost.Compile(package.Script);

            var serviceInstance = new ServiceInstance(id, configuration, scriptHost);

            if (configuration.Variables != null)
            {
                foreach (var variable in configuration.Variables)
                {
                    serviceInstance.SetVariable(variable.Key, variable.Value);
                }
            }

            return(serviceInstance);
        }