# Configuration API

Manage device settings including LoRa, encryption, and Modbus node configuration.

## Get Configuration

Retrieve the full device configuration including WiFi, MQTT, and LoRa settings.

```
GET /api/config
```

**Response:**
```json
{
  "wifi_ssid": "MyNetwork",
  "wifi_password": "********",
  "ap_mode": false,
  "mac_address": "AABBCCDDEEFF",
  "ip_address": "192.168.1.100",
  "mdns_address": "swychmd1-AABBCC.local",
  "firmware_version": "1.0.3",
  "loraEnabled": true,
  "encryptionEnabled": true,
  "encryptionKey": "your-32-character-encryption-key",
  "mqttHost": "mqtt.example.com",
  "mqttPort": 1883,
  "mqttUsername": "user",
  "mqttPassword": "********",
  "mqttTopic": "swych/md1/data",
  "webApiHost": "https://api.example.com/webhook",
  "webApiKey": "api-key-here",
  "modbusNodes": {
    "tempSensor1": {
      "address": 1,
      "channels": 8,
      "readRange": [0, 7],
      "readType": "INPUT_REGISTER"
    }
  }
}
```

## Save Full Configuration

Save complete device configuration. The device will restart after saving.

```
POST /api/config/save
Content-Type: application/json
```

**Request Body:**
```json
{
  "id": "AABBCCDDEEFF",
  "mdConfig": true,
  "loraEnabled": true,
  "encryptionEnabled": true,
  "encryptionKey": "your-32-character-encryption-key",
  "modbusNodes": {
    "solenoid1": {
      "address": 1,
      "channels": 8,
      "readRange": [0, 7],
      "readType": "COIL_STATUS"
    },
    "tempSensor1": {
      "address": 2,
      "channels": 4,
      "readRange": [0, 3],
      "readType": "INPUT_REGISTER"
    }
  }
}
```

> **Important:** The `id` field must match the device's MAC address. Either `mdConfig` or `md1Config` must be set to `true`.

**Response:**
```json
{
  "status": "Configuration saved. Restarting device..."
}
```

## Configuration Fields

### Core Settings

| Field | Type | Description |
|-------|------|-------------|
| `id` | string | Device MAC address (required) |
| `mdConfig` | boolean | Must be `true` to save config (also accepts `md1Config`) |
| `loraEnabled` | boolean | Enable/disable LoRa radio |
| `encryptionEnabled` | boolean | Enable AES-256-CBC encryption |
| `encryptionKey` | string | 32-character encryption key |

### Modbus Node Configuration

Each node in `modbusNodes` is defined with:

| Field | Type | Description |
|-------|------|-------------|
| `address` | integer | Modbus address (1-64) |
| `channels` | integer | Number of channels |
| `readRange` | array | `[start, count]` registers to read |
| `readType` | string | Register type (see below) |

### Read Types

| Value | Description |
|-------|-------------|
| `HOLDING_REGISTER` | Read/write registers (default) |
| `INPUT_REGISTER` | Read-only analog values |
| `COIL_STATUS` | Read/write single-bit values |
| `DISCRETE_INPUT` | Read-only single-bit values |

## Example: Temperature Sensor Setup

```json
{
  "id": "AABBCCDDEEFF",
  "mdConfig": true,
  "modbusNodes": {
    "freezer": {
      "address": 1,
      "channels": 1,
      "readRange": [0, 1],
      "readType": "INPUT_REGISTER"
    },
    "coolroom": {
      "address": 2,
      "channels": 1,
      "readRange": [0, 1],
      "readType": "INPUT_REGISTER"
    }
  }
}
```

## Example: Solenoid Valve Control

```json
{
  "id": "AABBCCDDEEFF",
  "mdConfig": true,
  "modbusNodes": {
    "valves": {
      "address": 1,
      "channels": 8,
      "readRange": [0, 8],
      "readType": "COIL_STATUS"
    }
  }
}
```
