Metadata-Version: 2.3
Name: modapi
Version: 0.1.5
Summary: Unified API for Modbus communication
Author: Tom Sapletta
Author-email: info@softreck.dev
Requires-Python: >=3.8,<4.0
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Requires-Dist: flask (>=2.0.0,<3.0.0)
Requires-Dist: paho-mqtt (>=2.0.0,<3.0.0)
Requires-Dist: pymodbus (>=3.0.0,<4.0.0)
Requires-Dist: python-dotenv (>=0.19.0,<0.20.0)
Description-Content-Type: text/markdown

# ModbusAPI

Unified API for Modbus communication with multiple interfaces: Shell CLI, REST API, and MQTT.

## Features

- **Modbus RTU Client** - Core functionality for communicating with Modbus devices
- **Auto-detection** - Automatically detect Modbus devices on serial ports
- **Multiple APIs**:
  - **Shell CLI** - Command line interface for direct Modbus operations
  - **REST API** - HTTP API for web applications
  - **MQTT API** - MQTT interface for IoT applications
- **Interactive Mode** - Interactive shell for manual Modbus operations
- **JSON Output** - Structured JSON output for easy parsing

## Installation

This project uses [Poetry](https://python-poetry.org/) for dependency management.

1. Install Poetry if you haven't already:
   ```bash
   curl -sSL https://install.python-poetry.org | python3 -
   ```

2. Clone the repository and install dependencies:
   ```bash
   git clone https://github.com/yourusername/modbusapi.git
   cd modbusapi
   poetry install  # Install all dependencies
   
   # Or install with specific groups:
   poetry install --only main,rest  # Only REST API
   poetry install --only main,mqtt  # Only MQTT API
   poetry install --with dev        # Development tools
   ```

3. Activate the virtual environment:
   ```bash
   poetry shell
   ```

## Development

- Install development dependencies:
  ```bash
  poetry install --with dev
  ```

- Run tests:
  ```bash
  poetry run pytest
  ```

- Run with coverage:
  ```bash
  poetry run pytest --cov=modapi tests/
  ```

## Building and Publishing

- Build the package:
  ```bash
  poetry build
  ```

- Publish to PyPI:
  ```bash
  poetry publish --build
  ```

## Usage

### Shell CLI

```bash
# Basic commands
modbusapi rc 0 8       # Read 8 coils starting at address 0
modbusapi wc 0 1       # Write value 1 to coil at address 0
modbusapi rh 0 5       # Read 5 holding registers starting at address 0

# With options
modbusapi -v rc 0 8    # Verbose mode
modbusapi -p /dev/ttyACM0 wc 0 1  # Specify port
modbusapi --scan       # Scan for Modbus devices

# Interactive mode
modbusapi --interactive
```

### REST API

```python
from modbusapi import create_rest_app

# Create and run Flask app
app = create_rest_app(port='/dev/ttyACM0', api_port=5000)
app.run_server()
```

#### REST API Endpoints

- `GET /api/status` - Get Modbus connection status
- `GET /api/coils/<address>` - Read single coil
- `GET /api/coils/<address>/<count>` - Read multiple coils
- `POST /api/coils/<address>` - Write single coil
- `POST /api/toggle/<address>` - Toggle coil state
- `GET /api/discrete_inputs/<address>/<count>` - Read discrete inputs
- `GET /api/holding_registers/<address>/<count>` - Read holding registers
- `POST /api/holding_registers/<address>` - Write holding register
- `GET /api/input_registers/<address>/<count>` - Read input registers
- `GET /api/scan` - Scan for Modbus devices
- `GET /api/docs` - Get API documentation

### MQTT API

```python
from modbusapi import start_mqtt_broker

# Start MQTT client
client = start_mqtt_broker(
    port='/dev/ttyACM0',
    mqtt_broker='localhost',
    mqtt_port=1883,
    mqtt_topic_prefix='modbus'
)
```

#### MQTT Topics

- `modbus/command/read_coil/<address>/<count>` - Read coils
- `modbus/command/write_coil/<address>` - Write coil
- `modbus/command/toggle_coil/<address>` - Toggle coil
- `modbus/command/read_discrete_input/<address>/<count>` - Read discrete inputs
- `modbus/command/read_holding_register/<address>/<count>` - Read holding registers
- `modbus/command/write_holding_register/<address>` - Write holding register
- `modbus/command/read_input_register/<address>/<count>` - Read input registers
- `modbus/status` - Connection status

## Configuration

ModbusAPI can be configured using environment variables or directly in code:

```
# .env file
MODBUS_PORT=/dev/ttyACM0
MODBUS_BAUDRATE=9600
MODBUS_TIMEOUT=1.0
MODBUS_DEVICE_ADDRESS=1
```

## Development

```bash
# Install development dependencies
pip install -e .[dev]

# Run tests
pytest
```

## License

Apache 2.0

