Skip to content

Commit c9c9620

Browse files
committed
Add docs, license, and touch up in-code docs
1 parent 1031176 commit c9c9620

File tree

3 files changed

+131
-2
lines changed

3 files changed

+131
-2
lines changed

LICENSE.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
The MIT License (MIT)
2+
=====================
3+
4+
Copyright © 2021 MCCI Corporation
5+
6+
Permission is hereby granted, free of charge, to any person
7+
obtaining a copy of this software and associated documentation
8+
files (the “Software”), to deal in the Software without
9+
restriction, including without limitation the rights to use,
10+
copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the
12+
Software is furnished to do so, subject to the following
13+
conditions:
14+
15+
The above copyright notice and this permission notice shall be
16+
included in all copies or substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
19+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25+
OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# ttn_storage_api
2+
3+
Simple (primitive) Python script to get data for a TTN Version 3 app from the [TTN storage application API](https://www.thethingsnetwork.org/docs/applications/storage/).
4+
5+
## Use
6+
7+
### TTN V3 Console Preparation
8+
9+
Set up your application on TTN V3.
10+
11+
Enable the "storage integration".
12+
13+
Create an API key for the application. Record the secret, because you'll need it to call the API.
14+
15+
### Python set up
16+
17+
Put `ttn_storage_api.py` in your current directory, or do the work to put it in your installed python module set. (We've not tested any of that; we just tested interactively in the same directory as this repo.)
18+
19+
```python
20+
import ttn_storage_api
21+
```
22+
23+
### `sensor_pull_storage`
24+
25+
```python
26+
def sensor_pull_storage(appname, accesskey, timestring, *,data_folder = None, ttn_version=3):
27+
```
28+
29+
Pull data from TTN via the TTN storage API.
30+
31+
- `appname` is the name of the TTN app, as a string.
32+
33+
- `accesskey` is the full access key from The Things Network Console. Meaning depends on whether you're using V2 or V3. For TTN V3, this is must be set to the secret that is output when an API key is created. For TTN V2, this is the API key string from the console, starting with "`ttn-acount-v2`."
34+
35+
- `timestring` indicates amount of data needed, e.g. `'100h'`. For some reason, in TTN , the `d` suffix is not supported; please use `h`.
36+
37+
- `ttn_version` should be 2 or 3; 3 is default.
38+
39+
- If `data_folder` is supplied, it is a string or a `Path`. This is taken as a directory, and the name `"sensors_lastperiod.json"` is appended to form an output file name. The fetched data is written to the resulting file, replacing any previous contents.
40+
41+
Otherwise, the data is returned as a Python array (for V3) or a string (for V2).
42+
43+
We've not really tested V2 extensively.
44+
45+
## Interactive Examples
46+
47+
```console
48+
>>> import ttn_storage_api
49+
>>>
50+
>>> # set the access key
51+
>>> key = "the secret output by TTN v3 console when you create an API key"
52+
>>>
53+
>>> # read 24 hours of data and display it
54+
>>> ttn_storage_api.sensor_pull_storage("my-app-name", key, "24h")
55+
[{'result': {'end_device_ids': {'device_id': 'device-1', 'application_ids': {}}, 'received_at': '2021-07-20T22:09:14.260657946Z', 'uplink_message': {'decoded_payload': {'humidity': 71, 'temperature': 21.8}, 'settings': {'data_rate': {}}, 'received_at': '2021-07-20T22:09:14.050246577Z'}}}, {'result': {'end_device_ids': {'device_id': 'device-2', 'application_ids': {}}, 'received_at': '2021-07-20T22:12:14.095320042Z', 'uplink_message': {'decoded_payload': {'humidity': 58, 'temperature': 26.8}, 'settings': {'data_rate': {}}, 'received_at': '2021-07-20T22:12:13.879292705Z'}}}, {'result': {'end_device_ids': {'device_id': 'device-3', 'application_ids': {}}, 'received_at': '2021-07-20T22:14:55.108253840Z', 'uplink_message': {'decoded_payload': {'humidity': 58, 'temperature': 25.4}, 'settings': {'data_rate': {}}, 'received_at': '2021-07-20T22:14:54.891246372Z'}
56+
}]
57+
>>>
58+
>>> # put the data in an array
59+
>>> r = ttn_storage_api.sensor_pull_storage("my-app-name", key, "24h")
60+
>>>
61+
>>> # size of the array
62+
>>> len(r)
63+
3
64+
>>> # the first record
65+
>>> r[0]['result']
66+
{'end_device_ids': {'device_id': 'device-1', 'application_ids': {}}, 'received_at': '2021-07-20T22:09:14.260657946Z', 'uplink_message': {'decoded_payload': {'humidity': 71, 'temperature': 21.8}, 'settings': {'data_rate': {}}, 'received_at': '2021-07-20T22:09:14.050246577Z'}}
67+
>>>
68+
>>> # drill down
69+
>>> r[0]['result']['uplink_message']
70+
{'decoded_payload': {'humidity': 71, 'temperature': 21.8}, 'settings': {'data_rate': {}}, 'received_at': '2021-07-20T22:09:14.050246577Z'}
71+
>>> r[0]['result']['uplink_message']['decoded_payload']
72+
{'humidity': 71, 'temperature': 21.8}
73+
>>> r[0]['result']['uplink_message']['decoded_payload']['humidity']
74+
71
75+
>>>
76+
>>> # display the timestamp of the message
77+
>>> r[0]['result']['uplink_message']['received_at']
78+
'2021-07-20T22:09:14.050246577Z'
79+
>>>
80+
>>> # display the sending device's ID
81+
>>> r[0]['result']['end_device_ids']['device_id']
82+
'device-1'
83+
>>>
84+
```
85+
86+
## Meta
87+
88+
### Contributions
89+
90+
This code was written by Terry Moore, MCCI Corporation, with feedback and test assistance from Brian Vant-Hull.
91+
92+
### License
93+
94+
This repository is released under the MIT license.
95+
96+
### Support Open Source Hardware and Software
97+
98+
MCCI invests time and resources providing this open source code, please support MCCI and open-source hardware by purchasing products from MCCI\ and other open-source hardware/software vendors!
99+
100+
For information about MCCI's products, please visit [store.mcci.com](https://store.mcci.com/).

ttn_storage_api.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,12 @@ def sensor_pull_storage(appname, accesskey, timestring, *,data_folder = None, tt
4848
4949
ttn_version should be 2 or 3; 3 is default.
5050
51-
If data_folder is supplied, it is a string or a Path; the name "
52-
The data is returned as a string. Use json.loads() to decode into an object.
51+
If data_folder is supplied, it is a string or a Path; it is taken as a directory,
52+
and the name "sensors_lastperiod.json" is appended to form an output file name, and
53+
the data is written to the resulting file, replacing any previous contents.
54+
55+
Otherwise, the data is returned as a Python array (for V3) or a string (for V2).
56+
We've not really tested V2 extensively.
5357
"""
5458
args = [ "curl" ]
5559
if ttn_version == 2:

0 commit comments

Comments
 (0)