Fix TTNv3 (, )

This commit is contained in:
Kyle Gabriel 2021-12-04 10:35:09 -05:00
parent 5c1a89c008
commit 3bbd110099
3 changed files with 45 additions and 25 deletions

View file

@ -80,9 +80,9 @@ Edit ```ttn-tracker/flask_app/config.py``` with your application API Key, applic
Open a web browser to the address, below, replacing IP_ADDRESS with the IP address of the system running the docker containers. Open a web browser to the address, below, replacing IP_ADDRESS with the IP address of the system running the docker containers.
```https://IP_ADDRESS/dsf673bh``` ```https://IP_ADDRESS/map```
Note: there is no security preventing someone from viewing this page if they happen to request "/dsf673bh" on the server (however, knowing this is the page is unlikely). Therefore, make sure you are comfortable with this or implement your own security measures such as not allowing port 443 to be publicly accessible (connect to your home network via VPN to access the app) or add a login system such as [Flask-Login](https://github.com/maxcountryman/flask-login). Note: there is no security preventing someone from viewing this page if they happen to request "/map" on the server (however, knowing this is the page is unlikely). Therefore, make sure you are comfortable with this or implement your own security measures such as not allowing port 443 to be publicly accessible (connect to your home network via VPN to access the app) or add a login system such as [Flask-Login](https://github.com/maxcountryman/flask-login).
## Notes ## Notes

View file

@ -1,11 +1,6 @@
import datetime import datetime
import json
import logging import logging
from math import atan2
from math import cos
from math import radians
from math import sin
from math import sqrt
import os import os
import requests import requests
from apscheduler.schedulers.background import BackgroundScheduler from apscheduler.schedulers.background import BackgroundScheduler
@ -14,10 +9,16 @@ from flask import Flask
from flask import jsonify from flask import jsonify
from flask import render_template from flask import render_template
from flask_sqlalchemy import SQLAlchemy from flask_sqlalchemy import SQLAlchemy
from math import atan2
from math import cos
from math import radians
from math import sin
from math import sqrt
from config import app_key from config import app_key
from config import application from config import application
from config import bing_api_key from config import bing_api_key
from config import cluster
from config import config_app from config import config_app
from config import devices from config import devices
from config import gateway_locations from config import gateway_locations
@ -86,7 +87,7 @@ if not os.path.exists(path_db):
db.create_all() db.create_all()
@app.route('/dsf673bh') @app.route('/map')
def main_page(): def main_page():
get_new_data() get_new_data()
return render_template('map.html', return render_template('map.html',
@ -99,7 +100,7 @@ def main_page():
start_lon=start_lon) start_lon=start_lon)
@app.route('/dsf673bh_past/<seconds>') @app.route('/past/<seconds>')
def get_past_data(seconds): def get_past_data(seconds):
if seconds_from_last() > 10: if seconds_from_last() > 10:
get_new_data() get_new_data()
@ -120,30 +121,46 @@ def get_new_data():
past_seconds = 604800 # 7 days, max The Things Network storage allows past_seconds = 604800 # 7 days, max The Things Network storage allows
for each_device in devices: for each_device in devices:
endpoint = "https://eu1.cloud.thethings.network/api/v3/as/applications/{app}/devices/{dev}/packages/storage/uplink_message?last={time}".format( endpoint = "https://{cluster_loc}.cloud.thethings.network/api/v3/as/applications/{app}/devices/{dev}/packages/storage/uplink_message?order=-received_at&type=uplink_message?last={time}".format(
app=application, dev=each_device, time="{}s".format(past_seconds)) cluster_loc=cluster, app=application, dev=each_device, time="{}s".format(past_seconds))
logger.info(endpoint) logger.info(endpoint)
key = 'Bearer {}'.format(app_key) key = 'Bearer {}'.format(app_key)
headers = {'Authorization': key, 'Content-Type': 'application/json'} headers = {'Accept': 'text/event-stream', 'Authorization': key}
response = requests.get(endpoint, headers=headers) response = requests.get(endpoint, headers=headers)
if response.status_code != 200: if response.status_code != 200:
logger.info(response.reason) logger.info(response.reason)
try: try:
for each_resp in response.json(): response_format = "{\"data\": [" + response.text.replace("\n\n", ",")[:-1] + "]}"
if (not Location.query.filter(Location.datetime == each_resp['time']).first() and response_data = json.loads(response_format)
-90 < float(each_resp['latitude']) <= 90 and -120 <= float(each_resp['longitude']) <= 80): uplink_msg = response_data["data"]
logger.info("{}, {}".format(each_resp['latitude'], each_resp['longitude'])) for each_resp in uplink_msg:
response_data = each_resp["result"]
uplink_message = response_data["uplink_message"]
received = response_data["received_at"]
lat = uplink_message["decoded_payload"].get("latitude", "")
lon = uplink_message["decoded_payload"].get("longitude", "")
alt = uplink_message["decoded_payload"].get("altitude", "")
qos = uplink_message["decoded_payload"].get("hdop", "")
end_device_ids = response_data["end_device_ids"]
device = end_device_ids["device_id"]
rawpay = uplink_message["frm_payload"]
if (not Location.query.filter(Location.datetime == received).first() and
-90 < float(lat) <= 90 and -120 <= float(lon) <= 80):
logger.info("{}, {}".format(lat, lon))
new_location = Location( new_location = Location(
device_id=each_resp['device_id'], device_id=device,
raw='', raw=rawpay,
datetime_obj=parser().parse(each_resp['time']), datetime_obj=parser().parse(received),
datetime=each_resp['time'], datetime=received,
latitude=each_resp['latitude'], latitude=lat,
longitude=each_resp['longitude'], longitude=lon,
altitude=each_resp['altitude'], altitude=alt,
hdop=each_resp['hdop']) hdop=qos)
db.session.add(new_location) db.session.add(new_location)
db.session.commit() db.session.commit()
logger.info(new_location)
except: except:
pass pass

View file

@ -9,6 +9,9 @@ refresh_period_seconds = 15
start_lat = 35.978781 start_lat = 35.978781
start_lon = -77.855346 start_lon = -77.855346
# TTN Cluster
cluster = "nam1"
# TTN Application # TTN Application
application = "ttn_application" application = "ttn_application"
app_key = "ttn-account-TTN_APP_KEY" app_key = "ttn-account-TTN_APP_KEY"