diff --git a/src/index.js b/src/index.js index 1d4ca398633cd553da2ede0016c50c4133a36fd9..bbfefc10f5aad8d68f3ff08bb7f26e2b748dc7ca 100644 --- a/src/index.js +++ b/src/index.js @@ -17,13 +17,14 @@ moment.locale('fr') // set the language moment.tz.setDefault('Europe/Paris') // set the timezone /*** Connector Constants ***/ -const startDailyDate = moment() - .subtract(32, 'month') - .format('YYYY-MM-DD') +const startDailyDate = moment().subtract(32, 'month') +const startDailyDateString = startDailyDate.format('YYYY-MM-DD') const startLoadDate = moment() .subtract(7, 'day') .format('YYYY-MM-DD') -const endDate = moment().format('YYYY-MM-DD') +const startLoadDateString = startLoadDate.format('YYYY-MM-DD') +const endDate = moment() +const endDateString = endDate.format('YYYY-MM-DD') const baseUrl = 'https://gw.prd.api.enedis.fr' /** @@ -114,9 +115,9 @@ async function getDailyData(token, usagePointID) { uri: baseUrl + '/v4/metering_data/daily_consumption?start=' + - startDailyDate + + startDailyDateString + '&end=' + - endDate + + endDateString + '&usage_point_id=' + usagePointID, headers: { @@ -138,17 +139,22 @@ async function startLoadDataProcess(token, usagePointID) { const isHistory = await isHistoryLoaded('com.grandlyon.enedis.minute') if (isHistory) { log('info', 'launch process without history') - await launchLoadDataProcess(token, usagePointID, startLoadDate, endDate) + await launchLoadDataProcess( + token, + usagePointID, + startLoadDateString, + endDateString + ) } else { log('info', 'launch process with history') - const startDateToIncrement = moment().subtract(7, 'day') - const endDateToIncrement = moment() + const increamentedStartDate = startLoadDate + const incrementedEndDate = endDate for (var i = 0; i < 4; i++) { await launchLoadDataProcess( token, usagePointID, - startDateToIncrement.subtract(7 * i, 'day').format('YYYY-MM-DD'), - endDateToIncrement.subtract(7 * i, 'day').format('YYYY-MM-DD') + increamentedStartDate.subtract(7 * i, 'day').format('YYYY-MM-DD'), + incrementedEndDate.subtract(7 * i, 'day').format('YYYY-MM-DD') ) } } @@ -160,15 +166,15 @@ async function startLoadDataProcess(token, usagePointID) { async function launchLoadDataProcess( token, usagePointID, - startLoadDate, - endDate + _startLoadDate, + _endDate ) { log('info', 'Fetching enedis load data') const fetchedLoadData = await getLoadData( token, usagePointID, - startLoadDate, - endDate + _startLoadDate, + _endDate ) if (fetchedLoadData && fetchedLoadData.length > 0) { log('info', 'Process enedis load data') @@ -188,15 +194,15 @@ async function launchLoadDataProcess( * Retrieve data from the API * Format: { value: "W", "date": "YYYY-MM-DD hh:mm:ss" } */ -async function getLoadData(token, usagePointID, startDate, endDate) { +async function getLoadData(token, usagePointID, _startDate, _endDate) { const dataRequest = { method: 'GET', uri: baseUrl + '/v4/metering_data/consumption_load_curve?start=' + - startDate + + _startDate + '&end=' + - endDate + + _endDate + '&usage_point_id=' + usagePointID, headers: {