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
|
||||
|
||||
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."""
|
||||
topic = f"smartcity-api-key/{sid}/attrs"
|
||||
"""Publie sur le topic IoT-Agent (json/smartcity-api-key/{sid}/attrs) via les 3 brokers."""
|
||||
topic = f"json/smartcity-api-key/{sid}/attrs"
|
||||
msg = json.dumps(payload, ensure_ascii=False)
|
||||
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:
|
||||
r = self.clients['emqx'].publish(topic, msg, qos=1)
|
||||
success = (r.rc == mqtt.MQTT_ERR_SUCCESS)
|
||||
if success:
|
||||
r = self.clients[broker_name].publish(topic, msg, qos=1)
|
||||
if r.rc == mqtt.MQTT_ERR_SUCCESS:
|
||||
success = True
|
||||
messages_published_total.labels(broker='iot-agent', sensor_type=sensor_type).inc()
|
||||
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:
|
||||
messages_errors_total.labels(broker='iot-agent', sensor_type=sensor_type, error_type="exception").inc()
|
||||
return False
|
||||
return False
|
||||
return success
|
||||
|
||||
def stop(self):
|
||||
for name, c in self.clients.items():
|
||||
|
||||
Reference in New Issue
Block a user