70 lines
1.5 KiB
Python
70 lines
1.5 KiB
Python
from __future__ import annotations
|
|
import json
|
|
import time
|
|
from pathlib import Path
|
|
from .queue import enqueue
|
|
|
|
AUTON_PATH = Path('/srv/prime-core/state/autonomous-mode.json')
|
|
GOALS_PATH = Path('/srv/prime-core/state/goals.json')
|
|
LAST_PATH = Path('/srv/prime-core/state/autonomy-last.json')
|
|
|
|
|
|
def is_enabled() -> bool:
|
|
if not AUTON_PATH.exists():
|
|
return False
|
|
try:
|
|
return bool(json.loads(AUTON_PATH.read_text()).get('enabled', False))
|
|
except Exception:
|
|
return False
|
|
|
|
|
|
def goals() -> list[dict]:
|
|
if not GOALS_PATH.exists():
|
|
return []
|
|
try:
|
|
return json.loads(GOALS_PATH.read_text())
|
|
except Exception:
|
|
return []
|
|
|
|
|
|
def last_map() -> dict:
|
|
if not LAST_PATH.exists():
|
|
return {}
|
|
try:
|
|
return json.loads(LAST_PATH.read_text())
|
|
except Exception:
|
|
return {}
|
|
|
|
|
|
def save_last(m: dict):
|
|
LAST_PATH.parent.mkdir(parents=True, exist_ok=True)
|
|
LAST_PATH.write_text(json.dumps(m, indent=2))
|
|
|
|
|
|
def tick():
|
|
if not is_enabled():
|
|
return
|
|
gs = goals()
|
|
if not gs:
|
|
return
|
|
lm = last_map()
|
|
now = int(time.time())
|
|
for g in gs:
|
|
gid = g.get('id')
|
|
prev = int(lm.get(gid, 0))
|
|
# seed one planning task per goal every 30m
|
|
if now - prev >= 1800:
|
|
enqueue('planning', f"Advance goal: {g.get('text','')}")
|
|
lm[gid] = now
|
|
save_last(lm)
|
|
|
|
|
|
def loop():
|
|
while True:
|
|
tick()
|
|
time.sleep(60)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
loop()
|