-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.py
306 lines (277 loc) · 10.1 KB
/
Main.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
#This program is the main program for boozebot and integrates all other programs
#Programmers: TRG
#Iteration: 0.5
#Last edited: 05/04/2019
#Created: 10/04/2019
#imports relevate libraries
import time #imports the time library
import Drink #imports the the drink class
import Data #imports the data file
import Update #imports the Dataupdate function set
import Arduino #imports Arduino communication library
from functools import partial
#threading
import threading
from queue import Queue
import VirtualQueue
#flask
from PIL import Image
from forms import uploadMenu, adminSettings, confirmOrder, adminLogin, addCredits, newUser, buyDrink, customDrink
from flask import Flask, render_template, url_for, flash, redirect, session, make_response
import os
app = Flask(__name__, template_folder="GUI", static_url_path="/GUI", static_folder="GUI")
app.config['SECRET_KEY'] = '5791628bb0b13ceh98dfjk7lp76dfde280ba245'
users = "Init/Users.csv"
menus = "Init/Menu.csv"
drinkQueue = Queue()
#Updates all data from files
def initializeMenu(InitFile="Standard"):
Update.updateCupType('Init/Cups.ini') #updates the cup volume list
Update.updateIngredientAlcohol('Init/AlcoholContent.ini') #updates the alcholic content of ingredients list
Update.updateIngredientPump('Init/' + InitFile + '/Pumps.ini') #updates the ingredient pumps
Update.updateIngredientList('Init/' + InitFile + '/Ingredients.ini') #updates a list of available ingredients
Update.updateMenu('Init/' + InitFile + '/Menu.ini') #updates the menu and recipe instructions
def startSession():
try:
if session['RUN'] != True:
initializeMenu()
session['OpenBar'] = False
session['CustomDrinks'] = False
session['AdminOveride'] = False
except:
session['RUN'] = True
initializeMenu()
session['OpenBar'] = False
session['CustomDrinks'] = False
session['AdminOveride'] = False
session['RUN'] = True
def closeAuth():
session.pop('Auth')
def generateAuth(_ID):
usersIN = open(users, 'r')
for line in usersIN:
if len(line) > 5:
ID, Credit, Name, Access = line.split(', ')
if _ID == ID:
if Access.strip('\n') == "Admin":
session['Auth'] = True
else:
flash('No Admin Access', 'danger')
usersIN.close()
def checkAuth():
return session.get('Auth')
def purchaseDrink(_ID, drink):
usersIN = open(users, 'r')
usersOUT = open('Init/out.csv', 'w')
for line in usersIN:
if len(line) > 5:
ID, Credit, Name, Access = line.split(', ')
if _ID == ID:
if session['AdminOveride'] == True and Access == 'Admin':
submitDrink(drink)
elif int(Credit) > 0:
Credit = int(Credit) - 1
line = ID + ', ' + str(Credit) + ', ' + Name + ', ' + Access + '\n'
submitDrink(drink)
else:
flash('No Credit', 'danger')
usersOUT.write(line)
usersIN.close()
usersOUT.close()
try:
os.remove(users)
os.rename('Init/out.csv', users)
except PermissionError:
print(users + 'is running in a higher process')
def submitDrink(drink='NULL'):
print(drink)
Data.menu[drink].setRecipeVolume()
Data.menu[drink].setRecipeInstructions()
ArduinoQueue.queue.put(Data.menu[drink].recipeInstructions)
def saveFile(file, location, name='NULL'):
try:
os.mkdir(location)
print("Directory " , location, " Created ")
except FileExistsError:
print("Directory " , location, " already exists")
if name == 'NULL':
name = file
fileIN = open(file, 'r')
fileOUT = open(location + name, 'w')
for line in fileIN:
fileOUT.write(line)
fileIN.close()
fileOUT.close()
def saveMenu(_menu, _ingredients, _pumps, name):
menusIN = open(menus, 'r')
menuNUM = 0
for line in menusIN:
if name in line:
menuNNUM += 1
if menuNUM > 0:
flash((name + ' already exists. creating ' + name + str(menuNUM)), 'danger')
name = name + str(menuNUM)
menusIN.close()
menusOUT = open(menus, 'a')
menusOUT.write(name)
menusOUT.close()
location = 'Init/' + name + '/'
try:
os.mkdir(location)
print("Directory " , location, " Created ")
except FileExistsError:
print("Directory " , location, " already exists")
saveFile(_menu, location, 'Menu.ini')
saveFile(_ingredients, location, 'Ingredients.ini')
saveFile(_pumps, location, 'Pumps.ini')
@app.route("/home")
@app.route("/")
def home():
startSession()
return redirect(url_for('setting'))
@app.route("/menu")
def menu():
return render_template('menu.html', menu=Data.menu, title='Menu')
@app.route("/menu/<drinkName>", methods=['GET', 'POST'])
def drink(drinkName):
drinkExists = False
if session['OpenBar'] == True:
form = confirmOrder()
else:
form = buyDrink()
if form.confirm.data:
for drink in Data.menu:
if Data.menu[drink].name == drinkName:
drinkExists = True
if session['OpenBar'] == True:
submitDrink(drink)
else:
purchaseDrink(form.ID.data, drink)
return redirect(url_for('menu'))
for drink in Data.menu:
if Data.menu[drink].name == drinkName:
drinkExists = True
return render_template('confirm.html', drink=Data.menu[drink], form=form)
if drinkExists == False:
return redirect(url_for('drinkMissing'))
return render_template('menu.html', menu=Data.menu)
@app.route("/purchase/credits", methods=['GET', 'POST'])
def buyCredit():
form = addCredits()
if form.ID.data:
generateAuth(form.adminID.data)
if checkAuth:
closeAuth()
usersIN = open(users, 'r')
usersOUT = open('Init/out.csv', 'w')
for line in usersIN:
if len(line) > 5:
ID, Credit, Name, Access = line.split(', ')
if form.ID.data == ID:
Credit = int(Credit) + form.credit.data
line = ID + ', ' + str(Credit) + ', ' + Name + ', ' + Access
usersOUT.write(line)
usersIN.close()
usersOUT.close()
try:
os.remove(users)
os.rename('Init/out.csv', users)
except PermissionError:
print(users + 'is running in a higher process')
flash((str(Credit) + "'s purchased"), 'success')
return redirect(url_for('menu'))
return render_template('credits.html', form=form)
@app.route("/missing")
def drinkMissing():
return render_template('missing.html')
@app.route("/register", methods=['GET', 'POST'])
def register():
form = newUser()
if form.ID.data:
generateAuth(form.adminID.data)
if checkAuth:
closeAuth()
usersIN = open(users, 'r')
exists = False
for line in usersIN:
if len(line) > 5:
ID, Credit, Name, Access = line.split(', ')
if ID == form.ID.data:
exists = True
usersIN.close()
if exists == False:
usersIN = open(users, 'a')
line = form.ID.data + ', ' + str(form.credit.data) + ', ' + form.name.data + ', User\n'
usersIN.write(line)
usersIN.close()
flash((form.ID.data + ' registered successfully'), 'success')
return redirect(url_for('menu'))
else:
flash((form.ID.data + ' has already registered'), 'danger')
return render_template('register.html', form=form)
@app.route("/login", methods=['GET', 'POST'])
def login():
form = adminLogin()
if form.ID.data:
generateAuth(form.ID.data)
if checkAuth():
flash('logged in successfully', 'success')
return redirect(url_for('setting'))
return render_template('login.html', form=form, title='login')
@app.route("/logout")
def logout():
closeAuth()
return redirect(url_for('menu'))
@app.route("/settings", methods=['GET', 'POST'])
def setting():
form = adminSettings()
if checkAuth() == True:
#SETTINGS
session['OpenBar'] = False
session['CustomDrinks'] = False
session['AdminOveride'] = False
if form.confirm.data:
if form.openBar.data:
session['OpenBar'] = True
else:
session['OpenBar'] = False
if form.custom.data:
session['CustomDrinks'] = True
else:
session['CustomDrinks'] = False
if form.adminOveride.data:
session['AdminOveride'] = True
else:
session['AdminOveride'] = False
return redirect(url_for('menu'))
return render_template('settings.html', form=form, title='settings')
@app.route("/select/menu", methods=['GET', 'POST'])
def selectMenu():
menusIN = open(menus, 'r')
list = []
for line in menusIN:
line = line.strip('\n')
list.append(line)
return render_template('select-menu.html', list=list, title='settings')
@app.route("/select/menu/<menuName>", methods=['GET', 'POST'])
def initMenu(menuName):
initializeMenu(menuName)
return redirect(url_for('menu'))
@app.route("/upload/menu", methods=['GET', 'POST'])
def newMenu():
form = uploadMenu()
if form.confirm.data:
absFilePath = form.location.data
if absFilePath[-1] != '/':
absFilePath = absFilePath + '/'
menu = absFilePath + 'Menu.ini'
ingredients = absFilePath + 'Ingredients.ini'
pumps = absFilePath + 'Pumps.ini'
saveMenu(menu, ingredients, pumps, form.name.data)
return redirect(url_for('selectMenu'))
return render_template('upload-menu.html', form=form, title='settings')
if __name__ == '__main__':
initializeMenu()
ArduinoQueue = VirtualQueue.ArduinoThread(drinkQueue)
ArduinoQueue.start()
app.run(debug=True)