protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { var powerState = PowerState.GetPowerState(); if (powerState.BatteryFlag == BatteryFlag.NoSystemBattery) { LogService.LogService.Log("No system battery"); await StopAsync(stoppingToken); } else { var batteryBrightness = GetBatteryBrightness(); var chargeBrightness = GetChargeBrightness(); var currentBrightness = BrightnessService.BrightnessService.GetBrightness(); var desiredBrightness = powerState.ACLineStatus == AcLineStatus.Offline ? batteryBrightness : chargeBrightness; if (currentBrightness != desiredBrightness) { BrightnessService.BrightnessService.SetBrightness(desiredBrightness); LogService.LogService.Log($"CurrentBrightness: {desiredBrightness}"); } } await Task.Delay(1000, stoppingToken); } }
protected override bool OnPowerEvent(PowerBroadcastStatus powerStatus) { string tm = DateTime.Now.ToString(); PowerState PS = new PowerState(); if (PS.GetPowerState().ACLineStatus == ACLineStatus.Online) { if (RunSetBrightnessProc(ACBrightnessValue)) //Running external program that uses WMI { LF.WriteLog($"{tm} Power source changed to AC. Brightness set to {ACBrightnessValue}%"); } } if (PS.GetPowerState().ACLineStatus == ACLineStatus.Offline) { if (RunSetBrightnessProc(DCBrightnessValue)) { LF.WriteLog($"{tm} Power source changed to DC. Brightness set to {DCBrightnessValue}%"); } } return(true); }