Skip to content

Commit

Permalink
receive information from submitted payload
Browse files Browse the repository at this point in the history
  • Loading branch information
Noodlesalat committed Apr 2, 2019
1 parent cd3ba85 commit a793b5c
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 0 deletions.
Empty file.
60 changes: 60 additions & 0 deletions owntracks-http-endpoint/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env python3
from flask import Flask, request
from helper.location import Location
import json

# debug
debug = True
from datetime import datetime
from debug.randomGeoCoordinates import GeoCoor

application = Flask(__name__)

# this is called if a device submits a new position
# stores new position in database and delivers the last reported position of all friends of this device
@application.route('/pub', methods = ['POST'])
def addNewDatapoint():
datapoint = request.data
if debug:
f = open("locations.txt","a+")
f.write(str(datapoint) + "\n")
parsedDatapoint = json.loads(datapoint.decode("utf-8"))
if parsedDatapoint["_type"] == 'location':
# store information from given datapoint
location = Location()
location.accuracy = parsedDatapoint["acc"]
location.altitude = parsedDatapoint["alt"]
location.battery = parsedDatapoint["batt"]
location.connection = parsedDatapoint["conn"]
location.latitude = parsedDatapoint["lat"]
location.longitude = parsedDatapoint["lon"]
# catch if trigger is transmitted (Move mode)
try:
location.trigger = parsedDatapoint["t"]
except KeyError as e:
location.trigger = "UNDEFINED"
location.trackerID = parsedDatapoint["tid"]
location.timestamp = parsedDatapoint["tst"]
location.velocity = parsedDatapoint["vel"]

if debug:
f = open("locations.txt","a+")
f.write("Acc: " + str(location.accuracy) +
" Alt: " + str(location.altitude) +
" Batt: " + str(location.battery) +
" Conn: " + str(location.connection) +
" Lat: " + str(location.latitude) +
" Lon: " + str(location.longitude) +
" T: " + str(location.trigger) +
" TID: " + str(location.trackerID) +
" Tst: " + str(location.timestamp) +
" Vel: " + str(location.velocity) + "\n")

return '{"_type":"location","acc":16,"alt":0,"batt":4,"conn":"w","lat":' + GeoCoor.randomCoordsLat() + ',"lon":' + GeoCoor.randomCoordsLon() + ',"t":"u","tid":"wo","tst":' + str(int(datetime.timestamp(datetime.now())) - 300) + ',"vac":0,"vel":0}'
else:
f = open("locations.txt","a+")
f.write(json.dumps(datapoint.decode("utf-8")) + "\n")
return ''

if __name__ == '__main__':
application.run(debug = True, host='0.0.0.0')
18 changes: 18 additions & 0 deletions owntracks-http-endpoint/database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os, sys, sqlite3

class Database():
def addPosition(payload):
create_database()
connection = sqlite3.connect("./tempdata.db")

def create_database():
if not os.path.exists("./database.db"):
connection = sqlite3.connect("./tempdata.db")
cursor = connection.cursor()
# Tabelle erzeugen
sql = "CREATE TABLE tempWerte("\
"id FLOAT, maxTempSensor FLOAT, minTempUser FLOAT, \
maxTempUser FLOAT) "
cursor.execute(sql)
connection.commit()
connection.close()
10 changes: 10 additions & 0 deletions owntracks-http-endpoint/debug/randomGeoCoordinates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from random import uniform

class GeoCoor():
def randomCoordsLat():
x = uniform(50, 53)
return str(x)

def randomCoordsLon():
y = uniform(7,10)
return str(y)
19 changes: 19 additions & 0 deletions owntracks-http-endpoint/helper/location.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Location():
# accuracy - Genauigkeit
# altitude - Höhe
# battery - Akkustand
# connection - w -> phone is connected to a WiFi connection (iOS,Android);
# o -> phone is offline (iOS,Android), m -> mobile data (iOS,Android)
# latitude - Breitengrad
# longitude - Längengrad
# trigger - p -> ping issued randomly by background task (iOS,Android)
# c -> circular region enter/leave event (iOS,Android)
# b -> beacon region enter/leave event (iOS)
# r -> response to a reportLocation cmd message (iOS,Android)
# u -> manual publish requested by the user (iOS,Android)
# t -> timer based publish in move move (iOS)
# v -> updated by Settings/Privacy/Locations Services/System Services/Frequent Locations monitoring (iOS)
# trackerID - Tracker Identifiaction
# timestamp - Publish Timestamp
# velocity - Geschwindigkeit
__slots__ = ['accuracy', 'altitude', 'battery', 'connection', 'latitude', 'longitude', 'trigger', 'trackerID', 'timestamp', 'velocity']
1 change: 1 addition & 0 deletions owntracks-http-endpoint/locations.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

0 comments on commit a793b5c

Please sign in to comment.