"""RoboScope AI commissioning example. Python 3, standard library only.
Use actual device state. Never put a credential in source code or logs.
"""
import getpass
import json
import urllib.error
import urllib.request
from datetime import datetime, timezone

key = getpass.getpass("Device credential: ").strip()
state = input("Actual state [running/idle/maintenance/stopped/fault/unknown]: ").strip()
if state not in {"running", "idle", "maintenance", "stopped", "fault", "unknown"}:
    raise SystemExit("Invalid state")

body = {
    "state": state,
    "observed_at": datetime.now(timezone.utc).isoformat(),
    "payload": {"note": "Operator-observed commissioning report"}
}
request = urllib.request.Request(
    "https://www.thewpaa.org/api/roboscope/telemetry",
    data=json.dumps(body).encode("utf-8"),
    headers={"Authorization": "Bearer " + key, "Content-Type": "application/json"},
    method="POST"
)
try:
    with urllib.request.urlopen(request, timeout=20) as response:
        print(response.status, response.read().decode("utf-8"))
except urllib.error.HTTPError as error:
    print(error.code, error.read().decode("utf-8"))
except urllib.error.URLError:
    print("Network error: delivery is unconfirmed; check before retrying.")
