odoo_mqtt/iot_bridge/tests/test-config-push.sh
matthias.lotz 19be0903d2 feat: Implement HTTP Config API for Bridge (Phase 3.1)
Implements PUSH architecture - Bridge receives config from Odoo via HTTP

NEW FEATURES:
- HTTP Config Server (FastAPI on port 8080)
  - POST /config: Receive device configuration from Odoo
  - GET /config: Query currently active configuration
  - GET /health: Health check endpoint with device statistics
  - Optional Bearer token authentication (BRIDGE_API_TOKEN)

- Config Validation (Pydantic models)
  - BridgeConfig, DeviceConfig, SessionConfig schemas
  - Validation: unique device_id, unique mqtt_topic
  - Threshold validation: working_threshold_w > standby_threshold_w

- Dynamic Device Management (DeviceManager)
  - Device diff detection (add/update/remove)
  - Automatic MQTT subscribe/unsubscribe for topic changes
  - SessionDetector lifecycle management
  - Active session closing on device removal

- Config Persistence
  - Saves received config to /data/config-active.yaml
  - Bridge startup: loads config-active.yaml as fallback
  - Docker volume iot-bridge-data:/data for persistence

NEW FILES:
- iot_bridge/config_server.py          - FastAPI HTTP server
- iot_bridge/device_manager.py         - Device lifecycle management
- iot_bridge/tests/test-config-push.sh - Integration test script
- iot_bridge/tests/test-config-push.json - Test configuration

MODIFIED FILES:
- iot_bridge/main.py                   - Integrated HTTP server in thread
- iot_bridge/mqtt_client.py            - Added subscribe()/unsubscribe()
- iot_bridge/requirements.txt          - Added fastapi, uvicorn, pydantic
- iot_bridge/Dockerfile                - EXPOSE 8080, HTTP healthcheck
- odoo/docker-compose.dev.yaml         - Port 8080, volume /data

TESTING:
Verified via iot_bridge/tests/test-config-push.sh:
 Config push via HTTP works (200 OK)
 Old devices removed, new devices added
 MQTT topics dynamically updated (subscribe/unsubscribe)
 Config persisted to /data/config-active.yaml
 Health endpoint shows last_config_update timestamp
 Bridge restarts load config-active.yaml automatically

NEXT STEP:
Phase 3.2 - Implement Odoo-side config push (Model hooks)
2026-02-12 20:05:49 +01:00

66 lines
1.6 KiB
Bash
Executable File

#!/bin/bash
# Test script for Config Push API
# Tests the new POST /config endpoint
BRIDGE_URL="${BRIDGE_URL:-http://localhost:8080}"
CONFIG_FILE="${1:-test-config-push.json}"
echo "======================================"
echo "IoT Bridge Config Push Test"
echo "======================================"
echo "Bridge URL: $BRIDGE_URL"
echo "Config File: $CONFIG_FILE"
echo ""
# Check if config file exists
if [ ! -f "$CONFIG_FILE" ]; then
echo "❌ Config file not found: $CONFIG_FILE"
exit 1
fi
echo "1. Testing Health Endpoint..."
echo "--------------------------------------"
curl -s "$BRIDGE_URL/health" | jq '.' || echo "❌ Health check failed"
echo ""
echo ""
echo "2. Pushing Config to Bridge..."
echo "--------------------------------------"
RESPONSE=$(curl -s -w "\n%{http_code}" -X POST \
-H "Content-Type: application/json" \
-d @"$CONFIG_FILE" \
"$BRIDGE_URL/config")
HTTP_CODE=$(echo "$RESPONSE" | tail -n1)
BODY=$(echo "$RESPONSE" | head -n-1)
echo "HTTP Status: $HTTP_CODE"
echo "Response:"
echo "$BODY" | jq '.' || echo "$BODY"
echo ""
if [ "$HTTP_CODE" = "200" ]; then
echo "✅ Config push successful!"
else
echo "❌ Config push failed with HTTP $HTTP_CODE"
exit 1
fi
echo ""
echo "3. Verifying Config Applied..."
echo "--------------------------------------"
sleep 2 # Give bridge time to apply config
curl -s "$BRIDGE_URL/health" | jq '.'
echo ""
echo ""
echo "4. Getting Current Config..."
echo "--------------------------------------"
curl -s "$BRIDGE_URL/config" | jq '.'
echo ""
echo ""
echo "======================================"
echo "✅ Test completed"
echo "======================================"