Move configuration to config.py
This commit is contained in:
27
devices.py
27
devices.py
@ -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):
|
||||
|
||||
Reference in New Issue
Block a user