Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
        public bool Initialize()
        {
            Logger.Info($"{new string('=', 64)}");
            Logger.Info("Initializing...");
            PluginLoader.LoadAll(Path.Combine(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);
            }

            _config      = _configManager.CurrentConfig;
            _cache       = new DataCache();
            _pluginStore = new PluginStore();

            _sensorManager = new SensorManager(_config);
            _deviceManager = new DeviceManager();

            _sensorManager.EnableSensors(_config.SensorConfigs.SelectMany(x => x.Sensors));
            foreach (var profile in _config.Profiles)
            {
                if (_pluginStore.Get(profile).Any())
                {
                    Logger.Fatal("Duplicate profile \"{0}\" found!", profile.Name);
                    return(false);
                }

                foreach (var effect in profile.Effects)
                {
                    _pluginStore.Add(profile, effect);
                    _sensorManager.EnableSensors(effect.UsedSensors);
                }

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

                profile.Ports.RemoveAll(p =>
                {
                    var portExists = _deviceManager.Controllers.SelectMany(c => c.Ports).Contains(p);
                    if (!portExists)
                    {
                        Logger.Warn("Removing invalid port: {0}", p);
                    }

                    return(!portExists);
                });
            }

            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);
                    _cache.StoreDeviceConfig(port, DeviceConfig.Default);
                }
            }

            _configManager.Accept(_cache.AsWriteOnly());

            ApplyComputerStateProfile(ComputerStateType.Boot);

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

            _timerManager.Start();

            Logger.Info("Initializing done!");
            Logger.Info($"{new string('=', 64)}");
            return(true);
        }
Ejemplo n.º 3
0
        public bool Initialize()
        {
            Logger.Info($"{new string('=', 64)}");
            Logger.Info("Initializing service, version \"{0}\"", FileVersionInfo.GetVersionInfo(Assembly.GetCallingAssembly().Location)?.ProductVersion);
            PluginLoader.LoadAll(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Plugins"));

            var serializationContext = new TrackingSerializationContext();

            serializationContext.Track(typeof(IPlugin));
            serializationContext.Track(typeof(Identifier));

            _configManager = new ConfigManager("config.json", serializationContext);
            if (!_configManager.LoadOrCreateConfig())
            {
                return(false);
            }

            _config      = _configManager.CurrentConfig;
            _cache       = new DataCache();
            _pluginStore = new PluginStore(serializationContext.Get <IPlugin>());

            _sensorManager = new SensorManager(_config);
            _deviceManager = new DeviceManager();

            _sensorManager.EnableSensors(_config.SensorConfigs.SelectMany(x => x.Sensors));
            foreach (var profile in _config.Profiles)
            {
                Logger.Info("Processing profile \"{0}\"", profile.Name);
                if (_pluginStore.Get(profile).Any())
                {
                    Logger.Fatal("Duplicate profile \"{0}\" found!", profile.Name);
                    return(false);
                }

                foreach (var effect in profile.Effects)
                {
                    _pluginStore.Assign(effect, profile);
                }

                foreach (var speedController in profile.SpeedControllers)
                {
                    _pluginStore.Assign(speedController, profile);
                }

                foreach (var port in profile.Ports)
                {
                    _cache.StorePortConfig(port, PortConfig.Default);
                    _cache.StoreDeviceConfig(port, DeviceConfig.Default);

                    if (!_deviceManager.Controllers.SelectMany(c => c.Ports).Contains(port))
                    {
                        Logger.Warn("Could not find matching controller for port {0}!", port);
                    }
                }
            }

            _sensorManager.EnableSensors(serializationContext.Get <Identifier>());
            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);
                    _cache.StoreDeviceConfig(port, DeviceConfig.Default);
                }
            }

            _configManager.Accept(_cache.AsWriteOnly());

            if (_config.IpcServerEnabled && _config.IpcServer != null)
            {
                _ipcClient = new ServiceIpcClient();
                _pluginStore.Add(_ipcClient);
                _config.IpcServer.RegisterClient(_ipcClient);

                foreach (var plugin in serializationContext.Get <IIpcClient>())
                {
                    _config.IpcServer.RegisterClient(plugin);
                }
                _config.IpcServer.Start();
            }

            ApplyComputerStateProfile(ComputerStateType.Boot);

            _timerManager = new TimerManager();
            _timerManager.RegisterTimer(_config.SensorTimerInterval, SensorTimerCallback);
            _timerManager.RegisterTimer(_config.DeviceSpeedTimerInterval, DeviceSpeedTimerCallback);
            _timerManager.RegisterTimer(_config.DeviceRgbTimerInterval, DeviceRgbTimerCallback);

            if (_config.IpcClientTimerInterval > 0)
            {
                _timerManager.RegisterTimer(_config.IpcClientTimerInterval, IpcClientTimerCallback);
            }
            if (LogManager.Configuration.LoggingRules.Any(r => r.IsLoggingEnabledForLevel(LogLevel.Debug)))
            {
                _timerManager.RegisterTimer(_config.DebugTimerInterval, DebugTimerCallback);
            }

            _timerManager.Start();

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