fix for auto-start timer, added status to json-file

This commit is contained in:
oberon 2026-02-19 09:32:21 +01:00
parent f9804ecb00
commit 4a4f55fbe9

View File

@ -19,6 +19,7 @@ audio_lock = threading.Lock()
auto_random_lock = threading.Lock() auto_random_lock = threading.Lock()
auto_random_timer = None auto_random_timer = None
auto_random_params = {} auto_random_params = {}
AUTO_STATE_PATH = os.path.join(Config.LOG_DIR, 'auto_random_state.json')
# Sound-Cache: path -> pygame.mixer.Sound # Sound-Cache: path -> pygame.mixer.Sound
loaded_sounds = {} loaded_sounds = {}
@ -135,12 +136,35 @@ def _auto_random_tick():
auto_random_timer = threading.Timer(delay, _auto_random_tick) auto_random_timer = threading.Timer(delay, _auto_random_tick)
auto_random_timer.daemon = True auto_random_timer.daemon = True
auto_random_timer.start() auto_random_timer.start()
_persist_auto_state()
def rand_between(a, b): def rand_between(a, b):
return a + random.random() * (b - a) return a + random.random() * (b - a)
def _persist_auto_state(clear=False):
try:
if clear:
if os.path.exists(AUTO_STATE_PATH):
os.remove(AUTO_STATE_PATH)
return
data = {
"active": state.auto_random_active,
"next_ts": auto_random_params.get('next_ts'),
"imin": auto_random_params.get('imin'),
"imax": auto_random_params.get('imax'),
"dmin": auto_random_params.get('dmin'),
"dmax": auto_random_params.get('dmax'),
"theme": auto_random_params.get('theme')
}
os.makedirs(os.path.dirname(AUTO_STATE_PATH), exist_ok=True)
with open(AUTO_STATE_PATH, 'w', encoding='utf-8') as f:
json.dump(data, f)
except Exception as e:
logger.error(f"Persist auto-random state fehlgeschlagen: {e}")
def _prune_history(sound_id, now): def _prune_history(sound_id, now):
dq = random_history[sound_id] dq = random_history[sound_id]
while dq and now - dq[0] > WINDOW_SECONDS: while dq and now - dq[0] > WINDOW_SECONDS:
@ -440,10 +464,12 @@ def api_soundboard_auto_start():
auto_random_params['dmin'] = dmin auto_random_params['dmin'] = dmin
auto_random_params['dmax'] = dmax auto_random_params['dmax'] = dmax
auto_random_params['next_ts'] = time.time() + delay auto_random_params['next_ts'] = time.time() + delay
auto_random_params['theme'] = state.current_soundboard.get('filename')
global auto_random_timer global auto_random_timer
auto_random_timer = threading.Timer(delay, _auto_random_tick) auto_random_timer = threading.Timer(delay, _auto_random_tick)
auto_random_timer.daemon = True auto_random_timer.daemon = True
auto_random_timer.start() auto_random_timer.start()
_persist_auto_state()
logger.info(f"Auto-Random gestartet (delay {delay/60:.1f} min, intervall {imin}-{imax} min)") logger.info(f"Auto-Random gestartet (delay {delay/60:.1f} min, intervall {imin}-{imax} min)")
return jsonify({"success": True, "message": "Auto-Random gestartet", return jsonify({"success": True, "message": "Auto-Random gestartet",
"next_seconds": delay}) "next_seconds": delay})
@ -464,6 +490,8 @@ def api_soundboard_current():
@api_bp.route('/soundboard/status', methods=['GET']) @api_bp.route('/soundboard/status', methods=['GET'])
def api_soundboard_status(): def api_soundboard_status():
# Wiederherstellen, falls Prozess neu gestartet und State aus Datei verfügbar
_restore_auto_state()
with auto_random_lock: with auto_random_lock:
active = state.auto_random_active active = state.auto_random_active
next_ts = auto_random_params.get('next_ts') next_ts = auto_random_params.get('next_ts')
@ -494,9 +522,52 @@ def _stop_auto_random_internal():
stopped = True stopped = True
state.auto_random_active = False state.auto_random_active = False
auto_random_params.clear() auto_random_params.clear()
_persist_auto_state(clear=True)
return stopped return stopped
def _restore_auto_state():
try:
if not os.path.exists(AUTO_STATE_PATH):
return
with open(AUTO_STATE_PATH, 'r', encoding='utf-8') as f:
data = json.load(f)
if not data.get('active'):
return
next_ts = data.get('next_ts')
if not next_ts or next_ts < time.time():
return # abgelaufen
theme = data.get('theme')
# Theme laden, falls nicht gesetzt
if theme and (state.current_soundboard is None or state.current_soundboard.get('filename') != theme):
try:
sb = load_soundboard_config(theme,
current_app.config['SOUNDBOARD_CONFIG_DIR'],
current_app.config['SOUNDS_DIR'])
state.current_soundboard = sb
except Exception:
return
with auto_random_lock:
if state.auto_random_active:
return
state.auto_random_active = True
auto_random_params.update({
'imin': data.get('imin', 5),
'imax': data.get('imax', 10),
'dmin': data.get('dmin', 3),
'dmax': data.get('dmax', 12),
'next_ts': next_ts,
'theme': theme
})
delay = max(1, next_ts - time.time())
global auto_random_timer
auto_random_timer = threading.Timer(delay, _auto_random_tick)
auto_random_timer.daemon = True
auto_random_timer.start()
except Exception as e:
logger.error(f"Restore auto-random state fehlgeschlagen: {e}")
@api_bp.route('/stop_sound', methods=['POST']) @api_bp.route('/stop_sound', methods=['POST'])
def api_stop_sound(): def api_stop_sound():
try: try: