feat: MQTT integration
This commit is contained in:
130
hack/README.md
Normal file
130
hack/README.md
Normal file
@@ -0,0 +1,130 @@
|
||||
# Hack Directory
|
||||
|
||||
This directory contains utility scripts for testing and development of the SPORE Gateway.
|
||||
|
||||
## Scripts
|
||||
|
||||
### mosquitto.sh
|
||||
Starts a local Mosquitto MQTT broker using Docker.
|
||||
|
||||
**Usage:**
|
||||
```bash
|
||||
./mosquitto.sh
|
||||
```
|
||||
|
||||
This will:
|
||||
- Start a Mosquitto broker on port 1883
|
||||
- Use the configuration from `mosquitto.conf`
|
||||
- Allow anonymous connections (no authentication required)
|
||||
|
||||
### mqtt-test.sh
|
||||
Sends various test events to the local MQTT broker to test the gateway's MQTT integration.
|
||||
|
||||
**Usage:**
|
||||
```bash
|
||||
# Make sure the broker is running first
|
||||
./mosquitto.sh # In terminal 1
|
||||
|
||||
# In another terminal, run the tests
|
||||
./mqtt-test.sh
|
||||
```
|
||||
|
||||
This script will send 16 different test messages covering:
|
||||
|
||||
## Test Message Coverage
|
||||
|
||||
The full `mqtt-test.sh` script will send 16 different test messages covering:
|
||||
- Simple text messages
|
||||
- JSON sensor data (temperature, humidity)
|
||||
- Device status updates
|
||||
- System events and alerts
|
||||
- Configuration updates
|
||||
- Metrics
|
||||
- Cluster/node discovery events
|
||||
- Firmware updates
|
||||
- Task status
|
||||
- Error logs
|
||||
- Light control (SPORE nodes)
|
||||
- Binary data
|
||||
- Edge cases (empty messages, large payloads)
|
||||
|
||||
## Testing MQTT Integration
|
||||
|
||||
### Complete Test Workflow
|
||||
|
||||
1. **Start the MQTT broker:**
|
||||
```bash
|
||||
cd hack
|
||||
./mosquitto.sh
|
||||
```
|
||||
|
||||
2. **In a new terminal, start the SPORE gateway with MQTT enabled:**
|
||||
```bash
|
||||
cd /path/to/spore-gateway
|
||||
./spore-gateway -mqtt tcp://localhost:1883
|
||||
```
|
||||
|
||||
3. **In another terminal, run the test script:**
|
||||
```bash
|
||||
cd hack
|
||||
./mqtt-test.sh
|
||||
```
|
||||
|
||||
4. **Monitor the WebSocket connection** to see the events being forwarded.
|
||||
You can use a WebSocket client or the SPORE UI to connect to `ws://localhost:3001/ws`.
|
||||
|
||||
### Expected Output
|
||||
|
||||
All MQTT messages will be forwarded through the WebSocket with this format:
|
||||
|
||||
```json
|
||||
{
|
||||
"topic": "sensor/temperature/living-room",
|
||||
"data": "{\"temperature\": 23.5, \"unit\": \"celsius\", \"timestamp\": \"2024-01-15T10:30:00Z\"}",
|
||||
"timestamp": "2024-01-15T10:30:00Z"
|
||||
}
|
||||
```
|
||||
|
||||
## Customization
|
||||
|
||||
### Using a Different MQTT Broker
|
||||
|
||||
You can change the broker URL using the `MQTT_BROKER` environment variable:
|
||||
|
||||
```bash
|
||||
MQTT_BROKER=tcp://broker.example.com:1883 ./mqtt-test.sh
|
||||
```
|
||||
|
||||
### Adding Your Own Test Messages
|
||||
|
||||
Edit `mqtt-test.sh` and add your custom test case:
|
||||
|
||||
```bash
|
||||
# Test N: Your custom test
|
||||
echo -e "${YELLOW}=== Test N: Your Description ===${NC}"
|
||||
publish_json "your/topic" '{"your": "data"}'
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Broker Not Starting
|
||||
- Make sure Docker is running
|
||||
- Check if port 1883 is already in use
|
||||
- Verify the Mosquitto image is available: `docker pull eclipse-mosquitto:latest`
|
||||
|
||||
### Messages Not Being Received
|
||||
- Verify the gateway is running with `-mqtt tcp://localhost:1883`
|
||||
- Check the gateway logs for connection errors
|
||||
- Ensure the WebSocket client is connected to `ws://localhost:3001/ws`
|
||||
|
||||
### Port Conflicts
|
||||
If port 1883 is in use, modify `mosquitto.sh` to use a different port:
|
||||
```bash
|
||||
-p 1884:1883 # Maps host port 1884 to container port 1883
|
||||
```
|
||||
|
||||
Then update your gateway command:
|
||||
```bash
|
||||
./spore-gateway -mqtt tcp://localhost:1884
|
||||
```
|
||||
|
||||
10
hack/mosquitto.conf
Normal file
10
hack/mosquitto.conf
Normal file
@@ -0,0 +1,10 @@
|
||||
# -----------------------------
|
||||
# Basic Mosquitto configuration
|
||||
# -----------------------------
|
||||
listener 1883
|
||||
allow_anonymous true
|
||||
|
||||
# (Optional) WebSocket listener if you exposed port 9001 above
|
||||
# listener 9001
|
||||
# protocol websockets
|
||||
# allow_anonymous true
|
||||
7
hack/mosquitto.sh
Executable file
7
hack/mosquitto.sh
Executable file
@@ -0,0 +1,7 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
docker run --rm -it \
|
||||
--name mqtt-broker \
|
||||
-p 1883:1883 \
|
||||
-v $(pwd)/mosquitto.conf:/mosquitto/config/mosquitto.conf:ro \
|
||||
eclipse-mosquitto:latest
|
||||
136
hack/mqtt-test.sh
Executable file
136
hack/mqtt-test.sh
Executable file
@@ -0,0 +1,136 @@
|
||||
#!/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 ""
|
||||
|
||||
Reference in New Issue
Block a user