position.py 2.52 KB
Newer Older
Jonathan Krebs's avatar
Jonathan Krebs committed
1
import datetime
2
from collections import defaultdict
3

4
5
6
7
8
import yaml
from flask import (Flask, abort, jsonify, render_template, request,
                   send_from_directory)

from iwlist import append_measurement
9
10
11
12
13
14
15
16
17
18

app = Flask(__name__, static_url_path='/static')

@app.route('/static/<path:path>')
def send_static(path):
    return send_from_directory('static', path)

@app.route('/static/assets/<path:path>')
def send_node_modules(path):
    return send_from_directory('node_modules', path)
Jonathan Krebs's avatar
Jonathan Krebs committed
19
20
21
22
23
24
25
26
27

@app.route("/")
def hello():
    return render_template("index.html")

@app.route("/set", methods=["POST"])
def setpos():
    data = request.get_json()
    print(data)
28
29
    time = datetime.datetime.fromtimestamp(data["t"] / 1000)
    time = time.strftime("%H:%M:%S") + ".%.2d" % round(time.microsecond / 10000)
Jonathan Krebs's avatar
Jonathan Krebs committed
30
31
    b = "%.2d%6.2f,%s" % (int(data["lat"]))
    print("$GPGGA,%s,BBBB.BBBB,b,LLLLL.LLLL,l,Q,NN,D.D,H.H,h,G.G,g,A.A,RRRR*PP" % (time))
32
    return jsonify({'success': True})
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

@app.route('/snapshot', methods=['POST'])
def snapshot():
    data = request.get_json()
    print('Snapshotting at lat:', data['lat'], 'lon:', data['lon'], 'acc:', data['acc'])
    append_measurement(lat=data['lat'], lon=data['lon'], acc=data['acc'])
    return jsonify({'success': True})

@app.route('/data')
def data():
    group_by = request.args.get('group_by', None)
    filter_by = request.args.get('filter_by_address', None)
    if group_by and group_by not in ['essid', 'address']:
        abort(400)
    if filter_by:
        filter_by = filter_by.split(',')
    with open('./data.yml', 'r' ) as file:
        data = yaml.load(file)
    if group_by == 'address':
        for measurement in data:
            by_address = defaultdict(list)
            if not measurement.get('data', None):
                continue
            for address, point in measurement['data'].items():
                by_address[address].append(point)
            measurement['data'] = by_address
    elif group_by == 'essid':
        for measurement in data:
            by_address = defaultdict(list)
            if not measurement.get('data', None):
                continue
            for address, point in measurement['data'].items():
                by_address[point['essid']].append(point)
            measurement['data'] = by_address
    else:
        if filter_by:
            for measurement in data:
                measurement['data'] = {
                    address: point for address, point in measurement['data'].items()
                    if address in filter_by
                }

    return jsonify(data)