Skip to content
Permalink
Browse files
init
  • Loading branch information
memica committed Feb 16, 2020
0 parents commit ce8991bebf3239b23e54429a8e886dc4b588d4e4
Show file tree
Hide file tree
Showing 20 changed files with 415 additions and 0 deletions.
@@ -0,0 +1,14 @@
# Codio
.codio

# Databases
databases/

# Python
__pycache__/

# Flask
venv/

# Test
*_test/
@@ -0,0 +1,5 @@
sudo apt-get update
sudo apt-get install python3-venv

mkdir location_app location_app/app
python3 -m venv location_app/venv
@@ -0,0 +1,5 @@
FLASK_APP=location_app.py
FLASK_ENV=development
FLASK_RUN_HOST='0.0.0.0'
FLASK_RUN_PORT=3000
MYAPP_SETTINGS=configuration.cfg
@@ -0,0 +1,13 @@
from flask import Flask
from app import mqtt, routes


app = Flask(__name__)
app.config.from_envvar('MYAPP_SETTINGS')

mqtt.init_app(app)

routes.init_app(app)



@@ -0,0 +1,9 @@
MQTT_BROKER_URL='mqtt.coventry.ac.uk'
MQTT_BROKER_PORT=8883
MQTT_USERNAME='4009user'
MQTT_PASSWORD='mqttBROKER'
MQTT_TLS_ENABLED=True
MQTT_TLS_INSECURE=True
MQTT_TLS_CA_CERTS='mqtt.coventry.ac.uk.crt'
MQTT_REFRESH_TIME=1.0
TEMPLATES_AUTO_RELOAD=True
@@ -0,0 +1,70 @@
import sqlite3 as sql


database_user = "app/databases/users.db"
database_locations = 'app/databases/locations.db'

def if_user_exists(username):
print(f"checked for existance - {username}")
with sql.connect(database_user) as cur:
res = cur.execute(f"SELECT count(*) FROM UserDatabase WHERE username='{username}';").fetchone()[0]
if res == 0:
return False
else:
return True

def if_tid_exists(username):
print(f"checked for tid existance - {username}")
with sql.connect(database_locations) as cur:
res = cur.execute(f"SELECT count(*) FROM Location WHERE tid='{username}';").fetchone()[0]
if res == 0:
return False
else:
return True

def password_for(username, password):
print(f"checked password - {username}")
with sql.connect(database_user) as cur:
res = cur.execute(f"SELECT password FROM UserDatabase WHERE username='{username}';").fetchone()[0]
if password == res:
return True
else:
return False

def make_user(username, password):
print(f"created a new user - {username}")
con = sql.connect(database_user)
cur = con.cursor()
cur.execute(f"INSERT INTO UserDatabase values('{username}','{password}');")
con.commit()
cur.close()
con.close()

def change_password(username, new_password):
print(f"created a new password - {username}")
con = sql.connect(database_user)
cur = con.cursor()
cur.execute(f"UPDATE UserDatabase SET password='{new_password}' WHERE username='{username}';")
con.commit()
cur.close()
con.close()

def username_and_password(username, password):
with sql.connect(database_user) as cur:
try:
cur.execute("CREATE TABLE UserDatabase(username VARCHAR2(20), password VARCHAR2(20));")
except:
pass
if username == "admin" and password == "admin":
return "L"
if if_user_exists(username):
if password_for(username, password):
return "L"
else:
return "WP"
else:
if if_tid_exists(username):
make_user(username, password)
return "L"
else:
return "NO"
@@ -0,0 +1,51 @@
import sqlite3 as sql
import time


database_user = "app/databases/users.db"
database_locations = "app/databases/locations.db"

def get_user_for(username):
with sql.connect(database_user) as cur:
res = cur.execute(f"SELECT * FROM UserDatabase WHERE username='{username}';").fetchone()

def get_filtered_data_for(tid):
data_city = get_frequency_for('city', tid)
data_road = get_frequency_for('road', tid)
data_batt = get_frequency_for('battery', tid)
return [data_city, data_road, data_batt]

def get_all_tids():
with sql.connect(database_locations) as cur:
tids = cur.execute("SELECT DISTINCT tid From Location;")
tids_list = list(map(lambda x: x[0], tids))
return tids_list

def get_frequency_for(data_type, tid):
with sql.connect(database_locations) as cur:
data = cur.execute(f"SELECT {data_type} From Location WHERE tid = '{tid}';")
data_list = list(map(lambda x: x[0], data))
frequent_data = max(set(data_list), key = data_list.count)
frequency_data = str(int((data_list.count(frequent_data)/len(data_list)) * 100))
return [frequent_data, frequency_data]

def get_filtered_data_for_admin():
all_data = []
tids = get_all_tids()
for tid in tids:
data_city = get_frequency_for('city', tid)
data_road = get_frequency_for('road', tid)
data_batt = get_frequency_for('battery', tid)
all_data.append([tid, data_city, data_road, data_batt])
return all_data

def get_locations():
locations = []
with sql.connect(database_locations) as cur:
res = cur.execute(f"SELECT DISTINCT * From Location ORDER BY tid, timestamp;")
for tid, batt, lon, lat, city, road, tst, in res:
rtst = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(tst))
locations.append([tid, batt, lon, lat, city, road, rtst])
print(locations)
return locations

