#!/usr/bin/env python3
"""Configure BasicAuthPassword for all 15 Cariflex charging stations"""
import json, time, urllib.request, urllib.error

CITRINEOS_URL = "http://localhost:8081"
PASSWORD = "DEADBEEFDEADBEEF"

for i in range(1, 16):
    cp_id = f"CP{i:03d}"
    url = f"{CITRINEOS_URL}/data/monitoring/variableAttribute?stationId={cp_id}&setOnCharger=true"
    payload = json.dumps({
        "component": {"name": "SecurityCtrlr"},
        "variable": {"name": "BasicAuthPassword"},
        "variableAttribute": [{"value": PASSWORD}],
        "variableCharacteristics": {"dataType": "passwordString", "supportsMonitoring": False}
    }).encode()
    
    req = urllib.request.Request(url, data=payload, method='PUT',
                                  headers={'Content-Type': 'application/json'})
    try:
        resp = urllib.request.urlopen(req, timeout=10)
        print(f"OK {cp_id}: HTTP {resp.status}")
    except urllib.error.HTTPError as e:
        print(f"FAIL {cp_id}: HTTP {e.code}")
    except Exception as e:
        print(f"FAIL {cp_id}: {e}")
    time.sleep(0.2)

print("Done")
