# Sound Level Meter Device Type Support

**⚠️ IMPORTANT**: This documentation uses the legacy `sound_level_meter` device type value. As of v0.4.3, the standardized value is `"slm"`. Run `backend/migrate_standardize_device_types.py` to update your database.

## Overview

Added full support for "Sound Level Meter" as a device type in the roster management system. Users can now create, edit, and manage SLM units through the Fleet Roster interface.

## Changes Made

### 1. Frontend - Unit Detail Page

**File**: `templates/unit_detail.html`

#### Added Device Type Option
- Added "Sound Level Meter" option to device type dropdown (line 243)
- Value: `sound_level_meter`

#### Added SLM-Specific Fields Section (lines 320-370)
New form fields for Sound Level Meter configuration:

- **Host (IP Address)**: Device network address
  - Field name: `slm_host`
  - Example: `192.168.1.100`

- **TCP Port**: Control port (default 2255)
  - Field name: `slm_tcp_port`
  - Type: number

- **Model**: Device model designation
  - Field name: `slm_model`
  - Example: `NL-43`, `NL-53`

- **Serial Number**: Manufacturer serial number
  - Field name: `slm_serial_number`

- **Frequency Weighting**: Sound measurement weighting curve
  - Field name: `slm_frequency_weighting`
  - Options: A-weighting, C-weighting, Z-weighting (Flat)

- **Time Weighting**: Temporal averaging method
  - Field name: `slm_time_weighting`
  - Options: Fast (125ms), Slow (1s), Impulse (35ms)

- **Measurement Range**: Device measurement capability
  - Field name: `slm_measurement_range`
  - Example: `30-130 dB`

#### Updated JavaScript Functions

**toggleDetailFields()** (lines 552-571)
- Now handles three device types: seismograph, modem, sound_level_meter
- Hides all device-specific sections, then shows only the relevant one
- Shows `slmFields` section when device type is `sound_level_meter`

**populateEditForm()** (lines 527-558)
- Populates all 7 SLM-specific fields from unit data
- Sets empty string as default if field is null

### 2. Backend - API Endpoints

**File**: `backend/routers/roster_edit.py`

#### Updated Add Unit Endpoint
`POST /api/roster/add`

**New Parameters** (lines 61-67):
- `slm_host`: str (optional)
- `slm_tcp_port`: int (optional)
- `slm_model`: str (optional)
- `slm_serial_number`: str (optional)
- `slm_frequency_weighting`: str (optional)
- `slm_time_weighting`: str (optional)
- `slm_measurement_range`: str (optional)

**Unit Creation** (lines 108-115):
All SLM fields are set when creating new unit.

#### Updated Get Unit Endpoint
`GET /api/roster/{unit_id}`

**New Response Fields** (lines 146-152):
Returns all 7 SLM fields in the response, with empty string as default if null.

#### Updated Edit Unit Endpoint
`POST /api/roster/edit/{unit_id}`

**New Parameters** (lines 177-183):
Same 7 SLM-specific parameters as add endpoint.

**Unit Update** (lines 232-239):
All SLM fields are updated when editing existing unit.

### 3. Database Schema

**File**: `backend/models.py`

The database schema already included SLM fields (no changes needed):
- All fields are nullable to support multiple device types
- Fields are only relevant when `device_type = "slm"`

## Usage

### Creating a New SLM Unit

1. Go to Fleet Roster page
2. Click "Add Unit" or edit an existing unit
3. Select "Sound Level Meter" from Device Type dropdown
4. Fill in SLM-specific fields (Host, Port, Model, etc.)
5. Save

### Converting Existing Unit to SLM

1. Open unit detail page
2. Click "Edit Unit"
3. Change Device Type to "Sound Level Meter"
4. SLM fields section will appear
5. Fill in required SLM configuration
6. Save changes

### Field Visibility

The form automatically shows/hides relevant fields based on device type:
- **Seismograph**: Shows calibration dates, modem deployment info
- **Modem**: Shows IP, phone number, hardware model
- **Sound Level Meter**: Shows host, port, model, serial, weightings, range

## Integration with SLMM Dashboard

Units with `device_type = "slm"` will:
- Appear in the Sound Level Meters dashboard (`/sound-level-meters`)
- Be available for live monitoring and control
- Use the configured `slm_host` and `slm_tcp_port` for device communication

## Testing

Test SLM units have been added via `add_test_slms.py`:
- `nl43-001` - Deployed at construction site A
- `nl43-002` - Deployed at construction site B
- `nl53-001` - Deployed at residential area
- `nl43-003` - Benched for calibration

You can edit any of these units to verify the form works correctly.

## Files Modified

1. `templates/unit_detail.html` - Added dropdown option, SLM fields section, updated JavaScript
2. `backend/routers/roster_edit.py` - Added SLM parameters to add/edit/get endpoints
3. `backend/models.py` - No changes (schema already supported SLM)

## Backward Compatibility

- Existing seismograph and modem units are unaffected
- All SLM fields are optional/nullable
- Forms gracefully handle units with missing device_type (defaults to seismograph)

---

**Version**: 1.0.0
**Date**: January 5, 2026
**Related**: SOUND_LEVEL_METERS_DASHBOARD.md
