Пример #1
0
        public bool Initialize()
        {
            var logConfig = new LoggingConfiguration();

            logConfig.AddTarget(new ConsoleTarget("console")
            {
                DetectConsoleAvailable = true,
                Layout = Layout.FromString("${time}: ${message}")
            });
            logConfig.AddRuleForAllLevels("console");
            LogManager.Configuration = logConfig;

            Logger.Info($"{new string('=', 64)}");
            Logger.Info("Initializing...");
            var pluginAssemblies = Directory.GetFiles($@"{AppDomain.CurrentDomain.BaseDirectory}\Plugins", "*.dll", SearchOption.AllDirectories)
                                   .Where(f => AppDomain.CurrentDomain.GetAssemblies().All(a => a.Location != f))
                                   .TrySelect(Assembly.LoadFile, ex => { })
                                   .ToList();

            AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
                                                       pluginAssemblies.FirstOrDefault(a => string.CompareOrdinal(a.FullName, args.Name) == 0);

            Logger.Info("Loading plugins...");
            foreach (var assembly in pluginAssemblies)
            {
                Logger.Info("Loading assembly: {0} [{1}]", assembly.GetName().Name, assembly.GetName().Version);
            }

            _cache         = new DataCache();
            _configManager = new ConfigManager("config.json");
            _configManager.LoadOrCreateConfig();

            _sensorManager = new SensorManager();

            var alpha           = Math.Exp(-_configManager.CurrentConfig.TemperatureTimerInterval / (double)_configManager.CurrentConfig.DeviceSpeedTimerInterval);
            var providerFactory = new MovingAverageTemperatureProviderFactory(alpha);

            _temperatureManager = new TemperatureManager(_sensorManager.TemperatureSensors.ToList(), providerFactory);

            _effectManager          = new EffectManager();
            _speedControllerManager = new SpeedControllerManager();
            _deviceManager          = new DeviceManager();
            _deviceManager.Accept(_cache.AsWriteOnly());

            Logger.Info("Applying config...");
            _configManager.Accept(_cache.AsWriteOnly());
            foreach (var profile in _configManager.CurrentConfig.Profiles)
            {
                foreach (var effect in profile.Effects)
                {
                    _effectManager.Add(profile.Guid, effect);
                }

                foreach (var speedController in profile.SpeedControllers)
                {
                    _speedControllerManager.Add(profile.Guid, speedController);
                }

                _temperatureManager.EnableSensors(_speedControllerManager.GetSpeedControllers(profile.Guid)?.SelectMany(c => c.UsedSensors));
                _temperatureManager.EnableSensors(_effectManager.GetEffects(profile.Guid)?.SelectMany(e => e.UsedSensors));
            }

            ApplyComputerStateProfile(ComputerStateType.Boot);

            _timerManager = new TimerManager();
            _timerManager.RegisterTimer(_configManager.CurrentConfig.TemperatureTimerInterval, TemperatureTimerCallback);
            _timerManager.RegisterTimer(_configManager.CurrentConfig.DeviceSpeedTimerInterval, DeviceSpeedTimerCallback);
            _timerManager.RegisterTimer(_configManager.CurrentConfig.DeviceRgbTimerInterval, DeviceRgbTimerCallback);
            if (Environment.UserInteractive)
            {
                _timerManager.RegisterTimer(_configManager.CurrentConfig.LoggingTimerInterval, LoggingTimerCallback);
            }

            _timerManager.Start();

            Logger.Info("Initializing done!");
            Logger.Info($"{new string('=', 64)}");
            return(true);
        }
Пример #2
0
        public bool Initialize()
        {
            Logger.Info($"{new string('=', 64)}");
            Logger.Info("Initializing...");
            PluginLoader.LoadAll($@"{AppDomain.CurrentDomain.BaseDirectory}\Plugins");

            const string key = "config-file";

            if (string.IsNullOrEmpty(AppSettingsHelper.ReadValue(key)))
            {
                AppSettingsHelper.WriteValue(key, "config.json");
            }

            _configManager = new ConfigManager(AppSettingsHelper.ReadValue(key));
            if (!_configManager.LoadOrCreateConfig())
            {
                return(false);
            }

            _cache = new DataCache();

            var alpha           = Math.Exp(-_configManager.CurrentConfig.SensorTimerInterval / (double)_configManager.CurrentConfig.DeviceSpeedTimerInterval);
            var providerFactory = new MovingAverageSensorValueProviderFactory(alpha);
            var sensorConfigs   = _configManager.CurrentConfig.SensorConfigs
                                  .SelectMany(x => x.Sensors.Select(s => (Sensor: s, Config: x.Config)))
                                  .ToDictionary(x => x.Sensor, x => x.Config);

            _sensorManager          = new SensorManager(providerFactory, sensorConfigs);
            _effectManager          = new EffectManager();
            _speedControllerManager = new SpeedControllerManager();
            _deviceManager          = new DeviceManager();

            _sensorManager.EnableSensors(sensorConfigs.Keys);
            foreach (var profile in _configManager.CurrentConfig.Profiles)
            {
                foreach (var effect in profile.Effects)
                {
                    _effectManager.Add(profile.Guid, effect);
                    _sensorManager.EnableSensors(effect.UsedSensors);
                }

                foreach (var speedController in profile.SpeedControllers)
                {
                    _speedControllerManager.Add(profile.Guid, speedController);
                    _sensorManager.EnableSensors(speedController.UsedSensors);
                }
            }

            foreach (var sensor in _sensorManager.EnabledSensors)
            {
                _cache.StoreSensorConfig(sensor, SensorConfig.Default);
            }

            foreach (var controller in _deviceManager.Controllers)
            {
                foreach (var port in controller.Ports)
                {
                    _cache.StorePortConfig(port, PortConfig.Default);
                }
            }

            foreach (var(ports, config) in _configManager.CurrentConfig.PortConfigs)
            {
                foreach (var port in ports)
                {
                    _cache.StorePortConfig(port, config);
                }
            }

            foreach (var(sensors, config) in _configManager.CurrentConfig.SensorConfigs)
            {
                foreach (var sensor in sensors)
                {
                    _cache.StoreSensorConfig(sensor, config);
                }
            }

            ApplyComputerStateProfile(ComputerStateType.Boot);

            _timerManager = new TimerManager();
            _timerManager.RegisterTimer(_configManager.CurrentConfig.SensorTimerInterval, SensorTimerCallback);
            _timerManager.RegisterTimer(_configManager.CurrentConfig.DeviceSpeedTimerInterval, DeviceSpeedTimerCallback);
            _timerManager.RegisterTimer(_configManager.CurrentConfig.DeviceRgbTimerInterval, DeviceRgbTimerCallback);
            if (LogManager.Configuration.LoggingRules.Any(r => r.IsLoggingEnabledForLevel(LogLevel.Debug)))
            {
                _timerManager.RegisterTimer(_configManager.CurrentConfig.LoggingTimerInterval, LoggingTimerCallback);
            }

            _timerManager.Start();

            Logger.Info("Initializing done!");
            Logger.Info($"{new string('=', 64)}");
            return(true);
        }