diff --git a/index.js b/index.js index c0a5e5c22b137b6e8e2a221822cdebe95d6bd96d..28751463eb8ae57d7bb6658c5b5406c45083627a 100644 --- a/index.js +++ b/index.js @@ -107,13 +107,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' /** @@ -204,9 +205,9 @@ async function getDailyData(token, usagePointID) { uri: baseUrl + '/v4/metering_data/daily_consumption?start=' + - startDailyDate + + startDailyDateString + '&end=' + - endDate + + endDateString + '&usage_point_id=' + usagePointID, headers: { @@ -228,15 +229,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') - for (var i = 1; i < 5; i++) { + const increamentedStartDate = startLoadDate + const incrementedEndDate = endDate + for (var i = 0; i < 4; i++) { await launchLoadDataProcess( token, usagePointID, - startLoadDate.subtract(7 * i, 'day'), - endDate.subtract(7 * i, 'day') + increamentedStartDate.subtract(7 * i, 'day').format('YYYY-MM-DD'), + incrementedEndDate.subtract(7 * i, 'day').format('YYYY-MM-DD') ) } } @@ -248,15 +256,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') @@ -276,15 +284,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: {