public void ThrowsWhenIndexIsInvalid() { var fanCfg = new FanConfiguration(); var cfg = new FanControlConfigV2() { EcPollInterval = 100, FanConfigurations = { fanCfg } }; var filter = A.Fake <ITemperatureFilter>(); A.CallTo(() => filter.FilterTemperature(A <double> .Ignored)).ReturnsLazily((double x) => x); var ec = A.Fake <IEmbeddedController>(); A.CallTo(() => ec.IsInitialized).Returns(true); A.CallTo(() => ec.AcquireLock(A <int> .Ignored)).Returns(true); var tempMon = A.Fake <ITemperatureMonitor>(); A.CallTo(() => tempMon.IsInitialized).Returns(true); using (var fanControl = new FanControl(cfg, filter, ec, tempMon)) { var exception = Record.Exception(() => fanControl.SetTargetFanSpeed(123, 12)); Assert.NotNull(exception); Assert.IsType <IndexOutOfRangeException>(exception); } }
private void AddNewConfig(FanControlConfigV2 config, string configName) { if (config == null) { throw new ArgumentNullException("config"); } if (!IsConfigNameValid(configName)) { throw new ArgumentException("Invalid config name", "configName"); } if (string.IsNullOrWhiteSpace(config.NotebookModel)) { config.NotebookModel = this.ActualNotebookModel; } if (this.configManager.Contains(configName)) { this.configManager.UpdateConfig(configName, config); } else { this.configManager.AddConfig(config, configName); } }
public static void ReturnsOnlyValidSuggestions() { string[] notebooks = new[] { "HP ProBook 1234", "HP EliteBook 1234", "Acer Foo 7683" }; var cfgMan = new FanControlConfigManager( Environment.CurrentDirectory, ".xml", new MockFileSystem()); int i = 0; foreach (string s in notebooks) { var cfg = new FanControlConfigV2() { FanConfigurations = new List <FanConfiguration>() { new FanConfiguration() { WriteRegister = i, ReadRegister = i + 1 } } }; cfgMan.AddConfig(cfg, s); i++; } List <string> recommendations = cfgMan.RecommendConfigs("HP ProBook 3334"); Assert.Contains(notebooks[0], recommendations); Assert.Contains(notebooks[1], recommendations); Assert.DoesNotContain(notebooks[2], recommendations); }
public FanControl(FanControlConfigV2 config, ITemperatureFilter filter, string pluginsDirectory) : this( config, filter, LoadPlugin<IEmbeddedController>(pluginsDirectory), LoadPlugin<ITemperatureMonitor>(pluginsDirectory)) { }
public FanControl(FanControlConfigV2 config, string pluginsPath) : this( config, new ArithmeticMeanTemperatureFilter(Math.Max(config.EcPollInterval, MinPollInterval)), pluginsPath) { }
public async Task CallsSetTargetSpeed(float speed) { var fanCfg1 = new FanConfiguration(); var fanCfg2 = new FanConfiguration(); var cfg = new FanControlConfigV2() { EcPollInterval = 100, FanConfigurations = { fanCfg1, fanCfg2 } }; var filter = A.Fake <ITemperatureFilter>(); A.CallTo(() => filter.FilterTemperature(A <double> .Ignored)).ReturnsLazily((double x) => x); var ec = A.Fake <IEmbeddedController>(); A.CallTo(() => ec.IsInitialized).Returns(true); A.CallTo(() => ec.AcquireLock(A <int> .Ignored)).Returns(true); var tempMon = A.Fake <ITemperatureMonitor>(); A.CallTo(() => tempMon.IsInitialized).Returns(true); var fan1 = A.Fake <Fan>(opt => opt.WithArgumentsForConstructor( new object[] { ec, fanCfg1, 70, false })); A.CallTo(() => fan1.GetCurrentSpeed()).Returns(0); var fan2 = A.Fake <Fan>(opt => opt.WithArgumentsForConstructor( new object[] { ec, fanCfg2, 70, false })); A.CallTo(() => fan2.GetCurrentSpeed()).Returns(0); var tsc = new TaskCompletionSource <bool>(); Task <bool> task = tsc.Task; var fanControl = new FanControl(cfg, filter, ec, tempMon, new[] { fan1, fan2 }); fanControl.EcUpdated += (s, e) => { tsc.TrySetResult(true); }; fanControl.SetTargetFanSpeed(speed, 0); fanControl.SetTargetFanSpeed(speed, 1); fanControl.Start(false); Assert.True(fanControl.Enabled, nameof(fanControl.Enabled)); await Task.WhenAny(task, Task.Delay(cfg.EcPollInterval * 3)); Assert.True(task.IsCompleted, nameof(task.IsCompleted)); A.CallTo(() => fan1.SetTargetSpeed(speed, A <float> .Ignored, false)) .MustHaveHappened(); A.CallTo(() => fan2.SetTargetSpeed(speed, A <float> .Ignored, false)) .MustHaveHappened(); }
private void InitializeFanSpeedSteps(FanControlConfigV2 cfg) { this.fanSpeedSteps = new int[cfg.FanConfigurations.Count]; for (int i = 0; i < this.fanSpeedSteps.Length; i++) { var fanConfig = cfg.FanConfigurations[i]; this.fanSpeedSteps[i] = Math.Max(fanConfig.MinSpeedValue, fanConfig.MaxSpeedValue) - Math.Min(fanConfig.MinSpeedValue, fanConfig.MaxSpeedValue); } }
public FanControl(FanControlConfigV2 config, ITemperatureFilter tempFilter, string pluginsPath) { if (config == null) { throw new ArgumentNullException("config"); } if (tempFilter == null) { throw new ArgumentNullException("filter"); } if (pluginsPath == null) { throw new ArgumentNullException("pluginsPath"); } if (!Directory.Exists(pluginsPath)) { throw new DirectoryNotFoundException(pluginsPath + " could not be found."); } var ecLoader = new FanControlPluginLoader <IEmbeddedController>(pluginsPath); this.ec = ecLoader.FanControlPlugin; this.EmbeddedControllerPluginId = ecLoader.FanControlPluginId; var tempMonloader = new FanControlPluginLoader <ITemperatureMonitor>(pluginsPath); this.tempMon = tempMonloader.FanControlPlugin; this.TemperatureMonitorPluginId = tempMonloader.FanControlPluginId; this.autoEvent = new AutoResetEvent(false); this.tempFilter = tempFilter; this.config = (FanControlConfigV2)config.Clone(); this.pollInterval = config.EcPollInterval; this.waitHandleTimeout = Math.Min(MaxWaitHandleTimeout, config.EcPollInterval); this.requestedSpeeds = new float[config.FanConfigurations.Count]; this.fanInfo = new FanInformation[config.FanConfigurations.Count]; this.fanInfoInternal = new FanInformation[config.FanConfigurations.Count]; this.fanSpeedManagers = new FanSpeedManager[config.FanConfigurations.Count]; this.asyncOp = AsyncOperationManager.CreateOperation(null); for (int i = 0; i < config.FanConfigurations.Count; i++) { var cfg = config.FanConfigurations[i]; this.fanSpeedManagers[i] = new FanSpeedManager(cfg, config.CriticalTemperature); this.requestedSpeeds[i] = AutoFanSpeedPercentage; this.fanInfo[i] = new FanInformation(0, 0, true, false, cfg.FanDisplayName); } }
public FanControl( FanControlConfigV2 config, ITemperatureFilter filter, IEmbeddedController ec, ITemperatureMonitor tempMon) { if (config == null) { throw new ArgumentNullException(nameof(config)); } if (filter == null) { throw new ArgumentNullException(nameof(filter)); } if (ec == null) { throw new ArgumentNullException(nameof(ec)); } if (tempMon == null) { throw new ArgumentNullException(nameof(tempMon)); } this.ec = ec; this.tempMon = tempMon; this.tempFilter = filter; this.config = (FanControlConfigV2)config.Clone(); this.pollInterval = config.EcPollInterval; this.requestedSpeeds = new float[config.FanConfigurations.Count]; this.fanInfo = new FanInformation[config.FanConfigurations.Count]; this.fans = new Fan[config.FanConfigurations.Count]; this.lockTimeout = Math.Min(MaxLockTimeout, config.EcPollInterval); this.asyncOp = AsyncOperationManager.CreateOperation(null); for (int i = 0; i < config.FanConfigurations.Count; i++) { var cfg = this.config.FanConfigurations[i]; if (string.IsNullOrWhiteSpace(cfg.FanDisplayName)) { cfg.FanDisplayName = "Fan #" + (i + 1); } this.fanInfo[i] = new FanInformation(0, 0, true, false, cfg.FanDisplayName); this.fans[i] = new Fan(this.ec, cfg, config.CriticalTemperature, config.ReadWriteWords); this.requestedSpeeds[i] = AutoFanSpeedPercentage; } }
public static void UpdatesExistingConfigs() { var cfgMan = new ConfigManager <FanControlConfigV2>( Environment.CurrentDirectory, ".xml", new MockFileSystem()); string id = "foo"; var cfg = new FanControlConfigV2(); cfgMan.AddConfig(cfg, id); cfg.Author = "bar"; cfgMan.UpdateConfig(id, cfg); var updatedCfg = cfgMan.GetConfig(id); Assert.True(updatedCfg.Author == cfg.Author); }
private bool TryLoadConfig(string id, out FanControlConfigV2 config) { bool result = false; var configManager = new FanControlConfigManager(FanControlService.ConfigsDirectory); if (!string.IsNullOrWhiteSpace(id) && configManager.SelectConfig(id)) { this.selectedConfig = configManager.SelectedConfigName; config = configManager.SelectedConfig; result = true; } else { config = null; } return(result); }
private static bool TryInitializeFanControl(FanControlConfigV2 cfg, out FanControl fanControl) { bool success = false; fanControl = null; try { float[] speeds = SettingsService.Settings.TargetFanSpeeds; if (speeds == null || speeds.Length != cfg.FanConfigurations.Count) { speeds = new float[cfg.FanConfigurations.Count]; for (int i = 0; i < speeds.Length; i++) { speeds[i] = FanControl.AutoFanSpeedPercentage; } SettingsService.Settings.TargetFanSpeeds = speeds; SaveSettings(); } fanControl = new FanControl(cfg); for (int i = 0; i < speeds.Length; i++) { fanControl.SetTargetFanSpeed(speeds[i], i); } success = true; } finally { if (!success && fanControl != null) { fanControl.Dispose(); fanControl = null; } } return(success); }
public void ResetsRegisterWriteConfigurations() { var fanCfg = new FanConfiguration(); var regWriteCfg = new RegisterWriteConfiguration() { Register = 123, Value = 12, ResetRequired = true, ResetValue = 24 }; var cfg = new FanControlConfigV2() { EcPollInterval = 100, FanConfigurations = { fanCfg }, RegisterWriteConfigurations = { regWriteCfg } }; var filter = A.Fake <ITemperatureFilter>(); A.CallTo(() => filter.FilterTemperature(A <double> .Ignored)).ReturnsLazily((double x) => x); var ec = A.Fake <IEmbeddedController>(); A.CallTo(() => ec.IsInitialized).Returns(true); A.CallTo(() => ec.AcquireLock(A <int> .Ignored)).Returns(true); A.CallTo(() => ec.ReadByte(A <byte> .Ignored)).Returns((byte)0); var tempMon = A.Fake <ITemperatureMonitor>(); A.CallTo(() => tempMon.IsInitialized).Returns(true); using (var fanControl = new FanControl(cfg, filter, ec, tempMon)) { fanControl.Start(false); fanControl.Stop(); A.CallTo(() => ec.WriteByte((byte)regWriteCfg.Register, (byte)regWriteCfg.ResetValue)) .MustHaveHappened(); } }
internal FanControl( FanControlConfigV2 config, ITemperatureFilter filter, IEmbeddedController ec, ITemperatureMonitor tempMon, Fan[] fans) : this(config, filter, ec, tempMon) { if (fans == null) { throw new ArgumentNullException(nameof(fans)); } if (fans.Length != this.fans.Length) { throw new ArgumentException( "The length must be equal to the number of fan configurations", nameof(fans)); } this.fans = fans; }
public void TriesToForceResetFans() { var fanCfg = new FanConfiguration() { ResetRequired = true }; var cfg = new FanControlConfigV2() { EcPollInterval = 100, FanConfigurations = { fanCfg } }; var filter = A.Fake <ITemperatureFilter>(); A.CallTo(() => filter.FilterTemperature(A <double> .Ignored)).ReturnsLazily((double x) => x); var ec = A.Fake <IEmbeddedController>(); A.CallTo(() => ec.IsInitialized).Returns(true); A.CallTo(() => ec.AcquireLock(A <int> .Ignored)).Returns(true); var tempMon = A.Fake <ITemperatureMonitor>(); A.CallTo(() => tempMon.IsInitialized).Returns(true); var fan = A.Fake <Fan>(opt => opt.WithArgumentsForConstructor( new object[] { ec, fanCfg, 70, false })); A.CallTo(() => fan.GetCurrentSpeed()).Returns(0); using (var fanControl = new FanControl(cfg, filter, ec, tempMon, new[] { fan })) { fanControl.Start(false); A.CallTo(() => ec.AcquireLock(A <int> .Ignored)).Returns(false); } A.CallTo(() => fan.Reset()).MustHaveHappened(); }
private static FanControlConfigV2 ConvertViewModelToConfig(MainViewModel viewModel) { var config = new FanControlConfigV2 { CriticalTemperature = viewModel.CriticalTemperature, EcPollInterval = viewModel.EcPollInterval, NotebookModel = viewModel.NotebookModel, Author = viewModel.Author, ReadWriteWords = viewModel.ReadWriteWords }; if (viewModel.FanConfigs != null) { config.FanConfigurations = ConvertViewModelsToFanConfigs(viewModel.FanConfigs); } if (viewModel.RegisterWriteConfigs != null) { config.RegisterWriteConfigurations = ConvertViewModelsToRegisterWriteConfigs(viewModel.RegisterWriteConfigs); } return(config); }
public static void DoNotRecommendConfigsWithSameRwRegisters() { string[] notebooks = new[] { "HP ProBook 1234", "HP ProBook 1235" }; var cfgMan = new FanControlConfigManager( Environment.CurrentDirectory, ".xml", new MockFileSystem()); foreach (string s in notebooks) { var cfg = new FanControlConfigV2() { FanConfigurations = new List <FanConfiguration>() { new FanConfiguration() } }; cfgMan.AddConfig(cfg, s); } List <string> recommendations = cfgMan.RecommendConfigs("HP ProBook 1234"); Assert.Contains(notebooks[0], recommendations); Assert.DoesNotContain(notebooks[1], recommendations); }
private static ITemperatureFilter CreateTemperatureFilter(FanControlConfigV2 cfg) { int interval = Math.Max(cfg.EcPollInterval, MinPollInterval); return new ArithmeticMeanTemperatureFilter(interval); }
public FanControl(FanControlConfigV2 config) : this(config, PluginsDirectory) { }
public FanControl(FanControlConfigV2 config, string pluginsDirectory) : this( config, CreateTemperatureFilter(config), pluginsDirectory) { }