Example #1
0
        public AmbiLightService(string configPath)
        {
            _configPath = configPath;
            _logger     = HostLogger.Get <AmbiLightService>();

            _screenCaptureServiceProvider  = new ScreenCaptureServiceProvider();
            _colorAveragingServiceProvider = new ColorAveragingServiceProvider();
            _colorTransformerService       = new ColorTransformerService();

            AmbiLightConfig config = ReadConfig(_configPath);

            ApplyConfig(config);

            var configFileInfo = new FileInfo(configPath);
            var watcher        = new FileSystemWatcher(configFileInfo.DirectoryName, configFileInfo.Name);

            watcher.Changed += (sender, eventArgs) =>
            {
                watcher.EnableRaisingEvents = false;
                while (FileIsLocked(eventArgs.FullPath))
                {
                    Thread.Sleep(100);
                }
                ReloadConfig();
                watcher.EnableRaisingEvents = true;
            };
            watcher.EnableRaisingEvents = true;
        }
Example #2
0
        private string SaveConfig()
        {
            var config = new AmbiLightConfig();

            config.RegionsToOutput = screenRegionsList.Items.Cast <ScreenRegionOutput>().ToList();

            return(JsonConvert.SerializeObject(config, Formatting.Indented));
        }
Example #3
0
 public void ReloadConfig()
 {
     lock (_configLock)
     {
         _logger.Info("Reloading config...");
         AmbiLightConfig config = ReadConfig(_configPath);
         ApplyConfig(config);
     }
 }
Example #4
0
        private void ApplyConfig(AmbiLightConfig config)
        {
            if (_screenCaptureService != null)
            {
                _screenCaptureService.Dispose();
            }

            _screenCaptureService = _screenCaptureServiceProvider.Provide(true);

            List <IOutputPlugin> plugins = OutputPlugins.GetAvailablePlugins();

            List <ScreenRegionOutput> regions = config.RegionsToOutput;

            if (_regionConfigurations != null)
            {
                foreach (RegionConfiguration regionConfiguration in _regionConfigurations)
                {
                    regionConfiguration.Dispose();
                }
            }

            _regionConfigurations = new List <RegionConfiguration>();

            var regionConfigId = 0;

            foreach (ScreenRegionOutput region in regions)
            {
                var regionConfig = new RegionConfiguration(regionConfigId++)
                {
                    ScreenRegion          = region.ScreenRegion,
                    ColorAveragingService = _colorAveragingServiceProvider.Provide(region.ColorAveragingConfig),
                    OutputConfigs         = new List <OutputConfiguration>()
                };

                var outputId = 0;

                foreach (Output output in region.Outputs)
                {
                    IOutputInfo   outputInfo = output.OutputInfo;
                    IOutputPlugin plugin     = plugins.FirstOrDefault(y => y.Name == outputInfo.PluginName);
                    if (plugin == null)
                    {
                        throw new InvalidOperationException(string.Format("Missing OutputPlugin {0}", outputInfo.PluginName));
                    }

                    OutputService outputService = _regionConfigurations.SelectMany(x => x.OutputConfigs).Select(x => x.OutputService).FirstOrDefault(x => x.IsReusableFor(outputInfo)) ?? plugin.GetNewOutputService();

                    outputService.Initialize(outputInfo);

                    IList <ColorTransformerConfig> colorTransformerConfigs = output.ColorTransformerConfigs;

                    if (colorTransformerConfigs == null)
                    {
                        colorTransformerConfigs = new List <ColorTransformerConfig>
                        {
                            new ColorTransformerConfig
                            {
                                Type   = typeof(HysteresisColorTransformer),
                                Config = new Dictionary <string, object>
                                {
                                    { "hysteresis", "0.01" }
                                }
                            },
                            new ColorTransformerConfig
                            {
                                Type   = typeof(BrightnessColorTransformer),
                                Config = new Dictionary <string, object>
                                {
                                    { "factorR", "1" },
                                    { "factorG", "1" },
                                    { "factorB", "0.9" }
                                }
                            },
                            //new ColorTransformerConfig
                            //{
                            //	Type = typeof (GammaColorTransformer),
                            //	Config = new Dictionary<string, object>
                            //	{
                            //		{"gammaR", "1"},
                            //		{"gammaG", "1.2"},
                            //		{"gammaB", "1.2"}
                            //	}
                            //},
                            new ColorTransformerConfig
                            {
                                Type   = typeof(ThresholdColorTransformer),
                                Config = new Dictionary <string, object>
                                {
                                    { "threshold", "0.01" }
                                }
                            }
                        };
                    }

                    var outputConfig = new OutputConfiguration(regionConfig.Id, outputId++)
                    {
                        ColorTransformerContexts = colorTransformerConfigs.Select(x => new ColorTransformerContext(x)).ToList(),
                        OutputService            = outputService,
                        ResendIntervalFunc       = outputService.GetResendInterval,
                        OutputInfo = outputInfo
                    };

                    regionConfig.OutputConfigs.Add(outputConfig);
                }

                _regionConfigurations.Add(regionConfig);
            }

            _screenRegions = _regionConfigurations.Select(x => x.ScreenRegion).ToList();
        }