Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
web-et-numerique
LLLE_Project
grdf-konnector
Commits
754316bc
Commit
754316bc
authored
Oct 20, 2020
by
Yoan VALLET
Browse files
rework of konnector
parent
229cd21c
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/index.js
View file @
754316bc
...
...
@@ -17,31 +17,12 @@ moment.locale('fr') // set the language
moment
.
tz
.
setDefault
(
'
Europe/Paris
'
)
// set the timezone
const
startDate
=
moment
()
.
subtract
(
1
,
'
year
'
)
.
format
(
'
YYYY-MM-DD
'
)
const
endDate
=
moment
()
.
startOf
(
'
day
'
)
.
subtract
(
4
,
'
day
'
)
.
subtract
(
1
,
'
day
'
)
.
format
(
'
YYYY-MM-DD
'
)
const
endDate
=
moment
().
format
(
'
YYYY-MM-DD
'
)
// const baseUrl = 'https://api.grdf.fr/adict/v1'
//console.log(startDate)
//console.log(endDate)
/*const timeRange = ['day', 'month', 'year']
const rangeDate = {
day: {
doctype: 'com.grandlyon.grdf.day',
keys: ['year', 'month', 'day']
},
month: {
doctype: 'com.grandlyon.grdf.month',
keys: ['year', 'month']
},
year: {
doctype: 'com.grandlyon.grdf.year',
keys: ['year']
}
}
const client_id = 'metropole_de_lyon_grdf'
const redirectUrl = 'https://grdf.pchugo.wf.alpha.grandlyon.com'*/
module
.
exports
=
new
BaseKonnector
(
start
)
...
...
@@ -50,44 +31,30 @@ module.exports = new BaseKonnector(start)
// 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
,
doRetry
=
true
)
{
// var grdfPCE = ''
async
function
start
(
fields
,
cozyParameters
,
doRetry
=
false
)
{
log
(
'
debug
'
,
'
Starting grdf adict konnector
'
)
// if (cozyParameters) log('info', 'CozyParameters found')
// const grdfToken = await authenticate(fields.login, fields.password)
const
accountId
=
getAccountId
()
let
id_pce
=
''
try
{
// const low_scope_access_token = fields.access_token // To use for /donnees-techniques -> fréquence du compteur
const
high_scope_access_token
=
await
authenticate
(
fields
.
client_id
,
fields
.
client_secret
body
=
await
cozyClient
.
fetchJSON
(
'
POST
'
,
`/accounts/grdfgrandlyon/
${
accountId
}
/refresh`
)
// is account necessary ? maybe id_token is already in fields from the begining
log
(
'
debug
'
,
'
KONNECTOR FIELDS :
'
+
high_scope_access_token
)
log
(
'
debug
'
,
'
KONNECTOR ACCOUNT :
'
+
this
.
_account
.
oauth_callback_results
.
id_token
)
fields
.
access_token
=
body
.
attributes
.
oauth
.
access_token
if
(
this
.
_account
&&
this
.
_account
.
oauth_callback_results
&&
this
.
_account
.
oauth_callback_results
.
id_token
this
.
_account
.
oauth_callback_results
.
pce
&&
fields
.
access_token
)
{
log
(
'
debug
'
,
'
time to decode jwt
'
)
// getPCEid(fields.id_token) ?
const
id_pce
=
await
getPCEid
(
this
.
_account
.
oauth_callback_results
.
id_token
)
//19108248691849 PCEid FOR TESTING PURPOSE
log
(
'
debug
'
,
'
Fetching grdf adict daily data
'
)
id_pce
=
this
.
_account
.
oauth_callback_results
.
pce
log
(
'
debug
'
,
'
THE ID_PCE SENDING TO GETDATA :
'
+
id_pce
)
const
grdfData
=
await
getData
(
high_scope_
access_token
,
id_pce
)
const
grdfData
=
await
getData
(
fields
.
access_token
,
id_pce
)
if
(
grdfData
.
consommation
)
{
if
(
grdfData
)
{
log
(
'
debug
'
,
'
Process grdf daily data
'
)
const
processedLoadData
=
await
processData
(
grdfData
,
...
...
@@ -99,8 +66,6 @@ async function start(fields, cozyParameters, doRetry = true) {
}
else
{
log
(
'
debug
'
,
'
No consent or data for load curve
'
)
}
// await addData(data, "io.cozy.accounts", accountId);
// return start(fields, cozyParameters, true);
}
else
{
log
(
'
debug
'
,
'
no id_token found in oauth_callback_results
'
)
log
(
...
...
@@ -111,79 +76,12 @@ async function start(fields, cozyParameters, doRetry = true) {
throw
errors
.
USER_ACTION_NEEDED_OAUTH_OUTDATED
}
}
catch
(
err
)
{
if
(
err
.
statusCode
===
403
||
err
.
code
===
403
||
err
.
statusCode
===
401
||
err
.
code
===
401
)
{
if
(
doRetry
)
{
log
(
'
debug
'
,
'
asking refresh from the stack
'
)
let
body
try
{
body
=
await
cozyClient
.
fetchJSON
(
'
POST
'
,
`/accounts/grdf-konnector/
${
accountId
}
/refresh`
)
}
catch
(
err
)
{
log
(
'
debug
'
,
`Error during refresh
${
err
.
message
}
`
)
throw
errors
.
USER_ACTION_NEEDED_OAUTH_OUTDATED
}
log
(
'
debug
'
,
'
refresh response
'
)
log
(
'
debug
'
,
JSON
.
stringify
(
body
))
fields
.
access_token
=
body
.
attributes
.
oauth
.
access_token
return
start
(
fields
,
cozyParameters
,
false
)
}
log
(
'
error
'
,
`Error during authentication:
${
err
.
message
}
`
)
throw
errors
.
VENDOR_DOWN
}
else
{
log
(
'
error
'
,
'
caught an unexpected error
'
)
log
(
'
error
'
,
err
.
message
)
throw
errors
.
VENDOR_DOWN
}
}
}
async
function
authenticate
(
client_id
,
client_secret
)
{
let
myHeaders
=
new
Headers
()
myHeaders
.
append
(
'
Content-Type
'
,
'
application/x-www-form-urlencoded
'
)
myHeaders
.
append
(
'
Authorization
'
,
'
Basic cG9jX2FwaTpwb2NfYXBp
'
)
let
urlencoded
=
new
URLSearchParams
()
urlencoded
.
append
(
'
grant_type
'
,
'
client_credentials
'
)
urlencoded
.
append
(
'
client_id
'
,
client_id
)
urlencoded
.
append
(
'
client_secret
'
,
client_secret
)
urlencoded
.
append
(
'
scope
'
,
'
/adict/v1
'
)
let
requestOptions
=
{
method
:
'
POST
'
,
headers
:
myHeaders
,
body
:
urlencoded
,
redirect
:
'
follow
'
log
(
'
error
'
,
'
caught an unexpected error
'
)
log
(
'
error
'
,
err
.
message
)
throw
errors
.
VENDOR_DOWN
}
const
rep
=
await
fetch
(
'
https://sofit-sso-oidc.grdf.fr/openam/oauth2/realms/externeGrdf/access_token
'
,
requestOptions
)
.
then
(
response
=>
response
.
text
())
.
then
(
result
=>
{
result
=
JSON
.
parse
(
result
)
log
(
'
info
'
,
result
)
return
result
.
access_token
})
.
catch
(
error
=>
log
(
'
error
'
,
error
))
return
rep
}
async
function
getPCEid
(
tokenToDecrypt
)
{
// verify a token symmetric - synchronous
log
(
'
debug
'
,
'
GETPCEID :
'
+
tokenToDecrypt
)
var
decoded
=
jwt
.
decode
(
tokenToDecrypt
)
const
decodedConsentements
=
JSON
.
parse
(
decoded
.
consentements
)
return
decodedConsentements
[
0
].
pce
}
//Retrieve data from grdf API
async
function
getData
(
token
,
idPCE
)
{
log
(
'
debug
'
,
'
ENTERING GETDATA
'
)
...
...
@@ -203,13 +101,24 @@ async function getData(token, idPCE) {
startDate
+
'
&date_fin=
'
+
endDate
log
(
'
debug
'
,
'
GET DATA URL
'
+
url
)
const
rep
=
await
fetch
(
url
,
requestOptions
)
.
then
(
response
=>
response
.
text
())
.
then
(
async
response
=>
{
if
(
response
.
status
!==
200
)
{
throw
new
Error
(
response
.
status
+
'
-
'
+
response
.
statusText
)
}
return
await
response
.
text
()
})
.
then
(
result
=>
{
result
=
JSON
.
parse
(
result
)
return
result
return
result
.
match
(
/.+/g
).
map
(
s
=>
{
result
=
JSON
.
parse
(
s
)
return
result
.
consommation
})
})
.
catch
(
error
=>
{
log
(
'
debug
'
,
'
Error from getData
'
)
throw
error
})
.
catch
(
error
=>
log
(
'
error
'
,
error
))
return
rep
}
...
...
@@ -220,13 +129,13 @@ async function getData(token, idPCE) {
* Return the list of filtered data
*/
async
function
processData
(
data
,
doctype
,
filterKeys
)
{
const
intervalData
=
data
.
consommation
const
formatedData
=
await
formateData
(
intervalData
)
const
dataArray
=
[
formatedData
]
const
formatedData
=
await
formateData
(
data
)
log
(
'
debug
'
,
'
processData - data formated
'
)
// Remove data for existing days into the DB
const
filteredData
=
await
hydrateAndFilter
(
dataArray
,
doctype
,
{
const
filteredData
=
await
hydrateAndFilter
(
formatedData
,
doctype
,
{
keys
:
filterKeys
})
log
(
'
debug
'
,
'
processData - data filtered
'
)
// Store new day data
await
storeData
(
filteredData
,
doctype
,
filterKeys
)
return
filteredData
...
...
@@ -249,70 +158,27 @@ async function storeData(data, doctype, filterKeys) {
*/
async
function
formateData
(
data
)
{
log
(
'
debug
'
,
'
Formating data
'
)
let
date
=
moment
(
data
.
date_debut_consommation
,
'
YYYY/MM/DD h:mm:ss
'
)
let
load
=
data
.
energie
!==
0
?
data
.
energie
:
data
.
volume_brut
*
data
.
coeff_calcul
.
coeff_conversion
//TRAITEMENT DES MINUTES
// const load =
// doctype === 'com.grandlyon.grdf.minute'
// ? record.value / 2
// : record.value
// if (doctype === 'com.grandlyon.grdf.minute') {
// date = date.subtract(30, 'minute')
// }
return
{
load
:
parseFloat
(
load
),
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
'
))
}
return
data
.
map
(
record
=>
{
let
date
=
moment
(
record
.
date_debut_consommation
,
'
YYYY/MM/DD h:mm:ss
'
)
let
load
=
record
.
energie
!==
0
?
record
.
energie
:
record
.
volume_brut
*
record
.
coeff_calcul
.
coeff_conversion
return
{
load
:
parseFloat
(
load
),
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 load data (every 30 min) to Hourly data
*/
/*async function agregateHourlyData(data) {
// Sum year and month values into object with year or year-month as keys
if (data && data.length > 0) {
let hourData = {}
data.forEach(element => {
let key =
element.year +
'-' +
element.month +
'-' +
element.day +
'-' +
element.hour
key in hourData
? (hourData[key] += element.load)
: (hourData[key] = element.load)
})
// Agregation for Month data
const agregatedMonthData = await buildAgregatedData(
hourData,
'com.grandlyon.grdf.hour'
)
await storeData(agregatedMonthData, 'com.grandlyon.grdf.hour', [
'year',
'month',
'day',
'hour'
])
}
}*/
/**
* Agregate data from daily data to monthly and yearly data
*/
async
function
agregateMonthAndYearData
(
data
)
{
//console.log('agregateMonthAndYearData')
//console.log(data)
// Sum year and month values into object with year or year-month as keys
if
(
data
&&
data
.
length
!==
0
)
{
let
monthData
=
{}
...
...
@@ -326,16 +192,11 @@ async function agregateMonthAndYearData(data) {
?
(
yearData
[
element
.
year
]
+=
element
.
load
)
:
(
yearData
[
element
.
year
]
=
element
.
load
)
})
//console.log(monthData)
//console.log(yearData)
// Agregation for Month data
const
agregatedMonthData
=
await
buildAgregatedData
(
monthData
,
'
com.grandlyon.grdf.month
'
)
//console.log('agregatedMonthData')
//console.log(agregatedMonthData)
await
storeData
(
agregatedMonthData
,
'
com.grandlyon.grdf.month
'
,
[
'
year
'
,
'
month
'
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment