-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
120 lines (99 loc) · 3.53 KB
/
test.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
from http.server import BaseHTTPRequestHandler, HTTPServer
import threading
from time import sleep
from gpiozero import LED
from gpiozero import Button
from signal import pause
hostName = "0.0.0.0"
serverPort = 8080
taso_lock = threading.Lock()
taso1, taso2, taso3, taso4 = 0, 0, 0, 0
def setLed(r, g, b, duration):
red_led = LED(2, active_high=False)
green_led = LED(3, active_high=False)
blue_led = LED(4, active_high=False)
try:
red_led.value = r
green_led.value = g
blue_led.value = b
sleep(duration)
finally:
red_led.close()
green_led.close()
blue_led.close()
def changeLed(path: str):
c = path[path.__len__() - 1]
print("led is now " + str(c))
if c == 'R':
setLed(1, 0, 0, 2)
elif c == 'G':
setLed(0, 1, 0, 2)
elif c == 'B':
setLed(0, 0, 1, 2)
class MyServer(BaseHTTPRequestHandler):
def do_GET(self):
global taso1, taso2, taso3, taso4
if self.path.startswith("/led"):
changeLed(self.path)
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(bytes("<html><head><title>Ruokalistaäänestys</title><meta charset=\"utf-8\"></head>", "utf-8"))
self.wfile.write(bytes("<body>", "utf-8"))
self.wfile.write(bytes(f"""
<h2>
Punainen (taso1) = {taso1}
</h2>
<h2>
oranssi (taso2) = {taso2}
</h2>
<h2>
keltainen (taso3) = {taso3}
</h2>
<h2>
vihreä (taso4) = {taso4}
</h2>
<br>
<button onclick="location.href = '/led?c=R';" id="myButton1" class="float-left submit-button" >LED RED</button>
<button onclick="location.href = '/led?c=G';" id="myButton1" class="float-left submit-button" >LED GREEN</button>
<button onclick="location.href = '/led?c=B';" id="myButton1" class="float-left submit-button" >LED BLUE</button>
""", "utf-8"))
self.wfile.write(bytes("</body></html>", "utf-8"))
def main(url, token):
#pinnit numeroitu Broadcom järjestelmällä, lisää https://gpiozero.readthedocs.io/en/stable/recipes.html#pin-numbering
red_button = Button(6, hold_time=0.05)
red_button.when_held = lambda: aanesta(url, token, 1)
orange_button = Button(13, hold_time=0.05)
orange_button.when_held = lambda: aanesta(url, token, 2)
yellow_button = Button(19, hold_time=0.05)
yellow_button.when_held = lambda: aanesta(url, token, 3)
green_button = Button(26, hold_time=0.05)
green_button.when_held = lambda: aanesta(url, token, 4)
pause()
def aanesta(url, token, taso):
global taso1, taso2, taso3, taso4
with taso_lock:
print(f"testi äänestetty, TASO:{taso} ")
if taso == 1:
taso1 += 1
elif taso == 2:
taso2 += 1
elif taso == 3:
taso3 += 1
elif taso == 4:
taso4 += 1
sleep(0.8)
if __name__ == "__main__":
webServer = HTTPServer((hostName, serverPort), MyServer)
print("Server started http://%s:%s" % (hostName, serverPort))
# Create a new thread for the webserver function
webserver_thread = threading.Thread(target=webServer.serve_forever)
# Start the webserver thread
webserver_thread.start()
# Call the main function in the main thread
main("url", "token")
# Wait for the webserver thread to finish
webserver_thread.join()
# Close the webserver
webServer.server_close()
print("Server stopped.")