@@ -0,0 +1,13 @@
from flask_mqtt import Mqtt
from app.mqtt import mqtt_message_handler

def init_app(app):
mqtt = Mqtt(app)

@mqtt.on_connect()
def handle_connect(client, userdata, flags, rc):
mqtt.subscribe('owntracks/4009user/#')

@mqtt.on_message()
def handle_mqtt_message(client, userdata, msg):
mqtt_message_handler.getMsg(msg)
@@ -0,0 +1,33 @@
from geopy.geocoders import Nominatim
import sqlite3 as sql
import json, os, time


database_locations = 'app/databases/locations.db'

def getMsg(msg):
con = sql.connect(database_locations)
cur = con.cursor()
geolocator = Nominatim(user_agent="Web_app")
try:
cur.execute("CREATE TABLE Location(tid VARCHAR2(2), battery INT(3), longitude NUMBER(10,6), latitude NUMBER(10,6), city VARCHAR2(20), road VARCHAR2(30), timestamp INT(20));")
except:
pass
data = json.loads(msg.payload.decode("utf8"))
tid = data["tid"]
batt = data["batt"]
lat = data["lat"]
lon = data["lon"]
location = geolocator.reverse(f"{data['lat']},{data['lon']}")
city = location.raw["address"]["city"]
road = location.raw["address"]["road"]
tstamp = data["tst"]

print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(tstamp)), ' ---- ', tid)

cur.execute(f"INSERT INTO Location values('{tid}','{batt}','{lat}','{lon}','{city}','{road}','{tstamp}');")

con.commit()
cur.close()
con.close()

@@ -0,0 +1,4 @@
from .login import main_bl

def init_app(app):
app.register_blueprint(main_bl)
@@ -0,0 +1,21 @@
from flask import Blueprint, render_template, request, url_for, escape, redirect
from app.functions.auth import password_check
from app.functions.data_tools import data_getter

main_bl = Blueprint('main', __name__)

@main_bl.route("/", methods = ["GET", "POST"])
@main_bl.route("/index", methods = ["GET", "POST"])
def login():
if request.method == "POST":
username = request.form["username"]
password = request.form["password"]
status = password_check.username_and_password(username, password)
if status == "L":
return redirect(url_for("testt", username = username))
elif status == "WP":
return render_template("index.html", error = 1)
elif status == "NO":
return render_template("index.html", error = 2)
else:
return render_template("index.html", error = 0)
@@ -0,0 +1,3 @@
body {
background-color: red;
}
@@ -0,0 +1,19 @@
{% extends 'base.html' %}


{% block title %} " Insert title here " {% endblock %}


{% block css %}

" Insert css here "

{% endblock %}


{% block body %}

" Insert body here "

{% endblock %}

@@ -0,0 +1,18 @@
<!doctype html>

<html lang="en">

<head>
<meta charset="utf-8">
<meta name="description" content="">
<meta name="author" content="">
<title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" href="{{ url_for('static', filename='css/base.css') }}">
{% block css %}{% endblock %}
</head>

<body>
{% block body %}{% endblock %}
</body>

</html>
@@ -0,0 +1,21 @@
{% extends 'base.html' %}

{% block title %} Trackmaster {% endblock %}

{% block body %}

{% if error == 1 %}
<h1>WRONG PASSWORD!!!</h1>
{% endif %}
{% if error == 2 %}
<h1>WRONG TID!!!</h1>
{% endif %}
<form method="post" action="index">
<label>tid :</label>
<input type="text" name="username" />
<label>password :</label>
<input type="password" name="password" />
<button type="submit">Login</button>
</form>

{% endblock %}
@@ -0,0 +1,19 @@
{% extends 'base.html' %}


{% block title %} " Insert title here " {% endblock %}


{% block body %}

{% for idd in tids %}
<h1>{{idd}}</h1>
{% endfor %}
<form method="post" action="index">
<label>test :</label>
<button type="submit">Login</button>
</form>

{% endblock %}


@@ -0,0 +1,53 @@
<!doctype html>
<html>

<head>
<meta charset="utf-8">
<title>Trackmaster</title>
<!-- <link rel="stylesheet" href="css/global.css"> -->
</head>

<body>
<!-- <table>
<tr>
<td>
<form method="get" action="filtered">
<input type="hidden" value={{username}} name="username" />
<button type="submit">Give frequencies</button>
</form>
</td>
<td>
<form method="get" action="index">
<button type="submit">Log Out</button>
</form>
</td>
</tr>
</table> -->
<table>
<caption class="table">Locations</caption>
<tr class="title">
<th>ID</th>
<th>Battery</th>
<th>Longitude</th>
<th>Latitude</th>
<th>City</th>
<th>Road</th>
<th>TST</th>
</tr>
{% for item in locations %}
<!-- {% if username == item[0] or username == "admin" %} -->
<tr>
<td class="id">{{ item[0] }}</td>
<td class="short">{{ item[1] }} %</td>
<td class="long">{{ item[2] }}</td>
<td class="long">{{ item[3] }}</td>
<td class="long">{{ item[4] }}</td>
<td class="long">{{ item[5] }}</td>
<td class="date">{{ item[6] }}</td>
</tr>
<!-- {% endif %} -->
{% endfor %}
</table>
</body>

</html>
@@ -0,0 +1 @@
from app import app

0 comments on commit ce8991b

Please sign in to comment.