-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
136 lines (105 loc) · 3.79 KB
/
server.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
from http.server import HTTPServer, BaseHTTPRequestHandler
import os
import json
import subprocess
import time
from urllib.parse import quote
import socket
import board
import digitalio
import random
import glob
from datetime import datetime
homedir="/home/pi/MakerspaceDoor"
port = 8000
hostname=socket.gethostname()
ipAddr = socket.gethostbyname(hostname)
print(f"Serving from: http://{hostname}.local:{port}")
print(f"at IP: http://{ipAddr}:{port}")
with open(f"{homedir}/log.txt","w") as f:
f.write(f"Serving from: http://{hostname}.local:{port} at IP: http://{ipAddr}:{port}")
''' Function to convert the post data to an array for easier use'''
def postDataToArray(postData):
raw_text = postData.decode("utf8")
print("Raw")
data = json.loads(raw_text)
return data
''' Handle GET and POST requests to the server'''
class uHTTPRequestHandler(BaseHTTPRequestHandler):
def _set_headers(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
if self.path == '/':
self.path = f"/{homedir}/index.html"
print ("path: "+self.path[1:])
try:
file_to_open = open(self.path[1:]).read()
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bytes(file_to_open, 'utf-8'))
except:
self.send_response(404)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(b'404 - Not Found')
def do_POST(self):
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
data = postDataToArray(post_data)
'''
data consists of
data['action'] and
data['value']
'''
self._set_headers()
print(data)
rData = {}
rData['item'] = ""
rData['status'] = ""
if data['action'] == "getDoor":
now = datetime.now()
print(now.ctime())
rData['item'] = "getDoor"
rData['status'] = s1.value
self.wfile.write(bytes(json.dumps(rData), 'utf-8'))
httpd = HTTPServer(('', port), uHTTPRequestHandler)
# httpd.serve_forever()
os.system(f"sudo python3 {homedir}/led_startup.py &")
os.system('amixer cset numid=1 100%')
os.system(f"cvlc --play-and-exit {homedir}/portal_start.mp3")
# subprocess.Popen('amixer cset numid=1 100%', shell=True)
# subprocess.Popen("cvlc --play-and-exit /home/pi/portal/portal_start.mp3", shell=True)
# filelist = os.listdir("/home/pi/portal/Sounds/*.mp3")
filelist = glob.glob (f"{homedir}/Sounds/*.mp3")
print(filelist)
s1 = digitalio.DigitalInOut(board.D17)
s1.direction = digitalio.Direction.INPUT
s1.pull = digitalio.Pull.DOWN
isopen = s1.value
print (isopen)
while True:
httpd.handle_request()
if (s1.value != isopen):
isopen = s1.value
if isopen == True:
print("closed")
os.system(f"sudo python3 /home/pi/door/led_close.py &")
else:
print("open")
os.system(f"sudo python3 /home/pi/door/led_open.py &")
soundFile = f"{random.choices(filelist, weights = (1, 30, 10), k = 1)[0]}"
cmd = f"cvlc --play-and-exit {soundFile}"
print ("sound",cmd)
os.system(cmd)
time.sleep(0.1)
# while True:
# httpd.handle_request()
# now = time.localtime()
# print(f"Time: {now.tm_hour}:{now.tm_min} | {alarmTime.hr}:{alarmTime.min}")
# if (now.tm_hour == alarmTime.hr) and (now.tm_min == alarmTime.min) and not alarmOn:
# print("We have alarm!")
# alarmOn = True
# rhythmboxCommand("play")