mirror of
https://github.com/YuleBest/LuminPro.git
synced 2026-12-04 00:54:42 +08:00
- internal/api:状态聚合(status)、操作锁读写与自愈、daemon 运行状态、 日志读取解析、module.prop 解析与通道判定 - status 一次调用取代原先 5~6 条 shell 命令;--no-display 跳过 dumpsys/settings - oplock 文件改为 JSON 并带 kind(ramp/boost/restart/manual), WebUI 可显示具体操作文案;兼容旧版裸 PID 格式 - daemon 维护 pid/state.json(模式、最近事件/操作、缓存的比率与亮度) - 新子命令:status / oplock / brightness set / config read|patch(stdin) / log tail|clear|export;config patch 走 stdin 并严格校验类型 - pid/inotifyd.pid → pid/daemon.pid;清理 action.sh 死标记、统一文案
86 lines
2.1 KiB
Go
86 lines
2.1 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
)
|
|
|
|
// daemon 运行状态文件的 DTO。daemon 每次判定后刷新,status 直接读取,
|
|
// 避免前端高频刷新时反复调用 dumpsys。
|
|
|
|
// EventInfo 最近一次监听到的事件。
|
|
type EventInfo struct {
|
|
Letters string `json:"letters"`
|
|
At int64 `json:"at"`
|
|
}
|
|
|
|
// ActionInfo 最近一次亮度操作。
|
|
type ActionInfo struct {
|
|
Kind string `json:"kind"`
|
|
From int `json:"from"`
|
|
To int `json:"to"`
|
|
At int64 `json:"at"`
|
|
}
|
|
|
|
// RatioInfo 最近一次读到的 HDR/SDR 比率。
|
|
type RatioInfo struct {
|
|
Value float64 `json:"value"`
|
|
At int64 `json:"at"`
|
|
}
|
|
|
|
// BoolInfo 带时间戳的布尔值。
|
|
type BoolInfo struct {
|
|
Value bool `json:"value"`
|
|
At int64 `json:"at"`
|
|
}
|
|
|
|
// IntInfo 带时间戳的整数。
|
|
type IntInfo struct {
|
|
Value int `json:"value"`
|
|
At int64 `json:"at"`
|
|
}
|
|
|
|
// DaemonState 是 pid/state.json 的内容。
|
|
type DaemonState struct {
|
|
PID int `json:"pid"`
|
|
StartedAt int64 `json:"startedAt"`
|
|
Mode string `json:"mode"` // event | polling
|
|
Node string `json:"node"`
|
|
Paused bool `json:"paused"`
|
|
HdrSleep bool `json:"hdrSleep"`
|
|
LastEvent *EventInfo `json:"lastEvent,omitempty"`
|
|
LastAction *ActionInfo `json:"lastAction,omitempty"`
|
|
Ratio *RatioInfo `json:"ratio,omitempty"`
|
|
AutoBrightness *BoolInfo `json:"autoBrightness,omitempty"`
|
|
Brightness *IntInfo `json:"brightness,omitempty"`
|
|
}
|
|
|
|
// WriteState 原子写入运行状态。
|
|
func WriteState(path string, s DaemonState) error {
|
|
data, err := json.Marshal(s)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
tmp := path + ".tmp"
|
|
if err := os.WriteFile(tmp, data, 0o644); err != nil {
|
|
return err
|
|
}
|
|
return os.Rename(tmp, path)
|
|
}
|
|
|
|
// ReadState 读取运行状态;文件缺失或损坏时返回 false。
|
|
func ReadState(path string) (DaemonState, bool) {
|
|
var s DaemonState
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return s, false
|
|
}
|
|
if err := json.Unmarshal(data, &s); err != nil {
|
|
return s, false
|
|
}
|
|
if s.PID <= 0 || !ProcessAlive(s.PID) {
|
|
return s, false
|
|
}
|
|
return s, true
|
|
}
|