Files
LuminPro/go/internal/daemon/daemon.go
T
YuleBest db3cc2faa5 feat(go): 新增 system 与 daemon 包及 CLI 入口
- system: settings/dumpsys 调用接口化,命令失败按无数据降级;
  mCurrentFocus 解析复刻旧 sed 逻辑
- daemon: 单进程守护(inotify 事件驱动 / 兼容模式轮询)、配置热重载、
  节点缺失 10s 重试、节点删除/移动时自动重建监听、渐变期持 oplock
- ops: boost 一键峰值/恢复、restart 经 SIGHUP 重建监听或按需拉起
- cmd/luminpro: daemon|boost|restart|config 子命令,安装脚本可用 config 替代 jq
- host 端到端冒烟已验证:事件触发、防抖、boost、oplock 窗口、
  stop.flag 暂停、兼容模式、节点删除恢复、旧配置迁移
2026-09-10 04:19:41 +08:00

482 lines
12 KiB
Go

// Package daemon 实现常驻守护进程,以及 boost / restart 一次性操作。
//
// 它取代旧的 service.sh + daemon.sh + up.sh + lumipro 四层结构:
// 单进程内完成监听、判定、渐变写入与状态文件维护。
package daemon
import (
"fmt"
"os"
"os/signal"
"path/filepath"
"strconv"
"strings"
"sync"
"syscall"
"time"
"github.com/YuleBest/LuminPro/go/internal/brightness"
"github.com/YuleBest/LuminPro/go/internal/config"
"github.com/YuleBest/LuminPro/go/internal/inotify"
"github.com/YuleBest/LuminPro/go/internal/logging"
"github.com/YuleBest/LuminPro/go/internal/policy"
"github.com/YuleBest/LuminPro/go/internal/system"
)
// Paths 是模块内的文件布局(与 WebUI 的契约,不可随意改名)。
type Paths struct {
ModuleDir string
PIDDir string
ConfigFile string
LogFile string
PIDFile string
StopFlag string
PauseFlag string
Oplock string
BoostFlag string
}
// DefaultPaths 按模块目录推导全部路径。
func DefaultPaths(moduleDir string) Paths {
pidDir := filepath.Join(moduleDir, "pid")
return Paths{
ModuleDir: moduleDir,
PIDDir: pidDir,
ConfigFile: filepath.Join(moduleDir, "config", "config.json"),
LogFile: filepath.Join(moduleDir, "service.log"),
PIDFile: filepath.Join(pidDir, "inotifyd.pid"),
StopFlag: filepath.Join(pidDir, "stop.flag"),
PauseFlag: filepath.Join(pidDir, "daemon.pause"),
Oplock: filepath.Join(pidDir, "oplock"),
BoostFlag: filepath.Join(pidDir, "boost.flag"),
}
}
// Deps 是守护进程的外部依赖,测试时可注入假实现。
type Deps struct {
AutoBrightness system.AutoBrightness
Focus system.Focus
HDR system.HdrRatio
Log *logging.Logger
Now func() time.Time
StepDelay time.Duration // 渐变每步间隔,默认 20ms
Debounce time.Duration // 事件防抖窗口,默认 300ms
CompatInterval time.Duration // 兼容模式轮询间隔,默认 2s
NodeRetry time.Duration // 节点缺失重试间隔,默认 10s
SettleDelay time.Duration // 等待亮度值落定的间隔,默认 300ms
}
func (d *Deps) withDefaults() {
if d.Now == nil {
d.Now = time.Now
}
if d.StepDelay <= 0 {
d.StepDelay = 20 * time.Millisecond
}
if d.Debounce <= 0 {
d.Debounce = 300 * time.Millisecond
}
if d.CompatInterval <= 0 {
d.CompatInterval = 2 * time.Second
}
if d.NodeRetry <= 0 {
d.NodeRetry = 10 * time.Second
}
if d.SettleDelay <= 0 {
d.SettleDelay = 300 * time.Millisecond
}
}
// Daemon 是守护进程。
type Daemon struct {
paths Paths
deps Deps
mu sync.RWMutex
cfg config.Config
hdr policy.HdrState
ratioCache float64
haveRatioCache bool
nodeMissingLogged bool
}
// New 创建守护进程并加载初始配置。
func New(paths Paths, deps Deps) (*Daemon, error) {
deps.withDefaults()
d := &Daemon{paths: paths, deps: deps}
cfg, err := config.Load(paths.ConfigFile)
if err != nil {
return nil, err
}
d.setConfig(cfg)
return d, nil
}
type exitReason int
const (
reasonReload exitReason = iota
reasonExit
reasonError
)
// Run 启动主循环,直到收到退出信号。
func (d *Daemon) Run() error {
if err := os.MkdirAll(d.paths.PIDDir, 0o755); err != nil {
return err
}
if err := d.writePID(); err != nil {
return err
}
defer d.cleanup()
d.log().Info("守护进程已启动")
sigCh := make(chan os.Signal, 4)
signal.Notify(sigCh, syscall.SIGHUP, syscall.SIGTERM, syscall.SIGINT)
defer signal.Stop(sigCh)
cfgCh := make(chan struct{}, 1)
stopCfgPoll := d.pollConfig(cfgCh)
defer stopCfgPoll()
for {
node := d.currentConfig().NowBriFile
if _, err := os.Stat(node); err != nil {
d.logNodeMissing(node)
select {
case <-time.After(d.deps.NodeRetry):
d.reloadConfig()
continue
case sig := <-sigCh:
if d.handleSignal(sig) {
return nil
}
d.reloadConfig()
continue
case <-cfgCh:
d.reloadConfig()
continue
}
}
if d.nodeMissingLogged {
d.log().Info("亮度节点已就绪: " + node)
d.nodeMissingLogged = false
}
reason := d.serve(sigCh, cfgCh)
switch reason {
case reasonExit:
return nil
case reasonError:
select {
case <-time.After(time.Second):
case sig := <-sigCh:
if d.handleSignal(sig) {
return nil
}
}
}
d.reloadConfig()
// 重写 PID 文件,作为「监听已重建」的就绪标记(restart 子命令据此等待)
if err := d.writePID(); err != nil {
d.log().Warn("重写 PID 文件失败: " + err.Error())
}
}
}
// serve 运行监听循环,直到配置变更/信号/监听失效。
func (d *Daemon) serve(sigCh chan os.Signal, cfgCh chan struct{}) exitReason {
stop := make(chan struct{})
defer close(stop)
// 事件模式下,节点被删除或监听失效时通过该通道请求重建
rebuild := make(chan struct{}, 1)
cfg := d.currentConfig()
if cfg.CompatibilityMode == 1 {
d.log().Warn("兼容模式已开启 (轮询驱动)")
go d.compatLoop(stop)
} else {
w, err := inotify.New()
if err != nil {
d.log().Error("初始化 inotify 失败: " + err.Error())
return reasonError
}
defer w.Close()
if err := w.Add(cfg.NowBriFile, cfg.InotifyEvents); err != nil {
d.log().Error("监听亮度节点失败: " + err.Error())
return reasonError
}
d.log().Info(fmt.Sprintf("正在监听: %s, 事件: %s", cfg.NowBriFile, cfg.InotifyEvents))
runStop := make(chan struct{})
go func() {
_ = w.NewRunner(d.deps.Debounce, d.debugf).Run(func(ev inotify.Event) {
// 节点被删除 (D) / 监听失效 (x) / 被移动 (M):交给外层循环重建
if strings.ContainsAny(ev.Letters, "xDM") {
select {
case rebuild <- struct{}{}:
default:
}
return
}
d.evaluate()
}, runStop)
}()
defer func() {
close(runStop)
w.Stop()
}()
}
for {
select {
case sig := <-sigCh:
if d.handleSignal(sig) {
return reasonExit
}
d.log().Info("收到重载信号,重建监听")
return reasonReload
case <-cfgCh:
d.log().Info("检测到配置变更,重新加载")
return reasonReload
case <-rebuild:
d.log().Warn("亮度节点监听失效,重建监听")
return reasonReload
}
}
}
// compatLoop 兼容模式:定期轮询执行判定。
func (d *Daemon) compatLoop(stop <-chan struct{}) {
ticker := time.NewTicker(d.deps.CompatInterval)
defer ticker.Stop()
for {
select {
case <-stop:
d.log().Info("轮询模式已退出")
return
case <-ticker.C:
d.evaluate()
}
}
}
// evaluate 执行一次完整判定;满足条件时执行亮度提升。
func (d *Daemon) evaluate() {
// 服务暂停 / 守护进程挂起:静默跳过(与旧脚本一致)
if fileExists(d.paths.StopFlag) || fileExists(d.paths.PauseFlag) {
return
}
cfg := d.currentConfig()
if cfg.UIMaxBri <= 0 || cfg.MaxBri <= 0 {
return // 未校准,等价于旧脚本的 `now >= 0 && now < 0`
}
if !fileExists(cfg.NowBriFile) {
return // 节点缺失由外层循环负责记录与重试,这里静默
}
if policy.InSleepWindow(cfg.SleepTime, d.deps.Now()) {
d.log().Info(fmt.Sprintf("处于休眠时段 (%s),跳过提升", cfg.SleepTime))
return
}
if cfg.AutoBriSleep == 1 && d.deps.AutoBrightness.Enabled() {
d.log().Info("自动亮度已启用,跳过提升")
return
}
if len(cfg.BlacklistApps) > 0 {
focus := d.deps.Focus.CurrentFocus()
if policy.BlacklistMatch(cfg.BlacklistApps, focus) {
d.log().Info(fmt.Sprintf("当前前台 (%s) 在黑名单中,跳过提升", focus))
return
}
}
if cfg.DisplayHdrSleep == 1 && !d.evaluateHdr(cfg) {
return
}
d.boost(cfg)
}
// evaluateHdr 推进 HDR 迟滞状态机,返回是否可以继续提升。
func (d *Daemon) evaluateHdr(cfg config.Config) bool {
ratio, ok := d.deps.HDR.Ratio()
if !ok && d.haveRatioCache {
ratio, ok = d.ratioCache, true
d.log().Info(fmt.Sprintf("HDR 比率读取为空,使用缓存值: %.2f", ratio))
}
if ok {
d.ratioCache, d.haveRatioCache = ratio, true
}
decision := d.hdr.Evaluate(ratio, ok, cfg.HdrEnterRatio, cfg.HdrExitRatio)
switch {
case decision.Entered:
d.log().Info(fmt.Sprintf("检测到 HDR 内容 (比率: %.2f ≥ 进入阈值 %.2f),进入休眠", ratio, cfg.HdrEnterRatio))
case decision.Exited:
d.log().Info(fmt.Sprintf("HDR 休眠解除 (比率: %.2f ≤ 退出阈值 %.2f)", ratio, cfg.HdrExitRatio))
case decision.Skip:
d.log().Info("HDR 休眠中,跳过提升")
}
return !decision.Skip
}
// boost 读取当前亮度并在满足阈值时执行渐变。
//
// 与旧 CHECK_BRI 一致:最多 10 次、间隔 0.3s 等待亮度值落定;
// 额外优化为「读数稳定且不满足条件时提前退出」,避免无谓占用事件循环。
func (d *Daemon) boost(cfg config.Config) {
prev := -1
for i := 0; i < 10; i++ {
now, err := brightness.ReadInt(cfg.NowBriFile)
if err != nil {
d.log().Error("读取亮度节点失败: " + err.Error())
return
}
if policy.ShouldBoost(now, cfg.UIMaxBri, cfg.MaxBri) {
d.log().Info(fmt.Sprintf("触发提升: 当前亮度 %d ≥ 阈值 %d,目标 %d", now, cfg.UIMaxBri, cfg.MaxBri))
if err := d.ramp(cfg, now); err != nil {
d.log().Error("亮度提升失败: " + err.Error())
return
}
d.log().Success(fmt.Sprintf("亮度提升完成 (%d)", cfg.MaxBri))
return
}
if prev == now {
return
}
prev = now
time.Sleep(d.deps.SettleDelay)
}
}
// ramp 执行渐变写入,期间持操作锁。
func (d *Daemon) ramp(cfg config.Config, start int) error {
d.log().Info(fmt.Sprintf("开始渐变调整: %d → %d (%d 步)", start, cfg.MaxBri, cfg.StepsNum))
if err := d.writeOplock(); err != nil {
d.log().Warn("写入操作锁失败: " + err.Error())
}
defer d.removeOplock()
return brightness.Ramp(cfg.NowBriFile, start, cfg.MaxBri, cfg.StepsNum, d.deps.StepDelay, nil)
}
// pollConfig 定期比对 config.json 的 mtime/大小,变更时通知主循环。
// 用轮询而非 inotify:配置写入可能使用「临时文件 + rename」,会打断对旧 inode 的监听。
func (d *Daemon) pollConfig(cfgCh chan struct{}) func() {
stop := make(chan struct{})
go func() {
var lastMod time.Time
var lastSize int64
if fi, err := os.Stat(d.paths.ConfigFile); err == nil {
lastMod, lastSize = fi.ModTime(), fi.Size()
}
ticker := time.NewTicker(2 * time.Second)
defer ticker.Stop()
for {
select {
case <-stop:
return
case <-ticker.C:
fi, err := os.Stat(d.paths.ConfigFile)
if err != nil {
continue
}
if fi.ModTime() != lastMod || fi.Size() != lastSize {
lastMod, lastSize = fi.ModTime(), fi.Size()
select {
case cfgCh <- struct{}{}:
default:
}
}
}
}
}()
return func() { close(stop) }
}
func (d *Daemon) handleSignal(sig os.Signal) bool {
if sig == syscall.SIGHUP {
return false
}
d.log().Info("收到退出信号,停止守护进程")
return true
}
func (d *Daemon) reloadConfig() {
cfg, err := config.Load(d.paths.ConfigFile)
if err != nil {
d.log().Error("加载配置失败: " + err.Error())
return
}
d.setConfig(cfg)
d.log().Info("配置已重新加载")
}
func (d *Daemon) setConfig(cfg config.Config) {
d.mu.Lock()
d.cfg = cfg
d.mu.Unlock()
if d.deps.Log != nil {
d.deps.Log.SetLevel(cfg.LogLevel)
d.deps.Log.SetMaxSizeKB(cfg.LogMaxSize)
d.deps.Log.SetDebug(cfg.DebugMode == 1)
}
}
func (d *Daemon) currentConfig() config.Config {
d.mu.RLock()
defer d.mu.RUnlock()
return d.cfg
}
func (d *Daemon) logNodeMissing(node string) {
if d.nodeMissingLogged {
return
}
d.log().Error("亮度节点不存在: " + node + ",功能挂起,每 10 秒重试")
d.nodeMissingLogged = true
}
func (d *Daemon) log() *logging.Logger {
return d.deps.Log
}
func (d *Daemon) debugf(format string, args ...any) {
if d.deps.Log != nil {
d.deps.Log.Debug(format, args...)
}
}
func (d *Daemon) writePID() error {
if err := os.MkdirAll(d.paths.PIDDir, 0o755); err != nil {
return err
}
return os.WriteFile(d.paths.PIDFile, []byte(strconv.Itoa(os.Getpid())), 0o644)
}
func (d *Daemon) writeOplock() error {
if err := os.MkdirAll(d.paths.PIDDir, 0o755); err != nil {
return err
}
return os.WriteFile(d.paths.Oplock, []byte(strconv.Itoa(os.Getpid())), 0o644)
}
func (d *Daemon) removeOplock() {
_ = os.Remove(d.paths.Oplock)
}
func (d *Daemon) cleanup() {
_ = os.Remove(d.paths.PIDFile)
_ = os.Remove(d.paths.Oplock)
}
func fileExists(path string) bool {
info, err := os.Stat(path)
return err == nil && !info.IsDir()
}