feat: Add IoT-Agent integration - simulator publishes to smartcity-api-key/{sid}/attrs via EMQX
This commit is contained in:
@@ -1,27 +1,38 @@
|
||||
version: '3.8'
|
||||
# IoT Agent JSON - Registry in Memory (no MongoDB needed)
|
||||
# Usage: docker compose -f docker-compose.yml -f docker-compose.iot-agent.yml up -d
|
||||
|
||||
networks:
|
||||
traefik-public:
|
||||
external: true
|
||||
smartcity-shared:
|
||||
external: true
|
||||
version: '3.8'
|
||||
|
||||
services:
|
||||
iot-agent:
|
||||
container_name: smart-city-iot-agent
|
||||
image: fiware/iotagent-json:latest
|
||||
container_name: smart-city-iot-agent
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
- IOTA_CB_HOST=fiware-gis-quickstart-orion-1
|
||||
- IOTA_CB_PORT=1026
|
||||
- IOTA_NORTH_PORT=4041
|
||||
- IOTA_REGISTRY_TYPE=mongodb
|
||||
- IOTA_MONGO_URL=mongodb://smart-city-mongodb:27017/iotagent
|
||||
- IOTA_PROVIDER_URL=http://smart-city-iot-agent:4041
|
||||
- IOTA_CB_NGSI_VERSION=ld
|
||||
networks:
|
||||
- smartcity-shared
|
||||
- traefik-public
|
||||
ports:
|
||||
- "4041:4041"
|
||||
environment:
|
||||
# Context Broker (Stellio)
|
||||
- IOTA_CB_HOST=stellio-api-gateway
|
||||
- IOTA_CB_PORT=8080
|
||||
- IOTA_CB_NGSI_VERSION=ld
|
||||
# IoT Agent settings
|
||||
- IOTA_NORTH_PORT=4041
|
||||
- IOTA_REGISTRY_TYPE=memory
|
||||
# MQTT Listener - connect to EMQX
|
||||
- IOTA_MQTT_HOST=emqx_emqx_1
|
||||
- IOTA_MQTT_PORT=1883
|
||||
# No MongoDB needed - using memory registry
|
||||
- IOTA_PROVIDER_URL=http://smart-city-iot-agent:4041
|
||||
- IOTA_DEFAULT_RESOURCE=/
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "curl -f http://localhost:4041/version || exit 1"]
|
||||
interval: 30s
|
||||
timeout: 10s
|
||||
retries: 5
|
||||
start_period: 60s
|
||||
labels:
|
||||
- "traefik.enable=true"
|
||||
- "traefik.http.routers.iot-agent.rule=Host(`iot-agent.digitribe.fr`)"
|
||||
@@ -29,14 +40,8 @@ services:
|
||||
- "traefik.http.routers.iot-agent.tls=true"
|
||||
- "traefik.http.services.iot-agent.loadbalancer.server.port=4041"
|
||||
|
||||
iot-agent-mongodb:
|
||||
container_name: smart-city-mongodb
|
||||
image: mongo:4.4
|
||||
restart: unless-stopped
|
||||
networks:
|
||||
- smartcity-shared
|
||||
volumes:
|
||||
- mongodb-data:/data/db
|
||||
|
||||
volumes:
|
||||
mongodb-data:
|
||||
smartcity-shared:
|
||||
external: true
|
||||
traefik-public:
|
||||
external: true
|
||||
|
||||
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