示例#1
0
        private Profile CreateIntroProfile()
        {
            try
            {
                // Load the intro profile from JSON into a ProfileEntity
                string        json          = File.ReadAllText(Path.Combine(Constants.ApplicationFolder, "Resources", "intro-profile.json"));
                ProfileEntity profileEntity = CoreJson.DeserializeObject <ProfileEntity>(json) !;
                // Inject every LED on the surface into each layer
                foreach (LayerEntity profileEntityLayer in profileEntity.Layers)
                {
                    profileEntityLayer.Leds.AddRange(_devices.SelectMany(d => d.Leds).Select(l => new LedEntity
                    {
                        DeviceIdentifier = l.Device.Identifier,
                        LedName          = l.RgbLed.Id.ToString()
                    }));
                }

                Profile profile = new(new DummyModule(), profileEntity);
                profile.Activate(_devices);

                _profileService.InstantiateProfile(profile);
                return(profile);
            }
            catch (Exception e)
            {
                _logger.Warning(e, "Failed to load intro profile");
            }

            return(new Profile(new DummyModule(), "Intro"));
        }
示例#2
0
        /// <summary>
        ///     Gets the setting with the provided name. If the setting does not exist yet, it is created.
        /// </summary>
        /// <typeparam name="T">The type of the setting, can be any serializable type</typeparam>
        /// <param name="name">The name of the setting</param>
        /// <param name="defaultValue">The default value to use if the setting does not exist yet</param>
        /// <returns></returns>
        public PluginSetting <T> GetSetting <T>(string name, T defaultValue = default)
        {
            lock (_settingEntities)
            {
                // Return cached value if available
                if (_settingEntities.ContainsKey(name))
                {
                    return((PluginSetting <T>)_settingEntities[name]);
                }
                // Try to find in database
                PluginSettingEntity settingEntity = _pluginRepository.GetSettingByNameAndGuid(name, Plugin.Guid);
                // If not found, create a new one
                if (settingEntity == null)
                {
                    settingEntity = new PluginSettingEntity
                    {
                        Name       = name,
                        PluginGuid = Plugin.Guid,
                        Value      = CoreJson.SerializeObject(defaultValue)
                    };
                    _pluginRepository.AddSetting(settingEntity);
                }

                PluginSetting <T> pluginSetting = new(Plugin, _pluginRepository, settingEntity);

                // This overrides null with the default value, I'm not sure if that's desirable because you
                // might expect something to go null and you might not
                // if (pluginSetting.Value == null && defaultValue != null)
                //    pluginSetting.Value = defaultValue;

                _settingEntities.Add(name, pluginSetting);
                return(pluginSetting);
            }
        }
示例#3
0
        /// <inheritdoc />
        public void Save()
        {
            if (_disposed)
            {
                throw new ObjectDisposedException("DataBindingModifier");
            }

            // Don't save an invalid state
            if (ParameterPath != null && !ParameterPath.IsValid)
            {
                return;
            }

            if (!DirectDataBinding.Entity.Modifiers.Contains(Entity))
            {
                DirectDataBinding.Entity.Modifiers.Add(Entity);
            }

            // Modifier
            if (ModifierType?.Plugin != null)
            {
                Entity.ModifierType           = ModifierType.GetType().Name;
                Entity.ModifierTypePluginGuid = ModifierType.Plugin.Guid;
            }

            // General
            Entity.Order         = Order;
            Entity.ParameterType = (int)ParameterType;

            // Parameter
            ParameterPath?.Save();
            Entity.ParameterPath = ParameterPath?.Entity;

            Entity.ParameterStaticValue = CoreJson.SerializeObject(ParameterStaticValue);
        }
示例#4
0
        internal void Initialize()
        {
            ConditionOperatorStore.ConditionOperatorAdded   += ConditionOperatorStoreOnConditionOperatorAdded;
            ConditionOperatorStore.ConditionOperatorRemoved += ConditionOperatorStoreOnConditionOperatorRemoved;

            InitializeLeftPath();

            // Operator
            if (Entity.OperatorPluginGuid != null)
            {
                BaseConditionOperator?conditionOperator = ConditionOperatorStore.Get(Entity.OperatorPluginGuid.Value, Entity.OperatorType)?.ConditionOperator;
                if (conditionOperator != null)
                {
                    UpdateOperator(conditionOperator);
                }
            }

            // Right side dynamic
            if (PredicateType == ProfileRightSideType.Dynamic)
            {
                InitializeRightPath();
            }
            // Right side static
            else if (PredicateType == ProfileRightSideType.Static && Entity.RightStaticValue != null)
            {
                try
                {
                    if (LeftPath != null && LeftPath.IsValid)
                    {
                        // Use the left side type so JSON.NET has a better idea what to do
                        Type   leftSideType = LeftPath.GetPropertyType() !;
                        object?rightSideValue;

                        try
                        {
                            rightSideValue = CoreJson.DeserializeObject(Entity.RightStaticValue, leftSideType);
                        }
                        // If deserialization fails, use the type's default
                        catch (JsonSerializationException e)
                        {
                            DeserializationLogger.LogPredicateDeserializationFailure(this, e);
                            rightSideValue = Activator.CreateInstance(leftSideType);
                        }

                        UpdateRightSideStatic(rightSideValue);
                    }
                    else
                    {
                        // Hope for the best...
                        UpdateRightSideStatic(CoreJson.DeserializeObject(Entity.RightStaticValue));
                    }
                }
                catch (JsonReaderException e)
                {
                    DeserializationLogger.LogPredicateDeserializationFailure(this, e);
                }
            }
        }
