forked from wisc-hci-curriculum/RobotAssistant
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
70 lines (59 loc) · 2.47 KB
/
app.py
File metadata and controls
70 lines (59 loc) · 2.47 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
from flask import Flask, request, jsonify, make_response, redirect
import json
from flask_cors import CORS
app = Flask(__name__)
app.config['SECRET_KEY'] = 'MYNOTSOSECRETKEY'
app.config['CORS_HEADERS'] = 'Content-Type'
app.url_map.strict_slashes = False
CORS(app)
items = [
{'id':0,'type':'cube','color':'blue','location':'shelf'},
{'id':1,'type':'ball','color':'yellow','location':'box'},
{'id':2,'type':'ball','color':'red','location':'box'},
{'id':3,'type':'wrench','color':'grey','location':'conveyor'}
]
scene = {
'shelf': [0],
'table': [],
'box': [1,2],
'conveyor': [3],
}
def create_response(data,code=200,origin='*'):
response = make_response(jsonify(data),code)
response.headers['Access-Control-Allow-Origin'] = origin
response.headers['Content-Type'] = 'json'
response.headers['Vary'] = 'Origin'
return response
@app.route('/objects/',methods=['GET'])
def get_objects():
return create_response({'objects':items},200)
@app.route('/objects/<int:object_id>',methods=['GET'])
def get_object(object_id):
return create_response(items[object_id],200)
@app.route('/locations/',methods=['GET'])
def get_locations():
return create_response({'locations':scene.keys()},200)
@app.route('/locations/<string:location>/objects/',methods=['GET'])
def get_objects_at_location(location):
if location not in scene.keys():
return create_response({'message':'location not found'},404)
return create_response({'objects':[items[i] for i in scene[location]]},200)
@app.route('/locations/<string:location>/objects/<int:object_id>',methods=['POST'])
def move_object_to_location(location,object_id):
if location not in scene.keys():
return create_response({'message':'location not found'},404)
if object_id > 3:
return create_response({'message':'object not found'},404)
for loc, loc_items in scene.items():
if object_id in loc_items and loc == location:
item = items[object_id]
return create_response({'message':'The {0} {1} is already at the {2}.'.format(item['color'],item['type'],location)},200)
elif object_id in loc_items:
scene[loc].remove(object_id)
if loc == location:
scene[loc].append(object_id)
item = items[object_id]
item['location'] = location
return create_response({'message':'I moved the {0} {1} to the {2}.'.format(item['color'],item['type'],location)},200)
if __name__ == '__main__':
app.run(debug=True)