-
Notifications
You must be signed in to change notification settings - Fork 0
/
runapp.py
145 lines (119 loc) · 4.28 KB
/
runapp.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
from flask import Flask, request, url_for
from flask.ext.mongoengine import MongoEngine
from mongoengine import *
import os, datetime, json, math
app = Flask(__name__)
app.debug = True
# app.config["MONGODB_SETTINGS"] = {'DB':"virtual_tag"}
# app.config["SECRET_KEY"] = "hackathon2013"
connect('app14643328', username='animohan', password='hello123', host='alex.mongohq.com',port=10091)
# db = MongoEngine(app)
# import views, models
class Tag(Document):
created_at = DateTimeField(default=datetime.datetime.now, required=True)
modified_at = DateTimeField(default=datetime.datetime.now, required=True)
qr_id = StringField(required=True)
message = StringField(required=True)
location_x = FloatField(required=True)
location_y = FloatField(required=True)
init_direction = FloatField(required=True)
direction = FloatField(required=True)
features = StringField()
def to_hash(self):
ret_hash = {
"created_at":self.created_at.strftime('%Y-%m-%d-%H:%M:%S'),
"modified_at":self.modified_at.strftime('%Y-%m-%d-%H:%M:%S'),
"qr_id":self.qr_id,
"message":self.message,
"location_x":self.location_x,
"location_y":self.location_y,
"init_direction":self.init_direction,
"direction":self.direction,
"features":self.features
}
return ret_hash
meta = {
'indexes': ['-created_at'],
'ordering': ['-created_at']
}
@app.route('/task',methods=['GET','POST','DELETE'])
def task():
if request.method == 'POST':
tag = Tag()
tag.qr_id = request.form.get('qr_id')
tag.location_x = float(request.form.get('location_x'))
tag.location_y = float(request.form.get('location_y'))
tag.init_direction = float(request.form.get('init_direction'))
tag.direction = float(request.form.get('direction'))
tag.message = request.form.get('message')
tag.features = request.form.get('features')
tag.save()
return json.dumps(tag.to_hash()), 200, {
"Content-Type": "application/json"}
elif request.method == 'DELETE':
Tag.drop_collection()
return json.dumps("All deleted"), 200, {
"Content-Type": "application/json"}
else:
tags = Tag.objects.all()
taghashes = [tag.to_hash() for tag in tags]
return json.dumps(taghashes), 200, {
"Content-Type": "application/json"}
@app.route('/intersect',methods=['POST'])
def intersect():
if request.method == 'POST':
cand_x1 = float(request.form.get('location_x'))
cand_y1 = float(request.form.get('location_y'))
theta2 = float(request.form.get('direction'))
tag = Tag.objects.all()[0]
ref_x1 = tag.location_x
ref_y1 = tag.location_y
theta1 = tag.direction
offset = tag.init_direction
theta1 = (theta1 - offset)/360*2*math.pi
theta2 = (theta2 - offset)/360*2*math.pi
if (math.cos(theta1) > 0 and math.sin(theta1) > 0):
ref_x2 = -30*math.sin(theta1) + ref_x1
ref_y2 = -30*math.cos(theta1) + ref_y1
elif (math.cos(theta1) < 0 and math.sin(theta1) < 0):
ref_x2 = -30*math.sin(theta1) + ref_x1
ref_y2 = -30*math.cos(theta1) + ref_y1
else:
ref_x2 = -30*math.sin(theta1) + ref_x1
ref_y2 = -30*math.cos(theta1) + ref_y1
if (math.cos(theta2) > 0 and math.sin(theta2) > 0):
cand_x2 = -30*math.sin(theta2) + cand_x1
cand_y2 = -30*math.cos(theta2) + cand_y1
elif (math.cos(theta2) < 0 and math.sin(theta2) < 0):
cand_x2 = -30*math.sin(theta2) + cand_x1
cand_y2 = -30*math.cos(theta2) + cand_y1
else:
cand_x2 = -30*math.sin(theta2) + cand_x1
cand_y2 = -30*math.cos(theta2) + cand_y1
print ref_x2
print ref_y2
print cand_x2
print cand_y2
intersect_min = max(min(ref_x1,ref_x2),min(cand_x1,cand_x2))
intersect_max = min(max(ref_x1,ref_x2),max(cand_x1,cand_x2))
if(max(ref_x1,ref_x2) < min(cand_x1,cand_x2)):
print "bad interval"
return json.dumps("false"), 200, {
"Content-Type": "application/json"}
Aref = (ref_y1-ref_y2)/(ref_x1-ref_x2)
Acand = (cand_y1-cand_y2)/(cand_x1-cand_x2)
bref = ref_y1-Aref*ref_x1
bcand = cand_y1-Acand*cand_x1
if(Aref==Acand):
return json.dumps("false"), 200, {
"Content-Type": "application/json"}
Xa = (bcand-bref)/(Aref-Acand)
if (Xa < intersect_min) or (Xa > intersect_max):
return json.dumps("false"), 200, {
"Content-Type": "application/json"}
else:
return json.dumps("true"), 200, {
"Content-Type": "application/json"}
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)