# Cariflex - Diagrammes d'Architecture v2

> Mise à jour avec OCPI et GIREVE

## 1. Architecture Générale

```mermaid
graph TB
    subgraph EXTERNAL["External Layer"]
        EPEX["EPEX SPOT<br/>Day-ahead Prices"]
        ENTSOE["ENTSO-E<br/>CO2/Prices"]
        WEATHER["Weather API<br/>Solcast/Météo"]
        DSO["DSO/TSO<br/>OpenADR/S2"]
    end

    subgraph INTEROP["Interoperability Layer"]
        OCPI["OCPI Gateway<br/>━━━━━━━━━━━━━━<br/>• Tarifs • Sessions<br/>• CDRs • Roaming<br/>• Commands"]
        GIREVE["GIREVE Connector<br/>━━━━━━━━━━━━━━<br/>• Session data<br/>• Roaming hub<br/>• Billing<br/>• PNIRE"]
    end

    subgraph INTEGRATION["Integration Layer"]
        FM["FlexMeasures EMS<br/>━━━━━━━━━━━━━━<br/>• Ingestion<br/>• Forecasting<br/>• Scheduling<br/>• Reporting"]
        
        CITRINEOS["CitrineOS<br/>CSMS<br/>━━━━━━━━━━━━━━<br/>• OCPP 2.0.1<br/>• Charge Points<br/>• Transactions"]
        
        EVEREST["EVerest<br/>Middleware<br/>━━━━━━━━━━━━━━<br/>• ISO 15118<br/>• Smart Charging<br/>• Power Mgmt"]
        
        OPENLEADR["OpenLEADR<br/>VEN Client<br/>━━━━━━━━━━━━━━<br/>• OpenADR 2.0b<br/>• DSO Signals<br/>• Flexibility"]
    end

    subgraph MARKET["Market Layer (R&D)"]
        GSY["Grid Singularity<br/>gsy-e<br/>━━━━━━━━━━━━━━<br/>• P2P Trading<br/>• Market Clearing"]
        
        OPLEM["OPLEM<br/>━━━━━━━━━━━━━━<br/>• Local Market<br/>• Prosumer Opt."]
        
        HAMLET["HAMLET<br/>━━━━━━━━━━━━━━<br/>• Multi-agent<br/>• Simulation"]
    end

    subgraph DEVICES["Device Layer"]
        PV["10 PV Panels<br/>5kWc"]
        BAT["10 Batteries<br/>100kWh"]
        EV["10 EVs<br/>75kWh V2G"]
        CHG["10 EV Chargers<br/>22kW"]
    end

    subgraph VIZ["Visualization"]
        GRAFANA["Grafana<br/>Dashboards"]
        METABASE["Metabase<br/>Analytics"]
        CARIFLEX_UI["Cariflex UI<br/>FM Frontend"]
    end

    %% External to Interop
    DSO -->|"Flex Requests"| OPENLEADR
    EPEX -->|"Prices"| FM
    ENTSOE -->|"CO2/Prices"| FM
    WEATHER -->|"Irradiance"| FM

    %% Interop layer
    OCPI <-->|"OCPI 2.2"| OtherCPOs["Other CPOs<br/>(Roaming)"]
    GIREVE <-->|"eMSP API"| GIREVEPlatform["GIREVE Platform"]
    OCPI -->|"Session data"| FM
    GIREVE -->|"CDRs/Sessions"| FM

    %% Integration to FM
    CITRINEOS -->|"OCPP/REST"| FM
    EVEREST -->|"OCPP"| CITRINEOS
    OPENLEADR -->|"REST API"| FM

    %% Market to FM
    GSY <-.->|"gsy-e-sdk"| FM
    OPLEM <-.->|"sensor data"| FM
    HAMLET <-.->|"forecasts"| FM

    %% Devices
    PV -->|"Modbus"| FM
    BAT -->|"Modbus"| FM
    EV -->|"ISO 15118"| EVEREST
    CHG -->|"OCPP 2.0.1"| CITRINEOS

    %% FM to Viz
    FM -->|"PostgreSQL"| GRAFANA
    FM -->|"PostgreSQL"| METABASE
    FM -->|"REST API"| CARIFLEX_UI

    style FM fill:#2196F3,stroke:#1565C0,color:#fff
    style OCPI fill:#E91E63,stroke:#C2185B,color:#fff
    style GIREVE fill:#795548,stroke:#5D4037,color:#fff
    style CITRINEOS fill:#4CAF50,stroke:#388E3C,color:#fff
    style EVEREST fill:#FF9800,stroke:#F57C00,color:#fff
    style OPENLEADR fill:#9C27B0,stroke:#7B1FA2,color:#fff
    style GSY fill:#00BCD4,stroke:#0097A7,color:#fff
```

## 2. Flux OCPI - Session de Recharge

