# Unified API Gateway Documentation

## Overview
This API gateway serves as a single entry point for all API services that were previously separate PHP scripts. Each service is accessed via a `service` parameter that routes the request to the appropriate functionality.

## Base URL
```
main.php?service={service_name}[&parameters]
```

## Available Services

### 1. cams
**Description**: Returns active camera information
**Usage**: `main.php?service=cams`
**Parameters**: None required
**Migration**: 
- Old: `cam.php`
- New: `main.php?service=cams`

### 2. camapi
**Description**: Returns camera API endpoints with filtering
**Usage**: `main.php?service=camapi[&cams&lat1={lat}&lon1={lon}&lat2={lat}&lon2={lon}&elevbottom={elev}&elevtop={elev}]`
**Parameters**:
- `cams` - Get RTSP cameras
- `lat1, lon1, lat2, lon2` - Bounding box coordinates
- `elevbottom, elevtop` - Elevation range
- `camstatic` - Get cameras near a point (requires `lat1`, `lon1`, `radius`)
- `camdb` - Get camera count
**Migration**:
- Old: `camapi.php?cams&lat1=...&lon1=...&lat2=...&lon2=...`
- New: `main.php?service=camapi&cams&lat1=...&lon1=...&lat2=...&lon2=...`

### 3. camlist
**Description**: Returns camera list with active status
**Usage**: `main.php?service=camlist`
**Parameters**: None required
**Migration**:
- Old: `camlist.php`
- New: `main.php?service=camlist`

### 4. admin
**Description**: Admin operations for cameras
**Usage**: `main.php?service=admin&action={action}`
**Parameters**:
- `action='checkurl'` - Check if URL exists (POST required)
- `action='newcam'` - Add new camera (POST required)
**Migration**:
- Old: `admin.php?action=checkurl`
- New: `main.php?service=admin&action=checkurl`

### 5. camcircle
**Description**: Returns camera coverage circles
**Usage**: `main.php?service=camcircle`
**Parameters**: None required
**Migration**:
- Old: `camcircle.php`
- New: `main.php?service=camcircle`

### 6. db
**Description**: Returns weather station data
**Usage**: `main.php?service=db[&outside]`
**Parameters**:
- `outside` - Get outside weather data
**Migration**:
- Old: `db.php`
- New: `main.php?service=db`
- Old: `db.php?outside`
- New: `main.php?service=db&outside`

### 7. fire
**Description**: Returns fire information
**Usage**: `main.php?service=fire`
**Parameters**: None required
**Migration**:
- Old: `fire.php`
- New: `main.php?service=fire`

### 8. individualcam
**Description**: Returns individual camera images
**Usage**: `main.php?service=individualcam&camid={camid}[&dtg={datetime}&camimages={count}]`
**Parameters**:
- `camid` - Camera ID (required)
- `dtg` - Date/time to get images before
- `camimages` - Number of images to return (default: 20)
**Migration**:
- Old: `individualcam.php?camid=...`
- New: `main.php?service=individualcam&camid=...`

### 9. lsr
**Description**: Returns local storm reports and related data
**Usage**: `main.php?service=lsr[&{various parameters}]`
**Parameters**:
- No parameters - Get all reports
- `ohgo` - Get Ohio traffic data
- `ohgotable` - Get Ohio traffic table data
- `lsrohgo`, `ohgohide` - Update records
- `vtec` - Get data for specific VTEC code
- `preflagreports` - Get pre-flagged reports
- `reports` - Get reports within polygon
- `outages` - Get power outages within polygon
- `rtcad` - Get real-time CAD reports
- `verify` - Get verification reports
- `stats` - Get statistics
- `metars` - Get METARs
- `news` - Get news data
- `news2` - Get news data (version 2)
- `news3` - Get news data (version 3)
- `wv511`, `ky511` - Get state traffic data
- `getCombinedTable` - Get combined traffic data
- `updater` - Update records
**Migration**:
- Old: `lsr.php`
- New: `main.php?service=lsr`
- Old: `lsr.php?ohgo`
- New: `main.php?service=lsr&ohgo`

### 10. nws
**Description**: Returns NWS personnel stats
**Usage**: `main.php?service=nws[&{various parameters}]`
**Parameters**:
- No parameters - Get all NWS data
- `officestats`, `officestats11`, `regionstats`, `drilldown` - Various statistics
- `datetime` - Date for statistics
**Migration**:
- Old: `nws.php`
- New: `main.php?service=nws`

### 11. powerapi
**Description**: Returns power outage information
**Usage**: `main.php?service=powerapi[&{various parameters}]`
**Parameters**:
- No parameters - Get current power outages
- `states` - Get state boundaries
- `max` - Get max outages (requires `start` and `end`)
- `county` - Get county power outages
- `countyarchive` - Get archived county outages (requires `start` and `end`)
- `archivepoint` - Get archived point outages (requires `start` and `end`)
- `svr` - Severe weather reports
- `svrpolys` - Get severe weather polygons
- `powerids` - Get specific power outages (requires comma-separated IDs)
- `poweridsgeojson` - Get specific power outages as GeoJSON
- `polygongeojson` - Get power outages in polygon (requires GeoJSON string)
**Migration**:
- Old: `powerapi.php`
- New: `main.php?service=powerapi`
- Old: `powerapi.php?states`
- New: `main.php?service=powerapi&states`

### 12. searchapi
**Description**: Returns search results for power outages
**Usage**: `main.php?service=searchapi[&{various parameters}]`
**Parameters**:
- No parameters - Get current power outages
- `county` - Get county data
- `countyarchive` - Get archived county data
- `archivepoint` - Get archived point data
- `svr` - Get severe weather data
**Migration**:
- Old: `searchapi.php`
- New: `main.php?service=searchapi`

