feat: Add IoT-Agent integration - simulator publishes to smartcity-api-key/{sid}/attrs via EMQX
This commit is contained in:
27
simulator.py
27
simulator.py
@@ -72,6 +72,7 @@ OR_ADMIN_USER = os.environ.get("OR_ADMIN_USER", "admin")
|
||||
OR_ADMIN_PASS = os.environ.get("OR_ADMIN_PASS", "Digitribe972")
|
||||
OR_REALM = os.environ.get("OR_REALM", "smartcity")
|
||||
OR_TOKEN_REALM = os.environ.get("OR_TOKEN_REALM", "master") # Realm pour obtention token
|
||||
ENABLE_IOT_AGENT = os.environ.get("ENABLE_IOT_AGENT", "1") == "1"
|
||||
FROST_URL = os.environ.get("FROST_URL", "http://localhost:8090/FROST-Server/v1.1") # Exposer frost_http-web-1:8080 -> host:8086
|
||||
|
||||
# Pulsar config (HTTP REST — pulsar-admin + producer REST API)
|
||||
@@ -664,6 +665,27 @@ class MultiMQTT:
|
||||
results[name] = False
|
||||
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"
|
||||
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):
|
||||
try:
|
||||
r = self.clients['emqx'].publish(topic, msg, qos=1)
|
||||
success = (r.rc == mqtt.MQTT_ERR_SUCCESS)
|
||||
if success:
|
||||
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
|
||||
|
||||
def stop(self):
|
||||
for name, c in self.clients.items():
|
||||
try:
|
||||
@@ -1239,6 +1261,11 @@ def main():
|
||||
if ok_mqtt:
|
||||
print(f" 📤 {topic} → {','.join(ok_mqtt)}")
|
||||
|
||||
# --- IoT-Agent (via EMQX) ---
|
||||
if ENABLE_IOT_AGENT:
|
||||
ok_iot = mqtt_client.publish_iot_agent(sid, payload_mqtt, sensor_type=stype)
|
||||
print(f" 🤖 IoT-Agent: {'✅' if ok_iot else '❌'}")
|
||||
|
||||
# Extraire les valeurs pour OpenRemote
|
||||
or_values = {}
|
||||
for field, val_range in ranges.items():
|
||||
|
||||
Reference in New Issue
Block a user