diff --git a/src/index.js b/src/index.js index 5b4ccc28290d467ab057c2134c3e221d7daa6536..ddd8a6e968683ac8cc62be677a55236bd2a18a76 100644 --- a/src/index.js +++ b/src/index.js @@ -69,20 +69,8 @@ async function start(fields, cozyParameters, doRetry = true) { log('info', 'Agregate enedis daily data for month and year') await agregateMonthAndYearData(processedDailyData) - log('info', 'Fetching enedis load data') - const fetchedLoadData = await getLoadData(access_token, usage_point_id) - if (fetchedLoadData && fetchedLoadData.length > 0) { - log('info', 'Process enedis load data') - const processedLoadData = await processData( - fetchedLoadData, - 'com.grandlyon.enedis.minute', - ['year', 'month', 'day', 'hour', 'minute'] - ) - log('info', 'Agregate enedis load data for hour') - await agregateHourlyData(processedLoadData) - } else { - log('info', 'No consent or data for load curve') - } + log('info', 'Process enedis load data') + await startLoadDataProcess(access_token, usage_point_id) } catch (err) { if (err.statusCode === 403 || err.code === 403) { if (!fields.refresh_token) { @@ -140,17 +128,70 @@ async function getDailyData(token, usagePointID) { return response } +/** + * 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 + */ +async function startLoadDataProcess(token, usagePointID) { + const isHistory = await isHistoryLoaded('com.grandlyon.enedis.hour') + if (isHistory) { + log('info', 'launc process without history') + await launchLoadDataProcess(token, usagePointID, startLoadDate, endDate) + } else { + log('info', 'launc process with history') + for (var i = 1; i < 5; i++) { + await launchLoadDataProcess( + token, + usagePointID, + startLoadDate.subtract(7 * i, 'day'), + endDate.subtract(7 * i, 'day') + ) + } + } +} + +/** + * Launch process to handle load data + */ +async function launchLoadDataProcess( + token, + usagePointID, + startLoadDate, + endDate +) { + log('info', 'Fetching enedis load data') + const fetchedLoadData = await getLoadData( + token, + usagePointID, + startLoadDate, + endDate + ) + if (fetchedLoadData && fetchedLoadData.length > 0) { + log('info', 'Process enedis load data') + const processedLoadData = await processData( + fetchedLoadData, + 'com.grandlyon.enedis.minute', + ['year', 'month', 'day', 'hour', 'minute'] + ) + log('info', 'Agregate enedis load data for hour') + await agregateHourlyData(processedLoadData) + } else { + log('info', 'No consent or data for load curve') + } +} + /** * Retrieve data from the API * Format: { value: "W", "date": "YYYY-MM-DD hh:mm:ss" } */ -async function getLoadData(token, usagePointID) { +async function getLoadData(token, usagePointID, startDate, endDate) { const dataRequest = { method: 'GET', uri: baseUrl + '/v4/metering_data/consumption_load_curve?start=' + - startLoadDate + + startDate + '&end=' + endDate + '&usage_point_id=' + @@ -352,6 +393,27 @@ async function buildDataFromKey(doctype, key, value) { } } +/** + * Function checking if the history is loaded + */ +async function isHistoryLoaded(doctype) { + log('debug', doctype, 'Retrieve data') + const result = await cozyClient.data.findAll(doctype) + if (result && result.length > 0) { + const filtered = result.filter(function(el) { + return ( + el.year <= startLoadDate.year && + el.month <= startLoadDate.month && + el.day <= startLoadDate.day + ) + }) + if (filtered.length > 0) { + return false + } + } + return true +} + /** * Function handling special case. * The temporary aggregated data need to be remove in order for the most recent one te be saved.