### 13. ohgo
**Description**: Returns Ohio traffic information
**Usage**: `main.php?service=ohgo`
**Parameters**: None required
**Migration**:
- Old: `ohgo.php`
- New: `main.php?service=ohgo`

### 14. power
**Description**: Returns power outage information (simplified)
**Usage**: `main.php?service=power`
**Parameters**: None required
**Migration**:
- Old: `power.php`
- New: `main.php?service=power`

### 15. stormdata
**Description**: Returns storm data (POST only)
**Usage**: POST to `main.php?service=stormdata`
**Parameters** (in POST body as JSON):
- `request_type` - Type of request (ohgo, ohgonopoly, power, powernopoly, wupoly, campoly)
- `start_time`, `end_time` - Time range
- `area_geojson` - Area as GeoJSON
- `buffer` - Buffer time in hours
**Migration**:
- Old: `stormdata.php`
- New: `main.php?service=stormdata` (POST request)

### 16. warntrack
**Description**: Returns warning tracking data
**Usage**: `main.php?service=warntrack`
**Parameters**: None required
**Migration**:
- Old: `warntrack.php`
- New: `main.php?service=warntrack`

### 17. ver
**Description**: Returns version information
**Usage**: `main.php?service=ver[&{various parameters}]`
**Parameters**:
- No parameters - Get version polygons
- `lsrslist` - Get LSRs list
- `reset` - Reset LSRs
- `lsrs` - Update LSRs
- `inc` - Update increment
- `hide` - Update hide status
**Migration**:
- Old: `ver.php`
- New: `main.php?service=ver`

### 18. update_field
**Description**: Updates table fields (POST only)
**Usage**: POST to `main.php?service=update_field`
**Parameters** (in POST body):
- `camid` - Camera ID
- `field` - Field to update (hydro, airport)
- `value` - Value to set
**Migration**:
- Old: `update_field.php`
- New: `main.php?service=update_field` (POST request)

### 19. camobs
**Description**: Returns camera observations with radius and bbox filtering
**Usage**: `main.php?service=camobs[&camstatic={radius|bbox}&lat1={lat}&lon1={lon}&radius={radius}&lat2={lat}&lon2={lon}&elevbottom={elev}&elevtop={elev}]`
**Parameters**:
- `camstatic=radius` - Get cameras within radius (requires `lat1`, `lon1`, `radius`)
- `camstatic=bbox` - Get cameras within bounding box (requires `lat1`, `lon1`, `lat2`, `lon2`, `elevbottom`, `elevtop`)
**Migration**:
- Old: `camobs.php?camstatic=radius&lat1=...&lon1=...&radius=...`
- New: `main.php?service=camobs&camstatic=radius&lat1=...&lon1=...&radius=...`

### 20. single
**Description**: Returns single camera information by camid
**Usage**: `main.php?service=single&camid={camid}`
**Parameters**:
- `camid` - Camera ID (required)
**Migration**:
- Old: `single.php?camid=...`
- New: `main.php?service=single&camid=...`

### 21. powerapitest
**Description**: Returns extended power outage testing data
**Usage**: `main.php?service=powerapitest[&{various parameters}]`
**Parameters**:
- No parameters - Get current power outages
- `max` - Get max outages (requires `start` and `end`)
- `county` - Get county data
- `countyarchive` - Get archived county data (requires `start` and `end`)
- `archivepoint` - Get archived point data (requires `start` and `end`)
- `svr` - Get severe weather reports
- `svr=current` - Get current severe weather
- `svr=archive` - Get archived severe weather (optional `start` and `end`)
**Migration**:
- Old: `powerapitest.php`
- New: `main.php?service=powerapitest`
- Old: `powerapitest.php?max&start=...&end=...`
- New: `main.php?service=powerapitest&max&start=...&end=...`

### 22. mp4
**Description**: Generates MP4/gif from images (POST only)
**Usage**: POST to `main.php?service=mp4`
**Parameters** (in POST body):
- `data` - Array of image paths
- `images` - Number of images
- `delay` - Delay between images
- `lastdelay` - Delay for last image
- `maxh`, `maxv` - Max dimensions
**Migration**:
- Old: `mp4.php`
- New: `main.php?service=mp4` (POST request)

## Error Handling
- Invalid service parameters return HTTP 400 with available services
- Database errors return appropriate error messages
- Missing required parameters return HTTP 400 with descriptive error messages

## Security Considerations
- All user-provided input is properly escaped using `pg_escape_string()`
- SQL injection prevention through parameterized queries
- Input validation for numeric and boolean values

## Migration Guide

### For Web Applications
1. Update all API calls to use the new endpoint format
2. Replace direct script calls with `main.php?service={service_name}`
3. Ensure all existing parameters are preserved
4. Test all functionality to verify compatibility

### For JavaScript Applications
```javascript
// Old approach
fetch('cam.php')
  .then(response => response.json())

// New approach
fetch('main.php?service=cams')
  .then(response => response.json())
```

### For Server-to-Server Communication
```php
// Old approach
$result = file_get_contents('http://yourserver/camapi.php?cams&lat1=...&lon1=...');

// New approach
$result = file_get_contents('http://yourserver/main.php?service=camapi&cams&lat1=...&lon1=...');
```

## Testing
- All services maintain the same output format as the original scripts
- Backwards compatibility is preserved for parameter usage
- JSON output remains consistent with previous implementations