feat: add initial device registry
This commit is contained in:
parent
9fca3675b7
commit
de51278d69
2
.gitignore
vendored
2
.gitignore
vendored
@ -2,3 +2,5 @@
|
|||||||
__pycache__/
|
__pycache__/
|
||||||
*.pyc
|
*.pyc
|
||||||
activation.log
|
activation.log
|
||||||
|
|
||||||
|
data/devices/*.json
|
||||||
|
|||||||
50
app.py
50
app.py
@ -15,6 +15,7 @@ app = FastAPI(title="Provisioning Activation Server", version="0.1.0")
|
|||||||
|
|
||||||
class ActivationRequest(BaseModel):
|
class ActivationRequest(BaseModel):
|
||||||
activation_code: str
|
activation_code: str
|
||||||
|
device_fingerprint: str
|
||||||
hostname: str | None = None
|
hostname: str | None = None
|
||||||
machine_id: str | None = None
|
machine_id: str | None = None
|
||||||
client_version: str | None = None
|
client_version: str | None = None
|
||||||
@ -29,6 +30,43 @@ def write_log(entry: dict) -> None:
|
|||||||
with LOG_PATH.open("a", encoding="utf-8") as f:
|
with LOG_PATH.open("a", encoding="utf-8") as f:
|
||||||
f.write(json.dumps(entry, ensure_ascii=False) + "\n")
|
f.write(json.dumps(entry, ensure_ascii=False) + "\n")
|
||||||
|
|
||||||
|
DEVICES_DIR = Path("data/devices")
|
||||||
|
|
||||||
|
def load_or_create_device(device_fingerprint: str) -> tuple[dict, bool]:
|
||||||
|
"""
|
||||||
|
Lädt ein bekanntes Gerät oder legt es neu an.
|
||||||
|
|
||||||
|
Rückgabe:
|
||||||
|
(device, created)
|
||||||
|
"""
|
||||||
|
|
||||||
|
DEVICES_DIR.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
device_file = DEVICES_DIR / f"{device_fingerprint}.json"
|
||||||
|
|
||||||
|
if device_file.exists():
|
||||||
|
with device_file.open("r", encoding="utf-8") as f:
|
||||||
|
device = json.load(f)
|
||||||
|
|
||||||
|
device["last_seen"] = datetime.now(timezone.utc).isoformat()
|
||||||
|
device["activation_count"] = device.get("activation_count", 0) + 1
|
||||||
|
|
||||||
|
with device_file.open("w", encoding="utf-8") as f:
|
||||||
|
json.dump(device, f, indent=2)
|
||||||
|
|
||||||
|
return device, False
|
||||||
|
|
||||||
|
device = {
|
||||||
|
"device_fingerprint": device_fingerprint,
|
||||||
|
"created_at": datetime.now(timezone.utc).isoformat(),
|
||||||
|
"last_seen": datetime.now(timezone.utc).isoformat(),
|
||||||
|
"activation_count": 1
|
||||||
|
}
|
||||||
|
|
||||||
|
with device_file.open("w", encoding="utf-8") as f:
|
||||||
|
json.dump(device, f, indent=2)
|
||||||
|
|
||||||
|
return device, True
|
||||||
|
|
||||||
@app.get("/health")
|
@app.get("/health")
|
||||||
def health():
|
def health():
|
||||||
@ -45,6 +83,13 @@ def activate(payload: ActivationRequest, request: Request):
|
|||||||
activation_entry = config.get("activation_codes", {}).get(payload.activation_code)
|
activation_entry = config.get("activation_codes", {}).get(payload.activation_code)
|
||||||
|
|
||||||
success = activation_entry is not None
|
success = activation_entry is not None
|
||||||
|
device = None
|
||||||
|
device_created = False
|
||||||
|
|
||||||
|
if success:
|
||||||
|
device, device_created = load_or_create_device(
|
||||||
|
payload.device_fingerprint
|
||||||
|
)
|
||||||
|
|
||||||
write_log({
|
write_log({
|
||||||
"timestamp": datetime.now(timezone.utc).isoformat(),
|
"timestamp": datetime.now(timezone.utc).isoformat(),
|
||||||
@ -73,6 +118,11 @@ def activate(payload: ActivationRequest, request: Request):
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
"success": True,
|
"success": True,
|
||||||
|
|
||||||
|
"device": {
|
||||||
|
"fingerprint": device["device_fingerprint"],
|
||||||
|
},
|
||||||
|
|
||||||
"customer": activation_entry["customer"],
|
"customer": activation_entry["customer"],
|
||||||
"profiles": available_profiles
|
"profiles": available_profiles
|
||||||
}
|
}
|
||||||
|
|||||||
0
data/devices/.gitkeep
Normal file
0
data/devices/.gitkeep
Normal file
Loading…
x
Reference in New Issue
Block a user