Files
LuminPro/webui/src/composables/useConfig.js
T
2026-04-10 01:59:23 +08:00

224 lines
6.8 KiB
JavaScript

import { ref } from 'vue'
import {
runCmd,
readConfig,
writeConfig,
updateConfig,
CONFIG_FILE,
BACKUP_CONFIG_FILE,
PID_FILE,
FLAG_FILE,
DEFAULT_NOW_BRI_FILE,
DEFAULT_SYS_MAX_BRI_FILE,
} from '../utils.js'
export function useConfig() {
// 亮度配置
const uiMaxBri = ref('')
const maxBri = ref('')
const stepsNum = ref('50')
const logMaxSize = ref('500')
// 执行策略
const autoBriSleep = ref(false)
const displayHdrSleep = ref(false)
const sleepMode = ref(false)
const sleepStartH = ref('19')
const sleepStartM = ref('00')
const sleepEndH = ref('06')
const sleepEndM = ref('00')
// 高级设置
const nowBriFile = ref(DEFAULT_NOW_BRI_FILE)
const sysMaxBriFile = ref(DEFAULT_SYS_MAX_BRI_FILE)
const inotifyEvents = ref('c')
// Web UI 配置 (localStorage 持久化)
const autoRefresh = ref(localStorage.getItem('autoRefresh') !== 'false')
const statusRefreshInterval = ref(
Math.max(100, parseInt(localStorage.getItem('statusLogRefreshInterval') || '1000', 10)),
)
const uiZoom = ref(
Math.min(150, Math.max(50, parseInt(localStorage.getItem('uiZoom') || '100', 10))),
)
const themeMode = ref(localStorage.getItem('themeMode') || 'system')
async function load() {
const cfg = await readConfig()
if (!cfg || Object.keys(cfg).length === 0) return
uiMaxBri.value = cfg.ui_max_bri != null ? String(cfg.ui_max_bri) : ''
maxBri.value = cfg.max_bri != null ? String(cfg.max_bri) : ''
stepsNum.value = cfg.steps_num != null ? String(cfg.steps_num) : '50'
logMaxSize.value = cfg.log_max_size != null ? String(cfg.log_max_size) : '500'
autoBriSleep.value = cfg.auto_bri_sleep === 1
displayHdrSleep.value = cfg.display_hdr_sleep === 1
nowBriFile.value = cfg.now_bri_file || DEFAULT_NOW_BRI_FILE
sysMaxBriFile.value = cfg.max_bri_file || DEFAULT_SYS_MAX_BRI_FILE
inotifyEvents.value = cfg.inotify_events || 'c'
const st = cfg.sleep_time || ''
if (st && st.includes('-')) {
sleepMode.value = true
const [s, e] = st.split('-')
if (s.length === 4) {
sleepStartH.value = s.slice(0, 2)
sleepStartM.value = s.slice(2)
}
if (e.length === 4) {
sleepEndH.value = e.slice(0, 2)
sleepEndM.value = e.slice(2)
}
} else {
sleepMode.value = false
}
}
function getSleepTimeStr() {
if (!sleepMode.value) return ''
const pad = (v) => String(v).padStart(2, '0')
return `${pad(sleepStartH.value)}${pad(sleepStartM.value)}-${pad(sleepEndH.value)}${pad(sleepEndM.value)}`
}
async function save(toast) {
if (!uiMaxBri.value || !maxBri.value) {
toast('亮度値不能为空')
return
}
toast('保存中...')
// 读取当前配置(保留 blacklist_apps 等其他字段)
const current = await readConfig()
await writeConfig({
...current,
ui_max_bri: parseInt(uiMaxBri.value) || 0,
max_bri: parseInt(maxBri.value) || 0,
steps_num: parseInt(stepsNum.value) || 50,
log_max_size: parseInt(logMaxSize.value) || 500,
auto_bri_sleep: autoBriSleep.value ? 1 : 0,
display_hdr_sleep: displayHdrSleep.value ? 1 : 0,
sleep_time: getSleepTimeStr(),
})
await runCmd(`rm -f "${FLAG_FILE}"`)
const pidRes = await runCmd(`cat "${PID_FILE}"`)
toast(
pidRes.errno === 0 && pidRes.stdout.trim()
? '配置已保存 (下次调整时生效)'
: '配置已保存 (服务未运行)',
)
}
async function saveAdvanced(toast, onPathsChanged) {
toast('保存中...')
await updateConfig({
now_bri_file: nowBriFile.value || DEFAULT_NOW_BRI_FILE,
max_bri_file: sysMaxBriFile.value || DEFAULT_SYS_MAX_BRI_FILE,
inotify_events: inotifyEvents.value || 'c',
})
onPathsChanged?.()
toast('高级设置已保存,需重启服务生效')
}
async function resetToDefaults(toast) {
toast('正在恢复默认配置...')
const backupRes = await runCmd(`cat "${BACKUP_CONFIG_FILE}"`)
if (backupRes.errno !== 0 || !backupRes.stdout.trim()) {
toast('备份文件不存在,无法恢复')
return
}
let backup
try {
backup = JSON.parse(backupRes.stdout)
} catch {
toast('备份文件损坏')
return
}
uiMaxBri.value = backup.ui_max_bri != null ? String(backup.ui_max_bri) : ''
maxBri.value = backup.max_bri != null ? String(backup.max_bri) : ''
autoBriSleep.value = backup.auto_bri_sleep === 1
displayHdrSleep.value = backup.display_hdr_sleep === 1
stepsNum.value = backup.steps_num != null ? String(backup.steps_num) : '50'
logMaxSize.value = backup.log_max_size != null ? String(backup.log_max_size) : '500'
nowBriFile.value = backup.now_bri_file || DEFAULT_NOW_BRI_FILE
sysMaxBriFile.value = backup.max_bri_file || DEFAULT_SYS_MAX_BRI_FILE
inotifyEvents.value = backup.inotify_events || 'c'
const st = backup.sleep_time || ''
if (st && st.includes('-')) {
sleepMode.value = true
const [s, e] = st.split('-')
if (s.length === 4) {
sleepStartH.value = s.slice(0, 2)
sleepStartM.value = s.slice(2)
}
if (e.length === 4) {
sleepEndH.value = e.slice(0, 2)
sleepEndM.value = e.slice(2)
}
} else {
sleepMode.value = true
sleepStartH.value = '19'
sleepStartM.value = '00'
sleepEndH.value = '06'
sleepEndM.value = '00'
}
await save(toast)
}
function saveWebUIConfig(toast, newAutoRefresh, newInterval, newZoom, newTheme, onRestart) {
const interval = Math.max(100, parseInt(newInterval, 10) || 1000)
const zoom = Math.min(150, Math.max(50, parseInt(newZoom, 10) || 100))
localStorage.setItem('autoRefresh', newAutoRefresh)
localStorage.setItem('statusLogRefreshInterval', interval)
localStorage.setItem('uiZoom', zoom)
localStorage.setItem('themeMode', newTheme)
autoRefresh.value = newAutoRefresh
statusRefreshInterval.value = interval
uiZoom.value = zoom
themeMode.value = newTheme
document.documentElement.style.zoom = zoom / 100
applyTheme(newTheme)
onRestart?.(newAutoRefresh, interval)
toast('Web UI 配置已保存')
}
function applyZoom() {
document.documentElement.style.zoom = uiZoom.value / 100
}
function applyTheme(mode) {
const html = document.documentElement
html.classList.remove('light', 'dark')
if (mode === 'light') html.classList.add('light')
else if (mode === 'dark') html.classList.add('dark')
}
return {
uiMaxBri,
maxBri,
stepsNum,
logMaxSize,
autoBriSleep,
displayHdrSleep,
sleepMode,
sleepStartH,
sleepStartM,
sleepEndH,
sleepEndM,
nowBriFile,
sysMaxBriFile,
inotifyEvents,
autoRefresh,
statusRefreshInterval,
uiZoom,
themeMode,
load,
save,
saveAdvanced,
resetToDefaults,
saveWebUIConfig,
applyZoom,
applyTheme,
}
}