-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.py
executable file
·117 lines (86 loc) · 2.78 KB
/
http.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import sys
import json
from flask import Flask, jsonify
import sqlite3
import collections
configfile = 'conf/daemon.json'
config = json.load(open(configfile))
app = Flask(__name__)
def dict_from_row(row):
return dict(zip(row.keys(), row))
@app.route("/v1/trein/<trein_nr>")
def trein(trein_nr):
db = sqlite3.connect(config['db'])
db.row_factory = sqlite3.Row
c = db.cursor()
treinen = []
rows = c.execute("SELECT * FROM posities WHERE trein_nummer = ?", [trein_nr])
status = "OK"
for row in rows:
row_dict = dict_from_row(row)
treinen.append(row_dict)
if len(treinen) is 0:
status = "NOTFOUND"
return_dict = {"status": status, "trains": treinen}
db.close()
return jsonify(return_dict)
@app.route("/v1/mat/<mat_nr>")
def mat(mat_nr):
db = sqlite3.connect(config['db'])
db.row_factory = sqlite3.Row
c = db.cursor()
mat = {}
rows = c.execute("SELECT * FROM posities WHERE mat_nummer = ? LIMIT 1", [mat_nr])
status = "OK"
for row in rows:
row_dict = dict_from_row(row)
mat = row_dict
if mat == {}:
status = "NOTFOUND"
return_dict = {"status": status, "mat": mat}
db.close()
return jsonify(return_dict)
@app.route("/v1/total")
def total():
db = sqlite3.connect(config['db'])
db.row_factory = sqlite3.Row
c = db.cursor()
mat = []
rows = c.execute("SELECT * FROM posities")
status = "OK"
for row in rows:
row_dict = dict_from_row(row)
mat.append(row_dict)
if mat == []:
status = "NOTFOUND"
return_dict = {"status": status, "mat": mat}
db.close()
return jsonify(return_dict)
@app.route("/v1/total_geojson")
def total_geojson():
db = sqlite3.connect(config['db'])
db.row_factory = sqlite3.Row
c = db.cursor()
features = []
rows = c.execute("SELECT * FROM posities")
for row in rows:
row_dict = dict_from_row(row)
geometry = collections.OrderedDict()
geometry['type'] = "Point"
geometry['coordinates'] = [row_dict['lon'], row_dict['lat']]
properties = collections.OrderedDict()
properties['mat_nummer'] = row_dict['mat_nummer']
properties['trein_nummer'] = row_dict['trein_nummer']
properties['speed'] = row_dict['speed_kmh']
properties['heading'] = row_dict['heading']
feature = collections.OrderedDict()
feature['type'] = "Feature"
feature['geometry'] = geometry
feature['properties'] = properties
print(feature)
features.append(feature)
return_dict = collections.OrderedDict()
return_dict['type'] = "FeatureCollection"
return_dict['features'] = features
db.close()
return jsonify(return_dict)