Skip to content

Commit

Permalink
feat: automatic switch power saving mode threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
zsien committed Nov 15, 2024
1 parent 88c35b2 commit a007b76
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
3 changes: 1 addition & 2 deletions system/power1/battery_display.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions system/power1/battery_display_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
23 changes: 21 additions & 2 deletions system/power1/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package power
import (
"encoding/json"
"errors"
"io/ioutil"
"os"
"sync"
"time"
Expand Down Expand Up @@ -81,6 +82,9 @@ type Manager struct {
// 开启节能模式时降低亮度的百分比值
PowerSavingModeBrightnessDropPercent uint32 `prop:"access:rw"`

// 开启自动节能模式电量的百分比值
PowerSavingModeAutoBatteryPercent uint32 `prop:"access:rw"`

// 开启节能模式时保存的数据
PowerSavingModeBrightnessData string `prop:"access:rw"`

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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,不会导致错误影响
Expand All @@ -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
}
Expand Down

0 comments on commit a007b76

Please sign in to comment.