Nexusbus
FastAPI-based Modbus TCP/RTU gateway. Features database-driven config, dynamic device management, automated polling, caching, MQTT publishing, and hot-reload
Install / Use
/learn @Jonathan0823/NexusbusREADME
NexusBus
NexusBus: A robust FastAPI-based middleware for Modbus TCP/RTU, offering a database-driven gateway for seamless data integration. It provides dynamic device management via REST API, automated polling with in-memory caching, real-time MQTT publishing, and hot-reload capabilities for configurations.
📚 Documentation Navigation
| Quick Start | Device Management | Polling | MQTT | Monitoring | Migrations | | ------------------------------------- | ----------------------------------------------- | ------------------------------------------------- | ---------------------------------------- | ------------------------------------------------- | ----------------------------------------- | | Database Setup | Device API Guide | Polling Config | MQTT Guide | Metrics & Monitoring | Migration Guide |
- ✅ Database-Driven Configuration - Store and manage Modbus devices in PostgreSQL
- ✅ Dynamic Device Management - Add/update/remove devices via REST API without restart
- ✅ Automatic Polling - Configure registers to poll automatically from database
- ✅ Parallel Polling - Poll multiple devices concurrently for improved performance
- ✅ Hot-Reload - Apply configuration changes without server restart
- ✅ Connection Pooling - Efficiently manage Modbus TCP connections & shared gateways
- ✅ Circuit Breaker - Automatic failure detection with fast-fail and auto-recovery
- ✅ Request Timeout Handling - Automatic timeout and connection reset
- ✅ REST API - Complete API for device interaction and management
- ✅ Async Support - Full async/await with asyncpg for optimal performance
- ✅ Smart Caching - Register value caching with TTL and automatic eviction
- ✅ MQTT Integration - Real-time data publishing to MQTT brokers
- ✅ Soft Delete - Deactivate devices/polling without losing configuration
- ✅ Structured Logging - JSON-formatted logs for easy parsing and monitoring
- ✅ Metrics Collection - Built-in metrics for performance monitoring
- ✅ Enhanced Health Checks - Comprehensive health monitoring for all services
- ✅ Input Validation - Robust validation for all API inputs
🚀 Quick Start
1. Installation
# Clone repository
git clone https://github.com/Jonathan0823/nexusbus.git
cd nexusbus
# Create virtual environment
python -m venv venv
.\venv\Scripts\Activate.ps1 # Windows PowerShell
# source venv/bin/activate # Linux/Mac
# Install dependencies
pip install -r requirements.txt
2. Database Setup
Configure Environment:
cp .env.example .env
# Edit .env with your PostgreSQL credentials
Run Migrations:
python migrate.py
This creates:
- ✅
modbus_devicestable with sample devices - ✅
polling_targetstable with sample polling configs
3. Start Application
uvicorn main:app --reload
Access:
- API: http://localhost:8000
- Interactive Docs: http://localhost:8000/docs
- Health Check: http://localhost:8000/health
- Metrics: http://localhost:8000/api/metrics
4. Run with Docker (Recommended) 🐳
For easier setup and deployment, you can use Docker Compose to run the application and its PostgreSQL database.
docker-compose up -d
This command will:
- Build the
nexusbus_appDocker image and start the container. - Create and start a PostgreSQL database container (
nexusbus_db). - Automatically run database migrations.
- Expose the application on port
8000.
Access:
- API:
http://localhost:8000 - Interactive Docs:
http://localhost:8000/docs - Health Check:
http://localhost:8000/health
To stop the services:
docker-compose down
📖 API Quick Reference
Device Operations
| Endpoint | Method | Description | Docs |
| ----------------------------------- | ------ | -------------- | ------------------------------------------------- |
| /api/devices | GET | List devices | → |
| /api/devices/{id}/registers | GET | Read registers | → |
| /api/devices/{id}/registers/write | POST | Write register | → |
| /api/devices/gateways | GET | Gateway status | → |
Admin - Device Management
| Endpoint | Method | Description | Docs |
| --------------------------- | ------ | ---------------------- | ------------------------------------------------------------- |
| /api/admin/devices | GET | List all devices | → |
| /api/admin/devices | POST | Create device | → |
| /api/admin/devices/{id} | PUT | Update device | → |
| /api/admin/devices/{id} | DELETE | Soft delete | → |
| /api/admin/devices/reload | POST | Hot-reload configs | → |
Admin - Polling Management
| Endpoint | Method | Description | Docs |
| ------------------------- | ------ | --------------------- | ---------------------------------------------------------------------- |
| /api/admin/polling | GET | List polling targets | → |
| /api/admin/polling | POST | Create polling target | → |
| /api/admin/polling/{id} | PUT | Update target | → |
| /api/admin/polling/{id} | DELETE | Soft delete | → |
Admin - Cache Management
| Endpoint | Method | Description | Docs |
| ------------------------------ | ------ | -------------------- | ---- |
| /api/admin/cache | GET | Inspect all cache | - |
| /api/admin/cache/stats | GET | Cache statistics | - |
| /api/admin/cache/device/{id} | GET | Inspect device cache | - |
| /api/admin/cache | DELETE | Clear all cache | - |
Metrics & Monitoring
| Endpoint | Method | Description | Docs |
| ------------------ | ------ | ------------------------ | ---- |
| /api/metrics | GET | Get all application metrics | - |
| /api/metrics/reset | POST | Reset metrics (testing) | - |
| /health | GET | Comprehensive health check | - |
📦 Configuration
Environment Variables
This application uses a .env file for configuration. Copy .env.example to .env to start.
Database Configuration
| Variable | Description | Default |
| :-------------- | :---------------------------- | :---------------------------------------------------------------- |
| DATABASE_URL | PostgreSQL connection string. | postgresql+asyncpg://postgres:postgres@localhost:5432/modbus_db |
| DATABASE_ECHO | If true, logs SQL queries. | false |
MQTT Configuration (Optional)
| Variable | Description | Default |
| :------------------ | :-------------------------------------- | :------------ |
| MQTT_BROKER_HOST | Broker hostname/IP. Set to enable MQTT. | None |
| MQTT_BROKER_PORT | Broker port number. | 1883 |
| MQTT_USERNAME | MQTT Username. | None |
| MQTT_PASSWORD | MQTT Password. | None |
| MQTT_TOPIC_PREFIX | Prefix for published topics. | modbus/data |
Application Settings
| Variable | Description | Default |
| :---------------------- | :--------------------------------------- | :--------- |
| APP_NAME | Application name. | NexusBus |
| APP_VERSION | Application version. | 0.1.0 |
| POLL_INTERVAL_SECONDS | Polling interval for background service. | 5 |
| CACHE_TTL_SECONDS | Cache entry time-to-live in seconds. | 300 |
**Log
Related Skills
notion
349.0kNotion API for creating and managing pages, databases, and blocks.
feishu-drive
349.0k|
things-mac
349.0kManage Things 3 via the `things` CLI on macOS (add/update projects+todos via URL scheme; read/search/list from the local Things database)
clawhub
349.0kUse the ClawHub CLI to search, install, update, and publish agent skills from clawhub.com
