Skip to content
Snippets Groups Projects
enedis.md 8.65 KiB
Newer Older
  • Learn to ignore specific revisions
  • Guilhem CARRON's avatar
    Guilhem CARRON committed
    This konnector fetches consumptions measures from Enedis API. This is an Oauth Konnector, meaning the authentification performed to access all data is made following an Oauth2 protocol.
    You can clone the project [here](https://forge.grandlyon.com/web-et-numerique/llle_project/enedis-konnector).
    
    You should also check Cozy's official documentations for konnectors :
    [https://docs.cozy.io/en/tutorials/konnector/getting-started/](https://docs.cozy.io/en/tutorials/konnector/getting-started/)
    [https://docs.cozy.io/en/tutorials/konnector/oauth/](https://docs.cozy.io/en/tutorials/konnector/oauth/)
    
    ## Enedis Konnector
    
    
    Hugo's avatar
    Hugo committed
    The Oauth protocol does not take place in the konnector code, therefore it is also important to take a look at the [proxy](../../proxy/description.md) documentation to fully understand all the interactions that will be told below.
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    
    All the actions performed by the stack are targetted from pre-registered paramaters, here is the list of all parameters needed by the stack to perform the Oauth protocol and allow the konnector to fetch data.
    On its first launch, following the Oauth Client Connect authentification.
    
    
    - The cozy stack calls the **authentification_endpoint** and start the oauth protocol, see [proxy doc](../../proxy/description.md).
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    - The account has now an access_token and an id_token from the oauth call
    
    !!! info ""
    
    id token is only given when requesting the token endpoint in \_authorization_code grant_type.
    This token holds several meta datas, including the usage_point_id (id of user's meter) that will be needed further to fetch user's datas.
    
    Hugo's avatar
    Hugo committed
    - Konnector starts, if no usage_point_id are found in database or fields then proceed as a first launch. Store the usage_point_id in oauth_callback_result in db (see [addData](https://docs.cozy.io/en/cozy-konnector-libs/api/#adddata_1)).
    
    - Konnector finds usage_point_id when starting but token is expired. Proceed to refresh token and restart konnector with usage_point_id in fields params (see _Troubleshooting when asking for refresh token_ section at the end of the page).
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    
    ## Enedis API
    
    API Url for Production : [https://gw.prd.api.enedis.fr](https://gw.prd.api.enedis.fr)
    
    API url for Development : [https://gw.hml.api.enedis.fr](https://gw.hml.api.enedis.fr)
    
    Once you've finished the OAuth protocol and have a valid token, you can go fetch the data. Currently we use the two following endpoints in Ecolyo:
    
    Method : **GET**
    
    Route : **/v4/metering_data/consumption_load_curve**
    
    Method : **GET**
    
    Route : **/v4/metering_data/daily_consumption**
    
    ```json
    "params":
    {
    	"usage_point_id": <your-usage-point-id>,
        "start": "YYYY-MM-DD",
        "end": "YYYY-MM-DD",
    }
    ```
    
    ```json
    "headers":
    {
    	"Accept": "application/json",
        "Authorization": "Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    }
    ```
    
    For more information about Datahub-Enedis API, checkout the [Datahub documentation](https://datahub-enedis.fr/data-connect/documentation/metering-data-v4/).
    
    ### Konnector Methods
    
    | Method                        | Description                                                                                                                                                                                                                                                                               |
    | ----------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
    | start                         | Init Function                                                                                                                                                                                                                                                                             |
    | getDailyData                  | Retrieve data from the API. Format: `{ value: "Wh", "date": "YYYY-MM-DD" }`"                                                                                                                                                                                                              |
    | startLoadDataProcess          | Check if history is loaded. If not, call several time the api to retrieve 1 month of history for load data. If yes only call once the api                                                                                                                                                 |
    | launchLoadDataProcess         | Launch process to handle load data                                                                                                                                                                                                                                                        |
    | getLoadData                   | Retrieve data from the API. Format: `{ value: "W", "date": "YYYY-MM-DD hh:mm:ss" }`                                                                                                                                                                                                       |
    | processData                   | Parse data. Remove existing data from DB using hydrateAndFilter, store filtered data and return the list of filtered data                                                                                                                                                                 |
    | agregateMonthAndYearData      | Agregate data from daily data to monthly and yearly data                                                                                                                                                                                                                                  |
    | agregateHourlyData            | Agregate data from load data (every 30 min) to Hourly data                                                                                                                                                                                                                                |
    | storeData                     | Save data in the right doctype db and prevent duplicated keys                                                                                                                                                                                                                             |
    | formateData                   | Format data for DB storage and remove bad data                                                                                                                                                                                                                                            |
    | buildAgregatedData            | Retrieve and remove old data for a specific doctype and return an array of agregated data                                                                                                                                                                                                 |
    | buildDataFromKey              | Format an entry for DB storage using key and value. For year doctype: key = "YYYY". For month doctype: key = "YYYY-MM"                                                                                                                                                                    |
    | isHistoryLoaded               | Function checking if the history is loaded                                                                                                                                                                                                                                                |
    | resetInProgressAggregatedData | Function handling special case.The temporary aggregated data need to be remove in order for the most recent one te be saved. Ex for com.grandlyon.enedis.year : `{ load: 76.712, year: 2020, ... }` need to be replace by `{ load: 82.212, year: 2020, ... }` after enedis data reprocess |
    
    Hugo's avatar
    Hugo committed
    
    ## Troubleshooting when asking for refresh token
    
    When a token expires, the konnector will encounter an error when fetching user data, leading to a refresh token request.
    
    
    Since refresh token requests restart the Konnector, it was decided to add the `usage_point_id` into the start fields when asking for a refresh token (see line 97).
    
    Hugo's avatar
    Hugo committed
    
    !!! info "restart to refresh token"
    
    Line 85: `` await cozyClient.fetchJSON( 'POST', `/accounts/enedisgrandlyon/${accountId}/refresh` ) ``
    
    Hugo's avatar
    Hugo committed
    
        Line 97: ```fields.usage_point_id = usage_point_id```
    
        Line 98: ```return start(fields, cozyParameters, false)```
    
    
    In fact the usage_point_id still remains in the account collection, but on restart, the konnector loses `this._account` context leading to an error when searching for usage_point_id in account.