From c259724c8db0543ad7152e09421c1a49dc33d542 Mon Sep 17 00:00:00 2001
From: Hugo SUBTIL <ext.sopra.husubtil@grandlyon.com>
Date: Mon, 2 May 2022 11:33:55 +0200
Subject: [PATCH] feat: add max power

---
 src/index.js   | 58 +++++++++++++++++++++++++++++++++++++++++++-
 src/parsing.js |  2 +-
 src/request.js | 65 ++++++++++++++++++++++++++++----------------------
 3 files changed, 95 insertions(+), 30 deletions(-)

diff --git a/src/index.js b/src/index.js
index cda6e9c..262dae5 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 183620e..64e6e7e 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 cfdef54..f524c26 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,
 }
-- 
GitLab