Files
spore-gateway/hack/mqtt-test.sh
2025-10-26 18:37:07 +01:00

137 lines
4.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# MQTT Test Script for SPORE Gateway
# This script sends various test events to the local MQTT broker
set -e
# Configuration
MQTT_BROKER="${MQTT_BROKER:-tcp://localhost:1883}"
DOCKER_IMAGE="eclipse-mosquitto:latest"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Function to publish an MQTT message
publish_message() {
local topic="$1"
local payload="$2"
local qos="${3:-0}"
echo -e "${YELLOW}Publishing to topic: ${GREEN}${topic}${NC}"
docker run --rm --network host \
"${DOCKER_IMAGE}" \
mosquitto_pub \
-h localhost \
-p 1883 \
-t "${topic}" \
-m "${payload}" \
-q "${qos}"
if [ $? -eq 0 ]; then
echo -e "${GREEN}✓ Message sent successfully${NC}"
else
echo -e "${RED}✗ Failed to send message${NC}"
exit 1
fi
echo ""
}
# Function to publish a JSON message
publish_json() {
local topic="$1"
local json="$2"
local qos="${3:-0}"
publish_message "${topic}" "${json}" "${qos}"
}
# Main test execution
echo "============================================"
echo " SPORE Gateway MQTT Test Suite"
echo "============================================"
echo ""
echo "Using MQTT broker: ${MQTT_BROKER}"
echo ""
# Test 1: Simple text message
echo -e "${YELLOW}=== Test 1: Simple Text Message ===${NC}"
publish_message "test/hello" "Hello from MQTT test script!"
# Test 2: Temperature sensor reading
echo -e "${YELLOW}=== Test 2: Temperature Sensor Reading ===${NC}"
publish_json "sensor/temperature/living-room" '{"temperature": 23.5, "unit": "celsius", "timestamp": "2024-01-15T10:30:00Z"}'
# Test 3: Humidity sensor reading
echo -e "${YELLOW}=== Test 3: Humidity Sensor Reading ===${NC}"
publish_json "sensor/humidity/bedroom" '{"humidity": 45.2, "unit": "percent", "timestamp": "2024-01-15T10:30:05Z"}'
# Test 4: Device status
echo -e "${YELLOW}=== Test 4: Device Status Update ===${NC}"
publish_json "device/status/esp32-001" '{"id": "esp32-001", "status": "online", "uptime": 3600, "firmware": "v1.2.3"}'
# Test 5: System event
echo -e "${YELLOW}=== Test 5: System Event ===${NC}"
publish_json "system/event" '{"type": "startup", "message": "Gateway started successfully", "timestamp": "2024-01-15T10:30:10Z"}'
# Test 6: Alert message
echo -e "${YELLOW}=== Test 6: Alert Message ===${NC}"
publish_json "alert/high-temperature" '{"level": "warning", "message": "Temperature exceeded threshold", "value": 35.5, "threshold": 30.0}'
# Test 7: Configuration update
echo -e "${YELLOW}=== Test 7: Configuration Update ===${NC}"
publish_json "config/update" '{"section": "network", "key": "retry_count", "value": 3, "updated": "2024-01-15T10:30:15Z"}'
# Test 8: Metric data
echo -e "${YELLOW}=== Test 8: Metric Data ===${NC}"
publish_json "metrics/system" '{"cpu": 45.2, "memory": 62.5, "disk": 38.7, "timestamp": "2024-01-15T10:30:20Z"}'
# Test 9: Node discovery event
echo -e "${YELLOW}=== Test 9: Node Discovery Event ===${NC}"
publish_json "cluster/node/discovered" '{"ip": "192.168.1.100", "hostname": "node-001", "status": "online", "version": "1.0.0"}'
# Test 10: Firmware update event
echo -e "${YELLOW}=== Test 10: Firmware Update Event ===${NC}"
publish_json "firmware/update/esp32-001" '{"node": "esp32-001", "status": "completed", "version": "v1.3.0", "size": 1234567}'
# Test 11: Task status
echo -e "${YELLOW}=== Test 11: Task Status ===${NC}"
publish_json "task/sync/status" '{"id": "sync-001", "status": "running", "progress": 75, "estimated_completion": "2024-01-15T10:35:00Z"}'
# Test 12: Error log
echo -e "${YELLOW}=== Test 12: Error Log ===${NC}"
publish_json "log/error" '{"severity": "error", "component": "mqtt-client", "message": "Connection timeout", "code": 1001}'
# Test 13: Light control (for SPORE nodes)
echo -e "${YELLOW}=== Test 13: Light Control ===${NC}"
publish_json "light/control" '{"id": "neopixel-001", "brightness": 128, "color": {"r": 255, "g": 0, "b": 0}, "pattern": "solid"}'
# Test 14: Binary data (as hex string)
echo -e "${YELLOW}=== Test 14: Binary Data ===${NC}"
publish_message "data/binary" "48656c6c6f20576f726c64" # "Hello World" in hex
# Test 15: Empty message
echo -e "${YELLOW}=== Test 15: Empty Message ===${NC}"
publish_message "test/empty" ""
# Test 16: Large payload
echo -e "${YELLOW}=== Test 16: Large Payload ===${NC}"
LARGE_PAYLOAD='{"data": "'$(head -c 1000 < /dev/zero | tr '\0' 'A')'"}'
publish_message "test/large" "${LARGE_PAYLOAD}"
echo "============================================"
echo -e "${GREEN}All tests completed successfully!${NC}"
echo "============================================"
echo ""
echo "To monitor these messages, connect to the WebSocket at:"
echo " ws://localhost:3001/ws"
echo ""
echo "You should see all these events forwarded with the format:"
echo ' {"topic": "...", "data": "...", "timestamp": "..."}'
echo ""