Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Expand
titlepython

github

Code Block
languagepy
"""
Example For Select Raw Data for tag API by using request in python
written by yeony kim
sensor1, sensor2 is applied in lake
"""

import requests

API_KEY = "YOUR_API_TOKEN"
LAKE_ID = "YOUR_LAKE_ID"
CLOUD_VENDOR="CLOUD_VENDOR"
CLOUD_REGION="CLOUD_REGION"

URL = f"https://{LAKE_ID{CLOUD_VENDOR}.{CLOUD_REGION}.machlake.com/v1/lakes/{LAKE_ID}/values/"

SELECT_TYPE="raw"

headers = {
    '"Content-Type'": '"application/json'",
    '"x-api-key'": API_KEY
}


# ------------------------------------------------------------------------------------------------- #

# CASE - DATA GET with date format YYYY-MM-DD HH24:MI:SS mmm:uuu:nnn

params = {
    '"type": SELECT_TYPE,
    "tag_name'": '"sensor1'",
    '"date_format'": '"YYYY-MM-DD HH24:MI:SS mmm:uuu:nnn'",
    '"start_time'": '"2021-01-06 17:00:00 000:000:000'",
    '"end_time'": '"2021-01-06 21:00:00 000:000:000'"
}

response = requests.get(URL, headers=headers,  params=params, verify=False)
print(response.content.decode('"utf-8'"))

# Return Format
# {"data":{"calc_mode
#     "success":true,
#     "reason":"rawsuccess",
#     "data":{
#         "columns":[
#             {"name":"TIMENAME","type":5,"length":409680},
#             {"name":"VALUETIME","type":205,"length":174096}],"samples":[{"tag_,
#             {"name":"sensor1VALUE","datatype":[{20,"TIME":"2021length":17}
#         ],
#         "rows":[
#             ["sensor1","2021-01-06 17:00:00 001:000:000","VALUE":1},{"TIME":],
#             ["sensor1","2021-01-06 17:00:00 002:000:000","VALUE":1.5},{"TIME":],
#             ["sensor1","2021-01-06 17:00:00 003:000:000","VALUE":2},{"TIME":],
#             ["sensor1","2021-01-06 17:00:00 004:000:000","VALUE":1},{"TIME":],
#             ["sensor1","2021-01-06 17:00:00 005:000:000","VALUE":1.5},{"TIME":"2021-01-06 17],
#             ["sensor1","2021-01-06 17:00:00 006:000:000","VALUE":2},{"TIME":],
#             ["sensor1","2021-01-06 17:00:00 007:000:000","VALUE":1},{"TIME":],
#             ["sensor1","2021-01-06 17:00:00 008:000:000","VALUE":1.5},{"TIME":],
#             ["sensor1","2021-01-06 17:00:00 009:000:000","VALUE":2},{"TIME":],
#             ["sensor1","2021-01-06 18:00:00 001:000:000","VALUE":2},{"TIME":],
#             ["sensor1","2021-01-06 18:00:00 002:000:000","VALUE":2.5},{"TIME":"2021-01-06 18:00:00 003:000:000","VALUE":2},{"TIME":"2021-01-06 20:00:04 000:000:000","VALUE":2},{"TIME":"2021-01-06 20:00:05 000:000:000","VALUE":2.5},{"TIME":"],
#             ["sensor1","2021-01-06 2018:00:0600 000003:000:000","VALUE":2}]}]},"status":"success"}


# CASE - DATA GET with date format YYYY-MM-DD HH24:MI:SS

params = {
    'tag_name': 'sensor1',
    'date_format': 'YYYY-MM-DD HH24:MI:SS',
    'start_time': '2021-01-06 17:00:00 000:000:000',
    'end_time': '2021-01-06 21:00:00 000:000:000'
}

response = requests.get(URL, headers=headers,  params=params, verify=False)
print(response.content.decode('utf-8'))  # {"data":{"calc_mode":"raw","columns":[{"name":"TIME","type":5,"length":4096},{"name":"VALUE","type":20,"length":17}],"samples":[{"tag_name":"sensor1","data":[{"TIME":"2021-01-06 17:00:00","VALUE":1},{"TIME": ]
#     }
# }

# ------------------------------------------------------------------------------------------------- #

# CASE - DATA GET with date format YYYY-MM-DD HH24:MI:SS

params = {
    "type": SELECT_TYPE,
    "tag_name": "sensor1",
    "date_format": "YYYY-MM-DD HH24:MI:SS",
    "start_time": "2021-01-06 17:00:00","VALUE":1.5},{"TIME":
    "end_time": "2021-01-06 1721:00:00","VALUE":2},{"TIME":"2021-01-06 17:00:00","VALUE":1},{"TIME":"2021-01-06 17:00:00","VALUE":1.5},{"TIME":"2021-01-06 17:00:00","VALUE":2},{"TIME":"2021-01-06 17:00:00","VALUE":1},{"TIME":"2021-01-06 17:00:00","VALUE":1.5},{"TIME":"2021-01-06 17:00:00","VALUE":2},{"TIME":"2021-01-06 18:00:00","VALUE":2},{"TIME":
}

response = requests.get(URL, headers=headers,  params=params, verify=False)
print(response.content.decode("utf-8"))

# Return Format
# {
#     "success":true,
#     "reason":"success",
#     "data":{
#         "columns":[
#             {"name":"NAME","type":5,"length":80},
#             {"name":"TIME","type":5,"length":4096},
#             {"name":"VALUE","type":20,"length":17}
#         ],
#         "rows":[
#             ["sensor1","2021-01-06 1817:00:00",1],"VALUE":2.5},{"TIME":"
#             ["sensor1","2021-01-06 1817:00:00","VALUE":2},{"TIME":1.5],
#             ["sensor1","2021-01-06 2017:00:0400","VALUE":2},{"TIME":],
#             ["sensor1","2021-01-06 2017:00:0500","VALUE":2.5},{"TIME":"1],
#             ["sensor1","2021-01-06 2017:00:0600","VALUE":2}]}]},"status":"success"}1.5],
#     # CASE - DATA GET with date format YYYY-MM-DD HH24:MI:SS, data inserted with date skip mmm:uuu:nnn

params = { ["sensor1","2021-01-06 17:00:00",2],
#            'tag_name': 'sensor1',
    'date_format': 'YYYY-MM-DD HH24:MI:SS mmm:uuu:nnn', ["sensor1","2021-01-06 17:00:00",1],
#            'start_time': ' ["sensor1","2021-01-06 17:00:00 000:000:000',",1.5],
#            'end_time': ' ["sensor1","2021-01-06 2117:00:00 000:000:000'
}

response = requests.get(URL, headers=headers,  params=params, verify=False)
print(response.content.decode('utf-8'))  # {"data":{"calc_mode":"raw","columns":[{"name":"TIME","type":5,"length":4096},{"name":"VALUE","type":20,"length":17}],"samples":[{"tag_name":"sensor1","data":[{"TIME":"",2],
#             ["sensor1","2021-01-06 18:00:00",2],
#             ["sensor1","2021-01-06 1718:00:00 001:000:000","VALUE":1},{"TIME":"2021-01-06 17:00:00 002:000:000","VALUE":1.5},{"TIME":"",2.5],
#             ["sensor1","2021-01-06 1718:00:00 003:000:000","VALUE":2},{"TIME":"2021-01-06 17:00:00 004:000:000","VALUE":1},{"TIME":"2021-01-06 17:00:00 005:000:000","VALUE":1.5},{"TIME":"2021-01-06 17:00:00 006:000:000","VALUE":2},{"TIME":"2021-01-06 17:00:00 007:000:000","VALUE":1},{"TIME":"2021-01-06 17:00:00 008:000:000","VALUE":1.5},{"TIME":"2021-01-06 17:00:00 009:000:000","VALUE":2},{"TIME":"2021-01-06 18:00:00 001:000:000","VALUE":2},{"TIME":"2021-01-06 18:00:00 002:000:000","VALUE":2.5},{"TIME":"2021-01-06 18:00:00 003:000:000","VALUE":2},{"TIME":"2021-01-06 20:00:04 000:000:000","VALUE":2},{"TIME":"2021-01-06 20:00:05 000:000:000","VALUE":2.5},{"TIME":"2021-01-06 20:00:06 000:000:000","VALUE":2}]}]},"status":"success"}


# CASE - DATA GET with date format YYYY-MM-DD HH24:MI:SS and with UTC-0 timezone

headers = {
    'Content-Type': 'application/json',
    'x-api-key': API_KEY,
    "Use-Timezone": "Africa/Abidjan"
}

params = {
    'tag_name': 'sensor1',
    'date_format': 'YYYY-MM-DD HH24:MI:SS mmm:uuu:nnn',
    'start_time': '2021-01-06 17:00:00 000:000:000',
    'end_time': '2021-01-06 21:00:00 000:000:000'
}

response = requests.get(URL, headers=headers,  params=params, verify=False)
print(response.content.decode('utf-8'))  # {"data":{"calc_mode":"raw","columns":[2]
#         ]
#     }
# }

# ------------------------------------------------------------------------------------------------- #

# CASE - DATA GET with date format YYYY-MM-DD HH24:MI:SS, data inserted with date skip mmm:uuu:nnn

params = {
    "type": SELECT_TYPE,
    "tag_name": "sensor1",
    "date_format": "YYYY-MM-DD HH24:MI:SS mmm:uuu:nnn",
    "start_time": "2021-01-06 17:00:00 000:000:000",
    "end_time": "2021-01-06 21:00:00 000:000:000"
}

response = requests.get(URL, headers=headers,  params=params, verify=False)
print(response.content.decode("utf-8"))

# Return Format
# {
#     "success":true,
#     "reason":"success",
#     "data":{
#         "columns":[
#             {"name":"NAME","type":5,"length":80},
#             {"name":"TIME","type":5,"length":4096},
#             {"name":"VALUE","type":20,"length":17}],"samples":null},"status":"success"}
#   # CASE - DATA GET with date],
format# YYYY-MM-DD HH24:MI:SS mmm:uuu:nnn and limit / offset / direction / value_retrun_form=0

headers = {"rows":[
#          'Content-Type': 'application/json',
    'x-api-key': API_KEY,
}

params = {  ["sensor2","2021-01-06 17:00:00 000:000:000",1],
#       'tag_name': 'sensor1,sensor2',     'date_format': 'YYYY-MM-DD HH24:MI:SS mmm:uuu:nnn',["sensor2","2021-01-06 17:00:01 000:000:000",1.5],
#     'start_time': '        ["sensor2","2021-01-06 17:00:0002 000:000:000',",2],
#     'end_time': '        ["sensor2","2021-01-06 2117:00:0003 000:000:000'",1],
  #  'limit': 10,     'offset': 5,     'direction': 0,["sensor2","2021-01-06 17:00:04 000:000:000",1.5],
#     'value_return_form': 0
}      response = requests.get(URL, headers=headers,  params=params, verify=False)
print(response.content.decode('utf-8'))  # {"data":{"calc_mode":"raw","columns":[{"name":"TIME","type":5,"length":4096},{"name":"VALUE","type":20,"length":17}],"samples":[{"tag_name":"sensor1","data":[{"TIME":"2021-01-06 17:00:00 006:000:000","VALUE":2},{"TIME":"2021-01-06 17:00:00 007:000:000","VALUE":1},{"TIME":"2021-01-06 17:00:00 008:000:000","VALUE":1.5},{"TIME":"2021-01-06 17:00:00 009:000:000","VALUE":2},{"TIME":"2021-01-06 18:00:00 001:000:000","VALUE":2},{"TIME":"2021-01-06 18:00:00 002:000:000","VALUE":2.5},{"TIME":"2021-01-06 18:00:00 003:000:000","VALUE":2},{"TIME":"2021-01-06 20:00:04 000:000:000","VALUE":2},{"TIME":"2021-01-06 20:00:05 000:000:000","VALUE":2.5},{"TIME":"2021-01-06 20:00:06 000:000:000","VALUE":2}]}]},"status":"success"}


# CASE - DATA GET with date format YYYY-MM-DD HH24:MI:SS mmm:uuu:nnn and limit / offset / direction / value_retrun_form=0

headers = {
    'Content-Type': 'application/json',
    'x-api-key': API_KEY,
}

params = {
    'tag_name': 'sensor1,sensor2',
    'date_format': 'YYYY-MM-DD HH24:MI:SS mmm:uuu:nnn',
    'start_time': '2021-01-06 17:00:00 000:000:000',
    'end_time': '2021-01-06 21:00:00 000:000:000',
    'direction': 1,
    'value_return_form': 1
}

response = requests.get(URL, headers=headers,  params=params, verify=False)
print(response.content.decode('utf-8'))  # {"data":{"calc_mode":"raw","columns":[{"name":"TIME","type":5,"length":4096},{"name":"VALUE","type":20,"length":17}],"samples":[{"tag_name":"sensor2","data":{"TIME":[" ["sensor2","2021-01-06 17:00:05 000:000:000",2],
#             ["sensor2","2021-01-06 18:00:00 001:000:000",2],
#             ["sensor2","2021-01-06 18:00:00 002:000:000",2.5],
#             ["sensor2","2021-01-06 18:00:00 003:000:000",2],
#             ["sensor2","2021-01-06 20:00:04 000:000:000",2],
#             ["sensor2","2021-01-06 20:00:05 000:000:000",2.5],
#             ["sensor2","2021-01-06 20:00:06 000:000:000",2]
#         ]
#     }
# }

# ------------------------------------------------------------------------------------------------- #

# CASE - DATA GET with UTC-0 timezone

params = {
    "type": SELECT_TYPE,
    "timezone": "Africa/Abidjan",
    "tag_name": "sensor1",
    "date_format": "YYYY-MM-DD HH24:MI:SS mmm:uuu:nnn",
    "start_time": "2021-01-06 17:00:00 000:000:000",
    "end_time": "2021-01-06 21:00:00 000:000:000"
}

response = requests.get(URL, headers=headers,  params=params, verify=False)
print(response.content.decode("utf-8"))

# Return Format
# {
#     "success":true,
#     "reason":"success",
#     "data":{
#         "columns":[
#             {"name":"NAME","type":5,"length":80},
#             {"name":"TIME","type":5,"length":4096},
#             {"name":"VALUE","type":20,"length":17}
#         ],
#         "rows":[
#             ["sensor2","2021-01-06 17:00:00 007:000:000",1],
#             ["sensor2","2021-01-06 17:00:00 008:000:000",1.5],
#             ["sensor2","2021-01-06 17:00:00 009:000:000",2]
#         ]
#     }
# }

# ------------------------------------------------------------------------------------------------- #

# CASE - DATA GET with local timezone, data inserted UTC-0 timezone setting

params = {
    "type": SELECT_TYPE,
    "tag_name": "sensor2",
    "date_format": "YYYY-MM-DD HH24:MI:SS mmm:uuu:nnn",
    "start_time": "2021-01-07 02:00:00 000:000:000",
    "end_time": "2021-01-07 12:00:00 000:000:000"
}

response = requests.get(URL, headers=headers,  params=params, verify=False)
print(response.content.decode("utf-8"))

# Return Format
# {
#     "success":true,
#     "reason":"success",
#     "data":{
#         "columns":[
#             {"name":"NAME","type":5,"length":80},
#             {"name":"TIME","type":5,"length":4096},
#             {"name":"VALUE","type":20,"length":17}
#         ],
#         "rows":[
#             ["sensor2","2021-01-07 02:00:00 007:000:000",1],
#             ["sensor2","2021-01-07 02:00:00 008:000:000",1.5],
#             ["sensor2","2021-01-07 02:00:00 009:000:000",2]
#         ]
#     }
# }

# ------------------------------------------------------------------------------------------------- #

# CASE - DATA GET with limit and offset

params = {
    "type": SELECT_TYPE,
    "tag_name": "sensor1",
    "date_format": "YYYY-MM-DD HH24:MI:SS mmm:uuu:nnn",
    "start_time": "2021-01-06 17:00:00 000:000:000",
    "end_time": "2021-01-06 21:00:00 000:000:000",
    "limit": 10,
    "offset": 5
}

response = requests.get(URL, headers=headers,  params=params, verify=False)
print(response.content.decode("utf-8"))

# Return Format
# {
#     "success":true,
#     "reason":"success",
#     "data":{
#         "columns":[
#             {"name":"NAME","type":5,"length":80},
#             {"name":"TIME","type":5,"length":4096},
#             {"name":"VALUE","type":20,"length":17}
#         ],
#         "rows":[
#             ["sensor1","2021-01-06 17:00:00 006:000:000",2],
#             ["sensor1","2021-01-06 17:00:00 007:000:000",1],
#             ["sensor1","2021-01-06 17:00:00 008:000:000",1.5],
#             ["sensor1","2021-01-06 17:00:00 009:000:000",2],
#             ["sensor1","2021-01-06 18:00:00 001:000:000",2],
#             ["sensor1","2021-01-06 18:00:00 002:000:000",2.5],
#             ["sensor1","2021-01-06 18:00:00 003:000:000",2],
#             ["sensor1","2021-01-06 20:00:04 000:000:000",2],
#             ["sensor1","2021-01-06 20:00:05 000:000:000",2.5],
#             ["sensor1","2021-01-06 20:00:06 000:000:000",2]
#         ]
#     }
# }

# ------------------------------------------------------------------------------------------------- #

# CASE - DATA GET with descent by time

params = {
    "type": SELECT_TYPE,
    "tag_name": "sensor1,sensor2",
    "date_format": "YYYY-MM-DD HH24:MI:SS mmm:uuu:nnn",
    "start_time": "2021-01-06 17:00:00 000:000:000",
    "end_time": "2021-01-06 21:00:00 000:000:000",
    "direction": 1
}

response = requests.get(URL, headers=headers,  params=params, verify=False)
print(response.content.decode("utf-8"))

# Return Format
# {
#     "success":true,
#     "reason":"success",
#     "data":{
#         "columns":[
#             {"name":"NAME","type":5,"length":80},
#             {"name":"TIME","type":5,"length":4096},
#             {"name":"VALUE","type":20,"length":17}
#         ],
#         "rows":[
#             ["sensor2","2021-01-06 20:00:06 000:000:000",2],
#             ["sensor1","2021-01-06 20:00:06 000:000:000",2],
#             ["sensor2","2021-01-06 20:00:05 000:000:000",2.5],
#             ["sensor1","2021-01-06 20:00:05 000:000:000",2.5],
#             ["sensor2","2021-01-06 20:00:04 000:000:000",2],
#             ["sensor1","2021-01-06 20:00:0604 000:000:000000",2],
#             ["sensor2","2021-01-06 2018:00:0500 000003:000:000",2],
#             ["sensor1","2021-01-06 2018:00:0400 000003:000:000",2],
#             ["sensor2","2021-01-06 18:00:00 003002:000:000",2.5],
#             ["sensor1","2021-01-06 18:00:00 002:000:000",2.5],
#             ["sensor2","2021-01-06 18:00:00 001:000:000","2021-01-06 17:00:05 000:000:0002],
#             ["sensor1","2021-01-06 1718:00:0400 000001:000:000","2021-01-06 17:00:03 000:000:0002],
#             ["sensor2","2021-01-06 17:00:0205 000:000:000","2021-01-06 17:00:01 000:000:0002],
#             ["sensor2","2021-01-06 17:00:0004 000:000:000"],"VALUE":[2,2.5,2,2,2.5,2,2,1.5,1,2,1.5,1]}},{"tag_name":"sensor1","data":{"TIME":[",
#             ["sensor2","2021-01-06 2017:00:0603 000:000:000",1],
#             ["sensor2","2021-01-06 2017:00:0502 000:000:000","2021-01-06 20:00:04 000:000:0002],
#             ["sensor2","2021-01-06 1817:00:0001 003000:000:000",1.5],
#             ["sensor1","2021-01-06 1817:00:00 002009:000:000","2021-01-06 18:00:00 001:000:0002],
#             ["sensor1","2021-01-06 17:00:00 009008:000:000",1.5],
#             ["sensor1","2021-01-06 17:00:00 008007:000:000",1],
#             ["sensor1","2021-01-06 17:00:00 007006:000:000",2],
#             ["sensor1","2021-01-06 17:00:00 006005:000:000",1.5],
#             ["sensor1","2021-01-06 17:00:00 005004:000:000",1],
#             ["sensor1","2021-01-06 17:00:00 004003:000:000",2],
#             ["sensor1","2021-01-06 17:00:00 003002:000:000",1.5],
#             ["sensor1","2021-01-06 17:00:00 002001:000:000",1],
#             ["sensor2","2021-01-06 17:00:00 001000:000:000"],"VALUE":[2,2.5,2,2,2.5,2,2,1.5,1,2,1.5,1,2,1.5,1]}}]},"status":"success"]
#         ]
#     }
# }