# Realtime Routes Setup Guide

## Overview

This guide explains how to integrate the realtime API routes into your Express backend.

## Files Created

- `realtime.routes.ts` - API endpoints for realtime data

## Setup Steps

### 1. Register the Route in Your Main App

**File**: `src/app.ts` or `src/server.ts`

```typescript
import realtimeRoutes from './routes/realtime.routes'

// Add this line to your Express app setup:
app.use('/api/realtime', realtimeRoutes)
```

### 2. Ensure TypeORM Entities Are Registered

Make sure these entities are imported in your TypeORM config:

```typescript
// src/typeorm.config.ts or similar
import { AssetMaster } from './entities/AssetMaster'
import { AssetMonitoringData } from './entities/AssetMonitoringData'
import { Meter } from './entities/Meter'
import { ParameterMaster } from './entities/ParameterMaster'

const ormconfig = {
  // ... other config
  entities: [
    AssetMaster,
    AssetMonitoringData,
    Meter,
    ParameterMaster,
    // ... other entities
  ]
}
```

### 3. Verify Database Connection

Ensure your PostgreSQL connection is working:

```bash
# Test connection
psql -h localhost -U postgres -d ic3 -c "SELECT COUNT(*) FROM ic3_asset_master;"
```

### 4. Test the Endpoints

```bash
# Test summary endpoint
curl http://localhost:3001/api/realtime/summary

# Test streams endpoint
curl http://localhost:3001/api/realtime/streams

# Test complete dashboard
curl http://localhost:3001/api/realtime/dashboard
```

## API Endpoints

| Endpoint | Method | Description |
|----------|--------|-------------|
| `/api/realtime/summary` | GET | System health KPIs |
| `/api/realtime/streams` | GET | Live data streams |
| `/api/realtime/sensors/current` | GET | Current sensor readings |
| `/api/realtime/sensors/stats` | GET | Sensor type statistics |
| `/api/realtime/meters` | GET | Meter consumption data |
| `/api/realtime/devices/registry` | GET | Device registry |
| `/api/realtime/sources/stale` | GET | Stale data sources |
| `/api/realtime/alerts` | GET | Critical alerts |
| `/api/realtime/dashboard` | GET | Complete dashboard data |

## Environment Variables

Add to your `.env` file:

```env
# API URL for frontend
REACT_APP_API_URL=http://localhost:3001/api

# Database (should already be configured)
DB_HOST=localhost
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=your-password
DB_NAME=ic3
```

## Troubleshooting

### No data in responses

1. Check PostgreSQL connection
2. Verify assets exist in `ic3_asset_master`
3. Verify monitoring data exists in `ic3_asset_monitoring_data`
4. Check browser console for error messages

### TypeORM entity errors

If you see "Entity is not registered" errors:

1. Import the entity in your TypeORM config
2. Ensure entity file paths are correct
3. Rebuild the project

### CORS errors on frontend

Add CORS headers to your Express app:

```typescript
import cors from 'cors'

app.use(cors({
  origin: 'http://localhost:3000', // Your frontend URL
  credentials: true
}))
```

## Next Steps

1. Create TypeORM entities if not already done
2. Register realtime routes in your Express app
3. Test endpoints with curl or Postman
4. Frontend will automatically call these endpoints when dashboard loads
