-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.py
73 lines (61 loc) · 1.73 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
from socketserver import ThreadingMixIn
from http.server import BaseHTTPRequestHandler, HTTPServer
import threading
import RPi.GPIO as GPIO
from os import path
from sched import scheduler
from time import time, sleep
HOST_NAME = ''
PORT_NUMBER = 8080
PATH = path.dirname(path.abspath(__file__)) + "/"
UPDATE_DELAY = 60
def sensorEvent(channel=0):
global wc_busy
wc_busy = GPIO.input(17)
def timedUpdate():
global wc_busy
wc_busy = GPIO.input(17)
threading.Timer(UPDATE_DELAY, timedUpdate, ()).start()
def isWCBusy():
return not wc_busy
class RequestHandler(BaseHTTPRequestHandler):
def do_HEAD(s):
print("HEAD")
pass
def do_GET(s):
"""Respond to a GET request"""
s.send_response(200)
if s.path == "/occupation":
s.send_header("Content-type", "text/plain")
s.end_headers()
if isWCBusy() == True:
s.wfile.write(bytes("busy", "UTF-8"))
else:
s.wfile.write(bytes("free", "UTF-8"))
if s.path.endswith(("/", ".html", "css", ".js", ".png")):
if s.path == "/":
s.path = "/index.html"
elif s.path.endswith((".png", ".js")):
s.send_header("Cache-Control", "max-age=86400, must-revalidate")
s.end_headers()
f = open(PATH + s.path[1:], 'rb')
s.wfile.write(f.read())
f.close()
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
pass
if __name__ == '__main__':
global wc_busy
try:
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(17, GPIO.BOTH, callback=sensorEvent)
wc_busy = GPIO.input(17)
threading.Timer(UPDATE_DELAY, timedUpdate, ()).start()
httpd = ThreadedHTTPServer((HOST_NAME, PORT_NUMBER), RequestHandler)
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
finally:
GPIO.cleanup()