Skip to content
Snippets Groups Projects
index.js 7.4 MiB
Newer Older
Hugo NOUTS's avatar
Hugo NOUTS committed
/******/ (() => { // webpackBootstrap
/******/ 	var __webpack_modules__ = ([
/* 0 */
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {

// @ts-check
const {
  BaseKonnector,
  log,
  hydrateAndFilter,
  addData,
} = __webpack_require__(1)
build-token's avatar
build-token committed
const soapRequest = __webpack_require__(1331)
const moment = __webpack_require__(1373)
__webpack_require__(1510)
const xml2js = __webpack_require__(1513)
const { buildAgregatedData } = __webpack_require__(1554)
Hugo NOUTS's avatar
Hugo NOUTS committed
const {
  parseSgeXmlData,
  parseSgeXmlTechnicalData,
  formateDataForDoctype,
  parseTags,
  parseValue,
build-token's avatar
build-token committed
} = __webpack_require__(1555)
const { userMesureDetailles, userMaxPower } = __webpack_require__(1556)
const { getContractData } = __webpack_require__(1557)
Hugo NOUTS's avatar
Hugo NOUTS committed
moment.locale('fr') // set the language
moment.tz.setDefault('Europe/Paris') // set the timezone

/*** Connector Constants ***/
const manualExecution =
  process.env.COZY_JOB_MANUAL_EXECUTION === 'true' ? true : false
let startDailyDate = manualExecution
  ? moment().subtract(12, 'month')
  : moment().subtract(6, 'month')
let startDailyDateString = startDailyDate.format('YYYY-MM-DD')
const startLoadDate = moment().subtract(7, 'day')
const endDate = moment()
const endDateString = endDate.format('YYYY-MM-DD')

module.exports = new BaseKonnector(start)

// The start function is run by the BaseKonnector instance only when it got all the account
// information (fields). When you run this connector yourself in "standalone" mode or "dev" mode,
// the account information come from ./konnector-dev-config.json file
// cozyParameters are static parameters, independents from the account. Most often, it can be a
// secret api key.
async function start(fields, cozyParameters) {
  log('info', 'Gathering data ...')
  let baseUrl = fields.wso2BaseUrl
  let apiAuthKey = fields.apiToken
  let loginUtilisateur = fields.loginUtilisateur
  log('info', 'Authenticating ...')
  if (cozyParameters && Object.keys(cozyParameters).length !== 0) {
    log('debug', 'Found COZY_PARAMETERS')
    baseUrl = cozyParameters.secret.wso2BaseUrl
    apiAuthKey = cozyParameters.secret.apiToken
    loginUtilisateur = cozyParameters.secret.loginUtilisateur
  }
  //TODO: authentification ?
  log('info', 'Successfully logged in')

  log('info', 'Querying data...')
  await getDataStartDate(
    `${baseUrl}/enedis_SGE_ConsultationDonneesTechniquesContractuelles/1.0`,
    apiAuthKey,
    loginUtilisateur,
    fields.pointId
  )
  await getData(
    `${baseUrl}/enedis_SGE_ConsultationMesuresDetaillees/1.0`,
    apiAuthKey,
    loginUtilisateur,
    fields.pointId
  )
  await getMaxPowerData(
    `${baseUrl}/enedis_SGE_ConsultationMesuresDetaillees/1.0`,
    apiAuthKey,
    loginUtilisateur,
    fields.pointId
  )
  await getDataHalfHour(
    `${baseUrl}/enedis_SGE_ConsultationMesuresDetaillees/1.0`,
    apiAuthKey,
    loginUtilisateur,
    fields.pointId
  )
  log('info', 'Querying data: done')
}
/**
 *
 * @param {string} url
 * @param {string} apiAuthKey
 * @param {string} userLogin
 * @param {number} pointId
 */
async function getDataStartDate(url, apiAuthKey, userLogin, pointId) {
  log('info', 'Fetching data start date')
build-token's avatar
build-token committed
  const { response } = await getContractData(
    url,
    apiAuthKey,
    userLogin,
    pointId
Hugo NOUTS's avatar
Hugo NOUTS committed
  )
build-token's avatar
build-token committed
  const parsingData = await new Promise((resolve, reject) => {
    xml2js.parseString(
      response.body,
      {
        tagNameProcessors: [parseTags],
        valueProcessors: [parseValue],
        explicitArray: false,
      },
      (err, result) => {
        if (err) reject(err)
        else resolve(result)
      }
    )
  })
  processStartDate(parsingData)
Hugo NOUTS's avatar
Hugo NOUTS committed
}

/**
 * Get hour data
 * @param {string} url
 * @param {string} apiAuthKey
 * @param {string} userLogin
 * @param {number} pointId
 */
async function getData(url, apiAuthKey, userLogin, pointId) {
  log('info', 'Fetching data')
  const sampleHeaders = {
    'Content-Type': 'text/xml;charset=UTF-8',
    apikey: apiAuthKey,
  }

  setStartDate()

  const { response } = await soapRequest({
    url: url,
    headers: sampleHeaders,
    xml: userMesureDetailles(
      pointId,
      userLogin,
      startDailyDateString,
      endDateString
    ),
  }).catch(err => {
    log('error', 'userMesureDetailles')
    log('error', err)
    return err
  })

  xml2js.parseString(
    response.body,
    {
      tagNameProcessors: [parseTags],
      valueProcessors: [parseValue],
      explicitArray: false,
    },
    processData()
  )
}

/**
 * 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,
  }

  setStartDate()

  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')
  )
}

Loading
Loading full blame...