예제 #1
0
        private void InitializeLogic(Component component, ComponentLogicConfiguration configuration)
        {
            var scope = new WirehomeDictionary
            {
                ["component_uid"] = component.Uid
            };

            var adapterEntity = _repositoryService.LoadEntity(configuration.Adapter.Uid);

            var adapter = new ScriptComponentAdapter(_pythonEngineService, _componentRegistryService, _loggerFactory);

            adapter.Initialize(component.Uid, adapterEntity.Script);

            if (configuration.Adapter.Variables != null)
            {
                foreach (var parameter in configuration.Adapter.Variables)
                {
                    adapter.SetVariable(parameter.Key, parameter.Value);
                }
            }

            scope["adapter_uid"]     = adapterEntity.Uid.ToString();
            scope["adapter_id"]      = adapterEntity.Uid.Id;
            scope["adapter_version"] = adapterEntity.Uid.Version;

            if (string.IsNullOrEmpty(configuration.Uid?.Id))
            {
                component.SetLogic(new EmptyLogic(adapter));
            }
            else
            {
                var logicEntity = _repositoryService.LoadEntity(configuration.Uid);

                var logic = new ScriptComponentLogic(_pythonEngineService, _componentRegistryService, _loggerFactory);
                adapter.MessagePublishedCallback      = message => logic.ProcessAdapterMessage(message);
                logic.AdapterMessagePublishedCallback = message => adapter.ProcessMessage(message);

                logic.Initialize(component.Uid, logicEntity.Script);

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

                scope["logic_uid"]     = logicEntity.Uid.ToString();
                scope["logic_id"]      = logicEntity.Uid.Id;
                scope["logic_version"] = logicEntity.Uid.Version;

                logic.SetVariable("scope", scope);
                component.SetLogic(logic);
            }

            adapter.SetVariable("scope", scope);
        }
        public void InitializeComponent(Component component, ComponentConfiguration configuration)
        {
            if (component == null)
            {
                throw new ArgumentNullException(nameof(component));
            }
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            var context = new WirehomeDictionary
            {
                ["component_uid"] = component.Uid
            };

            var adapterPackage = _packageManagerService.LoadPackage(configuration.Logic.Adapter.Uid);

            var adapter = new ScriptComponentAdapter(_pythonScriptHostFactoryService, _componentRegistryService, _scriptComponentAdapterLogger);

            adapter.Compile(component.Uid, adapterPackage.Script);

            if (configuration.Logic.Adapter.Variables != null)
            {
                foreach (var parameter in configuration.Logic.Adapter.Variables)
                {
                    adapter.SetVariable(parameter.Key, parameter.Value);
                }
            }

            context["adapter_uid"]     = adapterPackage.Uid.ToString();
            context["adapter_id"]      = adapterPackage.Uid.Id;
            context["adapter_version"] = adapterPackage.Uid.Version;

            if (string.IsNullOrEmpty(configuration.Logic.Uid?.Id))
            {
                component.SetLogic(new EmptyLogic(adapter));
            }
            else
            {
                var logicPackage = _packageManagerService.LoadPackage(configuration.Logic.Uid);

                var logic = new ScriptComponentLogic(_pythonScriptHostFactoryService, _componentRegistryService, _scriptComponentLogicLogger);
                adapter.MessagePublishedCallback      = message => logic.ProcessAdapterMessage(message);
                logic.AdapterMessagePublishedCallback = message => adapter.ProcessMessage(message);

                logic.Compile(component.Uid, logicPackage.Script);

                if (configuration.Logic.Variables != null)
                {
                    foreach (var parameter in configuration.Logic.Variables)
                    {
                        logic.SetVariable(parameter.Key, parameter.Value);
                    }
                }

                context["logic_uid"]     = logicPackage.Uid.ToString();
                context["logic_id"]      = logicPackage.Uid.Id;
                context["logic_version"] = logicPackage.Uid.Version;

                // TODO: Remove "scope" as soon as it is migrated.
                logic.SetVariable("scope", context);
                logic.SetVariable("context", context);
                logic.AddToWirehomeWrapper("context", context);

                component.SetLogic(logic);
            }

            // TODO: Remove "scope" as soon as it is migrated.
            adapter.SetVariable("scope", context);
            adapter.SetVariable("context", context);
            adapter.AddToWirehomeWrapper("context", context);
        }
예제 #3
0
        private void SetupComponentBasedOnPackages(Component component, ComponentConfiguration configuration, IDictionary <object, object> context)
        {
            Package                adapterPackage = null;
            IComponentAdapter      adapter;
            ScriptComponentAdapter scriptAdapter = null;

            if (configuration.Logic?.Adapter?.Uid != null)
            {
                adapterPackage = _packageManagerService.LoadPackage(configuration.Logic.Adapter.Uid);

                scriptAdapter = new ScriptComponentAdapter(_pythonScriptHostFactoryService, _componentRegistryService, _scriptComponentAdapterLogger);
                scriptAdapter.Compile(component.Uid, adapterPackage.Script);

                if (configuration.Logic.Adapter.Variables != null)
                {
                    foreach (var parameter in configuration.Logic.Adapter.Variables)
                    {
                        scriptAdapter.SetVariable(parameter.Key, parameter.Value);
                    }
                }

                adapter = scriptAdapter;
            }
            else
            {
                adapter = new EmptyComponentAdapter();
            }

            context["adapter_uid"]     = adapterPackage?.Uid?.ToString();
            context["adapter_id"]      = adapterPackage?.Uid?.Id;
            context["adapter_version"] = adapterPackage?.Uid?.Version;

            if (string.IsNullOrEmpty(configuration.Logic?.Uid?.Id))
            {
                component.SetLogic(new EmptyComponentLogic(adapter));
            }
            else
            {
                var logicPackage = _packageManagerService.LoadPackage(configuration.Logic.Uid);

                var logic = new ScriptComponentLogic(_pythonScriptHostFactoryService, _componentRegistryService, _scriptComponentLogicLogger);
                adapter.MessagePublishedCallback      = message => logic.ProcessAdapterMessage(message);
                logic.AdapterMessagePublishedCallback = message => adapter.ProcessMessage(message);

                logic.Compile(component.Uid, logicPackage.Script);

                if (configuration.Logic.Variables != null)
                {
                    foreach (var parameter in configuration.Logic.Variables)
                    {
                        logic.SetVariable(parameter.Key, parameter.Value);
                    }
                }

                context["logic_uid"]     = logicPackage.Uid.ToString();
                context["logic_id"]      = logicPackage.Uid.Id;
                context["logic_version"] = logicPackage.Uid.Version;

                logic.AddToWirehomeWrapper("context", context);

                component.SetLogic(logic);
            }

            scriptAdapter?.AddToWirehomeWrapper("context", context);
        }