/// <summary> /// Handles a power state change. /// </summary> /// <param name = "batteryService">The battery service.</param> public void HandlePowerStateChange(IBatteryService batteryService) { // State change, make sure we're in a valid state if (batteryService.IsValidState) { if (_lastPowerEvent == null // FSIGAP - just keep track of ac state changes.. battery level changes were causing battery/AC code to run || _lastPowerEvent == EventType.SwitchToAc && batteryService.OnBattery || // changed from ac to on battery _lastPowerEvent == EventType.SwitchToBattery && batteryService.OnAcPower // changes from battery to on ac ) { EventType eventType; if (batteryService.OnAcPower) { eventType = EventType.SwitchToAc; } else { eventType = EventType.SwitchToBattery; } // Check the event state //ProcessStateChange(eventType); _lastPowerEvent = eventType; // Execute all of the actions for this event if they are within the threshold var batteryLifePercent = batteryService.GetSystemPowerStatus().BatteryLifePercent; _profile.GetActionsForEvent(eventType) .TakeWhile(a => batteryLifePercent >= (a.BatteryPercentMin) && batteryLifePercent <= (a.BatteryPercentMax)) .Each(a => a.Execute()); } } }
/// <summary> /// Monitors the state. /// </summary> /// <param name = "batteryService">The battery service.</param> private void MonitorBatteryLevel(IBatteryService batteryService) { if (_lastKnownState == null) { _lastKnownState = batteryService.GetSystemPowerStatus(); } var currentState = batteryService.GetSystemPowerStatus(); if (currentState.BatteryLifePercent != _lastKnownState.BatteryLifePercent) { _lastKnownState = currentState; HandleBatteryLevelChange(batteryService, currentState.BatteryLifePercent, currentState.BatteryLifePercent - _lastKnownState.BatteryLifePercent); return; } _lastKnownState = currentState; }