Skip to main content
Glama
README.md
# `@shadojus/open-env-sensors`

Zero-dependency environmental telemetry normalizer, `node:sqlite` spatial engine, OpenAPI 3.1 REST server, CLI, MCP (`2024-11-05` stdio) server, and Python stdlib package normalizing:

- **Air Quality (`air`)** — `PM2.5`, `PM10`, `NO2`, `O3` (`µg/m³`) + composite **US EPA 2024 AQI** (`0–500`), category, and dominant pollutant.
- **River & Flood Water Levels (`river`)** — water level `levelCm` (`cm`) and discharge `dischargeM3s` (`m³/s`, converted from USGS `ft` / `ft³/s` and WSV Pegelonline `W` / `Q`).
- **Ambient Gamma Radiation (`radiation`)** — 1-hour ambient gamma dose rate `doseUsvH` (`µSv/h`) from BfS ODL-Info.

## Data Sources & Attribution

| Source ID | Domain | Upstream Endpoint | License |
| --- | --- | --- | --- |
| `sensor-community` | `air` | `https://data.sensor.community/static/v2/data.json` | ODbL / Open Data |
| `uba-air-de` | `air` | `https://www.umweltbundesamt.de/api/air_data/v3/` | DL-DE/CC BY |
| `pegelonline-wsv` | `river` | `https://www.pegelonline.wsv.de/webservices/rest-api/v2/stations.json?includeTimeseries=true&includeCurrentMeasurement=true` | DL-DE->Zero-2.0 |
| `usgs-water` | `river` | `https://waterservices.usgs.gov/nwis/iv/` | Public Domain |
| `bfs-odl` | `radiation` | `https://odlinfo.bfs.de` (`www.imis.bfs.de` WFS) | DL-DE->BY-2.0 |

## Quickstart

### Node.js ESM (`>=22.5.0`, zero dependencies)

```js
import { createEnvSensors, sensorsNear, getSensor, computeAqi } from '@shadojus/open-env-sensors';

const aqi = computeAqi(12.4, 28.0, 19.0, 44.0);
// -> { aqi: 57, category: 'Moderate', dominant: 'pm25', subIndices: { pm25: 57, pm10: 26, no2: 10, o3: 20 } }

const nearBerlin = await sensorsNear(52.52, 13.405, { radiusKm: 50, limit: 10 });
const store = createEnvSensors({ dbPath: ':memory:' });
const rhine = store.getSensor('pegelonline-wsv:koeln');
```

### CLI

```bash
npx @shadojus/open-env-sensors near 52.52 13.405 --domain air
npx @shadojus/open-env-sensors aqi --pm25 15.2 --pm10 34.0
npx @shadojus/open-env-sensors serve --port 8787
npx @shadojus/open-env-sensors --mcp
```

### REST API (`src/server.mjs`)

- `GET /health`
- `GET /openapi.json` (OpenAPI 3.1.0)
- `GET /v1/sources`
- `GET /v1/sensors?domain=air&country=DE&limit=50`
- `GET /v1/sensors/near?lat=52.52&lng=13.405&radiusKm=50`
- `GET /v1/air/near?lat=52.52&lng=13.405&radiusKm=50`
- `GET /v1/rivers/near?lat=50.937&lng=6.963&radiusKm=50`
- `GET /v1/radiation/near?lat=52.52&lng=13.405&radiusKm=50`
- `GET /v1/sensors/:id`

Includes CORS, SHA-256 ETag (`304 Not Modified`), and IPv6 `/64`-aware rate limiting (`429 Too Many Requests`).

### MCP Server (`2024-11-05` JSON-RPC 2.0 over stdio)

Tools exposed by `open-env-sensors --mcp`:
- `find_env_sensors_near`
- `query_air_quality`
- `query_river_levels`
- `query_ambient_radiation`
- `list_env_sources`

### Python Package (`shadojus-env-sensors`, Python `>=3.9` stdlib)

```python
from shadojus_env_sensors import compute_aqi, sensors_near, EnvSensorsClient

print(compute_aqi(pm25=14.0, pm10=30.0))
print(sensors_near(52.52, 13.405, domain="air", radius_km=50))
```

## License

MIT