feat: 新增功能允许用户自定义 inotifyd 要监测的事件(默认是 c),允许通过配置文件和 webui 修改

This commit is contained in:
YuleBest committed 2026-04-06 04:30:45 +08:00
1 parent 8431079246
commit b1d2a444c8
8 files changed
+81 -28

No files matched your search

+13 -5
View File
@@ -26,7 +26,8 @@ get_config_value() {
# 1. 检查旧配置是否存在且非空
if [ -f "$old_file" ]; then
local old_val="$(cat "$old_file" 2>/dev/null | tr -d ' \n')"
local old_val
old_val="$(cat "$old_file" 2>/dev/null | tr -d ' \n')"
if [ -n "$old_val" ]; then
echo "$old_val"
return
@@ -35,7 +36,8 @@ get_config_value() {
# 2. 检查默认配置是否存在且非空
if [ -f "$new_file" ]; then
local new_val="$(cat "$new_file" 2>/dev/null | tr -d ' \n')"
local new_val
new_val="$(cat "$new_file" 2>/dev/null | tr -d ' \n')"
if [ -n "$new_val" ]; then
echo "$new_val"
return
@@ -199,8 +201,10 @@ END() {
sleep 1
# 读取最终配置
local final_max_bri="$(get_config_value "max_bri.txt" "")"
local final_ui_max_bri="$(get_config_value "ui_max_bri.txt" "")"
local final_max_bri
local final_ui_max_bri
final_max_bri="$(get_config_value "max_bri.txt" "")"
final_ui_max_bri="$(get_config_value "ui_max_bri.txt" "")"
# 检查是否需要重新测试
if [ -z "$final_max_bri" ] || [ -z "$final_ui_max_bri" ]; then
@@ -278,7 +282,11 @@ IMPORT_OLD_CONFIG() {
echo "[OK] 已沿用旧配置"
cp -f "$old_config/ui_max_bri.txt" "$mod_config/"
cp -f "$old_config/max_bri.txt" "$mod_config/"
[ -e "$old_config/sleep_time.txt" ] && cp -f "$old_config/sleep_time.txt" "$mod_config/" || touch "$mod_config/sleep_time.txt"
if [ -e "$old_config/sleep_time.txt" ]; then
cp -f "$old_config/sleep_time.txt" "$mod_config/"
else
touch "$mod_config/sleep_time.txt"
fi
[ -s "$old_config/auto_bri_sleep.txt" ] && cp -f "$old_config/auto_bri_sleep.txt" "$mod_config/"
[ -s "$old_config/steps_num.txt" ] && cp -f "$old_config/steps_num.txt" "$mod_config/"
[ -s "$old_config/log_max_size.txt" ] && cp -f "$old_config/log_max_size.txt" "$mod_config/"
+11 -7
View File
@@ -1,5 +1,6 @@
#!/system/bin/sh
# LuminPro 守护进程 (Watchdog)
#shellcheck shell=ash
# LuminPro 守护进程
# 使用 readlink 获取脚本真实绝对路径
REAL_PATH="$(readlink -f "$0")"
@@ -31,14 +32,16 @@ get_config() {
# 获取当前配置的哈希值(用于检测配置变化)
get_config_hash() {
local now_file="$(get_config "$PATH_CONFIG_DIR/now_bri_file.txt" "$DEFAULT_NOW_BRI_FILE")"
echo "$now_file"
local now_file events
now_file="$(get_config "$PATH_CONFIG_DIR/now_bri_file.txt" "$DEFAULT_NOW_BRI_FILE")"
events="$(get_config "$CONFIG_DIR/inotify_events.txt" "c")"
echo "${now_file}|${events}"
}
# 日志函数
_log() {
local level="${2:-INFO}"
printf '[%s] [%-8s] [%-8s] %s\n' "$(date '+%m-%d %H:%M:%S')" "daemon" "$level" "$1" >>"$log_file"
printf '[%s] [%s] [%s] %s\n' "$(date '+%m-%d %H:%M:%S')" "daemon" "$level" "$1" >>"$log_file"
}
# 确保环境就绪
@@ -86,18 +89,19 @@ while true; do
# 正常启动并监听
_log "→ 启动 inotifyd 监听..." "INFO"
now_bri_file="$current_config"
now_bri_file="${current_config%|*}"
inotify_events="${current_config#*|}"
# 更新缓存,让 up.sh 知道当前监听的是哪个文件
echo -n "$now_bri_file" >"$PATH_CONFIG_DIR/.cached_path"
# 启动并重定向 (不再使用子进程管道以保证 PID 抓取准确)
inotifyd "$MODDIR/script/up.sh" "$now_bri_file:c" >>"$log_file" 2>&1 &
inotifyd "$MODDIR/script/up.sh" "$now_bri_file:$inotify_events" >>"$log_file" 2>&1 &
inotify_pid=$!
echo "$inotify_pid" >"$pid_file"
_log "✓ inotifyd 已启动 (PID: $inotify_pid)" "SUCCESS"
_log " 监听文件: $now_bri_file" "INFO"
_log " 监听文件: $now_bri_file, 事件: $inotify_events" "INFO"
# 3. 通过 wait 阻塞,实时感知进程死亡 (兼容所有 BusyBox 环境)
wait "$inotify_pid"
+1 -1
View File
@@ -8,7 +8,7 @@ flag_file="$PID_DIR/up.flag"
log_file="$MODDIR/service.log"
_log() {
printf '[%s] [service.restart] %s\n' "$(date '+%d %H:%M:%S.%3N')" "$1" >>"$log_file"
printf '[%s] [restart] %s\n' "$(date '+%d %H:%M:%S.%3N')" "$1" >>"$log_file"
}
_log "正在手动重启服务..."
+5 -6
View File
@@ -13,7 +13,6 @@ pause_file="$PID_DIR/daemon.pause"
# 默认设备路径
DEFAULT_NOW_BRI_FILE="/sys/class/backlight/panel0-backlight/brightness"
DEFAULT_MAX_BRI_FILE="/sys/class/backlight/panel0-backlight/max_brightness"
# 读取配置函数
get_config() {
@@ -28,7 +27,6 @@ get_config() {
# 获取亮度文件路径
now_bri_file="$(get_config "$PATH_CONFIG_DIR/now_bri_file.txt" "$DEFAULT_NOW_BRI_FILE")"
max_bri_file="$(get_config "$PATH_CONFIG_DIR/max_bri_file.txt" "$DEFAULT_MAX_BRI_FILE")"
# 日志函数
_log() {
@@ -48,11 +46,11 @@ _log() {
local cur_size
cur_size=$(du -k "$log_file" | cut -f1)
if [ "$cur_size" -ge "$max_size" ]; then
printf '[%s] [%-8s] [%-8s] %s\n' "$(date '+%m-%d %H:%M:%S')" "up" "WARN" "日志过大 ($cur_size KB), 自动重置" >"$log_file"
printf '[%s] [%s] [%s] %s\n' "$(date '+%m-%d %H:%M:%S')" "up" "WARN" "日志过大 ($cur_size KB), 自动重置" >"$log_file"
fi
fi
printf '[%s] [%-8s] [%-8s] %s\n' "$(date '+%m-%d %H:%M:%S')" "up" "$level" "$1" >>"$log_file"
printf '[%s] [%s] [%s] %s\n' "$(date '+%m-%d %H:%M:%S')" "up" "$level" "$1" >>"$log_file"
}
_log "[*] 收到 inotifyd 通知 (PID: $$)" "INFO"
@@ -187,8 +185,9 @@ MAIN() {
rm -f "$pause_file" # 解锁守护进程,让它自动拉起 inotifyd
# 如果配置文件被修改了,杀掉 inotifyd 让 daemon 重启并重新读取配置
local current_now_bri="$(cat "$PATH_CONFIG_DIR/now_bri_file.txt" 2>/dev/null || echo "$DEFAULT_NOW_BRI_FILE")"
local cached_now_bri="$(cat "$PATH_CONFIG_DIR/.cached_path" 2>/dev/null || echo "$DEFAULT_NOW_BRI_FILE")"
local current_now_bri cached_now_bri
current_now_bri="$(cat "$PATH_CONFIG_DIR/now_bri_file.txt" 2>/dev/null || echo "$DEFAULT_NOW_BRI_FILE")"
cached_now_bri="$(cat "$PATH_CONFIG_DIR/.cached_path" 2>/dev/null || echo "$DEFAULT_NOW_BRI_FILE")"
if [ "$current_now_bri" != "$cached_now_bri" ]; then
_log "[*] 配置已更新,重启 inotifyd..." "WARN"
View File
Whitespace-only changes.
+10 -1
View File
@@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
<title>LuminPro</title>
<link rel="stylesheet" type="text/css" href="/internal/insets.css" />
<script type="module" crossorigin src="/assets/index-BGcJ5yJ_.js"></script>
<script type="module" crossorigin src="/assets/index-CMU1LKW8.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-BsOofCBH.css">
</head>
@@ -190,6 +190,15 @@
</div>
<div class="config-group">
<div class="config-item">
<div class="config-label">
<span class="config-name">inotifyd 监听事件</span>
<span class="config-desc">默认 c。修改需重启服务</span>
</div>
<div class="config-value">
<input type="text" id="input-inotify-events" class="config-input" placeholder="c" />
</div>
</div>
<div class="config-item">
<div class="config-label">
<span class="config-name">当前亮度节点</span>
+10
View File
@@ -189,6 +189,16 @@
</div>
<div class="config-group">
<div class="config-item">
<div class="config-label">
<span class="config-name">inotifyd 监听事件</span>
<span class="config-desc">默认 c。修改需重启服务</span>
<button class="btn-text-s" id="btn-view-inotifyd-help" style="margin-top: 6px; padding: 2px 6px;">查看支持事件</button>
</div>
<div class="config-value">
<input type="text" id="input-inotify-events" class="config-input" placeholder="c" />
</div>
</div>
<div class="config-item">
<div class="config-label">
<span class="config-name">当前亮度节点</span>
+31 -8
View File
@@ -13,6 +13,7 @@ const DEFAULT_SYS_MAX_BRI_FILE = '/sys/class/backlight/panel0-backlight/max_brig
let NOW_BRI_FILE = DEFAULT_NOW_BRI_FILE;
let SYS_MAX_BRI_FILE = DEFAULT_SYS_MAX_BRI_FILE;
let INOTIFY_EVENTS = 'c';
// Web UI 配置
let statusLogRefreshInterval = 1000; // 状态/日志刷新间隔(默认1秒)
@@ -54,13 +55,19 @@ async function writeFile(path, content) {
// 1. 加载配置
async function loadConfig() {
// 首先加载设备路径配置
const [pathNowRes, pathMaxRes] = await Promise.all([
const [pathNowRes, pathMaxRes, eventsRes] = await Promise.all([
runCmd(`cat "${PATH_CONFIG_DIR}/now_bri_file.txt"`),
runCmd(`cat "${PATH_CONFIG_DIR}/max_bri_file.txt"`)
runCmd(`cat "${PATH_CONFIG_DIR}/max_bri_file.txt"`),
runCmd(`cat "${CONFIG_DIR}/inotify_events.txt"`)
]);
if (pathNowRes.errno === 0) NOW_BRI_FILE = pathNowRes.stdout.trim();
if (pathMaxRes.errno === 0) SYS_MAX_BRI_FILE = pathMaxRes.stdout.trim();
if (eventsRes.errno === 0 && eventsRes.stdout.trim()) INOTIFY_EVENTS = eventsRes.stdout.trim();
else INOTIFY_EVENTS = 'c';
document.getElementById('input-inotify-events').value = INOTIFY_EVENTS;
// 然后加载其他配置
const [uiRes, maxRes, sleepRes, autoRes, stepsRes, logSizeRes] = await Promise.all([
@@ -187,20 +194,23 @@ async function saveConfig() {
}
}
// 2.2 保存高级设置(设备路径)
// 2.2 保存高级设置(设备路径及事件监听)
async function saveAdvancedConfig() {
const nowBriFile = document.getElementById('input-now-bri-file').value || DEFAULT_NOW_BRI_FILE;
const sysMaxBriFile = document.getElementById('input-sys-max-bri-file').value || DEFAULT_SYS_MAX_BRI_FILE;
const eventsVal = document.getElementById('input-inotify-events').value || 'c';
showToast('保存中...');
// 保存设备路径配置
const pathChanged = (NOW_BRI_FILE !== nowBriFile) || (SYS_MAX_BRI_FILE !== sysMaxBriFile);
const configChanged = (NOW_BRI_FILE !== nowBriFile) || (SYS_MAX_BRI_FILE !== sysMaxBriFile) || (INOTIFY_EVENTS !== eventsVal);
await writeFile(`${PATH_CONFIG_DIR}/now_bri_file.txt`, nowBriFile);
await writeFile(`${PATH_CONFIG_DIR}/max_bri_file.txt`, sysMaxBriFile);
await writeFile(`${CONFIG_DIR}/inotify_events.txt`, eventsVal);
if (pathChanged) {
showToast('设备路径已修改,需要重启服务生效');
if (configChanged) {
INOTIFY_EVENTS = eventsVal;
showToast('高级设置已修改,需要重启服务生效');
} else {
showToast('设置已保存');
}
@@ -504,8 +514,6 @@ async function applyLogFilter() {
}
async function loadLogs() {
const logEl = document.getElementById('log-output');
const filterLevel = document.getElementById('log-level-filter').value;
// 读取最后 50 行日志
const res = await runCmd(`tail -n 50 "${LOG_FILE}"`);
@@ -715,6 +723,21 @@ async function init() {
btnClearLogs.addEventListener('click', handleClearLogs);
}
// 绑定查看支持事件按钮
const btnInotifyHelp = document.getElementById('btn-view-inotifyd-help');
if (btnInotifyHelp) {
btnInotifyHelp.addEventListener('click', async () => {
showToast('获取中...');
const res = await runCmd('inotifyd --help');
const output = (res.stderr || '') + (res.stdout || '');
if (output) {
alert(output.trim());
} else {
alert('无法获取该命令的帮助信息。');
}
});
}
// 定时休眠折叠逻辑
document.getElementById('input-sleep-mode').addEventListener('change', (e) => {
const expandArea = document.getElementById('sleep-time-expand');