-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
189 lines (154 loc) · 7.93 KB
/
server.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
from flask import Flask, request
from flask_restful import Resource, Api
from sqlalchemy import create_engine
from json import dumps
from flask.ext.jsonpify import jsonify
import socket
db_connect = create_engine('sqlite:///JMU_Autonomous.db')
app = Flask(__name__)
api = Api(app)
class Locations(Resource):
def get(self):
conn = db_connect.connect() # connect to database
query = conn.execute("select * from Locations") # This line performs query and returns json result
result = {'Locations': [dict(zip(tuple (query.keys()) ,i)) for i in query.cursor]}
return jsonify(result)
class addLocation(Resource):
def get(self, location_info):
info = location_info.split("+")
str = "INSERT INTO Locations VALUES (" + info[0] + ", \'" + info[1].replace("_", " ") + "\', \'" + info[2].replace("_", " ") + "\', " + info[3] + ", " + info[4] + ");"
conn = db_connect.connect() # connect to database
query = conn.execute(str) # This line performs query and returns json result
return "success"
class deleteLocation(Resource):
def get(self, location_name):
info = location_name.replace("_", " ")
str = "DELETE FROM Locations WHERE name=\'" + info + "\';"
conn = db_connect.connect() # connect to database
query = conn.execute(str) # This line performs query and returns json result
return "success"
class Location_name(Resource):
def get(self, location_name):
conn = db_connect.connect() # connect to database
query = conn.execute("select * from Locations WHERE Name = '" + location_name + "'") # This line performs query and returns json result
result = {'Locations': [dict(zip(tuple (query.keys()) ,i)) for i in query.cursor]}
return jsonify(result)
class Cardata(Resource):
def get(self):
conn = db_connect.connect() # connect to database
query = conn.execute("select * from Cardata") # This line performs query and returns json result
result = {'Cardata': [dict(zip(tuple (query.keys()) ,i)) for i in query.cursor]}
return jsonify(result)
def post(self):
conn = db_connect.connect() # connect to database
req = request.get_json()
newData = req['newData']
battery = newData['battery']
camera = newData['camera']
elevation = newData['elevation']
lat = newData['lat']
lightware = newData['lightware']
lon = newData['lon']
rplidar = newData['rplidar']
timestamp = newData['timestamp']
velocity = newData['velocity']
velodyne = newData['velodyne']
#clear the table
conn.execute("DELETE FROM Cardata;");
query = "INSERT INTO Cardata VALUES (" + str(velocity) + ", " + str(lat) + ", " + str(lon) + ", " + str(elevation) + ", \"" + str(battery) + "\", \"" + str(velodyne) + "\", \"" + str(lightware) + "\", \"" + str(rplidar) + "\", \"" + str(timestamp) + "\", \"" + str(camera) + "\");"
conn.execute(query);
#print "INSERT INTO Goals VALUES (" + str(lat) + "," + str(lon) + "," + str(elevation) +");"
return "Car data updated"
class battery_voltage(Resource):
def get(self):
conn = db_connect.connect() # connect to database
query = conn.execute(" select battery from Cardata") # This line performs query and returns json result
result = {'battery_voltage': [dict(zip(tuple (query.keys()) ,i)) for i in query.cursor]}
return jsonify(result)
class camera_status(Resource):
def get(self):
conn = db_connect.connect() # connect to database
query = conn.execute(" select camera from Cardata") # This line performs query and returns json result
result = {'camera_status': [dict(zip(tuple (query.keys()) ,i)) for i in query.cursor]}
return jsonify(result)
class gps_status(Resource):
def get(self):
conn = db_connect.connect() # connect to database
query = conn.execute(" select gps from Cardata") # This line performs query and returns json result
result = {'gps_status': [dict(zip(tuple (query.keys()) ,i)) for i in query.cursor]}
return jsonify(result)
class llidar_status(Resource):
def get(self):
conn = db_connect.connect() # connect to database
query = conn.execute(" select lightware from Cardata") # This line performs query and returns json result
result = {'llidar_status': [dict(zip(tuple (query.keys()) ,i)) for i in query.cursor]}
return jsonify(result)
class rlidar_status(Resource):
def get(self):
conn = db_connect.connect() # connect to database
query = conn.execute(" select rplidar from Cardata") # This line performs query and returns json result
result = {'rlidar_status': [dict(zip(tuple (query.keys()) ,i)) for i in query.cursor]}
return jsonify(result)
class velocity(Resource):
def get(self):
conn = db_connect.connect() # connect to database
query = conn.execute(" select velocity from Cardata") # This line performs query and returns json result
result = {'velocity': [dict(zip(tuple (query.keys()) ,i)) for i in query.cursor]}
return jsonify(result)
class velodyne_status(Resource):
def get(self):
conn = db_connect.connect() # connect to database
query = conn.execute(" select velodyne from Cardata") # This line performs query and returns json result
result = {'velodyne_status': [dict(zip(tuple (query.keys()) ,i)) for i in query.cursor]}
return jsonify(result)
class Goals(Resource):
def get(self):
conn = db_connect.connect() # connect to database
query = conn.execute("select * from Goals") # This line performs query and returns json result
result = {'Goals': [dict(zip(tuple (query.keys()) ,i)) for i in query.cursor]}
return jsonify(result)
def post(self):
conn = db_connect.connect() # connect to database
j = request.get_json()
list_of_goals = j['goals']
for item in list_of_goals:
lat = item['lat']
lon = item['long']
elevation = item['elevation']
conn.execute("INSERT INTO Goals VALUES (" + str(lat) + "," + str(lon) + "," + str(elevation) +");");
#print "INSERT INTO Goals VALUES (" + str(lat) + "," + str(lon) + "," + str(elevation) +");"
return 'goals added'
class clear_goals(Resource):
def get(self):
conn = db_connect.connect() # connect to database
query = conn.execute("delete from Goals") # This line performs query and returns json result
#result = {'Goals': [dict(zip(tuple (query.keys()) ,i)) for i in query.cursor]}
return 'goals cleared'
api.add_resource(Locations, '/locations')
api.add_resource(Location_name, '/locations/<location_name>')
api.add_resource(addLocation, '/addlocation/<string:location_info>')
api.add_resource(deleteLocation, '/deletelocation/<string:location_name>')
api.add_resource(Cardata, '/cardata')
api.add_resource(battery_voltage, '/cardata/battery')
api.add_resource(camera_status, '/cardata/camera')
api.add_resource(gps_status, '/cardata/gps')
api.add_resource(llidar_status, '/cardata/llidar')
api.add_resource(rlidar_status, '/cardata/rlidar')
api.add_resource(velocity, '/cardata/velocity')
api.add_resource(velodyne_status, '/cardata/velodyne')
api.add_resource(Goals, '/goals')
api.add_resource(clear_goals, '/clear')
@app.after_request
def after_request(response):
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Headers',
'Content-Type,Authorization')
response.headers.add('Access-Control-Allow-Methods',
'GET,PUT,POST,DELETE,OPTIONS')
return response
if __name__ == '__main__':
#hostname = socket.gethostname()
#dns_resolved_addr = socket.gethostbyname(hostname)
#print(str(dns_resolved_addr))
#app.run(host=str(dns_resolved_addr), port=8080)
app.run(host='192.168.99.72', port=5000)