-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
198 lines (173 loc) · 5.88 KB
/
app.py
File metadata and controls
198 lines (173 loc) · 5.88 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
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
190
191
192
193
194
195
196
197
198
from flask import Flask,request
from flask_pymongo import PyMongo
from ast import literal_eval
from bson.objectid import ObjectId
from priodict import priorityDictionary
app = Flask(__name__)
app.config['MONGO_DATABASE'] = 'smartpackaging'
app.config['MONGO_URI'] = 'mongodb://iot:iot123@ds263295.mlab.com:63295/smartpackaging'
mongo = PyMongo(app)
@app.route('/add_office',methods=['POST','GET'])
def office_Add():
#try:
user = mongo.db.office
p = request.get_data()
print(p)
a = p.decode("utf-8")
p = literal_eval(a)
user.insert({'name':p['name'],'address':p['address'],'longitude':p['longitude'],'latitude':p['latitude'], 'adj_office':[]})
return '0'
# except:
# return '1'
@app.route('/add_order', methods=['POST','GET'])
def order_Add():
try:
user = mongo.db.orders
p = request.get_data()
a = p.decode("utf-8")
p = literal_eval(a)
path = shortestPath(get_priorityDict(), p['from'], p['to'])
user.insert({'driver':'Not assigned','vibration':'Good','longitude':'','latitude':'','status':'at_office', 'current':p['from'],
'from_address':p['from_address'],'from_city':p['from'],'from_pincode':p['from_pin'],'to_address':p['to_address'],
'to_city':p['to'],'to_pincode':p['to_pin'],'category':p['category'],'path':path ,'tampered':'False', 'next': path[1], 'count': 1})
return '0'
except:
return '1'
@app.route('/track_cust',methods=['POST','GET'])
def track_cust():
try:
user = mongo.db.orders
p = request.get_data()
a = p.decode("utf-8")
p = literal_eval(a)
print(str(p['_id']))
ans = user.find_one({"_id":ObjectId(p['_id'])})
return "{0}, {1}, {2}, {3}".format(ans['status'], ans['current'], ans['longitude'], ans['latitude'])
except:
return '1'
@app.route('/office_list', methods=['POST','GET'])
def off_list():
try:
user = mongo.db.office
off_list = user.distinct('name')
return str(off_list)[1:-1]
except:
return '1'
@app.route('/add_route', methods=['POST','GET'])
def add_route():
try:
p = request.get_data()
a = p.decode("utf-8")
p = literal_eval(a)
users = mongo.db.office
users.update({'name':p['from']},{ '$push': {'adj_office':{'name':p['to'],'cost':p['cost']}}})
return '0'
except:
return '1'
@app.route('/admin', methods=['POST','GET'])
def admin_():
try:
p = request.get_data()
a = p.decode("utf-8")
a = literal_eval(a)
name = "Admin"
passw = '12345'
if name == a['name'] and passw == a['password']:
return '0'
else:
return '1'
except:
return '1'
@app.route('/update-data', methods=['POST','GET'])
def hardware():
try:
user = mongo.db.orders
p = request.get_data()
a = p.decode("utf-8")
p = literal_eval(a)
#
# Decide using training on data generated by testing by our own and then tested using logistic regression
#
ans = user.find_one({'_id': ObjectId(p['_id'])})
threshold = 200
if p['vibration'] >= threshold:
ans['vibration']='Bad'
ans['longitude'] = p['longitude']
ans['latitude'] = p['latitude']
#condition if in any of the office
if abs(p['longitude']-ans['longitude'])>10 and abs(p['latitude']-ans['latitude'])>10:
if ans['current'] == ans['path'][-1]:
user.update({'_id': p['_id']}, {'$set': {'status':'Delivered'}})
else:
c = 0
for i in range(0,len(ans['path'])-1):
if ans['next'] == ans['path'][i]:
break
c = c+1
if c == len(ans['path'])-1:
return c-1
user.update({'_id': p['_id']},{'$set':{'current':ans['next'], 'status':'At office', 'next':ans['path'][c+1],'tampered':p['tampered']}})
except:
return '1'
@app.route('/driver-req-packages',methods=['POST','GET'])
def driver_():
try:
user = mongo.db.orders
p = request.form
a = user.update({'from_city':p['from_city'],'to_city':p['to_city']},{'status':'At route','driver':p['name'],'driver_contact':p['contact']})
k = []
for i in a:
k.append(i['_id'])
return str (k)[1:-1]
except:
return '1'
@app.route('/track_admin', methods=['POST','GET'])
def track_admin():
# try:
user = mongo.db.orders
p = request.get_data()
a = p.decode("utf-8")
p = literal_eval(a)
ans = user.find_one({"_id": ObjectId(p['order_id'])})
#route = str(ans['path'])
return "{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}".format(ans['current'], ans['longitude'], ans['latitude'], ans['next'], ans['status'], ans['vibration'], ans['driver'], ans['tampered'])
# except:
# return '1'
def get_priorityDict():
office = mongo.db.office
allOffices = office.find()
G = dict()
for eachOffice in allOffices:
G[eachOffice['name']] = dict()
for eachAdjOffice in eachOffice['adj_office']:
G[eachOffice['name']][eachAdjOffice['name']] = int(eachAdjOffice['cost'])
return G
def Dijkstra(G,start,end=None):
D = {} # dictionary of final distances
P = {} # dictionary of predecessors
Q = priorityDictionary() # est.dist. of non-final vert.
Q[start] = 0
for v in Q:
D[v] = Q[v]
if v == end: break
print(G)
for w in G[v]:
vwLength = D[v] + G[v][w]
if w in D:
if vwLength < D[w]:
print("valueerror")
elif w not in Q or vwLength < Q[w]:
Q[w] = vwLength
P[w] = v
return (D,P)
def shortestPath(G,start,end):
D,P = Dijkstra(G,start,end)
Path = []
while 1:
Path.append(end)
if end == start: break
end = P[end]
Path.reverse()
return Path
if __name__ == '__main__':
app.run(debug=True)