Fix Orion-LD: Add source to @context + PATCH with full payload

- ORION_CONTEXT now includes source definition (uri.fiware.org)
- PATCH /entities/{id}/attrs now sends full entity (with @context)
- Orion-LD requires @context even in PATCH requests
- This fixes 400 Bad Request errors on update
This commit is contained in:
Eric FELIXINE
2026-05-04 23:12:56 -04:00
parent b2ba6f8202
commit 1ed03b5a57

View File

@@ -168,6 +168,7 @@ for stype, locs in SENSOR_LOCATIONS.items():
# Orion-LD ne peut pas résoudre raw.githubusercontent.com — utiliser uri.etsi.org uniquement # Orion-LD ne peut pas résoudre raw.githubusercontent.com — utiliser uri.etsi.org uniquement
ORION_CONTEXT = [ ORION_CONTEXT = [
"https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context.jsonld", "https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context.jsonld",
{"source": "https://uri.fiware.org/ngsi-ld/default-context/source"}
] ]
# Mapping sensor type → Smart Data Model type NGSI-LD # Mapping sensor type → Smart Data Model type NGSI-LD
@@ -527,13 +528,12 @@ def publish_orion(sid: str, sensor: dict) -> bool:
if e.code != 409: if e.code != 409:
print(f" ⚠️ Orion-LD → {e.code}: {e.read().decode()[:200]}") print(f" ⚠️ Orion-LD → {e.code}: {e.read().decode()[:200]}")
return False return False
# 2. Déjà existant (409) → PATCH sur les attributs (sans id/type/@context) # 2. Déjà existant (409) → PATCH sur les attributs (avec @context complet requis par Orion-LD)
try: try:
# PATCH ne doit contenir que les attributs (pas id/type/@context) # Orion-LD exige @context même dans le PATCH
attrs = {k: v for k, v in entity.items() if k not in ("id", "type", "@context")}
eid = urllib.parse.quote(entity['id'], safe='') eid = urllib.parse.quote(entity['id'], safe='')
patch_url = f"{base}/entities/{eid}/attrs" patch_url = f"{base}/entities/{eid}/attrs"
req2 = urllib.request.Request(patch_url, data=json.dumps(attrs).encode(), req2 = urllib.request.Request(patch_url, data=body,
headers={"Content-Type": "application/ld+json", "Accept": "application/ld+json"}, method="PATCH") headers={"Content-Type": "application/ld+json", "Accept": "application/ld+json"}, method="PATCH")
with urllib.request.urlopen(req2, timeout=8) as resp2: with urllib.request.urlopen(req2, timeout=8) as resp2:
print(f" 🌐 Orion-LD: ✅ (HTTP {resp2.status} updated)") print(f" 🌐 Orion-LD: ✅ (HTTP {resp2.status} updated)")