# Cariflex - Stack Technique

## Architecture des données

```
Simulateur IoT → MQTT → FlexMeasures API → PostgreSQL
                                  ↓
                            Scheduling/Forecasting
                                  ↓
                         Grafana (visualisation)
```

## Services

| Service | Rôle | URL |
|---------|-----|-----|
| FlexMeasures | Core RM (scheduling, forecasting, reporting) | https://flexmeasures.digitribe.fr |
| PostgreSQL | Base de données (assets, sensors, schedules) | Interne |
| Redis | File d'attente (jobs RQ) | Interne |
| Grafana | Visualisation | https://grafana.digitribr.fr |

## Flux de données

1. **Simulateur** publie des données MQTT sur des topics cariflex/#
2. **FlexMeasures** expose une API REST pour recevoir les données des capteurs
3. **FlexMeasures** stocke les données dans PostgreSQL
4. **FlexMeasures** génère des forecasts et schedules
5. **Grafana** visualise les données via l'API FlexMeasures

## API FlexMeasures

### Authentification
```bash
POST /api/v3_0/requestAuthToken
{
  "email": "admin@digitribe.fr",
  "password": "Digitribe972"
}
```

### Poster des données de capteur
```bash
POST /api/v3_0/sensors/data
Authorization: Bearer <token>
{
  "sensor": <sensor_id>,
  "values": [<value1>, <value2>, ...],
  "unit": "kW",
  "horizon": "PT0H"
}
```

### Créer un capteur
```bash
POST /api/v3_0/sensors
Authorization: Bearer <token>
{
  "name": "pv_01_power",
  "unit": "kW",
  "generic_asset_id": 1,
  "timezone": "America/Martinique",
  "event_resolution": "PT5M"
}
```

### Déclencher un schedule
```bash
POST /api/v3_0/assets/<asset_id>/schedules/trigger
Authorization: Bearer <token>
{
  "start": "2024-01-01T00:00:00+04:00",
  "duration": "P1D",
  "flex_model": {...},
  "flex_context": {...}
}
```

## Configuration FlexMeasures Cariflex

### Variables d'environnement
```
FLEXMEASURES_REDIS_URL=redis://:Digitribe972@flexmeasures-redis:6379/0
FLEXMEASURES_DB_URL=postgresql://flexmeasures:Digitribe972@flexmeasures-db:5432/flexmeasures
FLEXMEASURES_TIMEZONE=America/Martinique
MAPBOX_ACCESS_TOKEN=
```

### Types d'actifs et flex_context

#### PV (production)
```json
{
  "consumption-capacity": "0kW",
  "production-capacity": "5kW",
  "soc-min": "0kWh",
  "soc-max": "0kWh"
}
```

#### Batterie (stockage)
```json
{
  "consumption-capacity": "50kW",
  "production-capacity": "50kW",
  "soc-min": "10kWh",
  "soc-max": "100kWh",
  "charging-efficiency": "95%",
  "discharging-efficiency": "95%"
}
```

#### EV Borne (consommation flexible)
```json
{
  "consumption-capacity": "22kW",
  "production-capacity": "0kW",
  "soc-min": "0kWh",
  "soc-max": "0kWh"
}
```

#### EV (V2G)
```json
{
  "consumption-capacity": "11kW",
  "production-capacity": "11kW",
  "soc-min": "15kWh",
  "soc-max": "75kWh",
  "charging-efficiency": "95%",
  "discharging-efficiency": "95%"
}
```
