Move configuration to config.py

This commit is contained in:
2026-06-09 09:20:49 +02:00
parent 9002cbbe08
commit 0c6e94ef9f
3 changed files with 29 additions and 18 deletions

13
config.py Normal file
View File

@ -0,0 +1,13 @@
"""Application configuration."""
# ── Budget ────────────────────────────────────────────────────────────
BUDGET_S = 3600 # default 60 min in seconds
# ── Timer ─────────────────────────────────────────────────────────────
TICK_INTERVAL = 10 # seconds between budget check cycles
# ── Allowed device hours (curfew) ─────────────────────────────────────
ALLOWED_WEEKDAY_START = 15 # Mon-Fri start hour
ALLOWED_WEEKDAY_END = 20 # Mon-Fri end hour
ALLOWED_WEEKEND_START = 8 # Sat-Sun start hour
ALLOWED_END_MINUTE = 30 # End minute (all days)

View File

@ -1,23 +1,20 @@
"""Device management: check status, power off, and budget tracking."""
import json
import os
import subprocess
import threading
from datetime import datetime, timedelta
from pathlib import Path
# ── Config ────────────────────────────────────────────────────────────
import config
BUDGET_SECONDS = int(os.environ.get("BUDGET_MINUTES", "60")) * 60 # default 60 min in seconds
BUDGET_S = config.BUDGET_S
STATE_FILE = Path(__file__).parent / ".device_state.json"
TICK_INTERVAL = 10 # seconds between budget checks
# Allowed device hours (24h format)
ALLOWED_WEEKDAY_START = int(os.environ.get("ALLOWED_WEEKDAY_START", "15")) # Mon-Fri start hour
ALLOWED_WEEKDAY_END = int(os.environ.get("ALLOWED_WEEKDAY_END", "20")) # Mon-Fri end hour
ALLOWED_WEEKEND_START = int(os.environ.get("ALLOWED_WEEKEND_START", "8")) # Sat-Sun start hour
ALLOWED_END_MINUTE = int(os.environ.get("ALLOWED_END_MINUTE", "30")) # End minute (same for all days)
TICK_INTERVAL = config.TICK_INTERVAL
ALLOWED_WEEKDAY_START = config.ALLOWED_WEEKDAY_START
ALLOWED_WEEKDAY_END = config.ALLOWED_WEEKDAY_END
ALLOWED_WEEKEND_START = config.ALLOWED_WEEKEND_START
ALLOWED_END_MINUTE = config.ALLOWED_END_MINUTE
# ── Budget state (persisted to disk) ─────────────────────────────────
@ -148,20 +145,20 @@ DEVICES = {
def _get_budget(device: str) -> int:
"""Get remaining budget in seconds."""
state = _load_state()
return state.get(device, {}).get("budget", BUDGET_SECONDS)
return state.get(device, {}).get("budget", BUDGET_S)
def _set_budget(device: str, budget_seconds: int):
state = _load_state()
if device not in state:
state[device] = {"budget": BUDGET_SECONDS}
state[device] = {"budget": BUDGET_S}
state[device]["budget"] = budget_seconds
state[device]["last_online"] = datetime.now().isoformat()
_save_state(state)
def _reset_budget(device: str):
_set_budget(device, BUDGET_SECONDS)
_set_budget(device, BUDGET_S)
def _budget_to_minutes(budget_seconds: int) -> int:
@ -280,7 +277,7 @@ def _budget_tick():
with _timer_lock:
for dev_id, dev_info in DEVICES.items():
state = _load_state()
current_budget = state.get(dev_id, {}).get("budget", BUDGET_SECONDS)
current_budget = state.get(dev_id, {}).get("budget", BUDGET_S)
is_online = dev_info["check"]()
if isinstance(is_online, dict):
@ -351,7 +348,7 @@ def status_all() -> list[dict]:
actions = []
for dev_id, dev_info in DEVICES.items():
state = _load_state()
budget_seconds = state.get(dev_id, {}).get("budget", BUDGET_SECONDS)
budget_seconds = state.get(dev_id, {}).get("budget", BUDGET_S)
is_online = dev_info["check"]()
if isinstance(is_online, dict):

View File

@ -4,7 +4,8 @@
import json
from http.server import HTTPServer, BaseHTTPRequestHandler
from devices import shutdown_all, status_all, curfew_status, _set_budget, _get_budget, BUDGET_SECONDS, TICK_INTERVAL
import config
from devices import shutdown_all, status_all, curfew_status, _set_budget, _get_budget
class Handler(BaseHTTPRequestHandler):
@ -47,7 +48,7 @@ class Handler(BaseHTTPRequestHandler):
dev_id = data.get("device", "")
delta = int(data.get("delta", 0))
current = _get_budget(dev_id)
new_budget = max(0, min(BUDGET_SECONDS, current + delta))
new_budget = max(0, min(config.BUDGET_S, current + delta))
_set_budget(dev_id, new_budget)
self.send_response(200)
self.send_header("Content-Type", "application/json")
@ -242,7 +243,7 @@ if __name__ == "__main__":
start_timer()
server = HTTPServer(("0.0.0.0", port), Handler)
print(f"🚀 Open http://localhost:{port}")
print(f"⏱️ Budget timer running (checks every {TICK_INTERVAL}s, reset at 7:00 AM)")
print(f"⏱️ Budget timer running (checks every {config.TICK_INTERVAL}s, reset at 7:00 AM)")
try:
server.serve_forever()
except KeyboardInterrupt: