44602a11457192863362c277d1efbf0af23726e0
Watermaker PLC API
RESTful API for monitoring and controlling watermaker PLC systems. Provides access to sensors, timers, controls, and watermaker operation sequences.
Features
- Real-time Data Monitoring: Sensors, timers, outputs, runtime hours, water counters
- Selective Data Retrieval: Bandwidth-optimized API for specific data groups/keys
- Asynchronous Control Operations: DTS start/stop/skip sequences with progress tracking
- Background Data Updates: Continuous PLC monitoring with configurable intervals
- Thread-safe Data Caching: Centralized cache with concurrent access support
- Comprehensive Error Handling: Structured error responses and logging
- Modular Architecture: Clean separation of concerns with service layers
Quick Start
Installation
# Clone repository
git clone <repository-url>
cd watermaker-plc-api
# Install dependencies
pip install -r requirements.txt
# Install package
pip install -e .
Configuration
Set environment variables:
export PLC_IP=192.168.1.15
export PLC_PORT=502
export DATA_UPDATE_INTERVAL=5
export LOG_LEVEL=INFO
Running the API
# Using the command line entry point
watermaker-api --host 0.0.0.0 --port 5000
# Or run directly
python -m watermaker_plc_api.main
API Endpoints
Data Monitoring
GET /api/status- Connection and system statusGET /api/all- All PLC data in one responseGET /api/select- Selective data retrieval (bandwidth optimized)GET /api/sensors- All sensor dataGET /api/timers- All timer dataGET /api/outputs- Output control dataGET /api/runtime- Runtime hours dataGET /api/water_counters- Water production counters
Control Operations
POST /api/dts/start- Start DTS watermaker sequence (async)POST /api/dts/stop- Stop watermaker sequence (async)POST /api/dts/skip- Skip current step (async)GET /api/dts/status- Get DTS operation statusPOST /api/write/register- Write single register
Selective API Examples
# Get temperature and pressure sensors only
curl "http://localhost:5000/api/select?groups=temperature,pressure"
# Get specific registers
curl "http://localhost:5000/api/select?keys=1036,1003,1017,1121"
# Combined approach
curl "http://localhost:5000/api/select?groups=dts_timer&keys=1036"
Control Examples
# Start DTS sequence (returns immediately with task_id)
curl -X POST http://localhost:5000/api/dts/start
# Poll for progress
curl http://localhost:5000/api/dts/status/abc12345
# Stop watermaker
curl -X POST http://localhost:5000/api/dts/stop
Architecture
watermaker_plc_api/
├── app.py # Flask application factory
├── config.py # Configuration management
├── controllers/ # API route handlers
├── services/ # Business logic layer
├── models/ # Data models and mappings
└── utils/ # Utilities and helpers
Key Components
- PLCConnection: Thread-safe Modbus TCP communication
- DataCache: Centralized, thread-safe data storage
- RegisterReader: Service for reading PLC registers
- RegisterWriter: Service for writing PLC registers
- BackgroundTaskManager: Continuous data updates
- Controllers: RESTful API endpoints
Variable Groups
| Group | Description | Count |
|---|---|---|
| system | System status and mode | 2 |
| pressure | Water pressure sensors | 3 |
| temperature | Temperature monitoring | 2 |
| flow | Flow rate meters | 3 |
| quality | Water quality (TDS) sensors | 2 |
| fwf_timer | Fresh water flush timers | 1 |
| dts_timer | DTS process step timers | 6 |
| rtc | Real-time clock data | 6 |
| outputs | Digital output controls | 6 |
| runtime | System runtime hours | 1 |
| water_counters | Water production counters | 4 |
Development
Running Tests
python -m pytest tests/
Environment Setup
# Development environment
export FLASK_ENV=development
export DEBUG=true
export PLC_IP=127.0.0.1 # For simulator
# Production environment
export FLASK_ENV=production
export SECRET_KEY=your-secret-key
export PLC_IP=192.168.1.15
License
MIT License - see LICENSE file for details.
Description
Languages
Python
99.8%
Shell
0.2%