on_mqtt_register : mise à jour automatique du registre + distinction NOUVEAU/EN LIGNE

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-07 17:08:22 +00:00
parent dbaedc0799
commit dbda0787a2
2 changed files with 34 additions and 15 deletions
+21 -2
View File
@@ -134,7 +134,7 @@ def on_mqtt_notification(client, userdata, msg):
print("[MQTT] Erreur parsing notification scheduler : {}".format(e)) print("[MQTT] Erreur parsing notification scheduler : {}".format(e))
def on_mqtt_register(client, userdata, msg): def on_mqtt_register(client, userdata, msg):
"""Reçoit les déclarations de mise en ligne des agents.""" """Reçoit les déclarations de mise en ligne des agents et met à jour le registre."""
try: try:
data = json.loads(msg.payload.decode(errors="replace")) data = json.loads(msg.payload.decode(errors="replace"))
agent = data.get("agent", "?") agent = data.get("agent", "?")
@@ -142,9 +142,28 @@ def on_mqtt_register(client, userdata, msg):
mqtt_inbox = data.get("mqtt_inbox", "?") mqtt_inbox = data.get("mqtt_inbox", "?")
speciality = data.get("speciality", "") speciality = data.get("speciality", "")
print("[REGISTER] {} en ligne (JID: {}, inbox: {})".format(agent, jid, mqtt_inbox)) print("[REGISTER] {} en ligne (JID: {}, inbox: {})".format(agent, jid, mqtt_inbox))
# Mettre à jour agents_registry.json
registry_file = CONFIG_DIR / "agents_registry.json"
try:
registry = json.loads(registry_file.read_text(encoding="utf-8"))
except Exception:
registry = {}
is_new = agent not in registry
registry[agent] = {
"jid" : jid,
"mqtt_inbox" : mqtt_inbox,
"mqtt_outbox": "agents/agent1/inbox",
"speciality" : speciality,
}
registry_file.write_text(json.dumps(registry, indent=2, ensure_ascii=False), encoding="utf-8")
if is_new:
print("[REGISTER] {} ajouté au registre.".format(agent))
# Notifier sylvain via XMPP # Notifier sylvain via XMPP
if xmpp_bot: if xmpp_bot:
notif = "[EN LIGNE] {}\n JID : {}\n MQTT : {}".format(agent, jid, mqtt_inbox) status = "NOUVEAU" if is_new else "EN LIGNE"
notif = "[{}] {}\n JID : {}\n MQTT : {}".format(status, agent, jid, mqtt_inbox)
if speciality: if speciality:
notif += "\n Rôle : {}".format(speciality) notif += "\n Rôle : {}".format(speciality)
xmpp_bot.send_message(mto=ADMIN_JID, mbody=notif, mtype='chat') xmpp_bot.send_message(mto=ADMIN_JID, mbody=notif, mtype='chat')