fix: Simulateur publie sur 3 brokers (emqx, mosquitto, bunkerm) avec préfixe json/
This commit is contained in:
23
simulator.py
23
simulator.py
@@ -666,25 +666,24 @@ class MultiMQTT:
|
|||||||
return results
|
return results
|
||||||
|
|
||||||
def publish_iot_agent(self, sid: str, payload: dict, sensor_type: str = "unknown") -> bool:
|
def publish_iot_agent(self, sid: str, payload: dict, sensor_type: str = "unknown") -> bool:
|
||||||
"""Publie sur le topic IoT-Agent (smartcity-api-key/{sid}/attrs) via EMQX."""
|
"""Publie sur le topic IoT-Agent (json/smartcity-api-key/{sid}/attrs) via les 3 brokers."""
|
||||||
topic = f"smartcity-api-key/{sid}/attrs"
|
topic = f"json/smartcity-api-key/{sid}/attrs"
|
||||||
msg = json.dumps(payload, ensure_ascii=False)
|
msg = json.dumps(payload, ensure_ascii=False)
|
||||||
payload_bytes = len(msg.encode())
|
payload_bytes = len(msg.encode())
|
||||||
# Utiliser le client EMQX (présuppose que 'emqx' existe dans self.clients)
|
|
||||||
if 'emqx' in self.clients and self.ok.get('emqx', False):
|
success = False
|
||||||
|
# Publier sur les 3 brokers: emqx, mosquitto, bunkerm
|
||||||
|
for broker_name in ['emqx', 'mosquitto', 'bunkerm']:
|
||||||
|
if broker_name in self.clients and self.ok.get(broker_name, False):
|
||||||
try:
|
try:
|
||||||
r = self.clients['emqx'].publish(topic, msg, qos=1)
|
r = self.clients[broker_name].publish(topic, msg, qos=1)
|
||||||
success = (r.rc == mqtt.MQTT_ERR_SUCCESS)
|
if r.rc == mqtt.MQTT_ERR_SUCCESS:
|
||||||
if success:
|
success = True
|
||||||
messages_published_total.labels(broker='iot-agent', sensor_type=sensor_type).inc()
|
messages_published_total.labels(broker='iot-agent', sensor_type=sensor_type).inc()
|
||||||
message_payload_size.labels(broker='iot-agent').observe(payload_bytes)
|
message_payload_size.labels(broker='iot-agent').observe(payload_bytes)
|
||||||
else:
|
|
||||||
messages_errors_total.labels(broker='iot-agent', sensor_type=sensor_type, error_type="mqtt_rc").inc()
|
|
||||||
return success
|
|
||||||
except Exception:
|
except Exception:
|
||||||
messages_errors_total.labels(broker='iot-agent', sensor_type=sensor_type, error_type="exception").inc()
|
messages_errors_total.labels(broker='iot-agent', sensor_type=sensor_type, error_type="exception").inc()
|
||||||
return False
|
return success
|
||||||
return False
|
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
for name, c in self.clients.items():
|
for name, c in self.clients.items():
|
||||||
|
|||||||
Reference in New Issue
Block a user