Files

30 lines
806 B
Python

"""Speaker switch state, shared with sdr-speaker.service through a small json file."""
import json
import os
import time
from radio.config import SPEAKER_STATE
def speaker_state():
try:
with open(SPEAKER_STATE, encoding="utf-8") as fh:
st = json.load(fh)
except (OSError, ValueError):
st = {}
return {"on": bool(st.get("on")), "volume": int(st.get("volume", 70))}
def speaker_set(on=None, volume=None):
st = speaker_state()
if on is not None:
st["on"] = bool(on)
if volume is not None:
st["volume"] = max(0, min(100, int(volume)))
st["changed"] = time.time()
tmp = SPEAKER_STATE + ".tmp"
with open(tmp, "w", encoding="utf-8") as fh:
json.dump(st, fh)
os.replace(tmp, SPEAKER_STATE)
return speaker_state()