forked from HackerShackOfficial/Smart-Bartender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
299 lines (269 loc) · 11.7 KB
/
api.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import os
import sys
import json
import time
import threading
import traceback
import RPi.GPIO as GPIO
from flask import Flask, request, jsonify, abort, render_template
app = Flask(__name__)
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
RUNNING = False
drink_list = json.load(open('/home/pi/Smart-Bartender/drinks.json'))
ingred_list = json.load(open('/home/pi/Smart-Bartender/ingreds.json'))
pump_list = json.load(open('/home/pi/Smart-Bartender/pumps.json'))
settings = json.load(open('/home/pi/Smart-Bartender/settings.json'))
for pumps in pump_list:
GPIO.setup(pumps['value'], GPIO.OUT, initial=GPIO.HIGH)
def pour(pin, waitTime):
#GPIO.output(pin, GPIO.LOW)
time.sleep(waitTime)
#GPIO.output(pin, GPIO.HIGH)
@app.route('/')
def index():
return render_template("home.html")
@app.route("/about")
def about():
return render_template("about.html")
@app.route("/power")
def power():
os.system("sudo shutdown -h now")
@app.route("/restart")
def restart():
python = sys.executable
os.execl(python, python, * sys.argv)
@app.route("/settings")
def listSettings():
return jsonify(settings)
@app.route("/setting/<setting_val>")
def showSetting(setting_val):
setting = [setting for setting in settings if setting['value'] == setting_val]
return jsonify([setting[0]])
def getSetting(setting_val):
setting = [setting for setting in settings if setting['value'] == setting_val]
return setting[0]["set"]
@app.route("/drinks")
def listDrinks():
return jsonify(drink_list)
@app.route('/drink/<drink_val>', methods=['GET'])
def showDrink(drink_val):
drink = [drink for drink in drink_list if drink['value'] == drink_val]
if len(drink) == 0:
abort(404)
return jsonify([drink[0]])
@app.route('/drink/make/time/<drink_val>', methods=['GET'])
def calcMakeTime(drink_val):
waitTime = 0
# use for any modifier, such as "double"
modifier = request.args.get('modifier')
for ingreds in showDrinkIngreds(drink_val):
if getSetting("similar") == "Yes" and 'value-similar' in ingreds:
pump = [pump for pump in pump_list if pump['description'] == ingreds['value-similar']]
if getSetting("related") == "Yes" and 'value-related' in ingreds:
pump = [pump for pump in pump_list if pump['description'] == ingreds['value-related']]
try: pump
except NameError: pump = None
if pump is None or len(pump) == 0:
pump = [pump for pump in pump_list if pump['description'] == ingreds['value']]
if len(pump) > 0 and ingreds['value'] <> "garnish":
if ingreds['unit'] == "cl":
if ingreds['amount'] * 10 * float(getSetting("flow")) > waitTime:
waitTime = ingreds['amount'] * 10 * float(getSetting("flow"))
elif ingreds['unit'] == "ml":
if ingreds['amount'] * float(getSetting("flow")) > waitTime:
waitTime = ingreds['amount'] * float(getSetting("flow"))
if modifier == "double" and waitTime * 2 > waitTime:
waitTime = waitTime * 2
return str(waitTime)
@app.route('/drink/make/<drink_val>', methods=['GET'])
def makeDrink(drink_val):
pumpThreads = []
# use for any modifier, such as "double"
modifier = request.args.get('modifier')
for ingreds in showDrinkIngreds(drink_val):
if getSetting("similar") == "Yes" and 'value-similar' in ingreds:
pump = [pump for pump in pump_list if pump['description'] == ingreds['value-similar']]
if getSetting("related") == "Yes" and 'value-related' in ingreds:
pump = [pump for pump in pump_list if pump['description'] == ingreds['value-related']]
try: pump
except NameError: pump = None
if pump is None or len(pump) == 0:
pump = [pump for pump in pump_list if pump['description'] == ingreds['value']]
if len(pump) > 0 and ingreds['value'] <> "garnish":
if ingreds['unit'] == "cl":
waitTime = ingreds['amount'] * 10 * float(getSetting("flow"))
elif ingreds['unit'] == "ml":
waitTime = ingreds['amount'] * float(getSetting("flow"))
if modifier == "double":
waitTime = waitTime * 2
pump_t = threading.Thread(target=pour, args=(pump[0]['value'], waitTime))
pumpThreads.append(pump_t)
ingreds["pumped"] = True
ingreds["time"] = waitTime
else:
ingreds["pumped"] = False
RUNNING = True
# start the pump threads
for thread in pumpThreads:
thread.start()
# wait for threads to finish
for thread in pumpThreads:
thread.join()
RUNNING = False
return jsonify(showDrinkIngreds(drink_val))
@app.route('/drink/add', methods=['POST'])
def addDrink():
if not request.json or not 'name' in request.json:
abort(400)
drink = {
'value': drinks[-1]['value'] + 1,
'name': request.json['name'],
'description': request.json.get('description', ""),
'prepartion': request.json.get('preparation', ""),
'drinkware': request.json.get('drinkware', ""),
'image': request.json.get('image', ""),
'ingredients': request.json.get('ingredients', ""),
}
drink_list.append(drink)
return jsonify({'drink': drink}), 201
@app.route('/drink/update/<drink_val>', methods=['PUT'])
def updateDrink(drink_val):
drink = [drink for drink in drink_list if drink['value'] == drink_val]
if len(drink) == 0:
abort(404)
if not request.json:
abort(400)
if 'name' in request.json and type(request.json['name']) != unicode:
abort(400)
if 'description' in request.json and type(request.json['description']) is not unicode:
abort(400)
if 'preparation' in request.json and type(request.json['preparation']) is not unicode:
abort(400)
drink[0]['name'] = request.json.get('name', drink[0]['name'])
drink[0]['description'] = request.json.get('description', drink[0]['description'])
drink[0]['preparation'] = request.json.get('preparation', drink[0]['preparation'])
return jsonify({'drink': drink[0]})
@app.route('/drink/delete/<drink_val>', methods=['DELETE'])
def deleteDrink(drink_val):
drink = [drink for drink in drink_list if drink['value'] == drink_val]
if len(drink) == 0:
abort(404)
drink_list.remove(drink[0])
return jsonify({'result': True})
@app.route('/drink/ingreds/<drink_val>', methods=['GET'])
def showDrinkIngreds(drink_val):
drink = [drink for drink in drink_list if drink['value'] == drink_val]
for ingreds in drink[0]['ingredients']:
pump = [pump for pump in pump_list if pump['description'] == ingreds['value']]
if len(pump) > 0:
ingreds['pump'] = pump[0]['value']
# check and see if any of the related/similar ingredients are loaded on a pump
ingred = [ingred for ingred in ingred_list if ingred['value'] == ingreds['value']]
if len(ingred) > 0 and getSetting("related") == "Yes" and 'related' in ingred[0]:
# if there's only one related drink
pump_related = [pump_related for pump_related in pump_list if pump_related['description'] == ingred[0]['related']]
if len(pump_related) > 0:
ingreds['pump-related'] = pump_related[0]['value']
ingreds['value-related'] = ingred[0]['related']
# if there's a list of related drinks
for ingreds_related in ingred[0]['related']:
pump_related = [pump_related for pump_related in pump_list if pump_related['description'] == ingreds_related]
if len(pump_related) > 0:
ingreds['pump-related'] = pump_related[0]['value']
ingreds['value-related'] = ingreds_related
if len(ingred) > 0 and getSetting("similar") == "Yes" and 'similar' in ingred[0]:
# if there's only one similar drink
pump_similar = [pump_similar for pump_similar in pump_list if pump_similar['description'] == ingred[0]['similar']]
if len(pump_similar) > 0:
ingreds['pump-similar'] = pump_similar[0]['value']
ingreds['value-similar'] = ingred[0]['similar']
# if there's a list of similar drinks
for ingreds_similar in ingred[0]['similar']:
pump_similar = [pump_similar for pump_similar in pump_list if pump_similar['description'] == ingreds_similar]
if len(pump_similar) > 0:
ingreds['pump-similar'] = pump_similar[0]['value']
ingreds['value-similar'] = ingreds_similar
if request.args.get('json') == "true":
return jsonify(drink[0]['ingredients'])
else:
return drink[0]['ingredients']
@app.route('/drink/character/<drink_val>', methods=['GET'])
def calcDrinkCharacter(drink_val):
alcohol = 0
acid = 0
bitter = 0
sweet = 0
volume = 0
tot_vol = 0
abv = 0
character = []
drink = [drink for drink in drink_list if drink['value'] == drink_val]
for ingreds in drink[0]['ingredients']:
for attribute, value in ingreds.items():
if (attribute == "amount"):
volume = value
tot_vol = tot_vol + value
if (attribute == "value" and value != "garnish"):
ingred = [ingred for ingred in ingred_list if ingred['value'] == value]
alcohol = alcohol + ((ingred[0]['abv'] * volume) / 100)
acid = acid + (ingred[0]['acid'] * volume)
sweet = sweet + (ingred[0]['sweet'] * volume)
bitter = bitter + (ingred[0]['bitter'] * volume)
abv = (alcohol / tot_vol) * 100
balance = (sweet - acid * 10)
bitter = (bitter - sweet - acid * 10)
if (abv >= 20):
character.append("boozy")
if (balance < -20):
character.append("acidic")
elif (balance > 20):
charcter.append("sweet")
else:
character.append("balanced")
if (bitter >= 20):
character.append("bitter")
return jsonify({'character': character, 'alcohol': alcohol, 'abv': abv, 'acid': acid, 'sweet': sweet, 'bitter': bitter, 'balance': balance, 'tot_vol': tot_vol})
@app.route("/ingreds")
def listIngreds():
return jsonify(ingred_list)
@app.route('/ingred/<ingred_val>', methods=['GET'])
def showIngred(ingred_val):
ingred = [ingred for ingred in ingred_list if ingred['value'] == ingred_val]
if len(ingred) == 0:
abort(404)
return jsonify([ingred[0]])
@app.route('/ingred/pump/<ingred_val>', methods=['GET'])
def showIngredPump(ingred_val):
pump = [pump for pump in pump_list if pump['description'] == ingred_val]
if len(pump) == 0:
abort(404)
return jsonify([pump[0]])
@app.route("/pumps")
def listPumps():
return jsonify(pump_list)
@app.route('/pump/<int:pump_val>', methods=['GET'])
def showPump(pump_val):
pump = [pump for pump in pump_list if pump['value'] == pump_val]
if len(pump) == 0:
abort(404)
return jsonify([pump[0]])
@app.route('/pump/update/<int:pump_val>', methods=['PUT', 'POST'])
def updatePump(pump_val):
pump = [pump for pump in pump_list if pump['value'] == pump_val]
if len(pump) == 0:
abort(404)
if not request.json:
abort(400)
if 'description' in request.json and type(request.json['description']) != unicode:
abort(400)
pump[0]['description'] = request.json.get('description', pump[0]['description'])
json.dump(pump_list, open('/home/pi/Smart-Bartender/pump_config.json', 'w'))
return jsonify([pump[0]])
@app.route('/pin/<int:pin>/<int:waitTime>', methods=['GET'])
def triggerPin(pin, waitTime):
GPIO.output(pin, GPIO.LOW)
time.sleep(waitTime)
GPIO.output(pin, GPIO.HIGH)
if __name__ == '__main__':
app.run(debug=True, port=80, host='0.0.0.0')