Skip to content

Commit

Permalink
Updated version to properly create array of data
Browse files Browse the repository at this point in the history
  • Loading branch information
terrillmoore committed Jul 21, 2021
1 parent 3d248e7 commit 19765f8
Showing 1 changed file with 59 additions and 13 deletions.
72 changes: 59 additions & 13 deletions fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,80 @@
#
# Use:
# import fetch
# fetch.sensor_pull_storage(...)
# fetch.sensor_pull_storage(...) -- see docs for args
#
# Or just cut/paste into your script.
#

import subprocess
import json
import pathlib
import re

def sensor_pull_storage(accesskey, timestring, appname, data_folder):
class Error(Exception):
"""Base class for errors in this module"""
pass

class FetchError(Error):
"""Raised when sensor_pull_storage can't deal with input
Atrributes:
expression -- input expression where error occurred
message -- explanation of the error.
"""
def __init__(self, expression, message):
self.expression = expression
self.message = message

def sensor_pull_storage(appname, accesskey, timestring, *,data_folder = None, ttn_version=3):
"""
Pull data from TTN via the storage API.
accesskey is the full accesskey from ttn, starting with 'ttn-acount-v2.'
timestring indicates amount of data needed, e.g. '1d'.
appname is the name of the TTN app
data_folder, if not None, says where to put the data.
accesskey is the full accesskey from ttn. For TTN V3, this is is the
secret output when a kye is created. For TTN V2, this is
the string from the console, starting with 'ttn-acount-v2.'
timestring indicates amount of data needed, e.g. '1d'.
ttn_version should be 2 or 3; 3 is default.
If data_folder is supplied, it is a string or a Path; the name "
The data is returned as a string. Use json.loads() to decode into an object.
"""
args = [ "curl",
"-X", "GET",
"--header", "Accept: application/json",
"--header", f"Authorization: key {accesskey}",
f"https://{appname}.data.thethingsnetwork.org/api/v2/query?last={timestring}"
args = [ "curl" ]
if ttn_version == 2:
args += [
"-X", "GET",
"--header", "Accept: application/json",
"--header", f"Authorization: key {accesskey}",
f"https://{appname}.data.thethingsnetwork.org/api/v2/query?last={timestring}"
]
elif ttn_version == 3:
args += [
"-G", f"https://nam1.cloud.thethings.network/api/v3/as/applications/{appname}/packages/storage/uplink_message",
"--header", f"Authorization: Bearer {accesskey}",
"--header", "Accept: text/event-stream",
"-d", f"last={timestring}",
"-d", "field_mask=up.uplink_message.decoded_payload",
]
else:
raise FetchError(f"Illegal ttn_version (not 2 or 3)")


# if the user supplied a data_folder, than tack on the args.
# list1 += list2 syntax means "append each element of list2 to list 1"
# pathlib.Path allows
if data_folder != None:
args += [ "-o", f"{data_folder}sensors_lastperiod.json"]
args += [ "-o", pathlib.Path(data_folder, "sensors_lastperiod.json") ]

return subprocess.run(
args, shell=False
result = subprocess.run(
args, shell=False, check=True, capture_output=True
)

sresult = result.stdout
if ttn_version == 3:
return list(map(json.loads, re.sub(r'\n+', '\n', sresult.decode()).splitlines()))
else:
return sresult

0 comments on commit 19765f8

Please sign in to comment.