-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebapprework.py
More file actions
143 lines (104 loc) · 5.4 KB
/
webapprework.py
File metadata and controls
143 lines (104 loc) · 5.4 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
from flask import Flask, render_template, request
from time import time, sleep
import RPi.GPIO as GPIO
class Machine:
def __init__(self):
self.workingTillTime = 0
self.bottles = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
self.pins = [7, 8, 10, 12, 11, 13, 15, 16, 19, 21, 22, 23]
self.pourEndTimes = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
self.setupGPIO()
def setupGPIO(self):
"""Set up the GPIO pins"""
GPIO.setmode(GPIO.BOARD)
for pinNum in self.pins:
GPIO.setup(pinNum, GPIO.OUT)
def addBottleSeconds(self, bottle, seconds):
"""Adjust the end time for closing the pour for a bottle for a set amount of time in seconds
'bottle' should be an integer from 0-11 selecting the ingredient bottle
'seconds' should be a float of the amount of seconds to pour
"""
self.pourEndTimes[bottle] = time() + seconds
if self.pourEndTimes[bottle] > self.workingTillTime:
self.workingTillTime = self.pourEndTimes[bottle]
def openBottle(self, bottle):
"""Open the selected bottle by setting the GPIO pin to True"""
GPIO.output(self.pins[bottle], True)
def closeBottle(self, bottle):
"""Close the selected bottle by setting the GPIO pin to False"""
GPIO.output(self.pins[bottle], False)
def checkBottleOpen(self, bottle):
"""Return whether the selected GPIO pin is True or False"""
return GPIO.input(self.pins[bottle])
def checkBottlesForClosing(self):
"""Check if any bottles should be closed due to time lapsed. Close them if appropriate.
Return True if not finished, return False if finished."""
stillGoing = False
print("\nChecking bottles begin")
for bottle in self.bottles:
if self.checkBottleOpen(bottle):
stillGoing = True
if self.pourEndTimes[bottle] < time():
self.closeBottle(bottle)
print("\nChecking bottles end. stillGoing: " + str(stillGoing))
return stillGoing
def startRecipe(self, recipe):
"""Start pouring drinks appropriately for the given recipe.
It should open the appropriate bottles, then set the times they should close.
'recipe' should be a length-12 list of floats denoting the amount of seconds necessary per bottle.
"""
for bottle in self.bottles:
pourTime = recipe[bottle]
if pourTime > 0:
self.addBottleSeconds(bottle, pourTime)
self.openBottle(bottle)
# Initialising the machine class instance
machine = Machine()
# Writing down the recipes (in seconds per bottle)
recipes = {"1": [1,2,3,4,5,6,7,8,9,10,11,12],
"2": [1,1,1,1,1,1,3,3,3,3,3,3],
"3": [0,0,0,0,0,0,0,0,0,0,2,2],
"4": [1,2,3,1,2,3,1,2,3,0,0,4.0243]}
# Initialising Flask
app = Flask(__name__)
# This is a decorator that makes the main() function a Flask function - it can run a website.
@app.route('/', methods=['POST', 'GET'])
def main(): # This is what's called when the page is searched for
# This checks for information coming from the page in the form of a post request.
if request.method == 'POST':
# Finding the times both this script and the web page are reporting
pythonTime = round(time()*1000)
javascriptTime = int(str(request.data)[2:-1].split(",")[1])
# Getting the info of what drink the user wants!
drinkID = str(str(request.data)[2:-1].split(",")[0])
# Printing the received data
print("Req Data: " + str(request.data)[2:-1])
# Printing out both this script's and the webpage's reported times
print("py: " + str(pythonTime) + " js: " + str(javascriptTime))
print("Difference (py - js) = " + str(pythonTime - javascriptTime))
if (javascriptTime > machine.workingTillTime):
print("Let's mix up a drink: ")
print(drinkID)
machine.startRecipe(recipes[drinkID])
print("recipe started!")
print("Are the bottles all closed? " + str(machine.checkBottlesForClosing()))
# This continuously checks whether bottles should be closed, and returns false if they're all closed.
while machine.checkBottlesForClosing():
# Sleep a little to allow the pi to do things other than a really fast loop.
# I think not doing this is what caused an earlier version to crash.
print("presleep")
sleep(0.3) # <-- CHANGE THIS! It's too high a number atm (for testing), maybe put 0.05?
# Debugging statements
print("\nTest iteration. Still not finished!")
print("Now: " + str(time()) + " || End times: " + str(machine.pourEndTimes))
print([["Closed", "Open"][machine.checkBottleOpen(bottle)] for bottle in machine.bottles])
print("Drink finished pouring!")
else:
print("Currently mixing a drink! ")
print("Last req method statement")
print("pre-return")
return render_template('index.html')
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=80)
print("End of file!")
#If stuck in a stupid loop of not working type in localhost:80/index.html and it won't work but it seems to reset it somewhat