diff --git a/index.js b/index.js index 466e58e12b0390d08f12debaf6b84f41450e6c6b..2ea2e6ee4d891e2b0af802138d215d5e6b463dbc 100644 --- a/index.js +++ b/index.js @@ -159,8 +159,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') - await processLoadData(access_token, usage_point_id) + 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) { @@ -223,34 +223,66 @@ async function getDailyData(token, usagePointID) { * 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 processLoadData(token, usagePointID) { - const isHistoryLoaded = await isHistoryLoaded('com.grandlyon.enedis.hour') - 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'] +async function startLoadDataProcess(token, usagePointID) { + log('info', 'Check history') + const isHistory = await isHistoryLoaded('com.grandlyon.enedis.hour') + if (isHistory) { + log('info', 'launch process without history') + await launchLoadDataProcess(token, usagePointID, startLoadDate, endDate) + } else { + log('info', 'launch process with history') + for (var i = 1; i < 5; i++) { + await launchLoadDataProcess( + token, + usagePointID, + startLoadDate.subtract(7 * i, 'day'), + endDate.subtract(7 * i, 'day') ) - log('info', 'Agregate enedis load data for hour') - await agregateHourlyData(processedLoadData) - } else { - log('info', 'No consent or data for load curve') } + } +} + +/** + * 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=' + @@ -460,9 +492,11 @@ async function isHistoryLoaded(doctype) { 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 + return ( + el.year <= startLoadDate.year && + el.month <= startLoadDate.month && + el.day <= startLoadDate.day + ) }) if (filtered.length > 0) { return false