diff --git a/src/index.js b/src/index.js index cda6e9cda8980ad85403eaf1125a2ad95ebc8e42..262dae5f3b81c3820e235900d9795826a766289b 100644 --- a/src/index.js +++ b/src/index.js @@ -17,7 +17,11 @@ const { parseTags, parseValue, } = require('./parsing') -const { userTechnicalData, userMesureDetailles } = require('./request') +const { + userTechnicalData, + userMesureDetailles, + userMaxPower, +} = require('./request') moment.locale('fr') // set the language moment.tz.setDefault('Europe/Paris') // set the timezone @@ -67,6 +71,12 @@ async function start(fields, cozyParameters) { loginUtilisateur, fields.pointId ) + await getMaxPowerData( + `${baseUrl}/enedis_SGE_ConsultationMesuresDetaillees/1.0`, + apiAuthKey, + loginUtilisateur, + fields.pointId + ) await getDataHalfHour( `${baseUrl}/enedis_SGE_ConsultationMesuresDetaillees/1.0`, apiAuthKey, @@ -161,6 +171,52 @@ async function getData(url, apiAuthKey, userLogin, pointId) { ) } +/** + * Get Max power data + * @param {string} url + * @param {string} apiAuthKey + * @param {string} userLogin + * @param {number} pointId + */ +async function getMaxPowerData(url, apiAuthKey, userLogin, pointId) { + log('info', 'Fetching Max Power data') + const sampleHeaders = { + 'Content-Type': 'text/xml;charset=UTF-8', + apikey: apiAuthKey, + } + + // If start date exceed the maximum amount of data we can get with one query + // get only 24 month + if (moment(endDate).diff(startDailyDate, 'months', true) > 24) { + log( + 'info', + 'Start date exceed 24 month, setting start date to current date minus 24 month' + ) + startDailyDate = moment(endDate).subtract(24, 'month') + startDailyDateString = startDailyDate.format('YYYY-MM-DD') + } + + const { response } = await soapRequest({ + url: url, + headers: sampleHeaders, + xml: userMaxPower(pointId, userLogin, startDailyDateString, endDateString), + }).catch(err => { + log('error', 'getMaxPowerData') + log('error', err) + return err + }) + + xml2js.parseString( + response.body, + { + tagNameProcessors: [parseTags], + valueProcessors: [parseValue], + explicitArray: false, + }, + processData('com.grandlyon.enedis.maxpower') + ) +} + /** * Get half-hour data * @param {string} url diff --git a/src/parsing.js b/src/parsing.js index 183620e1c06c10a19775e65895e31e19fe07569a..64e6e7e57bd7e034d79bc4aceac97bce4ec11081 100644 --- a/src/parsing.js +++ b/src/parsing.js @@ -18,7 +18,7 @@ function parseSgeXmlTechnicalData(result) { } /** - * + * Parsing SGE xml reply to get only mesure data * @param {string} result * @returns {SGEData[]} */ diff --git a/src/request.js b/src/request.js index cfdef54bd46ce4e0f30edefc664007f6b8460bb0..f524c262afac6f4c2c0f4ee739c6c9a4438f7732 100644 --- a/src/request.js +++ b/src/request.js @@ -46,33 +46,6 @@ function userMesureDetailles( ` } -function userMesureDetaillesHalfHour(pointId, userLogin, startDt, endDt) { - log('info', `Query data between ${startDt} and ${endDt}`) - return `<?xml version='1.0' encoding='utf-8'?> - <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" - xmlns:v2="http://www.enedis.fr/sge/b2b/services/consultationmesuresdetaillees/v2.0" - xmlns:v1="http://www.enedis.fr/sge/b2b/technique/v1.0"> - <soapenv:Header/> - <soapenv:Body> - <v2:consulterMesuresDetaillees> - <demande> - <initiateurLogin>${userLogin}</initiateurLogin> - <pointId>${pointId}</pointId> - <mesuresTypeCode>COURBE</mesuresTypeCode> - <grandeurPhysique>PA</grandeurPhysique> - <soutirage>true</soutirage> - <injection>false</injection> - <dateDebut>${startDt}</dateDebut> - <dateFin>${endDt}</dateFin> - <mesuresCorrigees>false</mesuresCorrigees> - <accordClient>true</accordClient> - </demande> - </v2:consulterMesuresDetaillees> - </soapenv:Body> - </soapenv:Envelope> - ` -} - /** * Get user technical data * @param {number} pointId @@ -97,8 +70,44 @@ function userTechnicalData(pointId, userLogin) { ` } +/** + * Get user max power + * @param {number} pointId + * @param {string} userLogin + * @param {string} startDt + * @param {string} endDt + * @returns {string} + */ +function userMaxPower(pointId, userLogin, startDt, endDt) { + log('info', `Query userMesureDetailles`) + return `<?xml version='1.0' encoding='utf-8'?> + <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" + xmlns:v2="http://www.enedis.fr/sge/b2b/services/consultationmesuresdetaillees/v2.0" + xmlns:v1="http://www.enedis.fr/sge/b2b/technique/v1.0"> + <soapenv:Header/> + <soapenv:Body> + <v2:consulterMesuresDetaillees> + <demande> + <initiateurLogin>${userLogin}</initiateurLogin> + <pointId>${pointId}</pointId> + <mesuresTypeCode>PMAX</mesuresTypeCode> + <grandeurPhysique>PMA</grandeurPhysique> + <soutirage>true</soutirage> + <injection>false</injection> + <dateDebut>${startDt}</dateDebut> + <dateFin>${endDt}</dateFin> + <mesuresPas>P1D</mesuresPas> + <mesuresCorrigees>false</mesuresCorrigees> + <accordClient>true</accordClient> + </demande> + </v2:consulterMesuresDetaillees> + </soapenv:Body> + </soapenv:Envelope> + ` +} + module.exports = { userTechnicalData, - userMesureDetaillesHalfHour, + userMaxPower, userMesureDetailles, }