示例#5
0
        internal PluginSetting(Plugin plugin, IPluginRepository pluginRepository, PluginSettingEntity pluginSettingEntity)
        {
            _plugin              = plugin;
            _pluginRepository    = pluginRepository;
            _pluginSettingEntity = pluginSettingEntity;

            Name = pluginSettingEntity.Name;
            try
            {
                _value = CoreJson.DeserializeObject <T>(pluginSettingEntity.Value) !;
            }
            catch (JsonReaderException)
            {
                _value = default !;
示例#6
0
        /// <summary>
        ///     Creates a deep copy of the folder
        /// </summary>
        /// <returns>The newly created copy</returns>
        public Folder CreateCopy()
        {
            if (Parent == null)
            {
                throw new ArtemisCoreException("Cannot create a copy of a folder without a parent");
            }

            FolderEntity entityCopy = CoreJson.DeserializeObject <FolderEntity>(CoreJson.SerializeObject(FolderEntity, true), true) !;

            entityCopy.Id    = Guid.NewGuid();
            entityCopy.Name += " - Copy";

            // TODO Children

            return(new Folder(Profile, Parent, entityCopy));
        }
示例#7
0
        private void Initialize()
        {
            DataBindingModifierTypeStore.DataBindingModifierAdded   += DataBindingModifierTypeStoreOnDataBindingModifierAdded;
            DataBindingModifierTypeStore.DataBindingModifierRemoved += DataBindingModifierTypeStoreOnDataBindingModifierRemoved;

            // Modifier type
            if (Entity.ModifierTypePluginGuid != null && ModifierType == null)
            {
                BaseDataBindingModifierType?modifierType = DataBindingModifierTypeStore.Get(Entity.ModifierTypePluginGuid.Value, Entity.ModifierType)?.DataBindingModifierType;
                if (modifierType != null)
                {
                    UpdateModifierType(modifierType);
                }
            }

            // Dynamic parameter
            if (ParameterType == ProfileRightSideType.Dynamic && Entity.ParameterPath != null)
            {
                ParameterPath = new DataModelPath(null, Entity.ParameterPath);
            }
            // Static parameter
            else if (ParameterType == ProfileRightSideType.Static && Entity.ParameterStaticValue != null)
            {
                // Use the target type so JSON.NET has a better idea what to do
                Type?  parameterType = ModifierType?.ParameterType ?? DirectDataBinding.DataBinding.GetTargetType();
                object?staticValue   = null;

                try
                {
                    staticValue = parameterType != null
                        ? CoreJson.DeserializeObject(Entity.ParameterStaticValue, parameterType)
                        : CoreJson.DeserializeObject(Entity.ParameterStaticValue);
                }
                // If deserialization fails, use the type's default
                catch (JsonSerializationException e)
                {
                    DeserializationLogger.LogModifierDeserializationFailure(GetType().Name, e);
                    if (parameterType != null)
                    {
                        staticValue = Activator.CreateInstance(parameterType);
                    }
                }

                UpdateParameterStatic(staticValue);
            }
        }
示例#8
0
        internal override void Save()
        {
            // Don't save an invalid state
            if (LeftPath != null && !LeftPath.IsValid || RightPath != null && !RightPath.IsValid)
            {
                return;
            }

            Entity.PredicateType = (int)PredicateType;

            LeftPath?.Save();
            Entity.LeftPath = LeftPath?.Entity;
            RightPath?.Save();
            Entity.RightPath = RightPath?.Entity;

            Entity.RightStaticValue = CoreJson.SerializeObject(RightStaticValue);

            if (Operator?.Plugin != null)
            {
                Entity.OperatorPluginGuid = Operator.Plugin.Guid;
                Entity.OperatorType       = Operator.GetType().Name;
            }
        }