/sensors

Primary LanguagePython

🌑️ Sensor Stream Analytics System

Complete system of two microservices for sensor simulation and real-time data analysis:

  1. Rust Sensor Simulator - simulates sensors and sends data to RabbitMQ
  2. Python Analytics Engine - analyzes data in real-time

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Rust Sensor       │───▢│   RabbitMQ   │───▢│   Python Analytics  β”‚
β”‚   Simulator         β”‚    β”‚   Message    β”‚    β”‚   Engine            β”‚
β”‚   β€’ Temperature     β”‚    β”‚   Queue      β”‚    β”‚   β€’ Rolling Window  β”‚
β”‚   β€’ Humidity        β”‚    β”‚              β”‚    β”‚   β€’ Statistics      β”‚
β”‚   β€’ Pressure        β”‚    β”‚              β”‚    β”‚   β€’ JSON Export     β”‚
β”‚   β€’ Async timers    β”‚    β”‚              β”‚    β”‚   β€’ CLI Interface   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸš€ Quick Start

Prerequisites

🐳 Running with Docker (recommended)

# Start entire system (in background)
docker compose up -d

# Or start with real-time logs
docker compose up

# View logs for individual services
docker compose logs -f sensor-simulator
docker compose logs -f analytics-engine

# Stop the system
docker compose down

# Rebuild after code changes
docker compose up --build

Important: Analytics Engine runs in interactive mode. To interact with CLI use:

docker exec -it analytics-engine python cli.py

πŸ”§ Manual Setup

1. Start RabbitMQ

docker run -d --hostname my-rabbit --name rabbitmq \
  -p 5672:5672 -p 15672:15672 \
  rabbitmq:3-management

Management UI: http://localhost:15672
Login: guest / guest

2. Start Rust Sensor Simulator

# Build and run sensor simulator
cargo build
cargo run

3. Start Python Analytics Engine

# Install dependencies
cd analytics
pip install -r requirements.txt

# Run analytics
python cli.py

✨ System Features

πŸ¦€ Rust Sensor Simulator

  • βœ… Simulation of 3 sensor types (Temperature, Humidity, Pressure)
  • βœ… Different sampling intervals for each sensor
  • βœ… Realistic data (sine wave + noise)
  • βœ… Asynchronous programming with Tokio
  • βœ… Internal channels for separating generation and publishing
  • βœ… Retry logic and error handling
  • βœ… Detailed logging

🐍 Python Analytics Engine

  • βœ… Real-time data consumption from RabbitMQ
  • βœ… Rolling window (last 10 values)
  • βœ… Mean and standard deviation calculation
  • βœ… CLI with sensor type filtering
  • βœ… Statistics export to JSON
  • βœ… Retry logic and graceful error handling
  • βœ… Monitoring and logging
  • βœ… Docker containerization

πŸ“Š Message Format

{
  "sensor_id": "123e4567-e89b-12d3-a456-426614174000",
  "sensor_type": "Temperature",
  "timestamp": 1716581298,
  "value": 24.73
}

πŸ”§ Interactive Analytics Engine Commands

When running analytics, available commands:

  • s - show current statistics
  • e - export statistics to JSON
  • f Temperature - filter by sensor type
  • c - clear filter
  • q - quit

πŸ§ͺ Testing

Rust tests

cargo test

Python tests

cd analytics
python test_analytics.py

πŸ“ Project Structure

sensors/
β”œβ”€β”€ src/                    # Rust Sensor Simulator
β”‚   β”œβ”€β”€ main.rs            # Entry point and orchestrator
β”‚   β”œβ”€β”€ sensor.rs          # Sensor simulation logic
β”‚   β”œβ”€β”€ publisher.rs       # RabbitMQ connection and publishing
β”‚   └── models.rs          # Data models
β”œβ”€β”€ analytics/             # Python Analytics Engine
β”‚   β”œβ”€β”€ models.py          # Data models
β”‚   β”œβ”€β”€ analytics_engine.py # Main analytics logic
β”‚   β”œβ”€β”€ cli.py             # CLI interface
β”‚   β”œβ”€β”€ test_analytics.py  # Tests
β”‚   β”œβ”€β”€ Dockerfile         # Docker image
β”‚   └── requirements.txt   # Python dependencies
β”œβ”€β”€ docker-compose.yml     # Configuration for entire system
└── README.md             # Documentation

πŸ“ˆ Example Output

Real-time Analytics Engine output:

πŸ”₯ Temperature | ID: a1b2c3d4... | Value: 24.73 | Average: 24.12 | Std Dev: 1.45 | Window: 10/10 | Total messages: 150
πŸ”₯ Humidity | ID: e5f6g7h8... | Value: 62.45 | Average: 63.21 | Std Dev: 3.12 | Window: 10/10 | Total messages: 145
πŸ”₯ Pressure | ID: i9j0k1l2... | Value: 1013.25 | Average: 1012.87 | Std Dev: 2.73 | Window: 10/10 | Total messages: 140

Interactive CLI commands:

🌑️ Sensor Analytics CLI

Available commands:
  s - Show current statistics
  e - Export statistics to JSON
  f <sensor_type> - Set filter (Temperature/Humidity/Pressure)
  c - Clear filter
  q - Quit

> s
πŸ“Š Current Statistics:
Temperature: avg=24.12, count=150, window=10/10
Humidity: avg=63.21, count=145, window=10/10
Pressure: avg=1012.87, count=140, window=10/10

> f Temperature
βœ… Filter set for: Temperature

> e
βœ… Statistics exported to: exports/sensor_stats_20250526_105230.json

πŸ”§ Useful Commands

System Status Check

# Status of all containers
docker compose ps

# Check tests are working
docker exec analytics-engine python test_analytics.py

# Interactive work with Analytics CLI
docker exec -it analytics-engine python cli.py

System Monitoring

# View logs in real-time
docker compose logs -f

# View RabbitMQ Management UI
# Open http://localhost:15672 (guest/guest)

# Check message queues
docker exec rabbitmq rabbitmqctl list_queues

Debugging

# Connect to container for debugging
docker exec -it analytics-engine bash
docker exec -it sensor-simulator bash

# View exported files content
docker exec analytics-engine ls -la exports/