Fallback-JSON wenn corsairctl fehlschlägt oder Cache leer ist. flock -w 5 statt -n damit der zweite Prozess wartet statt sofort aufzugeben. Atomares mv statt direktem Schreiben in Cache.
30 lines
873 B
Bash
Executable File
30 lines
873 B
Bash
Executable File
#!/bin/bash
|
|
# ABOUTME: Waybar-Wrapper für corsairctl — cached JSON-Output.
|
|
# ABOUTME: Verhindert dass mehrere Waybar-Bars gleichzeitig das HID-Gerät ansprechen.
|
|
|
|
CACHE="/tmp/corsairctl-waybar.json"
|
|
LOCK="/tmp/corsairctl-waybar.lock"
|
|
MAX_AGE=10
|
|
FALLBACK='{"text":" ?","tooltip":"HS80: nicht erreichbar","class":"offline","percentage":0}'
|
|
|
|
# Cache frisch genug? Direkt ausgeben.
|
|
if [[ -f "$CACHE" ]] && [[ -s "$CACHE" ]]; then
|
|
age=$(( $(date +%s) - $(stat -c %Y "$CACHE") ))
|
|
if (( age < MAX_AGE )); then
|
|
cat "$CACHE"
|
|
exit 0
|
|
fi
|
|
fi
|
|
|
|
# Lock holen, abfragen, cachen.
|
|
(
|
|
flock -w 5 9 || { cat "$CACHE" 2>/dev/null || echo "$FALLBACK"; exit 0; }
|
|
if corsairctl json > "${CACHE}.tmp" 2>/dev/null; then
|
|
mv "${CACHE}.tmp" "$CACHE"
|
|
cat "$CACHE"
|
|
else
|
|
rm -f "${CACHE}.tmp"
|
|
echo "$FALLBACK"
|
|
fi
|
|
) 9>"$LOCK"
|