diff --git a/src/index.js b/src/index.js index bef6f9c7d97131c902970b06f0b6ecd5f3b3d26b..cda6e9cda8980ad85403eaf1125a2ad95ebc8e42 100644 --- a/src/index.js +++ b/src/index.js @@ -10,7 +10,13 @@ const moment = require('moment') require('moment-timezone') const xml2js = require('xml2js') const { buildAgregatedData } = require('./aggregate') -const { parseSgeXmlData, parseSgeXmlTechnicalData } = require('./parsing') +const { + parseSgeXmlData, + parseSgeXmlTechnicalData, + formateDataForDoctype, + parseTags, + parseValue, +} = require('./parsing') const { userTechnicalData, userMesureDetailles } = require('./request') moment.locale('fr') // set the language moment.tz.setDefault('Europe/Paris') // set the timezone @@ -211,32 +217,6 @@ async function getDataHalfHour(url, apiAuthKey, userLogin, pointId) { } } -/** - * Format tag in order to be manipulated easly - * @param {string} name - * @returns {string} name - */ -function parseTags(name) { - if (name.split(':')[1] !== undefined) { - return name.split(':')[1] - } - return name -} - -/** - * - * @param {string} value - * @param {string} name - * @returns {string|number} value - */ -function parseValue(value, name) { - // Wh => KWh - if (name === 'v') { - return parseFloat((parseInt(value) / 1000).toFixed(2)) - } - return value -} - /** * Parse data * @param {string} doctype @@ -258,15 +238,13 @@ function processData(doctype = 'com.grandlyon.enedis.day') { log('info', 'Agregate enedis daily data for month and year') if (doctype === 'com.grandlyon.enedis.day') { - console.log(processedDailyData.length) - await agregateMonthAndYearData(processedDailyData) } } } /** - * Parse data + * Store an accurate start date based on contrat start */ function processStartDate() { return async (err, result) => { @@ -296,26 +274,6 @@ async function storeData(data, doctype, filterKeys) { return filteredDocuments } -/** - * Format data for DB storage - * @param {SGEData[]} data - * @returns {Promise<EnedisKonnectorData[]>} Parsed timestamp array - */ -async function formateDataForDoctype(data) { - log('info', 'Formating data') - return data.map(record => { - let date = moment(record.d, 'YYYY/MM/DD h:mm:ss') - return { - load: record.v, - year: parseInt(date.format('YYYY')), - month: parseInt(date.format('M')), - day: parseInt(date.format('D')), - hour: parseInt(date.format('H')), - minute: parseInt(date.format('m')), - } - }) -} - /** * Agregate data from daily data to monthly and yearly data */ diff --git a/src/parsing.js b/src/parsing.js index f8c417ee7c8ecf04e13d5ee58c5be7b1320069f7..183620e1c06c10a19775e65895e31e19fe07569a 100644 --- a/src/parsing.js +++ b/src/parsing.js @@ -1,5 +1,6 @@ // @ts-check const { log } = require('cozy-konnector-libs') +const moment = require('moment') /** * Return start date @@ -29,7 +30,56 @@ function parseSgeXmlData(result) { ]['grandeur']['mesure'] } +/** + * Format data for DB storage + * @param {SGEData[]} data + * @returns {Promise<EnedisKonnectorData[]>} Parsed timestamp array + */ +async function formateDataForDoctype(data) { + log('info', 'Formating data') + return data.map(record => { + let date = moment(record.d, 'YYYY/MM/DD h:mm:ss') + return { + load: record.v, + year: parseInt(date.format('YYYY')), + month: parseInt(date.format('M')), + day: parseInt(date.format('D')), + hour: parseInt(date.format('H')), + minute: parseInt(date.format('m')), + } + }) +} + +/** + * Format tag in order to be manipulated easly + * @param {string} name + * @returns {string} name + */ +function parseTags(name) { + if (name.split(':')[1] !== undefined) { + return name.split(':')[1] + } + return name +} + +/** + * + * @param {string} value + * @param {string} name + * @returns {string|number} value + */ +function parseValue(value, name) { + // Wh => KWh + if (name === 'v') { + return parseFloat((parseInt(value) / 1000).toFixed(2)) + } + return value +} + module.exports = { parseSgeXmlData, parseSgeXmlTechnicalData, + formateDataForDoctype, + parseTags, + parseValue, }