Simplify client modes and add VAD retention policy
Some checks failed
Build and Push EVS Bridge Image / docker (push) Has been cancelled
Some checks failed
Build and Push EVS Bridge Image / docker (push) Has been cancelled
This commit is contained in:
@@ -53,7 +53,8 @@ WAV_HEADER_BYTES = 44
|
||||
|
||||
VAD_ENABLED = getenv_bool("VAD_ENABLED", True)
|
||||
VAD_DIR = Path(os.getenv("VAD_DIR", "/data/vad"))
|
||||
VAD_KEEP_FILES = int(os.getenv("VAD_KEEP_FILES", "100"))
|
||||
VAD_KEEP_FILES = int(os.getenv("VAD_KEEP_FILES", "200"))
|
||||
VAD_MAX_AGE_DAYS = int(os.getenv("VAD_MAX_AGE_DAYS", "7"))
|
||||
VAD_PREROLL_MS = int(os.getenv("VAD_PREROLL_MS", "1000"))
|
||||
VAD_POSTROLL_MS = int(os.getenv("VAD_POSTROLL_MS", "1000"))
|
||||
VAD_START_THRESHOLD = int(os.getenv("VAD_START_THRESHOLD", "900"))
|
||||
@@ -136,17 +137,34 @@ async def call_ha_webhook(event: str, payload: dict) -> None:
|
||||
log.exception("ha webhook call failed")
|
||||
|
||||
|
||||
def enforce_wav_retention(directory: Path, keep_files: int) -> None:
|
||||
if keep_files <= 0:
|
||||
def enforce_wav_retention(directory: Path, keep_files: int, max_age_days: int = 0) -> None:
|
||||
if keep_files <= 0 and max_age_days <= 0:
|
||||
return
|
||||
try:
|
||||
directory.mkdir(parents=True, exist_ok=True)
|
||||
wavs = sorted(
|
||||
[p for p in directory.glob("*.wav") if p.is_file()],
|
||||
key=lambda p: p.stat().st_mtime,
|
||||
)
|
||||
while len(wavs) > keep_files:
|
||||
oldest = wavs.pop(0)
|
||||
wavs = []
|
||||
now = time.time()
|
||||
max_age_seconds = max_age_days * 86400
|
||||
for p in directory.glob("*.wav"):
|
||||
if not p.is_file():
|
||||
continue
|
||||
try:
|
||||
st = p.stat()
|
||||
except Exception:
|
||||
continue
|
||||
if max_age_seconds > 0 and (now - st.st_mtime) > max_age_seconds:
|
||||
try:
|
||||
p.unlink()
|
||||
log.info("deleted old wav by age: %s", p)
|
||||
except Exception:
|
||||
log.exception("failed to delete old wav by age: %s", p)
|
||||
continue
|
||||
wavs.append((p, st.st_mtime))
|
||||
|
||||
wavs.sort(key=lambda x: x[1])
|
||||
files = [p for (p, _) in wavs]
|
||||
while keep_files > 0 and len(files) > keep_files:
|
||||
oldest = files.pop(0)
|
||||
try:
|
||||
oldest.unlink()
|
||||
log.info("deleted old wav: %s", oldest)
|
||||
@@ -198,7 +216,7 @@ def write_vad_wav_segment(session: DeviceSession, pcm: bytes) -> Optional[str]:
|
||||
wf.writeframes(pcm)
|
||||
wf.close()
|
||||
session.vad_segment_index += 1
|
||||
enforce_wav_retention(VAD_DIR, VAD_KEEP_FILES)
|
||||
enforce_wav_retention(VAD_DIR, VAD_KEEP_FILES, VAD_MAX_AGE_DAYS)
|
||||
return str(path)
|
||||
except Exception:
|
||||
log.exception("failed to write vad wav segment for %s", session.device_id)
|
||||
|
||||
Reference in New Issue
Block a user