-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
96 lines (70 loc) · 2.75 KB
/
server.py
File metadata and controls
96 lines (70 loc) · 2.75 KB
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
import json
import os
from flask import Flask, request, Response, jsonify, render_template
from datetime import datetime
# Import the database
import db
# Import API functions
import api
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST', 'PUT'])
def home():
# Handle GET
if request.method == 'GET':
return render_template('index.html')
# Handle POST and PUT
else:
# Get the reading
json_data = json.loads(request.data)
# Print the readings
print str(json_data)
# Store it in the DB
db.put(json_data)
# Send okie-dokie response
return Response(status=200)
@app.route('/view')
def view_ids():
# Get total number of database entries
db.getCur().execute("""SELECT COUNT(*) FROM obdreadings;""")
# Get the first item of the first tuple in the returned list
num_rows = (db.getCur().fetchall()[0])[0]
# Get a list of all stored vehicleids
db.getCur().execute("""SELECT DISTINCT vehicleid FROM obdreadings;""")
# Return id list
ids = db.getCur().fetchall()
# Since the above returns tuples, use a list comprehension to get the first elements
ids = [x[0] for x in ids]
# Get the most recent timestamp
db.getCur().execute("""SELECT unix_timestamp from obdreadings ORDER BY unix_timestamp DESC LIMIT 1;""")
timestamp = db.getCur().fetchone()[0] # [0] because result is a tuple - get the first element
# Also convert timestamp to human-readable time
int_timestamp_secs = int(timestamp) / 1000
ts_utc = datetime.utcfromtimestamp(int_timestamp_secs).strftime('%Y-%m-%d %H:%M:%S')
ts_local = datetime.fromtimestamp(int_timestamp_secs).strftime('%Y-%m-%d %H:%M:%S')
return render_template('view.html', num_rows=num_rows, ids=ids, ts_unix=timestamp, ts_utc=ts_utc, ts_local=ts_local)
@app.route('/view/<vehicleid>')
def view_id(vehicleid):
# Get the vehicleid records from the database
db.getCur().execute("""SELECT * FROM obdreadings WHERE vehicleid=%s;""", (vehicleid,))
response = Response(str(db.getCur().fetchall()))
response.mimetype = 'text/plain'
return response;
@app.route('/api/latlong.json')
# @crossdomain(origin='*')
def get_latlong_json():
return api.get_latlong_json()
@app.route('/realtime')
def realtime():
# Get 20 most recent entries
db.getCur().execute("""SELECT * from obdreadings ORDER BY unix_timestamp DESC LIMIT 20""")
# Publish it
result_str = ''
for record in db.getCur():
result_str += '{}\n'.format(record)
# Create and send response
response = Response(result_str)
response.mimetype = 'text/plain'
return response
if __name__ == '__main__':
port = int(os.environ.get("PORT", 5000))
app.run(host='0.0.0.0', port=port, debug=True)