-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
462 lines (370 loc) · 15.6 KB
/
app.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
from flask import Flask
from flask import render_template, redirect, url_for
from flask import request
from flask import jsonify
from werkzeug.utils import secure_filename
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from threading import Thread
from bar import Bar
UPLOAD_FOLDER = os.path.dirname(__file__) + '/static/pictures/temp/'
PICTURE_FOLDER = os.path.dirname(__file__) + '/static/pictures/'
rel_folder = 'pictures/temp/'
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
print(PICTURE_FOLDER)
print(__name__)
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
'''BARTENDER2 SECTION'''
'''
bar = Bar()
bar.create_pump(11)
bar.create_pump(14, flowrate=1000)
bar.create_pump(17)
bar.create_pump(23)
bar.create_pump(25)
bar.create_ingredient(name="Wodka", percentage=45, bottlesize=700)
bar.ingredients[0].assignPump(bar.pumps[0])
bar.create_ingredient(name="Ginger Beer", percentage=0, bottlesize=500)
bar.ingredients[1].assignPump(bar.pumps[1])
bar.create_ingredient(name="Gin", percentage=45, bottlesize=700)
bar.ingredients[2].assignPump(bar.pumps[3])
bar.create_ingredient(name='Red Bull', percentage=0, bottlesize=200)
bar.ingredients[3].assignPump(bar.pumps[2])
bar.create_ingredient(name='Tonic Water', percentage=0, bottlesize=500)
bar.ingredients[4].assignPump(bar.pumps[4])
bar.create_drink(name="Moscow Mule", type='Longdrink', picture='moscowMule.png', description="This is a drink from Moscow.")
bar.drinks[0].create_version(shortname='single', ingredients=[bar.ingredients[0], bar.ingredients[1]], proportions=[40,60], glassize=200)
bar.drinks[0].create_version(shortname='double', ingredients=[bar.ingredients[0], bar.ingredients[1]], proportions=[50,50], glassize=200)
bar.drinks[0].create_version(shortname='big', ingredients=[bar.ingredients[0], bar.ingredients[1]], proportions=[60,40], glassize=8000)
bar.create_drink(name='Wodka Bull', type='Longdrink', picture='wodkaEnergy.png', description='This is a drink from Austria.')
bar.drinks[1].create_version(shortname='single', ingredients=[bar.ingredients[0], bar.ingredients[3]], proportions=[30,70], glassize=200)
bar.create_drink(name='Gin Tonic', type='Longdrink', picture='ginTonic.png', description='This is a fancy hyped drink.')
bar.drinks[2].create_version(shortname='single', ingredients=[bar.ingredients[2], bar.ingredients[4]], proportions=[30,70], glassize=200)
bar.create_pump(27)
bar.create_pump(32)
bar.create_ingredient(name="Liqueur 43", percentage=31, bottlesize=700)
bar.ingredients[5].assignPump(bar.pumps[5])
bar.create_ingredient(name="Milch", percentage=0, bottlesize=1000)
bar.ingredients[6].assignPump(bar.pumps[6])
bar.create_drink(name="Milch 43", type='Longdrink', picture='milch43.png', description="This is Vreni's and Felix' drink ❤️.")
bar.drinks[3].create_version(shortname='single', ingredients=[bar.ingredients[5], bar.ingredients[6]], proportions=[30,70], glassize=200)
bar.save_config('config')
'''
'''BARTENDER2 SECTION END'''
# bar = reload_config('config')
bar = Bar(folder='config')
myIP = '127.0.0.1'
def isRpi():
if request.environ.get('HTTP_X_REAL_IP', request.remote_addr) == myIP:
return True
return False
@app.route("/")
def startpage():
bar.save_config('config')
if request.environ.get('HTTP_X_REAL_IP', request.remote_addr) == myIP:
print('This is the Raspberry device!')
return render_template('index.html', drinks=bar.drinks)
@app.route("/detail", methods=['GET'])
def detailview():
iddrink = int(request.args.get('iddrink'))
print(iddrink)
drink = bar.drinks[iddrink]
return render_template('detail.html', drink=drink, iddrink=iddrink)
@app.route("/make", methods=['POST'])
def makeview():
iddrink = int(request.form.get('iddrink'))
idversion = int(request.form.get('idversion'))
duration = bar.drinks[iddrink].versions[idversion].duration
bar.drinks[iddrink].versions[idversion].make()
bar.save_config('config')
return jsonify({'text': duration})
@app.route("/ingredients", methods=['GET','POST'])
def ingredientsview():
if request.form.get('toast'):
toast = request.form.get('toast')
else:
toast = ''
print(isRpi())
return render_template('/ingredients.html', ingredients=bar.ingredients, pumps=bar.pumps, bar=bar, toast=toast, isRpi=isRpi())
@app.route("/ingredients/changeBottleForm", methods=['POST'])
def changeBottleFormSnippet():
idingredient = int(request.form.get('idingredient'))
return render_template('snippets/changeBottleForm.html', idingredient=idingredient, ingredient=bar.ingredients[idingredient], pumps=bar.pumps, bar=bar, isRpi=isRpi())
@app.route("/submit/newIngredient", methods=['POST'])
def submitNewIngredient():
name = request.form.get('name')
pump = request.form.get('pump')
percentage = int(request.form.get('percentage'))
bottlesize = int(float(request.form.get('bottlesize')) * 1000)
if pump == "":
pumpid = None
else:
pumpid = int(pump)
bar.create_ingredient(name=name, percentage=percentage, bottlesize=bottlesize, pump=pumpid)
bar.save_config('config')
return jsonify({'error': 0})
@app.route("/submit/changeIngredient", methods=['POST'])
def submitChangeIngredient():
idingredient = int(request.form.get('idingredient'))
name = request.form.get('name')
pumpid = request.form.get('pump')
percentage = int(request.form.get('percentage'))
bottlesize = int(float(request.form.get('bottlesize')) * 1000)
print(str(idingredient)+ " " + name + " " + pumpid + " " + str(percentage) + " " + str(bottlesize))
if pumpid == "":
pump = None
else:
pump = bar.pumps[int(pumpid)]
bar.ingredients[idingredient].change_Ingredient(name=name, pump=pump, percentage=percentage, bottlesize=bottlesize)
bar.save_config('config')
return jsonify({'error': 0})
@app.route("/submit/deleteIngredient", methods=['POST'])
def submitDeleteIngredient():
idingredient = int(request.form.get('idingredient'))
bar.delete_ingredient(ingredient=bar.ingredients[idingredient])
bar.save_config('config')
return jsonify({'error': 0})
@app.route("/pumps", methods=['GET','POST'])
def pumpsview():
if request.form.get('toast'):
toast = request.form.get('toast')
else:
toast = ''
return render_template('/pumps.html', pumps=bar.pumps, bar=bar, toast=toast, isRpi=isRpi())
@app.route("/pumps/changePumpForm", methods=['POST'])
def changePumpFormSnippet():
idpump = int(request.form.get('idpump'))
return render_template('snippets/changePumpForm.html', idpump=idpump, pump=bar.pumps[idpump], bar=bar, isRpi=isRpi())
@app.route("/submit/newPump", methods=['POST'])
def submitNewPump():
pin = int(request.form.get('pin'))
flowrate = request.form.get('flowrate')
if flowrate == "":
bar.create_pump(pin=pin)
else:
bar.create_pump(pin=pin, flowrate=int(flowrate))
bar.save_config('config')
return jsonify({'error': 0})
@app.route("/submit/changePump", methods=['POST'])
def submitChangePump():
try:
pin = int(request.form.get('pin'))
flowrate = request.form.get('flowrate')
idpump = int(request.form.get('idpump'))
if not flowrate == "":
bar.pumps[idpump].flowrate = int(flowrate)
bar.pumps[idpump].pin = pin
bar.save_config('config')
return jsonify({'error': 0})
except:
return jsonify({'error': 1})
@app.route("/submit/deletePump", methods=['POST'])
def submitDeletePump():
idpump = int(request.form.get('idpump'))
bar.delete_pump(bar.pumps[idpump])
bar.save_config('config')
return jsonify({'error': 0})
@app.route("/drinks", methods=['GET', 'POST'])
def drinks(toast=''):
if request.form.get('toast'):
toast = request.form.get('toast')
elif toast != '':
toast = toast
else:
toast = ''
filelist = os.listdir(PICTURE_FOLDER)
for fichier in filelist[:]: # filelist[:] makes a copy of filelist.
if not (fichier.endswith(".png")):
filelist.remove(fichier)
print(filelist)
return render_template('/drinks/drinks.html', toast=toast, pictures=filelist, drinks=bar.drinks, isRpi=isRpi())
@app.route("/drinks/chooseImage")
def cropperChooseImage():
return render_template('drinks/chooseImage.html', isRpi=isRpi())
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route("/drinks/cropImage", methods=['POST'])
def cropperCropImage():
if request.method == 'POST':
if request.form.get('path'):
print(request.form.get('path'))
return render_template('drinks/cropImage.html', path=request.form.get('path'))
# check if the post request has the file part
if 'file' not in request.files:
print('No file part')
return render_template('drinks/drinks.html')
file = request.files['file']
# if user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
print('No selected file')
return render_template('drinks/drinks.html')
if file and allowed_file(file.filename):
print("works")
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return render_template('drinks/drinks.html', path=rel_folder+filename)
return render_template('drinks/drinks.html')
@app.route("/submit/newDrink", methods=['POST'])
def submitNewDrink():
try:
name = request.form.get('name')
type = request.form.get('type')
desc = request.form.get('desc')
pic = request.form.get('picture')
if name == '' or type == '' or pic == '':
raise Exception()
print(name + " " + type + " " + desc + " " + pic)
bar.create_drink(name=name, type=type, picture=pic, description=desc)
bar.save_config('config')
return drinks('Successfully added new drink!')
except:
return drinks('Something went wrong..')
@app.route("/submit/deleteDrink", methods=['POST'])
def submitDeleteDrink():
try:
iddrink = request.form.get('iddrink')
if iddrink == "":
raise Exception()
iddrink = int(iddrink)
bar.delete_drink(drink=bar.drinks[iddrink])
bar.save_config('config')
return drinks('Successfully delete drink!')
except:
return drinks('Something went wrong..')
@app.route("/drinks/changeDrinkForm", methods=['POST'])
def changeDrinkFormSnippet():
iddrink = int(request.form.get('iddrink'))
filelist = os.listdir(PICTURE_FOLDER)
for fichier in filelist[:]: # filelist[:] makes a copy of filelist.
if not (fichier.endswith(".png")):
filelist.remove(fichier)
return render_template('drinks/changeDrinkForm.html', iddrink=iddrink, drink=bar.drinks[iddrink], pictures=filelist, bar=bar, isRpi=isRpi())
@app.route("/submit/changeDrink", methods=['POST'])
def submitChangeDrink():
try:
iddrink = int(request.form.get('iddrink'))
name = request.form.get('name')
type = request.form.get('type')
desc = request.form.get('desc')
pic = request.form.get('picture')
if name == '' or type == '' or pic == '':
raise Exception()
print(name + " " + type + " " + desc + " " + pic)
bar.drinks[iddrink].name = name
bar.drinks[iddrink].type = type
bar.drinks[iddrink].picture = pic
bar.drinks[iddrink].description = desc
bar.save_config('config')
return drinks('Successfully changed drink!')
except:
return drinks('Something went wrong..')
@app.route("/submit/newVersion", methods=['POST'])
def submitNewVersion():
try:
iddrink = request.form.get('iddrink')
shortname = request.form.get('shortname')
size = request.form.get('size')
ings = request.form.getlist('ing[]')
props = request.form.getlist('prop[]')
if iddrink == "" or shortname == "" or size == "" or len(ings) == 0 or len(props) == 0:
return drinks('You have to fillout all fields.')
iddrink = int(iddrink)
size = int(size)
ingredients = []
proportions = []
for ing, prop in zip(ings, props):
ingredients.append(bar.ingredients[int(ing)])
proportions.append(int(prop))
print(proportions)
print(ingredients)
bar.drinks[iddrink].create_version(shortname=shortname, ingredients=ingredients, proportions=proportions, glassize=size)
bar.save_config('config')
return drinks('Successfully added new drink!')
except:
return drinks('Something went wrong..')
@app.route("/submit/deleteVersion", methods=['POST'])
def submitDeleteVersion():
try:
iddrink = request.form.get('iddrink')
idversion = request.form.get('idversion')
if iddrink == "" or idversion == "":
raise Exception()
iddrink = int(iddrink)
idversion = int(idversion)
print(iddrink)
print(idversion)
bar.drinks[iddrink].delete_version(versionid=idversion)
bar.save_config('config')
return drinks('Successfully delete version!')
except:
return drinks('Something went wrong..')
@app.route("/swapping")
def swapView(toast=""):
if request.method == 'GET':
toast = request.args.get('toast')
return render_template('/swapping.html', toast=toast, ingredients=bar.ingredients)
@app.route("/tools/swap", methods=['GET','POST'])
def swapAction():
try:
iding = "";
if request.method == 'POST':
iding = request.form.get('iding')
else:
iding = request.args.get('iding')
if iding == "":
raise Exception()
iding = int(iding)
bar.ingredients[iding].swapBottle()
bar.save_config('config')
return redirect(url_for('swapView', toast='Bottle refilled.'))
return swapView('Bottle refilled.')
except:
return swapView('Something went wrong..')
@app.route("/cleaning")
def cleanView(toast=""):
return render_template('/cleaning.html', toast=toast, pumps=bar.pumps)
@app.route("/tools/clean", methods=['POST'])
def cleanAction():
try:
iding = request.form.get('iding')
if iding == "":
raise Exception()
iding = int(iding)
bar.pumps[iding].clean()
bar.save_config('config')
return cleanView('Pump ' + str(iding+1) + ' cleaned.')
except:
return cleanView('Something went wrong..')
@app.route("/crop", methods=['POST'])
def crop():
from PIL import Image
path = request.form.get('path');
coords = (float(request.form.get('x')), float(request.form.get('y')), float(request.form.get('x2')), float(request.form.get('y2')))
width = float(request.form.get('w'))
name = request.form.get('name')
image_obj = Image.open('static/' + path)
print(coords)
cropped_image = image_obj.crop(coords)
cropped_image.save('static/pictures/'+name)
delpath = os.path.dirname(__file__) + "/static/" + path
os.remove(delpath)
return jsonify({'error': 0})
def startFirefox():
driver = webdriver.Firefox()
driver.get("http://127.0.0.1:5000")
def startChrome():
chrome_options = Options()
chrome_options.add_argument("--kiosk")
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("http://127.0.0.1:5000")
if __name__ == "__main__":
t = Thread(target=startChrome)
t.start()
#app.run(debug=True)
#myIP = '10.2.212.43'
app.run(host=myIP, port=5000, debug=True)
#app.run(host='10.2.211.39', port=5000, debug=True)