Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
tsm-orchestration
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
UFZ TSM
tsm-orchestration
Commits
54c4675a
Commit
54c4675a
authored
1 month ago
by
Florian Gransee
Browse files
Options
Downloads
Plain Diff
Merge branch '423-add-tsystems-api' into 'main'
423 add tsystems api script See merge request
!242
parents
3c9af162
ac8207b3
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!242
423 add tsystems api script
Pipeline
#487448
failed
1 month ago
Stage: integration
Stage: end-to-end
Changes
1
Pipelines
3
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
cron/scripts/ext_api_sync/tsystems_api_sync.py
+119
-0
119 additions, 0 deletions
cron/scripts/ext_api_sync/tsystems_api_sync.py
with
119 additions
and
0 deletions
cron/scripts/ext_api_sync/tsystems_api_sync.py
0 → 100755
+
119
−
0
View file @
54c4675a
#! /usr/bin/env python3
import
requests
import
click
import
logging
import
json
import
os
import
mqtt
from
datetime
import
datetime
,
timedelta
,
timezone
api_base_url
=
os
.
environ
.
get
(
"
DB_API_BASE_URL
"
)
tsystems_base_url
=
(
"
https://moc.caritc.de/sensorstation-management/api/measurements/average
"
)
def
unix_ts_to_str
(
ts_unix
:
int
)
->
str
:
"""
Convert unix timestamp to datetime string
"""
dt
=
datetime
.
fromtimestamp
(
ts_unix
,
tz
=
timezone
.
utc
)
ts_str
=
dt
.
strftime
(
"
%Y-%m-%d %H:%M:%S
"
)
return
ts_str
def
get_bearer_token
(
username
:
str
,
password
:
str
)
->
str
:
"""
Get bearer token for API authentication
"""
auth_url
=
"
https://lcmm.caritc.de/auth/realms/lcmm/protocol/openid-connect/token
"
headers
=
{
"
Content-Type
"
:
"
application/x-www-form-urlencoded
"
,
}
payload
=
{
"
client_id
"
:
"
lcmm
"
,
"
grant_type
"
:
"
password
"
,
"
username
"
:
username
,
"
password
"
:
password
,
}
response
=
requests
.
post
(
auth_url
,
headers
=
headers
,
data
=
payload
)
response
.
raise_for_status
()
return
response
.
json
()[
"
access_token
"
]
def
get_utc_timerange
():
now_utc
=
datetime
.
now
(
timezone
.
utc
)
now_str
=
now_utc
.
strftime
(
"
%Y-%m-%dT%H:%M:%SZ
"
)
timestamp_from
=
now_utc
-
timedelta
(
minutes
=
60
)
timestamp_from_str
=
timestamp_from
.
strftime
(
"
%Y-%m-%dT%H:%M:%SZ
"
)
return
timestamp_from_str
,
now_str
def
request_tsystems_api
(
group
:
str
,
station_id
:
str
,
username
:
str
,
password
:
str
)
->
list
:
bearer_token
=
get_bearer_token
(
username
,
password
)
time_from
,
time_to
=
get_utc_timerange
()
headers
=
{
"
Accept
"
:
"
*/*
"
,
"
Authorization
"
:
f
"
Bearer
{
bearer_token
}
"
}
params
=
{
"
aggregationTime
"
:
"
HOURLY
"
,
"
aggregationValues
"
:
"
ALL_FIELDS
"
,
"
from
"
:
time_from
,
"
to
"
:
time_to
,
}
response
=
requests
.
get
(
f
"
{
tsystems_base_url
}
/
{
group
}
/
{
station_id
}
"
,
headers
=
headers
,
params
=
params
)
response
.
raise_for_status
()
return
response
.
json
()
def
parse_api_response
(
response
:
list
)
->
dict
:
bodies
=
[]
for
entry
in
response
:
source
=
{
"
sensor_id
"
:
entry
.
pop
(
"
deviceId
"
),
"
aggregation_time
"
:
"
hourly
"
}
timestamp
=
entry
.
pop
(
"
timestamp
"
)
for
parameter
,
value
in
entry
.
items
():
if
value
:
body
=
{
"
result_time
"
:
unix_ts_to_str
(
timestamp
),
"
result_type
"
:
0
,
"
result_number
"
:
value
,
"
datastream_pos
"
:
parameter
,
"
parameters
"
:
json
.
dumps
(
{
"
origin
"
:
"
tsystems_data
"
,
"
column_header
"
:
source
}
),
}
bodies
.
append
(
body
)
return
{
"
observations
"
:
bodies
}
@click.command
()
@click.argument
(
"
thing_uuid
"
)
@click.argument
(
"
parameters
"
)
@click.argument
(
"
target_uri
"
)
def
main
(
thing_uuid
,
parameters
,
target_uri
):
logging
.
basicConfig
(
level
=
os
.
environ
.
get
(
"
LOG_LEVEL
"
,
"
INFO
"
).
upper
())
params
=
json
.
loads
(
parameters
.
replace
(
"'"
,
'"'
))
response
=
request_tsystems_api
(
params
[
"
group
"
],
params
[
"
station_id
"
],
params
[
"
username
"
],
params
[
"
password
"
]
)
parsed_observations
=
parse_api_response
(
response
)
resp
=
requests
.
post
(
f
"
{
api_base_url
}
/observations/upsert/
{
thing_uuid
}
"
,
json
=
parsed_observations
,
headers
=
{
"
Content-type
"
:
"
application/json
"
},
)
if
resp
.
status_code
!=
201
:
logging
.
error
(
f
"
{
resp
.
text
}
"
)
resp
.
raise_for_status
()
# exit
logging
.
info
(
f
"
Successfully inserted
{
len
(
parsed_observations
[
'
observations
'
])
}
"
f
"
observations for thing
{
thing_uuid
}
from TSystems API into TimeIO DB
"
)
mqtt
.
send_mqtt_info
(
"
data_parsed
"
,
json
.
dumps
({
"
thing_uuid
"
:
thing_uuid
}))
if
__name__
==
"
__main__
"
:
main
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment