From a007b76ba99b0260dec94bfe1cb80dcb0af10d15 Mon Sep 17 00:00:00 2001 From: zsien Date: Fri, 15 Nov 2024 16:01:14 +0800 Subject: [PATCH] feat: automatic switch power saving mode threshold Task: https://pms.uniontech.com/task-view-368069.html --- system/power1/battery_display.go | 3 +-- system/power1/battery_display_test.go | 8 ++++---- system/power1/manager.go | 23 +++++++++++++++++++++-- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/system/power1/battery_display.go b/system/power1/battery_display.go index 59a048004..59d6f831f 100644 --- a/system/power1/battery_display.go +++ b/system/power1/battery_display.go @@ -121,10 +121,9 @@ func (m *Manager) refreshBatteryDisplay() { } } -const lowBatteryThreshold = 20.0 // TODO dconfig func (m *Manager) changeBatteryLowByBatteryPercentage(percentage float64) { logger.Debug("changeBatteryLowByBatteryPercentage, battery percentage: ", percentage) - batteryLow := percentage <= lowBatteryThreshold + batteryLow := percentage <= float64(m.PowerSavingModeAutoBatteryPercent) if m.batteryLow != batteryLow { m.batteryLow = batteryLow m.updatePowerMode(false) // refresh battery percentage diff --git a/system/power1/battery_display_test.go b/system/power1/battery_display_test.go index 0619fe86e..296bd2f93 100644 --- a/system/power1/battery_display_test.go +++ b/system/power1/battery_display_test.go @@ -23,17 +23,17 @@ func Test_rightPercentage(t *testing.T) { func Test_changeBatteryLowByBatteryPercentage(t *testing.T) { m := Manager{} - percentage := lowBatteryThreshold - 1 + percentage := float64(m.PowerSavingModeAutoBatteryPercent - 1) m.changeBatteryLowByBatteryPercentage(percentage) - percentage = lowBatteryThreshold + 1 + percentage = float64(m.PowerSavingModeAutoBatteryPercent + 1) m.changeBatteryLowByBatteryPercentage(percentage) - percentage = lowBatteryThreshold + 1 + percentage = float64(m.PowerSavingModeAutoBatteryPercent + 1) m.batteryLow = false m.changeBatteryLowByBatteryPercentage(percentage) - percentage = lowBatteryThreshold + 1 + percentage = float64(m.PowerSavingModeAutoBatteryPercent + 1) m.batteryLow = true m.changeBatteryLowByBatteryPercentage(percentage) } diff --git a/system/power1/manager.go b/system/power1/manager.go index 0ec39a05b..84517add3 100644 --- a/system/power1/manager.go +++ b/system/power1/manager.go @@ -7,6 +7,7 @@ package power import ( "encoding/json" "errors" + "io/ioutil" "os" "sync" "time" @@ -81,6 +82,9 @@ type Manager struct { // 开启节能模式时降低亮度的百分比值 PowerSavingModeBrightnessDropPercent uint32 `prop:"access:rw"` + // 开启自动节能模式电量的百分比值 + PowerSavingModeAutoBatteryPercent uint32 `prop:"access:rw"` + // 开启节能模式时保存的数据 PowerSavingModeBrightnessData string `prop:"access:rw"` @@ -271,6 +275,7 @@ func (m *Manager) initDsgConfig() error { m.PowerSavingModeAuto = cfg.PowerSavingModeAuto // 自动切换节能模式,依据为是否插拔电源 m.PowerSavingModeAutoWhenBatteryLow = cfg.PowerSavingModeAutoWhenBatteryLow // 低电量时自动开启 m.PowerSavingModeBrightnessDropPercent = cfg.PowerSavingModeBrightnessDropPercent // 开启节能模式时降低亮度的百分比值 + m.PowerSavingModeAutoBatteryPercent = cfg.PowerSavingModeAutoBatteryPercent // 开启在低电量自动节能模式时候的百分比 m.Mode = cfg.Mode migrateErr := m.migrateFromCurrentConfigsToDsg() if migrateErr != nil { @@ -601,11 +606,12 @@ type Config struct { PowerSavingModeAuto bool PowerSavingModeAutoWhenBatteryLow bool PowerSavingModeBrightnessDropPercent uint32 + PowerSavingModeAutoBatteryPercent uint32 Mode string } func loadConfig() (*Config, error) { - content, err := os.ReadFile(configFile) + content, err := ioutil.ReadFile(configFile) if err != nil { return nil, err } @@ -625,7 +631,15 @@ func loadConfigSafe() *Config { if !os.IsNotExist(err) { logger.Warning(err) } - return nil + return &Config{ + // default config + PowerSavingModeAuto: true, + PowerSavingModeEnabled: false, + PowerSavingModeAutoWhenBatteryLow: false, + PowerSavingModeBrightnessDropPercent: 20, + PowerSavingModeAutoBatteryPercent: 20, + Mode: ddeBalance, + } } // 新增字段后第一次启动时,缺少两个新增字段的json,导致亮度下降百分比字段默认为0,导致与默认值不符,需要处理 // 低电量自动待机字段的默认值为false,不会导致错误影响 @@ -634,6 +648,11 @@ func loadConfigSafe() *Config { cfg.PowerSavingModeBrightnessDropPercent = 20 } + // when PowerSavingModeAutoBatteryPercent is lower than 10, it is not available, so change set it to 20 as default + if cfg.PowerSavingModeAutoBatteryPercent < 10 { + cfg.PowerSavingModeAutoBatteryPercent = 20 + } + if cfg.Mode == "" { cfg.Mode = ddeBalance }