Fix publish_orion: PATCH sends attrs only (not id/type/@context)
- PATCH /entities/{eid}/attrs now sends only attributes
- This allows updating entities with new fields (source, mqttTopic)
This commit is contained in:
10
simulator.py
10
simulator.py
@@ -519,7 +519,7 @@ def publish_orion(sid: str, sensor: dict) -> bool:
|
||||
try:
|
||||
body = json.dumps(entity).encode()
|
||||
req = urllib.request.Request(f"{base}/entities", data=body,
|
||||
headers={"Content-Type": "application/ld+json", "Accept": "application/ld+json"}, method="POST")
|
||||
headers={"Content-Type": "application/ld+json", "Accept": "application/ld+json"}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=8) as resp:
|
||||
print(f" 🌐 Orion-LD: ✅ (HTTP {resp.status} created)")
|
||||
return True
|
||||
@@ -527,17 +527,19 @@ def publish_orion(sid: str, sensor: dict) -> bool:
|
||||
if e.code != 409:
|
||||
print(f" ⚠️ Orion-LD → {e.code}: {e.read().decode()[:200]}")
|
||||
return False
|
||||
# 2. Déjà existant (409) → PATCH sur les attributs
|
||||
# 2. Déjà existant (409) → PATCH sur les attributs (sans id/type/@context)
|
||||
try:
|
||||
# PATCH ne doit contenir que les attributs (pas id/type/@context)
|
||||
attrs = {k: v for k, v in entity.items() if k not in ("id", "type", "@context")}
|
||||
eid = urllib.parse.quote(entity['id'], safe='')
|
||||
patch_url = f"{base}/entities/{eid}/attrs"
|
||||
req2 = urllib.request.Request(patch_url, data=body,
|
||||
req2 = urllib.request.Request(patch_url, data=json.dumps(attrs).encode(),
|
||||
headers={"Content-Type": "application/ld+json", "Accept": "application/ld+json"}, method="PATCH")
|
||||
with urllib.request.urlopen(req2, timeout=8) as resp2:
|
||||
print(f" 🌐 Orion-LD: ✅ (HTTP {resp2.status} updated)")
|
||||
return True
|
||||
except Exception as e2:
|
||||
print(f" ⚠️ Orion-LD → update failed: {e2}")
|
||||
print(f" ⚠️ Orion-LD PATCH failed: {e2}")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f" ⚠️ Orion-LD → {e}")
|
||||
|
||||
Reference in New Issue
Block a user