コード例 #1
0
    public ServiceInstance(string id, ServiceConfiguration configuration, PythonScriptHost scriptHost)
    {
        Id            = id ?? throw new ArgumentNullException(nameof(id));
        Configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));

        _scriptHost = scriptHost ?? throw new ArgumentNullException(nameof(scriptHost));
    }
コード例 #2
0
 private void Intialize()
 {
     try
     {
         LibPath              = "";
         PythonRootPath       = ConfigUtils.GetSetting("PythonRootPath", "PyScript");
         _runtimePath         = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
         _runtimePath         = Path.Combine(_runtimePath, PythonRootPath);
         _routeConfigFileName = Path.Combine(_runtimePath, "Route.config.xml");
         CacheListener routeListener = new CacheListener("__ACTION_ROUTES", 0, (key, value, reason) =>
         {
             if (reason == CacheItemRemovedReason.DependencyChanged)
             {
                 LoadRouteConfig(_routeConfigFileName);
                 //重新设置查找路径
                 PythonScriptHost.LibPath = LibPath;
                 PythonScriptHost.SetPythonSearchPath(_runtimePath);
             }
         }, _routeConfigFileName);
         routeListener.Start();
         LoadRouteConfig(_routeConfigFileName);
         bool isDebug = ConfigUtils.GetSetting("Python_IsDebug", "false").ToBool();
         PythonScriptHost.Initialize(_runtimePath, LibPath, isDebug);
     }
     catch (Exception ex)
     {
         TraceLog.WriteError("ActionFactory load route config:{0} error{1}", _routeConfigFileName, ex);
     }
 }
コード例 #3
0
        public PythonSingleLayer(PythonScriptHost host, string code, string name)
        {
            Name = name;
            Code = code;
            Host = host;

            string[] codeWithoutWhitespace = code.Split(new char[] { ' ', '\n' }, StringSplitOptions.RemoveEmptyEntries);
            string   alltokens             = "";

            foreach (string token in codeWithoutWhitespace)
            {
                alltokens += token;
            }

            _id = alltokens.GetHashCode().ToString();

            _scope = host.CreateScriptSource(code, name);

            if (_scope.ContainsVariable(_processannotations))
            {
                _processAnnotationsFunc = _scope.GetVariable(_processannotations);
            }

            if (_scope.ContainsVariable(_interpret))
            {
                _interpretFunc = _scope.GetVariable(_interpret);
            }
        }
コード例 #4
0
        public MacroInstance(string uid, List <MacroActionConfiguration> actions, PythonScriptHost scriptHost, ComponentRegistryService componentRegistryService, ILogger logger)
        {
            Uid         = uid ?? throw new ArgumentNullException(nameof(uid));
            _actions    = actions;
            _scriptHost = scriptHost;

            _componentRegistryService = componentRegistryService ?? throw new ArgumentNullException(nameof(componentRegistryService));
            _logger = logger ?? throw new ArgumentNullException(nameof(logger));
        }
コード例 #5
0
        public void Initialize(RepositoryEntityUid repositoryEntityUid)
        {
            RepositoryEntityUid = repositoryEntityUid ?? throw new ArgumentNullException(nameof(repositoryEntityUid));

            var repositoryEntitySource = _repositoryService.LoadEntity(RepositoryEntityUid);

            lock (_syncRoot)
            {
                _pythonScriptHost = _pythonEngineService.CreateScriptHost(_logger);
                _pythonScriptHost.Initialize(repositoryEntitySource.Script);
            }
        }
コード例 #6
0
        public void Initialize(string componentUid, string script)
        {
            if (componentUid == null)
            {
                throw new ArgumentNullException(nameof(componentUid));
            }
            if (script == null)
            {
                throw new ArgumentNullException(nameof(script));
            }

            _scriptHost = _pythonEngineService.CreateScriptHost(_logger, new ComponentPythonProxy(componentUid, _componentRegistryService));
            _scriptHost.SetVariable("publish_adapter_message", (Func <PythonDictionary, PythonDictionary>)OnMessageReceived);

            _scriptHost.Initialize(script);
        }
コード例 #7
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);
        }
コード例 #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)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);
        }
コード例 #9
0
        private bool TryInitializeScript(string uid, string scriptCode, out PythonScriptHost scriptHost)
        {
            try
            {
                scriptHost = _pythonEngineService.CreateScriptHost(_logger);
                scriptHost.Initialize(scriptCode);

                if (scriptHost.FunctionExists("initialize"))
                {
                    scriptHost.InvokeFunction("initialize");
                }

                return(true);
            }
            catch (Exception exception)
            {
                _logger.LogError(exception, $"Error while initializing startup script '{uid}'.");

                scriptHost = null;
                return(false);
            }
        }
コード例 #10
0
 public StartupScriptInstance(string uid, PythonScriptHost scriptHost)
 {
     Uid        = uid ?? throw new ArgumentNullException(nameof(uid));
     ScriptHost = scriptHost ?? throw new ArgumentNullException(nameof(scriptHost));
 }
コード例 #11
0
 public AutomationInstance(string uid, PythonScriptHost scriptHost)
 {
     Uid         = uid ?? throw new ArgumentNullException(nameof(uid));
     _scriptHost = scriptHost ?? throw new ArgumentNullException(nameof(scriptHost));
 }
コード例 #12
0
 public StartupScriptInstance(string uid, StartupScriptConfiguration configuration, PythonScriptHost scriptHost)
 {
     Uid           = uid ?? throw new ArgumentNullException(nameof(uid));
     Configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
     _scriptHost   = scriptHost ?? throw new ArgumentNullException(nameof(scriptHost));
 }
コード例 #13
0
 public Automation(PythonScriptHost scriptHost)
 {
     _scriptHost = scriptHost ?? throw new ArgumentNullException(nameof(scriptHost));
 }