```mermaid
sequenceDiagram
    participant EV as EV Driver
    participant CP as Charge Point
    participant CitrineOS as CitrineOS CSMS
    participant FM as FlexMeasures
    participant OCPI as OCPI Gateway
    participant OtherCPO as Other CPO

    %% Session start
    EV->>CP: Plug in (RFID/App)
    CP->>CitrineOS: StartTransaction
    CitrineOS->>FM: POST /sensors/{id}/data (power)
    FM->>OCPI: Check roaming rights
    OCPI->>OtherCPO: POST /cdrs (session start)
    OtherCPO-->>OCPI: 200 OK
    OCPI-->>FM: Authorized

    %% Charging loop
    loop Every 30s
        CitrineOS->>FM: Power readings
        FM->>OCPI: Update session
        OCPI->>OtherCPO: PATCH /sessions/{id}
    end

    %% Session end
    EV->>CP: Unplug
    CP->>CitrineOS: StopTransaction
    CitrineOS->>FM: Final readings
    FM->>OCPI: Generate CDR
    OCPI->>OtherCPO: POST /cdrs
    OtherCPO-->>OCPI: CDR accepted
```

## 3. Flux GIREVE - Session de Recharge

```mermaid
sequenceDiagram
    participant EV as EV Driver
    participant CP as Charge Point
    participant CitrineOS as CitrineOS CSMS
    participant FM as FlexMeasures
    participant GIREVE as GIREVE Platform

    %% Session start
    EV->>CP: Start charging
    CP->>CitrineOS: StartTransaction
    CitrineOS->>FM: POST /sensors/{id}/data
    FM->>GIREVE: POST /session (session start)
    GIREVE-->>FM: Session acknowledged

    %% Charging loop
    loop Every 30s
        CitrineOS->>FM: Power readings
        FM->>GIREVE: PATCH /session/{id} (consumption)
    end

    %% Session end
    EV->>CP: Stop charging
    CP->>CitrineOS: StopTransaction
    CitrineOS->>FM: Final readings
    FM->>GIREVE: POST /cdr (final CDR)
    GIREVE-->>FM: CDR accepted
```

## 4. Intégration Complète - Tous les Composants

```mermaid
sequenceDiagram
    participant GIREVE as GIREVE
    participant OCPI as OCPI Gateway
    participant FM as FlexMeasures
    participant OpenLEADR as OpenLEADR
    participant CitrineOS as CitrineOS
    participant EVEREST as EVerest
    participant EV as EV
    participant BAT as Battery
    participant PV as Panel

    %% Data ingestion
    PV->>FM: Power readings (30s)
    BAT->>FM: SOC readings (30s)
    EVEREST->>CitrineOS: OCPP
    CitrineOS->>FM: EV power readings

    %% Interoperability
    FM->>GIREVE: Sessions + CDRs
    FM->>OCPI: Tariffs + Sessions
    OCPI->>OtherCPOs: Roaming

    %% Forecasting
    FM->>FM: TrainPredictPipeline (daily)
    FM->>DB: Save forecasts

    %% Scheduling (triggered by OpenADR)
    DSO->>OpenLEADR: Flexibility request
    OpenLEADR->>FM: POST /schedules/trigger
    FM->>FM: StorageScheduler optimize
    FM->>DB: Save schedule
    FM->>CitrineOS: Charging profile
    CitrineOS->>EVEREST: OCPP commands
    EVEREST->>EV: Start/stop charging
```

## 5. Architecture Réseau Docker

```mermaid
graph LR
    subgraph Docker["Docker Network: cariflex-internal"]
        FM_SRV["flexmeasures-server:5000"]
        FM_WRK["flexmeasures-worker:5000"]
        FM_DB["flexmeasures-db:5432"]
        FM_REDIS["flexmeasures-redis:6379"]
        
        CITRINEOS["citrineos-server:8080"]
        EVEREST["everest:8080"]
        OCPI_GW["ocpi-gateway:8081"]
        GIREVE_CONN["gireve-connector:8082"]
        GSY["gsy-e:8080"]
    end

    subgraph Traefik["Traefik (websecure)"]
        ROUTER_FM["Router: cariflex.digitribe.fr"]
        ROUTER_OCPI["Router: ocpi.cariflex.digitribe.fr"]
    end

    ROUTER_FM -->|"HTTPS"| FM_SRV
    ROUTER_OCPI -->|"HTTPS"| OCPI_GW
    FM_SRV -->|"REST"| FM_WRK
    FM_WRK -->|"RQ"| FM_REDIS
    FM_WRK -->|"SQL"| FM_DB
    FM_SRV -->|"SQL"| FM_DB
    
    CITRINEOS -->|"REST"| FM_SRV
    EVEREST -->|"OCPP"| CITRINEOS
    OCPI_GW -->|"REST"| FM_SRV
    GIREVE_CONN -->|"REST"| FM_SRV
    GSY -->|"REST"| FM_SRV
```

## 6. TODO - Intégrations

| Intégration | Priorité | Complexité | Statut |
|-------------|----------|------------|--------|
| **OCPI Gateway** | Haute | Élevée | 📋 À faire |
| **GIREVE Connector** | Haute | Moyenne | 📋 À faire |
| **CitrineOS ↔ FM** | Haute | Moyenne | ⏳ À faire |
| **EVerest ↔ CitrineOS** | Haute | Moyenne | ⏳ À faire |
| **OpenLEADR ↔ FM** | Haute | Faible | ⏳ À faire |
| **Grid Singularity ↔ FM** | Moyenne | Élevée | ⏳ À faire |
| **OPLEM ↔ FM** | Moyenne | Moyenne | ⏳ À faire |
| **HAMLET ↔ FM** | Basse | Élevée | ⏳ À faire |
| **MQTT Brokers Cariflex** | Haute | Moyenne | ⏳ À faire |
