AnotterKiosk/kiosk_skeleton/usr/bin/schedule-screen-timers

87 lines
1.7 KiB
Plaintext
Raw Normal View History

#!/bin/bash
INI_FILE="/boot/kioskbrowser.ini"
SYSTEMD_DIR="/etc/systemd/system"
2025-06-04 12:59:05 +01:00
# Function to safely remount root FS
remount_root() {
local mode=$1
echo "Remounting root filesystem as $mode..."
mount -o remount,"$mode" / || {
echo "Failed to remount root as $mode"
exit 1
}
}
get_ini_value() {
2025-07-18 10:10:48 +01:00
get-ini "$INI_FILE" "$1" "$2"
}
create_recurring_timer() {
local action=$1
local time=$2
2025-06-04 12:59:05 +01:00
local value
if [[ "$action" == "on" ]]; then
value=1
elif [[ "$action" == "off" ]]; then
value=0
else
echo "Invalid action: $action"
return 1
fi
local name="screen-${action}"
echo "Setting daily screen ${action} at ${time}"
cat <<EOF > "$SYSTEMD_DIR/${name}.timer"
[Unit]
Description=Daily screen ${action} timer
[Timer]
OnCalendar=*-*-* ${time}:00
Persistent=true
[Install]
WantedBy=timers.target
EOF
cat <<EOF > "$SYSTEMD_DIR/${name}.service"
[Unit]
Description=Turn screen ${action}
[Service]
Type=oneshot
2025-06-04 12:59:05 +01:00
ExecStart=/usr/bin/vcgencmd display_power ${value}
2025-05-31 11:57:55 +01:00
User=pi
EOF
systemctl daemon-reload
systemctl enable --now "${name}.timer"
}
2025-06-04 12:59:05 +01:00
cleanup_screen_timers() {
for action in on off; do
systemctl disable --now screen-${action}.timer 2>/dev/null
rm -f "$SYSTEMD_DIR/screen-${action}.timer" "$SYSTEMD_DIR/screen-${action}.service"
done
systemctl daemon-reload
}
2025-06-04 12:59:05 +01:00
remount_root rw
# === MAIN ===
SCREEN_ON=$(get_ini_value screen screen_on_time)
SCREEN_OFF=$(get_ini_value screen screen_off_time)
cleanup_screen_timers
[[ "$SCREEN_ON" =~ ^[0-2][0-9]:[0-5][0-9]$ ]] && create_recurring_timer on "$SCREEN_ON"
[[ "$SCREEN_OFF" =~ ^[0-2][0-9]:[0-5][0-9]$ ]] && create_recurring_timer off "$SCREEN_OFF"
2025-06-04 12:59:05 +01:00
remount_root ro